painted_scrollbar_layer_unittest.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // 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. #include "cc/layers/painted_scrollbar_layer.h"
  5. #include <memory>
  6. #include "cc/animation/animation_host.h"
  7. #include "cc/test/fake_layer_tree_host.h"
  8. #include "cc/test/fake_layer_tree_host_client.h"
  9. #include "cc/test/fake_painted_scrollbar_layer.h"
  10. #include "cc/test/fake_scrollbar.h"
  11. #include "cc/test/layer_test_common.h"
  12. #include "cc/test/test_task_graph_runner.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. using ::testing::Mock;
  15. using ::testing::_;
  16. namespace cc {
  17. namespace {
  18. class PaintedScrollbarLayerTest : public testing::Test {
  19. protected:
  20. void SetUp() override {
  21. animation_host_ = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  22. layer_tree_host_ = FakeLayerTreeHost::Create(
  23. &fake_client_, &task_graph_runner_, animation_host_.get());
  24. }
  25. FakeLayerTreeHostClient fake_client_;
  26. TestTaskGraphRunner task_graph_runner_;
  27. std::unique_ptr<AnimationHost> animation_host_;
  28. std::unique_ptr<FakeLayerTreeHost> layer_tree_host_;
  29. };
  30. class MockScrollbar : public FakeScrollbar {
  31. public:
  32. MockScrollbar() {
  33. set_should_paint(true);
  34. set_has_thumb(true);
  35. set_is_overlay(false);
  36. }
  37. MOCK_METHOD3(PaintPart,
  38. void(PaintCanvas* canvas,
  39. ScrollbarPart part,
  40. const gfx::Rect& rect));
  41. private:
  42. ~MockScrollbar() override = default;
  43. };
  44. TEST_F(PaintedScrollbarLayerTest, NeedsPaint) {
  45. auto scrollbar = base::MakeRefCounted<MockScrollbar>();
  46. scoped_refptr<PaintedScrollbarLayer> scrollbar_layer =
  47. PaintedScrollbarLayer::Create(scrollbar);
  48. scrollbar_layer->SetIsDrawable(true);
  49. scrollbar_layer->SetBounds(gfx::Size(100, 100));
  50. layer_tree_host_->SetRootLayer(scrollbar_layer);
  51. UpdateDrawProperties(layer_tree_host_.get());
  52. EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
  53. // Request no paint, but expect them to be painted because they have not
  54. // yet been initialized.
  55. scrollbar->set_needs_repaint_thumb(false);
  56. scrollbar->set_needs_repaint_track(false);
  57. EXPECT_CALL(*scrollbar, PaintPart(_, ScrollbarPart::THUMB, _)).Times(1);
  58. EXPECT_CALL(*scrollbar,
  59. PaintPart(_, ScrollbarPart::TRACK_BUTTONS_TICKMARKS, _))
  60. .Times(1);
  61. scrollbar_layer->Update();
  62. Mock::VerifyAndClearExpectations(scrollbar.get());
  63. // The next update will paint nothing because the first update caused a paint.
  64. EXPECT_CALL(*scrollbar, PaintPart(_, ScrollbarPart::THUMB, _)).Times(0);
  65. EXPECT_CALL(*scrollbar,
  66. PaintPart(_, ScrollbarPart::TRACK_BUTTONS_TICKMARKS, _))
  67. .Times(0);
  68. scrollbar_layer->Update();
  69. Mock::VerifyAndClearExpectations(scrollbar.get());
  70. // Enable the thumb.
  71. EXPECT_CALL(*scrollbar, PaintPart(_, ScrollbarPart::THUMB, _)).Times(1);
  72. EXPECT_CALL(*scrollbar,
  73. PaintPart(_, ScrollbarPart::TRACK_BUTTONS_TICKMARKS, _))
  74. .Times(0);
  75. scrollbar->set_needs_repaint_thumb(true);
  76. scrollbar->set_needs_repaint_track(false);
  77. scrollbar_layer->Update();
  78. Mock::VerifyAndClearExpectations(scrollbar.get());
  79. // Enable the track.
  80. EXPECT_CALL(*scrollbar, PaintPart(_, ScrollbarPart::THUMB, _)).Times(0);
  81. EXPECT_CALL(*scrollbar,
  82. PaintPart(_, ScrollbarPart::TRACK_BUTTONS_TICKMARKS, _))
  83. .Times(1);
  84. scrollbar->set_needs_repaint_thumb(false);
  85. scrollbar->set_needs_repaint_track(true);
  86. scrollbar_layer->Update();
  87. Mock::VerifyAndClearExpectations(scrollbar.get());
  88. }
  89. TEST_F(PaintedScrollbarLayerTest, InternalContentBounds) {
  90. auto scrollbar = base::MakeRefCounted<FakeScrollbar>();
  91. auto scrollbar_layer = PaintedScrollbarLayer::Create(scrollbar);
  92. scrollbar_layer->SetIsDrawable(true);
  93. scrollbar_layer->SetBounds(gfx::Size(10, 100));
  94. layer_tree_host_->SetRootLayer(scrollbar_layer);
  95. UpdateDrawProperties(layer_tree_host_.get());
  96. EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
  97. scrollbar_layer->Update();
  98. EXPECT_EQ(gfx::Size(10, 100), scrollbar_layer->internal_content_bounds());
  99. layer_tree_host_->SetViewportRectAndScale(
  100. layer_tree_host_->device_viewport_rect(), 2.0f,
  101. layer_tree_host_->local_surface_id_from_parent());
  102. UpdateDrawProperties(layer_tree_host_.get());
  103. scrollbar_layer->Update();
  104. EXPECT_EQ(gfx::Size(20, 200), scrollbar_layer->internal_content_bounds());
  105. layer_tree_host_->SetViewportRectAndScale(
  106. layer_tree_host_->device_viewport_rect(), 0.1f,
  107. layer_tree_host_->local_surface_id_from_parent());
  108. UpdateDrawProperties(layer_tree_host_.get());
  109. scrollbar_layer->Update();
  110. EXPECT_EQ(gfx::Size(10, 100), scrollbar_layer->internal_content_bounds());
  111. }
  112. } // namespace
  113. } // namespace cc