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/animation.h"
7 #include "cc/test/animation_test_common.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 scoped_ptr
<Animation
> CreateAnimation(int iterations
, double duration
) {
15 scoped_ptr
<Animation
> to_return(Animation::Create(
17 new FakeFloatAnimationCurve(duration
)).PassAs
<AnimationCurve
>(),
21 to_return
->set_iterations(iterations
);
22 return to_return
.Pass();
25 scoped_ptr
<Animation
> CreateAnimation(int iterations
) {
26 return CreateAnimation(iterations
, 1);
29 TEST(AnimationTest
, TrimTimeZeroIterations
) {
30 scoped_ptr
<Animation
> anim(CreateAnimation(0));
31 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(-1.0));
32 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
33 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(1.0));
36 TEST(AnimationTest
, TrimTimeOneIteration
) {
37 scoped_ptr
<Animation
> anim(CreateAnimation(1));
38 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(-1.0));
39 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
40 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(1.0));
41 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(2.0));
44 TEST(AnimationTest
, TrimTimeInfiniteIterations
) {
45 scoped_ptr
<Animation
> anim(CreateAnimation(-1));
46 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
47 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(0.5));
48 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(1.0));
49 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(1.5));
52 TEST(AnimationTest
, TrimTimeReverse
) {
53 scoped_ptr
<Animation
> anim(CreateAnimation(-1));
54 anim
->set_direction(Animation::Reverse
);
55 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(0));
56 EXPECT_EQ(0.75, anim
->TrimTimeToCurrentIteration(0.25));
57 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(0.5));
58 EXPECT_EQ(0.25, anim
->TrimTimeToCurrentIteration(0.75));
59 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(1.0));
60 EXPECT_EQ(0.75, anim
->TrimTimeToCurrentIteration(1.25));
63 TEST(AnimationTest
, TrimTimeAlternate
) {
64 scoped_ptr
<Animation
> anim(CreateAnimation(-1));
65 anim
->set_direction(Animation::Alternate
);
66 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
67 EXPECT_EQ(0.25, anim
->TrimTimeToCurrentIteration(0.25));
68 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(0.5));
69 EXPECT_EQ(0.75, anim
->TrimTimeToCurrentIteration(0.75));
70 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(1.0));
71 EXPECT_EQ(0.75, anim
->TrimTimeToCurrentIteration(1.25));
74 TEST(AnimationTest
, TrimTimeAlternateReverse
) {
75 scoped_ptr
<Animation
> anim(CreateAnimation(-1));
76 anim
->set_direction(Animation::AlternateReverse
);
77 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(0.0));
78 EXPECT_EQ(0.75, anim
->TrimTimeToCurrentIteration(0.25));
79 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(0.5));
80 EXPECT_EQ(0.25, anim
->TrimTimeToCurrentIteration(0.75));
81 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(1.0));
82 EXPECT_EQ(0.25, anim
->TrimTimeToCurrentIteration(1.25));
85 TEST(AnimationTest
, TrimTimeStartTime
) {
86 scoped_ptr
<Animation
> anim(CreateAnimation(1));
87 anim
->set_start_time(4);
88 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
89 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(4.0));
90 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(4.5));
91 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(5.0));
92 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(6.0));
95 TEST(AnimationTest
, TrimTimeStartTimeReverse
) {
96 scoped_ptr
<Animation
> anim(CreateAnimation(1));
97 anim
->set_start_time(4);
98 anim
->set_direction(Animation::Reverse
);
99 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
100 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(4.0));
101 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(4.5));
102 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(5.0));
103 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(6.0));
106 TEST(AnimationTest
, TrimTimeTimeOffset
) {
107 scoped_ptr
<Animation
> anim(CreateAnimation(1));
108 anim
->set_time_offset(4);
109 anim
->set_start_time(4);
110 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
111 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(0.5));
112 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(1.0));
113 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(1.0));
116 TEST(AnimationTest
, TrimTimeTimeOffsetReverse
) {
117 scoped_ptr
<Animation
> anim(CreateAnimation(1));
118 anim
->set_time_offset(4);
119 anim
->set_start_time(4);
120 anim
->set_direction(Animation::Reverse
);
121 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(0.0));
122 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(0.5));
123 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(1.0));
124 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(1.0));
127 TEST(AnimationTest
, TrimTimeNegativeTimeOffset
) {
128 scoped_ptr
<Animation
> anim(CreateAnimation(1));
129 anim
->set_time_offset(-4);
131 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
132 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(4.0));
133 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(4.5));
134 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(5.0));
137 TEST(AnimationTest
, TrimTimeNegativeTimeOffsetReverse
) {
138 scoped_ptr
<Animation
> anim(CreateAnimation(1));
139 anim
->set_time_offset(-4);
140 anim
->set_direction(Animation::Reverse
);
142 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
143 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(4.0));
144 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(4.5));
145 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(5.0));
148 TEST(AnimationTest
, TrimTimePauseResume
) {
149 scoped_ptr
<Animation
> anim(CreateAnimation(1));
150 anim
->SetRunState(Animation::Running
, 0.0);
151 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
152 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(0.5));
153 anim
->SetRunState(Animation::Paused
, 0.5);
154 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(1024.0));
155 anim
->SetRunState(Animation::Running
, 1024.0);
156 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(1024.0));
157 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(1024.5));
160 TEST(AnimationTest
, TrimTimePauseResumeReverse
) {
161 scoped_ptr
<Animation
> anim(CreateAnimation(1));
162 anim
->set_direction(Animation::Reverse
);
163 anim
->SetRunState(Animation::Running
, 0.0);
164 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(0.0));
165 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(0.5));
166 anim
->SetRunState(Animation::Paused
, 0.25);
167 EXPECT_EQ(0.75, anim
->TrimTimeToCurrentIteration(1024.0));
168 anim
->SetRunState(Animation::Running
, 1024.0);
169 EXPECT_EQ(0.75, anim
->TrimTimeToCurrentIteration(1024.0));
170 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(1024.75));
173 TEST(AnimationTest
, TrimTimeSuspendResume
) {
174 scoped_ptr
<Animation
> anim(CreateAnimation(1));
175 anim
->SetRunState(Animation::Running
, 0.0);
176 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
177 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(0.5));
179 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(1024.0));
181 EXPECT_EQ(0.5, anim
->TrimTimeToCurrentIteration(1024.0));
182 EXPECT_EQ(1, anim
->TrimTimeToCurrentIteration(1024.5));
185 TEST(AnimationTest
, TrimTimeSuspendResumeReverse
) {
186 scoped_ptr
<Animation
> anim(CreateAnimation(1));
187 anim
->set_direction(Animation::Reverse
);
188 anim
->SetRunState(Animation::Running
, 0.0);
189 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(0.0));
190 EXPECT_EQ(0.75, anim
->TrimTimeToCurrentIteration(0.25));
192 EXPECT_EQ(0.25, anim
->TrimTimeToCurrentIteration(1024.0));
194 EXPECT_EQ(0.25, anim
->TrimTimeToCurrentIteration(1024.0));
195 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(1024.25));
198 TEST(AnimationTest
, TrimTimeZeroDuration
) {
199 scoped_ptr
<Animation
> anim(CreateAnimation(0, 0));
200 anim
->SetRunState(Animation::Running
, 0.0);
201 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(-1.0));
202 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(0.0));
203 EXPECT_EQ(0, anim
->TrimTimeToCurrentIteration(1.0));
206 TEST(AnimationTest
, TrimTimeStarting
) {
207 scoped_ptr
<Animation
> anim(CreateAnimation(1, 5.0));
208 anim
->SetRunState(Animation::Starting
, 0.0);
209 EXPECT_EQ(0.0, anim
->TrimTimeToCurrentIteration(-1.0));
210 EXPECT_EQ(0.0, anim
->TrimTimeToCurrentIteration(0.0));
211 EXPECT_EQ(0.0, anim
->TrimTimeToCurrentIteration(1.0));
212 anim
->set_time_offset(2.0);
213 EXPECT_EQ(2.0, anim
->TrimTimeToCurrentIteration(-1.0));
214 EXPECT_EQ(2.0, anim
->TrimTimeToCurrentIteration(0.0));
215 EXPECT_EQ(2.0, anim
->TrimTimeToCurrentIteration(1.0));
216 anim
->set_start_time(1.0);
217 EXPECT_EQ(0.0, anim
->TrimTimeToCurrentIteration(-1.0));
218 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(0.0));
219 EXPECT_EQ(2.0, anim
->TrimTimeToCurrentIteration(1.0));
220 EXPECT_EQ(3.0, anim
->TrimTimeToCurrentIteration(2.0));
223 TEST(AnimationTest
, TrimTimeNeedsSynchronizedStartTime
) {
224 scoped_ptr
<Animation
> anim(CreateAnimation(1, 5.0));
225 anim
->SetRunState(Animation::Running
, 0.0);
226 anim
->set_needs_synchronized_start_time(true);
227 EXPECT_EQ(0.0, anim
->TrimTimeToCurrentIteration(-1.0));
228 EXPECT_EQ(0.0, anim
->TrimTimeToCurrentIteration(0.0));
229 EXPECT_EQ(0.0, anim
->TrimTimeToCurrentIteration(1.0));
230 anim
->set_time_offset(2.0);
231 EXPECT_EQ(2.0, anim
->TrimTimeToCurrentIteration(-1.0));
232 EXPECT_EQ(2.0, anim
->TrimTimeToCurrentIteration(0.0));
233 EXPECT_EQ(2.0, anim
->TrimTimeToCurrentIteration(1.0));
234 anim
->set_start_time(1.0);
235 anim
->set_needs_synchronized_start_time(false);
236 EXPECT_EQ(1.0, anim
->TrimTimeToCurrentIteration(0.0));
237 EXPECT_EQ(2.0, anim
->TrimTimeToCurrentIteration(1.0));
238 EXPECT_EQ(3.0, anim
->TrimTimeToCurrentIteration(2.0));
241 TEST(AnimationTest
, IsFinishedAtZeroIterations
) {
242 scoped_ptr
<Animation
> anim(CreateAnimation(0));
243 anim
->SetRunState(Animation::Running
, 0.0);
244 EXPECT_FALSE(anim
->IsFinishedAt(-1.0));
245 EXPECT_TRUE(anim
->IsFinishedAt(0.0));
246 EXPECT_TRUE(anim
->IsFinishedAt(1.0));
249 TEST(AnimationTest
, IsFinishedAtOneIteration
) {
250 scoped_ptr
<Animation
> anim(CreateAnimation(1));
251 anim
->SetRunState(Animation::Running
, 0.0);
252 EXPECT_FALSE(anim
->IsFinishedAt(-1.0));
253 EXPECT_FALSE(anim
->IsFinishedAt(0.0));
254 EXPECT_TRUE(anim
->IsFinishedAt(1.0));
255 EXPECT_TRUE(anim
->IsFinishedAt(2.0));
258 TEST(AnimationTest
, IsFinishedAtInfiniteIterations
) {
259 scoped_ptr
<Animation
> anim(CreateAnimation(-1));
260 anim
->SetRunState(Animation::Running
, 0.0);
261 EXPECT_FALSE(anim
->IsFinishedAt(0.0));
262 EXPECT_FALSE(anim
->IsFinishedAt(0.5));
263 EXPECT_FALSE(anim
->IsFinishedAt(1.0));
264 EXPECT_FALSE(anim
->IsFinishedAt(1.5));
267 TEST(AnimationTest
, IsFinishedNegativeTimeOffset
) {
268 scoped_ptr
<Animation
> anim(CreateAnimation(1));
269 anim
->set_time_offset(-0.5);
270 anim
->SetRunState(Animation::Running
, 0.0);
272 EXPECT_FALSE(anim
->IsFinishedAt(-1.0));
273 EXPECT_FALSE(anim
->IsFinishedAt(0.0));
274 EXPECT_FALSE(anim
->IsFinishedAt(0.5));
275 EXPECT_FALSE(anim
->IsFinishedAt(1.0));
276 EXPECT_TRUE(anim
->IsFinishedAt(1.5));
277 EXPECT_TRUE(anim
->IsFinishedAt(2.0));
278 EXPECT_TRUE(anim
->IsFinishedAt(2.5));
281 TEST(AnimationTest
, IsFinishedPositiveTimeOffset
) {
282 scoped_ptr
<Animation
> anim(CreateAnimation(1));
283 anim
->set_time_offset(0.5);
284 anim
->SetRunState(Animation::Running
, 0.0);
286 EXPECT_FALSE(anim
->IsFinishedAt(-1.0));
287 EXPECT_FALSE(anim
->IsFinishedAt(0.0));
288 EXPECT_TRUE(anim
->IsFinishedAt(0.5));
289 EXPECT_TRUE(anim
->IsFinishedAt(1.0));
292 TEST(AnimationTest
, IsFinishedAtNotRunning
) {
293 scoped_ptr
<Animation
> anim(CreateAnimation(0));
294 anim
->SetRunState(Animation::Running
, 0.0);
295 EXPECT_TRUE(anim
->IsFinishedAt(0.0));
296 anim
->SetRunState(Animation::Paused
, 0.0);
297 EXPECT_FALSE(anim
->IsFinishedAt(0.0));
298 anim
->SetRunState(Animation::WaitingForTargetAvailability
, 0.0);
299 EXPECT_FALSE(anim
->IsFinishedAt(0.0));
300 anim
->SetRunState(Animation::Finished
, 0.0);
301 EXPECT_TRUE(anim
->IsFinishedAt(0.0));
302 anim
->SetRunState(Animation::Aborted
, 0.0);
303 EXPECT_TRUE(anim
->IsFinishedAt(0.0));
306 TEST(AnimationTest
, IsFinished
) {
307 scoped_ptr
<Animation
> anim(CreateAnimation(1));
308 anim
->SetRunState(Animation::Running
, 0.0);
309 EXPECT_FALSE(anim
->is_finished());
310 anim
->SetRunState(Animation::Paused
, 0.0);
311 EXPECT_FALSE(anim
->is_finished());
312 anim
->SetRunState(Animation::WaitingForTargetAvailability
, 0.0);
313 EXPECT_FALSE(anim
->is_finished());
314 anim
->SetRunState(Animation::Finished
, 0.0);
315 EXPECT_TRUE(anim
->is_finished());
316 anim
->SetRunState(Animation::Aborted
, 0.0);
317 EXPECT_TRUE(anim
->is_finished());
320 TEST(AnimationTest
, IsFinishedNeedsSynchronizedStartTime
) {
321 scoped_ptr
<Animation
> anim(CreateAnimation(1));
322 anim
->SetRunState(Animation::Running
, 2.0);
323 EXPECT_FALSE(anim
->is_finished());
324 anim
->SetRunState(Animation::Paused
, 2.0);
325 EXPECT_FALSE(anim
->is_finished());
326 anim
->SetRunState(Animation::WaitingForTargetAvailability
, 2.0);
327 EXPECT_FALSE(anim
->is_finished());
328 anim
->SetRunState(Animation::Finished
, 0.0);
329 EXPECT_TRUE(anim
->is_finished());
330 anim
->SetRunState(Animation::Aborted
, 0.0);
331 EXPECT_TRUE(anim
->is_finished());
334 TEST(AnimationTest
, RunStateChangesIgnoredWhileSuspended
) {
335 scoped_ptr
<Animation
> anim(CreateAnimation(1));
337 EXPECT_EQ(Animation::Paused
, anim
->run_state());
338 anim
->SetRunState(Animation::Running
, 0.0);
339 EXPECT_EQ(Animation::Paused
, anim
->run_state());
341 anim
->SetRunState(Animation::Running
, 0.0);
342 EXPECT_EQ(Animation::Running
, anim
->run_state());