Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / cc / layers / scrollbar_layer_unittest.cc
blobaf76bc207a0ea2d35aba23b8302064c3fd05eeea
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;
124 layer_tree_settings_.use_one_copy = false;
126 LayerTreeHost::InitParams params;
127 params.client = &fake_client_;
128 params.settings = &layer_tree_settings_;
129 params.task_graph_runner = &task_graph_runner_;
131 layer_tree_host_.reset(
132 new FakeResourceTrackingLayerTreeHost(&fake_client_, &params));
133 fake_client_.SetLayerTreeHost(layer_tree_host_.get());
134 // Force output surface creation for renderer capabilities.
135 layer_tree_host_->Composite(base::TimeTicks());
136 EXPECT_FALSE(layer_tree_host_->output_surface_lost());
139 const LayerSettings& layer_settings() { return layer_settings_; }
141 protected:
142 FakeLayerTreeHostClient fake_client_;
143 TestTaskGraphRunner task_graph_runner_;
144 LayerTreeSettings layer_tree_settings_;
145 LayerSettings layer_settings_;
146 scoped_ptr<FakeResourceTrackingLayerTreeHost> layer_tree_host_;
149 TEST_F(ScrollbarLayerTest, ResolveScrollLayerPointer) {
150 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
151 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
152 layer_settings(), layer_tree_host_.get(), scrollbar.Pass(), false, false,
153 0, 0);
155 LayerImpl* cc_child1 = layer_impl_tree_root->children()[0];
156 PaintedScrollbarLayerImpl* cc_child2 =
157 static_cast<PaintedScrollbarLayerImpl*>(
158 layer_impl_tree_root->children()[1]);
160 EXPECT_EQ(cc_child1->scrollbars()->size(), 1UL);
161 EXPECT_EQ(*(cc_child1->scrollbars()->begin()), cc_child2);
164 TEST_F(ScrollbarLayerTest, ResolveScrollLayerPointer_ReverseOrder) {
165 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
166 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
167 layer_settings(), layer_tree_host_.get(), scrollbar.Pass(), true, false,
168 0, 0);
170 PaintedScrollbarLayerImpl* cc_child1 =
171 static_cast<PaintedScrollbarLayerImpl*>(
172 layer_impl_tree_root->children()[0]);
173 LayerImpl* cc_child2 = layer_impl_tree_root->children()[1];
175 EXPECT_EQ(cc_child2->scrollbars()->size(), 1UL);
176 EXPECT_EQ(*(cc_child2->scrollbars()->begin()), cc_child1);
179 TEST_F(ScrollbarLayerTest, ShouldScrollNonOverlayOnMainThread) {
180 // Create and attach a non-overlay scrollbar.
181 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
182 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
183 layer_settings(), layer_tree_host_.get(), scrollbar.Pass(), false, false,
184 0, 0);
185 PaintedScrollbarLayerImpl* scrollbar_layer_impl =
186 static_cast<PaintedScrollbarLayerImpl*>(
187 layer_impl_tree_root->children()[1]);
189 // When the scrollbar is not an overlay scrollbar, the scroll should be
190 // responded to on the main thread as the compositor does not yet implement
191 // scrollbar scrolling.
192 EXPECT_EQ(
193 InputHandler::SCROLL_ON_MAIN_THREAD,
194 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0), InputHandler::GESTURE,
195 SCROLL_BLOCKS_ON_NONE));
197 // Create and attach an overlay scrollbar.
198 scrollbar.reset(new FakeScrollbar(false, false, true));
200 layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
201 layer_settings(), layer_tree_host_.get(), scrollbar.Pass(), false, false,
202 0, 0);
203 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>(
204 layer_impl_tree_root->children()[1]);
206 // The user shouldn't be able to drag an overlay scrollbar and the scroll
207 // may be handled in the compositor.
208 EXPECT_EQ(
209 InputHandler::SCROLL_IGNORED,
210 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0), InputHandler::GESTURE,
211 SCROLL_BLOCKS_ON_NONE));
214 TEST_F(ScrollbarLayerTest, ScrollOffsetSynchronization) {
215 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
216 scoped_refptr<Layer> layer_tree_root = Layer::Create(layer_settings());
217 scoped_refptr<Layer> scroll_layer = Layer::Create(layer_settings());
218 scoped_refptr<Layer> content_layer = Layer::Create(layer_settings());
219 scoped_refptr<Layer> scrollbar_layer = PaintedScrollbarLayer::Create(
220 layer_settings(), scrollbar.Pass(), layer_tree_root->id());
222 // Choose bounds to give max_scroll_offset = (30, 50).
223 layer_tree_root->SetBounds(gfx::Size(70, 150));
224 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
225 scroll_layer->SetScrollOffset(gfx::ScrollOffset(10, 20));
226 scroll_layer->SetBounds(gfx::Size(100, 200));
227 content_layer->SetBounds(gfx::Size(100, 200));
229 layer_tree_host_->SetRootLayer(layer_tree_root);
230 layer_tree_root->AddChild(scroll_layer);
231 scroll_layer->AddChild(content_layer);
232 layer_tree_root->AddChild(scrollbar_layer);
233 scrollbar_layer->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
234 scrollbar_layer->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
236 layer_tree_root->SavePaintProperties();
237 content_layer->SavePaintProperties();
239 LayerImpl* layer_impl_tree_root =
240 layer_tree_host_->CommitAndCreateLayerImplTree();
242 ScrollbarLayerImplBase* cc_scrollbar_layer =
243 static_cast<PaintedScrollbarLayerImpl*>(
244 layer_impl_tree_root->children()[1]);
246 EXPECT_EQ(10.f, cc_scrollbar_layer->current_pos());
247 EXPECT_EQ(30, cc_scrollbar_layer->maximum());
249 layer_tree_root->SetBounds(gfx::Size(700, 1500));
250 layer_tree_root->SavePaintProperties();
251 scroll_layer->SetBounds(gfx::Size(1000, 2000));
252 scroll_layer->SetScrollOffset(gfx::ScrollOffset(100, 200));
253 scroll_layer->SavePaintProperties();
254 content_layer->SetBounds(gfx::Size(1000, 2000));
255 content_layer->SavePaintProperties();
257 ScrollbarAnimationController* scrollbar_controller =
258 layer_impl_tree_root->scrollbar_animation_controller();
259 layer_impl_tree_root = layer_tree_host_->CommitAndCreateLayerImplTree();
260 EXPECT_EQ(scrollbar_controller,
261 layer_impl_tree_root->scrollbar_animation_controller());
263 EXPECT_EQ(100.f, cc_scrollbar_layer->current_pos());
264 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
266 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
267 scroll_layer_impl->ScrollBy(gfx::Vector2d(12, 34));
269 EXPECT_EQ(112.f, cc_scrollbar_layer->current_pos());
270 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
273 #define UPDATE_AND_EXTRACT_LAYER_POINTERS() \
274 do { \
275 scrollbar_layer->UpdateInternalContentScale(); \
276 scrollbar_layer->UpdateThumbAndTrackGeometry(); \
277 root_clip_layer_impl = layer_tree_host_->CommitAndCreateLayerImplTree(); \
278 root_layer_impl = root_clip_layer_impl->children()[0]; \
279 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>( \
280 root_layer_impl->children()[1]); \
281 scrollbar_layer_impl->ScrollbarParametersDidChange(false); \
282 } while (false)
284 TEST_F(ScrollbarLayerTest, UpdatePropertiesOfScrollBarWhenThumbRemoved) {
285 scoped_refptr<Layer> root_clip_layer = Layer::Create(layer_settings());
286 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings());
287 scoped_refptr<Layer> content_layer = Layer::Create(layer_settings());
288 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
289 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
290 root_layer->id());
292 root_layer->SetScrollClipLayerId(root_clip_layer->id());
293 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
294 root_clip_layer->SetBounds(gfx::Size(20, 50));
295 root_layer->SetBounds(gfx::Size(100, 50));
296 content_layer->SetBounds(gfx::Size(100, 50));
298 layer_tree_host_->SetRootLayer(root_clip_layer);
299 root_clip_layer->AddChild(root_layer);
300 root_layer->AddChild(content_layer);
301 root_layer->AddChild(scrollbar_layer);
303 root_layer->SetScrollOffset(gfx::ScrollOffset(0, 0));
304 scrollbar_layer->SetBounds(gfx::Size(70, 10));
305 scrollbar_layer->SetScrollLayer(root_layer->id());
306 scrollbar_layer->SetClipLayer(root_clip_layer->id());
307 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
308 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
309 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
310 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
311 LayerImpl* root_clip_layer_impl = nullptr;
312 LayerImpl* root_layer_impl = nullptr;
313 PaintedScrollbarLayerImpl* scrollbar_layer_impl = nullptr;
315 UPDATE_AND_EXTRACT_LAYER_POINTERS();
316 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
317 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
319 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
321 UPDATE_AND_EXTRACT_LAYER_POINTERS();
322 EXPECT_EQ(gfx::Rect(10, 0, 0, 0).ToString(),
323 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
326 TEST_F(ScrollbarLayerTest, ThumbRect) {
327 scoped_refptr<Layer> root_clip_layer = Layer::Create(layer_settings());
328 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings());
329 scoped_refptr<Layer> content_layer = Layer::Create(layer_settings());
330 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
331 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
332 root_layer->id());
334 root_layer->SetScrollClipLayerId(root_clip_layer->id());
335 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
336 root_clip_layer->SetBounds(gfx::Size(20, 50));
337 root_layer->SetBounds(gfx::Size(100, 50));
338 content_layer->SetBounds(gfx::Size(100, 50));
340 layer_tree_host_->SetRootLayer(root_clip_layer);
341 root_clip_layer->AddChild(root_layer);
342 root_layer->AddChild(content_layer);
343 root_layer->AddChild(scrollbar_layer);
345 root_layer->SetScrollOffset(gfx::ScrollOffset(0, 0));
346 scrollbar_layer->SetBounds(gfx::Size(70, 10));
347 scrollbar_layer->SetScrollLayer(root_layer->id());
348 scrollbar_layer->SetClipLayer(root_clip_layer->id());
349 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
350 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
351 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
352 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
353 LayerImpl* root_clip_layer_impl = nullptr;
354 LayerImpl* root_layer_impl = nullptr;
355 PaintedScrollbarLayerImpl* scrollbar_layer_impl = nullptr;
357 // Thumb is at the edge of the scrollbar (should be inset to
358 // the start of the track within the scrollbar layer's
359 // position).
360 UPDATE_AND_EXTRACT_LAYER_POINTERS();
361 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
362 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
364 // Under-scroll (thumb position should clamp and be unchanged).
365 root_layer->SetScrollOffset(gfx::ScrollOffset(-5, 0));
367 UPDATE_AND_EXTRACT_LAYER_POINTERS();
368 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
369 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
371 // Over-scroll (thumb position should clamp on the far side).
372 root_layer->SetScrollOffset(gfx::ScrollOffset(85, 0));
374 UPDATE_AND_EXTRACT_LAYER_POINTERS();
375 EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(),
376 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
378 // Change thumb thickness and length.
379 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(4);
380 scrollbar_layer->fake_scrollbar()->set_thumb_length(6);
382 UPDATE_AND_EXTRACT_LAYER_POINTERS();
383 EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(),
384 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
386 // Shrink the scrollbar layer to cover only the track.
387 scrollbar_layer->SetBounds(gfx::Size(50, 10));
388 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(30, 10));
389 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
391 UPDATE_AND_EXTRACT_LAYER_POINTERS();
392 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
393 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
395 // Shrink the track in the non-scrolling dimension so that it only covers the
396 // middle third of the scrollbar layer (this does not affect the thumb
397 // position).
398 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6));
400 UPDATE_AND_EXTRACT_LAYER_POINTERS();
401 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
402 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
405 TEST_F(ScrollbarLayerTest, SolidColorDrawQuads) {
406 const int kThumbThickness = 3;
407 const int kTrackStart = 1;
408 const int kTrackLength = 100;
410 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
411 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
412 layer_settings(), layer_tree_host_.get(), scrollbar.Pass(), false, true,
413 kThumbThickness, kTrackStart);
414 ScrollbarLayerImplBase* scrollbar_layer_impl =
415 static_cast<SolidColorScrollbarLayerImpl*>(
416 layer_impl_tree_root->children()[1]);
417 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
418 scrollbar_layer_impl->SetCurrentPos(10.f);
419 scrollbar_layer_impl->SetMaximum(100);
420 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.4f);
422 // Thickness should be overridden to 3.
424 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
425 AppendQuadsData data;
426 scrollbar_layer_impl->AppendQuads(render_pass.get(), &data);
428 const QuadList& quads = render_pass->quad_list;
429 ASSERT_EQ(1u, quads.size());
430 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
431 EXPECT_EQ(gfx::Rect(6, 0, 39, 3), quads.front()->rect);
434 // For solid color scrollbars, position and size should reflect the
435 // current viewport state.
436 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.2f);
438 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
439 AppendQuadsData data;
440 scrollbar_layer_impl->AppendQuads(render_pass.get(), &data);
442 const QuadList& quads = render_pass->quad_list;
443 ASSERT_EQ(1u, quads.size());
444 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
445 EXPECT_EQ(gfx::Rect(8, 0, 19, 3), quads.front()->rect);
448 // We shouldn't attempt div-by-zero when the maximum is zero.
449 scrollbar_layer_impl->SetCurrentPos(0.f);
450 scrollbar_layer_impl->SetMaximum(0);
452 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
453 AppendQuadsData data;
454 scrollbar_layer_impl->AppendQuads(render_pass.get(), &data);
456 const QuadList& quads = render_pass->quad_list;
457 ASSERT_EQ(1u, quads.size());
458 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
459 EXPECT_EQ(gfx::Rect(1, 0, 19, 3), quads.front()->rect);
463 TEST_F(ScrollbarLayerTest, LayerDrivenSolidColorDrawQuads) {
464 const int kThumbThickness = 3;
465 const int kTrackStart = 0;
466 const int kTrackLength = 10;
468 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
471 scoped_refptr<Layer> layer_tree_root = Layer::Create(layer_settings());
472 scoped_refptr<Layer> scroll_layer = Layer::Create(layer_settings());
473 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
474 scoped_refptr<Layer> child1 = Layer::Create(layer_settings());
475 scoped_refptr<Layer> child2;
476 const bool kIsLeftSideVerticalScrollbar = false;
477 child2 = SolidColorScrollbarLayer::Create(
478 layer_settings(), scrollbar->Orientation(), kThumbThickness,
479 kTrackStart, kIsLeftSideVerticalScrollbar, child1->id());
480 child2->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
481 child2->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
482 scroll_layer->AddChild(child1);
483 scroll_layer->InsertChild(child2, 1);
484 layer_tree_root->AddChild(scroll_layer);
485 layer_tree_host_->SetRootLayer(layer_tree_root);
487 LayerImpl* layer_impl_tree_root =
488 layer_tree_host_->CommitAndCreateLayerImplTree();
489 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
491 auto* scrollbar_layer_impl =
492 static_cast<ScrollbarLayerImplBase*>(scroll_layer_impl->children()[1]);
494 // Choose layer bounds to give max_scroll_offset = (8, 8).
495 layer_impl_tree_root->SetBounds(gfx::Size(2, 2));
496 scroll_layer_impl->SetBounds(gfx::Size(10, 10));
497 scroll_layer_impl->ScrollBy(gfx::Vector2dF(4.f, 0.f));
499 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
500 scrollbar_layer_impl->SetCurrentPos(4.f);
501 scrollbar_layer_impl->SetMaximum(8);
504 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
506 AppendQuadsData data;
507 scrollbar_layer_impl->AppendQuads(render_pass.get(), &data);
509 const QuadList& quads = render_pass->quad_list;
510 ASSERT_EQ(1u, quads.size());
511 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
512 EXPECT_EQ(gfx::Rect(3, 0, 3, 3), quads.front()->rect);
516 class ScrollbarLayerSolidColorThumbTest : public testing::Test {
517 public:
518 ScrollbarLayerSolidColorThumbTest() {
519 LayerTreeSettings layer_tree_settings;
520 host_impl_.reset(new FakeLayerTreeHostImpl(layer_tree_settings, &proxy_,
521 &shared_bitmap_manager_,
522 &task_graph_runner_));
524 const int kThumbThickness = 3;
525 const int kTrackStart = 0;
526 const bool kIsLeftSideVerticalScrollbar = false;
527 const bool kIsOverlayScrollbar = false;
529 horizontal_scrollbar_layer_ =
530 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
532 HORIZONTAL,
533 kThumbThickness,
534 kTrackStart,
535 kIsLeftSideVerticalScrollbar,
536 kIsOverlayScrollbar);
537 vertical_scrollbar_layer_ =
538 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
540 VERTICAL,
541 kThumbThickness,
542 kTrackStart,
543 kIsLeftSideVerticalScrollbar,
544 kIsOverlayScrollbar);
547 protected:
548 FakeImplProxy proxy_;
549 TestSharedBitmapManager shared_bitmap_manager_;
550 TestTaskGraphRunner task_graph_runner_;
551 scoped_ptr<FakeLayerTreeHostImpl> host_impl_;
552 scoped_ptr<SolidColorScrollbarLayerImpl> horizontal_scrollbar_layer_;
553 scoped_ptr<SolidColorScrollbarLayerImpl> vertical_scrollbar_layer_;
556 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbLength) {
557 horizontal_scrollbar_layer_->SetCurrentPos(0);
558 horizontal_scrollbar_layer_->SetMaximum(10);
560 // Simple case - one third of the scrollable area is visible, so the thumb
561 // should be one third as long as the track.
562 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.33f);
563 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
564 EXPECT_EQ(33, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
566 // The thumb's length should never be less than its thickness.
567 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.01f);
568 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
569 EXPECT_EQ(3, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
572 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbPosition) {
573 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
574 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.1f);
576 horizontal_scrollbar_layer_->SetCurrentPos(0);
577 horizontal_scrollbar_layer_->SetMaximum(100);
578 EXPECT_EQ(0, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
579 EXPECT_EQ(10, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
581 horizontal_scrollbar_layer_->SetCurrentPos(100);
582 // The thumb is 10px long and the track is 100px, so the maximum thumb
583 // position is 90px.
584 EXPECT_EQ(90, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
586 horizontal_scrollbar_layer_->SetCurrentPos(80);
587 // The scroll position is 80% of the maximum, so the thumb's position should
588 // be at 80% of its maximum or 72px.
589 EXPECT_EQ(72, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
592 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbVerticalAdjust) {
593 SolidColorScrollbarLayerImpl* layers[2] =
594 { horizontal_scrollbar_layer_.get(), vertical_scrollbar_layer_.get() };
595 for (size_t i = 0; i < 2; ++i) {
596 layers[i]->SetVisibleToTotalLengthRatio(0.2f);
597 layers[i]->SetCurrentPos(25);
598 layers[i]->SetMaximum(100);
600 layers[0]->SetBounds(gfx::Size(100, 3));
601 layers[1]->SetBounds(gfx::Size(3, 100));
603 EXPECT_EQ(gfx::RectF(20.f, 0.f, 20.f, 3.f),
604 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
605 EXPECT_EQ(gfx::RectF(0.f, 20.f, 3.f, 20.f),
606 vertical_scrollbar_layer_->ComputeThumbQuadRect());
608 horizontal_scrollbar_layer_->SetVerticalAdjust(10.f);
609 vertical_scrollbar_layer_->SetVerticalAdjust(10.f);
611 // The vertical adjustment factor has two effects:
612 // 1.) Moves the horizontal scrollbar down
613 // 2.) Increases the vertical scrollbar's effective track length which both
614 // increases the thumb's length and its position within the track.
615 EXPECT_EQ(gfx::Rect(20.f, 10.f, 20.f, 3.f),
616 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
617 EXPECT_EQ(gfx::Rect(0.f, 22, 3.f, 22.f),
618 vertical_scrollbar_layer_->ComputeThumbQuadRect());
621 class ScrollbarLayerTestMaxTextureSize : public LayerTreeTest {
622 public:
623 ScrollbarLayerTestMaxTextureSize() {}
625 void SetScrollbarBounds(const gfx::Size& bounds) { bounds_ = bounds; }
627 void BeginTest() override {
628 scroll_layer_ = Layer::Create(layer_settings());
629 layer_tree_host()->root_layer()->AddChild(scroll_layer_);
631 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
632 scrollbar_layer_ = PaintedScrollbarLayer::Create(
633 layer_settings(), scrollbar.Pass(), scroll_layer_->id());
634 scrollbar_layer_->SetScrollLayer(scroll_layer_->id());
635 scrollbar_layer_->SetLayerTreeHost(layer_tree_host());
636 scrollbar_layer_->SetBounds(bounds_);
637 scrollbar_layer_->SetIsDrawable(true);
638 layer_tree_host()->root_layer()->AddChild(scrollbar_layer_);
640 PostSetNeedsCommitToMainThread();
643 void DidCommitAndDrawFrame() override {
644 const int kMaxTextureSize =
645 layer_tree_host()->GetRendererCapabilities().max_texture_size;
647 // Check first that we're actually testing something.
648 EXPECT_GT(scrollbar_layer_->bounds().width(), kMaxTextureSize);
650 EXPECT_EQ(scrollbar_layer_->internal_content_bounds().width(),
651 kMaxTextureSize - 1);
652 EXPECT_EQ(scrollbar_layer_->internal_content_bounds().height(),
653 kMaxTextureSize - 1);
655 EndTest();
658 void AfterTest() override {}
660 private:
661 scoped_refptr<PaintedScrollbarLayer> scrollbar_layer_;
662 scoped_refptr<Layer> scroll_layer_;
663 gfx::Size bounds_;
666 TEST_F(ScrollbarLayerTestMaxTextureSize, DirectRenderer) {
667 scoped_ptr<TestWebGraphicsContext3D> context =
668 TestWebGraphicsContext3D::Create();
669 int max_size = 0;
670 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
671 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
672 RunTest(true, false);
675 TEST_F(ScrollbarLayerTestMaxTextureSize, DelegatingRenderer) {
676 scoped_ptr<TestWebGraphicsContext3D> context =
677 TestWebGraphicsContext3D::Create();
678 int max_size = 0;
679 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
680 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
681 RunTest(true, true);
684 class ScrollbarLayerTestResourceCreationAndRelease : public ScrollbarLayerTest {
685 public:
686 void TestResourceUpload(int num_updates,
687 size_t expected_resources,
688 int expected_created,
689 int expected_deleted,
690 bool use_solid_color_scrollbar) {
691 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, false));
692 scoped_refptr<Layer> layer_tree_root = Layer::Create(layer_settings());
693 scoped_refptr<Layer> content_layer = Layer::Create(layer_settings());
694 scoped_refptr<Layer> scrollbar_layer;
695 if (use_solid_color_scrollbar) {
696 const int kThumbThickness = 3;
697 const int kTrackStart = 0;
698 const bool kIsLeftSideVerticalScrollbar = false;
699 scrollbar_layer = SolidColorScrollbarLayer::Create(
700 layer_settings(), scrollbar->Orientation(), kThumbThickness,
701 kTrackStart, kIsLeftSideVerticalScrollbar, layer_tree_root->id());
702 } else {
703 scrollbar_layer = PaintedScrollbarLayer::Create(
704 layer_settings(), scrollbar.Pass(), layer_tree_root->id());
706 layer_tree_root->AddChild(content_layer);
707 layer_tree_root->AddChild(scrollbar_layer);
709 layer_tree_host_->SetRootLayer(layer_tree_root);
711 scrollbar_layer->SetIsDrawable(true);
712 scrollbar_layer->SetBounds(gfx::Size(100, 100));
713 layer_tree_root->SetScrollOffset(gfx::ScrollOffset(10, 20));
714 layer_tree_root->SetBounds(gfx::Size(100, 200));
715 content_layer->SetBounds(gfx::Size(100, 200));
716 scrollbar_layer->draw_properties().visible_layer_rect =
717 gfx::Rect(0, 0, 100, 200);
718 scrollbar_layer->CreateRenderSurface();
719 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
721 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
722 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
724 scrollbar_layer->SavePaintProperties();
725 for (int update_counter = 0; update_counter < num_updates; update_counter++)
726 scrollbar_layer->Update();
728 // A non-solid-color scrollbar should have requested two textures.
729 EXPECT_EQ(expected_resources, layer_tree_host_->UIResourceCount());
730 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
731 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
733 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
735 scrollbar_layer->ClearRenderSurface();
739 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, ResourceUpload) {
740 bool use_solid_color_scrollbars = false;
741 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
742 int num_updates[3] = {1, 5, 10};
743 int created = 0;
744 int deleted = 0;
745 for (int j = 0; j < 3; j++) {
746 created += num_updates[j] * 2;
747 deleted = created - 2;
748 TestResourceUpload(num_updates[j], 2, created, deleted,
749 use_solid_color_scrollbars);
753 TEST_F(ScrollbarLayerTestResourceCreationAndRelease,
754 SolidColorNoResourceUpload) {
755 bool use_solid_color_scrollbars = true;
756 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
757 TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars);
760 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, TestResourceUpdate) {
761 gfx::Point scrollbar_location(0, 185);
762 scoped_refptr<Layer> layer_tree_root = Layer::Create(layer_settings());
763 scoped_refptr<Layer> content_layer = Layer::Create(layer_settings());
764 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
765 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
766 layer_tree_root->id());
768 layer_tree_root->AddChild(content_layer);
769 layer_tree_root->AddChild(scrollbar_layer);
771 layer_tree_host_->SetRootLayer(layer_tree_root);
773 scrollbar_layer->SetIsDrawable(true);
774 scrollbar_layer->SetBounds(gfx::Size(100, 15));
775 scrollbar_layer->SetPosition(scrollbar_location);
776 layer_tree_root->SetBounds(gfx::Size(100, 200));
777 content_layer->SetBounds(gfx::Size(100, 200));
779 scrollbar_layer->draw_properties().visible_layer_rect =
780 gfx::Rect(0, 0, 100, 200);
782 scrollbar_layer->CreateRenderSurface();
783 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
785 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
786 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
788 size_t resource_count;
789 int expected_created, expected_deleted;
790 scrollbar_layer->SavePaintProperties();
792 resource_count = 2;
793 expected_created = 2;
794 expected_deleted = 0;
795 EXPECT_TRUE(scrollbar_layer->Update());
796 EXPECT_NE(0, scrollbar_layer->track_resource_id());
797 EXPECT_NE(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_TRUE(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 = 0;
814 expected_created = 2;
815 expected_deleted = 2;
816 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
817 EXPECT_FALSE(scrollbar_layer->Update());
818 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
819 EXPECT_EQ(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 = 2;
825 expected_created = 4;
826 expected_deleted = 2;
827 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
828 EXPECT_TRUE(scrollbar_layer->Update());
829 EXPECT_NE(0, scrollbar_layer->track_resource_id());
830 EXPECT_NE(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 = 1;
836 expected_created = 5;
837 expected_deleted = 4;
838 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
839 EXPECT_TRUE(scrollbar_layer->Update());
840 EXPECT_NE(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 = 0;
847 expected_created = 5;
848 expected_deleted = 5;
849 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
850 EXPECT_TRUE(scrollbar_layer->Update());
851 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
852 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
853 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
854 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
855 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
857 resource_count = 2;
858 expected_created = 7;
859 expected_deleted = 5;
860 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
861 scrollbar_layer->fake_scrollbar()->set_has_thumb(true);
862 EXPECT_TRUE(scrollbar_layer->Update());
863 EXPECT_NE(0, scrollbar_layer->track_resource_id());
864 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
866 resource_count = 1;
867 expected_created = 8;
868 expected_deleted = 7;
869 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
870 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
871 scrollbar_layer->SetBounds(gfx::Size(90, 15));
872 EXPECT_TRUE(scrollbar_layer->Update());
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());
876 EXPECT_EQ(
877 gfx::Size(90, 15),
878 layer_tree_host_->ui_resource_size(scrollbar_layer->track_resource_id()));
880 scrollbar_layer->ResetNeedsDisplayForTesting();
881 EXPECT_FALSE(scrollbar_layer->Update());
882 EXPECT_NE(0, scrollbar_layer->track_resource_id());
883 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
884 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
885 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
886 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
888 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
889 scrollbar_layer->ClearRenderSurface();
892 class ScaledScrollbarLayerTestResourceCreation : public ScrollbarLayerTest {
893 public:
894 void TestResourceUpload(const float test_scale) {
895 gfx::Point scrollbar_location(0, 185);
896 scoped_refptr<Layer> layer_tree_root = Layer::Create(layer_settings());
897 scoped_refptr<Layer> content_layer = Layer::Create(layer_settings());
898 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
899 FakePaintedScrollbarLayer::Create(layer_settings(), false, true,
900 layer_tree_root->id());
902 layer_tree_root->AddChild(content_layer);
903 layer_tree_root->AddChild(scrollbar_layer);
905 layer_tree_host_->SetRootLayer(layer_tree_root);
907 scrollbar_layer->SetIsDrawable(true);
908 scrollbar_layer->SetBounds(gfx::Size(100, 15));
909 scrollbar_layer->SetPosition(scrollbar_location);
910 layer_tree_root->SetBounds(gfx::Size(100, 200));
911 content_layer->SetBounds(gfx::Size(100, 200));
912 scrollbar_layer->draw_properties().visible_layer_rect =
913 gfx::Rect(scrollbar_location, scrollbar_layer->bounds());
914 scrollbar_layer->CreateRenderSurface();
915 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
917 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
918 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
920 layer_tree_host_->SetDeviceScaleFactor(test_scale);
922 scrollbar_layer->SavePaintProperties();
923 scrollbar_layer->Update();
925 // Verify that we have not generated any content uploads that are larger
926 // than their destination textures.
928 gfx::Size track_size = layer_tree_host_->ui_resource_size(
929 scrollbar_layer->track_resource_id());
930 gfx::Size thumb_size = layer_tree_host_->ui_resource_size(
931 scrollbar_layer->thumb_resource_id());
933 EXPECT_LE(track_size.width(),
934 scrollbar_layer->internal_content_bounds().width());
935 EXPECT_LE(track_size.height(),
936 scrollbar_layer->internal_content_bounds().height());
937 EXPECT_LE(thumb_size.width(),
938 scrollbar_layer->internal_content_bounds().width());
939 EXPECT_LE(thumb_size.height(),
940 scrollbar_layer->internal_content_bounds().height());
942 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
944 scrollbar_layer->ClearRenderSurface();
948 TEST_F(ScaledScrollbarLayerTestResourceCreation, ScaledResourceUpload) {
949 // Pick a test scale that moves the scrollbar's (non-zero) position to
950 // a non-pixel-aligned location.
951 TestResourceUpload(.041f);
952 TestResourceUpload(1.41f);
953 TestResourceUpload(4.1f);
956 class ScaledScrollbarLayerTestScaledRasterization : public ScrollbarLayerTest {
957 public:
958 void TestScale(const gfx::Rect scrollbar_rect, const float test_scale) {
959 bool paint_during_update = true;
960 bool has_thumb = false;
961 scoped_refptr<Layer> layer_tree_root = Layer::Create(layer_settings());
962 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
963 FakePaintedScrollbarLayer::Create(layer_settings(), paint_during_update,
964 has_thumb, layer_tree_root->id());
966 layer_tree_root->AddChild(scrollbar_layer);
968 layer_tree_host_->SetRootLayer(layer_tree_root);
970 scrollbar_layer->SetBounds(scrollbar_rect.size());
971 scrollbar_layer->SetPosition(scrollbar_rect.origin());
972 scrollbar_layer->fake_scrollbar()->set_location(scrollbar_rect.origin());
973 scrollbar_layer->fake_scrollbar()->set_track_rect(scrollbar_rect);
974 scrollbar_layer->draw_properties().visible_layer_rect = scrollbar_rect;
976 layer_tree_host_->SetDeviceScaleFactor(test_scale);
978 gfx::Rect screen_space_clip_rect;
979 scrollbar_layer->SavePaintProperties();
981 scrollbar_layer->Update();
983 UIResourceBitmap* bitmap = layer_tree_host_->ui_resource_bitmap(
984 scrollbar_layer->track_resource_id());
986 DCHECK(bitmap);
988 AutoLockUIResourceBitmap locked_bitmap(*bitmap);
990 const SkColor* pixels =
991 reinterpret_cast<const SkColor*>(locked_bitmap.GetPixels());
992 SkColor color = argb_to_skia(
993 scrollbar_layer->fake_scrollbar()->paint_fill_color());
994 int width = bitmap->GetSize().width();
995 int height = bitmap->GetSize().height();
997 // Make sure none of the corners of the bitmap were inadvertently clipped.
998 EXPECT_EQ(color, pixels[0])
999 << "Top left pixel doesn't match scrollbar color.";
1001 EXPECT_EQ(color, pixels[width - 1])
1002 << "Top right pixel doesn't match scrollbar color.";
1004 EXPECT_EQ(color, pixels[width * (height - 1)])
1005 << "Bottom left pixel doesn't match scrollbar color.";
1007 EXPECT_EQ(color, pixels[width * height - 1])
1008 << "Bottom right pixel doesn't match scrollbar color.";
1011 protected:
1012 // On Android, Skia uses ABGR
1013 static SkColor argb_to_skia(SkColor c) {
1014 return (SkColorGetA(c) << SK_A32_SHIFT) |
1015 (SkColorGetR(c) << SK_R32_SHIFT) |
1016 (SkColorGetG(c) << SK_G32_SHIFT) |
1017 (SkColorGetB(c) << SK_B32_SHIFT);
1021 TEST_F(ScaledScrollbarLayerTestScaledRasterization, TestLostPrecisionInClip) {
1022 // Try rasterization at coordinates and scale that caused problematic
1023 // rounding and clipping errors.
1024 // Vertical Scrollbars.
1025 TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f);
1026 TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f);
1028 // Horizontal Scrollbars.
1029 TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f);
1030 TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f);
1033 } // namespace
1034 } // namespace cc