1 // Copyright 2012 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.
5 #include "base/containers/hash_tables.h"
6 #include "cc/animation/scrollbar_animation_controller.h"
7 #include "cc/layers/append_quads_data.h"
8 #include "cc/layers/painted_scrollbar_layer.h"
9 #include "cc/layers/painted_scrollbar_layer_impl.h"
10 #include "cc/layers/scrollbar_layer_interface.h"
11 #include "cc/layers/solid_color_scrollbar_layer.h"
12 #include "cc/layers/solid_color_scrollbar_layer_impl.h"
13 #include "cc/quads/solid_color_draw_quad.h"
14 #include "cc/resources/resource_update_queue.h"
15 #include "cc/test/fake_impl_proxy.h"
16 #include "cc/test/fake_layer_tree_host.h"
17 #include "cc/test/fake_layer_tree_host_client.h"
18 #include "cc/test/fake_layer_tree_host_impl.h"
19 #include "cc/test/fake_painted_scrollbar_layer.h"
20 #include "cc/test/fake_scrollbar.h"
21 #include "cc/test/geometry_test_utils.h"
22 #include "cc/test/layer_tree_test.h"
23 #include "cc/test/mock_quad_culler.h"
24 #include "cc/test/test_web_graphics_context_3d.h"
25 #include "cc/trees/layer_tree_host.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "cc/trees/occlusion_tracker.h"
28 #include "cc/trees/single_thread_proxy.h"
29 #include "cc/trees/tree_synchronizer.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
36 LayerImpl* LayerImplForScrollAreaAndScrollbar(
37 FakeLayerTreeHost* host,
38 scoped_ptr<Scrollbar> scrollbar,
40 bool use_solid_color_scrollbar,
41 int thumb_thickness) {
42 scoped_refptr<Layer> layer_tree_root = Layer::Create();
43 scoped_refptr<Layer> child1 = Layer::Create();
44 scoped_refptr<Layer> child2;
45 if (use_solid_color_scrollbar) {
46 const bool kIsLeftSideVerticalScrollbar = false;
47 child2 = SolidColorScrollbarLayer::Create(
48 scrollbar->Orientation(), thumb_thickness,
49 kIsLeftSideVerticalScrollbar, child1->id());
51 child2 = PaintedScrollbarLayer::Create(scrollbar.Pass(), child1->id());
53 layer_tree_root->AddChild(child1);
54 layer_tree_root->InsertChild(child2, reverse_order ? 0 : 1);
55 host->SetRootLayer(layer_tree_root);
56 return host->CommitAndCreateLayerImplTree();
59 TEST(ScrollbarLayerTest, ResolveScrollLayerPointer) {
60 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
61 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
62 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
63 host.get(), scrollbar.Pass(), false, false, 0);
65 LayerImpl* cc_child1 = layer_impl_tree_root->children()[0];
66 PaintedScrollbarLayerImpl* cc_child2 =
67 static_cast<PaintedScrollbarLayerImpl*>(
68 layer_impl_tree_root->children()[1]);
70 EXPECT_EQ(cc_child1->scrollbars()->size(), 1UL);
71 EXPECT_EQ(*(cc_child1->scrollbars()->begin()), cc_child2);
74 TEST(ScrollbarLayerTest, ResolveScrollLayerPointer_ReverseOrder) {
75 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
76 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
77 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
78 host.get(), scrollbar.Pass(), true, false, 0);
80 PaintedScrollbarLayerImpl* cc_child1 =
81 static_cast<PaintedScrollbarLayerImpl*>(
82 layer_impl_tree_root->children()[0]);
83 LayerImpl* cc_child2 = layer_impl_tree_root->children()[1];
85 EXPECT_EQ(cc_child2->scrollbars()->size(), 1UL);
86 EXPECT_EQ(*(cc_child2->scrollbars()->begin()), cc_child1);
89 TEST(ScrollbarLayerTest, ShouldScrollNonOverlayOnMainThread) {
90 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
92 // Create and attach a non-overlay scrollbar.
93 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
94 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
95 host.get(), scrollbar.Pass(), false, false, 0);
96 PaintedScrollbarLayerImpl* scrollbar_layer_impl =
97 static_cast<PaintedScrollbarLayerImpl*>(
98 layer_impl_tree_root->children()[1]);
100 // When the scrollbar is not an overlay scrollbar, the scroll should be
101 // responded to on the main thread as the compositor does not yet implement
102 // scrollbar scrolling.
103 EXPECT_EQ(InputHandler::ScrollOnMainThread,
104 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
105 InputHandler::Gesture));
107 // Create and attach an overlay scrollbar.
108 scrollbar.reset(new FakeScrollbar(false, false, true));
110 layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
111 host.get(), scrollbar.Pass(), false, false, 0);
112 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>(
113 layer_impl_tree_root->children()[1]);
115 // The user shouldn't be able to drag an overlay scrollbar and the scroll
116 // may be handled in the compositor.
117 EXPECT_EQ(InputHandler::ScrollIgnored,
118 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
119 InputHandler::Gesture));
122 TEST(PaintedScrollbarLayerTest, ScrollOffsetSynchronization) {
123 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
125 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
126 scoped_refptr<Layer> layer_tree_root = Layer::Create();
127 scoped_refptr<Layer> scroll_layer = Layer::Create();
128 scoped_refptr<Layer> content_layer = Layer::Create();
129 scoped_refptr<Layer> scrollbar_layer =
130 PaintedScrollbarLayer::Create(scrollbar.Pass(), layer_tree_root->id());
132 // Choose bounds to give max_scroll_offset = (30, 50).
133 layer_tree_root->SetBounds(gfx::Size(70, 150));
134 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
135 scroll_layer->SetScrollOffset(gfx::Vector2d(10, 20));
136 scroll_layer->SetBounds(gfx::Size(100, 200));
137 content_layer->SetBounds(gfx::Size(100, 200));
139 host->SetRootLayer(layer_tree_root);
140 layer_tree_root->AddChild(scroll_layer);
141 scroll_layer->AddChild(content_layer);
142 layer_tree_root->AddChild(scrollbar_layer);
143 scrollbar_layer->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
144 scrollbar_layer->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
146 layer_tree_root->SavePaintProperties();
147 content_layer->SavePaintProperties();
149 LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
151 ScrollbarLayerImplBase* cc_scrollbar_layer =
152 static_cast<PaintedScrollbarLayerImpl*>(
153 layer_impl_tree_root->children()[1]);
155 EXPECT_EQ(10.f, cc_scrollbar_layer->current_pos());
156 EXPECT_EQ(30, cc_scrollbar_layer->maximum());
158 layer_tree_root->SetBounds(gfx::Size(700, 1500));
159 layer_tree_root->SavePaintProperties();
160 scroll_layer->SetBounds(gfx::Size(1000, 2000));
161 scroll_layer->SetScrollOffset(gfx::Vector2d(100, 200));
162 scroll_layer->SavePaintProperties();
163 content_layer->SetBounds(gfx::Size(1000, 2000));
164 content_layer->SavePaintProperties();
166 ScrollbarAnimationController* scrollbar_controller =
167 layer_impl_tree_root->scrollbar_animation_controller();
168 layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
169 EXPECT_EQ(scrollbar_controller,
170 layer_impl_tree_root->scrollbar_animation_controller());
172 EXPECT_EQ(100.f, cc_scrollbar_layer->current_pos());
173 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
175 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
176 scroll_layer_impl->ScrollBy(gfx::Vector2d(12, 34));
178 EXPECT_EQ(112.f, cc_scrollbar_layer->current_pos());
179 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
182 #define UPDATE_AND_EXTRACT_LAYER_POINTERS() \
184 scrollbar_layer->UpdateThumbAndTrackGeometry(); \
185 root_clip_layer_impl = host->CommitAndCreateLayerImplTree(); \
186 root_layer_impl = root_clip_layer_impl->children()[0]; \
187 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>( \
188 root_layer_impl->children()[1]); \
189 scrollbar_layer_impl->ScrollbarParametersDidChange(); \
192 TEST(ScrollbarLayerTest, ThumbRect) {
193 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
194 scoped_refptr<Layer> root_clip_layer = Layer::Create();
195 scoped_refptr<Layer> root_layer = Layer::Create();
196 scoped_refptr<Layer> content_layer = Layer::Create();
197 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
198 FakePaintedScrollbarLayer::Create(false, true, root_layer->id());
200 root_layer->SetScrollClipLayerId(root_clip_layer->id());
201 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
202 root_clip_layer->SetBounds(gfx::Size(20, 50));
203 root_layer->SetBounds(gfx::Size(100, 50));
204 content_layer->SetBounds(gfx::Size(100, 50));
206 host->SetRootLayer(root_clip_layer);
207 root_clip_layer->AddChild(root_layer);
208 root_layer->AddChild(content_layer);
209 root_layer->AddChild(scrollbar_layer);
211 root_layer->SetScrollOffset(gfx::Vector2d(0, 0));
212 scrollbar_layer->SetBounds(gfx::Size(70, 10));
213 scrollbar_layer->SetScrollLayer(root_layer->id());
214 scrollbar_layer->SetClipLayer(root_clip_layer->id());
215 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
216 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
217 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
218 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
219 scrollbar_layer->UpdateThumbAndTrackGeometry();
220 LayerImpl* root_clip_layer_impl = NULL;
221 LayerImpl* root_layer_impl = NULL;
222 PaintedScrollbarLayerImpl* scrollbar_layer_impl = NULL;
224 // Thumb is at the edge of the scrollbar (should be inset to
225 // the start of the track within the scrollbar layer's
227 UPDATE_AND_EXTRACT_LAYER_POINTERS();
228 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
229 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
231 // Under-scroll (thumb position should clamp and be unchanged).
232 root_layer->SetScrollOffset(gfx::Vector2d(-5, 0));
234 UPDATE_AND_EXTRACT_LAYER_POINTERS();
235 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
236 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
238 // Over-scroll (thumb position should clamp on the far side).
239 root_layer->SetScrollOffset(gfx::Vector2d(85, 0));
241 UPDATE_AND_EXTRACT_LAYER_POINTERS();
242 EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(),
243 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
245 // Change thumb thickness and length.
246 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(4);
247 scrollbar_layer->fake_scrollbar()->set_thumb_length(6);
249 UPDATE_AND_EXTRACT_LAYER_POINTERS();
250 EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(),
251 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
253 // Shrink the scrollbar layer to cover only the track.
254 scrollbar_layer->SetBounds(gfx::Size(50, 10));
255 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(30, 10));
256 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
258 UPDATE_AND_EXTRACT_LAYER_POINTERS();
259 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
260 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
262 // Shrink the track in the non-scrolling dimension so that it only covers the
263 // middle third of the scrollbar layer (this does not affect the thumb
265 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6));
267 UPDATE_AND_EXTRACT_LAYER_POINTERS();
268 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
269 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
272 TEST(ScrollbarLayerTest, SolidColorDrawQuads) {
273 const int kThumbThickness = 3;
274 const int kTrackLength = 100;
276 LayerTreeSettings layer_tree_settings;
277 scoped_ptr<FakeLayerTreeHost> host =
278 FakeLayerTreeHost::Create(layer_tree_settings);
280 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
281 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
282 host.get(), scrollbar.Pass(), false, true, kThumbThickness);
283 ScrollbarLayerImplBase* scrollbar_layer_impl =
284 static_cast<SolidColorScrollbarLayerImpl*>(
285 layer_impl_tree_root->children()[1]);
286 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
287 scrollbar_layer_impl->SetCurrentPos(10.f);
288 scrollbar_layer_impl->SetMaximum(100);
289 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.4f);
291 // Thickness should be overridden to 3.
293 MockQuadCuller quad_culler;
294 AppendQuadsData data;
295 scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
297 const QuadList& quads = quad_culler.quad_list();
298 ASSERT_EQ(1u, quads.size());
299 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
300 EXPECT_RECT_EQ(gfx::Rect(6, 0, 40, 3), quads[0]->rect);
303 // Contents scale should scale the draw quad.
304 scrollbar_layer_impl->draw_properties().contents_scale_x = 2.f;
305 scrollbar_layer_impl->draw_properties().contents_scale_y = 2.f;
307 MockQuadCuller quad_culler;
308 AppendQuadsData data;
309 scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
311 const QuadList& quads = quad_culler.quad_list();
312 ASSERT_EQ(1u, quads.size());
313 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
314 EXPECT_RECT_EQ(gfx::Rect(12, 0, 80, 6), quads[0]->rect);
316 scrollbar_layer_impl->draw_properties().contents_scale_x = 1.f;
317 scrollbar_layer_impl->draw_properties().contents_scale_y = 1.f;
319 // For solid color scrollbars, position and size should reflect the
320 // current viewport state.
321 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.2f);
323 MockQuadCuller quad_culler;
324 AppendQuadsData data;
325 scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
327 const QuadList& quads = quad_culler.quad_list();
328 ASSERT_EQ(1u, quads.size());
329 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
330 EXPECT_RECT_EQ(gfx::Rect(8, 0, 20, 3), quads[0]->rect);
334 TEST(ScrollbarLayerTest, LayerDrivenSolidColorDrawQuads) {
335 const int kThumbThickness = 3;
336 const int kTrackLength = 10;
338 LayerTreeSettings layer_tree_settings;
339 scoped_ptr<FakeLayerTreeHost> host =
340 FakeLayerTreeHost::Create(layer_tree_settings);
342 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
345 scoped_refptr<Layer> layer_tree_root = Layer::Create();
346 scoped_refptr<Layer> scroll_layer = Layer::Create();
347 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
348 scoped_refptr<Layer> child1 = Layer::Create();
349 scoped_refptr<Layer> child2;
350 const bool kIsLeftSideVerticalScrollbar = false;
351 child2 = SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
353 kIsLeftSideVerticalScrollbar,
355 child2->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
356 child2->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
357 scroll_layer->AddChild(child1);
358 scroll_layer->InsertChild(child2, 1);
359 layer_tree_root->AddChild(scroll_layer);
360 host->SetRootLayer(layer_tree_root);
362 LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
363 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
365 ScrollbarLayerImplBase* scrollbar_layer_impl =
366 static_cast<PaintedScrollbarLayerImpl*>(scroll_layer_impl->children()[1]);
368 // Choose layer bounds to give max_scroll_offset = (8, 8).
369 layer_impl_tree_root->SetBounds(gfx::Size(2, 2));
370 scroll_layer_impl->SetBounds(gfx::Size(10, 10));
371 scroll_layer_impl->ScrollBy(gfx::Vector2dF(4.f, 0.f));
373 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
374 scrollbar_layer_impl->SetCurrentPos(4.f);
375 scrollbar_layer_impl->SetMaximum(8);
378 MockQuadCuller quad_culler;
379 AppendQuadsData data;
380 scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
382 const QuadList& quads = quad_culler.quad_list();
383 ASSERT_EQ(1u, quads.size());
384 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
385 EXPECT_RECT_EQ(gfx::Rect(3, 0, 3, 3), quads[0]->rect);
389 class ScrollbarLayerSolidColorThumbTest : public testing::Test {
391 ScrollbarLayerSolidColorThumbTest() {
392 LayerTreeSettings layer_tree_settings;
393 host_impl_.reset(new FakeLayerTreeHostImpl(
394 layer_tree_settings, &proxy_, &shared_bitmap_manager_));
396 const int kThumbThickness = 3;
397 const bool kIsLeftSideVerticalScrollbar = false;
398 const bool kIsOverlayScrollbar = false;
400 horizontal_scrollbar_layer_ =
401 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
405 kIsLeftSideVerticalScrollbar,
406 kIsOverlayScrollbar);
407 vertical_scrollbar_layer_ =
408 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
412 kIsLeftSideVerticalScrollbar,
413 kIsOverlayScrollbar);
417 FakeImplProxy proxy_;
418 TestSharedBitmapManager shared_bitmap_manager_;
419 scoped_ptr<FakeLayerTreeHostImpl> host_impl_;
420 scoped_ptr<SolidColorScrollbarLayerImpl> horizontal_scrollbar_layer_;
421 scoped_ptr<SolidColorScrollbarLayerImpl> vertical_scrollbar_layer_;
424 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbLength) {
425 horizontal_scrollbar_layer_->SetCurrentPos(0);
426 horizontal_scrollbar_layer_->SetMaximum(10);
428 // Simple case - one third of the scrollable area is visible, so the thumb
429 // should be one third as long as the track.
430 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.33f);
431 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
432 EXPECT_EQ(33, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
434 // The thumb's length should never be less than its thickness.
435 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.01f);
436 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
437 EXPECT_EQ(3, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
440 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbPosition) {
441 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
442 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.1f);
444 horizontal_scrollbar_layer_->SetCurrentPos(0);
445 horizontal_scrollbar_layer_->SetMaximum(100);
446 EXPECT_EQ(0, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
447 EXPECT_EQ(10, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
449 horizontal_scrollbar_layer_->SetCurrentPos(100);
450 // The thumb is 10px long and the track is 100px, so the maximum thumb
452 EXPECT_EQ(90, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
454 horizontal_scrollbar_layer_->SetCurrentPos(80);
455 // The scroll position is 80% of the maximum, so the thumb's position should
456 // be at 80% of its maximum or 72px.
457 EXPECT_EQ(72, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
460 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbVerticalAdjust) {
461 SolidColorScrollbarLayerImpl* layers[2] =
462 { horizontal_scrollbar_layer_.get(), vertical_scrollbar_layer_.get() };
463 for (size_t i = 0; i < 2; ++i) {
464 layers[i]->SetVisibleToTotalLengthRatio(0.2f);
465 layers[i]->SetCurrentPos(25);
466 layers[i]->SetMaximum(100);
468 layers[0]->SetBounds(gfx::Size(100, 3));
469 layers[1]->SetBounds(gfx::Size(3, 100));
471 EXPECT_RECT_EQ(gfx::RectF(20.f, 0.f, 20.f, 3.f),
472 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
473 EXPECT_RECT_EQ(gfx::RectF(0.f, 20.f, 3.f, 20.f),
474 vertical_scrollbar_layer_->ComputeThumbQuadRect());
476 horizontal_scrollbar_layer_->SetVerticalAdjust(10.f);
477 vertical_scrollbar_layer_->SetVerticalAdjust(10.f);
479 // The vertical adjustment factor has two effects:
480 // 1.) Moves the horizontal scrollbar down
481 // 2.) Increases the vertical scrollbar's effective track length which both
482 // increases the thumb's length and its position within the track.
483 EXPECT_RECT_EQ(gfx::Rect(20.f, 10.f, 20.f, 3.f),
484 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
485 EXPECT_RECT_EQ(gfx::Rect(0.f, 22, 3.f, 22.f),
486 vertical_scrollbar_layer_->ComputeThumbQuadRect());
489 class ScrollbarLayerTestMaxTextureSize : public LayerTreeTest {
491 ScrollbarLayerTestMaxTextureSize() {}
493 void SetScrollbarBounds(const gfx::Size& bounds) { bounds_ = bounds; }
495 virtual void BeginTest() OVERRIDE {
496 scroll_layer_ = Layer::Create();
497 layer_tree_host()->root_layer()->AddChild(scroll_layer_);
499 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
501 PaintedScrollbarLayer::Create(scrollbar.Pass(), scroll_layer_->id());
502 scrollbar_layer_->SetScrollLayer(scroll_layer_->id());
503 scrollbar_layer_->SetLayerTreeHost(layer_tree_host());
504 scrollbar_layer_->SetBounds(bounds_);
505 layer_tree_host()->root_layer()->AddChild(scrollbar_layer_);
507 PostSetNeedsCommitToMainThread();
510 virtual void DidCommitAndDrawFrame() OVERRIDE {
511 const int kMaxTextureSize =
512 layer_tree_host()->GetRendererCapabilities().max_texture_size;
514 // Check first that we're actually testing something.
515 EXPECT_GT(scrollbar_layer_->bounds().width(), kMaxTextureSize);
517 EXPECT_EQ(scrollbar_layer_->content_bounds().width(),
518 kMaxTextureSize - 1);
519 EXPECT_EQ(scrollbar_layer_->content_bounds().height(),
520 kMaxTextureSize - 1);
525 virtual void AfterTest() OVERRIDE {}
528 scoped_refptr<PaintedScrollbarLayer> scrollbar_layer_;
529 scoped_refptr<Layer> scroll_layer_;
533 TEST_F(ScrollbarLayerTestMaxTextureSize, DirectRenderer) {
534 scoped_ptr<TestWebGraphicsContext3D> context =
535 TestWebGraphicsContext3D::Create();
537 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
538 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
539 RunTest(true, false, true);
542 TEST_F(ScrollbarLayerTestMaxTextureSize, DelegatingRenderer) {
543 scoped_ptr<TestWebGraphicsContext3D> context =
544 TestWebGraphicsContext3D::Create();
546 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
547 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
548 RunTest(true, true, true);
551 class MockLayerTreeHost : public LayerTreeHost {
553 MockLayerTreeHost(FakeLayerTreeHostClient* client,
554 const LayerTreeSettings& settings)
555 : LayerTreeHost(client, NULL, settings),
557 total_ui_resource_created_(0),
558 total_ui_resource_deleted_(0) {
559 InitializeSingleThreaded(client);
562 virtual UIResourceId CreateUIResource(UIResourceClient* content) OVERRIDE {
563 total_ui_resource_created_++;
564 UIResourceId nid = next_id_++;
565 ui_resource_bitmap_map_.insert(
566 std::make_pair(nid, content->GetBitmap(nid, false)));
570 // Deletes a UI resource. May safely be called more than once.
571 virtual void DeleteUIResource(UIResourceId id) OVERRIDE {
572 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
573 if (iter != ui_resource_bitmap_map_.end()) {
574 ui_resource_bitmap_map_.erase(iter);
575 total_ui_resource_deleted_++;
579 size_t UIResourceCount() { return ui_resource_bitmap_map_.size(); }
580 int TotalUIResourceDeleted() { return total_ui_resource_deleted_; }
581 int TotalUIResourceCreated() { return total_ui_resource_created_; }
583 gfx::Size ui_resource_size(UIResourceId id) {
584 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
585 if (iter != ui_resource_bitmap_map_.end())
586 return iter->second.GetSize();
590 UIResourceBitmap* ui_resource_bitmap(UIResourceId id) {
591 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
592 if (iter != ui_resource_bitmap_map_.end())
593 return &iter->second;
598 typedef base::hash_map<UIResourceId, UIResourceBitmap>
600 UIResourceBitmapMap ui_resource_bitmap_map_;
603 int total_ui_resource_created_;
604 int total_ui_resource_deleted_;
608 class ScrollbarLayerTestResourceCreation : public testing::Test {
610 ScrollbarLayerTestResourceCreation()
611 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
613 void TestResourceUpload(int num_updates,
614 size_t expected_resources,
615 int expected_created,
616 int expected_deleted,
617 bool use_solid_color_scrollbar) {
618 layer_tree_host_.reset(
619 new MockLayerTreeHost(&fake_client_, layer_tree_settings_));
621 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, false));
622 scoped_refptr<Layer> layer_tree_root = Layer::Create();
623 scoped_refptr<Layer> content_layer = Layer::Create();
624 scoped_refptr<Layer> scrollbar_layer;
625 if (use_solid_color_scrollbar) {
626 const int kThumbThickness = 3;
627 const bool kIsLeftSideVerticalScrollbar = false;
629 SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
631 kIsLeftSideVerticalScrollbar,
632 layer_tree_root->id());
634 scrollbar_layer = PaintedScrollbarLayer::Create(scrollbar.Pass(),
635 layer_tree_root->id());
637 layer_tree_root->AddChild(content_layer);
638 layer_tree_root->AddChild(scrollbar_layer);
640 layer_tree_host_->SetRootLayer(layer_tree_root);
642 scrollbar_layer->SetIsDrawable(true);
643 scrollbar_layer->SetBounds(gfx::Size(100, 100));
644 layer_tree_root->SetScrollOffset(gfx::Vector2d(10, 20));
645 layer_tree_root->SetBounds(gfx::Size(100, 200));
646 content_layer->SetBounds(gfx::Size(100, 200));
647 scrollbar_layer->draw_properties().content_bounds = gfx::Size(100, 200);
648 scrollbar_layer->draw_properties().visible_content_rect =
649 gfx::Rect(0, 0, 100, 200);
650 scrollbar_layer->CreateRenderSurface();
651 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
653 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
654 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
656 ResourceUpdateQueue queue;
657 gfx::Rect screen_space_clip_rect;
658 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
660 scrollbar_layer->SavePaintProperties();
661 for (int update_counter = 0; update_counter < num_updates; update_counter++)
662 scrollbar_layer->Update(&queue, &occlusion_tracker);
664 // A non-solid-color scrollbar should have requested two textures.
665 EXPECT_EQ(expected_resources, layer_tree_host_->UIResourceCount());
666 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
667 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
669 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
671 scrollbar_layer->ClearRenderSurface();
675 FakeLayerTreeHostClient fake_client_;
676 LayerTreeSettings layer_tree_settings_;
677 scoped_ptr<MockLayerTreeHost> layer_tree_host_;
680 TEST_F(ScrollbarLayerTestResourceCreation, ResourceUpload) {
681 bool use_solid_color_scrollbars = false;
682 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
683 int num_updates[3] = {1, 5, 10};
684 for (int j = 0; j < 3; j++) {
685 TestResourceUpload(num_updates[j],
688 (num_updates[j] - 1) * 2,
689 use_solid_color_scrollbars);
693 TEST_F(ScrollbarLayerTestResourceCreation, SolidColorNoResourceUpload) {
694 bool use_solid_color_scrollbars = true;
695 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
696 TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars);
699 class ScaledScrollbarLayerTestResourceCreation : public testing::Test {
701 ScaledScrollbarLayerTestResourceCreation()
702 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
704 void TestResourceUpload(const float test_scale) {
705 layer_tree_host_.reset(
706 new MockLayerTreeHost(&fake_client_, layer_tree_settings_));
708 gfx::Point scrollbar_location(0, 185);
709 scoped_refptr<Layer> layer_tree_root = Layer::Create();
710 scoped_refptr<Layer> content_layer = Layer::Create();
711 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
712 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root->id());
714 layer_tree_root->AddChild(content_layer);
715 layer_tree_root->AddChild(scrollbar_layer);
717 layer_tree_host_->SetRootLayer(layer_tree_root);
719 scrollbar_layer->SetIsDrawable(true);
720 scrollbar_layer->SetBounds(gfx::Size(100, 15));
721 scrollbar_layer->SetPosition(scrollbar_location);
722 layer_tree_root->SetBounds(gfx::Size(100, 200));
723 content_layer->SetBounds(gfx::Size(100, 200));
724 gfx::SizeF scaled_size =
725 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
726 gfx::PointF scaled_location =
727 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
728 scrollbar_layer->draw_properties().content_bounds =
729 gfx::Size(scaled_size.width(), scaled_size.height());
730 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
731 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
732 scrollbar_layer->draw_properties().visible_content_rect =
733 gfx::Rect(scaled_location.x(),
736 scaled_size.height());
737 scrollbar_layer->CreateRenderSurface();
738 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
740 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
741 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
743 ResourceUpdateQueue queue;
744 gfx::Rect screen_space_clip_rect;
745 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
746 scrollbar_layer->SavePaintProperties();
747 scrollbar_layer->Update(&queue, &occlusion_tracker);
749 // Verify that we have not generated any content uploads that are larger
750 // than their destination textures.
752 gfx::Size track_size = layer_tree_host_->ui_resource_size(
753 scrollbar_layer->track_resource_id());
754 gfx::Size thumb_size = layer_tree_host_->ui_resource_size(
755 scrollbar_layer->thumb_resource_id());
757 EXPECT_LE(track_size.width(), scrollbar_layer->content_bounds().width());
758 EXPECT_LE(track_size.height(), scrollbar_layer->content_bounds().height());
759 EXPECT_LE(thumb_size.width(), scrollbar_layer->content_bounds().width());
760 EXPECT_LE(thumb_size.height(), scrollbar_layer->content_bounds().height());
762 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
764 scrollbar_layer->ClearRenderSurface();
768 FakeLayerTreeHostClient fake_client_;
769 LayerTreeSettings layer_tree_settings_;
770 scoped_ptr<MockLayerTreeHost> layer_tree_host_;
773 TEST_F(ScaledScrollbarLayerTestResourceCreation, ScaledResourceUpload) {
774 // Pick a test scale that moves the scrollbar's (non-zero) position to
775 // a non-pixel-aligned location.
776 TestResourceUpload(.041f);
777 TestResourceUpload(1.41f);
778 TestResourceUpload(4.1f);
781 class ScaledScrollbarLayerTestScaledRasterization : public testing::Test {
783 ScaledScrollbarLayerTestScaledRasterization()
784 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
786 void TestScale(const gfx::Rect scrollbar_rect, const float test_scale) {
787 layer_tree_host_.reset(
788 new MockLayerTreeHost(&fake_client_, layer_tree_settings_));
790 bool paint_during_update = true;
791 bool has_thumb = false;
792 scoped_refptr<Layer> layer_tree_root = Layer::Create();
793 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
794 FakePaintedScrollbarLayer::Create(paint_during_update,
796 layer_tree_root->id());
798 layer_tree_root->AddChild(scrollbar_layer);
800 layer_tree_host_->SetRootLayer(layer_tree_root);
802 scrollbar_layer->SetBounds(scrollbar_rect.size());
803 scrollbar_layer->SetPosition(scrollbar_rect.origin());
804 scrollbar_layer->fake_scrollbar()->set_location(scrollbar_rect.origin());
805 scrollbar_layer->fake_scrollbar()->set_track_rect(scrollbar_rect);
806 gfx::SizeF scaled_size =
807 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
808 gfx::PointF scaled_location =
809 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
810 scrollbar_layer->draw_properties().content_bounds =
811 gfx::Size(scaled_size.width(), scaled_size.height());
812 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
813 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
814 scrollbar_layer->draw_properties().visible_content_rect =
815 gfx::Rect(scaled_location.x(),
818 scaled_size.height());
820 ResourceUpdateQueue queue;
821 gfx::Rect screen_space_clip_rect;
822 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
823 scrollbar_layer->SavePaintProperties();
825 scrollbar_layer->Update(&queue, &occlusion_tracker);
827 UIResourceBitmap* bitmap = layer_tree_host_->ui_resource_bitmap(
828 scrollbar_layer->track_resource_id());
832 AutoLockUIResourceBitmap locked_bitmap(*bitmap);
834 const SkColor* pixels =
835 reinterpret_cast<const SkColor*>(locked_bitmap.GetPixels());
836 SkColor color = argb_to_skia(
837 scrollbar_layer->fake_scrollbar()->paint_fill_color());
838 int width = bitmap->GetSize().width();
839 int height = bitmap->GetSize().height();
841 // Make sure none of the corners of the bitmap were inadvertently clipped.
842 EXPECT_EQ(color, pixels[0])
843 << "Top left pixel doesn't match scrollbar color.";
845 EXPECT_EQ(color, pixels[width - 1])
846 << "Top right pixel doesn't match scrollbar color.";
848 EXPECT_EQ(color, pixels[width * (height - 1)])
849 << "Bottom left pixel doesn't match scrollbar color.";
851 EXPECT_EQ(color, pixels[width * height - 1])
852 << "Bottom right pixel doesn't match scrollbar color.";
856 // On Android, Skia uses ABGR
857 static SkColor argb_to_skia(SkColor c) {
858 return (SkColorGetA(c) << SK_A32_SHIFT) |
859 (SkColorGetR(c) << SK_R32_SHIFT) |
860 (SkColorGetG(c) << SK_G32_SHIFT) |
861 (SkColorGetB(c) << SK_B32_SHIFT);
864 FakeLayerTreeHostClient fake_client_;
865 LayerTreeSettings layer_tree_settings_;
866 scoped_ptr<MockLayerTreeHost> layer_tree_host_;
869 TEST_F(ScaledScrollbarLayerTestScaledRasterization, TestLostPrecisionInClip) {
870 // Try rasterization at coordinates and scale that caused problematic
871 // rounding and clipping errors.
872 // Vertical Scrollbars.
873 TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f);
874 TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f);
876 // Horizontal Scrollbars.
877 TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f);
878 TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f);