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/animation.h"
6
7 #include "cc/test/animation_test_common.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace cc {
12 namespace {
13
14 using base::TimeDelta;
15
TicksFromSecondsF(double seconds)16 static base::TimeTicks TicksFromSecondsF(double seconds) {
17 return base::TimeTicks::FromInternalValue(seconds *
18 base::Time::kMicrosecondsPerSecond);
19 }
20
CreateAnimation(int iterations,double duration)21 scoped_ptr<Animation> CreateAnimation(int iterations, double duration) {
22 scoped_ptr<Animation> to_return(Animation::Create(
23 make_scoped_ptr(
24 new FakeFloatAnimationCurve(duration)).PassAs<AnimationCurve>(),
25 0,
26 1,
27 Animation::Opacity));
28 to_return->set_iterations(iterations);
29 return to_return.Pass();
30 }
31
CreateAnimation(int iterations)32 scoped_ptr<Animation> CreateAnimation(int iterations) {
33 return CreateAnimation(iterations, 1);
34 }
35
TEST(AnimationTest,TrimTimeZeroIterations)36 TEST(AnimationTest, TrimTimeZeroIterations) {
37 scoped_ptr<Animation> anim(CreateAnimation(0));
38 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
39 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
40 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
41 }
42
TEST(AnimationTest,TrimTimeOneIteration)43 TEST(AnimationTest, TrimTimeOneIteration) {
44 scoped_ptr<Animation> anim(CreateAnimation(1));
45 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
46 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
47 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
48 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
49 }
50
TEST(AnimationTest,TrimTimeInfiniteIterations)51 TEST(AnimationTest, TrimTimeInfiniteIterations) {
52 scoped_ptr<Animation> anim(CreateAnimation(-1));
53 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
54 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
55 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
56 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
57 }
58
TEST(AnimationTest,TrimTimeReverse)59 TEST(AnimationTest, TrimTimeReverse) {
60 scoped_ptr<Animation> anim(CreateAnimation(-1));
61 anim->set_direction(Animation::Reverse);
62 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0)));
63 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
64 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
65 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
66 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
67 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
68 }
69
TEST(AnimationTest,TrimTimeAlternateInfiniteIterations)70 TEST(AnimationTest, TrimTimeAlternateInfiniteIterations) {
71 scoped_ptr<Animation> anim(CreateAnimation(-1));
72 anim->set_direction(Animation::Alternate);
73 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
74 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
75 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
76 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
77 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
78 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
79 }
80
TEST(AnimationTest,TrimTimeAlternateOneIteration)81 TEST(AnimationTest, TrimTimeAlternateOneIteration) {
82 scoped_ptr<Animation> anim(CreateAnimation(1));
83 anim->set_direction(Animation::Alternate);
84 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
85 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
86 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
87 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
88 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
89 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
90 }
91
TEST(AnimationTest,TrimTimeAlternateTwoIterations)92 TEST(AnimationTest, TrimTimeAlternateTwoIterations) {
93 scoped_ptr<Animation> anim(CreateAnimation(2));
94 anim->set_direction(Animation::Alternate);
95 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
96 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
97 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
98 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
99 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
100 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
101 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
102 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
103 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
104 }
105
TEST(AnimationTest,TrimTimeAlternateReverseInfiniteIterations)106 TEST(AnimationTest, TrimTimeAlternateReverseInfiniteIterations) {
107 scoped_ptr<Animation> anim(CreateAnimation(-1));
108 anim->set_direction(Animation::AlternateReverse);
109 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
110 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
111 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
112 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
113 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
114 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
115 }
116
TEST(AnimationTest,TrimTimeAlternateReverseOneIteration)117 TEST(AnimationTest, TrimTimeAlternateReverseOneIteration) {
118 scoped_ptr<Animation> anim(CreateAnimation(1));
119 anim->set_direction(Animation::AlternateReverse);
120 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
121 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
122 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
123 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
124 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
125 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
126 }
127
TEST(AnimationTest,TrimTimeAlternateReverseTwoIterations)128 TEST(AnimationTest, TrimTimeAlternateReverseTwoIterations) {
129 scoped_ptr<Animation> anim(CreateAnimation(2));
130 anim->set_direction(Animation::AlternateReverse);
131 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
132 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
133 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
134 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
135 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
136 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
137 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
138 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
139 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
140 }
141
TEST(AnimationTest,TrimTimeStartTime)142 TEST(AnimationTest, TrimTimeStartTime) {
143 scoped_ptr<Animation> anim(CreateAnimation(1));
144 anim->set_start_time(TicksFromSecondsF(4));
145 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
146 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.0)));
147 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
148 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(5.0)));
149 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(6.0)));
150 }
151
TEST(AnimationTest,TrimTimeStartTimeReverse)152 TEST(AnimationTest, TrimTimeStartTimeReverse) {
153 scoped_ptr<Animation> anim(CreateAnimation(1));
154 anim->set_start_time(TicksFromSecondsF(4));
155 anim->set_direction(Animation::Reverse);
156 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
157 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.0)));
158 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
159 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(5.0)));
160 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(6.0)));
161 }
162
TEST(AnimationTest,TrimTimeTimeOffset)163 TEST(AnimationTest, TrimTimeTimeOffset) {
164 scoped_ptr<Animation> anim(CreateAnimation(1));
165 anim->set_time_offset(TimeDelta::FromMilliseconds(4000));
166 anim->set_start_time(TicksFromSecondsF(4));
167 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
168 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
169 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
170 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
171 }
172
TEST(AnimationTest,TrimTimeTimeOffsetReverse)173 TEST(AnimationTest, TrimTimeTimeOffsetReverse) {
174 scoped_ptr<Animation> anim(CreateAnimation(1));
175 anim->set_time_offset(TimeDelta::FromMilliseconds(4000));
176 anim->set_start_time(TicksFromSecondsF(4));
177 anim->set_direction(Animation::Reverse);
178 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
179 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
180 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
181 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
182 }
183
TEST(AnimationTest,TrimTimeNegativeTimeOffset)184 TEST(AnimationTest, TrimTimeNegativeTimeOffset) {
185 scoped_ptr<Animation> anim(CreateAnimation(1));
186 anim->set_time_offset(TimeDelta::FromMilliseconds(-4000));
187
188 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
189 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.0)));
190 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
191 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(5.0)));
192 }
193
TEST(AnimationTest,TrimTimeNegativeTimeOffsetReverse)194 TEST(AnimationTest, TrimTimeNegativeTimeOffsetReverse) {
195 scoped_ptr<Animation> anim(CreateAnimation(1));
196 anim->set_time_offset(TimeDelta::FromMilliseconds(-4000));
197 anim->set_direction(Animation::Reverse);
198
199 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
200 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.0)));
201 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
202 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(5.0)));
203 }
204
TEST(AnimationTest,TrimTimePauseResume)205 TEST(AnimationTest, TrimTimePauseResume) {
206 scoped_ptr<Animation> anim(CreateAnimation(1));
207 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
208 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
209 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
210 anim->SetRunState(Animation::Paused, TicksFromSecondsF(0.5));
211 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
212 anim->SetRunState(Animation::Running, TicksFromSecondsF(1024.0));
213 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
214 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.5)));
215 }
216
TEST(AnimationTest,TrimTimePauseResumeReverse)217 TEST(AnimationTest, TrimTimePauseResumeReverse) {
218 scoped_ptr<Animation> anim(CreateAnimation(1));
219 anim->set_direction(Animation::Reverse);
220 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
221 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
222 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
223 anim->SetRunState(Animation::Paused, TicksFromSecondsF(0.25));
224 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
225 anim->SetRunState(Animation::Running, TicksFromSecondsF(1024.0));
226 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
227 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.75)));
228 }
229
TEST(AnimationTest,TrimTimeSuspendResume)230 TEST(AnimationTest, TrimTimeSuspendResume) {
231 scoped_ptr<Animation> anim(CreateAnimation(1));
232 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
233 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
234 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
235 anim->Suspend(TicksFromSecondsF(0.5));
236 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
237 anim->Resume(TicksFromSecondsF(1024));
238 EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
239 EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.5)));
240 }
241
TEST(AnimationTest,TrimTimeSuspendResumeReverse)242 TEST(AnimationTest, TrimTimeSuspendResumeReverse) {
243 scoped_ptr<Animation> anim(CreateAnimation(1));
244 anim->set_direction(Animation::Reverse);
245 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
246 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
247 EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
248 anim->Suspend(TicksFromSecondsF(0.75));
249 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
250 anim->Resume(TicksFromSecondsF(1024));
251 EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
252 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.25)));
253 }
254
TEST(AnimationTest,TrimTimeZeroDuration)255 TEST(AnimationTest, TrimTimeZeroDuration) {
256 scoped_ptr<Animation> anim(CreateAnimation(0, 0));
257 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
258 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
259 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
260 EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
261 }
262
TEST(AnimationTest,TrimTimeStarting)263 TEST(AnimationTest, TrimTimeStarting) {
264 scoped_ptr<Animation> anim(CreateAnimation(1, 5.0));
265 anim->SetRunState(Animation::Starting, TicksFromSecondsF(0.0));
266 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
267 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
268 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
269 anim->set_time_offset(TimeDelta::FromMilliseconds(2000));
270 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
271 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
272 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
273 anim->set_start_time(TicksFromSecondsF(1.0));
274 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
275 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
276 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
277 EXPECT_EQ(3.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
278 }
279
TEST(AnimationTest,TrimTimeNeedsSynchronizedStartTime)280 TEST(AnimationTest, TrimTimeNeedsSynchronizedStartTime) {
281 scoped_ptr<Animation> anim(CreateAnimation(1, 5.0));
282 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
283 anim->set_needs_synchronized_start_time(true);
284 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
285 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
286 EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
287 anim->set_time_offset(TimeDelta::FromMilliseconds(2000));
288 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
289 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
290 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
291 anim->set_start_time(TicksFromSecondsF(1.0));
292 anim->set_needs_synchronized_start_time(false);
293 EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
294 EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
295 EXPECT_EQ(3.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
296 }
297
TEST(AnimationTest,IsFinishedAtZeroIterations)298 TEST(AnimationTest, IsFinishedAtZeroIterations) {
299 scoped_ptr<Animation> anim(CreateAnimation(0));
300 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
301 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(-1.0)));
302 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
303 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
304 }
305
TEST(AnimationTest,IsFinishedAtOneIteration)306 TEST(AnimationTest, IsFinishedAtOneIteration) {
307 scoped_ptr<Animation> anim(CreateAnimation(1));
308 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
309 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(-1.0)));
310 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
311 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
312 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(2.0)));
313 }
314
TEST(AnimationTest,IsFinishedAtInfiniteIterations)315 TEST(AnimationTest, IsFinishedAtInfiniteIterations) {
316 scoped_ptr<Animation> anim(CreateAnimation(-1));
317 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
318 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
319 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.5)));
320 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
321 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(1.5)));
322 }
323
TEST(AnimationTest,IsFinishedNegativeTimeOffset)324 TEST(AnimationTest, IsFinishedNegativeTimeOffset) {
325 scoped_ptr<Animation> anim(CreateAnimation(1));
326 anim->set_time_offset(TimeDelta::FromMilliseconds(-500));
327 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
328
329 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(-1.0)));
330 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
331 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.5)));
332 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
333 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(1.5)));
334 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(2.0)));
335 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(2.5)));
336 }
337
TEST(AnimationTest,IsFinishedPositiveTimeOffset)338 TEST(AnimationTest, IsFinishedPositiveTimeOffset) {
339 scoped_ptr<Animation> anim(CreateAnimation(1));
340 anim->set_time_offset(TimeDelta::FromMilliseconds(500));
341 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
342
343 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(-1.0)));
344 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
345 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.5)));
346 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
347 }
348
TEST(AnimationTest,IsFinishedAtNotRunning)349 TEST(AnimationTest, IsFinishedAtNotRunning) {
350 scoped_ptr<Animation> anim(CreateAnimation(0));
351 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
352 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
353 anim->SetRunState(Animation::Paused, TicksFromSecondsF(0.0));
354 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
355 anim->SetRunState(Animation::WaitingForTargetAvailability,
356 TicksFromSecondsF(0.0));
357 EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
358 anim->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
359 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
360 anim->SetRunState(Animation::Aborted, TicksFromSecondsF(0.0));
361 EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
362 }
363
TEST(AnimationTest,IsFinished)364 TEST(AnimationTest, IsFinished) {
365 scoped_ptr<Animation> anim(CreateAnimation(1));
366 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
367 EXPECT_FALSE(anim->is_finished());
368 anim->SetRunState(Animation::Paused, TicksFromSecondsF(0.0));
369 EXPECT_FALSE(anim->is_finished());
370 anim->SetRunState(Animation::WaitingForTargetAvailability,
371 TicksFromSecondsF(0.0));
372 EXPECT_FALSE(anim->is_finished());
373 anim->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
374 EXPECT_TRUE(anim->is_finished());
375 anim->SetRunState(Animation::Aborted, TicksFromSecondsF(0.0));
376 EXPECT_TRUE(anim->is_finished());
377 }
378
TEST(AnimationTest,IsFinishedNeedsSynchronizedStartTime)379 TEST(AnimationTest, IsFinishedNeedsSynchronizedStartTime) {
380 scoped_ptr<Animation> anim(CreateAnimation(1));
381 anim->SetRunState(Animation::Running, TicksFromSecondsF(2.0));
382 EXPECT_FALSE(anim->is_finished());
383 anim->SetRunState(Animation::Paused, TicksFromSecondsF(2.0));
384 EXPECT_FALSE(anim->is_finished());
385 anim->SetRunState(Animation::WaitingForTargetAvailability,
386 TicksFromSecondsF(2.0));
387 EXPECT_FALSE(anim->is_finished());
388 anim->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
389 EXPECT_TRUE(anim->is_finished());
390 anim->SetRunState(Animation::Aborted, TicksFromSecondsF(0.0));
391 EXPECT_TRUE(anim->is_finished());
392 }
393
TEST(AnimationTest,RunStateChangesIgnoredWhileSuspended)394 TEST(AnimationTest, RunStateChangesIgnoredWhileSuspended) {
395 scoped_ptr<Animation> anim(CreateAnimation(1));
396 anim->Suspend(TicksFromSecondsF(0));
397 EXPECT_EQ(Animation::Paused, anim->run_state());
398 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
399 EXPECT_EQ(Animation::Paused, anim->run_state());
400 anim->Resume(TicksFromSecondsF(0));
401 anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
402 EXPECT_EQ(Animation::Running, anim->run_state());
403 }
404
405 } // namespace
406 } // namespace cc
407