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 "base/thread_task_runner_handle.h"
7 #include "cc/animation/scrollbar_animation_controller.h"
8 #include "cc/layers/append_quads_data.h"
9 #include "cc/layers/painted_scrollbar_layer.h"
10 #include "cc/layers/painted_scrollbar_layer_impl.h"
11 #include "cc/layers/scrollbar_layer_interface.h"
12 #include "cc/layers/solid_color_scrollbar_layer.h"
13 #include "cc/layers/solid_color_scrollbar_layer_impl.h"
14 #include "cc/quads/solid_color_draw_quad.h"
15 #include "cc/resources/resource_update_queue.h"
16 #include "cc/test/fake_impl_proxy.h"
17 #include "cc/test/fake_layer_tree_host.h"
18 #include "cc/test/fake_layer_tree_host_client.h"
19 #include "cc/test/fake_layer_tree_host_impl.h"
20 #include "cc/test/fake_painted_scrollbar_layer.h"
21 #include "cc/test/fake_scrollbar.h"
22 #include "cc/test/geometry_test_utils.h"
23 #include "cc/test/layer_tree_test.h"
24 #include "cc/test/mock_occlusion_tracker.h"
25 #include "cc/test/test_task_graph_runner.h"
26 #include "cc/test/test_web_graphics_context_3d.h"
27 #include "cc/trees/layer_tree_host.h"
28 #include "cc/trees/layer_tree_impl.h"
29 #include "cc/trees/occlusion_tracker.h"
30 #include "cc/trees/single_thread_proxy.h"
31 #include "cc/trees/tree_synchronizer.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h"
38 LayerImpl
* LayerImplForScrollAreaAndScrollbar(FakeLayerTreeHost
* host
,
39 scoped_ptr
<Scrollbar
> scrollbar
,
41 bool use_solid_color_scrollbar
,
44 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create();
45 scoped_refptr
<Layer
> child1
= Layer::Create();
46 scoped_refptr
<Layer
> child2
;
47 if (use_solid_color_scrollbar
) {
48 const bool kIsLeftSideVerticalScrollbar
= false;
49 child2
= SolidColorScrollbarLayer::Create(scrollbar
->Orientation(),
52 kIsLeftSideVerticalScrollbar
,
55 child2
= PaintedScrollbarLayer::Create(scrollbar
.Pass(), child1
->id());
57 child2
->ToScrollbarLayer()->SetClipLayer(layer_tree_root
->id());
58 layer_tree_root
->AddChild(child1
);
59 layer_tree_root
->InsertChild(child2
, reverse_order
? 0 : 1);
60 host
->SetRootLayer(layer_tree_root
);
61 return host
->CommitAndCreateLayerImplTree();
64 class FakeResourceTrackingLayerTreeHost
: public FakeLayerTreeHost
{
66 FakeResourceTrackingLayerTreeHost(FakeLayerTreeHostClient
* client
,
67 LayerTreeHost::InitParams
* params
)
68 : FakeLayerTreeHost(client
, params
),
70 total_ui_resource_created_(0),
71 total_ui_resource_deleted_(0) {
72 InitializeSingleThreaded(client
, base::ThreadTaskRunnerHandle::Get(),
76 UIResourceId
CreateUIResource(UIResourceClient
* content
) override
{
77 total_ui_resource_created_
++;
78 UIResourceId nid
= next_id_
++;
79 ui_resource_bitmap_map_
.insert(
80 std::make_pair(nid
, content
->GetBitmap(nid
, false)));
84 // Deletes a UI resource. May safely be called more than once.
85 void DeleteUIResource(UIResourceId id
) override
{
86 UIResourceBitmapMap::iterator iter
= ui_resource_bitmap_map_
.find(id
);
87 if (iter
!= ui_resource_bitmap_map_
.end()) {
88 ui_resource_bitmap_map_
.erase(iter
);
89 total_ui_resource_deleted_
++;
93 size_t UIResourceCount() { return ui_resource_bitmap_map_
.size(); }
94 int TotalUIResourceDeleted() { return total_ui_resource_deleted_
; }
95 int TotalUIResourceCreated() { return total_ui_resource_created_
; }
97 gfx::Size
ui_resource_size(UIResourceId id
) {
98 UIResourceBitmapMap::iterator iter
= ui_resource_bitmap_map_
.find(id
);
99 if (iter
!= ui_resource_bitmap_map_
.end())
100 return iter
->second
.GetSize();
104 UIResourceBitmap
* ui_resource_bitmap(UIResourceId id
) {
105 UIResourceBitmapMap::iterator iter
= ui_resource_bitmap_map_
.find(id
);
106 if (iter
!= ui_resource_bitmap_map_
.end())
107 return &iter
->second
;
112 using UIResourceBitmapMap
= base::hash_map
<UIResourceId
, UIResourceBitmap
>;
113 UIResourceBitmapMap ui_resource_bitmap_map_
;
116 int total_ui_resource_created_
;
117 int total_ui_resource_deleted_
;
120 class ScrollbarLayerTest
: public testing::Test
{
122 ScrollbarLayerTest() : fake_client_(FakeLayerTreeHostClient::DIRECT_3D
) {
123 layer_tree_settings_
.single_thread_proxy_scheduler
= false;
125 LayerTreeHost::InitParams params
;
126 params
.client
= &fake_client_
;
127 params
.settings
= &layer_tree_settings_
;
129 layer_tree_host_
.reset(
130 new FakeResourceTrackingLayerTreeHost(&fake_client_
, ¶ms
));
131 fake_client_
.SetLayerTreeHost(layer_tree_host_
.get());
132 // Force output surface creation for renderer capabilities.
133 layer_tree_host_
->Composite(base::TimeTicks());
134 EXPECT_FALSE(layer_tree_host_
->output_surface_lost());
138 FakeLayerTreeHostClient fake_client_
;
139 LayerTreeSettings layer_tree_settings_
;
140 scoped_ptr
<FakeResourceTrackingLayerTreeHost
> layer_tree_host_
;
143 TEST_F(ScrollbarLayerTest
, ResolveScrollLayerPointer
) {
144 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
145 LayerImpl
* layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
146 layer_tree_host_
.get(), scrollbar
.Pass(), false, false, 0, 0);
148 LayerImpl
* cc_child1
= layer_impl_tree_root
->children()[0];
149 PaintedScrollbarLayerImpl
* cc_child2
=
150 static_cast<PaintedScrollbarLayerImpl
*>(
151 layer_impl_tree_root
->children()[1]);
153 EXPECT_EQ(cc_child1
->scrollbars()->size(), 1UL);
154 EXPECT_EQ(*(cc_child1
->scrollbars()->begin()), cc_child2
);
157 TEST_F(ScrollbarLayerTest
, ResolveScrollLayerPointer_ReverseOrder
) {
158 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
159 LayerImpl
* layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
160 layer_tree_host_
.get(), scrollbar
.Pass(), true, false, 0, 0);
162 PaintedScrollbarLayerImpl
* cc_child1
=
163 static_cast<PaintedScrollbarLayerImpl
*>(
164 layer_impl_tree_root
->children()[0]);
165 LayerImpl
* cc_child2
= layer_impl_tree_root
->children()[1];
167 EXPECT_EQ(cc_child2
->scrollbars()->size(), 1UL);
168 EXPECT_EQ(*(cc_child2
->scrollbars()->begin()), cc_child1
);
171 TEST_F(ScrollbarLayerTest
, ShouldScrollNonOverlayOnMainThread
) {
172 // Create and attach a non-overlay scrollbar.
173 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
174 LayerImpl
* layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
175 layer_tree_host_
.get(), scrollbar
.Pass(), false, false, 0, 0);
176 PaintedScrollbarLayerImpl
* scrollbar_layer_impl
=
177 static_cast<PaintedScrollbarLayerImpl
*>(
178 layer_impl_tree_root
->children()[1]);
180 // When the scrollbar is not an overlay scrollbar, the scroll should be
181 // responded to on the main thread as the compositor does not yet implement
182 // scrollbar scrolling.
184 InputHandler::SCROLL_ON_MAIN_THREAD
,
185 scrollbar_layer_impl
->TryScroll(gfx::Point(0, 0), InputHandler::GESTURE
,
186 SCROLL_BLOCKS_ON_NONE
));
188 // Create and attach an overlay scrollbar.
189 scrollbar
.reset(new FakeScrollbar(false, false, true));
191 layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
192 layer_tree_host_
.get(), scrollbar
.Pass(), false, false, 0, 0);
193 scrollbar_layer_impl
= static_cast<PaintedScrollbarLayerImpl
*>(
194 layer_impl_tree_root
->children()[1]);
196 // The user shouldn't be able to drag an overlay scrollbar and the scroll
197 // may be handled in the compositor.
199 InputHandler::SCROLL_IGNORED
,
200 scrollbar_layer_impl
->TryScroll(gfx::Point(0, 0), InputHandler::GESTURE
,
201 SCROLL_BLOCKS_ON_NONE
));
204 TEST_F(ScrollbarLayerTest
, ScrollOffsetSynchronization
) {
205 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
206 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create();
207 scoped_refptr
<Layer
> scroll_layer
= Layer::Create();
208 scoped_refptr
<Layer
> content_layer
= Layer::Create();
209 scoped_refptr
<Layer
> scrollbar_layer
=
210 PaintedScrollbarLayer::Create(scrollbar
.Pass(), layer_tree_root
->id());
212 // Choose bounds to give max_scroll_offset = (30, 50).
213 layer_tree_root
->SetBounds(gfx::Size(70, 150));
214 scroll_layer
->SetScrollClipLayerId(layer_tree_root
->id());
215 scroll_layer
->SetScrollOffset(gfx::ScrollOffset(10, 20));
216 scroll_layer
->SetBounds(gfx::Size(100, 200));
217 content_layer
->SetBounds(gfx::Size(100, 200));
219 layer_tree_host_
->SetRootLayer(layer_tree_root
);
220 layer_tree_root
->AddChild(scroll_layer
);
221 scroll_layer
->AddChild(content_layer
);
222 layer_tree_root
->AddChild(scrollbar_layer
);
223 scrollbar_layer
->ToScrollbarLayer()->SetScrollLayer(scroll_layer
->id());
224 scrollbar_layer
->ToScrollbarLayer()->SetClipLayer(layer_tree_root
->id());
226 layer_tree_root
->SavePaintProperties();
227 content_layer
->SavePaintProperties();
229 LayerImpl
* layer_impl_tree_root
=
230 layer_tree_host_
->CommitAndCreateLayerImplTree();
232 ScrollbarLayerImplBase
* cc_scrollbar_layer
=
233 static_cast<PaintedScrollbarLayerImpl
*>(
234 layer_impl_tree_root
->children()[1]);
236 EXPECT_EQ(10.f
, cc_scrollbar_layer
->current_pos());
237 EXPECT_EQ(30, cc_scrollbar_layer
->maximum());
239 layer_tree_root
->SetBounds(gfx::Size(700, 1500));
240 layer_tree_root
->SavePaintProperties();
241 scroll_layer
->SetBounds(gfx::Size(1000, 2000));
242 scroll_layer
->SetScrollOffset(gfx::ScrollOffset(100, 200));
243 scroll_layer
->SavePaintProperties();
244 content_layer
->SetBounds(gfx::Size(1000, 2000));
245 content_layer
->SavePaintProperties();
247 ScrollbarAnimationController
* scrollbar_controller
=
248 layer_impl_tree_root
->scrollbar_animation_controller();
249 layer_impl_tree_root
= layer_tree_host_
->CommitAndCreateLayerImplTree();
250 EXPECT_EQ(scrollbar_controller
,
251 layer_impl_tree_root
->scrollbar_animation_controller());
253 EXPECT_EQ(100.f
, cc_scrollbar_layer
->current_pos());
254 EXPECT_EQ(300, cc_scrollbar_layer
->maximum());
256 LayerImpl
* scroll_layer_impl
= layer_impl_tree_root
->children()[0];
257 scroll_layer_impl
->ScrollBy(gfx::Vector2d(12, 34));
259 EXPECT_EQ(112.f
, cc_scrollbar_layer
->current_pos());
260 EXPECT_EQ(300, cc_scrollbar_layer
->maximum());
263 #define UPDATE_AND_EXTRACT_LAYER_POINTERS() \
265 scrollbar_layer->UpdateInternalContentScale(); \
266 scrollbar_layer->UpdateThumbAndTrackGeometry(); \
267 root_clip_layer_impl = layer_tree_host_->CommitAndCreateLayerImplTree(); \
268 root_layer_impl = root_clip_layer_impl->children()[0]; \
269 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>( \
270 root_layer_impl->children()[1]); \
271 scrollbar_layer_impl->ScrollbarParametersDidChange(false); \
274 TEST_F(ScrollbarLayerTest
, UpdatePropertiesOfScrollBarWhenThumbRemoved
) {
275 scoped_refptr
<Layer
> root_clip_layer
= Layer::Create();
276 scoped_refptr
<Layer
> root_layer
= Layer::Create();
277 scoped_refptr
<Layer
> content_layer
= Layer::Create();
278 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
279 FakePaintedScrollbarLayer::Create(false, true, root_layer
->id());
281 root_layer
->SetScrollClipLayerId(root_clip_layer
->id());
282 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
283 root_clip_layer
->SetBounds(gfx::Size(20, 50));
284 root_layer
->SetBounds(gfx::Size(100, 50));
285 content_layer
->SetBounds(gfx::Size(100, 50));
287 layer_tree_host_
->SetRootLayer(root_clip_layer
);
288 root_clip_layer
->AddChild(root_layer
);
289 root_layer
->AddChild(content_layer
);
290 root_layer
->AddChild(scrollbar_layer
);
292 root_layer
->SetScrollOffset(gfx::ScrollOffset(0, 0));
293 scrollbar_layer
->SetBounds(gfx::Size(70, 10));
294 scrollbar_layer
->SetScrollLayer(root_layer
->id());
295 scrollbar_layer
->SetClipLayer(root_clip_layer
->id());
296 scrollbar_layer
->fake_scrollbar()->set_location(gfx::Point(20, 10));
297 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
298 scrollbar_layer
->fake_scrollbar()->set_thumb_thickness(10);
299 scrollbar_layer
->fake_scrollbar()->set_thumb_length(4);
300 LayerImpl
* root_clip_layer_impl
= nullptr;
301 LayerImpl
* root_layer_impl
= nullptr;
302 PaintedScrollbarLayerImpl
* scrollbar_layer_impl
= nullptr;
304 UPDATE_AND_EXTRACT_LAYER_POINTERS();
305 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
306 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
308 scrollbar_layer
->fake_scrollbar()->set_has_thumb(false);
310 UPDATE_AND_EXTRACT_LAYER_POINTERS();
311 EXPECT_EQ(gfx::Rect(10, 0, 0, 0).ToString(),
312 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
315 TEST_F(ScrollbarLayerTest
, ThumbRect
) {
316 scoped_refptr
<Layer
> root_clip_layer
= Layer::Create();
317 scoped_refptr
<Layer
> root_layer
= Layer::Create();
318 scoped_refptr
<Layer
> content_layer
= Layer::Create();
319 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
320 FakePaintedScrollbarLayer::Create(false, true, root_layer
->id());
322 root_layer
->SetScrollClipLayerId(root_clip_layer
->id());
323 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
324 root_clip_layer
->SetBounds(gfx::Size(20, 50));
325 root_layer
->SetBounds(gfx::Size(100, 50));
326 content_layer
->SetBounds(gfx::Size(100, 50));
328 layer_tree_host_
->SetRootLayer(root_clip_layer
);
329 root_clip_layer
->AddChild(root_layer
);
330 root_layer
->AddChild(content_layer
);
331 root_layer
->AddChild(scrollbar_layer
);
333 root_layer
->SetScrollOffset(gfx::ScrollOffset(0, 0));
334 scrollbar_layer
->SetBounds(gfx::Size(70, 10));
335 scrollbar_layer
->SetScrollLayer(root_layer
->id());
336 scrollbar_layer
->SetClipLayer(root_clip_layer
->id());
337 scrollbar_layer
->fake_scrollbar()->set_location(gfx::Point(20, 10));
338 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
339 scrollbar_layer
->fake_scrollbar()->set_thumb_thickness(10);
340 scrollbar_layer
->fake_scrollbar()->set_thumb_length(4);
341 LayerImpl
* root_clip_layer_impl
= nullptr;
342 LayerImpl
* root_layer_impl
= nullptr;
343 PaintedScrollbarLayerImpl
* scrollbar_layer_impl
= nullptr;
345 // Thumb is at the edge of the scrollbar (should be inset to
346 // the start of the track within the scrollbar layer's
348 UPDATE_AND_EXTRACT_LAYER_POINTERS();
349 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
350 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
352 // Under-scroll (thumb position should clamp and be unchanged).
353 root_layer
->SetScrollOffset(gfx::ScrollOffset(-5, 0));
355 UPDATE_AND_EXTRACT_LAYER_POINTERS();
356 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
357 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
359 // Over-scroll (thumb position should clamp on the far side).
360 root_layer
->SetScrollOffset(gfx::ScrollOffset(85, 0));
362 UPDATE_AND_EXTRACT_LAYER_POINTERS();
363 EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(),
364 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
366 // Change thumb thickness and length.
367 scrollbar_layer
->fake_scrollbar()->set_thumb_thickness(4);
368 scrollbar_layer
->fake_scrollbar()->set_thumb_length(6);
370 UPDATE_AND_EXTRACT_LAYER_POINTERS();
371 EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(),
372 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
374 // Shrink the scrollbar layer to cover only the track.
375 scrollbar_layer
->SetBounds(gfx::Size(50, 10));
376 scrollbar_layer
->fake_scrollbar()->set_location(gfx::Point(30, 10));
377 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
379 UPDATE_AND_EXTRACT_LAYER_POINTERS();
380 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
381 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
383 // Shrink the track in the non-scrolling dimension so that it only covers the
384 // middle third of the scrollbar layer (this does not affect the thumb
386 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6));
388 UPDATE_AND_EXTRACT_LAYER_POINTERS();
389 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
390 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
393 TEST_F(ScrollbarLayerTest
, SolidColorDrawQuads
) {
394 const int kThumbThickness
= 3;
395 const int kTrackStart
= 1;
396 const int kTrackLength
= 100;
398 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar(false, true, true));
399 LayerImpl
* layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
400 layer_tree_host_
.get(), scrollbar
.Pass(), false, true, kThumbThickness
,
402 ScrollbarLayerImplBase
* scrollbar_layer_impl
=
403 static_cast<SolidColorScrollbarLayerImpl
*>(
404 layer_impl_tree_root
->children()[1]);
405 scrollbar_layer_impl
->SetBounds(gfx::Size(kTrackLength
, kThumbThickness
));
406 scrollbar_layer_impl
->SetCurrentPos(10.f
);
407 scrollbar_layer_impl
->SetMaximum(100);
408 scrollbar_layer_impl
->SetVisibleToTotalLengthRatio(0.4f
);
410 // Thickness should be overridden to 3.
412 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
413 AppendQuadsData data
;
414 scrollbar_layer_impl
->AppendQuads(render_pass
.get(), &data
);
416 const QuadList
& quads
= render_pass
->quad_list
;
417 ASSERT_EQ(1u, quads
.size());
418 EXPECT_EQ(DrawQuad::SOLID_COLOR
, quads
.front()->material
);
419 EXPECT_EQ(gfx::Rect(6, 0, 39, 3), quads
.front()->rect
);
422 // Contents scale should scale the draw quad.
423 scrollbar_layer_impl
->draw_properties().contents_scale_x
= 2.f
;
424 scrollbar_layer_impl
->draw_properties().contents_scale_y
= 2.f
;
426 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
427 AppendQuadsData data
;
428 scrollbar_layer_impl
->AppendQuads(render_pass
.get(), &data
);
430 const QuadList
& quads
= render_pass
->quad_list
;
431 ASSERT_EQ(1u, quads
.size());
432 EXPECT_EQ(DrawQuad::SOLID_COLOR
, quads
.front()->material
);
433 EXPECT_EQ(gfx::Rect(12, 0, 78, 6), quads
.front()->rect
);
435 scrollbar_layer_impl
->draw_properties().contents_scale_x
= 1.f
;
436 scrollbar_layer_impl
->draw_properties().contents_scale_y
= 1.f
;
438 // For solid color scrollbars, position and size should reflect the
439 // current viewport state.
440 scrollbar_layer_impl
->SetVisibleToTotalLengthRatio(0.2f
);
442 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
443 AppendQuadsData data
;
444 scrollbar_layer_impl
->AppendQuads(render_pass
.get(), &data
);
446 const QuadList
& quads
= render_pass
->quad_list
;
447 ASSERT_EQ(1u, quads
.size());
448 EXPECT_EQ(DrawQuad::SOLID_COLOR
, quads
.front()->material
);
449 EXPECT_EQ(gfx::Rect(8, 0, 19, 3), quads
.front()->rect
);
452 // We shouldn't attempt div-by-zero when the maximum is zero.
453 scrollbar_layer_impl
->SetCurrentPos(0.f
);
454 scrollbar_layer_impl
->SetMaximum(0);
456 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
457 AppendQuadsData data
;
458 scrollbar_layer_impl
->AppendQuads(render_pass
.get(), &data
);
460 const QuadList
& quads
= render_pass
->quad_list
;
461 ASSERT_EQ(1u, quads
.size());
462 EXPECT_EQ(DrawQuad::SOLID_COLOR
, quads
.front()->material
);
463 EXPECT_EQ(gfx::Rect(1, 0, 19, 3), quads
.front()->rect
);
467 TEST_F(ScrollbarLayerTest
, LayerDrivenSolidColorDrawQuads
) {
468 const int kThumbThickness
= 3;
469 const int kTrackStart
= 0;
470 const int kTrackLength
= 10;
472 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar(false, true, true));
475 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create();
476 scoped_refptr
<Layer
> scroll_layer
= Layer::Create();
477 scroll_layer
->SetScrollClipLayerId(layer_tree_root
->id());
478 scoped_refptr
<Layer
> child1
= Layer::Create();
479 scoped_refptr
<Layer
> child2
;
480 const bool kIsLeftSideVerticalScrollbar
= false;
481 child2
= SolidColorScrollbarLayer::Create(scrollbar
->Orientation(),
484 kIsLeftSideVerticalScrollbar
,
486 child2
->ToScrollbarLayer()->SetScrollLayer(scroll_layer
->id());
487 child2
->ToScrollbarLayer()->SetClipLayer(layer_tree_root
->id());
488 scroll_layer
->AddChild(child1
);
489 scroll_layer
->InsertChild(child2
, 1);
490 layer_tree_root
->AddChild(scroll_layer
);
491 layer_tree_host_
->SetRootLayer(layer_tree_root
);
493 LayerImpl
* layer_impl_tree_root
=
494 layer_tree_host_
->CommitAndCreateLayerImplTree();
495 LayerImpl
* scroll_layer_impl
= layer_impl_tree_root
->children()[0];
497 ScrollbarLayerImplBase
* scrollbar_layer_impl
=
498 static_cast<PaintedScrollbarLayerImpl
*>(scroll_layer_impl
->children()[1]);
500 // Choose layer bounds to give max_scroll_offset = (8, 8).
501 layer_impl_tree_root
->SetBounds(gfx::Size(2, 2));
502 scroll_layer_impl
->SetBounds(gfx::Size(10, 10));
503 scroll_layer_impl
->ScrollBy(gfx::Vector2dF(4.f
, 0.f
));
505 scrollbar_layer_impl
->SetBounds(gfx::Size(kTrackLength
, kThumbThickness
));
506 scrollbar_layer_impl
->SetCurrentPos(4.f
);
507 scrollbar_layer_impl
->SetMaximum(8);
510 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
512 AppendQuadsData data
;
513 scrollbar_layer_impl
->AppendQuads(render_pass
.get(), &data
);
515 const QuadList
& quads
= render_pass
->quad_list
;
516 ASSERT_EQ(1u, quads
.size());
517 EXPECT_EQ(DrawQuad::SOLID_COLOR
, quads
.front()->material
);
518 EXPECT_EQ(gfx::Rect(3, 0, 3, 3), quads
.front()->rect
);
522 class ScrollbarLayerSolidColorThumbTest
: public testing::Test
{
524 ScrollbarLayerSolidColorThumbTest() {
525 LayerTreeSettings layer_tree_settings
;
526 host_impl_
.reset(new FakeLayerTreeHostImpl(layer_tree_settings
, &proxy_
,
527 &shared_bitmap_manager_
,
528 &task_graph_runner_
));
530 const int kThumbThickness
= 3;
531 const int kTrackStart
= 0;
532 const bool kIsLeftSideVerticalScrollbar
= false;
533 const bool kIsOverlayScrollbar
= false;
535 horizontal_scrollbar_layer_
=
536 SolidColorScrollbarLayerImpl::Create(host_impl_
->active_tree(),
541 kIsLeftSideVerticalScrollbar
,
542 kIsOverlayScrollbar
);
543 vertical_scrollbar_layer_
=
544 SolidColorScrollbarLayerImpl::Create(host_impl_
->active_tree(),
549 kIsLeftSideVerticalScrollbar
,
550 kIsOverlayScrollbar
);
554 FakeImplProxy proxy_
;
555 TestSharedBitmapManager shared_bitmap_manager_
;
556 TestTaskGraphRunner task_graph_runner_
;
557 scoped_ptr
<FakeLayerTreeHostImpl
> host_impl_
;
558 scoped_ptr
<SolidColorScrollbarLayerImpl
> horizontal_scrollbar_layer_
;
559 scoped_ptr
<SolidColorScrollbarLayerImpl
> vertical_scrollbar_layer_
;
562 TEST_F(ScrollbarLayerSolidColorThumbTest
, SolidColorThumbLength
) {
563 horizontal_scrollbar_layer_
->SetCurrentPos(0);
564 horizontal_scrollbar_layer_
->SetMaximum(10);
566 // Simple case - one third of the scrollable area is visible, so the thumb
567 // should be one third as long as the track.
568 horizontal_scrollbar_layer_
->SetVisibleToTotalLengthRatio(0.33f
);
569 horizontal_scrollbar_layer_
->SetBounds(gfx::Size(100, 3));
570 EXPECT_EQ(33, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().width());
572 // The thumb's length should never be less than its thickness.
573 horizontal_scrollbar_layer_
->SetVisibleToTotalLengthRatio(0.01f
);
574 horizontal_scrollbar_layer_
->SetBounds(gfx::Size(100, 3));
575 EXPECT_EQ(3, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().width());
578 TEST_F(ScrollbarLayerSolidColorThumbTest
, SolidColorThumbPosition
) {
579 horizontal_scrollbar_layer_
->SetBounds(gfx::Size(100, 3));
580 horizontal_scrollbar_layer_
->SetVisibleToTotalLengthRatio(0.1f
);
582 horizontal_scrollbar_layer_
->SetCurrentPos(0);
583 horizontal_scrollbar_layer_
->SetMaximum(100);
584 EXPECT_EQ(0, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().x());
585 EXPECT_EQ(10, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().width());
587 horizontal_scrollbar_layer_
->SetCurrentPos(100);
588 // The thumb is 10px long and the track is 100px, so the maximum thumb
590 EXPECT_EQ(90, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().x());
592 horizontal_scrollbar_layer_
->SetCurrentPos(80);
593 // The scroll position is 80% of the maximum, so the thumb's position should
594 // be at 80% of its maximum or 72px.
595 EXPECT_EQ(72, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().x());
598 TEST_F(ScrollbarLayerSolidColorThumbTest
, SolidColorThumbVerticalAdjust
) {
599 SolidColorScrollbarLayerImpl
* layers
[2] =
600 { horizontal_scrollbar_layer_
.get(), vertical_scrollbar_layer_
.get() };
601 for (size_t i
= 0; i
< 2; ++i
) {
602 layers
[i
]->SetVisibleToTotalLengthRatio(0.2f
);
603 layers
[i
]->SetCurrentPos(25);
604 layers
[i
]->SetMaximum(100);
606 layers
[0]->SetBounds(gfx::Size(100, 3));
607 layers
[1]->SetBounds(gfx::Size(3, 100));
609 EXPECT_EQ(gfx::RectF(20.f
, 0.f
, 20.f
, 3.f
),
610 horizontal_scrollbar_layer_
->ComputeThumbQuadRect());
611 EXPECT_EQ(gfx::RectF(0.f
, 20.f
, 3.f
, 20.f
),
612 vertical_scrollbar_layer_
->ComputeThumbQuadRect());
614 horizontal_scrollbar_layer_
->SetVerticalAdjust(10.f
);
615 vertical_scrollbar_layer_
->SetVerticalAdjust(10.f
);
617 // The vertical adjustment factor has two effects:
618 // 1.) Moves the horizontal scrollbar down
619 // 2.) Increases the vertical scrollbar's effective track length which both
620 // increases the thumb's length and its position within the track.
621 EXPECT_EQ(gfx::Rect(20.f
, 10.f
, 20.f
, 3.f
),
622 horizontal_scrollbar_layer_
->ComputeThumbQuadRect());
623 EXPECT_EQ(gfx::Rect(0.f
, 22, 3.f
, 22.f
),
624 vertical_scrollbar_layer_
->ComputeThumbQuadRect());
627 class ScrollbarLayerTestMaxTextureSize
: public LayerTreeTest
{
629 ScrollbarLayerTestMaxTextureSize() {}
631 void SetScrollbarBounds(const gfx::Size
& bounds
) { bounds_
= bounds
; }
633 void BeginTest() override
{
634 scroll_layer_
= Layer::Create();
635 layer_tree_host()->root_layer()->AddChild(scroll_layer_
);
637 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
639 PaintedScrollbarLayer::Create(scrollbar
.Pass(), scroll_layer_
->id());
640 scrollbar_layer_
->SetScrollLayer(scroll_layer_
->id());
641 scrollbar_layer_
->SetLayerTreeHost(layer_tree_host());
642 scrollbar_layer_
->SetBounds(bounds_
);
643 scrollbar_layer_
->SetIsDrawable(true);
644 layer_tree_host()->root_layer()->AddChild(scrollbar_layer_
);
646 PostSetNeedsCommitToMainThread();
649 void DidCommitAndDrawFrame() override
{
650 const int kMaxTextureSize
=
651 layer_tree_host()->GetRendererCapabilities().max_texture_size
;
653 // Check first that we're actually testing something.
654 EXPECT_GT(scrollbar_layer_
->bounds().width(), kMaxTextureSize
);
656 EXPECT_EQ(scrollbar_layer_
->internal_content_bounds().width(),
657 kMaxTextureSize
- 1);
658 EXPECT_EQ(scrollbar_layer_
->internal_content_bounds().height(),
659 kMaxTextureSize
- 1);
664 void AfterTest() override
{}
667 scoped_refptr
<PaintedScrollbarLayer
> scrollbar_layer_
;
668 scoped_refptr
<Layer
> scroll_layer_
;
672 TEST_F(ScrollbarLayerTestMaxTextureSize
, DirectRenderer
) {
673 scoped_ptr
<TestWebGraphicsContext3D
> context
=
674 TestWebGraphicsContext3D::Create();
676 context
->getIntegerv(GL_MAX_TEXTURE_SIZE
, &max_size
);
677 SetScrollbarBounds(gfx::Size(max_size
+ 100, max_size
+ 100));
678 RunTest(true, false, true);
681 TEST_F(ScrollbarLayerTestMaxTextureSize
, DelegatingRenderer
) {
682 scoped_ptr
<TestWebGraphicsContext3D
> context
=
683 TestWebGraphicsContext3D::Create();
685 context
->getIntegerv(GL_MAX_TEXTURE_SIZE
, &max_size
);
686 SetScrollbarBounds(gfx::Size(max_size
+ 100, max_size
+ 100));
687 RunTest(true, true, true);
690 class ScrollbarLayerTestResourceCreationAndRelease
: public ScrollbarLayerTest
{
692 void TestResourceUpload(int num_updates
,
693 size_t expected_resources
,
694 int expected_created
,
695 int expected_deleted
,
696 bool use_solid_color_scrollbar
) {
697 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar(false, true, false));
698 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create();
699 scoped_refptr
<Layer
> content_layer
= Layer::Create();
700 scoped_refptr
<Layer
> scrollbar_layer
;
701 if (use_solid_color_scrollbar
) {
702 const int kThumbThickness
= 3;
703 const int kTrackStart
= 0;
704 const bool kIsLeftSideVerticalScrollbar
= false;
706 SolidColorScrollbarLayer::Create(scrollbar
->Orientation(),
709 kIsLeftSideVerticalScrollbar
,
710 layer_tree_root
->id());
712 scrollbar_layer
= PaintedScrollbarLayer::Create(scrollbar
.Pass(),
713 layer_tree_root
->id());
715 layer_tree_root
->AddChild(content_layer
);
716 layer_tree_root
->AddChild(scrollbar_layer
);
718 layer_tree_host_
->SetRootLayer(layer_tree_root
);
720 scrollbar_layer
->SetIsDrawable(true);
721 scrollbar_layer
->SetBounds(gfx::Size(100, 100));
722 layer_tree_root
->SetScrollOffset(gfx::ScrollOffset(10, 20));
723 layer_tree_root
->SetBounds(gfx::Size(100, 200));
724 content_layer
->SetBounds(gfx::Size(100, 200));
725 scrollbar_layer
->draw_properties().content_bounds
= gfx::Size(100, 200);
726 scrollbar_layer
->draw_properties().visible_content_rect
=
727 gfx::Rect(0, 0, 100, 200);
728 scrollbar_layer
->CreateRenderSurface();
729 scrollbar_layer
->draw_properties().render_target
= scrollbar_layer
.get();
731 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
732 EXPECT_EQ(scrollbar_layer
->layer_tree_host(), layer_tree_host_
.get());
734 ResourceUpdateQueue queue
;
735 gfx::Rect screen_space_clip_rect
;
736 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
738 scrollbar_layer
->SavePaintProperties();
739 for (int update_counter
= 0; update_counter
< num_updates
; update_counter
++)
740 scrollbar_layer
->Update(&queue
, &occlusion_tracker
);
742 // A non-solid-color scrollbar should have requested two textures.
743 EXPECT_EQ(expected_resources
, layer_tree_host_
->UIResourceCount());
744 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
745 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
747 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
749 scrollbar_layer
->ClearRenderSurface();
753 TEST_F(ScrollbarLayerTestResourceCreationAndRelease
, ResourceUpload
) {
754 bool use_solid_color_scrollbars
= false;
755 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars
);
756 int num_updates
[3] = {1, 5, 10};
759 for (int j
= 0; j
< 3; j
++) {
760 created
+= num_updates
[j
] * 2;
761 deleted
= created
- 2;
762 TestResourceUpload(num_updates
[j
], 2, created
, deleted
,
763 use_solid_color_scrollbars
);
767 TEST_F(ScrollbarLayerTestResourceCreationAndRelease
,
768 SolidColorNoResourceUpload
) {
769 bool use_solid_color_scrollbars
= true;
770 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars
);
771 TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars
);
774 TEST_F(ScrollbarLayerTestResourceCreationAndRelease
, TestResourceUpdate
) {
775 gfx::Point
scrollbar_location(0, 185);
776 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create();
777 scoped_refptr
<Layer
> content_layer
= Layer::Create();
778 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
779 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root
->id());
781 layer_tree_root
->AddChild(content_layer
);
782 layer_tree_root
->AddChild(scrollbar_layer
);
784 layer_tree_host_
->SetRootLayer(layer_tree_root
);
786 scrollbar_layer
->SetIsDrawable(true);
787 scrollbar_layer
->SetBounds(gfx::Size(100, 15));
788 scrollbar_layer
->SetPosition(scrollbar_location
);
789 layer_tree_root
->SetBounds(gfx::Size(100, 200));
790 content_layer
->SetBounds(gfx::Size(100, 200));
792 scrollbar_layer
->draw_properties().content_bounds
= gfx::Size(100, 200);
793 scrollbar_layer
->draw_properties().visible_content_rect
=
794 gfx::Rect(0, 0, 100, 200);
796 scrollbar_layer
->CreateRenderSurface();
797 scrollbar_layer
->draw_properties().render_target
= scrollbar_layer
.get();
799 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
800 EXPECT_EQ(scrollbar_layer
->layer_tree_host(), layer_tree_host_
.get());
802 ResourceUpdateQueue queue
;
803 gfx::Rect screen_space_clip_rect
;
804 size_t resource_count
;
805 int expected_created
, expected_deleted
;
806 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
807 scrollbar_layer
->SavePaintProperties();
810 expected_created
= 2;
811 expected_deleted
= 0;
812 EXPECT_TRUE(scrollbar_layer
->Update(&queue
, &occlusion_tracker
));
813 EXPECT_NE(0, scrollbar_layer
->track_resource_id());
814 EXPECT_NE(0, scrollbar_layer
->thumb_resource_id());
815 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
816 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
817 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
820 expected_created
= 2;
821 expected_deleted
= 2;
822 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
823 EXPECT_TRUE(scrollbar_layer
->Update(&queue
, &occlusion_tracker
));
824 EXPECT_EQ(0, scrollbar_layer
->track_resource_id());
825 EXPECT_EQ(0, scrollbar_layer
->thumb_resource_id());
826 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
827 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
828 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
831 expected_created
= 2;
832 expected_deleted
= 2;
833 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
834 EXPECT_FALSE(scrollbar_layer
->Update(&queue
, &occlusion_tracker
));
835 EXPECT_EQ(0, scrollbar_layer
->track_resource_id());
836 EXPECT_EQ(0, scrollbar_layer
->thumb_resource_id());
837 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
838 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
839 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
842 expected_created
= 4;
843 expected_deleted
= 2;
844 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
845 EXPECT_TRUE(scrollbar_layer
->Update(&queue
, &occlusion_tracker
));
846 EXPECT_NE(0, scrollbar_layer
->track_resource_id());
847 EXPECT_NE(0, scrollbar_layer
->thumb_resource_id());
848 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
849 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
850 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
853 expected_created
= 5;
854 expected_deleted
= 4;
855 scrollbar_layer
->fake_scrollbar()->set_has_thumb(false);
856 EXPECT_TRUE(scrollbar_layer
->Update(&queue
, &occlusion_tracker
));
857 EXPECT_NE(0, scrollbar_layer
->track_resource_id());
858 EXPECT_EQ(0, scrollbar_layer
->thumb_resource_id());
859 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
860 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
861 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
864 expected_created
= 5;
865 expected_deleted
= 5;
866 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
867 EXPECT_TRUE(scrollbar_layer
->Update(&queue
, &occlusion_tracker
));
868 EXPECT_EQ(0, scrollbar_layer
->track_resource_id());
869 EXPECT_EQ(0, scrollbar_layer
->thumb_resource_id());
870 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
871 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
872 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
875 expected_created
= 7;
876 expected_deleted
= 5;
877 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
878 scrollbar_layer
->fake_scrollbar()->set_has_thumb(true);
879 EXPECT_TRUE(scrollbar_layer
->Update(&queue
, &occlusion_tracker
));
880 EXPECT_NE(0, scrollbar_layer
->track_resource_id());
881 EXPECT_NE(0, scrollbar_layer
->thumb_resource_id());
884 expected_created
= 8;
885 expected_deleted
= 7;
886 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
887 scrollbar_layer
->fake_scrollbar()->set_has_thumb(false);
888 scrollbar_layer
->SetBounds(gfx::Size(90, 15));
889 EXPECT_TRUE(scrollbar_layer
->Update(&queue
, &occlusion_tracker
));
890 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
891 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
892 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
895 layer_tree_host_
->ui_resource_size(scrollbar_layer
->track_resource_id()));
897 scrollbar_layer
->ResetNeedsDisplayForTesting();
898 EXPECT_FALSE(scrollbar_layer
->Update(&queue
, &occlusion_tracker
));
899 EXPECT_NE(0, scrollbar_layer
->track_resource_id());
900 EXPECT_EQ(0, scrollbar_layer
->thumb_resource_id());
901 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
902 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
903 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
905 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
906 scrollbar_layer
->ClearRenderSurface();
909 class ScaledScrollbarLayerTestResourceCreation
: public ScrollbarLayerTest
{
911 void TestResourceUpload(const float test_scale
) {
912 gfx::Point
scrollbar_location(0, 185);
913 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create();
914 scoped_refptr
<Layer
> content_layer
= Layer::Create();
915 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
916 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root
->id());
918 layer_tree_root
->AddChild(content_layer
);
919 layer_tree_root
->AddChild(scrollbar_layer
);
921 layer_tree_host_
->SetRootLayer(layer_tree_root
);
923 scrollbar_layer
->SetIsDrawable(true);
924 scrollbar_layer
->SetBounds(gfx::Size(100, 15));
925 scrollbar_layer
->SetPosition(scrollbar_location
);
926 layer_tree_root
->SetBounds(gfx::Size(100, 200));
927 content_layer
->SetBounds(gfx::Size(100, 200));
928 gfx::SizeF scaled_size
=
929 gfx::ScaleSize(scrollbar_layer
->bounds(), test_scale
, test_scale
);
930 gfx::PointF scaled_location
=
931 gfx::ScalePoint(scrollbar_layer
->position(), test_scale
, test_scale
);
932 scrollbar_layer
->draw_properties().content_bounds
=
933 gfx::Size(scaled_size
.width(), scaled_size
.height());
934 scrollbar_layer
->draw_properties().contents_scale_x
= test_scale
;
935 scrollbar_layer
->draw_properties().contents_scale_y
= test_scale
;
936 scrollbar_layer
->draw_properties().visible_content_rect
=
937 gfx::Rect(scaled_location
.x(),
940 scaled_size
.height());
941 scrollbar_layer
->CreateRenderSurface();
942 scrollbar_layer
->draw_properties().render_target
= scrollbar_layer
.get();
944 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
945 EXPECT_EQ(scrollbar_layer
->layer_tree_host(), layer_tree_host_
.get());
947 ResourceUpdateQueue queue
;
948 gfx::Rect screen_space_clip_rect
;
949 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
950 scrollbar_layer
->SavePaintProperties();
951 scrollbar_layer
->Update(&queue
, &occlusion_tracker
);
953 // Verify that we have not generated any content uploads that are larger
954 // than their destination textures.
956 gfx::Size track_size
= layer_tree_host_
->ui_resource_size(
957 scrollbar_layer
->track_resource_id());
958 gfx::Size thumb_size
= layer_tree_host_
->ui_resource_size(
959 scrollbar_layer
->thumb_resource_id());
961 EXPECT_LE(track_size
.width(),
962 scrollbar_layer
->internal_content_bounds().width());
963 EXPECT_LE(track_size
.height(),
964 scrollbar_layer
->internal_content_bounds().height());
965 EXPECT_LE(thumb_size
.width(),
966 scrollbar_layer
->internal_content_bounds().width());
967 EXPECT_LE(thumb_size
.height(),
968 scrollbar_layer
->internal_content_bounds().height());
970 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
972 scrollbar_layer
->ClearRenderSurface();
976 TEST_F(ScaledScrollbarLayerTestResourceCreation
, ScaledResourceUpload
) {
977 // Pick a test scale that moves the scrollbar's (non-zero) position to
978 // a non-pixel-aligned location.
979 TestResourceUpload(.041f
);
980 TestResourceUpload(1.41f
);
981 TestResourceUpload(4.1f
);
984 class ScaledScrollbarLayerTestScaledRasterization
: public ScrollbarLayerTest
{
986 void TestScale(const gfx::Rect scrollbar_rect
, const float test_scale
) {
987 bool paint_during_update
= true;
988 bool has_thumb
= false;
989 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create();
990 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
991 FakePaintedScrollbarLayer::Create(paint_during_update
,
993 layer_tree_root
->id());
995 layer_tree_root
->AddChild(scrollbar_layer
);
997 layer_tree_host_
->SetRootLayer(layer_tree_root
);
999 scrollbar_layer
->SetBounds(scrollbar_rect
.size());
1000 scrollbar_layer
->SetPosition(scrollbar_rect
.origin());
1001 scrollbar_layer
->fake_scrollbar()->set_location(scrollbar_rect
.origin());
1002 scrollbar_layer
->fake_scrollbar()->set_track_rect(scrollbar_rect
);
1003 gfx::SizeF scaled_size
=
1004 gfx::ScaleSize(scrollbar_layer
->bounds(), test_scale
, test_scale
);
1005 gfx::PointF scaled_location
=
1006 gfx::ScalePoint(scrollbar_layer
->position(), test_scale
, test_scale
);
1007 scrollbar_layer
->draw_properties().content_bounds
=
1008 gfx::Size(scaled_size
.width(), scaled_size
.height());
1009 scrollbar_layer
->draw_properties().contents_scale_x
= test_scale
;
1010 scrollbar_layer
->draw_properties().contents_scale_y
= test_scale
;
1011 scrollbar_layer
->draw_properties().visible_content_rect
=
1012 gfx::Rect(scaled_location
.x(),
1013 scaled_location
.y(),
1014 scaled_size
.width(),
1015 scaled_size
.height());
1017 ResourceUpdateQueue queue
;
1018 gfx::Rect screen_space_clip_rect
;
1019 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
1020 scrollbar_layer
->SavePaintProperties();
1022 scrollbar_layer
->Update(&queue
, &occlusion_tracker
);
1024 UIResourceBitmap
* bitmap
= layer_tree_host_
->ui_resource_bitmap(
1025 scrollbar_layer
->track_resource_id());
1029 AutoLockUIResourceBitmap
locked_bitmap(*bitmap
);
1031 const SkColor
* pixels
=
1032 reinterpret_cast<const SkColor
*>(locked_bitmap
.GetPixels());
1033 SkColor color
= argb_to_skia(
1034 scrollbar_layer
->fake_scrollbar()->paint_fill_color());
1035 int width
= bitmap
->GetSize().width();
1036 int height
= bitmap
->GetSize().height();
1038 // Make sure none of the corners of the bitmap were inadvertently clipped.
1039 EXPECT_EQ(color
, pixels
[0])
1040 << "Top left pixel doesn't match scrollbar color.";
1042 EXPECT_EQ(color
, pixels
[width
- 1])
1043 << "Top right pixel doesn't match scrollbar color.";
1045 EXPECT_EQ(color
, pixels
[width
* (height
- 1)])
1046 << "Bottom left pixel doesn't match scrollbar color.";
1048 EXPECT_EQ(color
, pixels
[width
* height
- 1])
1049 << "Bottom right pixel doesn't match scrollbar color.";
1053 // On Android, Skia uses ABGR
1054 static SkColor
argb_to_skia(SkColor c
) {
1055 return (SkColorGetA(c
) << SK_A32_SHIFT
) |
1056 (SkColorGetR(c
) << SK_R32_SHIFT
) |
1057 (SkColorGetG(c
) << SK_G32_SHIFT
) |
1058 (SkColorGetB(c
) << SK_B32_SHIFT
);
1062 TEST_F(ScaledScrollbarLayerTestScaledRasterization
, TestLostPrecisionInClip
) {
1063 // Try rasterization at coordinates and scale that caused problematic
1064 // rounding and clipping errors.
1065 // Vertical Scrollbars.
1066 TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f
);
1067 TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f
);
1069 // Horizontal Scrollbars.
1070 TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f
);
1071 TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f
);