Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / cc / layers / scrollbar_layer_unittest.cc
blob36c37e9d94dd91c155e98cc4818ca677dacc43ba
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/containers/hash_tables.h"
6 #include "cc/animation/scrollbar_animation_controller.h"
7 #include "cc/layers/append_quads_data.h"
8 #include "cc/layers/painted_scrollbar_layer.h"
9 #include "cc/layers/painted_scrollbar_layer_impl.h"
10 #include "cc/layers/scrollbar_layer_interface.h"
11 #include "cc/layers/solid_color_scrollbar_layer.h"
12 #include "cc/layers/solid_color_scrollbar_layer_impl.h"
13 #include "cc/quads/solid_color_draw_quad.h"
14 #include "cc/resources/resource_update_queue.h"
15 #include "cc/test/fake_impl_proxy.h"
16 #include "cc/test/fake_layer_tree_host.h"
17 #include "cc/test/fake_layer_tree_host_client.h"
18 #include "cc/test/fake_layer_tree_host_impl.h"
19 #include "cc/test/fake_painted_scrollbar_layer.h"
20 #include "cc/test/fake_scrollbar.h"
21 #include "cc/test/geometry_test_utils.h"
22 #include "cc/test/layer_tree_test.h"
23 #include "cc/test/mock_occlusion_tracker.h"
24 #include "cc/test/test_web_graphics_context_3d.h"
25 #include "cc/trees/layer_tree_host.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "cc/trees/occlusion_tracker.h"
28 #include "cc/trees/single_thread_proxy.h"
29 #include "cc/trees/tree_synchronizer.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 namespace cc {
34 namespace {
36 LayerImpl* LayerImplForScrollAreaAndScrollbar(FakeLayerTreeHost* host,
37 scoped_ptr<Scrollbar> scrollbar,
38 bool reverse_order,
39 bool use_solid_color_scrollbar,
40 int thumb_thickness,
41 int track_start) {
42 scoped_refptr<Layer> layer_tree_root = Layer::Create();
43 scoped_refptr<Layer> child1 = Layer::Create();
44 scoped_refptr<Layer> child2;
45 if (use_solid_color_scrollbar) {
46 const bool kIsLeftSideVerticalScrollbar = false;
47 child2 = SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
48 thumb_thickness,
49 track_start,
50 kIsLeftSideVerticalScrollbar,
51 child1->id());
52 } else {
53 child2 = PaintedScrollbarLayer::Create(scrollbar.Pass(), child1->id());
55 child2->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
56 layer_tree_root->AddChild(child1);
57 layer_tree_root->InsertChild(child2, reverse_order ? 0 : 1);
58 host->SetRootLayer(layer_tree_root);
59 return host->CommitAndCreateLayerImplTree();
62 class FakeResourceTrackingLayerTreeHost : public FakeLayerTreeHost {
63 public:
64 FakeResourceTrackingLayerTreeHost(FakeLayerTreeHostClient* client,
65 const LayerTreeSettings& settings)
66 : FakeLayerTreeHost(client, settings),
67 next_id_(1),
68 total_ui_resource_created_(0),
69 total_ui_resource_deleted_(0) {
70 InitializeSingleThreaded(client, base::MessageLoopProxy::current(),
71 nullptr);
74 UIResourceId CreateUIResource(UIResourceClient* content) override {
75 total_ui_resource_created_++;
76 UIResourceId nid = next_id_++;
77 ui_resource_bitmap_map_.insert(
78 std::make_pair(nid, content->GetBitmap(nid, false)));
79 return nid;
82 // Deletes a UI resource. May safely be called more than once.
83 void DeleteUIResource(UIResourceId id) override {
84 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
85 if (iter != ui_resource_bitmap_map_.end()) {
86 ui_resource_bitmap_map_.erase(iter);
87 total_ui_resource_deleted_++;
91 size_t UIResourceCount() { return ui_resource_bitmap_map_.size(); }
92 int TotalUIResourceDeleted() { return total_ui_resource_deleted_; }
93 int TotalUIResourceCreated() { return total_ui_resource_created_; }
95 gfx::Size ui_resource_size(UIResourceId id) {
96 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
97 if (iter != ui_resource_bitmap_map_.end())
98 return iter->second.GetSize();
99 return gfx::Size();
102 UIResourceBitmap* ui_resource_bitmap(UIResourceId id) {
103 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
104 if (iter != ui_resource_bitmap_map_.end())
105 return &iter->second;
106 return nullptr;
109 private:
110 using UIResourceBitmapMap = base::hash_map<UIResourceId, UIResourceBitmap>;
111 UIResourceBitmapMap ui_resource_bitmap_map_;
113 int next_id_;
114 int total_ui_resource_created_;
115 int total_ui_resource_deleted_;
118 class ScrollbarLayerTest : public testing::Test {
119 public:
120 ScrollbarLayerTest() : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {
121 layer_tree_settings_.single_thread_proxy_scheduler = false;
122 layer_tree_host_.reset(new FakeResourceTrackingLayerTreeHost(
123 &fake_client_, layer_tree_settings_));
124 fake_client_.SetLayerTreeHost(layer_tree_host_.get());
125 // Force output surface creation for renderer capabilities.
126 layer_tree_host_->Composite(base::TimeTicks());
127 EXPECT_FALSE(layer_tree_host_->output_surface_lost());
130 protected:
131 FakeLayerTreeHostClient fake_client_;
132 LayerTreeSettings layer_tree_settings_;
133 scoped_ptr<FakeResourceTrackingLayerTreeHost> layer_tree_host_;
136 TEST_F(ScrollbarLayerTest, ResolveScrollLayerPointer) {
137 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
138 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
139 layer_tree_host_.get(), scrollbar.Pass(), false, false, 0, 0);
141 LayerImpl* cc_child1 = layer_impl_tree_root->children()[0];
142 PaintedScrollbarLayerImpl* cc_child2 =
143 static_cast<PaintedScrollbarLayerImpl*>(
144 layer_impl_tree_root->children()[1]);
146 EXPECT_EQ(cc_child1->scrollbars()->size(), 1UL);
147 EXPECT_EQ(*(cc_child1->scrollbars()->begin()), cc_child2);
150 TEST_F(ScrollbarLayerTest, ResolveScrollLayerPointer_ReverseOrder) {
151 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
152 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
153 layer_tree_host_.get(), scrollbar.Pass(), true, false, 0, 0);
155 PaintedScrollbarLayerImpl* cc_child1 =
156 static_cast<PaintedScrollbarLayerImpl*>(
157 layer_impl_tree_root->children()[0]);
158 LayerImpl* cc_child2 = layer_impl_tree_root->children()[1];
160 EXPECT_EQ(cc_child2->scrollbars()->size(), 1UL);
161 EXPECT_EQ(*(cc_child2->scrollbars()->begin()), cc_child1);
164 TEST_F(ScrollbarLayerTest, ShouldScrollNonOverlayOnMainThread) {
165 // Create and attach a non-overlay scrollbar.
166 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
167 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
168 layer_tree_host_.get(), scrollbar.Pass(), false, false, 0, 0);
169 PaintedScrollbarLayerImpl* scrollbar_layer_impl =
170 static_cast<PaintedScrollbarLayerImpl*>(
171 layer_impl_tree_root->children()[1]);
173 // When the scrollbar is not an overlay scrollbar, the scroll should be
174 // responded to on the main thread as the compositor does not yet implement
175 // scrollbar scrolling.
176 EXPECT_EQ(
177 InputHandler::SCROLL_ON_MAIN_THREAD,
178 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0), InputHandler::GESTURE,
179 SCROLL_BLOCKS_ON_NONE));
181 // Create and attach an overlay scrollbar.
182 scrollbar.reset(new FakeScrollbar(false, false, true));
184 layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
185 layer_tree_host_.get(), scrollbar.Pass(), false, false, 0, 0);
186 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>(
187 layer_impl_tree_root->children()[1]);
189 // The user shouldn't be able to drag an overlay scrollbar and the scroll
190 // may be handled in the compositor.
191 EXPECT_EQ(
192 InputHandler::SCROLL_IGNORED,
193 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0), InputHandler::GESTURE,
194 SCROLL_BLOCKS_ON_NONE));
197 TEST_F(ScrollbarLayerTest, ScrollOffsetSynchronization) {
198 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
199 scoped_refptr<Layer> layer_tree_root = Layer::Create();
200 scoped_refptr<Layer> scroll_layer = Layer::Create();
201 scoped_refptr<Layer> content_layer = Layer::Create();
202 scoped_refptr<Layer> scrollbar_layer =
203 PaintedScrollbarLayer::Create(scrollbar.Pass(), layer_tree_root->id());
205 // Choose bounds to give max_scroll_offset = (30, 50).
206 layer_tree_root->SetBounds(gfx::Size(70, 150));
207 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
208 scroll_layer->SetScrollOffset(gfx::ScrollOffset(10, 20));
209 scroll_layer->SetBounds(gfx::Size(100, 200));
210 content_layer->SetBounds(gfx::Size(100, 200));
212 layer_tree_host_->SetRootLayer(layer_tree_root);
213 layer_tree_root->AddChild(scroll_layer);
214 scroll_layer->AddChild(content_layer);
215 layer_tree_root->AddChild(scrollbar_layer);
216 scrollbar_layer->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
217 scrollbar_layer->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
219 layer_tree_root->SavePaintProperties();
220 content_layer->SavePaintProperties();
222 LayerImpl* layer_impl_tree_root =
223 layer_tree_host_->CommitAndCreateLayerImplTree();
225 ScrollbarLayerImplBase* cc_scrollbar_layer =
226 static_cast<PaintedScrollbarLayerImpl*>(
227 layer_impl_tree_root->children()[1]);
229 EXPECT_EQ(10.f, cc_scrollbar_layer->current_pos());
230 EXPECT_EQ(30, cc_scrollbar_layer->maximum());
232 layer_tree_root->SetBounds(gfx::Size(700, 1500));
233 layer_tree_root->SavePaintProperties();
234 scroll_layer->SetBounds(gfx::Size(1000, 2000));
235 scroll_layer->SetScrollOffset(gfx::ScrollOffset(100, 200));
236 scroll_layer->SavePaintProperties();
237 content_layer->SetBounds(gfx::Size(1000, 2000));
238 content_layer->SavePaintProperties();
240 ScrollbarAnimationController* scrollbar_controller =
241 layer_impl_tree_root->scrollbar_animation_controller();
242 layer_impl_tree_root = layer_tree_host_->CommitAndCreateLayerImplTree();
243 EXPECT_EQ(scrollbar_controller,
244 layer_impl_tree_root->scrollbar_animation_controller());
246 EXPECT_EQ(100.f, cc_scrollbar_layer->current_pos());
247 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
249 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
250 scroll_layer_impl->ScrollBy(gfx::Vector2d(12, 34));
252 EXPECT_EQ(112.f, cc_scrollbar_layer->current_pos());
253 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
256 #define UPDATE_AND_EXTRACT_LAYER_POINTERS() \
257 do { \
258 scrollbar_layer->UpdateInternalContentScale(); \
259 scrollbar_layer->UpdateThumbAndTrackGeometry(); \
260 root_clip_layer_impl = layer_tree_host_->CommitAndCreateLayerImplTree(); \
261 root_layer_impl = root_clip_layer_impl->children()[0]; \
262 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>( \
263 root_layer_impl->children()[1]); \
264 scrollbar_layer_impl->ScrollbarParametersDidChange(false); \
265 } while (false)
267 TEST_F(ScrollbarLayerTest, UpdatePropertiesOfScrollBarWhenThumbRemoved) {
268 scoped_refptr<Layer> root_clip_layer = Layer::Create();
269 scoped_refptr<Layer> root_layer = Layer::Create();
270 scoped_refptr<Layer> content_layer = Layer::Create();
271 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
272 FakePaintedScrollbarLayer::Create(false, true, root_layer->id());
274 root_layer->SetScrollClipLayerId(root_clip_layer->id());
275 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
276 root_clip_layer->SetBounds(gfx::Size(20, 50));
277 root_layer->SetBounds(gfx::Size(100, 50));
278 content_layer->SetBounds(gfx::Size(100, 50));
280 layer_tree_host_->SetRootLayer(root_clip_layer);
281 root_clip_layer->AddChild(root_layer);
282 root_layer->AddChild(content_layer);
283 root_layer->AddChild(scrollbar_layer);
285 root_layer->SetScrollOffset(gfx::ScrollOffset(0, 0));
286 scrollbar_layer->SetBounds(gfx::Size(70, 10));
287 scrollbar_layer->SetScrollLayer(root_layer->id());
288 scrollbar_layer->SetClipLayer(root_clip_layer->id());
289 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
290 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
291 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
292 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
293 LayerImpl* root_clip_layer_impl = nullptr;
294 LayerImpl* root_layer_impl = nullptr;
295 PaintedScrollbarLayerImpl* scrollbar_layer_impl = nullptr;
297 UPDATE_AND_EXTRACT_LAYER_POINTERS();
298 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
299 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
301 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
303 UPDATE_AND_EXTRACT_LAYER_POINTERS();
304 EXPECT_EQ(gfx::Rect(10, 0, 0, 0).ToString(),
305 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
308 TEST_F(ScrollbarLayerTest, ThumbRect) {
309 scoped_refptr<Layer> root_clip_layer = Layer::Create();
310 scoped_refptr<Layer> root_layer = Layer::Create();
311 scoped_refptr<Layer> content_layer = Layer::Create();
312 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
313 FakePaintedScrollbarLayer::Create(false, true, root_layer->id());
315 root_layer->SetScrollClipLayerId(root_clip_layer->id());
316 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
317 root_clip_layer->SetBounds(gfx::Size(20, 50));
318 root_layer->SetBounds(gfx::Size(100, 50));
319 content_layer->SetBounds(gfx::Size(100, 50));
321 layer_tree_host_->SetRootLayer(root_clip_layer);
322 root_clip_layer->AddChild(root_layer);
323 root_layer->AddChild(content_layer);
324 root_layer->AddChild(scrollbar_layer);
326 root_layer->SetScrollOffset(gfx::ScrollOffset(0, 0));
327 scrollbar_layer->SetBounds(gfx::Size(70, 10));
328 scrollbar_layer->SetScrollLayer(root_layer->id());
329 scrollbar_layer->SetClipLayer(root_clip_layer->id());
330 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
331 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
332 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
333 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
334 LayerImpl* root_clip_layer_impl = nullptr;
335 LayerImpl* root_layer_impl = nullptr;
336 PaintedScrollbarLayerImpl* scrollbar_layer_impl = nullptr;
338 // Thumb is at the edge of the scrollbar (should be inset to
339 // the start of the track within the scrollbar layer's
340 // position).
341 UPDATE_AND_EXTRACT_LAYER_POINTERS();
342 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
343 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
345 // Under-scroll (thumb position should clamp and be unchanged).
346 root_layer->SetScrollOffset(gfx::ScrollOffset(-5, 0));
348 UPDATE_AND_EXTRACT_LAYER_POINTERS();
349 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
350 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
352 // Over-scroll (thumb position should clamp on the far side).
353 root_layer->SetScrollOffset(gfx::ScrollOffset(85, 0));
355 UPDATE_AND_EXTRACT_LAYER_POINTERS();
356 EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(),
357 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
359 // Change thumb thickness and length.
360 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(4);
361 scrollbar_layer->fake_scrollbar()->set_thumb_length(6);
363 UPDATE_AND_EXTRACT_LAYER_POINTERS();
364 EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(),
365 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
367 // Shrink the scrollbar layer to cover only the track.
368 scrollbar_layer->SetBounds(gfx::Size(50, 10));
369 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(30, 10));
370 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
372 UPDATE_AND_EXTRACT_LAYER_POINTERS();
373 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
374 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
376 // Shrink the track in the non-scrolling dimension so that it only covers the
377 // middle third of the scrollbar layer (this does not affect the thumb
378 // position).
379 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6));
381 UPDATE_AND_EXTRACT_LAYER_POINTERS();
382 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
383 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
386 TEST_F(ScrollbarLayerTest, SolidColorDrawQuads) {
387 const int kThumbThickness = 3;
388 const int kTrackStart = 1;
389 const int kTrackLength = 100;
391 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
392 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
393 layer_tree_host_.get(), scrollbar.Pass(), false, true, kThumbThickness,
394 kTrackStart);
395 ScrollbarLayerImplBase* scrollbar_layer_impl =
396 static_cast<SolidColorScrollbarLayerImpl*>(
397 layer_impl_tree_root->children()[1]);
398 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
399 scrollbar_layer_impl->SetCurrentPos(10.f);
400 scrollbar_layer_impl->SetMaximum(100);
401 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.4f);
403 // Thickness should be overridden to 3.
405 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
406 AppendQuadsData data;
407 scrollbar_layer_impl->AppendQuads(render_pass.get(), &data);
409 const QuadList& quads = render_pass->quad_list;
410 ASSERT_EQ(1u, quads.size());
411 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
412 EXPECT_EQ(gfx::Rect(6, 0, 39, 3), quads.front()->rect);
415 // Contents scale should scale the draw quad.
416 scrollbar_layer_impl->draw_properties().contents_scale_x = 2.f;
417 scrollbar_layer_impl->draw_properties().contents_scale_y = 2.f;
419 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
420 AppendQuadsData data;
421 scrollbar_layer_impl->AppendQuads(render_pass.get(), &data);
423 const QuadList& quads = render_pass->quad_list;
424 ASSERT_EQ(1u, quads.size());
425 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
426 EXPECT_EQ(gfx::Rect(12, 0, 78, 6), quads.front()->rect);
428 scrollbar_layer_impl->draw_properties().contents_scale_x = 1.f;
429 scrollbar_layer_impl->draw_properties().contents_scale_y = 1.f;
431 // For solid color scrollbars, position and size should reflect the
432 // current viewport state.
433 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.2f);
435 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
436 AppendQuadsData data;
437 scrollbar_layer_impl->AppendQuads(render_pass.get(), &data);
439 const QuadList& quads = render_pass->quad_list;
440 ASSERT_EQ(1u, quads.size());
441 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
442 EXPECT_EQ(gfx::Rect(8, 0, 19, 3), quads.front()->rect);
445 // We shouldn't attempt div-by-zero when the maximum is zero.
446 scrollbar_layer_impl->SetCurrentPos(0.f);
447 scrollbar_layer_impl->SetMaximum(0);
449 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
450 AppendQuadsData data;
451 scrollbar_layer_impl->AppendQuads(render_pass.get(), &data);
453 const QuadList& quads = render_pass->quad_list;
454 ASSERT_EQ(1u, quads.size());
455 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads.front()->material);
456 EXPECT_EQ(gfx::Rect(1, 0, 19, 3), quads.front()->rect);
460 TEST_F(ScrollbarLayerTest, LayerDrivenSolidColorDrawQuads) {
461 const int kThumbThickness = 3;
462 const int kTrackStart = 0;
463 const int kTrackLength = 10;
465 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
468 scoped_refptr<Layer> layer_tree_root = Layer::Create();
469 scoped_refptr<Layer> scroll_layer = Layer::Create();
470 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
471 scoped_refptr<Layer> child1 = Layer::Create();
472 scoped_refptr<Layer> child2;
473 const bool kIsLeftSideVerticalScrollbar = false;
474 child2 = SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
475 kThumbThickness,
476 kTrackStart,
477 kIsLeftSideVerticalScrollbar,
478 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 ScrollbarLayerImplBase* scrollbar_layer_impl =
491 static_cast<PaintedScrollbarLayerImpl*>(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(
520 layer_tree_settings, &proxy_, &shared_bitmap_manager_));
522 const int kThumbThickness = 3;
523 const int kTrackStart = 0;
524 const bool kIsLeftSideVerticalScrollbar = false;
525 const bool kIsOverlayScrollbar = false;
527 horizontal_scrollbar_layer_ =
528 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
530 HORIZONTAL,
531 kThumbThickness,
532 kTrackStart,
533 kIsLeftSideVerticalScrollbar,
534 kIsOverlayScrollbar);
535 vertical_scrollbar_layer_ =
536 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
538 VERTICAL,
539 kThumbThickness,
540 kTrackStart,
541 kIsLeftSideVerticalScrollbar,
542 kIsOverlayScrollbar);
545 protected:
546 FakeImplProxy proxy_;
547 TestSharedBitmapManager shared_bitmap_manager_;
548 scoped_ptr<FakeLayerTreeHostImpl> host_impl_;
549 scoped_ptr<SolidColorScrollbarLayerImpl> horizontal_scrollbar_layer_;
550 scoped_ptr<SolidColorScrollbarLayerImpl> vertical_scrollbar_layer_;
553 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbLength) {
554 horizontal_scrollbar_layer_->SetCurrentPos(0);
555 horizontal_scrollbar_layer_->SetMaximum(10);
557 // Simple case - one third of the scrollable area is visible, so the thumb
558 // should be one third as long as the track.
559 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.33f);
560 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
561 EXPECT_EQ(33, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
563 // The thumb's length should never be less than its thickness.
564 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.01f);
565 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
566 EXPECT_EQ(3, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
569 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbPosition) {
570 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
571 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.1f);
573 horizontal_scrollbar_layer_->SetCurrentPos(0);
574 horizontal_scrollbar_layer_->SetMaximum(100);
575 EXPECT_EQ(0, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
576 EXPECT_EQ(10, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
578 horizontal_scrollbar_layer_->SetCurrentPos(100);
579 // The thumb is 10px long and the track is 100px, so the maximum thumb
580 // position is 90px.
581 EXPECT_EQ(90, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
583 horizontal_scrollbar_layer_->SetCurrentPos(80);
584 // The scroll position is 80% of the maximum, so the thumb's position should
585 // be at 80% of its maximum or 72px.
586 EXPECT_EQ(72, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
589 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbVerticalAdjust) {
590 SolidColorScrollbarLayerImpl* layers[2] =
591 { horizontal_scrollbar_layer_.get(), vertical_scrollbar_layer_.get() };
592 for (size_t i = 0; i < 2; ++i) {
593 layers[i]->SetVisibleToTotalLengthRatio(0.2f);
594 layers[i]->SetCurrentPos(25);
595 layers[i]->SetMaximum(100);
597 layers[0]->SetBounds(gfx::Size(100, 3));
598 layers[1]->SetBounds(gfx::Size(3, 100));
600 EXPECT_EQ(gfx::RectF(20.f, 0.f, 20.f, 3.f),
601 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
602 EXPECT_EQ(gfx::RectF(0.f, 20.f, 3.f, 20.f),
603 vertical_scrollbar_layer_->ComputeThumbQuadRect());
605 horizontal_scrollbar_layer_->SetVerticalAdjust(10.f);
606 vertical_scrollbar_layer_->SetVerticalAdjust(10.f);
608 // The vertical adjustment factor has two effects:
609 // 1.) Moves the horizontal scrollbar down
610 // 2.) Increases the vertical scrollbar's effective track length which both
611 // increases the thumb's length and its position within the track.
612 EXPECT_EQ(gfx::Rect(20.f, 10.f, 20.f, 3.f),
613 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
614 EXPECT_EQ(gfx::Rect(0.f, 22, 3.f, 22.f),
615 vertical_scrollbar_layer_->ComputeThumbQuadRect());
618 class ScrollbarLayerTestMaxTextureSize : public LayerTreeTest {
619 public:
620 ScrollbarLayerTestMaxTextureSize() {}
622 void SetScrollbarBounds(const gfx::Size& bounds) { bounds_ = bounds; }
624 void BeginTest() override {
625 scroll_layer_ = Layer::Create();
626 layer_tree_host()->root_layer()->AddChild(scroll_layer_);
628 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
629 scrollbar_layer_ =
630 PaintedScrollbarLayer::Create(scrollbar.Pass(), scroll_layer_->id());
631 scrollbar_layer_->SetScrollLayer(scroll_layer_->id());
632 scrollbar_layer_->SetLayerTreeHost(layer_tree_host());
633 scrollbar_layer_->SetBounds(bounds_);
634 scrollbar_layer_->SetIsDrawable(true);
635 layer_tree_host()->root_layer()->AddChild(scrollbar_layer_);
637 PostSetNeedsCommitToMainThread();
640 void DidCommitAndDrawFrame() override {
641 const int kMaxTextureSize =
642 layer_tree_host()->GetRendererCapabilities().max_texture_size;
644 // Check first that we're actually testing something.
645 EXPECT_GT(scrollbar_layer_->bounds().width(), kMaxTextureSize);
647 EXPECT_EQ(scrollbar_layer_->internal_content_bounds().width(),
648 kMaxTextureSize - 1);
649 EXPECT_EQ(scrollbar_layer_->internal_content_bounds().height(),
650 kMaxTextureSize - 1);
652 EndTest();
655 void AfterTest() override {}
657 private:
658 scoped_refptr<PaintedScrollbarLayer> scrollbar_layer_;
659 scoped_refptr<Layer> scroll_layer_;
660 gfx::Size bounds_;
663 TEST_F(ScrollbarLayerTestMaxTextureSize, DirectRenderer) {
664 scoped_ptr<TestWebGraphicsContext3D> context =
665 TestWebGraphicsContext3D::Create();
666 int max_size = 0;
667 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
668 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
669 RunTest(true, false, true);
672 TEST_F(ScrollbarLayerTestMaxTextureSize, DelegatingRenderer) {
673 scoped_ptr<TestWebGraphicsContext3D> context =
674 TestWebGraphicsContext3D::Create();
675 int max_size = 0;
676 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
677 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
678 RunTest(true, true, true);
681 class ScrollbarLayerTestResourceCreationAndRelease : public ScrollbarLayerTest {
682 public:
683 void TestResourceUpload(int num_updates,
684 size_t expected_resources,
685 int expected_created,
686 int expected_deleted,
687 bool use_solid_color_scrollbar) {
688 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, false));
689 scoped_refptr<Layer> layer_tree_root = Layer::Create();
690 scoped_refptr<Layer> content_layer = Layer::Create();
691 scoped_refptr<Layer> scrollbar_layer;
692 if (use_solid_color_scrollbar) {
693 const int kThumbThickness = 3;
694 const int kTrackStart = 0;
695 const bool kIsLeftSideVerticalScrollbar = false;
696 scrollbar_layer =
697 SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
698 kThumbThickness,
699 kTrackStart,
700 kIsLeftSideVerticalScrollbar,
701 layer_tree_root->id());
702 } else {
703 scrollbar_layer = PaintedScrollbarLayer::Create(scrollbar.Pass(),
704 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().content_bounds = gfx::Size(100, 200);
717 scrollbar_layer->draw_properties().visible_content_rect =
718 gfx::Rect(0, 0, 100, 200);
719 scrollbar_layer->CreateRenderSurface();
720 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
722 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
723 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
725 ResourceUpdateQueue queue;
726 gfx::Rect screen_space_clip_rect;
727 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
729 scrollbar_layer->SavePaintProperties();
730 for (int update_counter = 0; update_counter < num_updates; update_counter++)
731 scrollbar_layer->Update(&queue, &occlusion_tracker);
733 // A non-solid-color scrollbar should have requested two textures.
734 EXPECT_EQ(expected_resources, layer_tree_host_->UIResourceCount());
735 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
736 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
738 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
740 scrollbar_layer->ClearRenderSurface();
744 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, ResourceUpload) {
745 bool use_solid_color_scrollbars = false;
746 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
747 int num_updates[3] = {1, 5, 10};
748 int created = 0;
749 int deleted = 0;
750 for (int j = 0; j < 3; j++) {
751 created += num_updates[j] * 2;
752 deleted = created - 2;
753 TestResourceUpload(num_updates[j], 2, created, deleted,
754 use_solid_color_scrollbars);
758 TEST_F(ScrollbarLayerTestResourceCreationAndRelease,
759 SolidColorNoResourceUpload) {
760 bool use_solid_color_scrollbars = true;
761 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
762 TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars);
765 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, TestResourceUpdate) {
766 gfx::Point scrollbar_location(0, 185);
767 scoped_refptr<Layer> layer_tree_root = Layer::Create();
768 scoped_refptr<Layer> content_layer = Layer::Create();
769 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
770 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root->id());
772 layer_tree_root->AddChild(content_layer);
773 layer_tree_root->AddChild(scrollbar_layer);
775 layer_tree_host_->SetRootLayer(layer_tree_root);
777 scrollbar_layer->SetIsDrawable(true);
778 scrollbar_layer->SetBounds(gfx::Size(100, 15));
779 scrollbar_layer->SetPosition(scrollbar_location);
780 layer_tree_root->SetBounds(gfx::Size(100, 200));
781 content_layer->SetBounds(gfx::Size(100, 200));
783 scrollbar_layer->draw_properties().content_bounds = gfx::Size(100, 200);
784 scrollbar_layer->draw_properties().visible_content_rect =
785 gfx::Rect(0, 0, 100, 200);
787 scrollbar_layer->CreateRenderSurface();
788 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
790 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
791 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
793 ResourceUpdateQueue queue;
794 gfx::Rect screen_space_clip_rect;
795 size_t resource_count;
796 int expected_created, expected_deleted;
797 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
798 scrollbar_layer->SavePaintProperties();
800 resource_count = 2;
801 expected_created = 2;
802 expected_deleted = 0;
803 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
804 EXPECT_NE(0, scrollbar_layer->track_resource_id());
805 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
806 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
807 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
808 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
810 resource_count = 0;
811 expected_created = 2;
812 expected_deleted = 2;
813 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
814 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
815 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
816 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
817 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
818 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
819 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
821 resource_count = 0;
822 expected_created = 2;
823 expected_deleted = 2;
824 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
825 EXPECT_FALSE(scrollbar_layer->Update(&queue, &occlusion_tracker));
826 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
827 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
828 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
829 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
830 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
832 resource_count = 2;
833 expected_created = 4;
834 expected_deleted = 2;
835 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
836 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
837 EXPECT_NE(0, scrollbar_layer->track_resource_id());
838 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
839 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
840 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
841 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
843 resource_count = 1;
844 expected_created = 5;
845 expected_deleted = 4;
846 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
847 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
848 EXPECT_NE(0, scrollbar_layer->track_resource_id());
849 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
850 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
851 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
852 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
854 resource_count = 0;
855 expected_created = 5;
856 expected_deleted = 5;
857 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
858 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
859 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
860 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
861 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
862 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
863 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
865 resource_count = 2;
866 expected_created = 7;
867 expected_deleted = 5;
868 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
869 scrollbar_layer->fake_scrollbar()->set_has_thumb(true);
870 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
871 EXPECT_NE(0, scrollbar_layer->track_resource_id());
872 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
874 resource_count = 1;
875 expected_created = 8;
876 expected_deleted = 7;
877 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
878 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
879 scrollbar_layer->SetBounds(gfx::Size(90, 15));
880 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
881 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
882 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
883 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
884 EXPECT_EQ(
885 gfx::Size(90, 15),
886 layer_tree_host_->ui_resource_size(scrollbar_layer->track_resource_id()));
888 scrollbar_layer->ResetNeedsDisplayForTesting();
889 EXPECT_FALSE(scrollbar_layer->Update(&queue, &occlusion_tracker));
890 EXPECT_NE(0, scrollbar_layer->track_resource_id());
891 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
892 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
893 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
894 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
896 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
897 scrollbar_layer->ClearRenderSurface();
900 class ScaledScrollbarLayerTestResourceCreation : public ScrollbarLayerTest {
901 public:
902 void TestResourceUpload(const float test_scale) {
903 gfx::Point scrollbar_location(0, 185);
904 scoped_refptr<Layer> layer_tree_root = Layer::Create();
905 scoped_refptr<Layer> content_layer = Layer::Create();
906 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
907 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root->id());
909 layer_tree_root->AddChild(content_layer);
910 layer_tree_root->AddChild(scrollbar_layer);
912 layer_tree_host_->SetRootLayer(layer_tree_root);
914 scrollbar_layer->SetIsDrawable(true);
915 scrollbar_layer->SetBounds(gfx::Size(100, 15));
916 scrollbar_layer->SetPosition(scrollbar_location);
917 layer_tree_root->SetBounds(gfx::Size(100, 200));
918 content_layer->SetBounds(gfx::Size(100, 200));
919 gfx::SizeF scaled_size =
920 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
921 gfx::PointF scaled_location =
922 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
923 scrollbar_layer->draw_properties().content_bounds =
924 gfx::Size(scaled_size.width(), scaled_size.height());
925 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
926 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
927 scrollbar_layer->draw_properties().visible_content_rect =
928 gfx::Rect(scaled_location.x(),
929 scaled_location.y(),
930 scaled_size.width(),
931 scaled_size.height());
932 scrollbar_layer->CreateRenderSurface();
933 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
935 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
936 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
938 ResourceUpdateQueue queue;
939 gfx::Rect screen_space_clip_rect;
940 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
941 scrollbar_layer->SavePaintProperties();
942 scrollbar_layer->Update(&queue, &occlusion_tracker);
944 // Verify that we have not generated any content uploads that are larger
945 // than their destination textures.
947 gfx::Size track_size = layer_tree_host_->ui_resource_size(
948 scrollbar_layer->track_resource_id());
949 gfx::Size thumb_size = layer_tree_host_->ui_resource_size(
950 scrollbar_layer->thumb_resource_id());
952 EXPECT_LE(track_size.width(),
953 scrollbar_layer->internal_content_bounds().width());
954 EXPECT_LE(track_size.height(),
955 scrollbar_layer->internal_content_bounds().height());
956 EXPECT_LE(thumb_size.width(),
957 scrollbar_layer->internal_content_bounds().width());
958 EXPECT_LE(thumb_size.height(),
959 scrollbar_layer->internal_content_bounds().height());
961 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
963 scrollbar_layer->ClearRenderSurface();
967 TEST_F(ScaledScrollbarLayerTestResourceCreation, ScaledResourceUpload) {
968 // Pick a test scale that moves the scrollbar's (non-zero) position to
969 // a non-pixel-aligned location.
970 TestResourceUpload(.041f);
971 TestResourceUpload(1.41f);
972 TestResourceUpload(4.1f);
975 class ScaledScrollbarLayerTestScaledRasterization : public ScrollbarLayerTest {
976 public:
977 void TestScale(const gfx::Rect scrollbar_rect, const float test_scale) {
978 bool paint_during_update = true;
979 bool has_thumb = false;
980 scoped_refptr<Layer> layer_tree_root = Layer::Create();
981 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
982 FakePaintedScrollbarLayer::Create(paint_during_update,
983 has_thumb,
984 layer_tree_root->id());
986 layer_tree_root->AddChild(scrollbar_layer);
988 layer_tree_host_->SetRootLayer(layer_tree_root);
990 scrollbar_layer->SetBounds(scrollbar_rect.size());
991 scrollbar_layer->SetPosition(scrollbar_rect.origin());
992 scrollbar_layer->fake_scrollbar()->set_location(scrollbar_rect.origin());
993 scrollbar_layer->fake_scrollbar()->set_track_rect(scrollbar_rect);
994 gfx::SizeF scaled_size =
995 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
996 gfx::PointF scaled_location =
997 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
998 scrollbar_layer->draw_properties().content_bounds =
999 gfx::Size(scaled_size.width(), scaled_size.height());
1000 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
1001 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
1002 scrollbar_layer->draw_properties().visible_content_rect =
1003 gfx::Rect(scaled_location.x(),
1004 scaled_location.y(),
1005 scaled_size.width(),
1006 scaled_size.height());
1008 ResourceUpdateQueue queue;
1009 gfx::Rect screen_space_clip_rect;
1010 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
1011 scrollbar_layer->SavePaintProperties();
1013 scrollbar_layer->Update(&queue, &occlusion_tracker);
1015 UIResourceBitmap* bitmap = layer_tree_host_->ui_resource_bitmap(
1016 scrollbar_layer->track_resource_id());
1018 DCHECK(bitmap);
1020 AutoLockUIResourceBitmap locked_bitmap(*bitmap);
1022 const SkColor* pixels =
1023 reinterpret_cast<const SkColor*>(locked_bitmap.GetPixels());
1024 SkColor color = argb_to_skia(
1025 scrollbar_layer->fake_scrollbar()->paint_fill_color());
1026 int width = bitmap->GetSize().width();
1027 int height = bitmap->GetSize().height();
1029 // Make sure none of the corners of the bitmap were inadvertently clipped.
1030 EXPECT_EQ(color, pixels[0])
1031 << "Top left pixel doesn't match scrollbar color.";
1033 EXPECT_EQ(color, pixels[width - 1])
1034 << "Top right pixel doesn't match scrollbar color.";
1036 EXPECT_EQ(color, pixels[width * (height - 1)])
1037 << "Bottom left pixel doesn't match scrollbar color.";
1039 EXPECT_EQ(color, pixels[width * height - 1])
1040 << "Bottom right pixel doesn't match scrollbar color.";
1043 protected:
1044 // On Android, Skia uses ABGR
1045 static SkColor argb_to_skia(SkColor c) {
1046 return (SkColorGetA(c) << SK_A32_SHIFT) |
1047 (SkColorGetR(c) << SK_R32_SHIFT) |
1048 (SkColorGetG(c) << SK_G32_SHIFT) |
1049 (SkColorGetB(c) << SK_B32_SHIFT);
1053 TEST_F(ScaledScrollbarLayerTestScaledRasterization, TestLostPrecisionInClip) {
1054 // Try rasterization at coordinates and scale that caused problematic
1055 // rounding and clipping errors.
1056 // Vertical Scrollbars.
1057 TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f);
1058 TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f);
1060 // Horizontal Scrollbars.
1061 TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f);
1062 TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f);
1065 } // namespace
1066 } // namespace cc