[GCM] Investigatory CHECKs for crash in parsing stream
[chromium-blink-merge.git] / cc / layers / scrollbar_layer_unittest.cc
blob7cb0e25118c4a04f26d9c40af92768be445789b8
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 TEST(ScrollbarLayerTest, ResolveScrollLayerPointer) {
63 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
64 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
65 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
66 host.get(), scrollbar.Pass(), false, false, 0, 0);
68 LayerImpl* cc_child1 = layer_impl_tree_root->children()[0];
69 PaintedScrollbarLayerImpl* cc_child2 =
70 static_cast<PaintedScrollbarLayerImpl*>(
71 layer_impl_tree_root->children()[1]);
73 EXPECT_EQ(cc_child1->scrollbars()->size(), 1UL);
74 EXPECT_EQ(*(cc_child1->scrollbars()->begin()), cc_child2);
77 TEST(ScrollbarLayerTest, ResolveScrollLayerPointer_ReverseOrder) {
78 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
79 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
80 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
81 host.get(), scrollbar.Pass(), true, false, 0, 0);
83 PaintedScrollbarLayerImpl* cc_child1 =
84 static_cast<PaintedScrollbarLayerImpl*>(
85 layer_impl_tree_root->children()[0]);
86 LayerImpl* cc_child2 = layer_impl_tree_root->children()[1];
88 EXPECT_EQ(cc_child2->scrollbars()->size(), 1UL);
89 EXPECT_EQ(*(cc_child2->scrollbars()->begin()), cc_child1);
92 TEST(ScrollbarLayerTest, ShouldScrollNonOverlayOnMainThread) {
93 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
95 // Create and attach a non-overlay scrollbar.
96 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
97 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
98 host.get(), scrollbar.Pass(), false, false, 0, 0);
99 PaintedScrollbarLayerImpl* scrollbar_layer_impl =
100 static_cast<PaintedScrollbarLayerImpl*>(
101 layer_impl_tree_root->children()[1]);
103 // When the scrollbar is not an overlay scrollbar, the scroll should be
104 // responded to on the main thread as the compositor does not yet implement
105 // scrollbar scrolling.
106 EXPECT_EQ(InputHandler::ScrollOnMainThread,
107 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
108 InputHandler::Gesture));
110 // Create and attach an overlay scrollbar.
111 scrollbar.reset(new FakeScrollbar(false, false, true));
113 layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
114 host.get(), scrollbar.Pass(), false, false, 0, 0);
115 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>(
116 layer_impl_tree_root->children()[1]);
118 // The user shouldn't be able to drag an overlay scrollbar and the scroll
119 // may be handled in the compositor.
120 EXPECT_EQ(InputHandler::ScrollIgnored,
121 scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
122 InputHandler::Gesture));
125 TEST(PaintedScrollbarLayerTest, ScrollOffsetSynchronization) {
126 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
128 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
129 scoped_refptr<Layer> layer_tree_root = Layer::Create();
130 scoped_refptr<Layer> scroll_layer = Layer::Create();
131 scoped_refptr<Layer> content_layer = Layer::Create();
132 scoped_refptr<Layer> scrollbar_layer =
133 PaintedScrollbarLayer::Create(scrollbar.Pass(), layer_tree_root->id());
135 // Choose bounds to give max_scroll_offset = (30, 50).
136 layer_tree_root->SetBounds(gfx::Size(70, 150));
137 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
138 scroll_layer->SetScrollOffset(gfx::Vector2d(10, 20));
139 scroll_layer->SetBounds(gfx::Size(100, 200));
140 content_layer->SetBounds(gfx::Size(100, 200));
142 host->SetRootLayer(layer_tree_root);
143 layer_tree_root->AddChild(scroll_layer);
144 scroll_layer->AddChild(content_layer);
145 layer_tree_root->AddChild(scrollbar_layer);
146 scrollbar_layer->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
147 scrollbar_layer->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
149 layer_tree_root->SavePaintProperties();
150 content_layer->SavePaintProperties();
152 LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
154 ScrollbarLayerImplBase* cc_scrollbar_layer =
155 static_cast<PaintedScrollbarLayerImpl*>(
156 layer_impl_tree_root->children()[1]);
158 EXPECT_EQ(10.f, cc_scrollbar_layer->current_pos());
159 EXPECT_EQ(30, cc_scrollbar_layer->maximum());
161 layer_tree_root->SetBounds(gfx::Size(700, 1500));
162 layer_tree_root->SavePaintProperties();
163 scroll_layer->SetBounds(gfx::Size(1000, 2000));
164 scroll_layer->SetScrollOffset(gfx::Vector2d(100, 200));
165 scroll_layer->SavePaintProperties();
166 content_layer->SetBounds(gfx::Size(1000, 2000));
167 content_layer->SavePaintProperties();
169 ScrollbarAnimationController* scrollbar_controller =
170 layer_impl_tree_root->scrollbar_animation_controller();
171 layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
172 EXPECT_EQ(scrollbar_controller,
173 layer_impl_tree_root->scrollbar_animation_controller());
175 EXPECT_EQ(100.f, cc_scrollbar_layer->current_pos());
176 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
178 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
179 scroll_layer_impl->ScrollBy(gfx::Vector2d(12, 34));
181 EXPECT_EQ(112.f, cc_scrollbar_layer->current_pos());
182 EXPECT_EQ(300, cc_scrollbar_layer->maximum());
185 #define UPDATE_AND_EXTRACT_LAYER_POINTERS() \
186 do { \
187 scrollbar_layer->UpdateThumbAndTrackGeometry(); \
188 root_clip_layer_impl = host->CommitAndCreateLayerImplTree(); \
189 root_layer_impl = root_clip_layer_impl->children()[0]; \
190 scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>( \
191 root_layer_impl->children()[1]); \
192 scrollbar_layer_impl->ScrollbarParametersDidChange(); \
193 } while (false)
195 TEST(ScrollbarLayerTest, UpdatePropertiesOfScrollBarWhenThumbRemoved) {
196 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
197 scoped_refptr<Layer> root_clip_layer = Layer::Create();
198 scoped_refptr<Layer> root_layer = Layer::Create();
199 scoped_refptr<Layer> content_layer = Layer::Create();
200 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
201 FakePaintedScrollbarLayer::Create(false, true, root_layer->id());
203 root_layer->SetScrollClipLayerId(root_clip_layer->id());
204 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
205 root_clip_layer->SetBounds(gfx::Size(20, 50));
206 root_layer->SetBounds(gfx::Size(100, 50));
207 content_layer->SetBounds(gfx::Size(100, 50));
209 host->SetRootLayer(root_clip_layer);
210 root_clip_layer->AddChild(root_layer);
211 root_layer->AddChild(content_layer);
212 root_layer->AddChild(scrollbar_layer);
214 root_layer->SetScrollOffset(gfx::Vector2d(0, 0));
215 scrollbar_layer->SetBounds(gfx::Size(70, 10));
216 scrollbar_layer->SetScrollLayer(root_layer->id());
217 scrollbar_layer->SetClipLayer(root_clip_layer->id());
218 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
219 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
220 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
221 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
223 scrollbar_layer->UpdateThumbAndTrackGeometry();
224 LayerImpl* root_clip_layer_impl = NULL;
225 LayerImpl* root_layer_impl = NULL;
226 PaintedScrollbarLayerImpl* scrollbar_layer_impl = NULL;
228 UPDATE_AND_EXTRACT_LAYER_POINTERS();
229 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
230 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
232 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
234 UPDATE_AND_EXTRACT_LAYER_POINTERS();
235 EXPECT_EQ(gfx::Rect(10, 0, 0, 0).ToString(),
236 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
239 TEST(ScrollbarLayerTest, ThumbRect) {
240 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
241 scoped_refptr<Layer> root_clip_layer = Layer::Create();
242 scoped_refptr<Layer> root_layer = Layer::Create();
243 scoped_refptr<Layer> content_layer = Layer::Create();
244 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
245 FakePaintedScrollbarLayer::Create(false, true, root_layer->id());
247 root_layer->SetScrollClipLayerId(root_clip_layer->id());
248 // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
249 root_clip_layer->SetBounds(gfx::Size(20, 50));
250 root_layer->SetBounds(gfx::Size(100, 50));
251 content_layer->SetBounds(gfx::Size(100, 50));
253 host->SetRootLayer(root_clip_layer);
254 root_clip_layer->AddChild(root_layer);
255 root_layer->AddChild(content_layer);
256 root_layer->AddChild(scrollbar_layer);
258 root_layer->SetScrollOffset(gfx::Vector2d(0, 0));
259 scrollbar_layer->SetBounds(gfx::Size(70, 10));
260 scrollbar_layer->SetScrollLayer(root_layer->id());
261 scrollbar_layer->SetClipLayer(root_clip_layer->id());
262 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
263 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
264 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
265 scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
266 scrollbar_layer->UpdateThumbAndTrackGeometry();
267 LayerImpl* root_clip_layer_impl = NULL;
268 LayerImpl* root_layer_impl = NULL;
269 PaintedScrollbarLayerImpl* scrollbar_layer_impl = NULL;
271 // Thumb is at the edge of the scrollbar (should be inset to
272 // the start of the track within the scrollbar layer's
273 // position).
274 UPDATE_AND_EXTRACT_LAYER_POINTERS();
275 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
276 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
278 // Under-scroll (thumb position should clamp and be unchanged).
279 root_layer->SetScrollOffset(gfx::Vector2d(-5, 0));
281 UPDATE_AND_EXTRACT_LAYER_POINTERS();
282 EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
283 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
285 // Over-scroll (thumb position should clamp on the far side).
286 root_layer->SetScrollOffset(gfx::Vector2d(85, 0));
288 UPDATE_AND_EXTRACT_LAYER_POINTERS();
289 EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(),
290 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
292 // Change thumb thickness and length.
293 scrollbar_layer->fake_scrollbar()->set_thumb_thickness(4);
294 scrollbar_layer->fake_scrollbar()->set_thumb_length(6);
296 UPDATE_AND_EXTRACT_LAYER_POINTERS();
297 EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(),
298 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
300 // Shrink the scrollbar layer to cover only the track.
301 scrollbar_layer->SetBounds(gfx::Size(50, 10));
302 scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(30, 10));
303 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
305 UPDATE_AND_EXTRACT_LAYER_POINTERS();
306 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
307 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
309 // Shrink the track in the non-scrolling dimension so that it only covers the
310 // middle third of the scrollbar layer (this does not affect the thumb
311 // position).
312 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6));
314 UPDATE_AND_EXTRACT_LAYER_POINTERS();
315 EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
316 scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
319 TEST(ScrollbarLayerTest, SolidColorDrawQuads) {
320 const int kThumbThickness = 3;
321 const int kTrackStart = 1;
322 const int kTrackLength = 100;
324 LayerTreeSettings layer_tree_settings;
325 scoped_ptr<FakeLayerTreeHost> host =
326 FakeLayerTreeHost::Create(layer_tree_settings);
328 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
329 LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
330 host.get(), scrollbar.Pass(), false, true, kThumbThickness, kTrackStart);
331 ScrollbarLayerImplBase* scrollbar_layer_impl =
332 static_cast<SolidColorScrollbarLayerImpl*>(
333 layer_impl_tree_root->children()[1]);
334 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
335 scrollbar_layer_impl->SetCurrentPos(10.f);
336 scrollbar_layer_impl->SetMaximum(100);
337 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.4f);
339 // Thickness should be overridden to 3.
341 MockOcclusionTracker<LayerImpl> occlusion_tracker;
342 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
343 AppendQuadsData data;
344 scrollbar_layer_impl->AppendQuads(
345 render_pass.get(), occlusion_tracker, &data);
347 const QuadList& quads = render_pass->quad_list;
348 ASSERT_EQ(1u, quads.size());
349 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
350 EXPECT_RECT_EQ(gfx::Rect(6, 0, 39, 3), quads[0]->rect);
353 // Contents scale should scale the draw quad.
354 scrollbar_layer_impl->draw_properties().contents_scale_x = 2.f;
355 scrollbar_layer_impl->draw_properties().contents_scale_y = 2.f;
357 MockOcclusionTracker<LayerImpl> occlusion_tracker;
358 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
359 AppendQuadsData data;
360 scrollbar_layer_impl->AppendQuads(
361 render_pass.get(), occlusion_tracker, &data);
363 const QuadList& quads = render_pass->quad_list;
364 ASSERT_EQ(1u, quads.size());
365 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
366 EXPECT_RECT_EQ(gfx::Rect(12, 0, 78, 6), quads[0]->rect);
368 scrollbar_layer_impl->draw_properties().contents_scale_x = 1.f;
369 scrollbar_layer_impl->draw_properties().contents_scale_y = 1.f;
371 // For solid color scrollbars, position and size should reflect the
372 // current viewport state.
373 scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.2f);
375 MockOcclusionTracker<LayerImpl> occlusion_tracker;
376 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
377 AppendQuadsData data;
378 scrollbar_layer_impl->AppendQuads(
379 render_pass.get(), occlusion_tracker, &data);
381 const QuadList& quads = render_pass->quad_list;
382 ASSERT_EQ(1u, quads.size());
383 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
384 EXPECT_RECT_EQ(gfx::Rect(8, 0, 19, 3), quads[0]->rect);
387 // We shouldn't attempt div-by-zero when the maximum is zero.
388 scrollbar_layer_impl->SetCurrentPos(0.f);
389 scrollbar_layer_impl->SetMaximum(0);
391 MockOcclusionTracker<LayerImpl> occlusion_tracker;
392 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
393 AppendQuadsData data;
394 scrollbar_layer_impl->AppendQuads(
395 render_pass.get(), occlusion_tracker, &data);
397 const QuadList& quads = render_pass->quad_list;
398 ASSERT_EQ(1u, quads.size());
399 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
400 EXPECT_RECT_EQ(gfx::Rect(1, 0, 19, 3), quads[0]->rect);
404 TEST(ScrollbarLayerTest, LayerDrivenSolidColorDrawQuads) {
405 const int kThumbThickness = 3;
406 const int kTrackStart = 0;
407 const int kTrackLength = 10;
409 LayerTreeSettings layer_tree_settings;
410 scoped_ptr<FakeLayerTreeHost> host =
411 FakeLayerTreeHost::Create(layer_tree_settings);
413 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
416 scoped_refptr<Layer> layer_tree_root = Layer::Create();
417 scoped_refptr<Layer> scroll_layer = Layer::Create();
418 scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
419 scoped_refptr<Layer> child1 = Layer::Create();
420 scoped_refptr<Layer> child2;
421 const bool kIsLeftSideVerticalScrollbar = false;
422 child2 = SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
423 kThumbThickness,
424 kTrackStart,
425 kIsLeftSideVerticalScrollbar,
426 child1->id());
427 child2->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
428 child2->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
429 scroll_layer->AddChild(child1);
430 scroll_layer->InsertChild(child2, 1);
431 layer_tree_root->AddChild(scroll_layer);
432 host->SetRootLayer(layer_tree_root);
434 LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
435 LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
437 ScrollbarLayerImplBase* scrollbar_layer_impl =
438 static_cast<PaintedScrollbarLayerImpl*>(scroll_layer_impl->children()[1]);
440 // Choose layer bounds to give max_scroll_offset = (8, 8).
441 layer_impl_tree_root->SetBounds(gfx::Size(2, 2));
442 scroll_layer_impl->SetBounds(gfx::Size(10, 10));
443 scroll_layer_impl->ScrollBy(gfx::Vector2dF(4.f, 0.f));
445 scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
446 scrollbar_layer_impl->SetCurrentPos(4.f);
447 scrollbar_layer_impl->SetMaximum(8);
450 MockOcclusionTracker<LayerImpl> occlusion_tracker;
451 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
453 AppendQuadsData data;
454 scrollbar_layer_impl->AppendQuads(
455 render_pass.get(), occlusion_tracker, &data);
457 const QuadList& quads = render_pass->quad_list;
458 ASSERT_EQ(1u, quads.size());
459 EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
460 EXPECT_RECT_EQ(gfx::Rect(3, 0, 3, 3), quads[0]->rect);
464 class ScrollbarLayerSolidColorThumbTest : public testing::Test {
465 public:
466 ScrollbarLayerSolidColorThumbTest() {
467 LayerTreeSettings layer_tree_settings;
468 host_impl_.reset(new FakeLayerTreeHostImpl(
469 layer_tree_settings, &proxy_, &shared_bitmap_manager_));
471 const int kThumbThickness = 3;
472 const int kTrackStart = 0;
473 const bool kIsLeftSideVerticalScrollbar = false;
474 const bool kIsOverlayScrollbar = false;
476 horizontal_scrollbar_layer_ =
477 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
479 HORIZONTAL,
480 kThumbThickness,
481 kTrackStart,
482 kIsLeftSideVerticalScrollbar,
483 kIsOverlayScrollbar);
484 vertical_scrollbar_layer_ =
485 SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
487 VERTICAL,
488 kThumbThickness,
489 kTrackStart,
490 kIsLeftSideVerticalScrollbar,
491 kIsOverlayScrollbar);
494 protected:
495 FakeImplProxy proxy_;
496 TestSharedBitmapManager shared_bitmap_manager_;
497 scoped_ptr<FakeLayerTreeHostImpl> host_impl_;
498 scoped_ptr<SolidColorScrollbarLayerImpl> horizontal_scrollbar_layer_;
499 scoped_ptr<SolidColorScrollbarLayerImpl> vertical_scrollbar_layer_;
502 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbLength) {
503 horizontal_scrollbar_layer_->SetCurrentPos(0);
504 horizontal_scrollbar_layer_->SetMaximum(10);
506 // Simple case - one third of the scrollable area is visible, so the thumb
507 // should be one third as long as the track.
508 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.33f);
509 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
510 EXPECT_EQ(33, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
512 // The thumb's length should never be less than its thickness.
513 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.01f);
514 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
515 EXPECT_EQ(3, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
518 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbPosition) {
519 horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
520 horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.1f);
522 horizontal_scrollbar_layer_->SetCurrentPos(0);
523 horizontal_scrollbar_layer_->SetMaximum(100);
524 EXPECT_EQ(0, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
525 EXPECT_EQ(10, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
527 horizontal_scrollbar_layer_->SetCurrentPos(100);
528 // The thumb is 10px long and the track is 100px, so the maximum thumb
529 // position is 90px.
530 EXPECT_EQ(90, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
532 horizontal_scrollbar_layer_->SetCurrentPos(80);
533 // The scroll position is 80% of the maximum, so the thumb's position should
534 // be at 80% of its maximum or 72px.
535 EXPECT_EQ(72, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
538 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbVerticalAdjust) {
539 SolidColorScrollbarLayerImpl* layers[2] =
540 { horizontal_scrollbar_layer_.get(), vertical_scrollbar_layer_.get() };
541 for (size_t i = 0; i < 2; ++i) {
542 layers[i]->SetVisibleToTotalLengthRatio(0.2f);
543 layers[i]->SetCurrentPos(25);
544 layers[i]->SetMaximum(100);
546 layers[0]->SetBounds(gfx::Size(100, 3));
547 layers[1]->SetBounds(gfx::Size(3, 100));
549 EXPECT_RECT_EQ(gfx::RectF(20.f, 0.f, 20.f, 3.f),
550 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
551 EXPECT_RECT_EQ(gfx::RectF(0.f, 20.f, 3.f, 20.f),
552 vertical_scrollbar_layer_->ComputeThumbQuadRect());
554 horizontal_scrollbar_layer_->SetVerticalAdjust(10.f);
555 vertical_scrollbar_layer_->SetVerticalAdjust(10.f);
557 // The vertical adjustment factor has two effects:
558 // 1.) Moves the horizontal scrollbar down
559 // 2.) Increases the vertical scrollbar's effective track length which both
560 // increases the thumb's length and its position within the track.
561 EXPECT_RECT_EQ(gfx::Rect(20.f, 10.f, 20.f, 3.f),
562 horizontal_scrollbar_layer_->ComputeThumbQuadRect());
563 EXPECT_RECT_EQ(gfx::Rect(0.f, 22, 3.f, 22.f),
564 vertical_scrollbar_layer_->ComputeThumbQuadRect());
567 class ScrollbarLayerTestMaxTextureSize : public LayerTreeTest {
568 public:
569 ScrollbarLayerTestMaxTextureSize() {}
571 void SetScrollbarBounds(const gfx::Size& bounds) { bounds_ = bounds; }
573 virtual void BeginTest() OVERRIDE {
574 scroll_layer_ = Layer::Create();
575 layer_tree_host()->root_layer()->AddChild(scroll_layer_);
577 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
578 scrollbar_layer_ =
579 PaintedScrollbarLayer::Create(scrollbar.Pass(), scroll_layer_->id());
580 scrollbar_layer_->SetScrollLayer(scroll_layer_->id());
581 scrollbar_layer_->SetLayerTreeHost(layer_tree_host());
582 scrollbar_layer_->SetBounds(bounds_);
583 layer_tree_host()->root_layer()->AddChild(scrollbar_layer_);
585 PostSetNeedsCommitToMainThread();
588 virtual void DidCommitAndDrawFrame() OVERRIDE {
589 const int kMaxTextureSize =
590 layer_tree_host()->GetRendererCapabilities().max_texture_size;
592 // Check first that we're actually testing something.
593 EXPECT_GT(scrollbar_layer_->bounds().width(), kMaxTextureSize);
595 EXPECT_EQ(scrollbar_layer_->content_bounds().width(),
596 kMaxTextureSize - 1);
597 EXPECT_EQ(scrollbar_layer_->content_bounds().height(),
598 kMaxTextureSize - 1);
600 EndTest();
603 virtual void AfterTest() OVERRIDE {}
605 private:
606 scoped_refptr<PaintedScrollbarLayer> scrollbar_layer_;
607 scoped_refptr<Layer> scroll_layer_;
608 gfx::Size bounds_;
611 TEST_F(ScrollbarLayerTestMaxTextureSize, DirectRenderer) {
612 scoped_ptr<TestWebGraphicsContext3D> context =
613 TestWebGraphicsContext3D::Create();
614 int max_size = 0;
615 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
616 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
617 RunTest(true, false, true);
620 TEST_F(ScrollbarLayerTestMaxTextureSize, DelegatingRenderer) {
621 scoped_ptr<TestWebGraphicsContext3D> context =
622 TestWebGraphicsContext3D::Create();
623 int max_size = 0;
624 context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
625 SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
626 RunTest(true, true, true);
629 class FakeLayerTreeHost : public LayerTreeHost {
630 public:
631 FakeLayerTreeHost(FakeLayerTreeHostClient* client,
632 const LayerTreeSettings& settings)
633 : LayerTreeHost(client, NULL, settings),
634 next_id_(1),
635 total_ui_resource_created_(0),
636 total_ui_resource_deleted_(0) {
637 InitializeSingleThreaded(client, base::MessageLoopProxy::current());
640 virtual UIResourceId CreateUIResource(UIResourceClient* content) OVERRIDE {
641 total_ui_resource_created_++;
642 UIResourceId nid = next_id_++;
643 ui_resource_bitmap_map_.insert(
644 std::make_pair(nid, content->GetBitmap(nid, false)));
645 return nid;
648 // Deletes a UI resource. May safely be called more than once.
649 virtual void DeleteUIResource(UIResourceId id) OVERRIDE {
650 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
651 if (iter != ui_resource_bitmap_map_.end()) {
652 ui_resource_bitmap_map_.erase(iter);
653 total_ui_resource_deleted_++;
657 size_t UIResourceCount() { return ui_resource_bitmap_map_.size(); }
658 int TotalUIResourceDeleted() { return total_ui_resource_deleted_; }
659 int TotalUIResourceCreated() { return total_ui_resource_created_; }
661 gfx::Size ui_resource_size(UIResourceId id) {
662 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
663 if (iter != ui_resource_bitmap_map_.end())
664 return iter->second.GetSize();
665 return gfx::Size();
668 UIResourceBitmap* ui_resource_bitmap(UIResourceId id) {
669 UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
670 if (iter != ui_resource_bitmap_map_.end())
671 return &iter->second;
672 return NULL;
675 private:
676 typedef base::hash_map<UIResourceId, UIResourceBitmap>
677 UIResourceBitmapMap;
678 UIResourceBitmapMap ui_resource_bitmap_map_;
680 int next_id_;
681 int total_ui_resource_created_;
682 int total_ui_resource_deleted_;
685 class ScrollbarLayerTestResourceCreationAndRelease : public testing::Test {
686 public:
687 ScrollbarLayerTestResourceCreationAndRelease()
688 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
690 void TestResourceUpload(int num_updates,
691 size_t expected_resources,
692 int expected_created,
693 int expected_deleted,
694 bool use_solid_color_scrollbar) {
695 layer_tree_host_.reset(
696 new FakeLayerTreeHost(&fake_client_, layer_tree_settings_));
698 scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, false));
699 scoped_refptr<Layer> layer_tree_root = Layer::Create();
700 scoped_refptr<Layer> content_layer = Layer::Create();
701 scoped_refptr<Layer> scrollbar_layer;
702 if (use_solid_color_scrollbar) {
703 const int kThumbThickness = 3;
704 const int kTrackStart = 0;
705 const bool kIsLeftSideVerticalScrollbar = false;
706 scrollbar_layer =
707 SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
708 kThumbThickness,
709 kTrackStart,
710 kIsLeftSideVerticalScrollbar,
711 layer_tree_root->id());
712 } else {
713 scrollbar_layer = PaintedScrollbarLayer::Create(scrollbar.Pass(),
714 layer_tree_root->id());
716 layer_tree_root->AddChild(content_layer);
717 layer_tree_root->AddChild(scrollbar_layer);
719 layer_tree_host_->SetRootLayer(layer_tree_root);
721 scrollbar_layer->SetIsDrawable(true);
722 scrollbar_layer->SetBounds(gfx::Size(100, 100));
723 layer_tree_root->SetScrollOffset(gfx::Vector2d(10, 20));
724 layer_tree_root->SetBounds(gfx::Size(100, 200));
725 content_layer->SetBounds(gfx::Size(100, 200));
726 scrollbar_layer->draw_properties().content_bounds = gfx::Size(100, 200);
727 scrollbar_layer->draw_properties().visible_content_rect =
728 gfx::Rect(0, 0, 100, 200);
729 scrollbar_layer->CreateRenderSurface();
730 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
732 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
733 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
735 ResourceUpdateQueue queue;
736 gfx::Rect screen_space_clip_rect;
737 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
739 scrollbar_layer->SavePaintProperties();
740 for (int update_counter = 0; update_counter < num_updates; update_counter++)
741 scrollbar_layer->Update(&queue, &occlusion_tracker);
743 // A non-solid-color scrollbar should have requested two textures.
744 EXPECT_EQ(expected_resources, layer_tree_host_->UIResourceCount());
745 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
746 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
748 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
750 scrollbar_layer->ClearRenderSurface();
753 protected:
754 FakeLayerTreeHostClient fake_client_;
755 LayerTreeSettings layer_tree_settings_;
756 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
759 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, ResourceUpload) {
760 bool use_solid_color_scrollbars = false;
761 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
762 int num_updates[3] = {1, 5, 10};
763 for (int j = 0; j < 3; j++) {
764 TestResourceUpload(num_updates[j],
766 num_updates[j] * 2,
767 (num_updates[j] - 1) * 2,
768 use_solid_color_scrollbars);
772 TEST_F(ScrollbarLayerTestResourceCreationAndRelease,
773 SolidColorNoResourceUpload) {
774 bool use_solid_color_scrollbars = true;
775 TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
776 TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars);
779 TEST_F(ScrollbarLayerTestResourceCreationAndRelease, TestResourceUpdate) {
780 FakeLayerTreeHostClient fake_client_(FakeLayerTreeHostClient::DIRECT_3D);
781 LayerTreeSettings layer_tree_settings_;
782 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
784 layer_tree_host_.reset(
785 new FakeLayerTreeHost(&fake_client_, layer_tree_settings_));
787 gfx::Point scrollbar_location(0, 185);
788 scoped_refptr<Layer> layer_tree_root = Layer::Create();
789 scoped_refptr<Layer> content_layer = Layer::Create();
790 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
791 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root->id());
793 layer_tree_root->AddChild(content_layer);
794 layer_tree_root->AddChild(scrollbar_layer);
796 layer_tree_host_->SetRootLayer(layer_tree_root);
798 scrollbar_layer->SetIsDrawable(true);
799 scrollbar_layer->SetBounds(gfx::Size(100, 15));
800 scrollbar_layer->SetPosition(scrollbar_location);
801 layer_tree_root->SetBounds(gfx::Size(100, 200));
802 content_layer->SetBounds(gfx::Size(100, 200));
804 scrollbar_layer->draw_properties().content_bounds = gfx::Size(100, 200);
805 scrollbar_layer->draw_properties().visible_content_rect =
806 gfx::Rect(0, 0, 100, 200);
808 scrollbar_layer->CreateRenderSurface();
809 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
811 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
812 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
814 ResourceUpdateQueue queue;
815 gfx::Rect screen_space_clip_rect;
816 size_t resource_count;
817 int expected_created, expected_deleted;
818 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
819 scrollbar_layer->SavePaintProperties();
821 resource_count = 2;
822 expected_created = 2;
823 expected_deleted = 0;
824 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
825 EXPECT_NE(0, scrollbar_layer->track_resource_id());
826 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
827 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
828 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
829 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
831 resource_count = 0;
832 expected_created = 2;
833 expected_deleted = 2;
834 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
835 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
836 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
837 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
838 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
839 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
840 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
842 resource_count = 0;
843 expected_created = 2;
844 expected_deleted = 2;
845 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
846 EXPECT_FALSE(scrollbar_layer->Update(&queue, &occlusion_tracker));
847 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
848 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
849 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
850 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
851 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
853 resource_count = 2;
854 expected_created = 4;
855 expected_deleted = 2;
856 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
857 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
858 EXPECT_NE(0, scrollbar_layer->track_resource_id());
859 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
860 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
861 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
862 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
864 resource_count = 1;
865 expected_created = 5;
866 expected_deleted = 4;
867 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
868 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
869 EXPECT_NE(0, scrollbar_layer->track_resource_id());
870 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
871 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
872 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
873 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
875 resource_count = 0;
876 expected_created = 5;
877 expected_deleted = 5;
878 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(0, 0, 0, 0));
879 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
880 EXPECT_EQ(0, scrollbar_layer->track_resource_id());
881 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
882 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
883 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
884 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
886 resource_count = 2;
887 expected_created = 7;
888 expected_deleted = 5;
889 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
890 scrollbar_layer->fake_scrollbar()->set_has_thumb(true);
891 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
892 EXPECT_NE(0, scrollbar_layer->track_resource_id());
893 EXPECT_NE(0, scrollbar_layer->thumb_resource_id());
895 resource_count = 1;
896 expected_created = 8;
897 expected_deleted = 7;
898 scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
899 scrollbar_layer->fake_scrollbar()->set_has_thumb(false);
900 scrollbar_layer->SetBounds(gfx::Size(90, 15));
901 EXPECT_TRUE(scrollbar_layer->Update(&queue, &occlusion_tracker));
902 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
903 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
904 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
905 EXPECT_EQ(
906 gfx::Size(90, 15),
907 layer_tree_host_->ui_resource_size(scrollbar_layer->track_resource_id()));
909 scrollbar_layer->ResetNeedsDisplayForTesting();
910 EXPECT_FALSE(scrollbar_layer->Update(&queue, &occlusion_tracker));
911 EXPECT_NE(0, scrollbar_layer->track_resource_id());
912 EXPECT_EQ(0, scrollbar_layer->thumb_resource_id());
913 EXPECT_EQ(resource_count, layer_tree_host_->UIResourceCount());
914 EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
915 EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
917 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
918 scrollbar_layer->ClearRenderSurface();
921 class ScaledScrollbarLayerTestResourceCreation : public testing::Test {
922 public:
923 ScaledScrollbarLayerTestResourceCreation()
924 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
926 void TestResourceUpload(const float test_scale) {
927 layer_tree_host_.reset(
928 new FakeLayerTreeHost(&fake_client_, layer_tree_settings_));
930 gfx::Point scrollbar_location(0, 185);
931 scoped_refptr<Layer> layer_tree_root = Layer::Create();
932 scoped_refptr<Layer> content_layer = Layer::Create();
933 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
934 FakePaintedScrollbarLayer::Create(false, true, layer_tree_root->id());
936 layer_tree_root->AddChild(content_layer);
937 layer_tree_root->AddChild(scrollbar_layer);
939 layer_tree_host_->SetRootLayer(layer_tree_root);
941 scrollbar_layer->SetIsDrawable(true);
942 scrollbar_layer->SetBounds(gfx::Size(100, 15));
943 scrollbar_layer->SetPosition(scrollbar_location);
944 layer_tree_root->SetBounds(gfx::Size(100, 200));
945 content_layer->SetBounds(gfx::Size(100, 200));
946 gfx::SizeF scaled_size =
947 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
948 gfx::PointF scaled_location =
949 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
950 scrollbar_layer->draw_properties().content_bounds =
951 gfx::Size(scaled_size.width(), scaled_size.height());
952 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
953 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
954 scrollbar_layer->draw_properties().visible_content_rect =
955 gfx::Rect(scaled_location.x(),
956 scaled_location.y(),
957 scaled_size.width(),
958 scaled_size.height());
959 scrollbar_layer->CreateRenderSurface();
960 scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
962 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
963 EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
965 ResourceUpdateQueue queue;
966 gfx::Rect screen_space_clip_rect;
967 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
968 scrollbar_layer->SavePaintProperties();
969 scrollbar_layer->Update(&queue, &occlusion_tracker);
971 // Verify that we have not generated any content uploads that are larger
972 // than their destination textures.
974 gfx::Size track_size = layer_tree_host_->ui_resource_size(
975 scrollbar_layer->track_resource_id());
976 gfx::Size thumb_size = layer_tree_host_->ui_resource_size(
977 scrollbar_layer->thumb_resource_id());
979 EXPECT_LE(track_size.width(), scrollbar_layer->content_bounds().width());
980 EXPECT_LE(track_size.height(), scrollbar_layer->content_bounds().height());
981 EXPECT_LE(thumb_size.width(), scrollbar_layer->content_bounds().width());
982 EXPECT_LE(thumb_size.height(), scrollbar_layer->content_bounds().height());
984 testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
986 scrollbar_layer->ClearRenderSurface();
989 protected:
990 FakeLayerTreeHostClient fake_client_;
991 LayerTreeSettings layer_tree_settings_;
992 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
995 TEST_F(ScaledScrollbarLayerTestResourceCreation, ScaledResourceUpload) {
996 // Pick a test scale that moves the scrollbar's (non-zero) position to
997 // a non-pixel-aligned location.
998 TestResourceUpload(.041f);
999 TestResourceUpload(1.41f);
1000 TestResourceUpload(4.1f);
1003 class ScaledScrollbarLayerTestScaledRasterization : public testing::Test {
1004 public:
1005 ScaledScrollbarLayerTestScaledRasterization()
1006 : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
1008 void TestScale(const gfx::Rect scrollbar_rect, const float test_scale) {
1009 layer_tree_host_.reset(
1010 new FakeLayerTreeHost(&fake_client_, layer_tree_settings_));
1012 bool paint_during_update = true;
1013 bool has_thumb = false;
1014 scoped_refptr<Layer> layer_tree_root = Layer::Create();
1015 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
1016 FakePaintedScrollbarLayer::Create(paint_during_update,
1017 has_thumb,
1018 layer_tree_root->id());
1020 layer_tree_root->AddChild(scrollbar_layer);
1022 layer_tree_host_->SetRootLayer(layer_tree_root);
1024 scrollbar_layer->SetBounds(scrollbar_rect.size());
1025 scrollbar_layer->SetPosition(scrollbar_rect.origin());
1026 scrollbar_layer->fake_scrollbar()->set_location(scrollbar_rect.origin());
1027 scrollbar_layer->fake_scrollbar()->set_track_rect(scrollbar_rect);
1028 gfx::SizeF scaled_size =
1029 gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
1030 gfx::PointF scaled_location =
1031 gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
1032 scrollbar_layer->draw_properties().content_bounds =
1033 gfx::Size(scaled_size.width(), scaled_size.height());
1034 scrollbar_layer->draw_properties().contents_scale_x = test_scale;
1035 scrollbar_layer->draw_properties().contents_scale_y = test_scale;
1036 scrollbar_layer->draw_properties().visible_content_rect =
1037 gfx::Rect(scaled_location.x(),
1038 scaled_location.y(),
1039 scaled_size.width(),
1040 scaled_size.height());
1042 ResourceUpdateQueue queue;
1043 gfx::Rect screen_space_clip_rect;
1044 OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
1045 scrollbar_layer->SavePaintProperties();
1047 scrollbar_layer->Update(&queue, &occlusion_tracker);
1049 UIResourceBitmap* bitmap = layer_tree_host_->ui_resource_bitmap(
1050 scrollbar_layer->track_resource_id());
1052 DCHECK(bitmap);
1054 AutoLockUIResourceBitmap locked_bitmap(*bitmap);
1056 const SkColor* pixels =
1057 reinterpret_cast<const SkColor*>(locked_bitmap.GetPixels());
1058 SkColor color = argb_to_skia(
1059 scrollbar_layer->fake_scrollbar()->paint_fill_color());
1060 int width = bitmap->GetSize().width();
1061 int height = bitmap->GetSize().height();
1063 // Make sure none of the corners of the bitmap were inadvertently clipped.
1064 EXPECT_EQ(color, pixels[0])
1065 << "Top left pixel doesn't match scrollbar color.";
1067 EXPECT_EQ(color, pixels[width - 1])
1068 << "Top right pixel doesn't match scrollbar color.";
1070 EXPECT_EQ(color, pixels[width * (height - 1)])
1071 << "Bottom left pixel doesn't match scrollbar color.";
1073 EXPECT_EQ(color, pixels[width * height - 1])
1074 << "Bottom right pixel doesn't match scrollbar color.";
1077 protected:
1078 // On Android, Skia uses ABGR
1079 static SkColor argb_to_skia(SkColor c) {
1080 return (SkColorGetA(c) << SK_A32_SHIFT) |
1081 (SkColorGetR(c) << SK_R32_SHIFT) |
1082 (SkColorGetG(c) << SK_G32_SHIFT) |
1083 (SkColorGetB(c) << SK_B32_SHIFT);
1086 FakeLayerTreeHostClient fake_client_;
1087 LayerTreeSettings layer_tree_settings_;
1088 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
1091 TEST_F(ScaledScrollbarLayerTestScaledRasterization, TestLostPrecisionInClip) {
1092 // Try rasterization at coordinates and scale that caused problematic
1093 // rounding and clipping errors.
1094 // Vertical Scrollbars.
1095 TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f);
1096 TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f);
1098 // Horizontal Scrollbars.
1099 TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f);
1100 TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f);
1103 } // namespace
1104 } // namespace cc