Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / cc / animation / scrollbar_animation_controller_thinning_unittest.cc
1 // Copyright 2013 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 "cc/animation/scrollbar_animation_controller_thinning.h"
6
7 #include "cc/layers/solid_color_scrollbar_layer_impl.h"
8 #include "cc/test/fake_impl_proxy.h"
9 #include "cc/test/fake_layer_tree_host_impl.h"
10 #include "cc/test/geometry_test_utils.h"
11 #include "cc/test/test_shared_bitmap_manager.h"
12 #include "cc/trees/layer_tree_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace cc {
16 namespace {
17
18 class ScrollbarAnimationControllerThinningTest
19     : public testing::Test,
20       public ScrollbarAnimationControllerClient {
21  public:
22   ScrollbarAnimationControllerThinningTest()
23       : host_impl_(&proxy_, &shared_bitmap_manager_) {}
24
25   void PostDelayedScrollbarFade(const base::Closure& start_fade,
26                                 base::TimeDelta delay) override {
27     start_fade_ = start_fade;
28   }
29   void SetNeedsScrollbarAnimationFrame() override {}
30
31  protected:
32   virtual void SetUp() {
33     scoped_ptr<LayerImpl> scroll_layer =
34         LayerImpl::Create(host_impl_.active_tree(), 1);
35     clip_layer_ = LayerImpl::Create(host_impl_.active_tree(), 3);
36     scroll_layer->SetScrollClipLayer(clip_layer_->id());
37     LayerImpl* scroll_layer_ptr = scroll_layer.get();
38     clip_layer_->AddChild(scroll_layer.Pass());
39
40     const int kId = 2;
41     const int kThumbThickness = 10;
42     const int kTrackStart = 0;
43     const bool kIsLeftSideVerticalScrollbar = false;
44     const bool kIsOverlayScrollbar = true;
45     scrollbar_layer_ =
46         SolidColorScrollbarLayerImpl::Create(host_impl_.active_tree(),
47                                              kId,
48                                              HORIZONTAL,
49                                              kThumbThickness,
50                                              kTrackStart,
51                                              kIsLeftSideVerticalScrollbar,
52                                              kIsOverlayScrollbar);
53
54     scrollbar_layer_->SetScrollLayerAndClipLayerByIds(scroll_layer_ptr->id(),
55                                                       clip_layer_->id());
56     clip_layer_->SetBounds(gfx::Size(100, 100));
57     scroll_layer_ptr->SetBounds(gfx::Size(200, 200));
58
59     scrollbar_controller_ = ScrollbarAnimationControllerThinning::Create(
60         scroll_layer_ptr,
61         this,
62         base::TimeDelta::FromSeconds(2),
63         base::TimeDelta::FromSeconds(5),
64         base::TimeDelta::FromSeconds(3));
65   }
66
67   FakeImplProxy proxy_;
68   TestSharedBitmapManager shared_bitmap_manager_;
69   FakeLayerTreeHostImpl host_impl_;
70   scoped_ptr<ScrollbarAnimationControllerThinning> scrollbar_controller_;
71   scoped_ptr<LayerImpl> clip_layer_;
72   scoped_ptr<SolidColorScrollbarLayerImpl> scrollbar_layer_;
73
74   base::Closure start_fade_;
75 };
76
77 // Check initialization of scrollbar.
78 TEST_F(ScrollbarAnimationControllerThinningTest, Idle) {
79   scrollbar_controller_->Animate(base::TimeTicks());
80   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
81   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
82 }
83
84 // Check that scrollbar disappears when the layer becomes non-scrollable.
85 TEST_F(ScrollbarAnimationControllerThinningTest, HideOnResize) {
86   LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
87   ASSERT_TRUE(scroll_layer);
88   EXPECT_SIZE_EQ(gfx::Size(200, 200), scroll_layer->bounds());
89
90   EXPECT_EQ(HORIZONTAL, scrollbar_layer_->orientation());
91
92   // Shrink along X axis, horizontal scrollbar should appear.
93   clip_layer_->SetBounds(gfx::Size(100, 200));
94   EXPECT_SIZE_EQ(gfx::Size(100, 200), clip_layer_->bounds());
95
96   scrollbar_controller_->DidScrollBegin();
97
98   scrollbar_controller_->DidScrollUpdate(false);
99   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
100
101   scrollbar_controller_->DidScrollEnd();
102
103   // Shrink along Y axis and expand along X, horizontal scrollbar
104   // should disappear.
105   clip_layer_->SetBounds(gfx::Size(200, 100));
106   EXPECT_SIZE_EQ(gfx::Size(200, 100), clip_layer_->bounds());
107
108   scrollbar_controller_->DidScrollBegin();
109
110   scrollbar_controller_->DidScrollUpdate(false);
111   EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());
112
113   scrollbar_controller_->DidScrollEnd();
114 }
115
116 // Scroll content. Confirm the scrollbar gets dark and then becomes light
117 // after stopping.
118 TEST_F(ScrollbarAnimationControllerThinningTest, AwakenByProgrammaticScroll) {
119   base::TimeTicks time;
120   time += base::TimeDelta::FromSeconds(1);
121   scrollbar_controller_->DidScrollUpdate(false);
122   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
123   // Scrollbar doesn't change size if triggered by scroll.
124   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
125
126   start_fade_.Run();
127
128   time += base::TimeDelta::FromSeconds(1);
129   scrollbar_controller_->Animate(time);
130   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
131   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
132
133   // Subsequent scroll restarts animation.
134   scrollbar_controller_->DidScrollUpdate(false);
135
136   start_fade_.Run();
137
138   time += base::TimeDelta::FromSeconds(2);
139   scrollbar_controller_->Animate(time);
140   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
141   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
142
143   time += base::TimeDelta::FromSeconds(1);
144   scrollbar_controller_->Animate(time);
145   EXPECT_FLOAT_EQ(0.9f, scrollbar_layer_->opacity());
146   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
147
148   time += base::TimeDelta::FromSeconds(1);
149   scrollbar_controller_->Animate(time);
150   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->opacity());
151   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
152
153   time += base::TimeDelta::FromSeconds(1);
154   scrollbar_controller_->Animate(time);
155   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
156   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
157 }
158
159 // Initiate a scroll when the pointer is already near the scrollbar. It should
160 // remain thick.
161 TEST_F(ScrollbarAnimationControllerThinningTest, ScrollWithMouseNear) {
162   base::TimeTicks time;
163   time += base::TimeDelta::FromSeconds(1);
164
165   scrollbar_controller_->DidMouseMoveNear(1);
166   scrollbar_controller_->Animate(time);
167   time += base::TimeDelta::FromSeconds(3);
168
169   scrollbar_controller_->Animate(time);
170   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
171
172   scrollbar_controller_->DidScrollUpdate(false);
173   start_fade_.Run();
174   scrollbar_controller_->Animate(time);
175   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
176   // Scrollbar should still be thick.
177   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
178
179   time += base::TimeDelta::FromSeconds(5);
180   scrollbar_controller_->Animate(time);
181   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
182   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
183 }
184
185 // Move the pointer near the scrollbar. Confirm it gets thick and narrow when
186 // moved away.
187 TEST_F(ScrollbarAnimationControllerThinningTest, MouseNear) {
188   base::TimeTicks time;
189   time += base::TimeDelta::FromSeconds(1);
190   scrollbar_controller_->DidMouseMoveNear(1);
191   scrollbar_controller_->Animate(time);
192   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
193   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
194
195   // Should animate to thickened but not darken.
196   time += base::TimeDelta::FromSeconds(1);
197   scrollbar_controller_->Animate(time);
198   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
199   EXPECT_FLOAT_EQ(0.6f, scrollbar_layer_->thumb_thickness_scale_factor());
200
201   time += base::TimeDelta::FromSeconds(1);
202   scrollbar_controller_->Animate(time);
203   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
204   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->thumb_thickness_scale_factor());
205
206   time += base::TimeDelta::FromSeconds(1);
207   scrollbar_controller_->Animate(time);
208   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
209   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
210
211   // Subsequent moves should not change anything.
212   scrollbar_controller_->DidMouseMoveNear(1);
213   scrollbar_controller_->Animate(time);
214   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
215   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
216
217   // Now move away from bar.
218   time += base::TimeDelta::FromSeconds(1);
219   scrollbar_controller_->DidMouseMoveNear(26);
220   scrollbar_controller_->Animate(time);
221   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
222   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
223
224   // Animate to narrow.
225   time += base::TimeDelta::FromSeconds(1);
226   scrollbar_controller_->Animate(time);
227   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
228   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->thumb_thickness_scale_factor());
229
230   time += base::TimeDelta::FromSeconds(1);
231   scrollbar_controller_->Animate(time);
232   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
233   EXPECT_FLOAT_EQ(0.6f, scrollbar_layer_->thumb_thickness_scale_factor());
234
235   time += base::TimeDelta::FromSeconds(1);
236   scrollbar_controller_->Animate(time);
237   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
238   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
239 }
240
241 // Move the pointer over the scrollbar. Make sure it gets thick and dark
242 // and that it gets thin and light when moved away.
243 TEST_F(ScrollbarAnimationControllerThinningTest, MouseOver) {
244   base::TimeTicks time;
245   time += base::TimeDelta::FromSeconds(1);
246   scrollbar_controller_->DidMouseMoveNear(0);
247   scrollbar_controller_->Animate(time);
248   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
249   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
250
251   // Should animate to thickened and darkened.
252   time += base::TimeDelta::FromSeconds(1);
253   scrollbar_controller_->Animate(time);
254   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->opacity());
255   EXPECT_FLOAT_EQ(0.6f, scrollbar_layer_->thumb_thickness_scale_factor());
256
257   time += base::TimeDelta::FromSeconds(1);
258   scrollbar_controller_->Animate(time);
259   EXPECT_FLOAT_EQ(0.9f, scrollbar_layer_->opacity());
260   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->thumb_thickness_scale_factor());
261
262   time += base::TimeDelta::FromSeconds(1);
263   scrollbar_controller_->Animate(time);
264   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
265   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
266
267   // Subsequent moves should not change anything.
268   scrollbar_controller_->DidMouseMoveNear(0);
269   scrollbar_controller_->Animate(time);
270   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
271   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
272
273   // Now move away from bar.
274   time += base::TimeDelta::FromSeconds(1);
275   scrollbar_controller_->DidMouseMoveNear(26);
276   scrollbar_controller_->Animate(time);
277   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
278   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
279
280   // Animate to narrow.
281   time += base::TimeDelta::FromSeconds(1);
282   scrollbar_controller_->Animate(time);
283   EXPECT_FLOAT_EQ(0.9f, scrollbar_layer_->opacity());
284   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->thumb_thickness_scale_factor());
285
286   time += base::TimeDelta::FromSeconds(1);
287   scrollbar_controller_->Animate(time);
288   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->opacity());
289   EXPECT_FLOAT_EQ(0.6f, scrollbar_layer_->thumb_thickness_scale_factor());
290
291   time += base::TimeDelta::FromSeconds(1);
292   scrollbar_controller_->Animate(time);
293   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
294   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
295 }
296
297 // First move the pointer near the scrollbar, then over it, then back near
298 // then far away. Confirm that first the bar gets thick, then dark, then light,
299 // then narrow.
300 TEST_F(ScrollbarAnimationControllerThinningTest, MouseNearThenOver) {
301   base::TimeTicks time;
302   time += base::TimeDelta::FromSeconds(1);
303   scrollbar_controller_->DidMouseMoveNear(1);
304   scrollbar_controller_->Animate(time);
305   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
306   EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
307
308   // Should animate to thickened but not darken.
309   time += base::TimeDelta::FromSeconds(3);
310   scrollbar_controller_->Animate(time);
311   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
312   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
313
314   // Now move over.
315   scrollbar_controller_->DidMouseMoveNear(0);
316   scrollbar_controller_->Animate(time);
317
318   // Should animate to darkened.
319   time += base::TimeDelta::FromSeconds(1);
320   scrollbar_controller_->Animate(time);
321   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->opacity());
322   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
323
324   time += base::TimeDelta::FromSeconds(1);
325   scrollbar_controller_->Animate(time);
326   EXPECT_FLOAT_EQ(0.9f, scrollbar_layer_->opacity());
327   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
328
329   time += base::TimeDelta::FromSeconds(1);
330   scrollbar_controller_->Animate(time);
331   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
332   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
333
334   // This is tricky. The DidMouseMoveOffScrollbar() is sent before the
335   // subsequent DidMouseMoveNear(), if the mouse moves in that direction.
336   // This results in the thumb thinning. We want to make sure that when the
337   // thumb starts expanding it doesn't first narrow to the idle thinness.
338   time += base::TimeDelta::FromSeconds(1);
339   scrollbar_controller_->DidMouseMoveOffScrollbar();
340   scrollbar_controller_->Animate(time);
341
342   time += base::TimeDelta::FromSeconds(1);
343   scrollbar_controller_->Animate(time);
344   EXPECT_FLOAT_EQ(0.9f, scrollbar_layer_->opacity());
345   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->thumb_thickness_scale_factor());
346
347   scrollbar_controller_->DidMouseMoveNear(1);
348   scrollbar_controller_->Animate(time);
349   // A new animation is kicked off.
350
351   time += base::TimeDelta::FromSeconds(1);
352   scrollbar_controller_->Animate(time);
353   // We will initiate the narrowing again, but it won't get decremented until
354   // the new animation catches up to it.
355   EXPECT_FLOAT_EQ(0.9f, scrollbar_layer_->opacity());
356   // Now the thickness should be increasing, but it shouldn't happen until the
357   // animation catches up.
358   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->thumb_thickness_scale_factor());
359
360   time += base::TimeDelta::FromSeconds(1);
361   scrollbar_controller_->Animate(time);
362   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->opacity());
363   // The thickness now gets big again.
364   EXPECT_FLOAT_EQ(0.8f, scrollbar_layer_->thumb_thickness_scale_factor());
365
366   time += base::TimeDelta::FromSeconds(1);
367   scrollbar_controller_->Animate(time);
368   EXPECT_FLOAT_EQ(0.7f, scrollbar_layer_->opacity());
369   // The thickness now gets big again.
370   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
371 }
372
373 }  // namespace
374 }  // namespace cc