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/DocumentTimeline.h"
34 #include "core/animation/Animation.h"
35 #include "core/animation/AnimationClock.h"
36 #include "core/animation/KeyframeEffectModel.h"
37 #include "core/animation/TimedItem.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 DocumentTimeline::PlatformTiming {
51 MOCK_METHOD1(wakeAfter, void(double));
52 MOCK_METHOD0(cancelWake, void());
53 MOCK_METHOD0(serviceOnNextFrame, void());
56 * DocumentTimelines 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);
82 class AnimationDocumentTimelineTest : public ::testing::Test {
86 document = Document::create();
87 document->animationClock().resetTimeForTesting();
88 element = Element::create(nullQName() , document.get());
89 platformTiming = new MockPlatformTiming;
90 timeline = DocumentTimeline::create(document.get(), adoptPtr(platformTiming));
91 timeline->setZeroTime(0);
92 ASSERT_EQ(0, timeline->currentTime());
95 virtual void TearDown()
102 void updateClockAndService(double time)
104 document->animationClock().updateTime(time);
105 timeline->serviceAnimations();
108 RefPtr<Document> document;
109 RefPtr<Element> element;
110 RefPtr<DocumentTimeline> timeline;
112 MockPlatformTiming* platformTiming;
119 double minimumDelay()
121 return DocumentTimeline::s_minimumDelay;
125 TEST_F(AnimationDocumentTimelineTest, HasStarted)
127 timeline = DocumentTimeline::create(document.get());
128 EXPECT_FALSE(timeline->hasStarted());
129 timeline->setZeroTime(0);
130 EXPECT_TRUE(timeline->hasStarted());
133 TEST_F(AnimationDocumentTimelineTest, EmptyKeyframeAnimation)
135 RefPtr<KeyframeEffectModel> effect = KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector());
136 RefPtr<Animation> anim = Animation::create(element.get(), effect, timing);
138 timeline->play(anim.get());
140 platformTiming->expectNoMoreActions();
141 updateClockAndService(0);
142 EXPECT_FLOAT_EQ(0, timeline->currentTime());
143 EXPECT_FALSE(anim->isInEffect());
145 platformTiming->expectNoMoreActions();
146 updateClockAndService(100);
147 EXPECT_FLOAT_EQ(100, timeline->currentTime());
150 TEST_F(AnimationDocumentTimelineTest, EmptyForwardsKeyframeAnimation)
152 RefPtr<KeyframeEffectModel> effect = KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector());
153 timing.fillMode = Timing::FillModeForwards;
154 RefPtr<Animation> anim = Animation::create(element.get(), effect, timing);
156 timeline->play(anim.get());
158 platformTiming->expectNoMoreActions();
159 updateClockAndService(0);
160 EXPECT_FLOAT_EQ(0, timeline->currentTime());
161 EXPECT_TRUE(anim->isInEffect());
162 EXPECT_TRUE(anim->compositableValues()->isEmpty());
164 platformTiming->expectNoMoreActions();
165 updateClockAndService(100);
166 EXPECT_FLOAT_EQ(100, timeline->currentTime());
169 TEST_F(AnimationDocumentTimelineTest, EmptyTimelineDoesNotTriggerStyleRecalc)
171 document->animationClock().updateTime(100);
172 EXPECT_FALSE(timeline->serviceAnimations());
175 TEST_F(AnimationDocumentTimelineTest, EmptyPlayerDoesNotTriggerStyleRecalc)
178 document->animationClock().updateTime(100);
179 EXPECT_FALSE(timeline->serviceAnimations());
182 TEST_F(AnimationDocumentTimelineTest, EmptyTargetDoesNotTriggerStyleRecalc)
184 timing.iterationDuration = 200;
185 timeline->play(Animation::create(0, KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector()), timing).get());
186 document->animationClock().updateTime(100);
187 EXPECT_FALSE(timeline->serviceAnimations());
190 TEST_F(AnimationDocumentTimelineTest, EmptyEffectDoesNotTriggerStyleRecalc)
192 timeline->play(Animation::create(element.get(), 0, timing).get());
193 document->animationClock().updateTime(100);
194 EXPECT_FALSE(timeline->serviceAnimations());
197 TEST_F(AnimationDocumentTimelineTest, TriggerStyleRecalc)
199 timing.iterationDuration = 200;
200 timeline->play(Animation::create(element.get(), KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector()), timing).get());
201 document->animationClock().updateTime(100);
202 EXPECT_TRUE(timeline->serviceAnimations());
205 TEST_F(AnimationDocumentTimelineTest, ZeroTime)
207 timeline = DocumentTimeline::create(document.get());
209 document->animationClock().updateTime(100);
210 EXPECT_TRUE(isNull(timeline->currentTime()));
212 document->animationClock().updateTime(200);
213 EXPECT_TRUE(isNull(timeline->currentTime()));
215 timeline->setZeroTime(300);
216 document->animationClock().updateTime(300);
217 EXPECT_EQ(0, timeline->currentTime());
219 document->animationClock().updateTime(400);
220 EXPECT_EQ(100, timeline->currentTime());
223 TEST_F(AnimationDocumentTimelineTest, PauseForTesting)
226 timing.fillMode = Timing::FillModeForwards;
227 RefPtr<Animation> anim1 = Animation::create(element.get(), KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector()), timing);
228 RefPtr<Animation> anim2 = Animation::create(element.get(), KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector()), timing);
229 Player* player1 = timeline->play(anim1.get());
230 Player* player2 = timeline->play(anim2.get());
231 timeline->pauseAnimationsForTesting(seekTime);
233 EXPECT_FLOAT_EQ(seekTime, player1->currentTime());
234 EXPECT_FLOAT_EQ(seekTime, player2->currentTime());
237 TEST_F(AnimationDocumentTimelineTest, NumberOfActiveAnimations)
239 Timing timingForwardFill;
240 timingForwardFill.iterationDuration = 2;
241 timingForwardFill.fillMode = Timing::FillModeForwards;
244 timingNoFill.iterationDuration = 2;
245 timingNoFill.fillMode = Timing::FillModeNone;
247 Timing timingBackwardFillDelay;
248 timingBackwardFillDelay.iterationDuration = 1;
249 timingBackwardFillDelay.fillMode = Timing::FillModeBackwards;
250 timingBackwardFillDelay.startDelay = 1;
252 Timing timingNoFillDelay;
253 timingNoFillDelay.iterationDuration = 1;
254 timingNoFillDelay.fillMode = Timing::FillModeNone;
255 timingNoFillDelay.startDelay = 1;
257 Timing timingAutoFill;
258 timingAutoFill.iterationDuration = 2;
260 RefPtr<Animation> anim1 = Animation::create(element.get(), KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector()), timingForwardFill);
261 RefPtr<Animation> anim2 = Animation::create(element.get(), KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector()), timingNoFill);
262 RefPtr<Animation> anim3 = Animation::create(element.get(), KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector()), timingBackwardFillDelay);
263 RefPtr<Animation> anim4 = Animation::create(element.get(), KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector()), timingNoFillDelay);
264 RefPtr<Animation> anim5 = Animation::create(element.get(), KeyframeEffectModel::create(KeyframeEffectModel::KeyframeVector()), timingAutoFill);
266 timeline->play(anim1.get());
267 timeline->play(anim2.get());
268 timeline->play(anim3.get());
269 timeline->play(anim4.get());
270 timeline->play(anim5.get());
272 platformTiming->expectNextFrameAction();
273 updateClockAndService(0);
274 EXPECT_EQ(5U, timeline->numberOfActiveAnimationsForTesting());
275 platformTiming->expectNextFrameAction();
276 updateClockAndService(0.5);
277 EXPECT_EQ(5U, timeline->numberOfActiveAnimationsForTesting());
278 platformTiming->expectNextFrameAction();
279 updateClockAndService(1.5);
280 EXPECT_EQ(5U, timeline->numberOfActiveAnimationsForTesting());
281 platformTiming->expectNoMoreActions();
282 updateClockAndService(3);
283 EXPECT_EQ(1U, timeline->numberOfActiveAnimationsForTesting());
286 TEST_F(AnimationDocumentTimelineTest, DelayBeforeAnimationStart)
288 timing.iterationDuration = 2;
289 timing.startDelay = 5;
291 RefPtr<Animation> anim = Animation::create(element.get(), 0, timing);
293 timeline->play(anim.get());
295 // TODO: Put the player startTime in the future when we add the capability to change player startTime
296 platformTiming->expectDelayedAction(timing.startDelay - minimumDelay());
297 updateClockAndService(0);
299 platformTiming->expectDelayedAction(timing.startDelay - minimumDelay() - 1.5);
300 updateClockAndService(1.5);
302 EXPECT_CALL(*platformTiming, serviceOnNextFrame());
305 platformTiming->expectNextFrameAction();
306 updateClockAndService(4.98);
309 TEST_F(AnimationDocumentTimelineTest, PlayAfterDocumentDeref)
311 timing.iterationDuration = 2;
312 timing.startDelay = 5;
314 timeline = document->timeline();
318 RefPtr<Animation> anim = Animation::create(0, 0, timing);
319 // Test passes if this does not crash.
320 timeline->play(anim.get());
323 TEST_F(AnimationDocumentTimelineTest, UsePlayerAfterTimelineDeref)
325 RefPtr<Player> player = timeline->createPlayer(0);
327 // Test passes if this does not crash.
328 player->setStartTime(0);