Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / cc / layers / scrollbar_layer_unittest.cc
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.
4
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_quad_culler.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"
32
33 namespace cc {
34 namespace {
35
36 LayerImpl* LayerImplForScrollAreaAndScrollbar(
37     FakeLayerTreeHost* host,
38     scoped_ptr<Scrollbar> scrollbar,
39     bool reverse_order,
40     bool use_solid_color_scrollbar,
41     int thumb_thickness) {
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(
48         scrollbar->Orientation(), thumb_thickness,
49         kIsLeftSideVerticalScrollbar, child1->id());
50   } else {
51     child2 = PaintedScrollbarLayer::Create(scrollbar.Pass(), child1->id());
52   }
53   layer_tree_root->AddChild(child1);
54   layer_tree_root->InsertChild(child2, reverse_order ? 0 : 1);
55   host->SetRootLayer(layer_tree_root);
56   return host->CommitAndCreateLayerImplTree();
57 }
58
59 TEST(ScrollbarLayerTest, ResolveScrollLayerPointer) {
60   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
61   scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
62   LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
63       host.get(), scrollbar.Pass(), false, false, 0);
64
65   LayerImpl* cc_child1 = layer_impl_tree_root->children()[0];
66   PaintedScrollbarLayerImpl* cc_child2 =
67       static_cast<PaintedScrollbarLayerImpl*>(
68           layer_impl_tree_root->children()[1]);
69
70   EXPECT_EQ(cc_child1->scrollbars()->size(), 1UL);
71   EXPECT_EQ(*(cc_child1->scrollbars()->begin()), cc_child2);
72 }
73
74 TEST(ScrollbarLayerTest, ResolveScrollLayerPointer_ReverseOrder) {
75   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
76   scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
77   LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
78       host.get(), scrollbar.Pass(), true, false, 0);
79
80   PaintedScrollbarLayerImpl* cc_child1 =
81       static_cast<PaintedScrollbarLayerImpl*>(
82           layer_impl_tree_root->children()[0]);
83   LayerImpl* cc_child2 = layer_impl_tree_root->children()[1];
84
85   EXPECT_EQ(cc_child2->scrollbars()->size(), 1UL);
86   EXPECT_EQ(*(cc_child2->scrollbars()->begin()), cc_child1);
87 }
88
89 TEST(ScrollbarLayerTest, ShouldScrollNonOverlayOnMainThread) {
90   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
91
92   // Create and attach a non-overlay scrollbar.
93   scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
94   LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
95       host.get(), scrollbar.Pass(), false, false, 0);
96   PaintedScrollbarLayerImpl* scrollbar_layer_impl =
97       static_cast<PaintedScrollbarLayerImpl*>(
98           layer_impl_tree_root->children()[1]);
99
100   // When the scrollbar is not an overlay scrollbar, the scroll should be
101   // responded to on the main thread as the compositor does not yet implement
102   // scrollbar scrolling.
103   EXPECT_EQ(InputHandler::ScrollOnMainThread,
104             scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
105                                             InputHandler::Gesture));
106
107   // Create and attach an overlay scrollbar.
108   scrollbar.reset(new FakeScrollbar(false, false, true));
109
110   layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
111       host.get(), scrollbar.Pass(), false, false, 0);
112   scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>(
113       layer_impl_tree_root->children()[1]);
114
115   // The user shouldn't be able to drag an overlay scrollbar and the scroll
116   // may be handled in the compositor.
117   EXPECT_EQ(InputHandler::ScrollIgnored,
118             scrollbar_layer_impl->TryScroll(gfx::Point(0, 0),
119                                             InputHandler::Gesture));
120 }
121
122 TEST(PaintedScrollbarLayerTest, ScrollOffsetSynchronization) {
123   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
124
125   scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
126   scoped_refptr<Layer> layer_tree_root = Layer::Create();
127   scoped_refptr<Layer> scroll_layer = Layer::Create();
128   scoped_refptr<Layer> content_layer = Layer::Create();
129   scoped_refptr<Layer> scrollbar_layer =
130       PaintedScrollbarLayer::Create(scrollbar.Pass(), layer_tree_root->id());
131
132   // Choose bounds to give max_scroll_offset = (30, 50).
133   layer_tree_root->SetBounds(gfx::Size(70, 150));
134   scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
135   scroll_layer->SetScrollOffset(gfx::Vector2d(10, 20));
136   scroll_layer->SetBounds(gfx::Size(100, 200));
137   content_layer->SetBounds(gfx::Size(100, 200));
138
139   host->SetRootLayer(layer_tree_root);
140   layer_tree_root->AddChild(scroll_layer);
141   scroll_layer->AddChild(content_layer);
142   layer_tree_root->AddChild(scrollbar_layer);
143   scrollbar_layer->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
144   scrollbar_layer->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
145
146   layer_tree_root->SavePaintProperties();
147   content_layer->SavePaintProperties();
148
149   LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
150
151   ScrollbarLayerImplBase* cc_scrollbar_layer =
152       static_cast<PaintedScrollbarLayerImpl*>(
153           layer_impl_tree_root->children()[1]);
154
155   EXPECT_EQ(10.f, cc_scrollbar_layer->current_pos());
156   EXPECT_EQ(30, cc_scrollbar_layer->maximum());
157
158   layer_tree_root->SetBounds(gfx::Size(700, 1500));
159   layer_tree_root->SavePaintProperties();
160   scroll_layer->SetBounds(gfx::Size(1000, 2000));
161   scroll_layer->SetScrollOffset(gfx::Vector2d(100, 200));
162   scroll_layer->SavePaintProperties();
163   content_layer->SetBounds(gfx::Size(1000, 2000));
164   content_layer->SavePaintProperties();
165
166   ScrollbarAnimationController* scrollbar_controller =
167       layer_impl_tree_root->scrollbar_animation_controller();
168   layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
169   EXPECT_EQ(scrollbar_controller,
170             layer_impl_tree_root->scrollbar_animation_controller());
171
172   EXPECT_EQ(100.f, cc_scrollbar_layer->current_pos());
173   EXPECT_EQ(300, cc_scrollbar_layer->maximum());
174
175   LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
176   scroll_layer_impl->ScrollBy(gfx::Vector2d(12, 34));
177
178   EXPECT_EQ(112.f, cc_scrollbar_layer->current_pos());
179   EXPECT_EQ(300, cc_scrollbar_layer->maximum());
180 }
181
182 #define UPDATE_AND_EXTRACT_LAYER_POINTERS()                         \
183   do {                                                              \
184     scrollbar_layer->UpdateThumbAndTrackGeometry();                 \
185     root_clip_layer_impl = host->CommitAndCreateLayerImplTree();    \
186     root_layer_impl = root_clip_layer_impl->children()[0];          \
187     scrollbar_layer_impl = static_cast<PaintedScrollbarLayerImpl*>( \
188         root_layer_impl->children()[1]);                            \
189     scrollbar_layer_impl->ScrollbarParametersDidChange();           \
190   } while (false)
191
192 TEST(ScrollbarLayerTest, ThumbRect) {
193   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
194   scoped_refptr<Layer> root_clip_layer = Layer::Create();
195   scoped_refptr<Layer> root_layer = Layer::Create();
196   scoped_refptr<Layer> content_layer = Layer::Create();
197   scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
198       FakePaintedScrollbarLayer::Create(false, true, root_layer->id());
199
200   root_layer->SetScrollClipLayerId(root_clip_layer->id());
201   // Give the root-clip a size that will result in MaxScrollOffset = (80, 0).
202   root_clip_layer->SetBounds(gfx::Size(20, 50));
203   root_layer->SetBounds(gfx::Size(100, 50));
204   content_layer->SetBounds(gfx::Size(100, 50));
205
206   host->SetRootLayer(root_clip_layer);
207   root_clip_layer->AddChild(root_layer);
208   root_layer->AddChild(content_layer);
209   root_layer->AddChild(scrollbar_layer);
210
211   root_layer->SetScrollOffset(gfx::Vector2d(0, 0));
212   scrollbar_layer->SetBounds(gfx::Size(70, 10));
213   scrollbar_layer->SetScrollLayer(root_layer->id());
214   scrollbar_layer->SetClipLayer(root_clip_layer->id());
215   scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10));
216   scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
217   scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10);
218   scrollbar_layer->fake_scrollbar()->set_thumb_length(4);
219   scrollbar_layer->UpdateThumbAndTrackGeometry();
220   LayerImpl* root_clip_layer_impl = NULL;
221   LayerImpl* root_layer_impl = NULL;
222   PaintedScrollbarLayerImpl* scrollbar_layer_impl = NULL;
223
224   // Thumb is at the edge of the scrollbar (should be inset to
225   // the start of the track within the scrollbar layer's
226   // position).
227   UPDATE_AND_EXTRACT_LAYER_POINTERS();
228   EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
229             scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
230
231   // Under-scroll (thumb position should clamp and be unchanged).
232   root_layer->SetScrollOffset(gfx::Vector2d(-5, 0));
233
234   UPDATE_AND_EXTRACT_LAYER_POINTERS();
235   EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(),
236             scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
237
238   // Over-scroll (thumb position should clamp on the far side).
239   root_layer->SetScrollOffset(gfx::Vector2d(85, 0));
240
241   UPDATE_AND_EXTRACT_LAYER_POINTERS();
242   EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(),
243             scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
244
245   // Change thumb thickness and length.
246   scrollbar_layer->fake_scrollbar()->set_thumb_thickness(4);
247   scrollbar_layer->fake_scrollbar()->set_thumb_length(6);
248
249   UPDATE_AND_EXTRACT_LAYER_POINTERS();
250   EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(),
251             scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
252
253   // Shrink the scrollbar layer to cover only the track.
254   scrollbar_layer->SetBounds(gfx::Size(50, 10));
255   scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(30, 10));
256   scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10));
257
258   UPDATE_AND_EXTRACT_LAYER_POINTERS();
259   EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
260             scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
261
262   // Shrink the track in the non-scrolling dimension so that it only covers the
263   // middle third of the scrollbar layer (this does not affect the thumb
264   // position).
265   scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6));
266
267   UPDATE_AND_EXTRACT_LAYER_POINTERS();
268   EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(),
269             scrollbar_layer_impl->ComputeThumbQuadRect().ToString());
270 }
271
272 TEST(ScrollbarLayerTest, SolidColorDrawQuads) {
273   const int kThumbThickness = 3;
274   const int kTrackLength = 100;
275
276   LayerTreeSettings layer_tree_settings;
277   scoped_ptr<FakeLayerTreeHost> host =
278       FakeLayerTreeHost::Create(layer_tree_settings);
279
280   scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
281   LayerImpl* layer_impl_tree_root = LayerImplForScrollAreaAndScrollbar(
282       host.get(), scrollbar.Pass(), false, true, kThumbThickness);
283   ScrollbarLayerImplBase* scrollbar_layer_impl =
284       static_cast<SolidColorScrollbarLayerImpl*>(
285           layer_impl_tree_root->children()[1]);
286   scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
287   scrollbar_layer_impl->SetCurrentPos(10.f);
288   scrollbar_layer_impl->SetMaximum(100);
289   scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.4f);
290
291   // Thickness should be overridden to 3.
292   {
293     MockQuadCuller quad_culler;
294     AppendQuadsData data;
295     scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
296
297     const QuadList& quads = quad_culler.quad_list();
298     ASSERT_EQ(1u, quads.size());
299     EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
300     EXPECT_RECT_EQ(gfx::Rect(6, 0, 40, 3), quads[0]->rect);
301   }
302
303   // Contents scale should scale the draw quad.
304   scrollbar_layer_impl->draw_properties().contents_scale_x = 2.f;
305   scrollbar_layer_impl->draw_properties().contents_scale_y = 2.f;
306   {
307     MockQuadCuller quad_culler;
308     AppendQuadsData data;
309     scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
310
311     const QuadList& quads = quad_culler.quad_list();
312     ASSERT_EQ(1u, quads.size());
313     EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
314     EXPECT_RECT_EQ(gfx::Rect(12, 0, 80, 6), quads[0]->rect);
315   }
316   scrollbar_layer_impl->draw_properties().contents_scale_x = 1.f;
317   scrollbar_layer_impl->draw_properties().contents_scale_y = 1.f;
318
319   // For solid color scrollbars, position and size should reflect the
320   // current viewport state.
321   scrollbar_layer_impl->SetVisibleToTotalLengthRatio(0.2f);
322   {
323     MockQuadCuller quad_culler;
324     AppendQuadsData data;
325     scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
326
327     const QuadList& quads = quad_culler.quad_list();
328     ASSERT_EQ(1u, quads.size());
329     EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
330     EXPECT_RECT_EQ(gfx::Rect(8, 0, 20, 3), quads[0]->rect);
331   }
332 }
333
334 TEST(ScrollbarLayerTest, LayerDrivenSolidColorDrawQuads) {
335   const int kThumbThickness = 3;
336   const int kTrackLength = 10;
337
338   LayerTreeSettings layer_tree_settings;
339   scoped_ptr<FakeLayerTreeHost> host =
340       FakeLayerTreeHost::Create(layer_tree_settings);
341
342   scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, true));
343
344   {
345     scoped_refptr<Layer> layer_tree_root = Layer::Create();
346     scoped_refptr<Layer> scroll_layer = Layer::Create();
347     scroll_layer->SetScrollClipLayerId(layer_tree_root->id());
348     scoped_refptr<Layer> child1 = Layer::Create();
349     scoped_refptr<Layer> child2;
350     const bool kIsLeftSideVerticalScrollbar = false;
351     child2 = SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
352                                               kThumbThickness,
353                                               kIsLeftSideVerticalScrollbar,
354                                               child1->id());
355     child2->ToScrollbarLayer()->SetScrollLayer(scroll_layer->id());
356     child2->ToScrollbarLayer()->SetClipLayer(layer_tree_root->id());
357     scroll_layer->AddChild(child1);
358     scroll_layer->InsertChild(child2, 1);
359     layer_tree_root->AddChild(scroll_layer);
360     host->SetRootLayer(layer_tree_root);
361   }
362   LayerImpl* layer_impl_tree_root = host->CommitAndCreateLayerImplTree();
363   LayerImpl* scroll_layer_impl = layer_impl_tree_root->children()[0];
364
365   ScrollbarLayerImplBase* scrollbar_layer_impl =
366       static_cast<PaintedScrollbarLayerImpl*>(scroll_layer_impl->children()[1]);
367
368   // Choose layer bounds to give max_scroll_offset = (8, 8).
369   layer_impl_tree_root->SetBounds(gfx::Size(2, 2));
370   scroll_layer_impl->SetBounds(gfx::Size(10, 10));
371   scroll_layer_impl->ScrollBy(gfx::Vector2dF(4.f, 0.f));
372
373   scrollbar_layer_impl->SetBounds(gfx::Size(kTrackLength, kThumbThickness));
374   scrollbar_layer_impl->SetCurrentPos(4.f);
375   scrollbar_layer_impl->SetMaximum(8);
376
377   {
378     MockQuadCuller quad_culler;
379     AppendQuadsData data;
380     scrollbar_layer_impl->AppendQuads(&quad_culler, &data);
381
382     const QuadList& quads = quad_culler.quad_list();
383     ASSERT_EQ(1u, quads.size());
384     EXPECT_EQ(DrawQuad::SOLID_COLOR, quads[0]->material);
385     EXPECT_RECT_EQ(gfx::Rect(3, 0, 3, 3), quads[0]->rect);
386   }
387 }
388
389 class ScrollbarLayerSolidColorThumbTest : public testing::Test {
390  public:
391   ScrollbarLayerSolidColorThumbTest() {
392     LayerTreeSettings layer_tree_settings;
393     host_impl_.reset(new FakeLayerTreeHostImpl(
394         layer_tree_settings, &proxy_, &shared_bitmap_manager_));
395
396     const int kThumbThickness = 3;
397     const bool kIsLeftSideVerticalScrollbar = false;
398     const bool kIsOverlayScrollbar = false;
399
400     horizontal_scrollbar_layer_ =
401         SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
402                                              1,
403                                              HORIZONTAL,
404                                              kThumbThickness,
405                                              kIsLeftSideVerticalScrollbar,
406                                              kIsOverlayScrollbar);
407     vertical_scrollbar_layer_ =
408         SolidColorScrollbarLayerImpl::Create(host_impl_->active_tree(),
409                                              2,
410                                              VERTICAL,
411                                              kThumbThickness,
412                                              kIsLeftSideVerticalScrollbar,
413                                              kIsOverlayScrollbar);
414   }
415
416  protected:
417   FakeImplProxy proxy_;
418   TestSharedBitmapManager shared_bitmap_manager_;
419   scoped_ptr<FakeLayerTreeHostImpl> host_impl_;
420   scoped_ptr<SolidColorScrollbarLayerImpl> horizontal_scrollbar_layer_;
421   scoped_ptr<SolidColorScrollbarLayerImpl> vertical_scrollbar_layer_;
422 };
423
424 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbLength) {
425   horizontal_scrollbar_layer_->SetCurrentPos(0);
426   horizontal_scrollbar_layer_->SetMaximum(10);
427
428   // Simple case - one third of the scrollable area is visible, so the thumb
429   // should be one third as long as the track.
430   horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.33f);
431   horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
432   EXPECT_EQ(33, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
433
434   // The thumb's length should never be less than its thickness.
435   horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.01f);
436   horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
437   EXPECT_EQ(3, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
438 }
439
440 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbPosition) {
441   horizontal_scrollbar_layer_->SetBounds(gfx::Size(100, 3));
442   horizontal_scrollbar_layer_->SetVisibleToTotalLengthRatio(0.1f);
443
444   horizontal_scrollbar_layer_->SetCurrentPos(0);
445   horizontal_scrollbar_layer_->SetMaximum(100);
446   EXPECT_EQ(0, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
447   EXPECT_EQ(10, horizontal_scrollbar_layer_->ComputeThumbQuadRect().width());
448
449   horizontal_scrollbar_layer_->SetCurrentPos(100);
450   // The thumb is 10px long and the track is 100px, so the maximum thumb
451   // position is 90px.
452   EXPECT_EQ(90, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
453
454   horizontal_scrollbar_layer_->SetCurrentPos(80);
455   // The scroll position is 80% of the maximum, so the thumb's position should
456   // be at 80% of its maximum or 72px.
457   EXPECT_EQ(72, horizontal_scrollbar_layer_->ComputeThumbQuadRect().x());
458 }
459
460 TEST_F(ScrollbarLayerSolidColorThumbTest, SolidColorThumbVerticalAdjust) {
461   SolidColorScrollbarLayerImpl* layers[2] =
462       { horizontal_scrollbar_layer_.get(), vertical_scrollbar_layer_.get() };
463   for (size_t i = 0; i < 2; ++i) {
464     layers[i]->SetVisibleToTotalLengthRatio(0.2f);
465     layers[i]->SetCurrentPos(25);
466     layers[i]->SetMaximum(100);
467   }
468   layers[0]->SetBounds(gfx::Size(100, 3));
469   layers[1]->SetBounds(gfx::Size(3, 100));
470
471   EXPECT_RECT_EQ(gfx::RectF(20.f, 0.f, 20.f, 3.f),
472                  horizontal_scrollbar_layer_->ComputeThumbQuadRect());
473   EXPECT_RECT_EQ(gfx::RectF(0.f, 20.f, 3.f, 20.f),
474                  vertical_scrollbar_layer_->ComputeThumbQuadRect());
475
476   horizontal_scrollbar_layer_->SetVerticalAdjust(10.f);
477   vertical_scrollbar_layer_->SetVerticalAdjust(10.f);
478
479   // The vertical adjustment factor has two effects:
480   // 1.) Moves the horizontal scrollbar down
481   // 2.) Increases the vertical scrollbar's effective track length which both
482   // increases the thumb's length and its position within the track.
483   EXPECT_RECT_EQ(gfx::Rect(20.f, 10.f, 20.f, 3.f),
484                  horizontal_scrollbar_layer_->ComputeThumbQuadRect());
485   EXPECT_RECT_EQ(gfx::Rect(0.f, 22, 3.f, 22.f),
486                  vertical_scrollbar_layer_->ComputeThumbQuadRect());
487 }
488
489 class ScrollbarLayerTestMaxTextureSize : public LayerTreeTest {
490  public:
491   ScrollbarLayerTestMaxTextureSize() {}
492
493   void SetScrollbarBounds(const gfx::Size& bounds) { bounds_ = bounds; }
494
495   virtual void BeginTest() OVERRIDE {
496     scroll_layer_ = Layer::Create();
497     layer_tree_host()->root_layer()->AddChild(scroll_layer_);
498
499     scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar);
500     scrollbar_layer_ =
501         PaintedScrollbarLayer::Create(scrollbar.Pass(), scroll_layer_->id());
502     scrollbar_layer_->SetScrollLayer(scroll_layer_->id());
503     scrollbar_layer_->SetLayerTreeHost(layer_tree_host());
504     scrollbar_layer_->SetBounds(bounds_);
505     layer_tree_host()->root_layer()->AddChild(scrollbar_layer_);
506
507     PostSetNeedsCommitToMainThread();
508   }
509
510   virtual void DidCommitAndDrawFrame() OVERRIDE {
511     const int kMaxTextureSize =
512         layer_tree_host()->GetRendererCapabilities().max_texture_size;
513
514     // Check first that we're actually testing something.
515     EXPECT_GT(scrollbar_layer_->bounds().width(), kMaxTextureSize);
516
517     EXPECT_EQ(scrollbar_layer_->content_bounds().width(),
518               kMaxTextureSize - 1);
519     EXPECT_EQ(scrollbar_layer_->content_bounds().height(),
520               kMaxTextureSize - 1);
521
522     EndTest();
523   }
524
525   virtual void AfterTest() OVERRIDE {}
526
527  private:
528   scoped_refptr<PaintedScrollbarLayer> scrollbar_layer_;
529   scoped_refptr<Layer> scroll_layer_;
530   gfx::Size bounds_;
531 };
532
533 TEST_F(ScrollbarLayerTestMaxTextureSize, DirectRenderer) {
534   scoped_ptr<TestWebGraphicsContext3D> context =
535       TestWebGraphicsContext3D::Create();
536   int max_size = 0;
537   context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
538   SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
539   RunTest(true, false, true);
540 }
541
542 TEST_F(ScrollbarLayerTestMaxTextureSize, DelegatingRenderer) {
543   scoped_ptr<TestWebGraphicsContext3D> context =
544       TestWebGraphicsContext3D::Create();
545   int max_size = 0;
546   context->getIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
547   SetScrollbarBounds(gfx::Size(max_size + 100, max_size + 100));
548   RunTest(true, true, true);
549 }
550
551 class MockLayerTreeHost : public LayerTreeHost {
552  public:
553   MockLayerTreeHost(FakeLayerTreeHostClient* client,
554                     const LayerTreeSettings& settings)
555       : LayerTreeHost(client, NULL, settings),
556         next_id_(1),
557         total_ui_resource_created_(0),
558         total_ui_resource_deleted_(0) {
559     InitializeSingleThreaded(client);
560   }
561
562   virtual UIResourceId CreateUIResource(UIResourceClient* content) OVERRIDE {
563     total_ui_resource_created_++;
564     UIResourceId nid = next_id_++;
565     ui_resource_bitmap_map_.insert(
566         std::make_pair(nid, content->GetBitmap(nid, false)));
567     return nid;
568   }
569
570   // Deletes a UI resource.  May safely be called more than once.
571   virtual void DeleteUIResource(UIResourceId id) OVERRIDE {
572     UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
573     if (iter != ui_resource_bitmap_map_.end()) {
574       ui_resource_bitmap_map_.erase(iter);
575       total_ui_resource_deleted_++;
576     }
577   }
578
579   size_t UIResourceCount() { return ui_resource_bitmap_map_.size(); }
580   int TotalUIResourceDeleted() { return total_ui_resource_deleted_; }
581   int TotalUIResourceCreated() { return total_ui_resource_created_; }
582
583   gfx::Size ui_resource_size(UIResourceId id) {
584     UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
585     if (iter != ui_resource_bitmap_map_.end())
586       return iter->second.GetSize();
587     return gfx::Size();
588   }
589
590   UIResourceBitmap* ui_resource_bitmap(UIResourceId id) {
591     UIResourceBitmapMap::iterator iter = ui_resource_bitmap_map_.find(id);
592     if (iter != ui_resource_bitmap_map_.end())
593       return &iter->second;
594     return NULL;
595   }
596
597  private:
598   typedef base::hash_map<UIResourceId, UIResourceBitmap>
599       UIResourceBitmapMap;
600   UIResourceBitmapMap ui_resource_bitmap_map_;
601
602   int next_id_;
603   int total_ui_resource_created_;
604   int total_ui_resource_deleted_;
605 };
606
607
608 class ScrollbarLayerTestResourceCreation : public testing::Test {
609  public:
610   ScrollbarLayerTestResourceCreation()
611       : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
612
613   void TestResourceUpload(int num_updates,
614                           size_t expected_resources,
615                           int expected_created,
616                           int expected_deleted,
617                           bool use_solid_color_scrollbar) {
618     layer_tree_host_.reset(
619         new MockLayerTreeHost(&fake_client_, layer_tree_settings_));
620
621     scoped_ptr<Scrollbar> scrollbar(new FakeScrollbar(false, true, false));
622     scoped_refptr<Layer> layer_tree_root = Layer::Create();
623     scoped_refptr<Layer> content_layer = Layer::Create();
624     scoped_refptr<Layer> scrollbar_layer;
625     if (use_solid_color_scrollbar) {
626       const int kThumbThickness = 3;
627       const bool kIsLeftSideVerticalScrollbar = false;
628       scrollbar_layer =
629           SolidColorScrollbarLayer::Create(scrollbar->Orientation(),
630                                            kThumbThickness,
631                                            kIsLeftSideVerticalScrollbar,
632                                            layer_tree_root->id());
633     } else {
634       scrollbar_layer = PaintedScrollbarLayer::Create(scrollbar.Pass(),
635                                                       layer_tree_root->id());
636     }
637     layer_tree_root->AddChild(content_layer);
638     layer_tree_root->AddChild(scrollbar_layer);
639
640     layer_tree_host_->SetRootLayer(layer_tree_root);
641
642     scrollbar_layer->SetIsDrawable(true);
643     scrollbar_layer->SetBounds(gfx::Size(100, 100));
644     layer_tree_root->SetScrollOffset(gfx::Vector2d(10, 20));
645     layer_tree_root->SetBounds(gfx::Size(100, 200));
646     content_layer->SetBounds(gfx::Size(100, 200));
647     scrollbar_layer->draw_properties().content_bounds = gfx::Size(100, 200);
648     scrollbar_layer->draw_properties().visible_content_rect =
649         gfx::Rect(0, 0, 100, 200);
650     scrollbar_layer->CreateRenderSurface();
651     scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
652
653     testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
654     EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
655
656     ResourceUpdateQueue queue;
657     gfx::Rect screen_space_clip_rect;
658     OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
659
660     scrollbar_layer->SavePaintProperties();
661     for (int update_counter = 0; update_counter < num_updates; update_counter++)
662       scrollbar_layer->Update(&queue, &occlusion_tracker);
663
664     // A non-solid-color scrollbar should have requested two textures.
665     EXPECT_EQ(expected_resources, layer_tree_host_->UIResourceCount());
666     EXPECT_EQ(expected_created, layer_tree_host_->TotalUIResourceCreated());
667     EXPECT_EQ(expected_deleted, layer_tree_host_->TotalUIResourceDeleted());
668
669     testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
670
671     scrollbar_layer->ClearRenderSurface();
672   }
673
674  protected:
675   FakeLayerTreeHostClient fake_client_;
676   LayerTreeSettings layer_tree_settings_;
677   scoped_ptr<MockLayerTreeHost> layer_tree_host_;
678 };
679
680 TEST_F(ScrollbarLayerTestResourceCreation, ResourceUpload) {
681   bool use_solid_color_scrollbars = false;
682   TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
683   int num_updates[3] = {1, 5, 10};
684   for (int j = 0; j < 3; j++) {
685     TestResourceUpload(num_updates[j],
686                        2,
687                        num_updates[j] * 2,
688                        (num_updates[j] - 1) * 2,
689                        use_solid_color_scrollbars);
690   }
691 }
692
693 TEST_F(ScrollbarLayerTestResourceCreation, SolidColorNoResourceUpload) {
694   bool use_solid_color_scrollbars = true;
695   TestResourceUpload(0, 0, 0, 0, use_solid_color_scrollbars);
696   TestResourceUpload(1, 0, 0, 0, use_solid_color_scrollbars);
697 }
698
699 class ScaledScrollbarLayerTestResourceCreation : public testing::Test {
700  public:
701   ScaledScrollbarLayerTestResourceCreation()
702       : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
703
704   void TestResourceUpload(const float test_scale) {
705     layer_tree_host_.reset(
706         new MockLayerTreeHost(&fake_client_, layer_tree_settings_));
707
708     gfx::Point scrollbar_location(0, 185);
709     scoped_refptr<Layer> layer_tree_root = Layer::Create();
710     scoped_refptr<Layer> content_layer = Layer::Create();
711     scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
712         FakePaintedScrollbarLayer::Create(false, true, layer_tree_root->id());
713
714     layer_tree_root->AddChild(content_layer);
715     layer_tree_root->AddChild(scrollbar_layer);
716
717     layer_tree_host_->SetRootLayer(layer_tree_root);
718
719     scrollbar_layer->SetIsDrawable(true);
720     scrollbar_layer->SetBounds(gfx::Size(100, 15));
721     scrollbar_layer->SetPosition(scrollbar_location);
722     layer_tree_root->SetBounds(gfx::Size(100, 200));
723     content_layer->SetBounds(gfx::Size(100, 200));
724     gfx::SizeF scaled_size =
725         gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
726     gfx::PointF scaled_location =
727         gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
728     scrollbar_layer->draw_properties().content_bounds =
729         gfx::Size(scaled_size.width(), scaled_size.height());
730     scrollbar_layer->draw_properties().contents_scale_x = test_scale;
731     scrollbar_layer->draw_properties().contents_scale_y = test_scale;
732     scrollbar_layer->draw_properties().visible_content_rect =
733         gfx::Rect(scaled_location.x(),
734                   scaled_location.y(),
735                   scaled_size.width(),
736                   scaled_size.height());
737     scrollbar_layer->CreateRenderSurface();
738     scrollbar_layer->draw_properties().render_target = scrollbar_layer.get();
739
740     testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
741     EXPECT_EQ(scrollbar_layer->layer_tree_host(), layer_tree_host_.get());
742
743     ResourceUpdateQueue queue;
744     gfx::Rect screen_space_clip_rect;
745     OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
746     scrollbar_layer->SavePaintProperties();
747     scrollbar_layer->Update(&queue, &occlusion_tracker);
748
749     // Verify that we have not generated any content uploads that are larger
750     // than their destination textures.
751
752     gfx::Size track_size = layer_tree_host_->ui_resource_size(
753         scrollbar_layer->track_resource_id());
754     gfx::Size thumb_size = layer_tree_host_->ui_resource_size(
755         scrollbar_layer->thumb_resource_id());
756
757     EXPECT_LE(track_size.width(), scrollbar_layer->content_bounds().width());
758     EXPECT_LE(track_size.height(), scrollbar_layer->content_bounds().height());
759     EXPECT_LE(thumb_size.width(), scrollbar_layer->content_bounds().width());
760     EXPECT_LE(thumb_size.height(), scrollbar_layer->content_bounds().height());
761
762     testing::Mock::VerifyAndClearExpectations(layer_tree_host_.get());
763
764     scrollbar_layer->ClearRenderSurface();
765   }
766
767  protected:
768   FakeLayerTreeHostClient fake_client_;
769   LayerTreeSettings layer_tree_settings_;
770   scoped_ptr<MockLayerTreeHost> layer_tree_host_;
771 };
772
773 TEST_F(ScaledScrollbarLayerTestResourceCreation, ScaledResourceUpload) {
774   // Pick a test scale that moves the scrollbar's (non-zero) position to
775   // a non-pixel-aligned location.
776   TestResourceUpload(.041f);
777   TestResourceUpload(1.41f);
778   TestResourceUpload(4.1f);
779 }
780
781 class ScaledScrollbarLayerTestScaledRasterization : public testing::Test {
782  public:
783   ScaledScrollbarLayerTestScaledRasterization()
784       : fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
785
786   void TestScale(const gfx::Rect scrollbar_rect, const float test_scale) {
787     layer_tree_host_.reset(
788         new MockLayerTreeHost(&fake_client_, layer_tree_settings_));
789
790     bool paint_during_update = true;
791     bool has_thumb = false;
792     scoped_refptr<Layer> layer_tree_root = Layer::Create();
793     scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer =
794         FakePaintedScrollbarLayer::Create(paint_during_update,
795                                           has_thumb,
796                                           layer_tree_root->id());
797
798     layer_tree_root->AddChild(scrollbar_layer);
799
800     layer_tree_host_->SetRootLayer(layer_tree_root);
801
802     scrollbar_layer->SetBounds(scrollbar_rect.size());
803     scrollbar_layer->SetPosition(scrollbar_rect.origin());
804     scrollbar_layer->fake_scrollbar()->set_location(scrollbar_rect.origin());
805     scrollbar_layer->fake_scrollbar()->set_track_rect(scrollbar_rect);
806     gfx::SizeF scaled_size =
807         gfx::ScaleSize(scrollbar_layer->bounds(), test_scale, test_scale);
808     gfx::PointF scaled_location =
809         gfx::ScalePoint(scrollbar_layer->position(), test_scale, test_scale);
810     scrollbar_layer->draw_properties().content_bounds =
811         gfx::Size(scaled_size.width(), scaled_size.height());
812     scrollbar_layer->draw_properties().contents_scale_x = test_scale;
813     scrollbar_layer->draw_properties().contents_scale_y = test_scale;
814     scrollbar_layer->draw_properties().visible_content_rect =
815         gfx::Rect(scaled_location.x(),
816                   scaled_location.y(),
817                   scaled_size.width(),
818                   scaled_size.height());
819
820     ResourceUpdateQueue queue;
821     gfx::Rect screen_space_clip_rect;
822     OcclusionTracker<Layer> occlusion_tracker(screen_space_clip_rect);
823     scrollbar_layer->SavePaintProperties();
824
825     scrollbar_layer->Update(&queue, &occlusion_tracker);
826
827     UIResourceBitmap* bitmap = layer_tree_host_->ui_resource_bitmap(
828         scrollbar_layer->track_resource_id());
829
830     DCHECK(bitmap);
831
832     AutoLockUIResourceBitmap locked_bitmap(*bitmap);
833
834     const SkColor* pixels =
835         reinterpret_cast<const SkColor*>(locked_bitmap.GetPixels());
836     SkColor color = argb_to_skia(
837         scrollbar_layer->fake_scrollbar()->paint_fill_color());
838     int width = bitmap->GetSize().width();
839     int height = bitmap->GetSize().height();
840
841     // Make sure none of the corners of the bitmap were inadvertently clipped.
842     EXPECT_EQ(color, pixels[0])
843         << "Top left pixel doesn't match scrollbar color.";
844
845     EXPECT_EQ(color, pixels[width - 1])
846         << "Top right pixel doesn't match scrollbar color.";
847
848     EXPECT_EQ(color, pixels[width * (height - 1)])
849         << "Bottom left pixel doesn't match scrollbar color.";
850
851     EXPECT_EQ(color, pixels[width * height - 1])
852         << "Bottom right pixel doesn't match scrollbar color.";
853   }
854
855  protected:
856   // On Android, Skia uses ABGR
857   static SkColor argb_to_skia(SkColor c) {
858       return (SkColorGetA(c) << SK_A32_SHIFT) |
859              (SkColorGetR(c) << SK_R32_SHIFT) |
860              (SkColorGetG(c) << SK_G32_SHIFT) |
861              (SkColorGetB(c) << SK_B32_SHIFT);
862   }
863
864   FakeLayerTreeHostClient fake_client_;
865   LayerTreeSettings layer_tree_settings_;
866   scoped_ptr<MockLayerTreeHost> layer_tree_host_;
867 };
868
869 TEST_F(ScaledScrollbarLayerTestScaledRasterization, TestLostPrecisionInClip) {
870   // Try rasterization at coordinates and scale that caused problematic
871   // rounding and clipping errors.
872   // Vertical Scrollbars.
873   TestScale(gfx::Rect(1240, 0, 15, 1333), 2.7754839f);
874   TestScale(gfx::Rect(1240, 0, 15, 677), 2.46677136f);
875
876   // Horizontal Scrollbars.
877   TestScale(gfx::Rect(0, 1240, 1333, 15), 2.7754839f);
878   TestScale(gfx::Rect(0, 1240, 677, 15), 2.46677136f);
879 }
880
881 }  // namespace
882 }  // namespace cc