2 * Copyright (c) 2013, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/animation/AnimationTimeline.h"
34 #include "core/animation/Animation.h"
35 #include "core/animation/AnimationClock.h"
36 #include "core/animation/AnimationNode.h"
37 #include "core/animation/KeyframeEffectModel.h"
38 #include "core/dom/Document.h"
39 #include "core/dom/Element.h"
40 #include "core/dom/QualifiedName.h"
41 #include "platform/weborigin/KURL.h"
43 #include <gmock/gmock.h>
44 #include <gtest/gtest.h>
48 class MockPlatformTiming : public AnimationTimeline::PlatformTiming {
51 MOCK_METHOD1(wakeAfter, void(double));
52 MOCK_METHOD0(cancelWake, void());
53 MOCK_METHOD0(serviceOnNextFrame, void());
56 * AnimationTimelines should do one of the following things after servicing animations:
57 * - cancel the timer and not request to be woken again (expectNoMoreActions)
58 * - cancel the timer and request to be woken on the next frame (expectNextFrameAction)
59 * - cancel the timer and request to be woken at some point in the future (expectDelayedAction)
62 void expectNoMoreActions()
64 EXPECT_CALL(*this, cancelWake());
67 void expectNextFrameAction()
69 ::testing::Sequence sequence;
70 EXPECT_CALL(*this, cancelWake()).InSequence(sequence);
71 EXPECT_CALL(*this, serviceOnNextFrame()).InSequence(sequence);
74 void expectDelayedAction(double when)
76 ::testing::Sequence sequence;
77 EXPECT_CALL(*this, cancelWake()).InSequence(sequence);
78 EXPECT_CALL(*this, wakeAfter(when)).InSequence(sequence);
81 void trace(Visitor* visitor)
83 AnimationTimeline::PlatformTiming::trace(visitor);
87 class AnimationAnimationTimelineTest : public ::testing::Test {
91 document = Document::create();
92 document->animationClock().resetTimeForTesting();
93 element = Element::create(QualifiedName::null() , document.get());
94 platformTiming = new MockPlatformTiming;
95 timeline = AnimationTimeline::create(document.get(), adoptPtrWillBeNoop(platformTiming));
96 ASSERT_EQ(0, timeline->currentTimeInternal());
99 virtual void TearDown()
105 Heap::collectAllGarbage();
109 void updateClockAndService(double time)
111 document->animationClock().updateTime(time);
112 document->compositorPendingAnimations().update(false);
113 timeline->serviceAnimations(TimingUpdateForAnimationFrame);
116 RefPtrWillBePersistent<Document> document;
117 RefPtrWillBePersistent<Element> element;
118 RefPtrWillBePersistent<AnimationTimeline> timeline;
120 MockPlatformTiming* platformTiming;
127 double minimumDelay()
129 return AnimationTimeline::s_minimumDelay;
133 TEST_F(AnimationAnimationTimelineTest, HasStarted)
135 timeline = AnimationTimeline::create(document.get());
138 TEST_F(AnimationAnimationTimelineTest, EmptyKeyframeAnimation)
140 RefPtrWillBeRawPtr<AnimatableValueKeyframeEffectModel> effect = AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector());
141 RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), effect, timing);
143 timeline->play(anim.get());
145 platformTiming->expectNoMoreActions();
146 updateClockAndService(0);
147 EXPECT_FLOAT_EQ(0, timeline->currentTimeInternal());
148 EXPECT_FALSE(anim->isInEffect());
150 platformTiming->expectNoMoreActions();
151 updateClockAndService(100);
152 EXPECT_FLOAT_EQ(100, timeline->currentTimeInternal());
155 TEST_F(AnimationAnimationTimelineTest, EmptyForwardsKeyframeAnimation)
157 RefPtrWillBeRawPtr<AnimatableValueKeyframeEffectModel> effect = AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector());
158 timing.fillMode = Timing::FillModeForwards;
159 RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), effect, timing);
161 timeline->play(anim.get());
163 platformTiming->expectNoMoreActions();
164 updateClockAndService(0);
165 EXPECT_FLOAT_EQ(0, timeline->currentTimeInternal());
166 EXPECT_TRUE(anim->isInEffect());
168 platformTiming->expectNoMoreActions();
169 updateClockAndService(100);
170 EXPECT_FLOAT_EQ(100, timeline->currentTimeInternal());
173 TEST_F(AnimationAnimationTimelineTest, ZeroTime)
175 timeline = AnimationTimeline::create(document.get());
178 document->animationClock().updateTime(100);
179 EXPECT_EQ(100, timeline->currentTimeInternal());
180 EXPECT_EQ(100, timeline->currentTimeInternal(isNull));
181 EXPECT_FALSE(isNull);
183 document->animationClock().updateTime(200);
184 EXPECT_EQ(200, timeline->currentTimeInternal());
185 EXPECT_EQ(200, timeline->currentTimeInternal(isNull));
186 EXPECT_FALSE(isNull);
189 TEST_F(AnimationAnimationTimelineTest, PauseForTesting)
192 timing.fillMode = Timing::FillModeForwards;
193 RefPtrWillBeRawPtr<Animation> anim1 = Animation::create(element.get(), AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector()), timing);
194 RefPtrWillBeRawPtr<Animation> anim2 = Animation::create(element.get(), AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector()), timing);
195 AnimationPlayer* player1 = timeline->play(anim1.get());
196 AnimationPlayer* player2 = timeline->play(anim2.get());
197 timeline->pauseAnimationsForTesting(seekTime);
199 EXPECT_FLOAT_EQ(seekTime, player1->currentTime() / 1000.0);
200 EXPECT_FLOAT_EQ(seekTime, player2->currentTime() / 1000.0);
203 TEST_F(AnimationAnimationTimelineTest, DelayBeforeAnimationStart)
205 timing.iterationDuration = 2;
206 timing.startDelay = 5;
208 RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), nullptr, timing);
210 timeline->play(anim.get());
212 // TODO: Put the player startTime in the future when we add the capability to change player startTime
213 platformTiming->expectDelayedAction(timing.startDelay - minimumDelay());
214 updateClockAndService(0);
216 platformTiming->expectDelayedAction(timing.startDelay - minimumDelay() - 1.5);
217 updateClockAndService(1.5);
219 EXPECT_CALL(*platformTiming, serviceOnNextFrame());
222 platformTiming->expectNextFrameAction();
223 updateClockAndService(4.98);
226 TEST_F(AnimationAnimationTimelineTest, PlayAfterDocumentDeref)
228 timing.iterationDuration = 2;
229 timing.startDelay = 5;
231 timeline = &document->timeline();
235 RefPtrWillBeRawPtr<Animation> anim = Animation::create(0, nullptr, timing);
236 // Test passes if this does not crash.
237 timeline->play(anim.get());
240 TEST_F(AnimationAnimationTimelineTest, UseAnimationPlayerAfterTimelineDeref)
242 RefPtrWillBeRawPtr<AnimationPlayer> player = timeline->createAnimationPlayer(0);
244 // Test passes if this does not crash.
245 player->setStartTime(0);