Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / AnimationPlayerTest.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/AnimationPlayer.h"
33
34 #include "core/animation/ActiveAnimations.h"
35 #include "core/animation/Animation.h"
36 #include "core/animation/AnimationClock.h"
37 #include "core/animation/DocumentTimeline.h"
38 #include "core/dom/Document.h"
39 #include "core/dom/QualifiedName.h"
40 #include "platform/weborigin/KURL.h"
41 #include <gtest/gtest.h>
42
43 using namespace WebCore;
44
45 namespace {
46
47 class AnimationAnimationPlayerTest : public ::testing::Test {
48 protected:
49     virtual void SetUp()
50     {
51         setUpWithoutStartingTimeline();
52         startTimeline();
53     }
54
55     void setUpWithoutStartingTimeline()
56     {
57         document = Document::create();
58         document->animationClock().resetTimeForTesting();
59         timeline = DocumentTimeline::create(document.get());
60         player = timeline->createAnimationPlayer(0);
61         player->setStartTimeInternal(0);
62         player->setSource(makeAnimation().get());
63     }
64
65     void startTimeline()
66     {
67         timeline->setZeroTime(0);
68         updateTimeline(0);
69     }
70
71     PassRefPtr<Animation> makeAnimation(double duration = 30, double playbackRate = 1)
72     {
73         Timing timing;
74         timing.iterationDuration = duration;
75         timing.playbackRate = playbackRate;
76         return Animation::create(0, nullptr, timing);
77     }
78
79     bool updateTimeline(double time)
80     {
81         document->animationClock().updateTime(time);
82         // The timeline does not know about our player, so we have to explicitly call update().
83         return player->update(TimingUpdateOnDemand);
84     }
85
86     RefPtr<Document> document;
87     RefPtr<DocumentTimeline> timeline;
88     RefPtr<AnimationPlayer> player;
89     TrackExceptionState exceptionState;
90 };
91
92 TEST_F(AnimationAnimationPlayerTest, InitialState)
93 {
94     setUpWithoutStartingTimeline();
95     player = timeline->createAnimationPlayer(0);
96     EXPECT_TRUE(isNull(timeline->currentTimeInternal()));
97     EXPECT_EQ(0, player->currentTimeInternal());
98     EXPECT_FALSE(player->paused());
99     EXPECT_EQ(1, player->playbackRate());
100     EXPECT_EQ(0, player->timeLagInternal());
101     EXPECT_FALSE(player->hasStartTime());
102     EXPECT_TRUE(isNull(player->startTimeInternal()));
103
104     startTimeline();
105     player->setStartTimeInternal(0);
106     EXPECT_EQ(0, timeline->currentTimeInternal());
107     EXPECT_EQ(0, player->currentTimeInternal());
108     EXPECT_FALSE(player->paused());
109     EXPECT_EQ(1, player->playbackRate());
110     EXPECT_EQ(0, player->startTimeInternal());
111     EXPECT_EQ(0, player->timeLagInternal());
112     EXPECT_TRUE(player->hasStartTime());
113 }
114
115
116 TEST_F(AnimationAnimationPlayerTest, CurrentTimeDoesNotSetOutdated)
117 {
118     EXPECT_FALSE(player->outdated());
119     EXPECT_EQ(0, player->currentTimeInternal());
120     EXPECT_FALSE(player->outdated());
121     // FIXME: We should split updateTimeline into a version that doesn't update
122     // the player and one that does, as most of the tests don't require update()
123     // to be called.
124     document->animationClock().updateTime(10);
125     EXPECT_EQ(10, player->currentTimeInternal());
126     EXPECT_FALSE(player->outdated());
127 }
128
129 TEST_F(AnimationAnimationPlayerTest, SetCurrentTime)
130 {
131     player->setCurrentTimeInternal(10);
132     EXPECT_EQ(10, player->currentTimeInternal());
133     updateTimeline(10);
134     EXPECT_EQ(20, player->currentTimeInternal());
135 }
136
137 TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeNegative)
138 {
139     player->setCurrentTimeInternal(-10);
140     EXPECT_EQ(-10, player->currentTimeInternal());
141     updateTimeline(20);
142     EXPECT_EQ(10, player->currentTimeInternal());
143
144     player->setPlaybackRate(-2);
145     player->setCurrentTimeInternal(-10);
146     EXPECT_EQ(-10, player->currentTimeInternal());
147     updateTimeline(40);
148     EXPECT_EQ(-10, player->currentTimeInternal());
149 }
150
151 TEST_F(AnimationAnimationPlayerTest, SetCurrentTimePastContentEnd)
152 {
153     player->setCurrentTimeInternal(50);
154     EXPECT_EQ(50, player->currentTimeInternal());
155     updateTimeline(20);
156     EXPECT_EQ(50, player->currentTimeInternal());
157
158     player->setPlaybackRate(-2);
159     player->setCurrentTimeInternal(50);
160     EXPECT_EQ(50, player->currentTimeInternal());
161     updateTimeline(40);
162     EXPECT_EQ(10, player->currentTimeInternal());
163 }
164
165 TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeBeforeTimelineStarted)
166 {
167     setUpWithoutStartingTimeline();
168     player->setCurrentTimeInternal(5);
169     EXPECT_EQ(5, player->currentTimeInternal());
170     startTimeline();
171     updateTimeline(10);
172     EXPECT_EQ(15, player->currentTimeInternal());
173 }
174
175 TEST_F(AnimationAnimationPlayerTest, SetCurrentTimePastContentEndBeforeTimelineStarted)
176 {
177     setUpWithoutStartingTimeline();
178     player->setCurrentTimeInternal(250);
179     EXPECT_EQ(250, player->currentTimeInternal());
180     startTimeline();
181     updateTimeline(10);
182     EXPECT_EQ(250, player->currentTimeInternal());
183 }
184
185 TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeMax)
186 {
187     player->setCurrentTimeInternal(std::numeric_limits<double>::max());
188     EXPECT_EQ(std::numeric_limits<double>::max(), player->currentTimeInternal());
189     updateTimeline(100);
190     EXPECT_EQ(std::numeric_limits<double>::max(), player->currentTimeInternal());
191 }
192
193 TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeUnrestrictedDouble)
194 {
195     updateTimeline(10);
196     player->setCurrentTimeInternal(nullValue());
197     EXPECT_EQ(10, player->currentTimeInternal());
198     player->setCurrentTimeInternal(std::numeric_limits<double>::infinity());
199     EXPECT_EQ(10, player->currentTimeInternal());
200     player->setCurrentTimeInternal(-std::numeric_limits<double>::infinity());
201     EXPECT_EQ(10, player->currentTimeInternal());
202 }
203
204 TEST_F(AnimationAnimationPlayerTest, TimeLag)
205 {
206     player->setCurrentTimeInternal(10);
207     EXPECT_EQ(-10, player->timeLagInternal());
208     updateTimeline(10);
209     EXPECT_EQ(-10, player->timeLagInternal());
210     player->setCurrentTimeInternal(40);
211     EXPECT_EQ(-30, player->timeLagInternal());
212     updateTimeline(20);
213     EXPECT_EQ(-20, player->timeLagInternal());
214 }
215
216
217 TEST_F(AnimationAnimationPlayerTest, SetStartTime)
218 {
219     updateTimeline(20);
220     EXPECT_EQ(0, player->startTimeInternal());
221     EXPECT_EQ(20, player->currentTimeInternal());
222     player->setStartTimeInternal(10);
223     EXPECT_EQ(10, player->startTimeInternal());
224     EXPECT_EQ(10, player->currentTimeInternal());
225     updateTimeline(30);
226     EXPECT_EQ(10, player->startTimeInternal());
227     EXPECT_EQ(20, player->currentTimeInternal());
228 }
229
230 TEST_F(AnimationAnimationPlayerTest, SetStartTimeLimitsAnimationPlayer)
231 {
232     player->setStartTimeInternal(-50);
233     EXPECT_EQ(30, player->currentTimeInternal());
234     player->setPlaybackRate(-1);
235     player->setStartTimeInternal(-100);
236     EXPECT_EQ(0, player->currentTimeInternal());
237     EXPECT_TRUE(player->finished());
238 }
239
240 TEST_F(AnimationAnimationPlayerTest, SetStartTimeOnLimitedAnimationPlayer)
241 {
242     updateTimeline(30);
243     player->setStartTimeInternal(-10);
244     EXPECT_EQ(30, player->currentTimeInternal());
245     player->setCurrentTimeInternal(50);
246     player->setStartTimeInternal(-40);
247     EXPECT_EQ(50, player->currentTimeInternal());
248     EXPECT_TRUE(player->finished());
249 }
250
251 TEST_F(AnimationAnimationPlayerTest, SetStartTimeWhilePaused)
252 {
253     updateTimeline(10);
254     player->pause();
255     player->setStartTimeInternal(-40);
256     EXPECT_EQ(10, player->currentTimeInternal());
257     updateTimeline(50);
258     player->setStartTimeInternal(60);
259     EXPECT_EQ(10, player->currentTimeInternal());
260 }
261
262
263 TEST_F(AnimationAnimationPlayerTest, PausePlay)
264 {
265     updateTimeline(10);
266     player->pause();
267     EXPECT_TRUE(player->paused());
268     EXPECT_EQ(10, player->currentTimeInternal());
269     updateTimeline(20);
270     player->play();
271     EXPECT_FALSE(player->paused());
272     EXPECT_EQ(10, player->currentTimeInternal());
273     updateTimeline(30);
274     EXPECT_EQ(20, player->currentTimeInternal());
275 }
276
277 TEST_F(AnimationAnimationPlayerTest, PauseBeforeTimelineStarted)
278 {
279     setUpWithoutStartingTimeline();
280     player->pause();
281     EXPECT_TRUE(player->paused());
282     player->play();
283     EXPECT_FALSE(player->paused());
284
285     player->pause();
286     startTimeline();
287     updateTimeline(100);
288     EXPECT_TRUE(player->paused());
289     EXPECT_EQ(0, player->currentTimeInternal());
290 }
291
292 TEST_F(AnimationAnimationPlayerTest, PauseBeforeStartTimeSet)
293 {
294     player = timeline->createAnimationPlayer(makeAnimation().get());
295     updateTimeline(100);
296     player->pause();
297     updateTimeline(200);
298     EXPECT_EQ(0, player->currentTimeInternal());
299
300     player->setStartTimeInternal(150);
301     player->play();
302     EXPECT_EQ(0, player->currentTimeInternal());
303     updateTimeline(220);
304     EXPECT_EQ(20, player->currentTimeInternal());
305 }
306
307 TEST_F(AnimationAnimationPlayerTest, PlayRewindsToStart)
308 {
309     player->setCurrentTimeInternal(30);
310     player->play();
311     EXPECT_EQ(0, player->currentTimeInternal());
312
313     player->setCurrentTimeInternal(40);
314     player->play();
315     EXPECT_EQ(0, player->currentTimeInternal());
316
317     player->setCurrentTimeInternal(-10);
318     player->play();
319     EXPECT_EQ(0, player->currentTimeInternal());
320 }
321
322 TEST_F(AnimationAnimationPlayerTest, PlayRewindsToEnd)
323 {
324     player->setPlaybackRate(-1);
325     player->play();
326     EXPECT_EQ(30, player->currentTimeInternal());
327
328     player->setCurrentTimeInternal(40);
329     player->play();
330     EXPECT_EQ(30, player->currentTimeInternal());
331
332     player->setCurrentTimeInternal(-10);
333     player->play();
334     EXPECT_EQ(30, player->currentTimeInternal());
335 }
336
337 TEST_F(AnimationAnimationPlayerTest, PlayWithPlaybackRateZeroDoesNotSeek)
338 {
339     player->setPlaybackRate(0);
340     player->play();
341     EXPECT_EQ(0, player->currentTimeInternal());
342
343     player->setCurrentTimeInternal(40);
344     player->play();
345     EXPECT_EQ(40, player->currentTimeInternal());
346
347     player->setCurrentTimeInternal(-10);
348     player->play();
349     EXPECT_EQ(-10, player->currentTimeInternal());
350 }
351
352
353 TEST_F(AnimationAnimationPlayerTest, Reverse)
354 {
355     player->setCurrentTimeInternal(10);
356     player->pause();
357     player->reverse();
358     EXPECT_FALSE(player->paused());
359     EXPECT_EQ(-1, player->playbackRate());
360     EXPECT_EQ(10, player->currentTimeInternal());
361 }
362
363 TEST_F(AnimationAnimationPlayerTest, ReverseDoesNothingWithPlaybackRateZero)
364 {
365     player->setCurrentTimeInternal(10);
366     player->setPlaybackRate(0);
367     player->pause();
368     player->reverse();
369     EXPECT_TRUE(player->paused());
370     EXPECT_EQ(0, player->playbackRate());
371     EXPECT_EQ(10, player->currentTimeInternal());
372 }
373
374 TEST_F(AnimationAnimationPlayerTest, ReverseDoesNotSeekWithNoSource)
375 {
376     player->setSource(0);
377     player->setCurrentTimeInternal(10);
378     player->reverse();
379     EXPECT_EQ(10, player->currentTimeInternal());
380 }
381
382 TEST_F(AnimationAnimationPlayerTest, ReverseSeeksToStart)
383 {
384     player->setCurrentTimeInternal(-10);
385     player->setPlaybackRate(-1);
386     player->reverse();
387     EXPECT_EQ(0, player->currentTimeInternal());
388 }
389
390 TEST_F(AnimationAnimationPlayerTest, ReverseSeeksToEnd)
391 {
392     player->setCurrentTimeInternal(40);
393     player->reverse();
394     EXPECT_EQ(30, player->currentTimeInternal());
395 }
396
397 TEST_F(AnimationAnimationPlayerTest, ReverseLimitsAnimationPlayer)
398 {
399     player->setCurrentTimeInternal(40);
400     player->setPlaybackRate(-1);
401     player->reverse();
402     EXPECT_TRUE(player->finished());
403     EXPECT_EQ(40, player->currentTimeInternal());
404
405     player->setCurrentTimeInternal(-10);
406     player->reverse();
407     EXPECT_TRUE(player->finished());
408     EXPECT_EQ(-10, player->currentTimeInternal());
409 }
410
411
412 TEST_F(AnimationAnimationPlayerTest, Finish)
413 {
414     player->finish(exceptionState);
415     EXPECT_EQ(30, player->currentTimeInternal());
416     EXPECT_TRUE(player->finished());
417
418     player->setPlaybackRate(-1);
419     player->finish(exceptionState);
420     EXPECT_EQ(0, player->currentTimeInternal());
421     EXPECT_TRUE(player->finished());
422
423     EXPECT_FALSE(exceptionState.hadException());
424 }
425
426 TEST_F(AnimationAnimationPlayerTest, FinishAfterSourceEnd)
427 {
428     player->setCurrentTimeInternal(40);
429     player->finish(exceptionState);
430     EXPECT_EQ(30, player->currentTimeInternal());
431 }
432
433 TEST_F(AnimationAnimationPlayerTest, FinishBeforeStart)
434 {
435     player->setCurrentTimeInternal(-10);
436     player->setPlaybackRate(-1);
437     player->finish(exceptionState);
438     EXPECT_EQ(0, player->currentTimeInternal());
439 }
440
441 TEST_F(AnimationAnimationPlayerTest, FinishDoesNothingWithPlaybackRateZero)
442 {
443     player->setCurrentTimeInternal(10);
444     player->setPlaybackRate(0);
445     player->finish(exceptionState);
446     EXPECT_EQ(10, player->currentTimeInternal());
447 }
448
449 TEST_F(AnimationAnimationPlayerTest, FinishRaisesException)
450 {
451     Timing timing;
452     timing.iterationDuration = 1;
453     timing.iterationCount = std::numeric_limits<double>::infinity();
454     player->setSource(Animation::create(0, nullptr, timing).get());
455     player->setCurrentTimeInternal(10);
456
457     player->finish(exceptionState);
458     EXPECT_EQ(10, player->currentTimeInternal());
459     EXPECT_TRUE(exceptionState.hadException());
460     EXPECT_EQ(InvalidStateError, exceptionState.code());
461 }
462
463
464 TEST_F(AnimationAnimationPlayerTest, LimitingAtSourceEnd)
465 {
466     updateTimeline(30);
467     EXPECT_EQ(30, player->currentTimeInternal());
468     EXPECT_TRUE(player->finished());
469     updateTimeline(40);
470     EXPECT_EQ(30, player->currentTimeInternal());
471     EXPECT_FALSE(player->paused());
472 }
473
474 TEST_F(AnimationAnimationPlayerTest, LimitingAtStart)
475 {
476     updateTimeline(30);
477     player->setPlaybackRate(-2);
478     updateTimeline(45);
479     EXPECT_EQ(0, player->currentTimeInternal());
480     EXPECT_TRUE(player->finished());
481     updateTimeline(60);
482     EXPECT_EQ(0, player->currentTimeInternal());
483     EXPECT_FALSE(player->paused());
484 }
485
486 TEST_F(AnimationAnimationPlayerTest, LimitingWithNoSource)
487 {
488     player->setSource(0);
489     EXPECT_TRUE(player->finished());
490     updateTimeline(30);
491     EXPECT_EQ(0, player->currentTimeInternal());
492 }
493
494
495 TEST_F(AnimationAnimationPlayerTest, SetPlaybackRate)
496 {
497     player->setPlaybackRate(2);
498     EXPECT_EQ(2, player->playbackRate());
499     EXPECT_EQ(0, player->currentTimeInternal());
500     updateTimeline(10);
501     EXPECT_EQ(20, player->currentTimeInternal());
502 }
503
504 TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateBeforeTimelineStarted)
505 {
506     setUpWithoutStartingTimeline();
507     player->setPlaybackRate(2);
508     EXPECT_EQ(2, player->playbackRate());
509     EXPECT_EQ(0, player->currentTimeInternal());
510     startTimeline();
511     updateTimeline(10);
512     EXPECT_EQ(20, player->currentTimeInternal());
513 }
514
515 TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateWhilePaused)
516 {
517     updateTimeline(10);
518     player->pause();
519     player->setPlaybackRate(2);
520     updateTimeline(20);
521     player->play();
522     EXPECT_EQ(10, player->currentTimeInternal());
523     updateTimeline(25);
524     EXPECT_EQ(20, player->currentTimeInternal());
525 }
526
527 TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateWhileLimited)
528 {
529     updateTimeline(40);
530     EXPECT_EQ(30, player->currentTimeInternal());
531     player->setPlaybackRate(2);
532     updateTimeline(50);
533     EXPECT_EQ(30, player->currentTimeInternal());
534     player->setPlaybackRate(-2);
535     EXPECT_FALSE(player->finished());
536     updateTimeline(60);
537     EXPECT_EQ(10, player->currentTimeInternal());
538 }
539
540 TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateZero)
541 {
542     updateTimeline(10);
543     player->setPlaybackRate(0);
544     EXPECT_EQ(10, player->currentTimeInternal());
545     updateTimeline(20);
546     EXPECT_EQ(10, player->currentTimeInternal());
547     player->setCurrentTimeInternal(20);
548     EXPECT_EQ(20, player->currentTimeInternal());
549 }
550
551 TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateMax)
552 {
553     player->setPlaybackRate(std::numeric_limits<double>::max());
554     EXPECT_EQ(std::numeric_limits<double>::max(), player->playbackRate());
555     EXPECT_EQ(0, player->currentTimeInternal());
556     updateTimeline(1);
557     EXPECT_EQ(30, player->currentTimeInternal());
558 }
559
560
561 TEST_F(AnimationAnimationPlayerTest, SetSource)
562 {
563     player = timeline->createAnimationPlayer(0);
564     player->setStartTimeInternal(0);
565     RefPtr<TimedItem> source1 = makeAnimation();
566     RefPtr<TimedItem> source2 = makeAnimation();
567     player->setSource(source1.get());
568     EXPECT_EQ(source1, player->source());
569     EXPECT_EQ(0, player->currentTimeInternal());
570     player->setCurrentTimeInternal(15);
571     player->setSource(source2.get());
572     EXPECT_EQ(15, player->currentTimeInternal());
573     EXPECT_EQ(0, source1->player());
574     EXPECT_EQ(player.get(), source2->player());
575     EXPECT_EQ(source2, player->source());
576 }
577
578 TEST_F(AnimationAnimationPlayerTest, SetSourceLimitsAnimationPlayer)
579 {
580     player->setCurrentTimeInternal(20);
581     player->setSource(makeAnimation(10).get());
582     EXPECT_EQ(20, player->currentTimeInternal());
583     EXPECT_TRUE(player->finished());
584     updateTimeline(10);
585     EXPECT_EQ(20, player->currentTimeInternal());
586 }
587
588 TEST_F(AnimationAnimationPlayerTest, SetSourceUnlimitsAnimationPlayer)
589 {
590     player->setCurrentTimeInternal(40);
591     player->setSource(makeAnimation(60).get());
592     EXPECT_FALSE(player->finished());
593     EXPECT_EQ(40, player->currentTimeInternal());
594     updateTimeline(10);
595     EXPECT_EQ(50, player->currentTimeInternal());
596 }
597
598
599 TEST_F(AnimationAnimationPlayerTest, EmptyAnimationPlayersDontUpdateEffects)
600 {
601     player = timeline->createAnimationPlayer(0);
602     player->update(TimingUpdateOnDemand);
603     EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
604
605     updateTimeline(1234);
606     EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
607 }
608
609 TEST_F(AnimationAnimationPlayerTest, AnimationPlayersDisassociateFromSource)
610 {
611     TimedItem* timedItem = player->source();
612     AnimationPlayer* player2 = timeline->createAnimationPlayer(timedItem);
613     EXPECT_EQ(0, player->source());
614     player->setSource(timedItem);
615     EXPECT_EQ(0, player2->source());
616 }
617
618 TEST_F(AnimationAnimationPlayerTest, AnimationPlayersReturnTimeToNextEffect)
619 {
620     Timing timing;
621     timing.startDelay = 1;
622     timing.iterationDuration = 1;
623     timing.endDelay = 1;
624     RefPtr<Animation> animation = Animation::create(0, nullptr, timing);
625     player = timeline->createAnimationPlayer(animation.get());
626     player->setStartTimeInternal(0);
627
628     updateTimeline(0);
629     EXPECT_EQ(1, player->timeToEffectChange());
630
631     updateTimeline(0.5);
632     EXPECT_EQ(0.5, player->timeToEffectChange());
633
634     updateTimeline(1);
635     EXPECT_EQ(0, player->timeToEffectChange());
636
637     updateTimeline(1.5);
638     EXPECT_EQ(0, player->timeToEffectChange());
639
640     updateTimeline(2);
641     EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
642
643     updateTimeline(3);
644     EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
645
646     player->setCurrentTimeInternal(0);
647     player->update(TimingUpdateOnDemand);
648     EXPECT_EQ(1, player->timeToEffectChange());
649
650     player->setPlaybackRate(2);
651     player->update(TimingUpdateOnDemand);
652     EXPECT_EQ(0.5, player->timeToEffectChange());
653
654     player->setPlaybackRate(0);
655     player->update(TimingUpdateOnDemand);
656     EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
657
658     player->setCurrentTimeInternal(3);
659     player->setPlaybackRate(-1);
660     player->update(TimingUpdateOnDemand);
661     EXPECT_EQ(1, player->timeToEffectChange());
662
663     player->setPlaybackRate(-2);
664     player->update(TimingUpdateOnDemand);
665     EXPECT_EQ(0.5, player->timeToEffectChange());
666 }
667
668 TEST_F(AnimationAnimationPlayerTest, TimeToNextEffectWhenPaused)
669 {
670     EXPECT_EQ(0, player->timeToEffectChange());
671     player->pause();
672     player->update(TimingUpdateOnDemand);
673     EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
674 }
675
676 TEST_F(AnimationAnimationPlayerTest, TimeToNextEffectWhenCancelledBeforeStart)
677 {
678     EXPECT_EQ(0, player->timeToEffectChange());
679     player->setCurrentTimeInternal(-8);
680     player->setPlaybackRate(2);
681     player->cancel();
682     player->update(TimingUpdateOnDemand);
683     EXPECT_EQ(4, player->timeToEffectChange());
684 }
685
686 TEST_F(AnimationAnimationPlayerTest, TimeToNextEffectWhenCancelledBeforeStartReverse)
687 {
688     EXPECT_EQ(0, player->timeToEffectChange());
689     player->setCurrentTimeInternal(9);
690     player->setPlaybackRate(-3);
691     player->cancel();
692     player->update(TimingUpdateOnDemand);
693     EXPECT_EQ(3, player->timeToEffectChange());
694 }
695
696 TEST_F(AnimationAnimationPlayerTest, AttachedAnimationPlayers)
697 {
698     RefPtr<Element> element = document->createElement("foo", ASSERT_NO_EXCEPTION);
699
700     Timing timing;
701     RefPtr<Animation> animation = Animation::create(element.get(), nullptr, timing);
702     RefPtr<AnimationPlayer> player = timeline->createAnimationPlayer(animation.get());
703     player->setStartTime(0);
704     timeline->serviceAnimations(TimingUpdateForAnimationFrame);
705     EXPECT_EQ(1, element->activeAnimations()->players().find(player.get())->value);
706
707     player.release();
708     EXPECT_TRUE(element->activeAnimations()->players().isEmpty());
709 }
710
711 TEST_F(AnimationAnimationPlayerTest, HasLowerPriority)
712 {
713     // Sort time defaults to timeline current time
714     updateTimeline(15);
715     RefPtr<AnimationPlayer> player1 = timeline->createAnimationPlayer(0);
716     RefPtr<AnimationPlayer> player2 = timeline->createAnimationPlayer(0);
717     player2->setStartTimeInternal(10);
718     RefPtr<AnimationPlayer> player3 = timeline->createAnimationPlayer(0);
719     RefPtr<AnimationPlayer> player4 = timeline->createAnimationPlayer(0);
720     player4->setStartTimeInternal(20);
721     RefPtr<AnimationPlayer> player5 = timeline->createAnimationPlayer(0);
722     player5->setStartTimeInternal(10);
723     RefPtr<AnimationPlayer> player6 = timeline->createAnimationPlayer(0);
724     player6->setStartTimeInternal(-10);
725     Vector<RefPtr<AnimationPlayer> > players;
726     players.append(player6);
727     players.append(player2);
728     players.append(player5);
729     players.append(player1);
730     players.append(player3);
731     players.append(player4);
732     for (size_t i = 0; i < players.size(); i++) {
733         for (size_t j = 0; j < players.size(); j++)
734             EXPECT_EQ(i < j, AnimationPlayer::hasLowerPriority(players[i].get(), players[j].get()));
735     }
736 }
737
738 }