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
43using namespace blink;
44
45namespace {
46
47class AnimationAnimationPlayerTest : public ::testing::Test {
48protected:
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->setStartTime(0);
62        player->setSource(makeAnimation().get());
63    }
64
65    void startTimeline()
66    {
67        simulateFrame(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 simulateFrame(double time)
79    {
80        document->animationClock().updateTime(time);
81        document->compositorPendingAnimations().update(false);
82        // The timeline does not know about our player, so we have to explicitly call update().
83        return player->update(TimingUpdateForAnimationFrame);
84    }
85
86    RefPtrWillBePersistent<Document> document;
87    RefPtrWillBePersistent<AnimationTimeline> timeline;
88    RefPtrWillBePersistent<AnimationPlayer> player;
89    TrackExceptionState exceptionState;
90};
91
92TEST_F(AnimationAnimationPlayerTest, InitialState)
93{
94    setUpWithoutStartingTimeline();
95    player = timeline->createAnimationPlayer(0);
96    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
97    EXPECT_EQ(0, player->currentTimeInternal());
98    EXPECT_FALSE(player->paused());
99    EXPECT_EQ(1, player->playbackRate());
100    EXPECT_FALSE(player->hasStartTime());
101    EXPECT_TRUE(isNull(player->startTimeInternal()));
102
103    startTimeline();
104    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
105    EXPECT_EQ(0, timeline->currentTimeInternal());
106    EXPECT_EQ(0, player->currentTimeInternal());
107    EXPECT_FALSE(player->paused());
108    EXPECT_EQ(1, player->playbackRate());
109    EXPECT_EQ(0, player->startTimeInternal());
110    EXPECT_TRUE(player->hasStartTime());
111}
112
113
114TEST_F(AnimationAnimationPlayerTest, CurrentTimeDoesNotSetOutdated)
115{
116    EXPECT_FALSE(player->outdated());
117    EXPECT_EQ(0, player->currentTimeInternal());
118    EXPECT_FALSE(player->outdated());
119    // FIXME: We should split simulateFrame into a version that doesn't update
120    // the player and one that does, as most of the tests don't require update()
121    // to be called.
122    document->animationClock().updateTime(10);
123    EXPECT_EQ(10, player->currentTimeInternal());
124    EXPECT_FALSE(player->outdated());
125}
126
127TEST_F(AnimationAnimationPlayerTest, SetCurrentTime)
128{
129    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
130    player->setCurrentTimeInternal(10);
131    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
132    EXPECT_EQ(10, player->currentTimeInternal());
133    simulateFrame(10);
134    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
135    EXPECT_EQ(20, player->currentTimeInternal());
136}
137
138TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeNegative)
139{
140    player->setCurrentTimeInternal(-10);
141    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
142    EXPECT_EQ(-10, player->currentTimeInternal());
143    simulateFrame(20);
144    EXPECT_EQ(10, player->currentTimeInternal());
145
146    player->setPlaybackRate(-2);
147    player->setCurrentTimeInternal(-10);
148    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
149    EXPECT_EQ(-10, player->currentTimeInternal());
150    simulateFrame(40);
151    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
152    EXPECT_EQ(-10, player->currentTimeInternal());
153}
154
155TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeNegativeWithoutSimultaneousPlaybackRateChange)
156{
157    simulateFrame(20);
158    EXPECT_EQ(20, player->currentTimeInternal());
159    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
160    player->setPlaybackRate(-1);
161    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
162    simulateFrame(30);
163    EXPECT_EQ(20, player->currentTimeInternal());
164    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
165    player->setCurrentTimeInternal(-10);
166    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
167}
168
169TEST_F(AnimationAnimationPlayerTest, SetCurrentTimePastContentEnd)
170{
171    player->setCurrentTimeInternal(50);
172    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
173    EXPECT_EQ(50, player->currentTimeInternal());
174    simulateFrame(20);
175    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
176    EXPECT_EQ(50, player->currentTimeInternal());
177
178    player->setPlaybackRate(-2);
179    player->setCurrentTimeInternal(50);
180    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
181    EXPECT_EQ(50, player->currentTimeInternal());
182    simulateFrame(20);
183    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
184    simulateFrame(40);
185    EXPECT_EQ(10, player->currentTimeInternal());
186}
187
188TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeBeforeTimelineStarted)
189{
190    setUpWithoutStartingTimeline();
191    player->setCurrentTimeInternal(5);
192    EXPECT_EQ(5, player->currentTimeInternal());
193    startTimeline();
194    simulateFrame(10);
195    EXPECT_EQ(15, player->currentTimeInternal());
196}
197
198TEST_F(AnimationAnimationPlayerTest, SetCurrentTimePastContentEndBeforeTimelineStarted)
199{
200    setUpWithoutStartingTimeline();
201    player->setCurrentTimeInternal(250);
202    EXPECT_EQ(250, player->currentTimeInternal());
203    startTimeline();
204    simulateFrame(10);
205    EXPECT_EQ(250, player->currentTimeInternal());
206}
207
208TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeMax)
209{
210    player->setCurrentTimeInternal(std::numeric_limits<double>::max());
211    EXPECT_EQ(std::numeric_limits<double>::max(), player->currentTimeInternal());
212    simulateFrame(100);
213    EXPECT_EQ(std::numeric_limits<double>::max(), player->currentTimeInternal());
214}
215
216TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeUnrestrictedDouble)
217{
218    simulateFrame(10);
219    player->setCurrentTime(nullValue());
220    EXPECT_EQ(10, player->currentTimeInternal());
221    player->setCurrentTime(std::numeric_limits<double>::infinity());
222    EXPECT_EQ(10, player->currentTimeInternal());
223    player->setCurrentTime(-std::numeric_limits<double>::infinity());
224    EXPECT_EQ(10, player->currentTimeInternal());
225}
226
227
228TEST_F(AnimationAnimationPlayerTest, SetCurrentTimeSetsStartTime)
229{
230    EXPECT_EQ(0, player->startTime());
231    player->setCurrentTime(1000);
232    EXPECT_EQ(-1000, player->startTime());
233    simulateFrame(1);
234    EXPECT_EQ(-1000, player->startTime());
235    EXPECT_EQ(2000, player->currentTime());
236}
237
238TEST_F(AnimationAnimationPlayerTest, SetStartTime)
239{
240    simulateFrame(20);
241    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
242    EXPECT_EQ(0, player->startTimeInternal());
243    EXPECT_EQ(20, player->currentTimeInternal());
244    player->setStartTime(10 * 1000);
245    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
246    EXPECT_EQ(10, player->startTimeInternal());
247    EXPECT_EQ(10, player->currentTimeInternal());
248    simulateFrame(30);
249    EXPECT_EQ(10, player->startTimeInternal());
250    EXPECT_EQ(20, player->currentTimeInternal());
251    player->setStartTime(-20 * 1000);
252    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
253}
254
255TEST_F(AnimationAnimationPlayerTest, SetStartTimeLimitsAnimationPlayer)
256{
257    player->setStartTime(-50 * 1000);
258    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
259    EXPECT_EQ(30, player->currentTimeInternal());
260    player->setPlaybackRate(-1);
261    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
262    player->setStartTime(-100 * 1000);
263    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
264    EXPECT_EQ(0, player->currentTimeInternal());
265    EXPECT_TRUE(player->finished());
266}
267
268TEST_F(AnimationAnimationPlayerTest, SetStartTimeOnLimitedAnimationPlayer)
269{
270    simulateFrame(30);
271    player->setStartTime(-10 * 1000);
272    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
273    EXPECT_EQ(30, player->currentTimeInternal());
274    player->setCurrentTimeInternal(50);
275    player->setStartTime(-40 * 1000);
276    EXPECT_EQ(30, player->currentTimeInternal());
277    EXPECT_EQ(AnimationPlayer::Finished, player->playStateInternal());
278    EXPECT_TRUE(player->finished());
279}
280
281TEST_F(AnimationAnimationPlayerTest, StartTimePauseFinish)
282{
283    player->pause();
284    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
285    EXPECT_TRUE(std::isnan(player->startTime()));
286    player->finish(exceptionState);
287    EXPECT_EQ(AnimationPlayer::Paused, player->playStateInternal());
288    EXPECT_TRUE(std::isnan(player->startTime()));
289}
290
291TEST_F(AnimationAnimationPlayerTest, PauseBeatsFinish)
292{
293    player->pause();
294    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
295    simulateFrame(10);
296    EXPECT_EQ(AnimationPlayer::Paused, player->playStateInternal());
297    player->finish(exceptionState);
298    EXPECT_EQ(AnimationPlayer::Paused, player->playStateInternal());
299}
300
301TEST_F(AnimationAnimationPlayerTest, StartTimeFinishPause)
302{
303    player->finish(exceptionState);
304    EXPECT_EQ(-30 * 1000, player->startTime());
305    player->pause();
306    EXPECT_TRUE(std::isnan(player->startTime()));
307}
308
309TEST_F(AnimationAnimationPlayerTest, StartTimeWithZeroPlaybackRate)
310{
311    player->setPlaybackRate(0);
312    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
313    EXPECT_TRUE(std::isnan(player->startTime()));
314    simulateFrame(10);
315    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
316}
317
318TEST_F(AnimationAnimationPlayerTest, PausePlay)
319{
320    simulateFrame(10);
321    player->pause();
322    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
323    EXPECT_TRUE(player->paused());
324    EXPECT_EQ(10, player->currentTimeInternal());
325    simulateFrame(20);
326    EXPECT_EQ(AnimationPlayer::Paused, player->playStateInternal());
327    player->play();
328    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
329    simulateFrame(20);
330    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
331    EXPECT_FALSE(player->paused());
332    EXPECT_EQ(10, player->currentTimeInternal());
333    simulateFrame(30);
334    EXPECT_EQ(20, player->currentTimeInternal());
335}
336
337TEST_F(AnimationAnimationPlayerTest, PauseBeforeTimelineStarted)
338{
339    setUpWithoutStartingTimeline();
340    player->pause();
341    EXPECT_TRUE(player->paused());
342    player->play();
343    EXPECT_FALSE(player->paused());
344
345    player->pause();
346    startTimeline();
347    simulateFrame(100);
348    EXPECT_TRUE(player->paused());
349    EXPECT_EQ(0, player->currentTimeInternal());
350}
351
352TEST_F(AnimationAnimationPlayerTest, PlayRewindsToStart)
353{
354    player->setCurrentTimeInternal(30);
355    player->play();
356    EXPECT_EQ(0, player->currentTimeInternal());
357
358    player->setCurrentTimeInternal(40);
359    player->play();
360    EXPECT_EQ(0, player->currentTimeInternal());
361    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
362    simulateFrame(10);
363    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
364
365    player->setCurrentTimeInternal(-10);
366    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
367    player->play();
368    EXPECT_EQ(0, player->currentTimeInternal());
369    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
370}
371
372TEST_F(AnimationAnimationPlayerTest, PlayRewindsToEnd)
373{
374    player->setPlaybackRate(-1);
375    player->play();
376    EXPECT_EQ(30, player->currentTimeInternal());
377
378    player->setCurrentTimeInternal(40);
379    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
380    player->play();
381    EXPECT_EQ(30, player->currentTimeInternal());
382    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
383    simulateFrame(10);
384    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
385
386    player->setCurrentTimeInternal(-10);
387    player->play();
388    EXPECT_EQ(30, player->currentTimeInternal());
389    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
390    simulateFrame(20);
391    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
392}
393
394TEST_F(AnimationAnimationPlayerTest, PlayWithPlaybackRateZeroDoesNotSeek)
395{
396    player->setPlaybackRate(0);
397    player->play();
398    EXPECT_EQ(0, player->currentTimeInternal());
399
400    player->setCurrentTimeInternal(40);
401    player->play();
402    EXPECT_EQ(40, player->currentTimeInternal());
403
404    player->setCurrentTimeInternal(-10);
405    player->play();
406    EXPECT_EQ(-10, player->currentTimeInternal());
407}
408
409TEST_F(AnimationAnimationPlayerTest, PlayAfterPauseWithPlaybackRateZeroUpdatesPlayState)
410{
411    player->pause();
412    player->setPlaybackRate(0);
413    simulateFrame(1);
414    EXPECT_EQ(AnimationPlayer::Paused, player->playStateInternal());
415    player->play();
416    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
417}
418
419TEST_F(AnimationAnimationPlayerTest, Reverse)
420{
421    player->setCurrentTimeInternal(10);
422    player->pause();
423    player->reverse();
424    EXPECT_FALSE(player->paused());
425    EXPECT_EQ(-1, player->playbackRate());
426    EXPECT_EQ(10, player->currentTimeInternal());
427}
428
429TEST_F(AnimationAnimationPlayerTest, ReverseDoesNothingWithPlaybackRateZero)
430{
431    player->setCurrentTimeInternal(10);
432    player->setPlaybackRate(0);
433    player->pause();
434    player->reverse();
435    EXPECT_TRUE(player->paused());
436    EXPECT_EQ(0, player->playbackRate());
437    EXPECT_EQ(10, player->currentTimeInternal());
438}
439
440TEST_F(AnimationAnimationPlayerTest, ReverseDoesNotSeekWithNoSource)
441{
442    player->setSource(0);
443    player->setCurrentTimeInternal(10);
444    player->reverse();
445    EXPECT_EQ(10, player->currentTimeInternal());
446}
447
448TEST_F(AnimationAnimationPlayerTest, ReverseSeeksToStart)
449{
450    player->setCurrentTimeInternal(-10);
451    player->setPlaybackRate(-1);
452    player->reverse();
453    EXPECT_EQ(0, player->currentTimeInternal());
454}
455
456TEST_F(AnimationAnimationPlayerTest, ReverseSeeksToEnd)
457{
458    player->setCurrentTimeInternal(40);
459    player->reverse();
460    EXPECT_EQ(30, player->currentTimeInternal());
461}
462
463TEST_F(AnimationAnimationPlayerTest, ReverseBeyondLimit)
464{
465    player->setCurrentTimeInternal(40);
466    player->setPlaybackRate(-1);
467    player->reverse();
468    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
469    EXPECT_EQ(0, player->currentTimeInternal());
470
471    player->setCurrentTimeInternal(-10);
472    player->reverse();
473    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
474    EXPECT_EQ(30, player->currentTimeInternal());
475}
476
477
478TEST_F(AnimationAnimationPlayerTest, Finish)
479{
480    player->finish(exceptionState);
481    EXPECT_EQ(30, player->currentTimeInternal());
482    EXPECT_TRUE(player->finished());
483
484    player->setPlaybackRate(-1);
485    player->finish(exceptionState);
486    EXPECT_EQ(0, player->currentTimeInternal());
487    EXPECT_TRUE(player->finished());
488
489    EXPECT_FALSE(exceptionState.hadException());
490}
491
492TEST_F(AnimationAnimationPlayerTest, FinishAfterSourceEnd)
493{
494    player->setCurrentTimeInternal(40);
495    player->finish(exceptionState);
496    EXPECT_EQ(30, player->currentTimeInternal());
497}
498
499TEST_F(AnimationAnimationPlayerTest, FinishBeforeStart)
500{
501    player->setCurrentTimeInternal(-10);
502    player->setPlaybackRate(-1);
503    player->finish(exceptionState);
504    EXPECT_EQ(0, player->currentTimeInternal());
505}
506
507TEST_F(AnimationAnimationPlayerTest, FinishDoesNothingWithPlaybackRateZero)
508{
509    player->setCurrentTimeInternal(10);
510    player->setPlaybackRate(0);
511    player->finish(exceptionState);
512    EXPECT_EQ(10, player->currentTimeInternal());
513}
514
515TEST_F(AnimationAnimationPlayerTest, FinishRaisesException)
516{
517    Timing timing;
518    timing.iterationDuration = 1;
519    timing.iterationCount = std::numeric_limits<double>::infinity();
520    player->setSource(Animation::create(0, nullptr, timing).get());
521    player->setCurrentTimeInternal(10);
522
523    player->finish(exceptionState);
524    EXPECT_EQ(10, player->currentTimeInternal());
525    EXPECT_TRUE(exceptionState.hadException());
526    EXPECT_EQ(InvalidStateError, exceptionState.code());
527}
528
529
530TEST_F(AnimationAnimationPlayerTest, LimitingAtSourceEnd)
531{
532    simulateFrame(30);
533    EXPECT_EQ(30, player->currentTimeInternal());
534    EXPECT_TRUE(player->finished());
535    simulateFrame(40);
536    EXPECT_EQ(30, player->currentTimeInternal());
537    EXPECT_FALSE(player->paused());
538}
539
540TEST_F(AnimationAnimationPlayerTest, LimitingAtStart)
541{
542    simulateFrame(30);
543    player->setPlaybackRate(-2);
544    simulateFrame(30);
545    simulateFrame(45);
546    EXPECT_EQ(0, player->currentTimeInternal());
547    EXPECT_TRUE(player->finished());
548    simulateFrame(60);
549    EXPECT_EQ(0, player->currentTimeInternal());
550    EXPECT_FALSE(player->paused());
551}
552
553TEST_F(AnimationAnimationPlayerTest, LimitingWithNoSource)
554{
555    player->setSource(0);
556    EXPECT_TRUE(player->finished());
557    simulateFrame(30);
558    EXPECT_EQ(0, player->currentTimeInternal());
559}
560
561
562TEST_F(AnimationAnimationPlayerTest, SetPlaybackRate)
563{
564    player->setPlaybackRate(2);
565    simulateFrame(0);
566    EXPECT_EQ(2, player->playbackRate());
567    EXPECT_EQ(0, player->currentTimeInternal());
568    simulateFrame(10);
569    EXPECT_EQ(20, player->currentTimeInternal());
570}
571
572TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateBeforeTimelineStarted)
573{
574    setUpWithoutStartingTimeline();
575    player->setPlaybackRate(2);
576    EXPECT_EQ(2, player->playbackRate());
577    EXPECT_EQ(0, player->currentTimeInternal());
578    startTimeline();
579    simulateFrame(10);
580    EXPECT_EQ(20, player->currentTimeInternal());
581}
582
583TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateWhilePaused)
584{
585    simulateFrame(10);
586    player->pause();
587    player->setPlaybackRate(2);
588    simulateFrame(20);
589    player->play();
590    EXPECT_EQ(10, player->currentTimeInternal());
591    simulateFrame(20);
592    simulateFrame(25);
593    EXPECT_EQ(20, player->currentTimeInternal());
594}
595
596TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateWhileLimited)
597{
598    simulateFrame(40);
599    EXPECT_EQ(30, player->currentTimeInternal());
600    player->setPlaybackRate(2);
601    simulateFrame(50);
602    EXPECT_EQ(30, player->currentTimeInternal());
603    player->setPlaybackRate(-2);
604    simulateFrame(50);
605    simulateFrame(60);
606    EXPECT_FALSE(player->finished());
607    EXPECT_EQ(10, player->currentTimeInternal());
608}
609
610TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateZero)
611{
612    simulateFrame(0);
613    simulateFrame(10);
614    player->setPlaybackRate(0);
615    simulateFrame(10);
616    EXPECT_EQ(10, player->currentTimeInternal());
617    simulateFrame(20);
618    EXPECT_EQ(10, player->currentTimeInternal());
619    player->setCurrentTimeInternal(20);
620    EXPECT_EQ(20, player->currentTimeInternal());
621}
622
623TEST_F(AnimationAnimationPlayerTest, SetPlaybackRateMax)
624{
625    player->setPlaybackRate(std::numeric_limits<double>::max());
626    simulateFrame(0);
627    EXPECT_EQ(std::numeric_limits<double>::max(), player->playbackRate());
628    EXPECT_EQ(0, player->currentTimeInternal());
629    simulateFrame(1);
630    EXPECT_EQ(30, player->currentTimeInternal());
631}
632
633
634TEST_F(AnimationAnimationPlayerTest, SetSource)
635{
636    player = timeline->createAnimationPlayer(0);
637    player->setStartTime(0);
638    RefPtrWillBeRawPtr<AnimationNode> source1 = makeAnimation();
639    RefPtrWillBeRawPtr<AnimationNode> source2 = makeAnimation();
640    player->setSource(source1.get());
641    EXPECT_EQ(source1, player->source());
642    EXPECT_EQ(0, player->currentTimeInternal());
643    player->setCurrentTimeInternal(15);
644    player->setSource(source2.get());
645    EXPECT_EQ(15, player->currentTimeInternal());
646    EXPECT_EQ(0, source1->player());
647    EXPECT_EQ(player.get(), source2->player());
648    EXPECT_EQ(source2, player->source());
649}
650
651TEST_F(AnimationAnimationPlayerTest, SetSourceLimitsAnimationPlayer)
652{
653    player->setCurrentTimeInternal(20);
654    player->setSource(makeAnimation(10).get());
655    EXPECT_EQ(20, player->currentTimeInternal());
656    EXPECT_TRUE(player->finished());
657    simulateFrame(10);
658    EXPECT_EQ(20, player->currentTimeInternal());
659}
660
661TEST_F(AnimationAnimationPlayerTest, SetSourceUnlimitsAnimationPlayer)
662{
663    player->setCurrentTimeInternal(40);
664    player->setSource(makeAnimation(60).get());
665    EXPECT_FALSE(player->finished());
666    EXPECT_EQ(40, player->currentTimeInternal());
667    simulateFrame(10);
668    EXPECT_EQ(50, player->currentTimeInternal());
669}
670
671
672TEST_F(AnimationAnimationPlayerTest, EmptyAnimationPlayersDontUpdateEffects)
673{
674    player = timeline->createAnimationPlayer(0);
675    player->update(TimingUpdateOnDemand);
676    EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
677
678    simulateFrame(1234);
679    EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
680}
681
682TEST_F(AnimationAnimationPlayerTest, AnimationPlayersDisassociateFromSource)
683{
684    AnimationNode* animationNode = player->source();
685    AnimationPlayer* player2 = timeline->createAnimationPlayer(animationNode);
686    EXPECT_EQ(0, player->source());
687    player->setSource(animationNode);
688    EXPECT_EQ(0, player2->source());
689}
690
691TEST_F(AnimationAnimationPlayerTest, AnimationPlayersReturnTimeToNextEffect)
692{
693    Timing timing;
694    timing.startDelay = 1;
695    timing.iterationDuration = 1;
696    timing.endDelay = 1;
697    RefPtrWillBeRawPtr<Animation> animation = Animation::create(0, nullptr, timing);
698    player = timeline->createAnimationPlayer(animation.get());
699    player->setStartTime(0);
700
701    simulateFrame(0);
702    EXPECT_EQ(1, player->timeToEffectChange());
703
704    simulateFrame(0.5);
705    EXPECT_EQ(0.5, player->timeToEffectChange());
706
707    simulateFrame(1);
708    EXPECT_EQ(0, player->timeToEffectChange());
709
710    simulateFrame(1.5);
711    EXPECT_EQ(0, player->timeToEffectChange());
712
713    simulateFrame(2);
714    EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
715
716    simulateFrame(3);
717    EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
718
719    player->setCurrentTimeInternal(0);
720    simulateFrame(3);
721    EXPECT_EQ(1, player->timeToEffectChange());
722
723    player->setPlaybackRate(2);
724    simulateFrame(3);
725    EXPECT_EQ(0.5, player->timeToEffectChange());
726
727    player->setPlaybackRate(0);
728    player->update(TimingUpdateOnDemand);
729    EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
730
731    player->setCurrentTimeInternal(3);
732    player->setPlaybackRate(-1);
733    player->update(TimingUpdateOnDemand);
734    simulateFrame(3);
735    EXPECT_EQ(1, player->timeToEffectChange());
736
737    player->setPlaybackRate(-2);
738    player->update(TimingUpdateOnDemand);
739    simulateFrame(3);
740    EXPECT_EQ(0.5, player->timeToEffectChange());
741}
742
743TEST_F(AnimationAnimationPlayerTest, TimeToNextEffectWhenPaused)
744{
745    EXPECT_EQ(0, player->timeToEffectChange());
746    player->pause();
747    player->update(TimingUpdateOnDemand);
748    EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
749}
750
751TEST_F(AnimationAnimationPlayerTest, TimeToNextEffectWhenCancelledBeforeStart)
752{
753    EXPECT_EQ(0, player->timeToEffectChange());
754    player->setCurrentTimeInternal(-8);
755    player->setPlaybackRate(2);
756    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
757    player->cancel();
758    EXPECT_EQ(AnimationPlayer::Idle, player->playStateInternal());
759    player->update(TimingUpdateOnDemand);
760    // This frame will fire the finish event event though no start time has been
761    // received from the compositor yet, as cancel() nukes start times.
762    simulateFrame(0);
763    EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
764}
765
766TEST_F(AnimationAnimationPlayerTest, TimeToNextEffectWhenCancelledBeforeStartReverse)
767{
768    EXPECT_EQ(0, player->timeToEffectChange());
769    player->setCurrentTimeInternal(9);
770    player->setPlaybackRate(-3);
771    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
772    player->cancel();
773    EXPECT_EQ(AnimationPlayer::Idle, player->playStateInternal());
774    player->update(TimingUpdateOnDemand);
775    // This frame will fire the finish event event though no start time has been
776    // received from the compositor yet, as cancel() nukes start times.
777    simulateFrame(0);
778    EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
779}
780
781TEST_F(AnimationAnimationPlayerTest, TimeToNextEffectSimpleCancelledBeforeStart)
782{
783    EXPECT_EQ(0, player->timeToEffectChange());
784    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
785    player->cancel();
786    player->update(TimingUpdateOnDemand);
787    // This frame will fire the finish event event though no start time has been
788    // received from the compositor yet, as cancel() nukes start times.
789    simulateFrame(0);
790    EXPECT_EQ(std::numeric_limits<double>::infinity(), player->timeToEffectChange());
791}
792
793TEST_F(AnimationAnimationPlayerTest, AttachedAnimationPlayers)
794{
795    RefPtrWillBePersistent<Element> element = document->createElement("foo", ASSERT_NO_EXCEPTION);
796
797    Timing timing;
798    RefPtrWillBeRawPtr<Animation> animation = Animation::create(element.get(), nullptr, timing);
799    RefPtrWillBeRawPtr<AnimationPlayer> player = timeline->createAnimationPlayer(animation.get());
800    simulateFrame(0);
801    timeline->serviceAnimations(TimingUpdateForAnimationFrame);
802    EXPECT_EQ(1U, element->activeAnimations()->players().find(player.get())->value);
803
804    player.release();
805    Heap::collectAllGarbage();
806    EXPECT_TRUE(element->activeAnimations()->players().isEmpty());
807}
808
809TEST_F(AnimationAnimationPlayerTest, HasLowerPriority)
810{
811    RefPtrWillBeRawPtr<AnimationPlayer> player1 = timeline->createAnimationPlayer(0);
812    RefPtrWillBeRawPtr<AnimationPlayer> player2 = timeline->createAnimationPlayer(0);
813    EXPECT_TRUE(AnimationPlayer::hasLowerPriority(player1.get(), player2.get()));
814}
815
816TEST_F(AnimationAnimationPlayerTest, PlayAfterCancel)
817{
818    player->cancel();
819    EXPECT_EQ(AnimationPlayer::Idle, player->playStateInternal());
820    EXPECT_TRUE(std::isnan(player->currentTime()));
821    EXPECT_TRUE(std::isnan(player->startTime()));
822    player->play();
823    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
824    EXPECT_EQ(0, player->currentTime());
825    EXPECT_TRUE(std::isnan(player->startTime()));
826    simulateFrame(10);
827    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
828    EXPECT_EQ(0, player->currentTime());
829    EXPECT_EQ(10 * 1000, player->startTime());
830}
831
832TEST_F(AnimationAnimationPlayerTest, PlayBackwardsAfterCancel)
833{
834    player->setPlaybackRate(-1);
835    player->setCurrentTime(15 * 1000);
836    simulateFrame(0);
837    player->cancel();
838    EXPECT_EQ(AnimationPlayer::Idle, player->playStateInternal());
839    EXPECT_TRUE(std::isnan(player->currentTime()));
840    EXPECT_TRUE(std::isnan(player->startTime()));
841    player->play();
842    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
843    EXPECT_EQ(30 * 1000, player->currentTime());
844    EXPECT_TRUE(std::isnan(player->startTime()));
845    simulateFrame(10);
846    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
847    EXPECT_EQ(30 * 1000, player->currentTime());
848    EXPECT_EQ(40 * 1000, player->startTime());
849}
850
851TEST_F(AnimationAnimationPlayerTest, ReverseAfterCancel)
852{
853    player->cancel();
854    EXPECT_EQ(AnimationPlayer::Idle, player->playStateInternal());
855    EXPECT_TRUE(std::isnan(player->currentTime()));
856    EXPECT_TRUE(std::isnan(player->startTime()));
857    player->reverse();
858    EXPECT_EQ(AnimationPlayer::Pending, player->playStateInternal());
859    EXPECT_EQ(30 * 1000, player->currentTime());
860    EXPECT_TRUE(std::isnan(player->startTime()));
861    simulateFrame(10);
862    EXPECT_EQ(AnimationPlayer::Running, player->playStateInternal());
863    EXPECT_EQ(30 * 1000, player->currentTime());
864    EXPECT_EQ(40 * 1000, player->startTime());
865}
866
867TEST_F(AnimationAnimationPlayerTest, FinishAfterCancel)
868{
869    player->cancel();
870    EXPECT_EQ(AnimationPlayer::Idle, player->playStateInternal());
871    EXPECT_TRUE(std::isnan(player->currentTime()));
872    EXPECT_TRUE(std::isnan(player->startTime()));
873    player->finish(exceptionState);
874    EXPECT_TRUE(std::isnan(player->currentTime()));
875    EXPECT_TRUE(std::isnan(player->startTime()));
876    EXPECT_EQ(AnimationPlayer::Idle, player->playStateInternal());
877}
878
879TEST_F(AnimationAnimationPlayerTest, PauseAfterCancel)
880{
881    player->cancel();
882    EXPECT_EQ(AnimationPlayer::Idle, player->playStateInternal());
883    EXPECT_TRUE(std::isnan(player->currentTime()));
884    EXPECT_TRUE(std::isnan(player->startTime()));
885    player->pause();
886    EXPECT_EQ(AnimationPlayer::Idle, player->playStateInternal());
887    EXPECT_TRUE(std::isnan(player->currentTime()));
888    EXPECT_TRUE(std::isnan(player->startTime()));
889}
890
891}
892