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/trees/layer_tree_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 class ScrollbarAnimationControllerLinearFadeTest
19 : public testing::Test
,
20 public ScrollbarAnimationControllerClient
{
22 ScrollbarAnimationControllerLinearFadeTest()
23 : host_impl_(&proxy_
, &shared_bitmap_manager_
), needs_frame_count_(0) {}
25 void PostDelayedScrollbarFade(const base::Closure
& start_fade
,
26 base::TimeDelta delay
) override
{
27 start_fade_
= start_fade
;
30 void SetNeedsScrollbarAnimationFrame() override
{ needs_frame_count_
++; }
33 virtual void SetUp() {
34 const int kThumbThickness
= 10;
35 const int kTrackStart
= 0;
36 const bool kIsLeftSideVerticalScrollbar
= false;
37 const bool kIsOverlayScrollbar
= true; // Allow opacity animations.
39 scoped_ptr
<LayerImpl
> scroll_layer
=
40 LayerImpl::Create(host_impl_
.active_tree(), 1);
42 SolidColorScrollbarLayerImpl::Create(host_impl_
.active_tree(),
47 kIsLeftSideVerticalScrollbar
,
49 clip_layer_
= LayerImpl::Create(host_impl_
.active_tree(), 3);
50 scroll_layer
->SetScrollClipLayer(clip_layer_
->id());
51 LayerImpl
* scroll_layer_ptr
= scroll_layer
.get();
52 clip_layer_
->AddChild(scroll_layer
.Pass());
54 scrollbar_layer_
->SetScrollLayerAndClipLayerByIds(scroll_layer_ptr
->id(),
56 clip_layer_
->SetBounds(gfx::Size(100, 100));
57 scroll_layer_ptr
->SetBounds(gfx::Size(200, 200));
59 scrollbar_controller_
= ScrollbarAnimationControllerLinearFade::Create(
62 base::TimeDelta::FromSeconds(2),
63 base::TimeDelta::FromSeconds(5),
64 base::TimeDelta::FromSeconds(3));
67 virtual ScrollbarOrientation
orientation() const { return HORIZONTAL
; }
70 TestSharedBitmapManager shared_bitmap_manager_
;
71 FakeLayerTreeHostImpl host_impl_
;
72 scoped_ptr
<ScrollbarAnimationControllerLinearFade
> scrollbar_controller_
;
73 scoped_ptr
<LayerImpl
> clip_layer_
;
74 scoped_ptr
<SolidColorScrollbarLayerImpl
> scrollbar_layer_
;
76 base::Closure start_fade_
;
77 base::TimeDelta delay_
;
78 int needs_frame_count_
;
81 class VerticalScrollbarAnimationControllerLinearFadeTest
82 : public ScrollbarAnimationControllerLinearFadeTest
{
84 ScrollbarOrientation
orientation() const override
{ return VERTICAL
; }
87 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, DelayAnimationOnResize
) {
88 scrollbar_layer_
->SetOpacity(0.0f
);
89 scrollbar_controller_
->DidScrollBegin();
90 scrollbar_controller_
->DidScrollUpdate(true);
91 scrollbar_controller_
->DidScrollEnd();
92 // Normal Animation delay of 2 seconds.
93 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
94 EXPECT_EQ(delay_
, base::TimeDelta::FromSeconds(2));
96 scrollbar_layer_
->SetOpacity(0.0f
);
97 scrollbar_controller_
->DidScrollUpdate(true);
98 // Delay animation on resize to 5 seconds.
99 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
100 EXPECT_EQ(delay_
, base::TimeDelta::FromSeconds(5));
103 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, HiddenInBegin
) {
104 scrollbar_layer_
->SetOpacity(0.0f
);
105 scrollbar_controller_
->Animate(base::TimeTicks());
106 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
107 EXPECT_EQ(0, needs_frame_count_
);
110 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
111 HiddenAfterNonScrollingGesture
) {
112 scrollbar_layer_
->SetOpacity(0.0f
);
113 scrollbar_controller_
->DidScrollBegin();
115 base::TimeTicks time
;
116 time
+= base::TimeDelta::FromSeconds(100);
117 scrollbar_controller_
->Animate(time
);
118 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
119 scrollbar_controller_
->DidScrollEnd();
121 EXPECT_TRUE(start_fade_
.Equals(base::Closure()));
123 time
+= base::TimeDelta::FromSeconds(100);
124 scrollbar_controller_
->Animate(time
);
125 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
127 EXPECT_EQ(0, needs_frame_count_
);
130 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, HideOnResize
) {
131 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
132 ASSERT_TRUE(scroll_layer
);
133 EXPECT_SIZE_EQ(gfx::Size(200, 200), scroll_layer
->bounds());
135 EXPECT_EQ(HORIZONTAL
, scrollbar_layer_
->orientation());
137 // Shrink along X axis, horizontal scrollbar should appear.
138 clip_layer_
->SetBounds(gfx::Size(100, 200));
139 EXPECT_SIZE_EQ(gfx::Size(100, 200), clip_layer_
->bounds());
141 scrollbar_controller_
->DidScrollBegin();
143 scrollbar_controller_
->DidScrollUpdate(false);
144 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
146 scrollbar_controller_
->DidScrollEnd();
148 // Shrink along Y axis and expand along X, horizontal scrollbar
150 clip_layer_
->SetBounds(gfx::Size(200, 100));
151 EXPECT_SIZE_EQ(gfx::Size(200, 100), clip_layer_
->bounds());
153 scrollbar_controller_
->DidScrollBegin();
155 scrollbar_controller_
->DidScrollUpdate(false);
156 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
158 scrollbar_controller_
->DidScrollEnd();
161 TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest
, HideOnResize
) {
162 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
163 ASSERT_TRUE(scroll_layer
);
164 EXPECT_SIZE_EQ(gfx::Size(200, 200), scroll_layer
->bounds());
166 EXPECT_EQ(VERTICAL
, scrollbar_layer_
->orientation());
168 // Shrink along X axis, vertical scrollbar should remain invisible.
169 clip_layer_
->SetBounds(gfx::Size(100, 200));
170 EXPECT_SIZE_EQ(gfx::Size(100, 200), clip_layer_
->bounds());
172 scrollbar_controller_
->DidScrollBegin();
174 scrollbar_controller_
->DidScrollUpdate(false);
175 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
177 scrollbar_controller_
->DidScrollEnd();
179 // Shrink along Y axis and expand along X, vertical scrollbar should appear.
180 clip_layer_
->SetBounds(gfx::Size(200, 100));
181 EXPECT_SIZE_EQ(gfx::Size(200, 100), clip_layer_
->bounds());
183 scrollbar_controller_
->DidScrollBegin();
185 scrollbar_controller_
->DidScrollUpdate(false);
186 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
188 scrollbar_controller_
->DidScrollEnd();
191 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
192 HideOnUserNonScrollableHorz
) {
193 EXPECT_EQ(HORIZONTAL
, scrollbar_layer_
->orientation());
195 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
196 ASSERT_TRUE(scroll_layer
);
197 scroll_layer
->set_user_scrollable_horizontal(false);
199 scrollbar_controller_
->DidScrollBegin();
201 scrollbar_controller_
->DidScrollUpdate(false);
202 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
204 scrollbar_controller_
->DidScrollEnd();
207 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
208 ShowOnUserNonScrollableVert
) {
209 EXPECT_EQ(HORIZONTAL
, scrollbar_layer_
->orientation());
211 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
212 ASSERT_TRUE(scroll_layer
);
213 scroll_layer
->set_user_scrollable_vertical(false);
215 scrollbar_controller_
->DidScrollBegin();
217 scrollbar_controller_
->DidScrollUpdate(false);
218 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
220 scrollbar_controller_
->DidScrollEnd();
223 TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest
,
224 HideOnUserNonScrollableVert
) {
225 EXPECT_EQ(VERTICAL
, scrollbar_layer_
->orientation());
227 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
228 ASSERT_TRUE(scroll_layer
);
229 scroll_layer
->set_user_scrollable_vertical(false);
231 scrollbar_controller_
->DidScrollBegin();
233 scrollbar_controller_
->DidScrollUpdate(false);
234 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
236 scrollbar_controller_
->DidScrollEnd();
239 TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest
,
240 ShowOnUserNonScrollableHorz
) {
241 EXPECT_EQ(VERTICAL
, scrollbar_layer_
->orientation());
243 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
244 ASSERT_TRUE(scroll_layer
);
245 scroll_layer
->set_user_scrollable_horizontal(false);
247 scrollbar_controller_
->DidScrollBegin();
249 scrollbar_controller_
->DidScrollUpdate(false);
250 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
252 scrollbar_controller_
->DidScrollEnd();
255 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, AwakenByScrollingGesture
) {
256 base::TimeTicks time
;
257 time
+= base::TimeDelta::FromSeconds(1);
258 scrollbar_controller_
->DidScrollBegin();
260 scrollbar_controller_
->DidScrollUpdate(false);
261 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
263 EXPECT_TRUE(start_fade_
.Equals(base::Closure()));
265 time
+= base::TimeDelta::FromSeconds(100);
266 scrollbar_controller_
->Animate(time
);
267 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
268 scrollbar_controller_
->DidScrollEnd();
271 time
+= base::TimeDelta::FromSeconds(2);
272 scrollbar_controller_
->Animate(time
);
273 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
275 time
+= base::TimeDelta::FromSeconds(1);
276 scrollbar_controller_
->Animate(time
);
277 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
279 time
+= base::TimeDelta::FromSeconds(1);
280 scrollbar_controller_
->Animate(time
);
281 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
283 time
+= base::TimeDelta::FromSeconds(1);
285 scrollbar_controller_
->DidScrollBegin();
286 scrollbar_controller_
->DidScrollUpdate(false);
287 scrollbar_controller_
->DidScrollEnd();
290 time
+= base::TimeDelta::FromSeconds(2);
291 scrollbar_controller_
->Animate(time
);
292 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
294 time
+= base::TimeDelta::FromSeconds(1);
295 scrollbar_controller_
->Animate(time
);
296 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
298 time
+= base::TimeDelta::FromSeconds(1);
299 scrollbar_controller_
->Animate(time
);
300 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
302 time
+= base::TimeDelta::FromSeconds(1);
303 scrollbar_controller_
->Animate(time
);
304 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
306 EXPECT_EQ(8, needs_frame_count_
);
309 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, AwakenByProgrammaticScroll
) {
310 base::TimeTicks time
;
311 time
+= base::TimeDelta::FromSeconds(1);
312 scrollbar_controller_
->DidScrollUpdate(false);
314 scrollbar_controller_
->Animate(time
);
315 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
317 time
+= base::TimeDelta::FromSeconds(1);
318 scrollbar_controller_
->Animate(time
);
319 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
320 scrollbar_controller_
->DidScrollUpdate(false);
323 time
+= base::TimeDelta::FromSeconds(1);
324 scrollbar_controller_
->Animate(time
);
325 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
327 time
+= base::TimeDelta::FromSeconds(1);
328 scrollbar_controller_
->Animate(time
);
329 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
331 time
+= base::TimeDelta::FromSeconds(1);
332 scrollbar_controller_
->Animate(time
);
333 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
335 time
+= base::TimeDelta::FromSeconds(1);
336 scrollbar_controller_
->DidScrollUpdate(false);
338 time
+= base::TimeDelta::FromSeconds(1);
339 scrollbar_controller_
->Animate(time
);
340 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
342 time
+= base::TimeDelta::FromSeconds(1);
343 scrollbar_controller_
->Animate(time
);
344 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
346 time
+= base::TimeDelta::FromSeconds(1);
347 scrollbar_controller_
->Animate(time
);
348 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
350 time
+= base::TimeDelta::FromSeconds(1);
351 scrollbar_controller_
->Animate(time
);
352 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
354 EXPECT_EQ(11, needs_frame_count_
);
357 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
358 AnimationPreservedByNonScrollingGesture
) {
359 base::TimeTicks time
;
360 time
+= base::TimeDelta::FromSeconds(1);
361 scrollbar_controller_
->DidScrollUpdate(false);
363 scrollbar_controller_
->Animate(time
);
364 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
366 time
+= base::TimeDelta::FromSeconds(1);
367 scrollbar_controller_
->Animate(time
);
368 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
370 scrollbar_controller_
->DidScrollBegin();
371 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
373 time
+= base::TimeDelta::FromSeconds(1);
374 scrollbar_controller_
->Animate(time
);
375 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
377 scrollbar_controller_
->DidScrollEnd();
378 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
380 time
+= base::TimeDelta::FromSeconds(1);
381 scrollbar_controller_
->Animate(time
);
382 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
384 scrollbar_controller_
->Animate(time
);
386 EXPECT_EQ(4, needs_frame_count_
);
389 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
390 AnimationOverriddenByScrollingGesture
) {
391 base::TimeTicks time
;
392 time
+= base::TimeDelta::FromSeconds(1);
393 scrollbar_controller_
->DidScrollUpdate(false);
395 scrollbar_controller_
->Animate(time
);
396 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
398 time
+= base::TimeDelta::FromSeconds(1);
399 scrollbar_controller_
->Animate(time
);
400 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
402 scrollbar_controller_
->DidScrollBegin();
403 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
405 time
+= base::TimeDelta::FromSeconds(1);
406 scrollbar_controller_
->Animate(time
);
407 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
409 time
+= base::TimeDelta::FromSeconds(1);
410 scrollbar_controller_
->DidScrollUpdate(false);
411 EXPECT_FLOAT_EQ(1, scrollbar_layer_
->opacity());
413 time
+= base::TimeDelta::FromSeconds(1);
414 scrollbar_controller_
->DidScrollEnd();
415 EXPECT_FLOAT_EQ(1, scrollbar_layer_
->opacity());