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
5 #include "cc/layers/painted_overlay_scrollbar_layer.h"
6
7 #include "cc/animation/animation_host.h"
8 #include "cc/test/fake_layer_tree_host.h"
9 #include "cc/test/fake_layer_tree_host_client.h"
10 #include "cc/test/fake_scrollbar.h"
11 #include "cc/test/test_task_graph_runner.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace cc {
15
16 namespace {
17
18 class MockScrollbar : public FakeScrollbar {
19 public:
MockScrollbar()20 MockScrollbar() {
21 set_should_paint(true);
22 set_has_thumb(true);
23 set_is_overlay(true);
24 }
25
PaintPart(PaintCanvas *,ScrollbarPart part,const gfx::Rect &)26 void PaintPart(PaintCanvas*, ScrollbarPart part, const gfx::Rect&) override {
27 if (part == ScrollbarPart::TRACK_BUTTONS_TICKMARKS)
28 paint_tickmarks_called_ = true;
29 }
30
UsesNinePatchThumbResource() const31 bool UsesNinePatchThumbResource() const override { return true; }
32
NinePatchThumbCanvasSize() const33 gfx::Size NinePatchThumbCanvasSize() const override {
34 return gfx::Size(10, 10);
35 }
36
PaintTickmarksCalled()37 bool PaintTickmarksCalled() { return paint_tickmarks_called_; }
38
SetPaintTickmarksCalled(bool called)39 void SetPaintTickmarksCalled(bool called) {
40 paint_tickmarks_called_ = called;
41 }
42
43 private:
44 ~MockScrollbar() override = default;
45
46 bool paint_tickmarks_called_ = false;
47 };
48
TEST(PaintedOverlayScrollbarLayerTest,PaintTickmarks)49 TEST(PaintedOverlayScrollbarLayerTest, PaintTickmarks) {
50 FakeLayerTreeHostClient fake_client_;
51 TestTaskGraphRunner task_graph_runner_;
52
53 auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
54 auto layer_tree_host = FakeLayerTreeHost::Create(
55 &fake_client_, &task_graph_runner_, animation_host.get());
56
57 auto scrollbar = base::MakeRefCounted<MockScrollbar>();
58 scrollbar->set_has_tickmarks(false);
59
60 scoped_refptr<PaintedOverlayScrollbarLayer> scrollbar_layer =
61 PaintedOverlayScrollbarLayer::Create(scrollbar);
62
63 scrollbar_layer->SetIsDrawable(true);
64 scrollbar_layer->SetBounds(gfx::Size(100, 100));
65
66 layer_tree_host->SetRootLayer(scrollbar_layer);
67 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host.get());
68
69 // Request no paint when initialization.
70 scrollbar_layer->Update();
71 EXPECT_FALSE(scrollbar->PaintTickmarksCalled());
72
73 // The next update will paint nothing because still no tickmarks applied.
74 scrollbar_layer->Update();
75 EXPECT_FALSE(scrollbar->PaintTickmarksCalled());
76
77 // Enable the tickmarks.
78 scrollbar->set_has_tickmarks(true);
79 scrollbar_layer->Update();
80 EXPECT_TRUE(scrollbar->PaintTickmarksCalled());
81 scrollbar->SetPaintTickmarksCalled(false);
82
83 // Disable the tickmarks. No paint.
84 scrollbar->set_has_tickmarks(false);
85 scrollbar_layer->Update();
86 EXPECT_FALSE(scrollbar->PaintTickmarksCalled());
87 }
88
89 } // namespace
90 } // namespace cc
91