painted_overlay_scrollbar_layer_unittest.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "cc/layers/painted_overlay_scrollbar_layer.h"
  5. #include "cc/animation/animation_host.h"
  6. #include "cc/test/fake_layer_tree_host.h"
  7. #include "cc/test/fake_layer_tree_host_client.h"
  8. #include "cc/test/fake_scrollbar.h"
  9. #include "cc/test/test_task_graph_runner.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace cc {
  12. namespace {
  13. class MockScrollbar : public FakeScrollbar {
  14. public:
  15. MockScrollbar() {
  16. set_should_paint(true);
  17. set_has_thumb(true);
  18. set_is_overlay(true);
  19. }
  20. void PaintPart(PaintCanvas*, ScrollbarPart part, const gfx::Rect&) override {
  21. if (part == ScrollbarPart::TRACK_BUTTONS_TICKMARKS)
  22. paint_tickmarks_called_ = true;
  23. }
  24. bool UsesNinePatchThumbResource() const override { return true; }
  25. gfx::Size NinePatchThumbCanvasSize() const override {
  26. return gfx::Size(10, 10);
  27. }
  28. bool PaintTickmarksCalled() { return paint_tickmarks_called_; }
  29. void SetPaintTickmarksCalled(bool called) {
  30. paint_tickmarks_called_ = called;
  31. }
  32. private:
  33. ~MockScrollbar() override = default;
  34. bool paint_tickmarks_called_ = false;
  35. };
  36. TEST(PaintedOverlayScrollbarLayerTest, PaintTickmarks) {
  37. FakeLayerTreeHostClient fake_client_;
  38. TestTaskGraphRunner task_graph_runner_;
  39. auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  40. auto layer_tree_host = FakeLayerTreeHost::Create(
  41. &fake_client_, &task_graph_runner_, animation_host.get());
  42. auto scrollbar = base::MakeRefCounted<MockScrollbar>();
  43. scrollbar->set_has_tickmarks(false);
  44. scoped_refptr<PaintedOverlayScrollbarLayer> scrollbar_layer =
  45. PaintedOverlayScrollbarLayer::Create(scrollbar);
  46. scrollbar_layer->SetIsDrawable(true);
  47. scrollbar_layer->SetBounds(gfx::Size(100, 100));
  48. layer_tree_host->SetRootLayer(scrollbar_layer);
  49. EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host.get());
  50. // Request no paint when initialization.
  51. scrollbar_layer->Update();
  52. EXPECT_FALSE(scrollbar->PaintTickmarksCalled());
  53. // The next update will paint nothing because still no tickmarks applied.
  54. scrollbar_layer->Update();
  55. EXPECT_FALSE(scrollbar->PaintTickmarksCalled());
  56. // Enable the tickmarks.
  57. scrollbar->set_has_tickmarks(true);
  58. scrollbar_layer->Update();
  59. EXPECT_TRUE(scrollbar->PaintTickmarksCalled());
  60. scrollbar->SetPaintTickmarksCalled(false);
  61. // Disable the tickmarks. No paint.
  62. scrollbar->set_has_tickmarks(false);
  63. scrollbar_layer->Update();
  64. EXPECT_FALSE(scrollbar->PaintTickmarksCalled());
  65. }
  66. } // namespace
  67. } // namespace cc