blob: ed58fa0136c266a17c9636f75dbf7ba7b528738e [file] [log] [blame]
ccamerona54da382015-11-27 00:52:331// Copyright 2015 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_scrollbar_layer.h"
6
loyso2cb3f32f2016-11-08 07:08:347#include "cc/animation/animation_host.h"
ccamerona54da382015-11-27 00:52:338#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/gmock/include/gmock/gmock.h"
13
14using ::testing::Mock;
15using ::testing::_;
16
17namespace cc {
18
19namespace {
20
21class MockScrollbar : public FakeScrollbar {
22 public:
23 MockScrollbar() : FakeScrollbar(true, true, true) {}
24 MOCK_METHOD3(PaintPart,
enne5a9630362017-02-24 23:41:0325 void(PaintCanvas* canvas,
ccamerona54da382015-11-27 00:52:3326 ScrollbarPart part,
27 const gfx::Rect& content_rect));
28};
29
30TEST(PaintedScrollbarLayerTest, NeedsPaint) {
danakj6021ec32016-07-22 22:16:0831 FakeLayerTreeHostClient fake_client_;
ccamerona54da382015-11-27 00:52:3332 TestTaskGraphRunner task_graph_runner_;
ccamerona54da382015-11-27 00:52:3333
loyso2cb3f32f2016-11-08 07:08:3434 auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
35 auto layer_tree_host = FakeLayerTreeHost::Create(
36 &fake_client_, &task_graph_runner_, animation_host.get());
ccamerona54da382015-11-27 00:52:3337
38 MockScrollbar* scrollbar = new MockScrollbar();
39 scoped_refptr<PaintedScrollbarLayer> scrollbar_layer =
danakj60bc3bc2016-04-09 00:24:4840 PaintedScrollbarLayer::Create(std::unique_ptr<Scrollbar>(scrollbar), 1);
ccamerona54da382015-11-27 00:52:3341
42 scrollbar_layer->SetIsDrawable(true);
43 scrollbar_layer->SetBounds(gfx::Size(100, 100));
44
loyso2cb3f32f2016-11-08 07:08:3445 layer_tree_host->SetRootLayer(scrollbar_layer);
khushalsagarb1931ad2016-09-15 02:07:5146 EXPECT_EQ(scrollbar_layer->GetLayerTreeHostForTesting(),
loyso2cb3f32f2016-11-08 07:08:3447 layer_tree_host.get());
ccamerona54da382015-11-27 00:52:3348 scrollbar_layer->SavePaintProperties();
49
50 // Request no paint, but expect them to be painted because they have not
51 // yet been initialized.
52 scrollbar->set_needs_paint_thumb(false);
53 scrollbar->set_needs_paint_track(false);
54 EXPECT_CALL(*scrollbar, PaintPart(_, THUMB, _)).Times(1);
55 EXPECT_CALL(*scrollbar, PaintPart(_, TRACK, _)).Times(1);
56 scrollbar_layer->Update();
57 Mock::VerifyAndClearExpectations(scrollbar);
58
59 // The next update will paint nothing because the first update caused a paint.
60 EXPECT_CALL(*scrollbar, PaintPart(_, THUMB, _)).Times(0);
61 EXPECT_CALL(*scrollbar, PaintPart(_, TRACK, _)).Times(0);
62 scrollbar_layer->Update();
63 Mock::VerifyAndClearExpectations(scrollbar);
64
65 // Enable the thumb.
66 EXPECT_CALL(*scrollbar, PaintPart(_, THUMB, _)).Times(1);
67 EXPECT_CALL(*scrollbar, PaintPart(_, TRACK, _)).Times(0);
68 scrollbar->set_needs_paint_thumb(true);
69 scrollbar->set_needs_paint_track(false);
70 scrollbar_layer->Update();
71 Mock::VerifyAndClearExpectations(scrollbar);
72
73 // Enable the track.
74 EXPECT_CALL(*scrollbar, PaintPart(_, THUMB, _)).Times(0);
75 EXPECT_CALL(*scrollbar, PaintPart(_, TRACK, _)).Times(1);
76 scrollbar->set_needs_paint_thumb(false);
77 scrollbar->set_needs_paint_track(true);
78 scrollbar_layer->Update();
79 Mock::VerifyAndClearExpectations(scrollbar);
80}
81
82} // namespace
83} // namespace cc