IPC: Remove debugging code.
[chromium-blink-merge.git] / cc / layers / scrollbar_layer_unittest.cc
blob8a998f3a004ed2c84ce7e233e9c025c2ab475383
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_occlusion_tracker.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 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
64 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
65 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
66 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
67 host.get(), scrollbar.Pass(), false, false, 0, 0);
69 LayerImpl* cc_child1 = layer_impl_tree_root->children()[0];
70 PaintedScrollbarLayerImpl* cc_child2 =
71 static_cast<PaintedScrollbarLayerImpl*>(
72 layer_impl_tree_root->children()[1]);
74 EXPECT_EQ(cc_child1->scrollbars()->size(), 1UL);
75 EXPECT_EQ(*(cc_child1->scrollbars()->begin()), cc_child2);
78 TEST(ScrollbarLayerTest, ResolveScrollLayerPointer_ReverseOrder) {
79 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
80 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
81 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
82 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
83 host.get(), scrollbar.Pass(), true, false, 0, 0);
85 PaintedScrollbarLayerImpl* cc_child1 =
86 static_cast<PaintedScrollbarLayerImpl*>(
87 layer_impl_tree_root->children()[0]);
88 LayerImpl* cc_child2 = layer_impl_tree_root->children()[1];
90 EXPECT_EQ(cc_child2->scrollbars()->size(), 1UL);
91 EXPECT_EQ(*(cc_child2->scrollbars()->begin()), cc_child1);
94 TEST(ScrollbarLayerTest, ShouldScrollNonOverlayOnMainThread) {
95 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
96 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
98 // Create and attach a non-overlay scrollbar.
99 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
100 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
101 host.get(), scrollbar.Pass(), false, false, 0, 0);
102 PaintedScrollbarLayerImpl* scrollbar_layer_impl =
103 static_cast<PaintedScrollbarLayerImpl*>(
104 layer_impl_tree_root->children()[1]);
106 // When the scrollbar is not an overlay scrollbar, the scroll should be
107 // responded to on the main thread as the compositor does not yet implement
108 // scrollbar scrolling.
109 EXPECT_EQ(InputHandler::ScrollOnMainThread,
110 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
111 InputHandler::Gesture));
113 // Create and attach an overlay scrollbar.
114 scrollbar.reset(new FakeScrollbar(false, false, true));
116 layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
117 host.get(), scrollbar.Pass(), false, false, 0, 0);
118 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>(
119 layer_impl_tree_root->children()[1]);
121 // The user shouldn't be able to drag an overlay scrollbar and the scroll
122 // may be handled in the compositor.
123 EXPECT_EQ(InputHandler::ScrollIgnored,
124 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
125 InputHandler::Gesture));
128 TEST(PaintedScrollbarLayerTest, ScrollOffsetSynchronization) {
129 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
130 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
132 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
133 scoped_refptr<Layer> layer_tree_root = Layer::Create();
134 scoped_refptr<Layer> scroll_layer = Layer::Create();
135 scoped_refptr<Layer> content_layer = Layer::Create();
136 scoped_refptr<Layer> scrollbar_layer =
137 PaintedScrollbarLayer::Create(scrollbar.Pass(), layer_tree_root->id());
139 // Choose bounds to give max_scroll_offset = (30, 50).
140 layer_tree_root->SetBounds(gfx::Size(70, 150));
141 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
142 scroll_layer->SetScrollOffset(gfx::ScrollOffset(10, 20));
143 scroll_layer->SetBounds(gfx::Size(100, 200));
144 content_layer->SetBounds(gfx::Size(100, 200));
146 host->SetRootLayer(layer_tree_root);
147 layer_tree_root->AddChild(scroll_layer);
148 scroll_layer->AddChild(content_layer);
149 layer_tree_root->AddChild(scrollbar_layer);
150 scrollbar_layer->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
151 scrollbar_layer->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
153 layer_tree_root->SavePaintProperties();
154 content_layer->SavePaintProperties();
156 LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
158 ScrollbarLayerImplBase* cc_scrollbar_layer =
159 static_cast<PaintedScrollbarLayerImpl*>(
160 layer_impl_tree_root->children()[1]);
162 EXPECT_EQ(10.f, cc_scrollbar_layer->current_pos());
163 EXPECT_EQ(30, cc_scrollbar_layer->maximum());
165 layer_tree_root->SetBounds(gfx::Size(700, 1500));
166 layer_tree_root->SavePaintProperties();
167 scroll_layer->SetBounds(gfx::Size(1000, 2000));
168 scroll_layer->SetScrollOffset(gfx::ScrollOffset(100, 200));
169 scroll_layer->SavePaintProperties();
170 content_layer->SetBounds(gfx::Size(1000, 2000));
171 content_layer->SavePaintProperties();
173 ScrollbarAnimationController* scrollbar_controller =
174 layer_impl_tree_root->scrollbar_animation_controller();
175 layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
176 EXPECT_EQ(scrollbar_controller,
177 layer_impl_tree_root->scrollbar_animation_controller());
179 EXPECT_EQ(100.f, cc_scrollbar_layer->current_pos());
180 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
182 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
183 scroll_layer_impl->ScrollBy(gfx::Vector2d(12, 34));
185 EXPECT_EQ(112.f, cc_scrollbar_layer->current_pos());
186 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
189 #define UPDATE_AND_EXTRACT_LAYER_POINTERS() \
190 do { \
191 scrollbar_layer->UpdateThumbAndTrackGeometry(); \
192 root_clip_layer_impl = host->CommitAndCreateLayerImplTree(); \
193 root_layer_impl = root_clip_layer_impl->children()[0]; \
194 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>( \
195 root_layer_impl->children()[1]); \
196 scrollbar_layer_impl->ScrollbarParametersDidChange(false); \
197 } while (false)
199 TEST(ScrollbarLayerTest, UpdatePropertiesOfScrollBarWhenThumbRemoved) {
200 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
201 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
202 scoped_refptr<Layer> root_clip_layer = Layer::Create();
203 scoped_refptr<Layer> root_layer = Layer::Create();
204 scoped_refptr<Layer> content_layer = Layer::Create();
205 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
206 FakePaintedScrollbarLayer::Create(false, true, root_layer->id());
208 root_layer->SetScrollClipLayerId(root_clip_layer->id());
209 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
210 root_clip_layer->SetBounds(gfx::Size(20, 50));
211 root_layer->SetBounds(gfx::Size(100, 50));
212 content_layer->SetBounds(gfx::Size(100, 50));
214 host->SetRootLayer(root_clip_layer);
215 root_clip_layer->AddChild(root_layer);
216 root_layer->AddChild(content_layer);
217 root_layer->AddChild(scrollbar_layer);
219 root_layer->SetScrollOffset(gfx::ScrollOffset(0, 0));
220 scrollbar_layer->SetBounds(gfx::Size(70, 10));
221 scrollbar_layer->SetScrollLayer(root_layer->id());
222 scrollbar_layer->SetClipLayer(root_clip_layer->id());
223 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
224 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
225 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
226 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
228 scrollbar_layer->UpdateThumbAndTrackGeometry();
229 LayerImpl* root_clip_layer_impl = nullptr;
230 LayerImpl* root_layer_impl = nullptr;
231 PaintedScrollbarLayerImpl* scrollbar_layer_impl = nullptr;
233 UPDATE_AND_EXTRACT_LAYER_POINTERS();
234 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
235 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
237 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
239 UPDATE_AND_EXTRACT_LAYER_POINTERS();
240 EXPECT_EQ(gfx::Rect(10, 0, 0, 0).ToString(),
241 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
244 TEST(ScrollbarLayerTest, ThumbRect) {
245 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
246 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
247 scoped_refptr<Layer> root_clip_layer = Layer::Create();
248 scoped_refptr<Layer> root_layer = Layer::Create();
249 scoped_refptr<Layer> content_layer = Layer::Create();
250 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
251 FakePaintedScrollbarLayer::Create(false, true, root_layer->id());
253 root_layer->SetScrollClipLayerId(root_clip_layer->id());
254 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
255 root_clip_layer->SetBounds(gfx::Size(20, 50));
256 root_layer->SetBounds(gfx::Size(100, 50));
257 content_layer->SetBounds(gfx::Size(100, 50));
259 host->SetRootLayer(root_clip_layer);
260 root_clip_layer->AddChild(root_layer);
261 root_layer->AddChild(content_layer);
262 root_layer->AddChild(scrollbar_layer);
264 root_layer->SetScrollOffset(gfx::ScrollOffset(0, 0));
265 scrollbar_layer->SetBounds(gfx::Size(70, 10));
266 scrollbar_layer->SetScrollLayer(root_layer->id());
267 scrollbar_layer->SetClipLayer(root_clip_layer->id());
268 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
269 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
270 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
271 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
272 scrollbar_layer->UpdateThumbAndTrackGeometry();
273 LayerImpl* root_clip_layer_impl = nullptr;
274 LayerImpl* root_layer_impl = nullptr;
275 PaintedScrollbarLayerImpl* scrollbar_layer_impl = nullptr;
277 // Thumb is at the edge of the scrollbar (should be inset to
278 // the start of the track within the scrollbar layer's
279 // position).
280 UPDATE_AND_EXTRACT_LAYER_POINTERS();
281 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
282 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
284 // Under-scroll (thumb position should clamp and be unchanged).
285 root_layer->SetScrollOffset(gfx::ScrollOffset(-5, 0));
287 UPDATE_AND_EXTRACT_LAYER_POINTERS();
288 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
289 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
291 // Over-scroll (thumb position should clamp on the far side).
292 root_layer->SetScrollOffset(gfx::ScrollOffset(85, 0));
294 UPDATE_AND_EXTRACT_LAYER_POINTERS();
295 EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(),
296 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
298 // Change thumb thickness and length.
299 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(4);
300 scrollbar_layer->fake_scrollbar()->set_thumb_length(6);
302 UPDATE_AND_EXTRACT_LAYER_POINTERS();
303 EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(),
304 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
306 // Shrink the scrollbar layer to cover only the track.
307 scrollbar_layer->SetBounds(gfx::Size(50, 10));
308 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(30, 10));
309 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
311 UPDATE_AND_EXTRACT_LAYER_POINTERS();
312 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
313 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
315 // Shrink the track in the non-scrolling dimension so that it only covers the
316 // middle third of the scrollbar layer (this does not affect the thumb
317 // position).
318 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6));
320 UPDATE_AND_EXTRACT_LAYER_POINTERS();
321 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
322 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
325 TEST(ScrollbarLayerTest, SolidColorDrawQuads) {
326 const int kThumbThickness = 3;
327 const int kTrackStart = 1;
328 const int kTrackLength = 100;
330 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
331 LayerTreeSettings layer_tree_settings;
332 scoped_ptr<FakeLayerTreeHost> host =
333 FakeLayerTreeHost::Create(&client, layer_tree_settings);
335 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
336 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
337 host.get(), scrollbar.Pass(), false, true, kThumbThickness, kTrackStart);
338 ScrollbarLayerImplBase* scrollbar_layer_impl =
339 static_cast<SolidColorScrollbarLayerImpl*>(
340 layer_impl_tree_root->children()[1]);
341 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
342 scrollbar_layer_impl->SetCurrentPos(10.f);
343 scrollbar_layer_impl->SetMaximum(100);
344 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.4f);
346 // Thickness should be overridden to 3.
348 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
349 AppendQuadsData data;
350 scrollbar_layer_impl->AppendQuads(render_pass.get(), Occlusion(), &data);
352 const QuadList& quads = render_pass->quad_list;
353 ASSERT_EQ(1u, quads.size());
354 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
355 EXPECT_RECT_EQ(gfx::Rect(6, 0, 39, 3), quads.front()->rect);
358 // Contents scale should scale the draw quad.
359 scrollbar_layer_impl->draw_properties().contents_scale_x = 2.f;
360 scrollbar_layer_impl->draw_properties().contents_scale_y = 2.f;
362 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
363 AppendQuadsData data;
364 scrollbar_layer_impl->AppendQuads(render_pass.get(), Occlusion(), &data);
366 const QuadList& quads = render_pass->quad_list;
367 ASSERT_EQ(1u, quads.size());
368 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
369 EXPECT_RECT_EQ(gfx::Rect(12, 0, 78, 6), quads.front()->rect);
371 scrollbar_layer_impl->draw_properties().contents_scale_x = 1.f;
372 scrollbar_layer_impl->draw_properties().contents_scale_y = 1.f;
374 // For solid color scrollbars, position and size should reflect the
375 // current viewport state.
376 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.2f);
378 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
379 AppendQuadsData data;
380 scrollbar_layer_impl->AppendQuads(render_pass.get(), Occlusion(), &data);
382 const QuadList& quads = render_pass->quad_list;
383 ASSERT_EQ(1u, quads.size());
384 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
385 EXPECT_RECT_EQ(gfx::Rect(8, 0, 19, 3), quads.front()->rect);
388 // We shouldn't attempt div-by-zero when the maximum is zero.
389 scrollbar_layer_impl->SetCurrentPos(0.f);
390 scrollbar_layer_impl->SetMaximum(0);
392 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
393 AppendQuadsData data;
394 scrollbar_layer_impl->AppendQuads(render_pass.get(), Occlusion(), &data);
396 const QuadList& quads = render_pass->quad_list;
397 ASSERT_EQ(1u, quads.size());
398 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
399 EXPECT_RECT_EQ(gfx::Rect(1, 0, 19, 3), quads.front()->rect);
403 TEST(ScrollbarLayerTest, LayerDrivenSolidColorDrawQuads) {
404 const int kThumbThickness = 3;
405 const int kTrackStart = 0;
406 const int kTrackLength = 10;
408 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
409 LayerTreeSettings layer_tree_settings;
410 scoped_ptr<FakeLayerTreeHost> host =
411 FakeLayerTreeHost::Create(&client, layer_tree_settings);
413 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
416 scoped_refptr<Layer> layer_tree_root = Layer::Create();
417 scoped_refptr<Layer> scroll_layer = Layer::Create();
418 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
419 scoped_refptr<Layer> child1 = Layer::Create();
420 scoped_refptr<Layer> child2;
421 const bool kIsLeftSideVerticalScrollbar = false;
422 child2 = SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
423 kThumbThickness,
424 kTrackStart,
425 kIsLeftSideVerticalScrollbar,
426 child1->id());
427 child2->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
428 child2->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
429 scroll_layer->AddChild(child1);
430 scroll_layer->InsertChild(child2, 1);
431 layer_tree_root->AddChild(scroll_layer);
432 host->SetRootLayer(layer_tree_root);
434 LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
435 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
437 ScrollbarLayerImplBase* scrollbar_layer_impl =
438 static_cast<PaintedScrollbarLayerImpl*>(scroll_layer_impl->children()[1]);
440 // Choose layer bounds to give max_scroll_offset = (8, 8).
441 layer_impl_tree_root->SetBounds(gfx::Size(2, 2));
442 scroll_layer_impl->SetBounds(gfx::Size(10, 10));
443 scroll_layer_impl->ScrollBy(gfx::Vector2dF(4.f, 0.f));
445 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
446 scrollbar_layer_impl->SetCurrentPos(4.f);
447 scrollbar_layer_impl->SetMaximum(8);
450 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
452 AppendQuadsData data;
453 scrollbar_layer_impl->AppendQuads(render_pass.get(), Occlusion(), &data);
455 const QuadList& quads = render_pass->quad_list;
456 ASSERT_EQ(1u, quads.size());
457 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
458 EXPECT_RECT_EQ(gfx::Rect(3, 0, 3, 3), quads.front()->rect);
462 class ScrollbarLayerSolidColorThumbTest : public testing::Test {
463 public:
464 ScrollbarLayerSolidColorThumbTest() {
465 LayerTreeSettings layer_tree_settings;
466 host_impl_.reset(new FakeLayerTreeHostImpl(
467 layer_tree_settings, &proxy_, &shared_bitmap_manager_));
469 const int kThumbThickness = 3;
470 const int kTrackStart = 0;
471 const bool kIsLeftSideVerticalScrollbar = false;
472 const bool kIsOverlayScrollbar = false;
474 horizontal_scrollbar_layer_ =
475 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
477 HORIZONTAL,
478 kThumbThickness,
479 kTrackStart,
480 kIsLeftSideVerticalScrollbar,
481 kIsOverlayScrollbar);
482 vertical_scrollbar_layer_ =
483 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
485 VERTICAL,
486 kThumbThickness,
487 kTrackStart,
488 kIsLeftSideVerticalScrollbar,
489 kIsOverlayScrollbar);
492 protected:
493 FakeImplProxy proxy_;
494 TestSharedBitmapManager shared_bitmap_manager_;
495 scoped_ptr<FakeLayerTreeHostImpl> host_impl_;
496 scoped_ptr<SolidColorScrollbarLayerImpl> horizontal_scrollbar_layer_;
497 scoped_ptr<SolidColorScrollbarLayerImpl> vertical_scrollbar_layer_;
500 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbLength) {
501 horizontal_scrollbar_layer_->SetCurrentPos(0);
502 horizontal_scrollbar_layer_->SetMaximum(10);
504 // Simple case - one third of the scrollable area is visible, so the thumb
505 // should be one third as long as the track.
506 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.33f);
507 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
508 EXPECT_EQ(33, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
510 // The thumb's length should never be less than its thickness.
511 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.01f);
512 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
513 EXPECT_EQ(3, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
516 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbPosition) {
517 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
518 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.1f);
520 horizontal_scrollbar_layer_->SetCurrentPos(0);
521 horizontal_scrollbar_layer_->SetMaximum(100);
522 EXPECT_EQ(0, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
523 EXPECT_EQ(10, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
525 horizontal_scrollbar_layer_->SetCurrentPos(100);
526 // The thumb is 10px long and the track is 100px, so the maximum thumb
527 // position is 90px.
528 EXPECT_EQ(90, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
530 horizontal_scrollbar_layer_->SetCurrentPos(80);
531 // The scroll position is 80% of the maximum, so the thumb's position should
532 // be at 80% of its maximum or 72px.
533 EXPECT_EQ(72, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
536 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbVerticalAdjust) {
537 SolidColorScrollbarLayerImpl* layers[2] =
538 { horizontal_scrollbar_layer_.get(), vertical_scrollbar_layer_.get() };
539 for (size_t i = 0; i < 2; ++i) {
540 layers[i]->SetVisibleToTotalLengthRatio(0.2f);
541 layers[i]->SetCurrentPos(25);
542 layers[i]->SetMaximum(100);
544 layers[0]->SetBounds(gfx::Size(100, 3));
545 layers[1]->SetBounds(gfx::Size(3, 100));
547 EXPECT_RECT_EQ(gfx::RectF(20.f, 0.f, 20.f, 3.f),
548 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
549 EXPECT_RECT_EQ(gfx::RectF(0.f, 20.f, 3.f, 20.f),
550 vertical_scrollbar_layer_->ComputeThumbQuadRect());
552 horizontal_scrollbar_layer_->SetVerticalAdjust(10.f);
553 vertical_scrollbar_layer_->SetVerticalAdjust(10.f);
555 // The vertical adjustment factor has two effects:
556 // 1.) Moves the horizontal scrollbar down
557 // 2.) Increases the vertical scrollbar's effective track length which both
558 // increases the thumb's length and its position within the track.
559 EXPECT_RECT_EQ(gfx::Rect(20.f, 10.f, 20.f, 3.f),
560 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
561 EXPECT_RECT_EQ(gfx::Rect(0.f, 22, 3.f, 22.f),
562 vertical_scrollbar_layer_->ComputeThumbQuadRect());
565 class ScrollbarLayerTestMaxTextureSize : public LayerTreeTest {
566 public:
567 ScrollbarLayerTestMaxTextureSize() {}
569 void SetScrollbarBounds(const gfx::Size& bounds) { bounds_ = bounds; }
571 void BeginTest() override {
572 scroll_layer_ = Layer::Create();
573 layer_tree_host()->root_layer()->AddChild(scroll_layer_);
575 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
576 scrollbar_layer_ =
577 PaintedScrollbarLayer::Create(scrollbar.Pass(), scroll_layer_->id());
578 scrollbar_layer_->SetScrollLayer(scroll_layer_->id());
579 scrollbar_layer_->SetLayerTreeHost(layer_tree_host());
580 scrollbar_layer_->SetBounds(bounds_);
581 layer_tree_host()->root_layer()->AddChild(scrollbar_layer_);
583 PostSetNeedsCommitToMainThread();
586 void DidCommitAndDrawFrame() override {
587 const int kMaxTextureSize =
588 layer_tree_host()->GetRendererCapabilities().max_texture_size;
590 // Check first that we're actually testing something.
591 EXPECT_GT(scrollbar_layer_->bounds().width(), kMaxTextureSize);
593 EXPECT_EQ(scrollbar_layer_->content_bounds().width(),
594 kMaxTextureSize - 1);
595 EXPECT_EQ(scrollbar_layer_->content_bounds().height(),
596 kMaxTextureSize - 1);
598 EndTest();
601 void AfterTest() override {}
603 private:
604 scoped_refptr<PaintedScrollbarLayer> scrollbar_layer_;
605 scoped_refptr<Layer> scroll_layer_;
606 gfx::Size bounds_;
609 TEST_F(ScrollbarLayerTestMaxTextureSize, DirectRenderer) {
610 scoped_ptr<TestWebGraphicsContext3D> context =
611 TestWebGraphicsContext3D::Create();
612 int max_size = 0;
613 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
614 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
615 RunTest(true, false, true);
618 TEST_F(ScrollbarLayerTestMaxTextureSize, DelegatingRenderer) {
619 scoped_ptr<TestWebGraphicsContext3D> context =
620 TestWebGraphicsContext3D::Create();
621 int max_size = 0;
622 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
623 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
624 RunTest(true, true, true);
627 class FakeLayerTreeHost : public LayerTreeHost {
628 public:
629 FakeLayerTreeHost(FakeLayerTreeHostClient* client,
630 const LayerTreeSettings& settings)
631 : LayerTreeHost(client, nullptr, nullptr, settings),
632 next_id_(1),
633 total_ui_resource_created_(0),
634 total_ui_resource_deleted_(0) {
635 InitializeSingleThreaded(client,
636 base::MessageLoopProxy::current(),
637 nullptr);
640 UIResourceId CreateUIResource(UIResourceClient* content) override {
641 total_ui_resource_created_++;
642 UIResourceId nid = next_id_++;
643 ui_resource_bitmap_map_.insert(
644 std::make_pair(nid, content->GetBitmap(nid, false)));
645 return nid;
648 // Deletes a UI resource. May safely be called more than once.
649 void DeleteUIResource(UIResourceId id) override {
650 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
651 if (iter != ui_resource_bitmap_map_.end()) {
652 ui_resource_bitmap_map_.erase(iter);
653 total_ui_resource_deleted_++;
657 size_t UIResourceCount() { return ui_resource_bitmap_map_.size(); }
658 int TotalUIResourceDeleted() { return total_ui_resource_deleted_; }
659 int TotalUIResourceCreated() { return total_ui_resource_created_; }
661 gfx::Size ui_resource_size(UIResourceId id) {
662 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
663 if (iter != ui_resource_bitmap_map_.end())
664 return iter->second.GetSize();
665 return gfx::Size();
668 UIResourceBitmap* ui_resource_bitmap(UIResourceId id) {
669 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
670 if (iter != ui_resource_bitmap_map_.end())
671 return &iter->second;
672 return nullptr;
675 private:
676 typedef base::hash_map<UIResourceId, UIResourceBitmap>
677 UIResourceBitmapMap;
678 UIResourceBitmapMap ui_resource_bitmap_map_;
680 int next_id_;
681 int total_ui_resource_created_;
682 int total_ui_resource_deleted_;
685 class ScrollbarLayerTestResourceCreationAndRelease : public testing::Test {
686 public:
687 ScrollbarLayerTestResourceCreationAndRelease()
688 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
690 void TestResourceUpload(int num_updates,
691 size_t expected_resources,
692 int expected_created,
693 int expected_deleted,
694 bool use_solid_color_scrollbar) {
695 layer_tree_host_.reset(
696 new FakeLayerTreeHost(&fake_client_, layer_tree_settings_));
698 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, false));
699 scoped_refptr<Layer> layer_tree_root = Layer::Create();
700 scoped_refptr<Layer> content_layer = Layer::Create();
701 scoped_refptr<Layer> scrollbar_layer;
702 if (use_solid_color_scrollbar) {
703 const int kThumbThickness = 3;
704 const int kTrackStart = 0;
705 const bool kIsLeftSideVerticalScrollbar = false;
706 scrollbar_layer =
707 SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
708 kThumbThickness,
709 kTrackStart,
710 kIsLeftSideVerticalScrollbar,
711 layer_tree_root->id());
712 } else {
713 scrollbar_layer = PaintedScrollbarLayer::Create(scrollbar.Pass(),
714 layer_tree_root->id());
716 layer_tree_root->AddChild(content_layer);
717 layer_tree_root->AddChild(scrollbar_layer);
719 layer_tree_host_->SetRootLayer(layer_tree_root);
721 scrollbar_layer->SetIsDrawable(true);
722 scrollbar_layer->SetBounds(gfx::Size(100, 100));
723 layer_tree_root->SetScrollOffset(gfx::ScrollOffset(10, 20));
724 layer_tree_root->SetBounds(gfx::Size(100, 200));
725 content_layer->SetBounds(gfx::Size(100, 200));
726 scrollbar_layer->draw_properties().content_bounds = gfx::Size(100, 200);
727 scrollbar_layer->draw_properties().visible_content_rect =
728 gfx::Rect(0, 0, 100, 200);
729 scrollbar_layer->CreateRenderSurface();
730 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
732 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
733 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
735 ResourceUpdateQueue queue;
736 gfx::Rect screen_space_clip_rect;
737 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
739 scrollbar_layer->SavePaintProperties();
740 for (int update_counter = 0; update_counter < num_updates; update_counter++)
741 scrollbar_layer->Update(&queue, &occlusion_tracker);
743 // A non-solid-color scrollbar should have requested two textures.
744 EXPECT_EQ(expected_resources, layer_tree_host_->UIResourceCount());
745 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
746 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
748 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
750 scrollbar_layer->ClearRenderSurface();
753 protected:
754 FakeLayerTreeHostClient fake_client_;
755 LayerTreeSettings layer_tree_settings_;
756 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
759 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, ResourceUpload) {
760 bool use_solid_color_scrollbars = false;
761 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
762 int num_updates[3] = {1, 5, 10};
763 for (int j = 0; j < 3; j++) {
764 TestResourceUpload(num_updates[j],
766 num_updates[j] * 2,
767 (num_updates[j] - 1) * 2,
768 use_solid_color_scrollbars);
772 TEST_F(ScrollbarLayerTestResourceCreationAndRelease,
773 SolidColorNoResourceUpload) {
774 bool use_solid_color_scrollbars = true;
775 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
776 TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars);
779 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, TestResourceUpdate) {
780 FakeLayerTreeHostClient fake_client_(FakeLayerTreeHostClient::DIRECT_3D);
781 LayerTreeSettings layer_tree_settings_;
782 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
784 layer_tree_host_.reset(
785 new FakeLayerTreeHost(&fake_client_, layer_tree_settings_));
787 gfx::Point scrollbar_location(0, 185);
788 scoped_refptr<Layer> layer_tree_root = Layer::Create();
789 scoped_refptr<Layer> content_layer = Layer::Create();
790 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
791 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root->id());
793 layer_tree_root->AddChild(content_layer);
794 layer_tree_root->AddChild(scrollbar_layer);
796 layer_tree_host_->SetRootLayer(layer_tree_root);
798 scrollbar_layer->SetIsDrawable(true);
799 scrollbar_layer->SetBounds(gfx::Size(100, 15));
800 scrollbar_layer->SetPosition(scrollbar_location);
801 layer_tree_root->SetBounds(gfx::Size(100, 200));
802 content_layer->SetBounds(gfx::Size(100, 200));
804 scrollbar_layer->draw_properties().content_bounds = gfx::Size(100, 200);
805 scrollbar_layer->draw_properties().visible_content_rect =
806 gfx::Rect(0, 0, 100, 200);
808 scrollbar_layer->CreateRenderSurface();
809 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
811 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
812 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
814 ResourceUpdateQueue queue;
815 gfx::Rect screen_space_clip_rect;
816 size_t resource_count;
817 int expected_created, expected_deleted;
818 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
819 scrollbar_layer->SavePaintProperties();
821 resource_count = 2;
822 expected_created = 2;
823 expected_deleted = 0;
824 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
825 EXPECT_NE(0, scrollbar_layer->track_resource_id());
826 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
827 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
828 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
829 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
831 resource_count = 0;
832 expected_created = 2;
833 expected_deleted = 2;
834 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
835 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
836 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
837 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
838 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
839 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
840 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
842 resource_count = 0;
843 expected_created = 2;
844 expected_deleted = 2;
845 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
846 EXPECT_FALSE(scrollbar_layer->Update(&queue, &occlusion_tracker));
847 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
848 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
849 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
850 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
851 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
853 resource_count = 2;
854 expected_created = 4;
855 expected_deleted = 2;
856 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
857 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
858 EXPECT_NE(0, scrollbar_layer->track_resource_id());
859 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
860 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
861 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
862 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
864 resource_count = 1;
865 expected_created = 5;
866 expected_deleted = 4;
867 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
868 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
869 EXPECT_NE(0, scrollbar_layer->track_resource_id());
870 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
871 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
872 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
873 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
875 resource_count = 0;
876 expected_created = 5;
877 expected_deleted = 5;
878 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
879 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
880 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
881 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
882 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
883 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
884 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
886 resource_count = 2;
887 expected_created = 7;
888 expected_deleted = 5;
889 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
890 scrollbar_layer->fake_scrollbar()->set_has_thumb(true);
891 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
892 EXPECT_NE(0, scrollbar_layer->track_resource_id());
893 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
895 resource_count = 1;
896 expected_created = 8;
897 expected_deleted = 7;
898 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
899 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
900 scrollbar_layer->SetBounds(gfx::Size(90, 15));
901 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
902 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
903 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
904 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
905 EXPECT_EQ(
906 gfx::Size(90, 15),
907 layer_tree_host_->ui_resource_size(scrollbar_layer->track_resource_id()));
909 scrollbar_layer->ResetNeedsDisplayForTesting();
910 EXPECT_FALSE(scrollbar_layer->Update(&queue, &occlusion_tracker));
911 EXPECT_NE(0, scrollbar_layer->track_resource_id());
912 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
913 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
914 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
915 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
917 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
918 scrollbar_layer->ClearRenderSurface();
921 class ScaledScrollbarLayerTestResourceCreation : public testing::Test {
922 public:
923 ScaledScrollbarLayerTestResourceCreation()
924 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
926 void TestResourceUpload(const float test_scale) {
927 layer_tree_host_.reset(
928 new FakeLayerTreeHost(&fake_client_, layer_tree_settings_));
930 gfx::Point scrollbar_location(0, 185);
931 scoped_refptr<Layer> layer_tree_root = Layer::Create();
932 scoped_refptr<Layer> content_layer = Layer::Create();
933 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
934 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root->id());
936 layer_tree_root->AddChild(content_layer);
937 layer_tree_root->AddChild(scrollbar_layer);
939 layer_tree_host_->SetRootLayer(layer_tree_root);
941 scrollbar_layer->SetIsDrawable(true);
942 scrollbar_layer->SetBounds(gfx::Size(100, 15));
943 scrollbar_layer->SetPosition(scrollbar_location);
944 layer_tree_root->SetBounds(gfx::Size(100, 200));
945 content_layer->SetBounds(gfx::Size(100, 200));
946 gfx::SizeF scaled_size =
947 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
948 gfx::PointF scaled_location =
949 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
950 scrollbar_layer->draw_properties().content_bounds =
951 gfx::Size(scaled_size.width(), scaled_size.height());
952 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
953 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
954 scrollbar_layer->draw_properties().visible_content_rect =
955 gfx::Rect(scaled_location.x(),
956 scaled_location.y(),
957 scaled_size.width(),
958 scaled_size.height());
959 scrollbar_layer->CreateRenderSurface();
960 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
962 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
963 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
965 ResourceUpdateQueue queue;
966 gfx::Rect screen_space_clip_rect;
967 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
968 scrollbar_layer->SavePaintProperties();
969 scrollbar_layer->Update(&queue, &occlusion_tracker);
971 // Verify that we have not generated any content uploads that are larger
972 // than their destination textures.
974 gfx::Size track_size = layer_tree_host_->ui_resource_size(
975 scrollbar_layer->track_resource_id());
976 gfx::Size thumb_size = layer_tree_host_->ui_resource_size(
977 scrollbar_layer->thumb_resource_id());
979 EXPECT_LE(track_size.width(), scrollbar_layer->content_bounds().width());
980 EXPECT_LE(track_size.height(), scrollbar_layer->content_bounds().height());
981 EXPECT_LE(thumb_size.width(), scrollbar_layer->content_bounds().width());
982 EXPECT_LE(thumb_size.height(), scrollbar_layer->content_bounds().height());
984 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
986 scrollbar_layer->ClearRenderSurface();
989 protected:
990 FakeLayerTreeHostClient fake_client_;
991 LayerTreeSettings layer_tree_settings_;
992 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
995 TEST_F(ScaledScrollbarLayerTestResourceCreation, ScaledResourceUpload) {
996 // Pick a test scale that moves the scrollbar's (non-zero) position to
997 // a non-pixel-aligned location.
998 TestResourceUpload(.041f);
999 TestResourceUpload(1.41f);
1000 TestResourceUpload(4.1f);
1003 class ScaledScrollbarLayerTestScaledRasterization : public testing::Test {
1004 public:
1005 ScaledScrollbarLayerTestScaledRasterization()
1006 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
1008 void TestScale(const gfx::Rect scrollbar_rect, const float test_scale) {
1009 layer_tree_host_.reset(
1010 new FakeLayerTreeHost(&fake_client_, layer_tree_settings_));
1012 bool paint_during_update = true;
1013 bool has_thumb = false;
1014 scoped_refptr<Layer> layer_tree_root = Layer::Create();
1015 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
1016 FakePaintedScrollbarLayer::Create(paint_during_update,
1017 has_thumb,
1018 layer_tree_root->id());
1020 layer_tree_root->AddChild(scrollbar_layer);
1022 layer_tree_host_->SetRootLayer(layer_tree_root);
1024 scrollbar_layer->SetBounds(scrollbar_rect.size());
1025 scrollbar_layer->SetPosition(scrollbar_rect.origin());
1026 scrollbar_layer->fake_scrollbar()->set_location(scrollbar_rect.origin());
1027 scrollbar_layer->fake_scrollbar()->set_track_rect(scrollbar_rect);
1028 gfx::SizeF scaled_size =
1029 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
1030 gfx::PointF scaled_location =
1031 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
1032 scrollbar_layer->draw_properties().content_bounds =
1033 gfx::Size(scaled_size.width(), scaled_size.height());
1034 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
1035 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
1036 scrollbar_layer->draw_properties().visible_content_rect =
1037 gfx::Rect(scaled_location.x(),
1038 scaled_location.y(),
1039 scaled_size.width(),
1040 scaled_size.height());
1042 ResourceUpdateQueue queue;
1043 gfx::Rect screen_space_clip_rect;
1044 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
1045 scrollbar_layer->SavePaintProperties();
1047 scrollbar_layer->Update(&queue, &occlusion_tracker);
1049 UIResourceBitmap* bitmap = layer_tree_host_->ui_resource_bitmap(
1050 scrollbar_layer->track_resource_id());
1052 DCHECK(bitmap);
1054 AutoLockUIResourceBitmap locked_bitmap(*bitmap);
1056 const SkColor* pixels =
1057 reinterpret_cast<const SkColor*>(locked_bitmap.GetPixels());
1058 SkColor color = argb_to_skia(
1059 scrollbar_layer->fake_scrollbar()->paint_fill_color());
1060 int width = bitmap->GetSize().width();
1061 int height = bitmap->GetSize().height();
1063 // Make sure none of the corners of the bitmap were inadvertently clipped.
1064 EXPECT_EQ(color, pixels[0])
1065 << "Top left pixel doesn't match scrollbar color.";
1067 EXPECT_EQ(color, pixels[width - 1])
1068 << "Top right pixel doesn't match scrollbar color.";
1070 EXPECT_EQ(color, pixels[width * (height - 1)])
1071 << "Bottom left pixel doesn't match scrollbar color.";
1073 EXPECT_EQ(color, pixels[width * height - 1])
1074 << "Bottom right pixel doesn't match scrollbar color.";
1077 protected:
1078 // On Android, Skia uses ABGR
1079 static SkColor argb_to_skia(SkColor c) {
1080 return (SkColorGetA(c) << SK_A32_SHIFT) |
1081 (SkColorGetR(c) << SK_R32_SHIFT) |
1082 (SkColorGetG(c) << SK_G32_SHIFT) |
1083 (SkColorGetB(c) << SK_B32_SHIFT);
1086 FakeLayerTreeHostClient fake_client_;
1087 LayerTreeSettings layer_tree_settings_;
1088 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
1091 TEST_F(ScaledScrollbarLayerTestScaledRasterization, TestLostPrecisionInClip) {
1092 // Try rasterization at coordinates and scale that caused problematic
1093 // rounding and clipping errors.
1094 // Vertical Scrollbars.
1095 TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f);
1096 TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f);
1098 // Horizontal Scrollbars.
1099 TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f);
1100 TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f);
1103 } // namespace
1104 } // namespace cc