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
11namespace cc {
12namespace {
13
14using base::TimeDelta;
15
16static base::TimeTicks TicksFromSecondsF(double seconds) {
17  return base::TimeTicks::FromInternalValue(seconds *
18                                            base::Time::kMicrosecondsPerSecond);
19}
20
21scoped_ptr<Animation> CreateAnimation(double iterations,
22                                      double duration,
23                                      double playback_rate) {
24  scoped_ptr<Animation> to_return(Animation::Create(
25      make_scoped_ptr(
26          new FakeFloatAnimationCurve(duration)).PassAs<AnimationCurve>(),
27      0,
28      1,
29      Animation::Opacity));
30  to_return->set_iterations(iterations);
31  to_return->set_playback_rate(playback_rate);
32  return to_return.Pass();
33}
34
35scoped_ptr<Animation> CreateAnimation(double iterations, double duration) {
36  return CreateAnimation(iterations, duration, 1);
37}
38
39scoped_ptr<Animation> CreateAnimation(double iterations) {
40  return CreateAnimation(iterations, 1, 1);
41}
42
43TEST(AnimationTest, TrimTimeZeroIterations) {
44  scoped_ptr<Animation> anim(CreateAnimation(0));
45  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
46  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
47  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
48}
49
50TEST(AnimationTest, TrimTimeOneIteration) {
51  scoped_ptr<Animation> anim(CreateAnimation(1));
52  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
53  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
54  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
55  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
56}
57
58TEST(AnimationTest, TrimTimeOneHalfIteration) {
59  scoped_ptr<Animation> anim(CreateAnimation(1.5));
60  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
61  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
62  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
63  EXPECT_EQ(0.9, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.9)));
64  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
65  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
66  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
67}
68
69TEST(AnimationTest, TrimTimeInfiniteIterations) {
70  scoped_ptr<Animation> anim(CreateAnimation(-1));
71  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
72  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
73  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
74  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
75}
76
77TEST(AnimationTest, TrimTimeReverse) {
78  scoped_ptr<Animation> anim(CreateAnimation(-1));
79  anim->set_direction(Animation::Reverse);
80  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0)));
81  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
82  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
83  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
84  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
85  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
86}
87
88TEST(AnimationTest, TrimTimeAlternateInfiniteIterations) {
89  scoped_ptr<Animation> anim(CreateAnimation(-1));
90  anim->set_direction(Animation::Alternate);
91  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
92  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
93  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
94  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
95  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
96  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
97}
98
99TEST(AnimationTest, TrimTimeAlternateOneIteration) {
100  scoped_ptr<Animation> anim(CreateAnimation(1));
101  anim->set_direction(Animation::Alternate);
102  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
103  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
104  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
105  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
106  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
107  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
108}
109
110TEST(AnimationTest, TrimTimeAlternateTwoIterations) {
111  scoped_ptr<Animation> anim(CreateAnimation(2));
112  anim->set_direction(Animation::Alternate);
113  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
114  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
115  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
116  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
117  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
118  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
119  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
120  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
121  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
122}
123
124TEST(AnimationTest, TrimTimeAlternateTwoHalfIterations) {
125  scoped_ptr<Animation> anim(CreateAnimation(2.5));
126  anim->set_direction(Animation::Alternate);
127  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
128  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
129  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
130  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
131  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
132  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
133  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
134  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
135  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
136  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.50)));
137  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.75)));
138}
139
140TEST(AnimationTest, TrimTimeAlternateReverseInfiniteIterations) {
141  scoped_ptr<Animation> anim(CreateAnimation(-1));
142  anim->set_direction(Animation::AlternateReverse);
143  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
144  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
145  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
146  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
147  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
148  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
149}
150
151TEST(AnimationTest, TrimTimeAlternateReverseOneIteration) {
152  scoped_ptr<Animation> anim(CreateAnimation(1));
153  anim->set_direction(Animation::AlternateReverse);
154  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
155  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
156  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
157  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
158  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
159  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
160}
161
162TEST(AnimationTest, TrimTimeAlternateReverseTwoIterations) {
163  scoped_ptr<Animation> anim(CreateAnimation(2));
164  anim->set_direction(Animation::AlternateReverse);
165  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
166  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
167  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
168  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
169  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
170  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
171  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
172  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
173  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
174}
175
176TEST(AnimationTest, TrimTimeStartTime) {
177  scoped_ptr<Animation> anim(CreateAnimation(1));
178  anim->set_start_time(TicksFromSecondsF(4));
179  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
180  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.0)));
181  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
182  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(5.0)));
183  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(6.0)));
184}
185
186TEST(AnimationTest, TrimTimeStartTimeReverse) {
187  scoped_ptr<Animation> anim(CreateAnimation(1));
188  anim->set_start_time(TicksFromSecondsF(4));
189  anim->set_direction(Animation::Reverse);
190  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
191  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.0)));
192  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
193  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(5.0)));
194  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(6.0)));
195}
196
197TEST(AnimationTest, TrimTimeTimeOffset) {
198  scoped_ptr<Animation> anim(CreateAnimation(1));
199  anim->set_time_offset(TimeDelta::FromMilliseconds(4000));
200  anim->set_start_time(TicksFromSecondsF(4));
201  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
202  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
203  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
204  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
205}
206
207TEST(AnimationTest, TrimTimeTimeOffsetReverse) {
208  scoped_ptr<Animation> anim(CreateAnimation(1));
209  anim->set_time_offset(TimeDelta::FromMilliseconds(4000));
210  anim->set_start_time(TicksFromSecondsF(4));
211  anim->set_direction(Animation::Reverse);
212  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
213  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
214  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
215  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
216}
217
218TEST(AnimationTest, TrimTimeNegativeTimeOffset) {
219  scoped_ptr<Animation> anim(CreateAnimation(1));
220  anim->set_time_offset(TimeDelta::FromMilliseconds(-4000));
221
222  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
223  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.0)));
224  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
225  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(5.0)));
226}
227
228TEST(AnimationTest, TrimTimeNegativeTimeOffsetReverse) {
229  scoped_ptr<Animation> anim(CreateAnimation(1));
230  anim->set_time_offset(TimeDelta::FromMilliseconds(-4000));
231  anim->set_direction(Animation::Reverse);
232
233  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
234  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.0)));
235  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
236  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(5.0)));
237}
238
239TEST(AnimationTest, TrimTimePauseResume) {
240  scoped_ptr<Animation> anim(CreateAnimation(1));
241  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
242  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
243  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
244  anim->SetRunState(Animation::Paused, TicksFromSecondsF(0.5));
245  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
246  anim->SetRunState(Animation::Running, TicksFromSecondsF(1024.0));
247  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
248  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.5)));
249}
250
251TEST(AnimationTest, TrimTimePauseResumeReverse) {
252  scoped_ptr<Animation> anim(CreateAnimation(1));
253  anim->set_direction(Animation::Reverse);
254  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
255  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
256  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
257  anim->SetRunState(Animation::Paused, TicksFromSecondsF(0.25));
258  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
259  anim->SetRunState(Animation::Running, TicksFromSecondsF(1024.0));
260  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
261  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.75)));
262}
263
264TEST(AnimationTest, TrimTimeSuspendResume) {
265  scoped_ptr<Animation> anim(CreateAnimation(1));
266  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
267  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
268  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
269  anim->Suspend(TicksFromSecondsF(0.5));
270  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
271  anim->Resume(TicksFromSecondsF(1024));
272  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
273  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.5)));
274}
275
276TEST(AnimationTest, TrimTimeSuspendResumeReverse) {
277  scoped_ptr<Animation> anim(CreateAnimation(1));
278  anim->set_direction(Animation::Reverse);
279  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
280  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
281  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
282  anim->Suspend(TicksFromSecondsF(0.75));
283  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
284  anim->Resume(TicksFromSecondsF(1024));
285  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.0)));
286  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1024.25)));
287}
288
289TEST(AnimationTest, TrimTimeZeroDuration) {
290  scoped_ptr<Animation> anim(CreateAnimation(0, 0));
291  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
292  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
293  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
294  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
295}
296
297TEST(AnimationTest, TrimTimeStarting) {
298  scoped_ptr<Animation> anim(CreateAnimation(1, 5.0));
299  anim->SetRunState(Animation::Starting, TicksFromSecondsF(0.0));
300  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
301  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
302  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
303  anim->set_time_offset(TimeDelta::FromMilliseconds(2000));
304  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
305  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
306  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
307  anim->set_start_time(TicksFromSecondsF(1.0));
308  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
309  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
310  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
311  EXPECT_EQ(3.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
312}
313
314TEST(AnimationTest, TrimTimeNeedsSynchronizedStartTime) {
315  scoped_ptr<Animation> anim(CreateAnimation(1, 5.0));
316  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
317  anim->set_needs_synchronized_start_time(true);
318  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
319  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
320  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
321  anim->set_time_offset(TimeDelta::FromMilliseconds(2000));
322  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
323  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
324  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
325  anim->set_start_time(TicksFromSecondsF(1.0));
326  anim->set_needs_synchronized_start_time(false);
327  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
328  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
329  EXPECT_EQ(3.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
330}
331
332TEST(AnimationTest, IsFinishedAtZeroIterations) {
333  scoped_ptr<Animation> anim(CreateAnimation(0));
334  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
335  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(-1.0)));
336  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
337  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
338}
339
340TEST(AnimationTest, IsFinishedAtOneIteration) {
341  scoped_ptr<Animation> anim(CreateAnimation(1));
342  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
343  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(-1.0)));
344  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
345  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
346  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(2.0)));
347}
348
349TEST(AnimationTest, IsFinishedAtInfiniteIterations) {
350  scoped_ptr<Animation> anim(CreateAnimation(-1));
351  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
352  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
353  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.5)));
354  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
355  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(1.5)));
356}
357
358TEST(AnimationTest, IsFinishedNegativeTimeOffset) {
359  scoped_ptr<Animation> anim(CreateAnimation(1));
360  anim->set_time_offset(TimeDelta::FromMilliseconds(-500));
361  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
362
363  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(-1.0)));
364  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
365  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.5)));
366  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
367  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(1.5)));
368  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(2.0)));
369  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(2.5)));
370}
371
372TEST(AnimationTest, IsFinishedPositiveTimeOffset) {
373  scoped_ptr<Animation> anim(CreateAnimation(1));
374  anim->set_time_offset(TimeDelta::FromMilliseconds(500));
375  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
376
377  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(-1.0)));
378  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
379  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.5)));
380  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(1.0)));
381}
382
383TEST(AnimationTest, IsFinishedAtNotRunning) {
384  scoped_ptr<Animation> anim(CreateAnimation(0));
385  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
386  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
387  anim->SetRunState(Animation::Paused, TicksFromSecondsF(0.0));
388  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
389  anim->SetRunState(Animation::WaitingForTargetAvailability,
390                    TicksFromSecondsF(0.0));
391  EXPECT_FALSE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
392  anim->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
393  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
394  anim->SetRunState(Animation::Aborted, TicksFromSecondsF(0.0));
395  EXPECT_TRUE(anim->IsFinishedAt(TicksFromSecondsF(0.0)));
396}
397
398TEST(AnimationTest, IsFinished) {
399  scoped_ptr<Animation> anim(CreateAnimation(1));
400  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
401  EXPECT_FALSE(anim->is_finished());
402  anim->SetRunState(Animation::Paused, TicksFromSecondsF(0.0));
403  EXPECT_FALSE(anim->is_finished());
404  anim->SetRunState(Animation::WaitingForTargetAvailability,
405                    TicksFromSecondsF(0.0));
406  EXPECT_FALSE(anim->is_finished());
407  anim->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
408  EXPECT_TRUE(anim->is_finished());
409  anim->SetRunState(Animation::Aborted, TicksFromSecondsF(0.0));
410  EXPECT_TRUE(anim->is_finished());
411}
412
413TEST(AnimationTest, IsFinishedNeedsSynchronizedStartTime) {
414  scoped_ptr<Animation> anim(CreateAnimation(1));
415  anim->SetRunState(Animation::Running, TicksFromSecondsF(2.0));
416  EXPECT_FALSE(anim->is_finished());
417  anim->SetRunState(Animation::Paused, TicksFromSecondsF(2.0));
418  EXPECT_FALSE(anim->is_finished());
419  anim->SetRunState(Animation::WaitingForTargetAvailability,
420                    TicksFromSecondsF(2.0));
421  EXPECT_FALSE(anim->is_finished());
422  anim->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
423  EXPECT_TRUE(anim->is_finished());
424  anim->SetRunState(Animation::Aborted, TicksFromSecondsF(0.0));
425  EXPECT_TRUE(anim->is_finished());
426}
427
428TEST(AnimationTest, RunStateChangesIgnoredWhileSuspended) {
429  scoped_ptr<Animation> anim(CreateAnimation(1));
430  anim->Suspend(TicksFromSecondsF(0));
431  EXPECT_EQ(Animation::Paused, anim->run_state());
432  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
433  EXPECT_EQ(Animation::Paused, anim->run_state());
434  anim->Resume(TicksFromSecondsF(0));
435  anim->SetRunState(Animation::Running, TicksFromSecondsF(0.0));
436  EXPECT_EQ(Animation::Running, anim->run_state());
437}
438
439TEST(AnimationTest, TrimTimePlaybackNormal) {
440  scoped_ptr<Animation> anim(CreateAnimation(1, 1, 1));
441  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
442  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
443  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
444  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
445  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
446}
447
448TEST(AnimationTest, TrimTimePlaybackSlow) {
449  scoped_ptr<Animation> anim(CreateAnimation(1, 1, 0.5));
450  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
451  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
452  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
453  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
454  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
455  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.0)));
456}
457
458TEST(AnimationTest, TrimTimePlaybackFast) {
459  scoped_ptr<Animation> anim(CreateAnimation(1, 4, 2));
460  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
461  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
462  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
463  EXPECT_EQ(2, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
464  EXPECT_EQ(3, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
465  EXPECT_EQ(4, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
466  EXPECT_EQ(4, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.5)));
467}
468
469TEST(AnimationTest, TrimTimePlaybackNormalReverse) {
470  scoped_ptr<Animation> anim(CreateAnimation(1, 2, -1));
471  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
472  EXPECT_EQ(2, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0)));
473  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
474  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
475  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
476  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
477  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.5)));
478}
479
480TEST(AnimationTest, TrimTimePlaybackSlowReverse) {
481  scoped_ptr<Animation> anim(CreateAnimation(1, 2, -0.5));
482  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
483  EXPECT_EQ(2, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0)));
484  EXPECT_EQ(1.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
485  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
486  EXPECT_EQ(1.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
487  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
488  EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.5)));
489  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3)));
490  EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.5)));
491  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4)));
492  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
493}
494
495TEST(AnimationTest, TrimTimePlaybackFastReverse) {
496  scoped_ptr<Animation> anim(CreateAnimation(1, 2, -2));
497  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
498  EXPECT_EQ(2, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0)));
499  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
500  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
501  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
502  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
503  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
504}
505
506TEST(AnimationTest, TrimTimePlaybackFastInfiniteIterations) {
507  scoped_ptr<Animation> anim(CreateAnimation(-1, 4, 4));
508  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
509  EXPECT_EQ(2, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
510  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
511  EXPECT_EQ(2, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
512  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1000.0)));
513  EXPECT_EQ(2, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1000.5)));
514}
515
516TEST(AnimationTest, TrimTimePlaybackNormalDoubleReverse) {
517  scoped_ptr<Animation> anim(CreateAnimation(1, 1, -1));
518  anim->set_direction(Animation::Reverse);
519  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
520  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
521  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
522  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
523  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
524}
525
526TEST(AnimationTest, TrimTimePlaybackFastDoubleReverse) {
527  scoped_ptr<Animation> anim(CreateAnimation(1, 4, -2));
528  anim->set_direction(Animation::Reverse);
529  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
530  EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
531  EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
532  EXPECT_EQ(2, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
533  EXPECT_EQ(3, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
534  EXPECT_EQ(4, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
535  EXPECT_EQ(4, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.5)));
536}
537
538TEST(AnimationTest, TrimTimeAlternateTwoIterationsPlaybackFast) {
539  scoped_ptr<Animation> anim(CreateAnimation(2, 2, 2));
540  anim->set_direction(Animation::Alternate);
541  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
542  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
543  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
544  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
545  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
546  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
547  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
548  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
549  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
550  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
551}
552
553TEST(AnimationTest, TrimTimeAlternateTwoIterationsPlaybackFastReverse) {
554  scoped_ptr<Animation> anim(CreateAnimation(2, 2, 2));
555  anim->set_direction(Animation::AlternateReverse);
556  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
557  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
558  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
559  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
560  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
561  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
562  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
563  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
564  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
565  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
566  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
567}
568
569TEST(AnimationTest, TrimTimeAlternateTwoIterationsPlaybackFastDoubleReverse) {
570  scoped_ptr<Animation> anim(CreateAnimation(2, 2, -2));
571  anim->set_direction(Animation::AlternateReverse);
572  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
573  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
574  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
575  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
576  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
577  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
578  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
579  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
580  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
581  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
582}
583
584TEST(AnimationTest,
585     TrimTimeAlternateReverseThreeIterationsPlaybackFastAlternateReverse) {
586  scoped_ptr<Animation> anim(CreateAnimation(3, 2, -2));
587  anim->set_direction(Animation::AlternateReverse);
588  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
589  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
590  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
591  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
592  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
593  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
594  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
595  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
596  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
597  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
598  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.5)));
599  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.75)));
600  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.0)));
601  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.25)));
602}
603
604TEST(AnimationTest,
605     TrimTimeAlternateReverseTwoIterationsPlaybackNormalAlternate) {
606  scoped_ptr<Animation> anim(CreateAnimation(2, 2, -1));
607  anim->set_direction(Animation::Alternate);
608  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
609  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
610  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
611  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
612  EXPECT_EQ(2.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
613  EXPECT_EQ(1.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.5)));
614  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.0)));
615  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.5)));
616  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.0)));
617  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(4.5)));
618}
619
620TEST(AnimationTest, TrimTimeIterationStart) {
621  scoped_ptr<Animation> anim(CreateAnimation(2, 1, 1));
622  anim->set_iteration_start(0.5);
623  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
624  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
625  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
626  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
627  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
628  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
629  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.5)));
630}
631
632TEST(AnimationTest, TrimTimeIterationStartAlternate) {
633  scoped_ptr<Animation> anim(CreateAnimation(2, 1, 1));
634  anim->set_direction(Animation::Alternate);
635  anim->set_iteration_start(0.3);
636  EXPECT_EQ(0.3, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
637  EXPECT_EQ(0.3, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
638  EXPECT_EQ(0.8, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
639  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.7)));
640  EXPECT_EQ(0.7, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
641  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.2)));
642  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.7)));
643}
644
645TEST(AnimationTest, TrimTimeIterationStartAlternateThreeIterations) {
646  scoped_ptr<Animation> anim(CreateAnimation(3, 1, 1));
647  anim->set_direction(Animation::Alternate);
648  anim->set_iteration_start(1);
649  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
650  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
651  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
652  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
653  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
654  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
655  EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.5)));
656  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.0)));
657  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.5)));
658}
659
660TEST(AnimationTest,
661     TrimTimeIterationStartAlternateThreeIterationsPlaybackReverse) {
662  scoped_ptr<Animation> anim(CreateAnimation(3, 1, -1));
663  anim->set_direction(Animation::Alternate);
664  anim->set_iteration_start(1);
665  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
666  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
667  EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
668  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.0)));
669  EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(3.5)));
670}
671
672TEST(AnimationTest, InEffectFillMode) {
673  scoped_ptr<Animation> anim(CreateAnimation(1));
674  anim->set_fill_mode(Animation::FillModeNone);
675  EXPECT_FALSE(anim->InEffect(TicksFromSecondsF(-1.0)));
676  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(0.0)));
677  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(1.0)));
678
679  anim->set_fill_mode(Animation::FillModeForwards);
680  EXPECT_FALSE(anim->InEffect(TicksFromSecondsF(-1.0)));
681  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(0.0)));
682  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(1.0)));
683
684  anim->set_fill_mode(Animation::FillModeBackwards);
685  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(-1.0)));
686  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(0.0)));
687  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(1.0)));
688
689  anim->set_fill_mode(Animation::FillModeBoth);
690  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(-1.0)));
691  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(0.0)));
692  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(1.0)));
693}
694
695TEST(AnimationTest, InEffectFillModePlayback) {
696  scoped_ptr<Animation> anim(CreateAnimation(1, 1, -1));
697  anim->set_fill_mode(Animation::FillModeNone);
698  EXPECT_FALSE(anim->InEffect(TicksFromSecondsF(-1.0)));
699  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(0.0)));
700  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(1.0)));
701
702  anim->set_fill_mode(Animation::FillModeForwards);
703  EXPECT_FALSE(anim->InEffect(TicksFromSecondsF(-1.0)));
704  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(0.0)));
705  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(1.0)));
706
707  anim->set_fill_mode(Animation::FillModeBackwards);
708  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(-1.0)));
709  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(0.0)));
710  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(1.0)));
711
712  anim->set_fill_mode(Animation::FillModeBoth);
713  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(-1.0)));
714  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(0.0)));
715  EXPECT_TRUE(anim->InEffect(TicksFromSecondsF(1.0)));
716}
717
718}  // namespace
719}  // namespace cc
720