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/test_shared_bitmap_manager.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 class ScrollbarAnimationControllerLinearFadeTest
17 : public testing::Test
,
18 public ScrollbarAnimationControllerClient
{
20 ScrollbarAnimationControllerLinearFadeTest()
21 : host_impl_(&proxy_
, &shared_bitmap_manager_
), needs_frame_count_(0) {}
23 virtual void PostDelayedScrollbarFade(const base::Closure
& start_fade
,
24 base::TimeDelta delay
) OVERRIDE
{
25 start_fade_
= start_fade
;
27 virtual void SetNeedsScrollbarAnimationFrame() OVERRIDE
{
32 virtual void SetUp() {
33 const int kThumbThickness
= 10;
34 const int kTrackStart
= 0;
35 const bool kIsLeftSideVerticalScrollbar
= false;
36 const bool kIsOverlayScrollbar
= true; // Allow opacity animations.
38 scoped_ptr
<LayerImpl
> scroll_layer
=
39 LayerImpl::Create(host_impl_
.active_tree(), 1);
41 SolidColorScrollbarLayerImpl::Create(host_impl_
.active_tree(),
46 kIsLeftSideVerticalScrollbar
,
48 clip_layer_
= LayerImpl::Create(host_impl_
.active_tree(), 3);
49 scroll_layer
->SetScrollClipLayer(clip_layer_
->id());
50 LayerImpl
* scroll_layer_ptr
= scroll_layer
.get();
51 clip_layer_
->AddChild(scroll_layer
.Pass());
53 scrollbar_layer_
->SetClipLayerById(clip_layer_
->id());
54 scrollbar_layer_
->SetScrollLayerById(scroll_layer_ptr
->id());
55 clip_layer_
->SetBounds(gfx::Size(100, 100));
56 scroll_layer_ptr
->SetBounds(gfx::Size(50, 50));
58 scrollbar_controller_
= ScrollbarAnimationControllerLinearFade::Create(
61 base::TimeDelta::FromSeconds(2),
62 base::TimeDelta::FromSeconds(3));
66 TestSharedBitmapManager shared_bitmap_manager_
;
67 FakeLayerTreeHostImpl host_impl_
;
68 scoped_ptr
<ScrollbarAnimationControllerLinearFade
> scrollbar_controller_
;
69 scoped_ptr
<LayerImpl
> clip_layer_
;
70 scoped_ptr
<SolidColorScrollbarLayerImpl
> scrollbar_layer_
;
72 base::Closure start_fade_
;
73 int needs_frame_count_
;
76 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, HiddenInBegin
) {
77 scrollbar_layer_
->SetOpacity(0.0f
);
78 scrollbar_controller_
->Animate(base::TimeTicks());
79 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
80 EXPECT_EQ(0, needs_frame_count_
);
83 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
84 HiddenAfterNonScrollingGesture
) {
85 scrollbar_layer_
->SetOpacity(0.0f
);
86 scrollbar_controller_
->DidScrollBegin();
89 time
+= base::TimeDelta::FromSeconds(100);
90 scrollbar_controller_
->Animate(time
);
91 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
92 scrollbar_controller_
->DidScrollEnd();
94 EXPECT_TRUE(start_fade_
.Equals(base::Closure()));
96 time
+= base::TimeDelta::FromSeconds(100);
97 scrollbar_controller_
->Animate(time
);
98 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
100 EXPECT_EQ(0, needs_frame_count_
);
103 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, AwakenByScrollingGesture
) {
104 base::TimeTicks time
;
105 time
+= base::TimeDelta::FromSeconds(1);
106 scrollbar_controller_
->DidScrollBegin();
108 scrollbar_controller_
->DidScrollUpdate();
109 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
111 EXPECT_TRUE(start_fade_
.Equals(base::Closure()));
113 time
+= base::TimeDelta::FromSeconds(100);
114 scrollbar_controller_
->Animate(time
);
115 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
116 scrollbar_controller_
->DidScrollEnd();
119 time
+= base::TimeDelta::FromSeconds(2);
120 scrollbar_controller_
->Animate(time
);
121 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
123 time
+= base::TimeDelta::FromSeconds(1);
124 scrollbar_controller_
->Animate(time
);
125 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
127 time
+= base::TimeDelta::FromSeconds(1);
128 scrollbar_controller_
->Animate(time
);
129 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
131 time
+= base::TimeDelta::FromSeconds(1);
133 scrollbar_controller_
->DidScrollBegin();
134 scrollbar_controller_
->DidScrollUpdate();
135 scrollbar_controller_
->DidScrollEnd();
138 time
+= base::TimeDelta::FromSeconds(2);
139 scrollbar_controller_
->Animate(time
);
140 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
142 time
+= base::TimeDelta::FromSeconds(1);
143 scrollbar_controller_
->Animate(time
);
144 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
146 time
+= base::TimeDelta::FromSeconds(1);
147 scrollbar_controller_
->Animate(time
);
148 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
150 time
+= base::TimeDelta::FromSeconds(1);
151 scrollbar_controller_
->Animate(time
);
152 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
154 EXPECT_EQ(8, needs_frame_count_
);
157 TEST_F(ScrollbarAnimationControllerLinearFadeTest
, AwakenByProgrammaticScroll
) {
158 base::TimeTicks time
;
159 time
+= base::TimeDelta::FromSeconds(1);
160 scrollbar_controller_
->DidScrollUpdate();
162 scrollbar_controller_
->Animate(time
);
163 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
165 time
+= base::TimeDelta::FromSeconds(1);
166 scrollbar_controller_
->Animate(time
);
167 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
168 scrollbar_controller_
->DidScrollUpdate();
171 time
+= base::TimeDelta::FromSeconds(1);
172 scrollbar_controller_
->Animate(time
);
173 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
175 time
+= base::TimeDelta::FromSeconds(1);
176 scrollbar_controller_
->Animate(time
);
177 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
179 time
+= base::TimeDelta::FromSeconds(1);
180 scrollbar_controller_
->Animate(time
);
181 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
183 time
+= base::TimeDelta::FromSeconds(1);
184 scrollbar_controller_
->DidScrollUpdate();
186 time
+= base::TimeDelta::FromSeconds(1);
187 scrollbar_controller_
->Animate(time
);
188 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
190 time
+= base::TimeDelta::FromSeconds(1);
191 scrollbar_controller_
->Animate(time
);
192 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
194 time
+= base::TimeDelta::FromSeconds(1);
195 scrollbar_controller_
->Animate(time
);
196 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
198 time
+= base::TimeDelta::FromSeconds(1);
199 scrollbar_controller_
->Animate(time
);
200 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
202 EXPECT_EQ(11, needs_frame_count_
);
205 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
206 AnimationPreservedByNonScrollingGesture
) {
207 base::TimeTicks time
;
208 time
+= base::TimeDelta::FromSeconds(1);
209 scrollbar_controller_
->DidScrollUpdate();
211 scrollbar_controller_
->Animate(time
);
212 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
214 time
+= base::TimeDelta::FromSeconds(1);
215 scrollbar_controller_
->Animate(time
);
216 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
218 scrollbar_controller_
->DidScrollBegin();
219 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
221 time
+= base::TimeDelta::FromSeconds(1);
222 scrollbar_controller_
->Animate(time
);
223 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
225 scrollbar_controller_
->DidScrollEnd();
226 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
228 time
+= base::TimeDelta::FromSeconds(1);
229 scrollbar_controller_
->Animate(time
);
230 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
232 scrollbar_controller_
->Animate(time
);
234 EXPECT_EQ(4, needs_frame_count_
);
237 TEST_F(ScrollbarAnimationControllerLinearFadeTest
,
238 AnimationOverriddenByScrollingGesture
) {
239 base::TimeTicks time
;
240 time
+= base::TimeDelta::FromSeconds(1);
241 scrollbar_controller_
->DidScrollUpdate();
243 scrollbar_controller_
->Animate(time
);
244 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
246 time
+= base::TimeDelta::FromSeconds(1);
247 scrollbar_controller_
->Animate(time
);
248 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
250 scrollbar_controller_
->DidScrollBegin();
251 EXPECT_FLOAT_EQ(2.0f
/ 3.0f
, scrollbar_layer_
->opacity());
253 time
+= base::TimeDelta::FromSeconds(1);
254 scrollbar_controller_
->Animate(time
);
255 EXPECT_FLOAT_EQ(1.0f
/ 3.0f
, scrollbar_layer_
->opacity());
257 time
+= base::TimeDelta::FromSeconds(1);
258 scrollbar_controller_
->DidScrollUpdate();
259 EXPECT_FLOAT_EQ(1, scrollbar_layer_
->opacity());
261 time
+= base::TimeDelta::FromSeconds(1);
262 scrollbar_controller_
->DidScrollEnd();
263 EXPECT_FLOAT_EQ(1, scrollbar_layer_
->opacity());