Support swap promises that are pinned to a particular layer tree.
[chromium-blink-merge.git] / cc / layers / scrollbar_layer_unittest.cc
blob9ad1cd7e83866450e231fd31b78856f1ac8ba8cb
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"
34 namespace cc {
35 namespace {
37 LayerImpl* LayerImplForScrollAreaAndScrollbar(const LayerSettings& settings,
38 FakeLayerTreeHost* host,
39 scoped_ptr<Scrollbar> scrollbar,
40 bool reverse_order,
41 bool use_solid_color_scrollbar,
42 int thumb_thickness,
43 int track_start) {
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());
52 } else {
53 child2 =
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 {
64 public:
65 FakeResourceTrackingLayerTreeHost(FakeLayerTreeHostClient* client,
66 LayerTreeHost::InitParams* params)
67 : FakeLayerTreeHost(client, params),
68 next_id_(1),
69 total_ui_resource_created_(0),
70 total_ui_resource_deleted_(0) {
71 InitializeSingleThreaded(client, base::ThreadTaskRunnerHandle::Get(),
72 nullptr);
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)));
80 return nid;
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();
100 return gfx::Size();
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;
107 return nullptr;
110 private:
111 using UIResourceBitmapMap = base::hash_map<UIResourceId, UIResourceBitmap>;
112 UIResourceBitmapMap ui_resource_bitmap_map_;
114 int next_id_;
115 int total_ui_resource_created_;
116 int total_ui_resource_deleted_;
119 class ScrollbarLayerTest : public testing::Test {
120 public:
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_, &params));
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_; }
140 protected:
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,
152 0, 0);
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,
167 0, 0);
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,
183 0, 0);
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.
191 EXPECT_EQ(
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,
201 0, 0);
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.
207 EXPECT_EQ(
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() \
273 do { \
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); \
281 } while (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,
289 root_layer->id());
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,
331 root_layer->id());
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
358 // position).
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
396 // position).
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 {
516 public:
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(),
531 HORIZONTAL,
532 kThumbThickness,
533 kTrackStart,
534 kIsLeftSideVerticalScrollbar,
535 kIsOverlayScrollbar);
536 vertical_scrollbar_layer_ =
537 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
539 VERTICAL,
540 kThumbThickness,
541 kTrackStart,
542 kIsLeftSideVerticalScrollbar,
543 kIsOverlayScrollbar);
546 protected:
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
582 // position is 90px.
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 {
621 public:
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);
654 EndTest();
657 void AfterTest() override {}
659 private:
660 scoped_refptr<PaintedScrollbarLayer> scrollbar_layer_;
661 scoped_refptr<Layer> scroll_layer_;
662 gfx::Size bounds_;
665 TEST_F(ScrollbarLayerTestMaxTextureSize, DirectRenderer) {
666 scoped_ptr<TestWebGraphicsContext3D> context =
667 TestWebGraphicsContext3D::Create();
668 int max_size = 0;
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();
677 int max_size = 0;
678 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
679 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
680 RunTest(true, true);
683 class ScrollbarLayerTestResourceCreationAndRelease : public ScrollbarLayerTest {
684 public:
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());
701 } else {
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->set_visible_layer_rect(gfx::Rect(0, 0, 100, 200));
717 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
718 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
720 scrollbar_layer->SavePaintProperties();
721 for (int update_counter = 0; update_counter < num_updates; update_counter++)
722 scrollbar_layer->Update();
724 // A non-solid-color scrollbar should have requested two textures.
725 EXPECT_EQ(expected_resources, layer_tree_host_->UIResourceCount());
726 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
727 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
729 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
733 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, ResourceUpload) {
734 bool use_solid_color_scrollbars = false;
735 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
736 int num_updates[3] = {1, 5, 10};
737 int created = 0;
738 int deleted = 0;
739 for (int j = 0; j < 3; j++) {
740 created += num_updates[j] * 2;
741 deleted = created - 2;
742 TestResourceUpload(num_updates[j], 2, created, deleted,
743 use_solid_color_scrollbars);
747 TEST_F(ScrollbarLayerTestResourceCreationAndRelease,
748 SolidColorNoResourceUpload) {
749 bool use_solid_color_scrollbars = true;
750 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
751 TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars);
754 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, TestResourceUpdate) {
755 gfx::Point scrollbar_location(0, 185);
756 scoped_refptr<Layer> layer_tree_root = Layer::Create(layer_settings());
757 scoped_refptr<Layer> content_layer = Layer::Create(layer_settings());
758 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
759 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
760 layer_tree_root->id());
762 layer_tree_root->AddChild(content_layer);
763 layer_tree_root->AddChild(scrollbar_layer);
765 layer_tree_host_->SetRootLayer(layer_tree_root);
767 scrollbar_layer->SetIsDrawable(true);
768 scrollbar_layer->SetBounds(gfx::Size(100, 15));
769 scrollbar_layer->SetPosition(scrollbar_location);
770 layer_tree_root->SetBounds(gfx::Size(100, 200));
771 content_layer->SetBounds(gfx::Size(100, 200));
772 scrollbar_layer->set_visible_layer_rect(gfx::Rect(0, 0, 100, 200));
774 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
775 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
777 size_t resource_count;
778 int expected_created, expected_deleted;
779 scrollbar_layer->SavePaintProperties();
781 resource_count = 2;
782 expected_created = 2;
783 expected_deleted = 0;
784 EXPECT_TRUE(scrollbar_layer->Update());
785 EXPECT_NE(0, scrollbar_layer->track_resource_id());
786 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
787 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
788 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
789 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
791 resource_count = 0;
792 expected_created = 2;
793 expected_deleted = 2;
794 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
795 EXPECT_TRUE(scrollbar_layer->Update());
796 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
797 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
798 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
799 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
800 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
802 resource_count = 0;
803 expected_created = 2;
804 expected_deleted = 2;
805 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
806 EXPECT_FALSE(scrollbar_layer->Update());
807 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
808 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
809 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
810 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
811 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
813 resource_count = 2;
814 expected_created = 4;
815 expected_deleted = 2;
816 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
817 EXPECT_TRUE(scrollbar_layer->Update());
818 EXPECT_NE(0, scrollbar_layer->track_resource_id());
819 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
820 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
821 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
822 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
824 resource_count = 1;
825 expected_created = 5;
826 expected_deleted = 4;
827 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
828 EXPECT_TRUE(scrollbar_layer->Update());
829 EXPECT_NE(0, scrollbar_layer->track_resource_id());
830 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
831 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
832 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
833 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
835 resource_count = 0;
836 expected_created = 5;
837 expected_deleted = 5;
838 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
839 EXPECT_TRUE(scrollbar_layer->Update());
840 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
841 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
842 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
843 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
844 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
846 resource_count = 2;
847 expected_created = 7;
848 expected_deleted = 5;
849 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
850 scrollbar_layer->fake_scrollbar()->set_has_thumb(true);
851 EXPECT_TRUE(scrollbar_layer->Update());
852 EXPECT_NE(0, scrollbar_layer->track_resource_id());
853 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
855 resource_count = 1;
856 expected_created = 8;
857 expected_deleted = 7;
858 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
859 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
860 scrollbar_layer->SetBounds(gfx::Size(90, 15));
861 EXPECT_TRUE(scrollbar_layer->Update());
862 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
863 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
864 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
865 EXPECT_EQ(
866 gfx::Size(90, 15),
867 layer_tree_host_->ui_resource_size(scrollbar_layer->track_resource_id()));
869 scrollbar_layer->ResetNeedsDisplayForTesting();
870 EXPECT_FALSE(scrollbar_layer->Update());
871 EXPECT_NE(0, scrollbar_layer->track_resource_id());
872 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
873 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
874 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
875 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
877 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
880 class ScaledScrollbarLayerTestResourceCreation : public ScrollbarLayerTest {
881 public:
882 void TestResourceUpload(const float test_scale) {
883 gfx::Point scrollbar_location(0, 185);
884 scoped_refptr<Layer> layer_tree_root = Layer::Create(layer_settings());
885 scoped_refptr<Layer> content_layer = Layer::Create(layer_settings());
886 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
887 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
888 layer_tree_root->id());
890 layer_tree_root->AddChild(content_layer);
891 layer_tree_root->AddChild(scrollbar_layer);
893 layer_tree_host_->SetRootLayer(layer_tree_root);
895 scrollbar_layer->SetIsDrawable(true);
896 scrollbar_layer->SetBounds(gfx::Size(100, 15));
897 scrollbar_layer->SetPosition(scrollbar_location);
898 layer_tree_root->SetBounds(gfx::Size(100, 200));
899 content_layer->SetBounds(gfx::Size(100, 200));
900 scrollbar_layer->set_visible_layer_rect(
901 gfx::Rect(scrollbar_location, scrollbar_layer->bounds()));
903 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
904 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
906 layer_tree_host_->SetDeviceScaleFactor(test_scale);
908 scrollbar_layer->SavePaintProperties();
909 scrollbar_layer->Update();
911 // Verify that we have not generated any content uploads that are larger
912 // than their destination textures.
914 gfx::Size track_size = layer_tree_host_->ui_resource_size(
915 scrollbar_layer->track_resource_id());
916 gfx::Size thumb_size = layer_tree_host_->ui_resource_size(
917 scrollbar_layer->thumb_resource_id());
919 EXPECT_LE(track_size.width(),
920 scrollbar_layer->internal_content_bounds().width());
921 EXPECT_LE(track_size.height(),
922 scrollbar_layer->internal_content_bounds().height());
923 EXPECT_LE(thumb_size.width(),
924 scrollbar_layer->internal_content_bounds().width());
925 EXPECT_LE(thumb_size.height(),
926 scrollbar_layer->internal_content_bounds().height());
928 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
932 TEST_F(ScaledScrollbarLayerTestResourceCreation, ScaledResourceUpload) {
933 // Pick a test scale that moves the scrollbar's (non-zero) position to
934 // a non-pixel-aligned location.
935 TestResourceUpload(.041f);
936 TestResourceUpload(1.41f);
937 TestResourceUpload(4.1f);
940 class ScaledScrollbarLayerTestScaledRasterization : public ScrollbarLayerTest {
941 public:
942 void TestScale(const gfx::Rect scrollbar_rect, const float test_scale) {
943 bool paint_during_update = true;
944 bool has_thumb = false;
945 scoped_refptr<Layer> layer_tree_root = Layer::Create(layer_settings());
946 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
947 FakePaintedScrollbarLayer::Create(layer_settings(), paint_during_update,
948 has_thumb, layer_tree_root->id());
950 layer_tree_root->AddChild(scrollbar_layer);
952 layer_tree_host_->SetRootLayer(layer_tree_root);
954 scrollbar_layer->SetBounds(scrollbar_rect.size());
955 scrollbar_layer->SetPosition(scrollbar_rect.origin());
956 scrollbar_layer->fake_scrollbar()->set_location(scrollbar_rect.origin());
957 scrollbar_layer->fake_scrollbar()->set_track_rect(scrollbar_rect);
958 scrollbar_layer->set_visible_layer_rect(scrollbar_rect);
960 layer_tree_host_->SetDeviceScaleFactor(test_scale);
962 gfx::Rect screen_space_clip_rect;
963 scrollbar_layer->SavePaintProperties();
965 scrollbar_layer->Update();
967 UIResourceBitmap* bitmap = layer_tree_host_->ui_resource_bitmap(
968 scrollbar_layer->track_resource_id());
970 DCHECK(bitmap);
972 AutoLockUIResourceBitmap locked_bitmap(*bitmap);
974 const SkColor* pixels =
975 reinterpret_cast<const SkColor*>(locked_bitmap.GetPixels());
976 SkColor color = argb_to_skia(
977 scrollbar_layer->fake_scrollbar()->paint_fill_color());
978 int width = bitmap->GetSize().width();
979 int height = bitmap->GetSize().height();
981 // Make sure none of the corners of the bitmap were inadvertently clipped.
982 EXPECT_EQ(color, pixels[0])
983 << "Top left pixel doesn't match scrollbar color.";
985 EXPECT_EQ(color, pixels[width - 1])
986 << "Top right pixel doesn't match scrollbar color.";
988 EXPECT_EQ(color, pixels[width * (height - 1)])
989 << "Bottom left pixel doesn't match scrollbar color.";
991 EXPECT_EQ(color, pixels[width * height - 1])
992 << "Bottom right pixel doesn't match scrollbar color.";
995 protected:
996 // On Android, Skia uses ABGR
997 static SkColor argb_to_skia(SkColor c) {
998 return (SkColorGetA(c) << SK_A32_SHIFT) |
999 (SkColorGetR(c) << SK_R32_SHIFT) |
1000 (SkColorGetG(c) << SK_G32_SHIFT) |
1001 (SkColorGetB(c) << SK_B32_SHIFT);
1005 TEST_F(ScaledScrollbarLayerTestScaledRasterization, TestLostPrecisionInClip) {
1006 // Try rasterization at coordinates and scale that caused problematic
1007 // rounding and clipping errors.
1008 // Vertical Scrollbars.
1009 TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f);
1010 TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f);
1012 // Horizontal Scrollbars.
1013 TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f);
1014 TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f);
1017 } // namespace
1018 } // namespace cc