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 "cc/animation/scrollbar_animation_controller_linear_fade.h"
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/test/test_task_graph_runner.h"
13 #include "cc/trees/layer_tree_impl.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 class ScrollbarAnimationControllerLinearFadeTest
20 : public testing::Test
,
21 public ScrollbarAnimationControllerClient
{
23 ScrollbarAnimationControllerLinearFadeTest()
24 : host_impl_(&proxy_
, &shared_bitmap_manager_
, &task_graph_runner_
) {}
26 void StartAnimatingScrollbarAnimationController(
27 ScrollbarAnimationController
* controller
) override
{
30 void StopAnimatingScrollbarAnimationController(
31 ScrollbarAnimationController
* controller
) override
{
32 is_animating_
= false;
34 void PostDelayedScrollbarAnimationTask(const base::Closure
& start_fade
,
35 base::TimeDelta delay
) override
{
36 start_fade_
= start_fade
;
39 void SetNeedsRedrawForScrollbarAnimation() override
{
40 did_request_redraw_
= true;
44 void SetUp() override
{
45 const int kThumbThickness
= 10;
46 const int kTrackStart
= 0;
47 const bool kIsLeftSideVerticalScrollbar
= false;
48 const bool kIsOverlayScrollbar
= true; // Allow opacity animations.
50 scoped_ptr
<LayerImpl
> scroll_layer
=
51 LayerImpl::Create(host_impl_
.active_tree(), 1);
53 SolidColorScrollbarLayerImpl::Create(host_impl_
.active_tree(),
58 kIsLeftSideVerticalScrollbar
,
60 clip_layer_
= LayerImpl::Create(host_impl_
.active_tree(), 3);
61 scroll_layer
->SetScrollClipLayer(clip_layer_
->id());
62 LayerImpl
* scroll_layer_ptr
= scroll_layer
.get();
63 clip_layer_
->AddChild(scroll_layer
.Pass());
65 scrollbar_layer_
->SetScrollLayerAndClipLayerByIds(scroll_layer_ptr
->id(),
67 clip_layer_
->SetBounds(gfx::Size(100, 100));
68 scroll_layer_ptr
->SetBounds(gfx::Size(200, 200));
70 scrollbar_controller_
= ScrollbarAnimationControllerLinearFade::Create(
71 scroll_layer_ptr
, this, base::TimeDelta::FromSeconds(2),
72 base::TimeDelta::FromSeconds(5), base::TimeDelta::FromSeconds(3));
75 virtual ScrollbarOrientation
orientation() const { return HORIZONTAL
; }
78 TestSharedBitmapManager shared_bitmap_manager_
;
79 TestTaskGraphRunner task_graph_runner_
;
80 FakeLayerTreeHostImpl host_impl_
;
81 scoped_ptr
<ScrollbarAnimationControllerLinearFade
> scrollbar_controller_
;
82 scoped_ptr
<LayerImpl
> clip_layer_
;
83 scoped_ptr
<SolidColorScrollbarLayerImpl
> scrollbar_layer_
;
85 base::Closure start_fade_
;
86 base::TimeDelta delay_
;
88 bool did_request_redraw_
;
91 class VerticalScrollbarAnimationControllerLinearFadeTest
92 : public ScrollbarAnimationControllerLinearFadeTest
{
94 ScrollbarOrientation
orientation() const override
{ return VERTICAL
; }
97 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, DelayAnimationOnResize
) {
98 scrollbar_layer_
->SetOpacity(0.0f
);
99 scrollbar_controller_
->DidScrollBegin();
100 scrollbar_controller_
->DidScrollUpdate(true);
101 scrollbar_controller_
->DidScrollEnd();
102 // Normal Animation delay of 2 seconds.
103 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
104 EXPECT_EQ(delay_
, base::TimeDelta::FromSeconds(2));
106 scrollbar_layer_
->SetOpacity(0.0f
);
107 scrollbar_controller_
->DidScrollUpdate(true);
108 // Delay animation on resize to 5 seconds.
109 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
110 EXPECT_EQ(delay_
, base::TimeDelta::FromSeconds(5));
113 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, HiddenInBegin
) {
114 scrollbar_layer_
->SetOpacity(0.0f
);
115 scrollbar_controller_
->Animate(base::TimeTicks());
116 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
119 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
120 HiddenAfterNonScrollingGesture
) {
121 scrollbar_layer_
->SetOpacity(0.0f
);
122 scrollbar_controller_
->DidScrollBegin();
124 base::TimeTicks time
;
125 time
+= base::TimeDelta::FromSeconds(100);
126 scrollbar_controller_
->Animate(time
);
127 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
128 scrollbar_controller_
->DidScrollEnd();
130 EXPECT_TRUE(start_fade_
.Equals(base::Closure()));
132 time
+= base::TimeDelta::FromSeconds(100);
133 scrollbar_controller_
->Animate(time
);
134 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
137 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, HideOnResize
) {
138 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
139 ASSERT_TRUE(scroll_layer
);
140 EXPECT_EQ(gfx::Size(200, 200), scroll_layer
->bounds());
142 EXPECT_EQ(HORIZONTAL
, scrollbar_layer_
->orientation());
144 // Shrink along X axis, horizontal scrollbar should appear.
145 clip_layer_
->SetBounds(gfx::Size(100, 200));
146 EXPECT_EQ(gfx::Size(100, 200), clip_layer_
->bounds());
148 scrollbar_controller_
->DidScrollBegin();
150 scrollbar_controller_
->DidScrollUpdate(false);
151 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
153 scrollbar_controller_
->DidScrollEnd();
155 // Shrink along Y axis and expand along X, horizontal scrollbar
157 clip_layer_
->SetBounds(gfx::Size(200, 100));
158 EXPECT_EQ(gfx::Size(200, 100), clip_layer_
->bounds());
160 scrollbar_controller_
->DidScrollBegin();
162 scrollbar_controller_
->DidScrollUpdate(false);
163 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
165 scrollbar_controller_
->DidScrollEnd();
168 TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest
, HideOnResize
) {
169 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
170 ASSERT_TRUE(scroll_layer
);
171 EXPECT_EQ(gfx::Size(200, 200), scroll_layer
->bounds());
173 EXPECT_EQ(VERTICAL
, scrollbar_layer_
->orientation());
175 // Shrink along X axis, vertical scrollbar should remain invisible.
176 clip_layer_
->SetBounds(gfx::Size(100, 200));
177 EXPECT_EQ(gfx::Size(100, 200), clip_layer_
->bounds());
179 scrollbar_controller_
->DidScrollBegin();
181 scrollbar_controller_
->DidScrollUpdate(false);
182 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
184 scrollbar_controller_
->DidScrollEnd();
186 // Shrink along Y axis and expand along X, vertical scrollbar should appear.
187 clip_layer_
->SetBounds(gfx::Size(200, 100));
188 EXPECT_EQ(gfx::Size(200, 100), clip_layer_
->bounds());
190 scrollbar_controller_
->DidScrollBegin();
192 scrollbar_controller_
->DidScrollUpdate(false);
193 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
195 scrollbar_controller_
->DidScrollEnd();
198 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
199 HideOnUserNonScrollableHorz
) {
200 EXPECT_EQ(HORIZONTAL
, scrollbar_layer_
->orientation());
202 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
203 ASSERT_TRUE(scroll_layer
);
204 scroll_layer
->set_user_scrollable_horizontal(false);
206 scrollbar_controller_
->DidScrollBegin();
208 scrollbar_controller_
->DidScrollUpdate(false);
209 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
211 scrollbar_controller_
->DidScrollEnd();
214 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
215 ShowOnUserNonScrollableVert
) {
216 EXPECT_EQ(HORIZONTAL
, scrollbar_layer_
->orientation());
218 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
219 ASSERT_TRUE(scroll_layer
);
220 scroll_layer
->set_user_scrollable_vertical(false);
222 scrollbar_controller_
->DidScrollBegin();
224 scrollbar_controller_
->DidScrollUpdate(false);
225 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
227 scrollbar_controller_
->DidScrollEnd();
230 TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest
,
231 HideOnUserNonScrollableVert
) {
232 EXPECT_EQ(VERTICAL
, scrollbar_layer_
->orientation());
234 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
235 ASSERT_TRUE(scroll_layer
);
236 scroll_layer
->set_user_scrollable_vertical(false);
238 scrollbar_controller_
->DidScrollBegin();
240 scrollbar_controller_
->DidScrollUpdate(false);
241 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
243 scrollbar_controller_
->DidScrollEnd();
246 TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest
,
247 ShowOnUserNonScrollableHorz
) {
248 EXPECT_EQ(VERTICAL
, scrollbar_layer_
->orientation());
250 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
251 ASSERT_TRUE(scroll_layer
);
252 scroll_layer
->set_user_scrollable_horizontal(false);
254 scrollbar_controller_
->DidScrollBegin();
256 scrollbar_controller_
->DidScrollUpdate(false);
257 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
259 scrollbar_controller_
->DidScrollEnd();
262 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, AwakenByScrollingGesture
) {
263 base::TimeTicks time
;
264 time
+= base::TimeDelta::FromSeconds(1);
265 scrollbar_controller_
->DidScrollBegin();
267 scrollbar_controller_
->DidScrollUpdate(false);
268 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
270 EXPECT_TRUE(start_fade_
.Equals(base::Closure()));
272 time
+= base::TimeDelta::FromSeconds(100);
274 scrollbar_controller_
->Animate(time
);
275 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
276 scrollbar_controller_
->DidScrollEnd();
279 time
+= base::TimeDelta::FromSeconds(2);
280 EXPECT_TRUE(is_animating_
);
281 scrollbar_controller_
->Animate(time
);
282 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
284 time
+= base::TimeDelta::FromSeconds(1);
285 EXPECT_TRUE(is_animating_
);
286 scrollbar_controller_
->Animate(time
);
287 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
289 time
+= base::TimeDelta::FromSeconds(1);
290 EXPECT_TRUE(is_animating_
);
291 scrollbar_controller_
->Animate(time
);
292 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
294 time
+= base::TimeDelta::FromSeconds(1);
296 scrollbar_controller_
->DidScrollBegin();
297 scrollbar_controller_
->DidScrollUpdate(false);
298 scrollbar_controller_
->DidScrollEnd();
302 time
+= base::TimeDelta::FromSeconds(2);
303 EXPECT_TRUE(is_animating_
);
304 scrollbar_controller_
->Animate(time
);
305 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
307 time
+= base::TimeDelta::FromSeconds(1);
308 EXPECT_TRUE(is_animating_
);
309 scrollbar_controller_
->Animate(time
);
310 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
312 time
+= base::TimeDelta::FromSeconds(1);
313 EXPECT_TRUE(is_animating_
);
314 scrollbar_controller_
->Animate(time
);
315 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
317 time
+= base::TimeDelta::FromSeconds(1);
318 EXPECT_TRUE(is_animating_
);
319 scrollbar_controller_
->Animate(time
);
320 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
321 EXPECT_FALSE(is_animating_
);
324 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, AwakenByProgrammaticScroll
) {
325 base::TimeTicks time
;
326 time
+= base::TimeDelta::FromSeconds(1);
327 scrollbar_controller_
->DidScrollUpdate(false);
330 EXPECT_TRUE(is_animating_
);
331 scrollbar_controller_
->Animate(time
);
332 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
334 time
+= base::TimeDelta::FromSeconds(1);
335 EXPECT_TRUE(is_animating_
);
336 scrollbar_controller_
->Animate(time
);
337 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
338 scrollbar_controller_
->DidScrollUpdate(false);
341 time
+= base::TimeDelta::FromSeconds(2);
342 EXPECT_TRUE(is_animating_
);
343 scrollbar_controller_
->Animate(time
);
344 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
346 time
+= base::TimeDelta::FromSeconds(1);
347 EXPECT_TRUE(is_animating_
);
348 scrollbar_controller_
->Animate(time
);
349 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
351 time
+= base::TimeDelta::FromSeconds(1);
352 EXPECT_TRUE(is_animating_
);
353 scrollbar_controller_
->Animate(time
);
354 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
356 time
+= base::TimeDelta::FromSeconds(1);
357 scrollbar_controller_
->DidScrollUpdate(false);
359 time
+= base::TimeDelta::FromSeconds(1);
360 EXPECT_TRUE(is_animating_
);
361 scrollbar_controller_
->Animate(time
);
362 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
364 time
+= base::TimeDelta::FromSeconds(1);
365 EXPECT_TRUE(is_animating_
);
366 scrollbar_controller_
->Animate(time
);
367 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
369 time
+= base::TimeDelta::FromSeconds(1);
370 EXPECT_TRUE(is_animating_
);
371 scrollbar_controller_
->Animate(time
);
372 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
374 time
+= base::TimeDelta::FromSeconds(1);
375 EXPECT_TRUE(is_animating_
);
376 scrollbar_controller_
->Animate(time
);
377 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
378 EXPECT_FALSE(is_animating_
);
381 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
382 AnimationPreservedByNonScrollingGesture
) {
383 base::TimeTicks time
;
384 time
+= base::TimeDelta::FromSeconds(1);
385 scrollbar_controller_
->DidScrollUpdate(false);
387 EXPECT_TRUE(is_animating_
);
388 scrollbar_controller_
->Animate(time
);
389 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
391 time
+= base::TimeDelta::FromSeconds(1);
392 EXPECT_TRUE(is_animating_
);
393 scrollbar_controller_
->Animate(time
);
394 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
396 scrollbar_controller_
->DidScrollBegin();
397 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
399 time
+= base::TimeDelta::FromSeconds(1);
400 EXPECT_TRUE(is_animating_
);
401 scrollbar_controller_
->Animate(time
);
402 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
404 scrollbar_controller_
->DidScrollEnd();
405 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
407 time
+= base::TimeDelta::FromSeconds(1);
408 EXPECT_TRUE(is_animating_
);
409 scrollbar_controller_
->Animate(time
);
410 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
411 EXPECT_FALSE(is_animating_
);
414 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
415 AnimationOverriddenByScrollingGesture
) {
416 base::TimeTicks time
;
417 time
+= base::TimeDelta::FromSeconds(1);
418 scrollbar_controller_
->DidScrollUpdate(false);
420 EXPECT_TRUE(is_animating_
);
421 scrollbar_controller_
->Animate(time
);
422 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
424 time
+= base::TimeDelta::FromSeconds(1);
425 EXPECT_TRUE(is_animating_
);
426 scrollbar_controller_
->Animate(time
);
427 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
429 scrollbar_controller_
->DidScrollBegin();
430 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
432 time
+= base::TimeDelta::FromSeconds(1);
433 EXPECT_TRUE(is_animating_
);
434 scrollbar_controller_
->Animate(time
);
435 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
437 time
+= base::TimeDelta::FromSeconds(1);
438 scrollbar_controller_
->DidScrollUpdate(false);
439 EXPECT_FLOAT_EQ(1, scrollbar_layer_
->opacity());
441 time
+= base::TimeDelta::FromSeconds(1);
442 scrollbar_controller_
->DidScrollEnd();
443 EXPECT_FLOAT_EQ(1, scrollbar_layer_
->opacity());