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/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_task_graph_runner.h"
25 #include "cc/test/test_web_graphics_context_3d.h"
26 #include "cc/trees/layer_tree_host.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "cc/trees/occlusion_tracker.h"
29 #include "cc/trees/single_thread_proxy.h"
30 #include "cc/trees/tree_synchronizer.h"
31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "testing/gtest/include/gtest/gtest.h"
37 LayerImpl
* LayerImplForScrollAreaAndScrollbar(const LayerSettings
& settings
,
38 FakeLayerTreeHost
* host
,
39 scoped_ptr
<Scrollbar
> scrollbar
,
41 bool use_solid_color_scrollbar
,
44 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create(settings
);
45 scoped_refptr
<Layer
> child1
= Layer::Create(settings
);
46 scoped_refptr
<Layer
> child2
;
47 if (use_solid_color_scrollbar
) {
48 const bool kIsLeftSideVerticalScrollbar
= false;
49 child2
= SolidColorScrollbarLayer::Create(
50 settings
, scrollbar
->Orientation(), thumb_thickness
, track_start
,
51 kIsLeftSideVerticalScrollbar
, child1
->id());
54 PaintedScrollbarLayer::Create(settings
, scrollbar
.Pass(), child1
->id());
56 child2
->ToScrollbarLayer()->SetClipLayer(layer_tree_root
->id());
57 layer_tree_root
->AddChild(child1
);
58 layer_tree_root
->InsertChild(child2
, reverse_order
? 0 : 1);
59 host
->SetRootLayer(layer_tree_root
);
60 return host
->CommitAndCreateLayerImplTree();
63 class FakeResourceTrackingLayerTreeHost
: public FakeLayerTreeHost
{
65 FakeResourceTrackingLayerTreeHost(FakeLayerTreeHostClient
* client
,
66 LayerTreeHost::InitParams
* params
)
67 : FakeLayerTreeHost(client
, params
),
69 total_ui_resource_created_(0),
70 total_ui_resource_deleted_(0) {
71 InitializeSingleThreaded(client
, base::ThreadTaskRunnerHandle::Get(),
75 UIResourceId
CreateUIResource(UIResourceClient
* content
) override
{
76 total_ui_resource_created_
++;
77 UIResourceId nid
= next_id_
++;
78 ui_resource_bitmap_map_
.insert(
79 std::make_pair(nid
, content
->GetBitmap(nid
, false)));
83 // Deletes a UI resource. May safely be called more than once.
84 void DeleteUIResource(UIResourceId id
) override
{
85 UIResourceBitmapMap::iterator iter
= ui_resource_bitmap_map_
.find(id
);
86 if (iter
!= ui_resource_bitmap_map_
.end()) {
87 ui_resource_bitmap_map_
.erase(iter
);
88 total_ui_resource_deleted_
++;
92 size_t UIResourceCount() { return ui_resource_bitmap_map_
.size(); }
93 int TotalUIResourceDeleted() { return total_ui_resource_deleted_
; }
94 int TotalUIResourceCreated() { return total_ui_resource_created_
; }
96 gfx::Size
ui_resource_size(UIResourceId id
) {
97 UIResourceBitmapMap::iterator iter
= ui_resource_bitmap_map_
.find(id
);
98 if (iter
!= ui_resource_bitmap_map_
.end())
99 return iter
->second
.GetSize();
103 UIResourceBitmap
* ui_resource_bitmap(UIResourceId id
) {
104 UIResourceBitmapMap::iterator iter
= ui_resource_bitmap_map_
.find(id
);
105 if (iter
!= ui_resource_bitmap_map_
.end())
106 return &iter
->second
;
111 using UIResourceBitmapMap
= base::hash_map
<UIResourceId
, UIResourceBitmap
>;
112 UIResourceBitmapMap ui_resource_bitmap_map_
;
115 int total_ui_resource_created_
;
116 int total_ui_resource_deleted_
;
119 class ScrollbarLayerTest
: public testing::Test
{
121 ScrollbarLayerTest() : fake_client_(FakeLayerTreeHostClient::DIRECT_3D
) {
122 layer_tree_settings_
.single_thread_proxy_scheduler
= false;
123 layer_tree_settings_
.use_zero_copy
= true;
125 LayerTreeHost::InitParams params
;
126 params
.client
= &fake_client_
;
127 params
.settings
= &layer_tree_settings_
;
128 params
.task_graph_runner
= &task_graph_runner_
;
130 layer_tree_host_
.reset(
131 new FakeResourceTrackingLayerTreeHost(&fake_client_
, ¶ms
));
132 fake_client_
.SetLayerTreeHost(layer_tree_host_
.get());
133 // Force output surface creation for renderer capabilities.
134 layer_tree_host_
->Composite(base::TimeTicks());
135 EXPECT_FALSE(layer_tree_host_
->output_surface_lost());
138 const LayerSettings
& layer_settings() { return layer_settings_
; }
141 FakeLayerTreeHostClient fake_client_
;
142 TestTaskGraphRunner task_graph_runner_
;
143 LayerTreeSettings layer_tree_settings_
;
144 LayerSettings layer_settings_
;
145 scoped_ptr
<FakeResourceTrackingLayerTreeHost
> layer_tree_host_
;
148 TEST_F(ScrollbarLayerTest
, ResolveScrollLayerPointer
) {
149 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
150 LayerImpl
* layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
151 layer_settings(), layer_tree_host_
.get(), scrollbar
.Pass(), false, false,
154 LayerImpl
* cc_child1
= layer_impl_tree_root
->children()[0];
155 PaintedScrollbarLayerImpl
* cc_child2
=
156 static_cast<PaintedScrollbarLayerImpl
*>(
157 layer_impl_tree_root
->children()[1]);
159 EXPECT_EQ(cc_child1
->scrollbars()->size(), 1UL);
160 EXPECT_EQ(*(cc_child1
->scrollbars()->begin()), cc_child2
);
163 TEST_F(ScrollbarLayerTest
, ResolveScrollLayerPointer_ReverseOrder
) {
164 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
165 LayerImpl
* layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
166 layer_settings(), layer_tree_host_
.get(), scrollbar
.Pass(), true, false,
169 PaintedScrollbarLayerImpl
* cc_child1
=
170 static_cast<PaintedScrollbarLayerImpl
*>(
171 layer_impl_tree_root
->children()[0]);
172 LayerImpl
* cc_child2
= layer_impl_tree_root
->children()[1];
174 EXPECT_EQ(cc_child2
->scrollbars()->size(), 1UL);
175 EXPECT_EQ(*(cc_child2
->scrollbars()->begin()), cc_child1
);
178 TEST_F(ScrollbarLayerTest
, ShouldScrollNonOverlayOnMainThread
) {
179 // Create and attach a non-overlay scrollbar.
180 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
181 LayerImpl
* layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
182 layer_settings(), layer_tree_host_
.get(), scrollbar
.Pass(), false, false,
184 PaintedScrollbarLayerImpl
* scrollbar_layer_impl
=
185 static_cast<PaintedScrollbarLayerImpl
*>(
186 layer_impl_tree_root
->children()[1]);
188 // When the scrollbar is not an overlay scrollbar, the scroll should be
189 // responded to on the main thread as the compositor does not yet implement
190 // scrollbar scrolling.
192 InputHandler::SCROLL_ON_MAIN_THREAD
,
193 scrollbar_layer_impl
->TryScroll(gfx::Point(0, 0), InputHandler::GESTURE
,
194 SCROLL_BLOCKS_ON_NONE
));
196 // Create and attach an overlay scrollbar.
197 scrollbar
.reset(new FakeScrollbar(false, false, true));
199 layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
200 layer_settings(), layer_tree_host_
.get(), scrollbar
.Pass(), false, false,
202 scrollbar_layer_impl
= static_cast<PaintedScrollbarLayerImpl
*>(
203 layer_impl_tree_root
->children()[1]);
205 // The user shouldn't be able to drag an overlay scrollbar and the scroll
206 // may be handled in the compositor.
208 InputHandler::SCROLL_IGNORED
,
209 scrollbar_layer_impl
->TryScroll(gfx::Point(0, 0), InputHandler::GESTURE
,
210 SCROLL_BLOCKS_ON_NONE
));
213 TEST_F(ScrollbarLayerTest
, ScrollOffsetSynchronization
) {
214 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
215 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create(layer_settings());
216 scoped_refptr
<Layer
> scroll_layer
= Layer::Create(layer_settings());
217 scoped_refptr
<Layer
> content_layer
= Layer::Create(layer_settings());
218 scoped_refptr
<Layer
> scrollbar_layer
= PaintedScrollbarLayer::Create(
219 layer_settings(), scrollbar
.Pass(), layer_tree_root
->id());
221 // Choose bounds to give max_scroll_offset = (30, 50).
222 layer_tree_root
->SetBounds(gfx::Size(70, 150));
223 scroll_layer
->SetScrollClipLayerId(layer_tree_root
->id());
224 scroll_layer
->SetScrollOffset(gfx::ScrollOffset(10, 20));
225 scroll_layer
->SetBounds(gfx::Size(100, 200));
226 content_layer
->SetBounds(gfx::Size(100, 200));
228 layer_tree_host_
->SetRootLayer(layer_tree_root
);
229 layer_tree_root
->AddChild(scroll_layer
);
230 scroll_layer
->AddChild(content_layer
);
231 layer_tree_root
->AddChild(scrollbar_layer
);
232 scrollbar_layer
->ToScrollbarLayer()->SetScrollLayer(scroll_layer
->id());
233 scrollbar_layer
->ToScrollbarLayer()->SetClipLayer(layer_tree_root
->id());
235 layer_tree_root
->SavePaintProperties();
236 content_layer
->SavePaintProperties();
238 LayerImpl
* layer_impl_tree_root
=
239 layer_tree_host_
->CommitAndCreateLayerImplTree();
241 ScrollbarLayerImplBase
* cc_scrollbar_layer
=
242 static_cast<PaintedScrollbarLayerImpl
*>(
243 layer_impl_tree_root
->children()[1]);
245 EXPECT_EQ(10.f
, cc_scrollbar_layer
->current_pos());
246 EXPECT_EQ(30, cc_scrollbar_layer
->maximum());
248 layer_tree_root
->SetBounds(gfx::Size(700, 1500));
249 layer_tree_root
->SavePaintProperties();
250 scroll_layer
->SetBounds(gfx::Size(1000, 2000));
251 scroll_layer
->SetScrollOffset(gfx::ScrollOffset(100, 200));
252 scroll_layer
->SavePaintProperties();
253 content_layer
->SetBounds(gfx::Size(1000, 2000));
254 content_layer
->SavePaintProperties();
256 ScrollbarAnimationController
* scrollbar_controller
=
257 layer_impl_tree_root
->scrollbar_animation_controller();
258 layer_impl_tree_root
= layer_tree_host_
->CommitAndCreateLayerImplTree();
259 EXPECT_EQ(scrollbar_controller
,
260 layer_impl_tree_root
->scrollbar_animation_controller());
262 EXPECT_EQ(100.f
, cc_scrollbar_layer
->current_pos());
263 EXPECT_EQ(300, cc_scrollbar_layer
->maximum());
265 LayerImpl
* scroll_layer_impl
= layer_impl_tree_root
->children()[0];
266 scroll_layer_impl
->ScrollBy(gfx::Vector2d(12, 34));
268 EXPECT_EQ(112.f
, cc_scrollbar_layer
->current_pos());
269 EXPECT_EQ(300, cc_scrollbar_layer
->maximum());
272 #define UPDATE_AND_EXTRACT_LAYER_POINTERS() \
274 scrollbar_layer->UpdateInternalContentScale(); \
275 scrollbar_layer->UpdateThumbAndTrackGeometry(); \
276 root_clip_layer_impl = layer_tree_host_->CommitAndCreateLayerImplTree(); \
277 root_layer_impl = root_clip_layer_impl->children()[0]; \
278 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>( \
279 root_layer_impl->children()[1]); \
280 scrollbar_layer_impl->ScrollbarParametersDidChange(false); \
283 TEST_F(ScrollbarLayerTest
, UpdatePropertiesOfScrollBarWhenThumbRemoved
) {
284 scoped_refptr
<Layer
> root_clip_layer
= Layer::Create(layer_settings());
285 scoped_refptr
<Layer
> root_layer
= Layer::Create(layer_settings());
286 scoped_refptr
<Layer
> content_layer
= Layer::Create(layer_settings());
287 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
288 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
291 root_layer
->SetScrollClipLayerId(root_clip_layer
->id());
292 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
293 root_clip_layer
->SetBounds(gfx::Size(20, 50));
294 root_layer
->SetBounds(gfx::Size(100, 50));
295 content_layer
->SetBounds(gfx::Size(100, 50));
297 layer_tree_host_
->SetRootLayer(root_clip_layer
);
298 root_clip_layer
->AddChild(root_layer
);
299 root_layer
->AddChild(content_layer
);
300 root_layer
->AddChild(scrollbar_layer
);
302 root_layer
->SetScrollOffset(gfx::ScrollOffset(0, 0));
303 scrollbar_layer
->SetBounds(gfx::Size(70, 10));
304 scrollbar_layer
->SetScrollLayer(root_layer
->id());
305 scrollbar_layer
->SetClipLayer(root_clip_layer
->id());
306 scrollbar_layer
->fake_scrollbar()->set_location(gfx::Point(20, 10));
307 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
308 scrollbar_layer
->fake_scrollbar()->set_thumb_thickness(10);
309 scrollbar_layer
->fake_scrollbar()->set_thumb_length(4);
310 LayerImpl
* root_clip_layer_impl
= nullptr;
311 LayerImpl
* root_layer_impl
= nullptr;
312 PaintedScrollbarLayerImpl
* scrollbar_layer_impl
= nullptr;
314 UPDATE_AND_EXTRACT_LAYER_POINTERS();
315 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
316 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
318 scrollbar_layer
->fake_scrollbar()->set_has_thumb(false);
320 UPDATE_AND_EXTRACT_LAYER_POINTERS();
321 EXPECT_EQ(gfx::Rect(10, 0, 0, 0).ToString(),
322 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
325 TEST_F(ScrollbarLayerTest
, ThumbRect
) {
326 scoped_refptr
<Layer
> root_clip_layer
= Layer::Create(layer_settings());
327 scoped_refptr
<Layer
> root_layer
= Layer::Create(layer_settings());
328 scoped_refptr
<Layer
> content_layer
= Layer::Create(layer_settings());
329 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
330 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
333 root_layer
->SetScrollClipLayerId(root_clip_layer
->id());
334 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
335 root_clip_layer
->SetBounds(gfx::Size(20, 50));
336 root_layer
->SetBounds(gfx::Size(100, 50));
337 content_layer
->SetBounds(gfx::Size(100, 50));
339 layer_tree_host_
->SetRootLayer(root_clip_layer
);
340 root_clip_layer
->AddChild(root_layer
);
341 root_layer
->AddChild(content_layer
);
342 root_layer
->AddChild(scrollbar_layer
);
344 root_layer
->SetScrollOffset(gfx::ScrollOffset(0, 0));
345 scrollbar_layer
->SetBounds(gfx::Size(70, 10));
346 scrollbar_layer
->SetScrollLayer(root_layer
->id());
347 scrollbar_layer
->SetClipLayer(root_clip_layer
->id());
348 scrollbar_layer
->fake_scrollbar()->set_location(gfx::Point(20, 10));
349 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
350 scrollbar_layer
->fake_scrollbar()->set_thumb_thickness(10);
351 scrollbar_layer
->fake_scrollbar()->set_thumb_length(4);
352 LayerImpl
* root_clip_layer_impl
= nullptr;
353 LayerImpl
* root_layer_impl
= nullptr;
354 PaintedScrollbarLayerImpl
* scrollbar_layer_impl
= nullptr;
356 // Thumb is at the edge of the scrollbar (should be inset to
357 // the start of the track within the scrollbar layer's
359 UPDATE_AND_EXTRACT_LAYER_POINTERS();
360 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
361 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
363 // Under-scroll (thumb position should clamp and be unchanged).
364 root_layer
->SetScrollOffset(gfx::ScrollOffset(-5, 0));
366 UPDATE_AND_EXTRACT_LAYER_POINTERS();
367 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
368 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
370 // Over-scroll (thumb position should clamp on the far side).
371 root_layer
->SetScrollOffset(gfx::ScrollOffset(85, 0));
373 UPDATE_AND_EXTRACT_LAYER_POINTERS();
374 EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(),
375 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
377 // Change thumb thickness and length.
378 scrollbar_layer
->fake_scrollbar()->set_thumb_thickness(4);
379 scrollbar_layer
->fake_scrollbar()->set_thumb_length(6);
381 UPDATE_AND_EXTRACT_LAYER_POINTERS();
382 EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(),
383 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
385 // Shrink the scrollbar layer to cover only the track.
386 scrollbar_layer
->SetBounds(gfx::Size(50, 10));
387 scrollbar_layer
->fake_scrollbar()->set_location(gfx::Point(30, 10));
388 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
390 UPDATE_AND_EXTRACT_LAYER_POINTERS();
391 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
392 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
394 // Shrink the track in the non-scrolling dimension so that it only covers the
395 // middle third of the scrollbar layer (this does not affect the thumb
397 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6));
399 UPDATE_AND_EXTRACT_LAYER_POINTERS();
400 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
401 scrollbar_layer_impl
->ComputeThumbQuadRect().ToString());
404 TEST_F(ScrollbarLayerTest
, SolidColorDrawQuads
) {
405 const int kThumbThickness
= 3;
406 const int kTrackStart
= 1;
407 const int kTrackLength
= 100;
409 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar(false, true, true));
410 LayerImpl
* layer_impl_tree_root
= LayerImplForScrollAreaAndScrollbar(
411 layer_settings(), layer_tree_host_
.get(), scrollbar
.Pass(), false, true,
412 kThumbThickness
, kTrackStart
);
413 ScrollbarLayerImplBase
* scrollbar_layer_impl
=
414 static_cast<SolidColorScrollbarLayerImpl
*>(
415 layer_impl_tree_root
->children()[1]);
416 scrollbar_layer_impl
->SetBounds(gfx::Size(kTrackLength
, kThumbThickness
));
417 scrollbar_layer_impl
->SetCurrentPos(10.f
);
418 scrollbar_layer_impl
->SetMaximum(100);
419 scrollbar_layer_impl
->SetVisibleToTotalLengthRatio(0.4f
);
421 // Thickness should be overridden to 3.
423 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
424 AppendQuadsData data
;
425 scrollbar_layer_impl
->AppendQuads(render_pass
.get(), &data
);
427 const QuadList
& quads
= render_pass
->quad_list
;
428 ASSERT_EQ(1u, quads
.size());
429 EXPECT_EQ(DrawQuad::SOLID_COLOR
, quads
.front()->material
);
430 EXPECT_EQ(gfx::Rect(6, 0, 39, 3), quads
.front()->rect
);
433 // For solid color scrollbars, position and size should reflect the
434 // current viewport state.
435 scrollbar_layer_impl
->SetVisibleToTotalLengthRatio(0.2f
);
437 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
438 AppendQuadsData data
;
439 scrollbar_layer_impl
->AppendQuads(render_pass
.get(), &data
);
441 const QuadList
& quads
= render_pass
->quad_list
;
442 ASSERT_EQ(1u, quads
.size());
443 EXPECT_EQ(DrawQuad::SOLID_COLOR
, quads
.front()->material
);
444 EXPECT_EQ(gfx::Rect(8, 0, 19, 3), quads
.front()->rect
);
447 // We shouldn't attempt div-by-zero when the maximum is zero.
448 scrollbar_layer_impl
->SetCurrentPos(0.f
);
449 scrollbar_layer_impl
->SetMaximum(0);
451 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
452 AppendQuadsData data
;
453 scrollbar_layer_impl
->AppendQuads(render_pass
.get(), &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_EQ(gfx::Rect(1, 0, 19, 3), quads
.front()->rect
);
462 TEST_F(ScrollbarLayerTest
, LayerDrivenSolidColorDrawQuads
) {
463 const int kThumbThickness
= 3;
464 const int kTrackStart
= 0;
465 const int kTrackLength
= 10;
467 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar(false, true, true));
470 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create(layer_settings());
471 scoped_refptr
<Layer
> scroll_layer
= Layer::Create(layer_settings());
472 scroll_layer
->SetScrollClipLayerId(layer_tree_root
->id());
473 scoped_refptr
<Layer
> child1
= Layer::Create(layer_settings());
474 scoped_refptr
<Layer
> child2
;
475 const bool kIsLeftSideVerticalScrollbar
= false;
476 child2
= SolidColorScrollbarLayer::Create(
477 layer_settings(), scrollbar
->Orientation(), kThumbThickness
,
478 kTrackStart
, kIsLeftSideVerticalScrollbar
, child1
->id());
479 child2
->ToScrollbarLayer()->SetScrollLayer(scroll_layer
->id());
480 child2
->ToScrollbarLayer()->SetClipLayer(layer_tree_root
->id());
481 scroll_layer
->AddChild(child1
);
482 scroll_layer
->InsertChild(child2
, 1);
483 layer_tree_root
->AddChild(scroll_layer
);
484 layer_tree_host_
->SetRootLayer(layer_tree_root
);
486 LayerImpl
* layer_impl_tree_root
=
487 layer_tree_host_
->CommitAndCreateLayerImplTree();
488 LayerImpl
* scroll_layer_impl
= layer_impl_tree_root
->children()[0];
490 auto* scrollbar_layer_impl
=
491 static_cast<ScrollbarLayerImplBase
*>(scroll_layer_impl
->children()[1]);
493 // Choose layer bounds to give max_scroll_offset = (8, 8).
494 layer_impl_tree_root
->SetBounds(gfx::Size(2, 2));
495 scroll_layer_impl
->SetBounds(gfx::Size(10, 10));
496 scroll_layer_impl
->ScrollBy(gfx::Vector2dF(4.f
, 0.f
));
498 scrollbar_layer_impl
->SetBounds(gfx::Size(kTrackLength
, kThumbThickness
));
499 scrollbar_layer_impl
->SetCurrentPos(4.f
);
500 scrollbar_layer_impl
->SetMaximum(8);
503 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
505 AppendQuadsData data
;
506 scrollbar_layer_impl
->AppendQuads(render_pass
.get(), &data
);
508 const QuadList
& quads
= render_pass
->quad_list
;
509 ASSERT_EQ(1u, quads
.size());
510 EXPECT_EQ(DrawQuad::SOLID_COLOR
, quads
.front()->material
);
511 EXPECT_EQ(gfx::Rect(3, 0, 3, 3), quads
.front()->rect
);
515 class ScrollbarLayerSolidColorThumbTest
: public testing::Test
{
517 ScrollbarLayerSolidColorThumbTest() {
518 LayerTreeSettings layer_tree_settings
;
519 host_impl_
.reset(new FakeLayerTreeHostImpl(layer_tree_settings
, &proxy_
,
520 &shared_bitmap_manager_
,
521 &task_graph_runner_
));
523 const int kThumbThickness
= 3;
524 const int kTrackStart
= 0;
525 const bool kIsLeftSideVerticalScrollbar
= false;
526 const bool kIsOverlayScrollbar
= false;
528 horizontal_scrollbar_layer_
=
529 SolidColorScrollbarLayerImpl::Create(host_impl_
->active_tree(),
534 kIsLeftSideVerticalScrollbar
,
535 kIsOverlayScrollbar
);
536 vertical_scrollbar_layer_
=
537 SolidColorScrollbarLayerImpl::Create(host_impl_
->active_tree(),
542 kIsLeftSideVerticalScrollbar
,
543 kIsOverlayScrollbar
);
547 FakeImplProxy proxy_
;
548 TestSharedBitmapManager shared_bitmap_manager_
;
549 TestTaskGraphRunner task_graph_runner_
;
550 scoped_ptr
<FakeLayerTreeHostImpl
> host_impl_
;
551 scoped_ptr
<SolidColorScrollbarLayerImpl
> horizontal_scrollbar_layer_
;
552 scoped_ptr
<SolidColorScrollbarLayerImpl
> vertical_scrollbar_layer_
;
555 TEST_F(ScrollbarLayerSolidColorThumbTest
, SolidColorThumbLength
) {
556 horizontal_scrollbar_layer_
->SetCurrentPos(0);
557 horizontal_scrollbar_layer_
->SetMaximum(10);
559 // Simple case - one third of the scrollable area is visible, so the thumb
560 // should be one third as long as the track.
561 horizontal_scrollbar_layer_
->SetVisibleToTotalLengthRatio(0.33f
);
562 horizontal_scrollbar_layer_
->SetBounds(gfx::Size(100, 3));
563 EXPECT_EQ(33, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().width());
565 // The thumb's length should never be less than its thickness.
566 horizontal_scrollbar_layer_
->SetVisibleToTotalLengthRatio(0.01f
);
567 horizontal_scrollbar_layer_
->SetBounds(gfx::Size(100, 3));
568 EXPECT_EQ(3, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().width());
571 TEST_F(ScrollbarLayerSolidColorThumbTest
, SolidColorThumbPosition
) {
572 horizontal_scrollbar_layer_
->SetBounds(gfx::Size(100, 3));
573 horizontal_scrollbar_layer_
->SetVisibleToTotalLengthRatio(0.1f
);
575 horizontal_scrollbar_layer_
->SetCurrentPos(0);
576 horizontal_scrollbar_layer_
->SetMaximum(100);
577 EXPECT_EQ(0, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().x());
578 EXPECT_EQ(10, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().width());
580 horizontal_scrollbar_layer_
->SetCurrentPos(100);
581 // The thumb is 10px long and the track is 100px, so the maximum thumb
583 EXPECT_EQ(90, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().x());
585 horizontal_scrollbar_layer_
->SetCurrentPos(80);
586 // The scroll position is 80% of the maximum, so the thumb's position should
587 // be at 80% of its maximum or 72px.
588 EXPECT_EQ(72, horizontal_scrollbar_layer_
->ComputeThumbQuadRect().x());
591 TEST_F(ScrollbarLayerSolidColorThumbTest
, SolidColorThumbVerticalAdjust
) {
592 SolidColorScrollbarLayerImpl
* layers
[2] =
593 { horizontal_scrollbar_layer_
.get(), vertical_scrollbar_layer_
.get() };
594 for (size_t i
= 0; i
< 2; ++i
) {
595 layers
[i
]->SetVisibleToTotalLengthRatio(0.2f
);
596 layers
[i
]->SetCurrentPos(25);
597 layers
[i
]->SetMaximum(100);
599 layers
[0]->SetBounds(gfx::Size(100, 3));
600 layers
[1]->SetBounds(gfx::Size(3, 100));
602 EXPECT_EQ(gfx::RectF(20.f
, 0.f
, 20.f
, 3.f
),
603 horizontal_scrollbar_layer_
->ComputeThumbQuadRect());
604 EXPECT_EQ(gfx::RectF(0.f
, 20.f
, 3.f
, 20.f
),
605 vertical_scrollbar_layer_
->ComputeThumbQuadRect());
607 horizontal_scrollbar_layer_
->SetVerticalAdjust(10.f
);
608 vertical_scrollbar_layer_
->SetVerticalAdjust(10.f
);
610 // The vertical adjustment factor has two effects:
611 // 1.) Moves the horizontal scrollbar down
612 // 2.) Increases the vertical scrollbar's effective track length which both
613 // increases the thumb's length and its position within the track.
614 EXPECT_EQ(gfx::Rect(20.f
, 10.f
, 20.f
, 3.f
),
615 horizontal_scrollbar_layer_
->ComputeThumbQuadRect());
616 EXPECT_EQ(gfx::Rect(0.f
, 22, 3.f
, 22.f
),
617 vertical_scrollbar_layer_
->ComputeThumbQuadRect());
620 class ScrollbarLayerTestMaxTextureSize
: public LayerTreeTest
{
622 ScrollbarLayerTestMaxTextureSize() {}
624 void SetScrollbarBounds(const gfx::Size
& bounds
) { bounds_
= bounds
; }
626 void BeginTest() override
{
627 scroll_layer_
= Layer::Create(layer_settings());
628 layer_tree_host()->root_layer()->AddChild(scroll_layer_
);
630 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar
);
631 scrollbar_layer_
= PaintedScrollbarLayer::Create(
632 layer_settings(), scrollbar
.Pass(), scroll_layer_
->id());
633 scrollbar_layer_
->SetScrollLayer(scroll_layer_
->id());
634 scrollbar_layer_
->SetLayerTreeHost(layer_tree_host());
635 scrollbar_layer_
->SetBounds(bounds_
);
636 scrollbar_layer_
->SetIsDrawable(true);
637 layer_tree_host()->root_layer()->AddChild(scrollbar_layer_
);
639 PostSetNeedsCommitToMainThread();
642 void DidCommitAndDrawFrame() override
{
643 const int kMaxTextureSize
=
644 layer_tree_host()->GetRendererCapabilities().max_texture_size
;
646 // Check first that we're actually testing something.
647 EXPECT_GT(scrollbar_layer_
->bounds().width(), kMaxTextureSize
);
649 EXPECT_EQ(scrollbar_layer_
->internal_content_bounds().width(),
650 kMaxTextureSize
- 1);
651 EXPECT_EQ(scrollbar_layer_
->internal_content_bounds().height(),
652 kMaxTextureSize
- 1);
657 void AfterTest() override
{}
660 scoped_refptr
<PaintedScrollbarLayer
> scrollbar_layer_
;
661 scoped_refptr
<Layer
> scroll_layer_
;
665 TEST_F(ScrollbarLayerTestMaxTextureSize
, DirectRenderer
) {
666 scoped_ptr
<TestWebGraphicsContext3D
> context
=
667 TestWebGraphicsContext3D::Create();
669 context
->getIntegerv(GL_MAX_TEXTURE_SIZE
, &max_size
);
670 SetScrollbarBounds(gfx::Size(max_size
+ 100, max_size
+ 100));
671 RunTest(true, false);
674 TEST_F(ScrollbarLayerTestMaxTextureSize
, DelegatingRenderer
) {
675 scoped_ptr
<TestWebGraphicsContext3D
> context
=
676 TestWebGraphicsContext3D::Create();
678 context
->getIntegerv(GL_MAX_TEXTURE_SIZE
, &max_size
);
679 SetScrollbarBounds(gfx::Size(max_size
+ 100, max_size
+ 100));
683 class ScrollbarLayerTestResourceCreationAndRelease
: public ScrollbarLayerTest
{
685 void TestResourceUpload(int num_updates
,
686 size_t expected_resources
,
687 int expected_created
,
688 int expected_deleted
,
689 bool use_solid_color_scrollbar
) {
690 scoped_ptr
<Scrollbar
> scrollbar(new FakeScrollbar(false, true, false));
691 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create(layer_settings());
692 scoped_refptr
<Layer
> content_layer
= Layer::Create(layer_settings());
693 scoped_refptr
<Layer
> scrollbar_layer
;
694 if (use_solid_color_scrollbar
) {
695 const int kThumbThickness
= 3;
696 const int kTrackStart
= 0;
697 const bool kIsLeftSideVerticalScrollbar
= false;
698 scrollbar_layer
= SolidColorScrollbarLayer::Create(
699 layer_settings(), scrollbar
->Orientation(), kThumbThickness
,
700 kTrackStart
, kIsLeftSideVerticalScrollbar
, layer_tree_root
->id());
702 scrollbar_layer
= PaintedScrollbarLayer::Create(
703 layer_settings(), scrollbar
.Pass(), layer_tree_root
->id());
705 layer_tree_root
->AddChild(content_layer
);
706 layer_tree_root
->AddChild(scrollbar_layer
);
708 layer_tree_host_
->SetRootLayer(layer_tree_root
);
710 scrollbar_layer
->SetIsDrawable(true);
711 scrollbar_layer
->SetBounds(gfx::Size(100, 100));
712 layer_tree_root
->SetScrollOffset(gfx::ScrollOffset(10, 20));
713 layer_tree_root
->SetBounds(gfx::Size(100, 200));
714 content_layer
->SetBounds(gfx::Size(100, 200));
715 scrollbar_layer
->draw_properties().visible_layer_rect
=
716 gfx::Rect(0, 0, 100, 200);
717 scrollbar_layer
->draw_properties().render_target
= scrollbar_layer
.get();
719 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
720 EXPECT_EQ(scrollbar_layer
->layer_tree_host(), layer_tree_host_
.get());
722 scrollbar_layer
->SavePaintProperties();
723 for (int update_counter
= 0; update_counter
< num_updates
; update_counter
++)
724 scrollbar_layer
->Update();
726 // A non-solid-color scrollbar should have requested two textures.
727 EXPECT_EQ(expected_resources
, layer_tree_host_
->UIResourceCount());
728 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
729 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
731 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
735 TEST_F(ScrollbarLayerTestResourceCreationAndRelease
, ResourceUpload
) {
736 bool use_solid_color_scrollbars
= false;
737 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars
);
738 int num_updates
[3] = {1, 5, 10};
741 for (int j
= 0; j
< 3; j
++) {
742 created
+= num_updates
[j
] * 2;
743 deleted
= created
- 2;
744 TestResourceUpload(num_updates
[j
], 2, created
, deleted
,
745 use_solid_color_scrollbars
);
749 TEST_F(ScrollbarLayerTestResourceCreationAndRelease
,
750 SolidColorNoResourceUpload
) {
751 bool use_solid_color_scrollbars
= true;
752 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars
);
753 TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars
);
756 TEST_F(ScrollbarLayerTestResourceCreationAndRelease
, TestResourceUpdate
) {
757 gfx::Point
scrollbar_location(0, 185);
758 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create(layer_settings());
759 scoped_refptr
<Layer
> content_layer
= Layer::Create(layer_settings());
760 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
761 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
762 layer_tree_root
->id());
764 layer_tree_root
->AddChild(content_layer
);
765 layer_tree_root
->AddChild(scrollbar_layer
);
767 layer_tree_host_
->SetRootLayer(layer_tree_root
);
769 scrollbar_layer
->SetIsDrawable(true);
770 scrollbar_layer
->SetBounds(gfx::Size(100, 15));
771 scrollbar_layer
->SetPosition(scrollbar_location
);
772 layer_tree_root
->SetBounds(gfx::Size(100, 200));
773 content_layer
->SetBounds(gfx::Size(100, 200));
775 scrollbar_layer
->draw_properties().visible_layer_rect
=
776 gfx::Rect(0, 0, 100, 200);
778 scrollbar_layer
->draw_properties().render_target
= scrollbar_layer
.get();
780 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
781 EXPECT_EQ(scrollbar_layer
->layer_tree_host(), layer_tree_host_
.get());
783 size_t resource_count
;
784 int expected_created
, expected_deleted
;
785 scrollbar_layer
->SavePaintProperties();
788 expected_created
= 2;
789 expected_deleted
= 0;
790 EXPECT_TRUE(scrollbar_layer
->Update());
791 EXPECT_NE(0, scrollbar_layer
->track_resource_id());
792 EXPECT_NE(0, scrollbar_layer
->thumb_resource_id());
793 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
794 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
795 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
798 expected_created
= 2;
799 expected_deleted
= 2;
800 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
801 EXPECT_TRUE(scrollbar_layer
->Update());
802 EXPECT_EQ(0, scrollbar_layer
->track_resource_id());
803 EXPECT_EQ(0, scrollbar_layer
->thumb_resource_id());
804 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
805 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
806 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
809 expected_created
= 2;
810 expected_deleted
= 2;
811 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
812 EXPECT_FALSE(scrollbar_layer
->Update());
813 EXPECT_EQ(0, scrollbar_layer
->track_resource_id());
814 EXPECT_EQ(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
= 4;
821 expected_deleted
= 2;
822 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
823 EXPECT_TRUE(scrollbar_layer
->Update());
824 EXPECT_NE(0, scrollbar_layer
->track_resource_id());
825 EXPECT_NE(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
= 5;
832 expected_deleted
= 4;
833 scrollbar_layer
->fake_scrollbar()->set_has_thumb(false);
834 EXPECT_TRUE(scrollbar_layer
->Update());
835 EXPECT_NE(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
= 5;
843 expected_deleted
= 5;
844 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
845 EXPECT_TRUE(scrollbar_layer
->Update());
846 EXPECT_EQ(0, scrollbar_layer
->track_resource_id());
847 EXPECT_EQ(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
= 7;
854 expected_deleted
= 5;
855 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
856 scrollbar_layer
->fake_scrollbar()->set_has_thumb(true);
857 EXPECT_TRUE(scrollbar_layer
->Update());
858 EXPECT_NE(0, scrollbar_layer
->track_resource_id());
859 EXPECT_NE(0, scrollbar_layer
->thumb_resource_id());
862 expected_created
= 8;
863 expected_deleted
= 7;
864 scrollbar_layer
->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
865 scrollbar_layer
->fake_scrollbar()->set_has_thumb(false);
866 scrollbar_layer
->SetBounds(gfx::Size(90, 15));
867 EXPECT_TRUE(scrollbar_layer
->Update());
868 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
869 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
870 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
873 layer_tree_host_
->ui_resource_size(scrollbar_layer
->track_resource_id()));
875 scrollbar_layer
->ResetNeedsDisplayForTesting();
876 EXPECT_FALSE(scrollbar_layer
->Update());
877 EXPECT_NE(0, scrollbar_layer
->track_resource_id());
878 EXPECT_EQ(0, scrollbar_layer
->thumb_resource_id());
879 EXPECT_EQ(resource_count
, layer_tree_host_
->UIResourceCount());
880 EXPECT_EQ(expected_created
, layer_tree_host_
->TotalUIResourceCreated());
881 EXPECT_EQ(expected_deleted
, layer_tree_host_
->TotalUIResourceDeleted());
883 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
886 class ScaledScrollbarLayerTestResourceCreation
: public ScrollbarLayerTest
{
888 void TestResourceUpload(const float test_scale
) {
889 gfx::Point
scrollbar_location(0, 185);
890 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create(layer_settings());
891 scoped_refptr
<Layer
> content_layer
= Layer::Create(layer_settings());
892 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
893 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
894 layer_tree_root
->id());
896 layer_tree_root
->AddChild(content_layer
);
897 layer_tree_root
->AddChild(scrollbar_layer
);
899 layer_tree_host_
->SetRootLayer(layer_tree_root
);
901 scrollbar_layer
->SetIsDrawable(true);
902 scrollbar_layer
->SetBounds(gfx::Size(100, 15));
903 scrollbar_layer
->SetPosition(scrollbar_location
);
904 layer_tree_root
->SetBounds(gfx::Size(100, 200));
905 content_layer
->SetBounds(gfx::Size(100, 200));
906 scrollbar_layer
->draw_properties().visible_layer_rect
=
907 gfx::Rect(scrollbar_location
, scrollbar_layer
->bounds());
908 scrollbar_layer
->draw_properties().render_target
= scrollbar_layer
.get();
910 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
911 EXPECT_EQ(scrollbar_layer
->layer_tree_host(), layer_tree_host_
.get());
913 layer_tree_host_
->SetDeviceScaleFactor(test_scale
);
915 scrollbar_layer
->SavePaintProperties();
916 scrollbar_layer
->Update();
918 // Verify that we have not generated any content uploads that are larger
919 // than their destination textures.
921 gfx::Size track_size
= layer_tree_host_
->ui_resource_size(
922 scrollbar_layer
->track_resource_id());
923 gfx::Size thumb_size
= layer_tree_host_
->ui_resource_size(
924 scrollbar_layer
->thumb_resource_id());
926 EXPECT_LE(track_size
.width(),
927 scrollbar_layer
->internal_content_bounds().width());
928 EXPECT_LE(track_size
.height(),
929 scrollbar_layer
->internal_content_bounds().height());
930 EXPECT_LE(thumb_size
.width(),
931 scrollbar_layer
->internal_content_bounds().width());
932 EXPECT_LE(thumb_size
.height(),
933 scrollbar_layer
->internal_content_bounds().height());
935 testing::Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
939 TEST_F(ScaledScrollbarLayerTestResourceCreation
, ScaledResourceUpload
) {
940 // Pick a test scale that moves the scrollbar's (non-zero) position to
941 // a non-pixel-aligned location.
942 TestResourceUpload(.041f
);
943 TestResourceUpload(1.41f
);
944 TestResourceUpload(4.1f
);
947 class ScaledScrollbarLayerTestScaledRasterization
: public ScrollbarLayerTest
{
949 void TestScale(const gfx::Rect scrollbar_rect
, const float test_scale
) {
950 bool paint_during_update
= true;
951 bool has_thumb
= false;
952 scoped_refptr
<Layer
> layer_tree_root
= Layer::Create(layer_settings());
953 scoped_refptr
<FakePaintedScrollbarLayer
> scrollbar_layer
=
954 FakePaintedScrollbarLayer::Create(layer_settings(), paint_during_update
,
955 has_thumb
, layer_tree_root
->id());
957 layer_tree_root
->AddChild(scrollbar_layer
);
959 layer_tree_host_
->SetRootLayer(layer_tree_root
);
961 scrollbar_layer
->SetBounds(scrollbar_rect
.size());
962 scrollbar_layer
->SetPosition(scrollbar_rect
.origin());
963 scrollbar_layer
->fake_scrollbar()->set_location(scrollbar_rect
.origin());
964 scrollbar_layer
->fake_scrollbar()->set_track_rect(scrollbar_rect
);
965 scrollbar_layer
->draw_properties().visible_layer_rect
= scrollbar_rect
;
967 layer_tree_host_
->SetDeviceScaleFactor(test_scale
);
969 gfx::Rect screen_space_clip_rect
;
970 scrollbar_layer
->SavePaintProperties();
972 scrollbar_layer
->Update();
974 UIResourceBitmap
* bitmap
= layer_tree_host_
->ui_resource_bitmap(
975 scrollbar_layer
->track_resource_id());
979 AutoLockUIResourceBitmap
locked_bitmap(*bitmap
);
981 const SkColor
* pixels
=
982 reinterpret_cast<const SkColor
*>(locked_bitmap
.GetPixels());
983 SkColor color
= argb_to_skia(
984 scrollbar_layer
->fake_scrollbar()->paint_fill_color());
985 int width
= bitmap
->GetSize().width();
986 int height
= bitmap
->GetSize().height();
988 // Make sure none of the corners of the bitmap were inadvertently clipped.
989 EXPECT_EQ(color
, pixels
[0])
990 << "Top left pixel doesn't match scrollbar color.";
992 EXPECT_EQ(color
, pixels
[width
- 1])
993 << "Top right pixel doesn't match scrollbar color.";
995 EXPECT_EQ(color
, pixels
[width
* (height
- 1)])
996 << "Bottom left pixel doesn't match scrollbar color.";
998 EXPECT_EQ(color
, pixels
[width
* height
- 1])
999 << "Bottom right pixel doesn't match scrollbar color.";
1003 // On Android, Skia uses ABGR
1004 static SkColor
argb_to_skia(SkColor c
) {
1005 return (SkColorGetA(c
) << SK_A32_SHIFT
) |
1006 (SkColorGetR(c
) << SK_R32_SHIFT
) |
1007 (SkColorGetG(c
) << SK_G32_SHIFT
) |
1008 (SkColorGetB(c
) << SK_B32_SHIFT
);
1012 TEST_F(ScaledScrollbarLayerTestScaledRasterization
, TestLostPrecisionInClip
) {
1013 // Try rasterization at coordinates and scale that caused problematic
1014 // rounding and clipping errors.
1015 // Vertical Scrollbars.
1016 TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f
);
1017 TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f
);
1019 // Horizontal Scrollbars.
1020 TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f
);
1021 TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f
);