- add sources.
[platform/framework/web/crosswalk.git] / src / cc / animation / scrollbar_animation_controller_linear_fade_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 "cc/animation/scrollbar_animation_controller_linear_fade.h"
6
7 #include "cc/layers/painted_scrollbar_layer_impl.h"
8 #include "cc/test/fake_impl_proxy.h"
9 #include "cc/test/fake_layer_tree_host_impl.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace cc {
13 namespace {
14
15 class ScrollbarAnimationControllerLinearFadeTest : public testing::Test {
16  public:
17   ScrollbarAnimationControllerLinearFadeTest() : host_impl_(&proxy_) {}
18
19  protected:
20   virtual void SetUp() {
21     scroll_layer_ = LayerImpl::Create(host_impl_.active_tree(), 1);
22     scrollbar_layer_ = PaintedScrollbarLayerImpl::Create(
23         host_impl_.active_tree(), 2, HORIZONTAL);
24
25     scroll_layer_->SetMaxScrollOffset(gfx::Vector2d(50, 50));
26     scroll_layer_->SetBounds(gfx::Size(50, 50));
27     scroll_layer_->SetHorizontalScrollbarLayer(scrollbar_layer_.get());
28
29     scrollbar_controller_ = ScrollbarAnimationControllerLinearFade::Create(
30         scroll_layer_.get(),
31         base::TimeDelta::FromSeconds(2),
32         base::TimeDelta::FromSeconds(3));
33   }
34
35   FakeImplProxy proxy_;
36   FakeLayerTreeHostImpl host_impl_;
37   scoped_ptr<ScrollbarAnimationControllerLinearFade> scrollbar_controller_;
38   scoped_ptr<LayerImpl> scroll_layer_;
39   scoped_ptr<PaintedScrollbarLayerImpl> scrollbar_layer_;
40 };
41
42 TEST_F(ScrollbarAnimationControllerLinearFadeTest, HiddenInBegin) {
43   scrollbar_controller_->Animate(base::TimeTicks());
44   EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());
45 }
46
47 TEST_F(ScrollbarAnimationControllerLinearFadeTest,
48        HiddenAfterNonScrollingGesture) {
49   scrollbar_controller_->DidScrollGestureBegin();
50   EXPECT_FALSE(scrollbar_controller_->IsAnimating());
51   EXPECT_FALSE(scrollbar_controller_->Animate(base::TimeTicks()));
52   EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());
53
54   base::TimeTicks time;
55   time += base::TimeDelta::FromSeconds(100);
56   EXPECT_FALSE(scrollbar_controller_->Animate(time));
57   EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());
58   scrollbar_controller_->DidScrollGestureEnd(time);
59
60   time += base::TimeDelta::FromSeconds(100);
61   EXPECT_FALSE(scrollbar_controller_->IsAnimating());
62   EXPECT_FALSE(scrollbar_controller_->Animate(time));
63   EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());
64 }
65
66 TEST_F(ScrollbarAnimationControllerLinearFadeTest, AwakenByScrollingGesture) {
67   base::TimeTicks time;
68   time += base::TimeDelta::FromSeconds(1);
69   scrollbar_controller_->DidScrollGestureBegin();
70   scrollbar_controller_->Animate(time);
71   EXPECT_FALSE(scrollbar_controller_->IsAnimating());
72   EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());
73
74   EXPECT_FALSE(scrollbar_controller_->DidScrollUpdate(time));
75   EXPECT_FALSE(scrollbar_controller_->IsAnimating());
76   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
77
78   time += base::TimeDelta::FromSeconds(100);
79   scrollbar_controller_->Animate(time);
80   EXPECT_FALSE(scrollbar_controller_->IsAnimating());
81   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
82   scrollbar_controller_->DidScrollGestureEnd(time);
83
84   EXPECT_TRUE(scrollbar_controller_->IsAnimating());
85   EXPECT_EQ(2, scrollbar_controller_->DelayBeforeStart(time).InSeconds());
86
87   time += base::TimeDelta::FromSeconds(1);
88   scrollbar_controller_->Animate(time);
89   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
90
91   time += base::TimeDelta::FromSeconds(1);
92   scrollbar_controller_->Animate(time);
93   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
94
95   time += base::TimeDelta::FromSeconds(1);
96   scrollbar_controller_->Animate(time);
97   EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->opacity());
98
99   time += base::TimeDelta::FromSeconds(1);
100   scrollbar_controller_->Animate(time);
101   EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->opacity());
102
103   time += base::TimeDelta::FromSeconds(1);
104
105   scrollbar_controller_->DidScrollGestureBegin();
106   EXPECT_FALSE(scrollbar_controller_->DidScrollUpdate(time));
107   scrollbar_controller_->DidScrollGestureEnd(time);
108
109   time += base::TimeDelta::FromSeconds(1);
110   scrollbar_controller_->Animate(time);
111   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
112
113   time += base::TimeDelta::FromSeconds(1);
114   scrollbar_controller_->Animate(time);
115   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
116
117   time += base::TimeDelta::FromSeconds(1);
118   scrollbar_controller_->Animate(time);
119   EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->opacity());
120
121   time += base::TimeDelta::FromSeconds(1);
122   scrollbar_controller_->Animate(time);
123   EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->opacity());
124
125   time += base::TimeDelta::FromSeconds(1);
126   scrollbar_controller_->Animate(time);
127   EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());
128 }
129
130 TEST_F(ScrollbarAnimationControllerLinearFadeTest, AwakenByProgrammaticScroll) {
131   base::TimeTicks time;
132   time += base::TimeDelta::FromSeconds(1);
133   EXPECT_TRUE(scrollbar_controller_->DidScrollUpdate(time));
134   EXPECT_TRUE(scrollbar_controller_->IsAnimating());
135   EXPECT_EQ(2, scrollbar_controller_->DelayBeforeStart(time).InSeconds());
136   scrollbar_controller_->Animate(time);
137   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
138
139   time += base::TimeDelta::FromSeconds(1);
140   scrollbar_controller_->Animate(time);
141   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
142   EXPECT_TRUE(scrollbar_controller_->DidScrollUpdate(time));
143
144   time += base::TimeDelta::FromSeconds(1);
145   scrollbar_controller_->Animate(time);
146   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
147
148   time += base::TimeDelta::FromSeconds(1);
149   scrollbar_controller_->Animate(time);
150   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
151
152   time += base::TimeDelta::FromSeconds(1);
153   scrollbar_controller_->Animate(time);
154   EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->opacity());
155
156   time += base::TimeDelta::FromSeconds(1);
157   scrollbar_controller_->Animate(time);
158   EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->opacity());
159
160   time += base::TimeDelta::FromSeconds(1);
161   EXPECT_TRUE(scrollbar_controller_->DidScrollUpdate(time));
162   time += base::TimeDelta::FromSeconds(1);
163   scrollbar_controller_->Animate(time);
164   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
165
166   time += base::TimeDelta::FromSeconds(1);
167   scrollbar_controller_->Animate(time);
168   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
169
170   time += base::TimeDelta::FromSeconds(1);
171   scrollbar_controller_->Animate(time);
172   EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->opacity());
173
174   time += base::TimeDelta::FromSeconds(1);
175   scrollbar_controller_->Animate(time);
176   EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->opacity());
177
178   time += base::TimeDelta::FromSeconds(1);
179   scrollbar_controller_->Animate(time);
180   EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());
181 }
182
183 TEST_F(ScrollbarAnimationControllerLinearFadeTest,
184        AnimationPreservedByNonScrollingGesture) {
185   base::TimeTicks time;
186   time += base::TimeDelta::FromSeconds(1);
187   EXPECT_TRUE(scrollbar_controller_->DidScrollUpdate(time));
188   EXPECT_TRUE(scrollbar_controller_->IsAnimating());
189   scrollbar_controller_->Animate(time);
190   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
191
192   time += base::TimeDelta::FromSeconds(3);
193   scrollbar_controller_->Animate(time);
194   EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->opacity());
195
196   scrollbar_controller_->DidScrollGestureBegin();
197   EXPECT_TRUE(scrollbar_controller_->IsAnimating());
198   EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->opacity());
199
200   time += base::TimeDelta::FromSeconds(1);
201   scrollbar_controller_->Animate(time);
202   EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->opacity());
203
204   scrollbar_controller_->DidScrollGestureEnd(time);
205   EXPECT_TRUE(scrollbar_controller_->IsAnimating());
206   EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->opacity());
207
208   time += base::TimeDelta::FromSeconds(1);
209   EXPECT_FALSE(scrollbar_controller_->Animate(time));
210   EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());
211 }
212
213 TEST_F(ScrollbarAnimationControllerLinearFadeTest,
214        AnimationOverriddenByScrollingGesture) {
215   base::TimeTicks time;
216   time += base::TimeDelta::FromSeconds(1);
217   EXPECT_TRUE(scrollbar_controller_->DidScrollUpdate(time));
218   EXPECT_TRUE(scrollbar_controller_->IsAnimating());
219   scrollbar_controller_->Animate(time);
220   EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());
221
222   time += base::TimeDelta::FromSeconds(3);
223   scrollbar_controller_->Animate(time);
224   EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->opacity());
225
226   scrollbar_controller_->DidScrollGestureBegin();
227   EXPECT_TRUE(scrollbar_controller_->IsAnimating());
228   EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->opacity());
229
230   time += base::TimeDelta::FromSeconds(1);
231   scrollbar_controller_->Animate(time);
232   EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->opacity());
233
234   time += base::TimeDelta::FromSeconds(1);
235   EXPECT_FALSE(scrollbar_controller_->DidScrollUpdate(time));
236   EXPECT_FALSE(scrollbar_controller_->IsAnimating());
237   EXPECT_FLOAT_EQ(1, scrollbar_layer_->opacity());
238
239   time += base::TimeDelta::FromSeconds(1);
240   scrollbar_controller_->DidScrollGestureEnd(time);
241   EXPECT_TRUE(scrollbar_controller_->IsAnimating());
242   EXPECT_FLOAT_EQ(1, scrollbar_layer_->opacity());
243 }
244
245 }  // namespace
246 }  // namespace cc