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