Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / TimedItemTest.cpp
1 /*
2  * Copyright (c) 2013, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "core/animation/TimedItem.h"
33
34 #include <gtest/gtest.h>
35
36 using namespace WebCore;
37
38 namespace {
39
40 class TestTimedItemEventDelegate : public TimedItem::EventDelegate {
41 public:
42     virtual void onEventCondition(const TimedItem* timedItem, bool isFirstSample, TimedItem::Phase previousPhase, double previousIteration) OVERRIDE
43     {
44         m_eventTriggered = true;
45         m_phaseChanged = previousPhase != timedItem->phase();
46         m_iterationChanged = previousIteration != timedItem->currentIteration();
47
48     }
49     void reset()
50     {
51         m_eventTriggered = false;
52         m_phaseChanged = false;
53         m_iterationChanged = false;
54     }
55     bool eventTriggered() { return m_eventTriggered; }
56     bool phaseChanged() { return m_phaseChanged; }
57     bool iterationChanged() { return m_iterationChanged; }
58
59 private:
60     bool m_eventTriggered;
61     bool m_phaseChanged;
62     bool m_iterationChanged;
63 };
64
65 class TestTimedItem : public TimedItem {
66 public:
67     static PassRefPtr<TestTimedItem> create(const Timing& specified)
68     {
69         return adoptRef(new TestTimedItem(specified, new TestTimedItemEventDelegate()));
70     }
71
72     void updateInheritedTime(double time)
73     {
74         m_eventDelegate->reset();
75         TimedItem::updateInheritedTime(time);
76     }
77
78     virtual bool updateChildrenAndEffects() const OVERRIDE { return false; }
79     void willDetach() { }
80     TestTimedItemEventDelegate* eventDelegate() { return m_eventDelegate; }
81     virtual double calculateTimeToEffectChange(double localTime, double timeToNextIteration) const OVERRIDE
82     {
83         m_localTime = localTime;
84         m_timeToNextIteration = timeToNextIteration;
85         return -1;
86     }
87
88     double takeLocalTime()
89     {
90         const double result = m_localTime;
91         m_localTime = nullValue();
92         return result;
93     }
94
95     double takeTimeToNextIteration()
96     {
97         const double result = m_timeToNextIteration;
98         m_timeToNextIteration = nullValue();
99         return result;
100     }
101
102 private:
103     TestTimedItem(const Timing& specified, TestTimedItemEventDelegate* eventDelegate)
104         : TimedItem(specified, adoptPtr(eventDelegate))
105         , m_eventDelegate(eventDelegate)
106     {
107     }
108
109     TestTimedItemEventDelegate* m_eventDelegate;
110     mutable double m_localTime;
111     mutable double m_timeToNextIteration;
112 };
113
114 TEST(AnimationTimedItemTest, Sanity)
115 {
116     Timing timing;
117     timing.hasIterationDuration = true;
118     timing.iterationDuration = 2;
119     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
120
121     EXPECT_EQ(0, timedItem->startTime());
122
123     timedItem->updateInheritedTime(0);
124
125     EXPECT_EQ(TimedItem::PhaseActive, timedItem->phase());
126     EXPECT_TRUE(timedItem->isInPlay());
127     EXPECT_TRUE(timedItem->isCurrent());
128     EXPECT_TRUE(timedItem->isInEffect());
129     EXPECT_EQ(0, timedItem->currentIteration());
130     EXPECT_EQ(0, timedItem->startTime());
131     EXPECT_EQ(2, timedItem->activeDuration());
132     EXPECT_EQ(0, timedItem->timeFraction());
133
134     timedItem->updateInheritedTime(1);
135
136     EXPECT_EQ(TimedItem::PhaseActive, timedItem->phase());
137     EXPECT_TRUE(timedItem->isInPlay());
138     EXPECT_TRUE(timedItem->isCurrent());
139     EXPECT_TRUE(timedItem->isInEffect());
140     EXPECT_EQ(0, timedItem->currentIteration());
141     EXPECT_EQ(0, timedItem->startTime());
142     EXPECT_EQ(2, timedItem->activeDuration());
143     EXPECT_EQ(0.5, timedItem->timeFraction());
144
145     timedItem->updateInheritedTime(2);
146
147     EXPECT_EQ(TimedItem::PhaseAfter, timedItem->phase());
148     EXPECT_FALSE(timedItem->isInPlay());
149     EXPECT_FALSE(timedItem->isCurrent());
150     EXPECT_TRUE(timedItem->isInEffect());
151     EXPECT_EQ(0, timedItem->currentIteration());
152     EXPECT_EQ(0, timedItem->startTime());
153     EXPECT_EQ(2, timedItem->activeDuration());
154     EXPECT_EQ(1, timedItem->timeFraction());
155
156     timedItem->updateInheritedTime(3);
157
158     EXPECT_EQ(TimedItem::PhaseAfter, timedItem->phase());
159     EXPECT_FALSE(timedItem->isInPlay());
160     EXPECT_FALSE(timedItem->isCurrent());
161     EXPECT_TRUE(timedItem->isInEffect());
162     EXPECT_EQ(0, timedItem->currentIteration());
163     EXPECT_EQ(0, timedItem->startTime());
164     EXPECT_EQ(2, timedItem->activeDuration());
165     EXPECT_EQ(1, timedItem->timeFraction());
166 }
167
168 TEST(AnimationTimedItemTest, FillForwards)
169 {
170     Timing timing;
171     timing.hasIterationDuration = true;
172     timing.iterationDuration = 1;
173     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
174
175     timedItem->updateInheritedTime(-1);
176     EXPECT_TRUE(isNull(timedItem->timeFraction()));
177
178     timedItem->updateInheritedTime(2);
179     EXPECT_EQ(1, timedItem->timeFraction());
180 }
181
182 TEST(AnimationTimedItemTest, FillBackwards)
183 {
184     Timing timing;
185     timing.hasIterationDuration = true;
186     timing.iterationDuration = 1;
187     timing.fillMode = Timing::FillModeBackwards;
188     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
189
190     timedItem->updateInheritedTime(-1);
191     EXPECT_EQ(0, timedItem->timeFraction());
192
193     timedItem->updateInheritedTime(2);
194     EXPECT_TRUE(isNull(timedItem->timeFraction()));
195 }
196
197 TEST(AnimationTimedItemTest, FillBoth)
198 {
199     Timing timing;
200     timing.hasIterationDuration = true;
201     timing.iterationDuration = 1;
202     timing.fillMode = Timing::FillModeBoth;
203     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
204
205     timedItem->updateInheritedTime(-1);
206     EXPECT_EQ(0, timedItem->timeFraction());
207
208     timedItem->updateInheritedTime(2);
209     EXPECT_EQ(1, timedItem->timeFraction());
210 }
211
212 TEST(AnimationTimedItemTest, StartDelay)
213 {
214     Timing timing;
215     timing.hasIterationDuration = true;
216     timing.iterationDuration = 1;
217     timing.startDelay = 0.5;
218     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
219
220     timedItem->updateInheritedTime(0);
221     EXPECT_TRUE(isNull(timedItem->timeFraction()));
222
223     timedItem->updateInheritedTime(0.5);
224     EXPECT_EQ(0, timedItem->timeFraction());
225
226     timedItem->updateInheritedTime(1.5);
227     EXPECT_EQ(1, timedItem->timeFraction());
228 }
229
230 TEST(AnimationTimedItemTest, ZeroIteration)
231 {
232     Timing timing;
233     timing.hasIterationDuration = true;
234     timing.iterationDuration = 1;
235     timing.iterationCount = 0;
236     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
237
238     timedItem->updateInheritedTime(-1);
239     EXPECT_EQ(0, timedItem->activeDuration());
240     EXPECT_TRUE(isNull(timedItem->currentIteration()));
241     EXPECT_TRUE(isNull(timedItem->timeFraction()));
242
243     timedItem->updateInheritedTime(0);
244     EXPECT_EQ(0, timedItem->activeDuration());
245     EXPECT_EQ(0, timedItem->currentIteration());
246     EXPECT_EQ(0, timedItem->timeFraction());
247 }
248
249 TEST(AnimationTimedItemTest, InfiniteIteration)
250 {
251     Timing timing;
252     timing.hasIterationDuration = true;
253     timing.iterationDuration = 1;
254     timing.iterationCount = std::numeric_limits<double>::infinity();
255     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
256
257     timedItem->updateInheritedTime(-1);
258     EXPECT_TRUE(isNull(timedItem->currentIteration()));
259     EXPECT_TRUE(isNull(timedItem->timeFraction()));
260
261     EXPECT_EQ(std::numeric_limits<double>::infinity(), timedItem->activeDuration());
262
263     timedItem->updateInheritedTime(0);
264     EXPECT_EQ(0, timedItem->currentIteration());
265     EXPECT_EQ(0, timedItem->timeFraction());
266 }
267
268 TEST(AnimationTimedItemTest, Iteration)
269 {
270     Timing timing;
271     timing.iterationCount = 2;
272     timing.hasIterationDuration = true;
273     timing.iterationDuration = 2;
274     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
275
276     timedItem->updateInheritedTime(0);
277     EXPECT_EQ(0, timedItem->currentIteration());
278     EXPECT_EQ(0, timedItem->timeFraction());
279
280     timedItem->updateInheritedTime(1);
281     EXPECT_EQ(0, timedItem->currentIteration());
282     EXPECT_EQ(0.5, timedItem->timeFraction());
283
284     timedItem->updateInheritedTime(2);
285     EXPECT_EQ(1, timedItem->currentIteration());
286     EXPECT_EQ(0, timedItem->timeFraction());
287
288     timedItem->updateInheritedTime(2);
289     EXPECT_EQ(1, timedItem->currentIteration());
290     EXPECT_EQ(0, timedItem->timeFraction());
291
292     timedItem->updateInheritedTime(5);
293     EXPECT_EQ(1, timedItem->currentIteration());
294     EXPECT_EQ(1, timedItem->timeFraction());
295 }
296
297 TEST(AnimationTimedItemTest, IterationStart)
298 {
299     Timing timing;
300     timing.iterationStart = 1.2;
301     timing.iterationCount = 2.2;
302     timing.hasIterationDuration = true;
303     timing.iterationDuration = 1;
304     timing.fillMode = Timing::FillModeBoth;
305     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
306
307     timedItem->updateInheritedTime(-1);
308     EXPECT_EQ(1, timedItem->currentIteration());
309     EXPECT_NEAR(0.2, timedItem->timeFraction(), 0.000000000000001);
310
311     timedItem->updateInheritedTime(0);
312     EXPECT_EQ(1, timedItem->currentIteration());
313     EXPECT_NEAR(0.2, timedItem->timeFraction(), 0.000000000000001);
314
315     timedItem->updateInheritedTime(10);
316     EXPECT_EQ(3, timedItem->currentIteration());
317     EXPECT_NEAR(0.4, timedItem->timeFraction(), 0.000000000000001);
318 }
319
320 TEST(AnimationTimedItemTest, IterationAlternate)
321 {
322     Timing timing;
323     timing.iterationCount = 10;
324     timing.hasIterationDuration = true;
325     timing.iterationDuration = 1;
326     timing.direction = Timing::PlaybackDirectionAlternate;
327     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
328
329     timedItem->updateInheritedTime(0.75);
330     EXPECT_EQ(0, timedItem->currentIteration());
331     EXPECT_EQ(0.75, timedItem->timeFraction());
332
333     timedItem->updateInheritedTime(1.75);
334     EXPECT_EQ(1, timedItem->currentIteration());
335     EXPECT_EQ(0.25, timedItem->timeFraction());
336
337     timedItem->updateInheritedTime(2.75);
338     EXPECT_EQ(2, timedItem->currentIteration());
339     EXPECT_EQ(0.75, timedItem->timeFraction());
340 }
341
342 TEST(AnimationTimedItemTest, IterationAlternateReverse)
343 {
344     Timing timing;
345     timing.iterationCount = 10;
346     timing.hasIterationDuration = true;
347     timing.iterationDuration = 1;
348     timing.direction = Timing::PlaybackDirectionAlternateReverse;
349     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
350
351     timedItem->updateInheritedTime(0.75);
352     EXPECT_EQ(0, timedItem->currentIteration());
353     EXPECT_EQ(0.25, timedItem->timeFraction());
354
355     timedItem->updateInheritedTime(1.75);
356     EXPECT_EQ(1, timedItem->currentIteration());
357     EXPECT_EQ(0.75, timedItem->timeFraction());
358
359     timedItem->updateInheritedTime(2.75);
360     EXPECT_EQ(2, timedItem->currentIteration());
361     EXPECT_EQ(0.25, timedItem->timeFraction());
362 }
363
364 TEST(AnimationTimedItemTest, ZeroDurationSanity)
365 {
366     Timing timing;
367     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
368
369     EXPECT_EQ(0, timedItem->startTime());
370
371     timedItem->updateInheritedTime(0);
372
373     EXPECT_EQ(TimedItem::PhaseAfter, timedItem->phase());
374     EXPECT_FALSE(timedItem->isInPlay());
375     EXPECT_FALSE(timedItem->isCurrent());
376     EXPECT_TRUE(timedItem->isInEffect());
377     EXPECT_EQ(0, timedItem->currentIteration());
378     EXPECT_EQ(0, timedItem->startTime());
379     EXPECT_EQ(0, timedItem->activeDuration());
380     EXPECT_EQ(1, timedItem->timeFraction());
381
382     timedItem->updateInheritedTime(1);
383
384     EXPECT_EQ(TimedItem::PhaseAfter, timedItem->phase());
385     EXPECT_FALSE(timedItem->isInPlay());
386     EXPECT_FALSE(timedItem->isCurrent());
387     EXPECT_TRUE(timedItem->isInEffect());
388     EXPECT_EQ(0, timedItem->currentIteration());
389     EXPECT_EQ(0, timedItem->startTime());
390     EXPECT_EQ(0, timedItem->activeDuration());
391     EXPECT_EQ(1, timedItem->timeFraction());
392 }
393
394 TEST(AnimationTimedItemTest, ZeroDurationFillForwards)
395 {
396     Timing timing;
397     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
398
399     timedItem->updateInheritedTime(-1);
400     EXPECT_TRUE(isNull(timedItem->timeFraction()));
401
402     timedItem->updateInheritedTime(0);
403     EXPECT_EQ(1, timedItem->timeFraction());
404
405     timedItem->updateInheritedTime(1);
406     EXPECT_EQ(1, timedItem->timeFraction());
407 }
408
409 TEST(AnimationTimedItemTest, ZeroDurationFillBackwards)
410 {
411     Timing timing;
412     timing.fillMode = Timing::FillModeBackwards;
413     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
414
415     timedItem->updateInheritedTime(-1);
416     EXPECT_EQ(0, timedItem->timeFraction());
417
418     timedItem->updateInheritedTime(0);
419     EXPECT_TRUE(isNull(timedItem->timeFraction()));
420
421     timedItem->updateInheritedTime(1);
422     EXPECT_TRUE(isNull(timedItem->timeFraction()));
423 }
424
425 TEST(AnimationTimedItemTest, ZeroDurationFillBoth)
426 {
427     Timing timing;
428     timing.fillMode = Timing::FillModeBoth;
429     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
430
431     timedItem->updateInheritedTime(-1);
432     EXPECT_EQ(0, timedItem->timeFraction());
433
434     timedItem->updateInheritedTime(0);
435     EXPECT_EQ(1, timedItem->timeFraction());
436
437     timedItem->updateInheritedTime(1);
438     EXPECT_EQ(1, timedItem->timeFraction());
439 }
440
441 TEST(AnimationTimedItemTest, ZeroDurationStartDelay)
442 {
443     Timing timing;
444     timing.startDelay = 0.5;
445     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
446
447     timedItem->updateInheritedTime(0);
448     EXPECT_TRUE(isNull(timedItem->timeFraction()));
449
450     timedItem->updateInheritedTime(0.5);
451     EXPECT_EQ(1, timedItem->timeFraction());
452
453     timedItem->updateInheritedTime(1.5);
454     EXPECT_EQ(1, timedItem->timeFraction());
455 }
456
457 TEST(AnimationTimedItemTest, ZeroDurationIterationStartAndCount)
458 {
459     Timing timing;
460     timing.iterationStart = 0.1;
461     timing.iterationCount = 0.2;
462     timing.fillMode = Timing::FillModeBoth;
463     timing.startDelay = 0.3;
464     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
465
466     timedItem->updateInheritedTime(0);
467     EXPECT_EQ(0.1, timedItem->timeFraction());
468
469     timedItem->updateInheritedTime(0.3);
470     EXPECT_DOUBLE_EQ(0.3, timedItem->timeFraction());
471
472     timedItem->updateInheritedTime(1);
473     EXPECT_DOUBLE_EQ(0.3, timedItem->timeFraction());
474 }
475
476 // FIXME: Needs specification work.
477 TEST(AnimationTimedItemTest, ZeroDurationInfiniteIteration)
478 {
479     Timing timing;
480     timing.iterationCount = std::numeric_limits<double>::infinity();
481     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
482
483     timedItem->updateInheritedTime(-1);
484     EXPECT_EQ(0, timedItem->activeDuration());
485     EXPECT_TRUE(isNull(timedItem->currentIteration()));
486     EXPECT_TRUE(isNull(timedItem->timeFraction()));
487
488     timedItem->updateInheritedTime(0);
489     EXPECT_EQ(0, timedItem->activeDuration());
490     EXPECT_EQ(std::numeric_limits<double>::infinity(), timedItem->currentIteration());
491     EXPECT_EQ(1, timedItem->timeFraction());
492 }
493
494 TEST(AnimationTimedItemTest, ZeroDurationIteration)
495 {
496     Timing timing;
497     timing.iterationCount = 2;
498     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
499
500     timedItem->updateInheritedTime(-1);
501     EXPECT_TRUE(isNull(timedItem->currentIteration()));
502     EXPECT_TRUE(isNull(timedItem->timeFraction()));
503
504     timedItem->updateInheritedTime(0);
505     EXPECT_EQ(1, timedItem->currentIteration());
506     EXPECT_EQ(1, timedItem->timeFraction());
507
508     timedItem->updateInheritedTime(1);
509     EXPECT_EQ(1, timedItem->currentIteration());
510     EXPECT_EQ(1, timedItem->timeFraction());
511 }
512
513 TEST(AnimationTimedItemTest, ZeroDurationIterationStart)
514 {
515     Timing timing;
516     timing.iterationStart = 1.2;
517     timing.iterationCount = 2.2;
518     timing.fillMode = Timing::FillModeBoth;
519     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
520
521     timedItem->updateInheritedTime(-1);
522     EXPECT_EQ(1, timedItem->currentIteration());
523     EXPECT_NEAR(0.2, timedItem->timeFraction(), 0.000000000000001);
524
525     timedItem->updateInheritedTime(0);
526     EXPECT_EQ(3, timedItem->currentIteration());
527     EXPECT_NEAR(0.4, timedItem->timeFraction(), 0.000000000000001);
528
529     timedItem->updateInheritedTime(10);
530     EXPECT_EQ(3, timedItem->currentIteration());
531     EXPECT_NEAR(0.4, timedItem->timeFraction(), 0.000000000000001);
532 }
533
534 TEST(AnimationTimedItemTest, ZeroDurationIterationAlternate)
535 {
536     Timing timing;
537     timing.iterationCount = 2;
538     timing.direction = Timing::PlaybackDirectionAlternate;
539     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
540
541     timedItem->updateInheritedTime(-1);
542     EXPECT_TRUE(isNull(timedItem->currentIteration()));
543     EXPECT_TRUE(isNull(timedItem->timeFraction()));
544
545     timedItem->updateInheritedTime(0);
546     EXPECT_EQ(1, timedItem->currentIteration());
547     EXPECT_EQ(0, timedItem->timeFraction());
548
549     timedItem->updateInheritedTime(1);
550     EXPECT_EQ(1, timedItem->currentIteration());
551     EXPECT_EQ(0, timedItem->timeFraction());
552 }
553
554 TEST(AnimationTimedItemTest, ZeroDurationIterationAlternateReverse)
555 {
556     Timing timing;
557     timing.iterationCount = 2;
558     timing.direction = Timing::PlaybackDirectionAlternateReverse;
559     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
560
561     timedItem->updateInheritedTime(-1);
562     EXPECT_TRUE(isNull(timedItem->currentIteration()));
563     EXPECT_TRUE(isNull(timedItem->timeFraction()));
564
565     timedItem->updateInheritedTime(0);
566     EXPECT_EQ(1, timedItem->currentIteration());
567     EXPECT_EQ(1, timedItem->timeFraction());
568
569     timedItem->updateInheritedTime(1);
570     EXPECT_EQ(1, timedItem->currentIteration());
571     EXPECT_EQ(1, timedItem->timeFraction());
572 }
573
574 TEST(AnimationTimedItemTest, InfiniteDurationSanity)
575 {
576     Timing timing;
577     timing.hasIterationDuration = true;
578     timing.iterationDuration = std::numeric_limits<double>::infinity();
579     timing.iterationCount = 1;
580     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
581
582     EXPECT_EQ(0, timedItem->startTime());
583
584     timedItem->updateInheritedTime(0);
585
586     EXPECT_EQ(std::numeric_limits<double>::infinity(), timedItem->activeDuration());
587     EXPECT_EQ(TimedItem::PhaseActive, timedItem->phase());
588     EXPECT_TRUE(timedItem->isInPlay());
589     EXPECT_TRUE(timedItem->isCurrent());
590     EXPECT_TRUE(timedItem->isInEffect());
591     EXPECT_EQ(0, timedItem->currentIteration());
592     EXPECT_EQ(0, timedItem->timeFraction());
593
594     timedItem->updateInheritedTime(1);
595
596     EXPECT_EQ(std::numeric_limits<double>::infinity(), timedItem->activeDuration());
597     EXPECT_EQ(TimedItem::PhaseActive, timedItem->phase());
598     EXPECT_TRUE(timedItem->isInPlay());
599     EXPECT_TRUE(timedItem->isCurrent());
600     EXPECT_TRUE(timedItem->isInEffect());
601     EXPECT_EQ(0, timedItem->currentIteration());
602     EXPECT_EQ(0, timedItem->timeFraction());
603 }
604
605 // FIXME: Needs specification work.
606 TEST(AnimationTimedItemTest, InfiniteDurationZeroIterations)
607 {
608     Timing timing;
609     timing.hasIterationDuration = true;
610     timing.iterationDuration = std::numeric_limits<double>::infinity();
611     timing.iterationCount = 0;
612     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
613
614     EXPECT_EQ(0, timedItem->startTime());
615
616     timedItem->updateInheritedTime(0);
617
618     EXPECT_EQ(0, timedItem->activeDuration());
619     EXPECT_EQ(TimedItem::PhaseAfter, timedItem->phase());
620     EXPECT_FALSE(timedItem->isInPlay());
621     EXPECT_FALSE(timedItem->isCurrent());
622     EXPECT_TRUE(timedItem->isInEffect());
623     EXPECT_EQ(0, timedItem->currentIteration());
624     EXPECT_EQ(0, timedItem->timeFraction());
625
626     timedItem->updateInheritedTime(1);
627
628     EXPECT_EQ(TimedItem::PhaseAfter, timedItem->phase());
629     EXPECT_EQ(TimedItem::PhaseAfter, timedItem->phase());
630     EXPECT_FALSE(timedItem->isInPlay());
631     EXPECT_FALSE(timedItem->isCurrent());
632     EXPECT_TRUE(timedItem->isInEffect());
633     EXPECT_EQ(0, timedItem->currentIteration());
634     EXPECT_EQ(0, timedItem->timeFraction());
635 }
636
637 TEST(AnimationTimedItemTest, InfiniteDurationInfiniteIterations)
638 {
639     Timing timing;
640     timing.hasIterationDuration = true;
641     timing.iterationDuration = std::numeric_limits<double>::infinity();
642     timing.iterationCount = std::numeric_limits<double>::infinity();
643     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
644
645     EXPECT_EQ(0, timedItem->startTime());
646
647     timedItem->updateInheritedTime(0);
648
649     EXPECT_EQ(std::numeric_limits<double>::infinity(), timedItem->activeDuration());
650     EXPECT_EQ(TimedItem::PhaseActive, timedItem->phase());
651     EXPECT_TRUE(timedItem->isInPlay());
652     EXPECT_TRUE(timedItem->isCurrent());
653     EXPECT_TRUE(timedItem->isInEffect());
654     EXPECT_EQ(0, timedItem->currentIteration());
655     EXPECT_EQ(0, timedItem->timeFraction());
656
657     timedItem->updateInheritedTime(1);
658
659     EXPECT_EQ(std::numeric_limits<double>::infinity(), timedItem->activeDuration());
660     EXPECT_EQ(TimedItem::PhaseActive, timedItem->phase());
661     EXPECT_TRUE(timedItem->isInPlay());
662     EXPECT_TRUE(timedItem->isCurrent());
663     EXPECT_TRUE(timedItem->isInEffect());
664     EXPECT_EQ(0, timedItem->currentIteration());
665     EXPECT_EQ(0, timedItem->timeFraction());
666 }
667
668 TEST(AnimationTimedItemTest, InfiniteDurationZeroPlaybackRate)
669 {
670     Timing timing;
671     timing.hasIterationDuration = true;
672     timing.iterationDuration = std::numeric_limits<double>::infinity();
673     timing.playbackRate = 0;
674     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
675
676     EXPECT_EQ(0, timedItem->startTime());
677
678     timedItem->updateInheritedTime(0);
679
680     EXPECT_EQ(std::numeric_limits<double>::infinity(), timedItem->activeDuration());
681     EXPECT_EQ(TimedItem::PhaseActive, timedItem->phase());
682     EXPECT_TRUE(timedItem->isInPlay());
683     EXPECT_TRUE(timedItem->isCurrent());
684     EXPECT_TRUE(timedItem->isInEffect());
685     EXPECT_EQ(0, timedItem->currentIteration());
686     EXPECT_EQ(0, timedItem->timeFraction());
687
688     timedItem->updateInheritedTime(std::numeric_limits<double>::infinity());
689
690     EXPECT_EQ(std::numeric_limits<double>::infinity(), timedItem->activeDuration());
691     EXPECT_EQ(TimedItem::PhaseAfter, timedItem->phase());
692     EXPECT_FALSE(timedItem->isInPlay());
693     EXPECT_FALSE(timedItem->isCurrent());
694     EXPECT_TRUE(timedItem->isInEffect());
695     EXPECT_EQ(0, timedItem->currentIteration());
696     EXPECT_EQ(0, timedItem->timeFraction());
697 }
698
699 TEST(AnimationTimedItemTest, Events)
700 {
701     Timing timing;
702     timing.hasIterationDuration = true;
703     timing.iterationDuration = 1;
704     timing.iterationCount = 2;
705     timing.startDelay = 1;
706     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
707
708     // First sample
709     timedItem->updateInheritedTime(0.0);
710     EXPECT_TRUE(timedItem->eventDelegate()->eventTriggered());
711
712     // Before start
713     timedItem->updateInheritedTime(0.5);
714     EXPECT_FALSE(timedItem->eventDelegate()->eventTriggered());
715
716     // First iteration
717     timedItem->updateInheritedTime(1.5);
718     EXPECT_TRUE(timedItem->eventDelegate()->eventTriggered());
719     EXPECT_TRUE(timedItem->eventDelegate()->phaseChanged());
720     EXPECT_TRUE(timedItem->eventDelegate()->iterationChanged());
721
722     timedItem->updateInheritedTime(1.6);
723     EXPECT_FALSE(timedItem->eventDelegate()->eventTriggered());
724
725     // Second iteration
726     timedItem->updateInheritedTime(2.5);
727     EXPECT_TRUE(timedItem->eventDelegate()->eventTriggered());
728     EXPECT_FALSE(timedItem->eventDelegate()->phaseChanged());
729     EXPECT_TRUE(timedItem->eventDelegate()->iterationChanged());
730
731     timedItem->updateInheritedTime(2.6);
732     EXPECT_FALSE(timedItem->eventDelegate()->eventTriggered());
733
734     // After end
735     timedItem->updateInheritedTime(3.5);
736     EXPECT_TRUE(timedItem->eventDelegate()->eventTriggered());
737     EXPECT_TRUE(timedItem->eventDelegate()->phaseChanged());
738     EXPECT_FALSE(timedItem->eventDelegate()->iterationChanged());
739
740     timedItem->updateInheritedTime(3.6);
741     EXPECT_FALSE(timedItem->eventDelegate()->eventTriggered());
742 }
743
744 TEST(AnimationTimedItemTest, TimeToEffectChange)
745 {
746     Timing timing;
747     timing.hasIterationDuration = true;
748     timing.iterationDuration = 1;
749     timing.iterationStart = 0.2;
750     timing.iterationCount = 2.5;
751     timing.startDelay = 1;
752     timing.direction = Timing::PlaybackDirectionAlternate;
753     RefPtr<TestTimedItem> timedItem = TestTimedItem::create(timing);
754
755     timedItem->updateInheritedTime(0);
756     EXPECT_EQ(0, timedItem->takeLocalTime());
757     EXPECT_TRUE(std::isinf(timedItem->takeTimeToNextIteration()));
758
759     // Normal iteration.
760     timedItem->updateInheritedTime(1.75);
761     EXPECT_EQ(1.75, timedItem->takeLocalTime());
762     EXPECT_NEAR(0.05, timedItem->takeTimeToNextIteration(), 0.000000000000001);
763
764     // Reverse iteration.
765     timedItem->updateInheritedTime(2.75);
766     EXPECT_EQ(2.75, timedItem->takeLocalTime());
767     EXPECT_NEAR(0.05, timedItem->takeTimeToNextIteration(), 0.000000000000001);
768
769     // Item ends before iteration finishes.
770     timedItem->updateInheritedTime(3.4);
771     EXPECT_EQ(TimedItem::PhaseActive, timedItem->phase());
772     EXPECT_EQ(3.4, timedItem->takeLocalTime());
773     EXPECT_TRUE(std::isinf(timedItem->takeTimeToNextIteration()));
774
775     // Item has finished.
776     timedItem->updateInheritedTime(3.5);
777     EXPECT_EQ(TimedItem::PhaseAfter, timedItem->phase());
778     EXPECT_EQ(3.5, timedItem->takeLocalTime());
779     EXPECT_TRUE(std::isinf(timedItem->takeTimeToNextIteration()));
780 }
781
782 }