Abstract GoogleURLTracker & google_util Profile dependencies
[chromium-blink-merge.git] / cc / layers / scrollbar_layer_unittest.cc
blob2f9ede6730e0daa28cd2852568a7338a361dacff
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"
33 namespace cc {
34 namespace {
36 LayerImpl* LayerImplForScrollAreaAndScrollbar(FakeLayerTreeHost* host,
37 scoped_ptr<Scrollbar> scrollbar,
38 bool reverse_order,
39 bool use_solid_color_scrollbar,
40 int thumb_thickness,
41 int track_start) {
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(scrollbar->Orientation(),
48 thumb_thickness,
49 track_start,
50 kIsLeftSideVerticalScrollbar,
51 child1->id());
52 } else {
53 child2 = PaintedScrollbarLayer::Create(scrollbar.Pass(), child1->id());
55 child2->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
56 layer_tree_root->AddChild(child1);
57 layer_tree_root->InsertChild(child2, reverse_order ? 0 : 1);
58 host->SetRootLayer(layer_tree_root);
59 return host->CommitAndCreateLayerImplTree();
62 TEST(ScrollbarLayerTest, ResolveScrollLayerPointer) {
63 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
64 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
65 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
66 host.get(), scrollbar.Pass(), false, false, 0, 0);
68 LayerImpl* cc_child1 = layer_impl_tree_root->children()[0];
69 PaintedScrollbarLayerImpl* cc_child2 =
70 static_cast<PaintedScrollbarLayerImpl*>(
71 layer_impl_tree_root->children()[1]);
73 EXPECT_EQ(cc_child1->scrollbars()->size(), 1UL);
74 EXPECT_EQ(*(cc_child1->scrollbars()->begin()), cc_child2);
77 TEST(ScrollbarLayerTest, ResolveScrollLayerPointer_ReverseOrder) {
78 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
79 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
80 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
81 host.get(), scrollbar.Pass(), true, false, 0, 0);
83 PaintedScrollbarLayerImpl* cc_child1 =
84 static_cast<PaintedScrollbarLayerImpl*>(
85 layer_impl_tree_root->children()[0]);
86 LayerImpl* cc_child2 = layer_impl_tree_root->children()[1];
88 EXPECT_EQ(cc_child2->scrollbars()->size(), 1UL);
89 EXPECT_EQ(*(cc_child2->scrollbars()->begin()), cc_child1);
92 TEST(ScrollbarLayerTest, ShouldScrollNonOverlayOnMainThread) {
93 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
95 // Create and attach a non-overlay scrollbar.
96 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
97 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
98 host.get(), scrollbar.Pass(), false, false, 0, 0);
99 PaintedScrollbarLayerImpl* scrollbar_layer_impl =
100 static_cast<PaintedScrollbarLayerImpl*>(
101 layer_impl_tree_root->children()[1]);
103 // When the scrollbar is not an overlay scrollbar, the scroll should be
104 // responded to on the main thread as the compositor does not yet implement
105 // scrollbar scrolling.
106 EXPECT_EQ(InputHandler::ScrollOnMainThread,
107 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
108 InputHandler::Gesture));
110 // Create and attach an overlay scrollbar.
111 scrollbar.reset(new FakeScrollbar(false, false, true));
113 layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
114 host.get(), scrollbar.Pass(), false, false, 0, 0);
115 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>(
116 layer_impl_tree_root->children()[1]);
118 // The user shouldn't be able to drag an overlay scrollbar and the scroll
119 // may be handled in the compositor.
120 EXPECT_EQ(InputHandler::ScrollIgnored,
121 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
122 InputHandler::Gesture));
125 TEST(PaintedScrollbarLayerTest, ScrollOffsetSynchronization) {
126 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
128 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
129 scoped_refptr<Layer> layer_tree_root = Layer::Create();
130 scoped_refptr<Layer> scroll_layer = Layer::Create();
131 scoped_refptr<Layer> content_layer = Layer::Create();
132 scoped_refptr<Layer> scrollbar_layer =
133 PaintedScrollbarLayer::Create(scrollbar.Pass(), layer_tree_root->id());
135 // Choose bounds to give max_scroll_offset = (30, 50).
136 layer_tree_root->SetBounds(gfx::Size(70, 150));
137 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
138 scroll_layer->SetScrollOffset(gfx::Vector2d(10, 20));
139 scroll_layer->SetBounds(gfx::Size(100, 200));
140 content_layer->SetBounds(gfx::Size(100, 200));
142 host->SetRootLayer(layer_tree_root);
143 layer_tree_root->AddChild(scroll_layer);
144 scroll_layer->AddChild(content_layer);
145 layer_tree_root->AddChild(scrollbar_layer);
146 scrollbar_layer->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
147 scrollbar_layer->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
149 layer_tree_root->SavePaintProperties();
150 content_layer->SavePaintProperties();
152 LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
154 ScrollbarLayerImplBase* cc_scrollbar_layer =
155 static_cast<PaintedScrollbarLayerImpl*>(
156 layer_impl_tree_root->children()[1]);
158 EXPECT_EQ(10.f, cc_scrollbar_layer->current_pos());
159 EXPECT_EQ(30, cc_scrollbar_layer->maximum());
161 layer_tree_root->SetBounds(gfx::Size(700, 1500));
162 layer_tree_root->SavePaintProperties();
163 scroll_layer->SetBounds(gfx::Size(1000, 2000));
164 scroll_layer->SetScrollOffset(gfx::Vector2d(100, 200));
165 scroll_layer->SavePaintProperties();
166 content_layer->SetBounds(gfx::Size(1000, 2000));
167 content_layer->SavePaintProperties();
169 ScrollbarAnimationController* scrollbar_controller =
170 layer_impl_tree_root->scrollbar_animation_controller();
171 layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
172 EXPECT_EQ(scrollbar_controller,
173 layer_impl_tree_root->scrollbar_animation_controller());
175 EXPECT_EQ(100.f, cc_scrollbar_layer->current_pos());
176 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
178 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
179 scroll_layer_impl->ScrollBy(gfx::Vector2d(12, 34));
181 EXPECT_EQ(112.f, cc_scrollbar_layer->current_pos());
182 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
185 #define UPDATE_AND_EXTRACT_LAYER_POINTERS() \
186 do { \
187 scrollbar_layer->UpdateThumbAndTrackGeometry(); \
188 root_clip_layer_impl = host->CommitAndCreateLayerImplTree(); \
189 root_layer_impl = root_clip_layer_impl->children()[0]; \
190 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>( \
191 root_layer_impl->children()[1]); \
192 scrollbar_layer_impl->ScrollbarParametersDidChange(); \
193 } while (false)
195 TEST(ScrollbarLayerTest, ThumbRect) {
196 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
197 scoped_refptr<Layer> root_clip_layer = Layer::Create();
198 scoped_refptr<Layer> root_layer = Layer::Create();
199 scoped_refptr<Layer> content_layer = Layer::Create();
200 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
201 FakePaintedScrollbarLayer::Create(false, true, root_layer->id());
203 root_layer->SetScrollClipLayerId(root_clip_layer->id());
204 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
205 root_clip_layer->SetBounds(gfx::Size(20, 50));
206 root_layer->SetBounds(gfx::Size(100, 50));
207 content_layer->SetBounds(gfx::Size(100, 50));
209 host->SetRootLayer(root_clip_layer);
210 root_clip_layer->AddChild(root_layer);
211 root_layer->AddChild(content_layer);
212 root_layer->AddChild(scrollbar_layer);
214 root_layer->SetScrollOffset(gfx::Vector2d(0, 0));
215 scrollbar_layer->SetBounds(gfx::Size(70, 10));
216 scrollbar_layer->SetScrollLayer(root_layer->id());
217 scrollbar_layer->SetClipLayer(root_clip_layer->id());
218 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
219 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
220 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
221 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
222 scrollbar_layer->UpdateThumbAndTrackGeometry();
223 LayerImpl* root_clip_layer_impl = NULL;
224 LayerImpl* root_layer_impl = NULL;
225 PaintedScrollbarLayerImpl* scrollbar_layer_impl = NULL;
227 // Thumb is at the edge of the scrollbar (should be inset to
228 // the start of the track within the scrollbar layer's
229 // position).
230 UPDATE_AND_EXTRACT_LAYER_POINTERS();
231 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
232 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
234 // Under-scroll (thumb position should clamp and be unchanged).
235 root_layer->SetScrollOffset(gfx::Vector2d(-5, 0));
237 UPDATE_AND_EXTRACT_LAYER_POINTERS();
238 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
239 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
241 // Over-scroll (thumb position should clamp on the far side).
242 root_layer->SetScrollOffset(gfx::Vector2d(85, 0));
244 UPDATE_AND_EXTRACT_LAYER_POINTERS();
245 EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(),
246 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
248 // Change thumb thickness and length.
249 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(4);
250 scrollbar_layer->fake_scrollbar()->set_thumb_length(6);
252 UPDATE_AND_EXTRACT_LAYER_POINTERS();
253 EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(),
254 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
256 // Shrink the scrollbar layer to cover only the track.
257 scrollbar_layer->SetBounds(gfx::Size(50, 10));
258 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(30, 10));
259 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
261 UPDATE_AND_EXTRACT_LAYER_POINTERS();
262 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
263 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
265 // Shrink the track in the non-scrolling dimension so that it only covers the
266 // middle third of the scrollbar layer (this does not affect the thumb
267 // position).
268 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6));
270 UPDATE_AND_EXTRACT_LAYER_POINTERS();
271 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
272 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
275 TEST(ScrollbarLayerTest, SolidColorDrawQuads) {
276 const int kThumbThickness = 3;
277 const int kTrackStart = 0;
278 const int kTrackLength = 100;
280 LayerTreeSettings layer_tree_settings;
281 scoped_ptr<FakeLayerTreeHost> host =
282 FakeLayerTreeHost::Create(layer_tree_settings);
284 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
285 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
286 host.get(), scrollbar.Pass(), false, true, kThumbThickness, kTrackStart);
287 ScrollbarLayerImplBase* scrollbar_layer_impl =
288 static_cast<SolidColorScrollbarLayerImpl*>(
289 layer_impl_tree_root->children()[1]);
290 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
291 scrollbar_layer_impl->SetCurrentPos(10.f);
292 scrollbar_layer_impl->SetMaximum(100);
293 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.4f);
295 // Thickness should be overridden to 3.
297 MockQuadCuller quad_culler;
298 AppendQuadsData data;
299 scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
301 const QuadList& quads = quad_culler.quad_list();
302 ASSERT_EQ(1u, quads.size());
303 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
304 EXPECT_RECT_EQ(gfx::Rect(6, 0, 40, 3), quads[0]->rect);
307 // Contents scale should scale the draw quad.
308 scrollbar_layer_impl->draw_properties().contents_scale_x = 2.f;
309 scrollbar_layer_impl->draw_properties().contents_scale_y = 2.f;
311 MockQuadCuller quad_culler;
312 AppendQuadsData data;
313 scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
315 const QuadList& quads = quad_culler.quad_list();
316 ASSERT_EQ(1u, quads.size());
317 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
318 EXPECT_RECT_EQ(gfx::Rect(12, 0, 80, 6), quads[0]->rect);
320 scrollbar_layer_impl->draw_properties().contents_scale_x = 1.f;
321 scrollbar_layer_impl->draw_properties().contents_scale_y = 1.f;
323 // For solid color scrollbars, position and size should reflect the
324 // current viewport state.
325 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.2f);
327 MockQuadCuller quad_culler;
328 AppendQuadsData data;
329 scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
331 const QuadList& quads = quad_culler.quad_list();
332 ASSERT_EQ(1u, quads.size());
333 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
334 EXPECT_RECT_EQ(gfx::Rect(8, 0, 20, 3), quads[0]->rect);
338 TEST(ScrollbarLayerTest, LayerDrivenSolidColorDrawQuads) {
339 const int kThumbThickness = 3;
340 const int kTrackStart = 0;
341 const int kTrackLength = 10;
343 LayerTreeSettings layer_tree_settings;
344 scoped_ptr<FakeLayerTreeHost> host =
345 FakeLayerTreeHost::Create(layer_tree_settings);
347 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
350 scoped_refptr<Layer> layer_tree_root = Layer::Create();
351 scoped_refptr<Layer> scroll_layer = Layer::Create();
352 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
353 scoped_refptr<Layer> child1 = Layer::Create();
354 scoped_refptr<Layer> child2;
355 const bool kIsLeftSideVerticalScrollbar = false;
356 child2 = SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
357 kThumbThickness,
358 kTrackStart,
359 kIsLeftSideVerticalScrollbar,
360 child1->id());
361 child2->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
362 child2->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
363 scroll_layer->AddChild(child1);
364 scroll_layer->InsertChild(child2, 1);
365 layer_tree_root->AddChild(scroll_layer);
366 host->SetRootLayer(layer_tree_root);
368 LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
369 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
371 ScrollbarLayerImplBase* scrollbar_layer_impl =
372 static_cast<PaintedScrollbarLayerImpl*>(scroll_layer_impl->children()[1]);
374 // Choose layer bounds to give max_scroll_offset = (8, 8).
375 layer_impl_tree_root->SetBounds(gfx::Size(2, 2));
376 scroll_layer_impl->SetBounds(gfx::Size(10, 10));
377 scroll_layer_impl->ScrollBy(gfx::Vector2dF(4.f, 0.f));
379 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
380 scrollbar_layer_impl->SetCurrentPos(4.f);
381 scrollbar_layer_impl->SetMaximum(8);
384 MockQuadCuller quad_culler;
385 AppendQuadsData data;
386 scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
388 const QuadList& quads = quad_culler.quad_list();
389 ASSERT_EQ(1u, quads.size());
390 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
391 EXPECT_RECT_EQ(gfx::Rect(3, 0, 3, 3), quads[0]->rect);
395 class ScrollbarLayerSolidColorThumbTest : public testing::Test {
396 public:
397 ScrollbarLayerSolidColorThumbTest() {
398 LayerTreeSettings layer_tree_settings;
399 host_impl_.reset(new FakeLayerTreeHostImpl(
400 layer_tree_settings, &proxy_, &shared_bitmap_manager_));
402 const int kThumbThickness = 3;
403 const int kTrackStart = 0;
404 const bool kIsLeftSideVerticalScrollbar = false;
405 const bool kIsOverlayScrollbar = false;
407 horizontal_scrollbar_layer_ =
408 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
410 HORIZONTAL,
411 kThumbThickness,
412 kTrackStart,
413 kIsLeftSideVerticalScrollbar,
414 kIsOverlayScrollbar);
415 vertical_scrollbar_layer_ =
416 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
418 VERTICAL,
419 kThumbThickness,
420 kTrackStart,
421 kIsLeftSideVerticalScrollbar,
422 kIsOverlayScrollbar);
425 protected:
426 FakeImplProxy proxy_;
427 TestSharedBitmapManager shared_bitmap_manager_;
428 scoped_ptr<FakeLayerTreeHostImpl> host_impl_;
429 scoped_ptr<SolidColorScrollbarLayerImpl> horizontal_scrollbar_layer_;
430 scoped_ptr<SolidColorScrollbarLayerImpl> vertical_scrollbar_layer_;
433 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbLength) {
434 horizontal_scrollbar_layer_->SetCurrentPos(0);
435 horizontal_scrollbar_layer_->SetMaximum(10);
437 // Simple case - one third of the scrollable area is visible, so the thumb
438 // should be one third as long as the track.
439 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.33f);
440 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
441 EXPECT_EQ(33, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
443 // The thumb's length should never be less than its thickness.
444 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.01f);
445 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
446 EXPECT_EQ(3, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
449 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbPosition) {
450 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
451 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.1f);
453 horizontal_scrollbar_layer_->SetCurrentPos(0);
454 horizontal_scrollbar_layer_->SetMaximum(100);
455 EXPECT_EQ(0, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
456 EXPECT_EQ(10, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
458 horizontal_scrollbar_layer_->SetCurrentPos(100);
459 // The thumb is 10px long and the track is 100px, so the maximum thumb
460 // position is 90px.
461 EXPECT_EQ(90, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
463 horizontal_scrollbar_layer_->SetCurrentPos(80);
464 // The scroll position is 80% of the maximum, so the thumb's position should
465 // be at 80% of its maximum or 72px.
466 EXPECT_EQ(72, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
469 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbVerticalAdjust) {
470 SolidColorScrollbarLayerImpl* layers[2] =
471 { horizontal_scrollbar_layer_.get(), vertical_scrollbar_layer_.get() };
472 for (size_t i = 0; i < 2; ++i) {
473 layers[i]->SetVisibleToTotalLengthRatio(0.2f);
474 layers[i]->SetCurrentPos(25);
475 layers[i]->SetMaximum(100);
477 layers[0]->SetBounds(gfx::Size(100, 3));
478 layers[1]->SetBounds(gfx::Size(3, 100));
480 EXPECT_RECT_EQ(gfx::RectF(20.f, 0.f, 20.f, 3.f),
481 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
482 EXPECT_RECT_EQ(gfx::RectF(0.f, 20.f, 3.f, 20.f),
483 vertical_scrollbar_layer_->ComputeThumbQuadRect());
485 horizontal_scrollbar_layer_->SetVerticalAdjust(10.f);
486 vertical_scrollbar_layer_->SetVerticalAdjust(10.f);
488 // The vertical adjustment factor has two effects:
489 // 1.) Moves the horizontal scrollbar down
490 // 2.) Increases the vertical scrollbar's effective track length which both
491 // increases the thumb's length and its position within the track.
492 EXPECT_RECT_EQ(gfx::Rect(20.f, 10.f, 20.f, 3.f),
493 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
494 EXPECT_RECT_EQ(gfx::Rect(0.f, 22, 3.f, 22.f),
495 vertical_scrollbar_layer_->ComputeThumbQuadRect());
498 class ScrollbarLayerTestMaxTextureSize : public LayerTreeTest {
499 public:
500 ScrollbarLayerTestMaxTextureSize() {}
502 void SetScrollbarBounds(const gfx::Size& bounds) { bounds_ = bounds; }
504 virtual void BeginTest() OVERRIDE {
505 scroll_layer_ = Layer::Create();
506 layer_tree_host()->root_layer()->AddChild(scroll_layer_);
508 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
509 scrollbar_layer_ =
510 PaintedScrollbarLayer::Create(scrollbar.Pass(), scroll_layer_->id());
511 scrollbar_layer_->SetScrollLayer(scroll_layer_->id());
512 scrollbar_layer_->SetLayerTreeHost(layer_tree_host());
513 scrollbar_layer_->SetBounds(bounds_);
514 layer_tree_host()->root_layer()->AddChild(scrollbar_layer_);
516 PostSetNeedsCommitToMainThread();
519 virtual void DidCommitAndDrawFrame() OVERRIDE {
520 const int kMaxTextureSize =
521 layer_tree_host()->GetRendererCapabilities().max_texture_size;
523 // Check first that we're actually testing something.
524 EXPECT_GT(scrollbar_layer_->bounds().width(), kMaxTextureSize);
526 EXPECT_EQ(scrollbar_layer_->content_bounds().width(),
527 kMaxTextureSize - 1);
528 EXPECT_EQ(scrollbar_layer_->content_bounds().height(),
529 kMaxTextureSize - 1);
531 EndTest();
534 virtual void AfterTest() OVERRIDE {}
536 private:
537 scoped_refptr<PaintedScrollbarLayer> scrollbar_layer_;
538 scoped_refptr<Layer> scroll_layer_;
539 gfx::Size bounds_;
542 TEST_F(ScrollbarLayerTestMaxTextureSize, DirectRenderer) {
543 scoped_ptr<TestWebGraphicsContext3D> context =
544 TestWebGraphicsContext3D::Create();
545 int max_size = 0;
546 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
547 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
548 RunTest(true, false, true);
551 TEST_F(ScrollbarLayerTestMaxTextureSize, DelegatingRenderer) {
552 scoped_ptr<TestWebGraphicsContext3D> context =
553 TestWebGraphicsContext3D::Create();
554 int max_size = 0;
555 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
556 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
557 RunTest(true, true, true);
560 class MockLayerTreeHost : public LayerTreeHost {
561 public:
562 MockLayerTreeHost(FakeLayerTreeHostClient* client,
563 const LayerTreeSettings& settings)
564 : LayerTreeHost(client, NULL, settings),
565 next_id_(1),
566 total_ui_resource_created_(0),
567 total_ui_resource_deleted_(0) {
568 InitializeSingleThreaded(client);
571 virtual UIResourceId CreateUIResource(UIResourceClient* content) OVERRIDE {
572 total_ui_resource_created_++;
573 UIResourceId nid = next_id_++;
574 ui_resource_bitmap_map_.insert(
575 std::make_pair(nid, content->GetBitmap(nid, false)));
576 return nid;
579 // Deletes a UI resource. May safely be called more than once.
580 virtual void DeleteUIResource(UIResourceId id) OVERRIDE {
581 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
582 if (iter != ui_resource_bitmap_map_.end()) {
583 ui_resource_bitmap_map_.erase(iter);
584 total_ui_resource_deleted_++;
588 size_t UIResourceCount() { return ui_resource_bitmap_map_.size(); }
589 int TotalUIResourceDeleted() { return total_ui_resource_deleted_; }
590 int TotalUIResourceCreated() { return total_ui_resource_created_; }
592 gfx::Size ui_resource_size(UIResourceId id) {
593 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
594 if (iter != ui_resource_bitmap_map_.end())
595 return iter->second.GetSize();
596 return gfx::Size();
599 UIResourceBitmap* ui_resource_bitmap(UIResourceId id) {
600 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
601 if (iter != ui_resource_bitmap_map_.end())
602 return &iter->second;
603 return NULL;
606 private:
607 typedef base::hash_map<UIResourceId, UIResourceBitmap>
608 UIResourceBitmapMap;
609 UIResourceBitmapMap ui_resource_bitmap_map_;
611 int next_id_;
612 int total_ui_resource_created_;
613 int total_ui_resource_deleted_;
617 class ScrollbarLayerTestResourceCreation : public testing::Test {
618 public:
619 ScrollbarLayerTestResourceCreation()
620 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
622 void TestResourceUpload(int num_updates,
623 size_t expected_resources,
624 int expected_created,
625 int expected_deleted,
626 bool use_solid_color_scrollbar) {
627 layer_tree_host_.reset(
628 new MockLayerTreeHost(&fake_client_, layer_tree_settings_));
630 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, false));
631 scoped_refptr<Layer> layer_tree_root = Layer::Create();
632 scoped_refptr<Layer> content_layer = Layer::Create();
633 scoped_refptr<Layer> scrollbar_layer;
634 if (use_solid_color_scrollbar) {
635 const int kThumbThickness = 3;
636 const int kTrackStart = 0;
637 const bool kIsLeftSideVerticalScrollbar = false;
638 scrollbar_layer =
639 SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
640 kThumbThickness,
641 kTrackStart,
642 kIsLeftSideVerticalScrollbar,
643 layer_tree_root->id());
644 } else {
645 scrollbar_layer = PaintedScrollbarLayer::Create(scrollbar.Pass(),
646 layer_tree_root->id());
648 layer_tree_root->AddChild(content_layer);
649 layer_tree_root->AddChild(scrollbar_layer);
651 layer_tree_host_->SetRootLayer(layer_tree_root);
653 scrollbar_layer->SetIsDrawable(true);
654 scrollbar_layer->SetBounds(gfx::Size(100, 100));
655 layer_tree_root->SetScrollOffset(gfx::Vector2d(10, 20));
656 layer_tree_root->SetBounds(gfx::Size(100, 200));
657 content_layer->SetBounds(gfx::Size(100, 200));
658 scrollbar_layer->draw_properties().content_bounds = gfx::Size(100, 200);
659 scrollbar_layer->draw_properties().visible_content_rect =
660 gfx::Rect(0, 0, 100, 200);
661 scrollbar_layer->CreateRenderSurface();
662 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
664 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
665 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
667 ResourceUpdateQueue queue;
668 gfx::Rect screen_space_clip_rect;
669 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
671 scrollbar_layer->SavePaintProperties();
672 for (int update_counter = 0; update_counter < num_updates; update_counter++)
673 scrollbar_layer->Update(&queue, &occlusion_tracker);
675 // A non-solid-color scrollbar should have requested two textures.
676 EXPECT_EQ(expected_resources, layer_tree_host_->UIResourceCount());
677 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
678 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
680 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
682 scrollbar_layer->ClearRenderSurface();
685 protected:
686 FakeLayerTreeHostClient fake_client_;
687 LayerTreeSettings layer_tree_settings_;
688 scoped_ptr<MockLayerTreeHost> layer_tree_host_;
691 TEST_F(ScrollbarLayerTestResourceCreation, ResourceUpload) {
692 bool use_solid_color_scrollbars = false;
693 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
694 int num_updates[3] = {1, 5, 10};
695 for (int j = 0; j < 3; j++) {
696 TestResourceUpload(num_updates[j],
698 num_updates[j] * 2,
699 (num_updates[j] - 1) * 2,
700 use_solid_color_scrollbars);
704 TEST_F(ScrollbarLayerTestResourceCreation, SolidColorNoResourceUpload) {
705 bool use_solid_color_scrollbars = true;
706 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
707 TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars);
710 class ScaledScrollbarLayerTestResourceCreation : public testing::Test {
711 public:
712 ScaledScrollbarLayerTestResourceCreation()
713 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
715 void TestResourceUpload(const float test_scale) {
716 layer_tree_host_.reset(
717 new MockLayerTreeHost(&fake_client_, layer_tree_settings_));
719 gfx::Point scrollbar_location(0, 185);
720 scoped_refptr<Layer> layer_tree_root = Layer::Create();
721 scoped_refptr<Layer> content_layer = Layer::Create();
722 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
723 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root->id());
725 layer_tree_root->AddChild(content_layer);
726 layer_tree_root->AddChild(scrollbar_layer);
728 layer_tree_host_->SetRootLayer(layer_tree_root);
730 scrollbar_layer->SetIsDrawable(true);
731 scrollbar_layer->SetBounds(gfx::Size(100, 15));
732 scrollbar_layer->SetPosition(scrollbar_location);
733 layer_tree_root->SetBounds(gfx::Size(100, 200));
734 content_layer->SetBounds(gfx::Size(100, 200));
735 gfx::SizeF scaled_size =
736 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
737 gfx::PointF scaled_location =
738 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
739 scrollbar_layer->draw_properties().content_bounds =
740 gfx::Size(scaled_size.width(), scaled_size.height());
741 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
742 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
743 scrollbar_layer->draw_properties().visible_content_rect =
744 gfx::Rect(scaled_location.x(),
745 scaled_location.y(),
746 scaled_size.width(),
747 scaled_size.height());
748 scrollbar_layer->CreateRenderSurface();
749 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
751 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
752 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
754 ResourceUpdateQueue queue;
755 gfx::Rect screen_space_clip_rect;
756 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
757 scrollbar_layer->SavePaintProperties();
758 scrollbar_layer->Update(&queue, &occlusion_tracker);
760 // Verify that we have not generated any content uploads that are larger
761 // than their destination textures.
763 gfx::Size track_size = layer_tree_host_->ui_resource_size(
764 scrollbar_layer->track_resource_id());
765 gfx::Size thumb_size = layer_tree_host_->ui_resource_size(
766 scrollbar_layer->thumb_resource_id());
768 EXPECT_LE(track_size.width(), scrollbar_layer->content_bounds().width());
769 EXPECT_LE(track_size.height(), scrollbar_layer->content_bounds().height());
770 EXPECT_LE(thumb_size.width(), scrollbar_layer->content_bounds().width());
771 EXPECT_LE(thumb_size.height(), scrollbar_layer->content_bounds().height());
773 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
775 scrollbar_layer->ClearRenderSurface();
778 protected:
779 FakeLayerTreeHostClient fake_client_;
780 LayerTreeSettings layer_tree_settings_;
781 scoped_ptr<MockLayerTreeHost> layer_tree_host_;
784 TEST_F(ScaledScrollbarLayerTestResourceCreation, ScaledResourceUpload) {
785 // Pick a test scale that moves the scrollbar's (non-zero) position to
786 // a non-pixel-aligned location.
787 TestResourceUpload(.041f);
788 TestResourceUpload(1.41f);
789 TestResourceUpload(4.1f);
792 class ScaledScrollbarLayerTestScaledRasterization : public testing::Test {
793 public:
794 ScaledScrollbarLayerTestScaledRasterization()
795 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
797 void TestScale(const gfx::Rect scrollbar_rect, const float test_scale) {
798 layer_tree_host_.reset(
799 new MockLayerTreeHost(&fake_client_, layer_tree_settings_));
801 bool paint_during_update = true;
802 bool has_thumb = false;
803 scoped_refptr<Layer> layer_tree_root = Layer::Create();
804 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
805 FakePaintedScrollbarLayer::Create(paint_during_update,
806 has_thumb,
807 layer_tree_root->id());
809 layer_tree_root->AddChild(scrollbar_layer);
811 layer_tree_host_->SetRootLayer(layer_tree_root);
813 scrollbar_layer->SetBounds(scrollbar_rect.size());
814 scrollbar_layer->SetPosition(scrollbar_rect.origin());
815 scrollbar_layer->fake_scrollbar()->set_location(scrollbar_rect.origin());
816 scrollbar_layer->fake_scrollbar()->set_track_rect(scrollbar_rect);
817 gfx::SizeF scaled_size =
818 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
819 gfx::PointF scaled_location =
820 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
821 scrollbar_layer->draw_properties().content_bounds =
822 gfx::Size(scaled_size.width(), scaled_size.height());
823 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
824 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
825 scrollbar_layer->draw_properties().visible_content_rect =
826 gfx::Rect(scaled_location.x(),
827 scaled_location.y(),
828 scaled_size.width(),
829 scaled_size.height());
831 ResourceUpdateQueue queue;
832 gfx::Rect screen_space_clip_rect;
833 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
834 scrollbar_layer->SavePaintProperties();
836 scrollbar_layer->Update(&queue, &occlusion_tracker);
838 UIResourceBitmap* bitmap = layer_tree_host_->ui_resource_bitmap(
839 scrollbar_layer->track_resource_id());
841 DCHECK(bitmap);
843 AutoLockUIResourceBitmap locked_bitmap(*bitmap);
845 const SkColor* pixels =
846 reinterpret_cast<const SkColor*>(locked_bitmap.GetPixels());
847 SkColor color = argb_to_skia(
848 scrollbar_layer->fake_scrollbar()->paint_fill_color());
849 int width = bitmap->GetSize().width();
850 int height = bitmap->GetSize().height();
852 // Make sure none of the corners of the bitmap were inadvertently clipped.
853 EXPECT_EQ(color, pixels[0])
854 << "Top left pixel doesn't match scrollbar color.";
856 EXPECT_EQ(color, pixels[width - 1])
857 << "Top right pixel doesn't match scrollbar color.";
859 EXPECT_EQ(color, pixels[width * (height - 1)])
860 << "Bottom left pixel doesn't match scrollbar color.";
862 EXPECT_EQ(color, pixels[width * height - 1])
863 << "Bottom right pixel doesn't match scrollbar color.";
866 protected:
867 // On Android, Skia uses ABGR
868 static SkColor argb_to_skia(SkColor c) {
869 return (SkColorGetA(c) << SK_A32_SHIFT) |
870 (SkColorGetR(c) << SK_R32_SHIFT) |
871 (SkColorGetG(c) << SK_G32_SHIFT) |
872 (SkColorGetB(c) << SK_B32_SHIFT);
875 FakeLayerTreeHostClient fake_client_;
876 LayerTreeSettings layer_tree_settings_;
877 scoped_ptr<MockLayerTreeHost> layer_tree_host_;
880 TEST_F(ScaledScrollbarLayerTestScaledRasterization, TestLostPrecisionInClip) {
881 // Try rasterization at coordinates and scale that caused problematic
882 // rounding and clipping errors.
883 // Vertical Scrollbars.
884 TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f);
885 TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f);
887 // Horizontal Scrollbars.
888 TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f);
889 TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f);
892 } // namespace
893 } // namespace cc