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 timeline->serviceAnimations(TimingUpdateForAnimationFrame);
115 RefPtrWillBePersistent<Document> document;
116 RefPtrWillBePersistent<Element> element;
117 RefPtrWillBePersistent<AnimationTimeline> timeline;
119 MockPlatformTiming* platformTiming;
126 double minimumDelay()
128 return AnimationTimeline::s_minimumDelay;
132 TEST_F(AnimationAnimationTimelineTest, HasStarted)
134 timeline = AnimationTimeline::create(document.get());
137 TEST_F(AnimationAnimationTimelineTest, EmptyKeyframeAnimation)
139 RefPtrWillBeRawPtr<AnimatableValueKeyframeEffectModel> effect = AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector());
140 RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), effect, timing);
142 timeline->play(anim.get());
144 platformTiming->expectNoMoreActions();
145 updateClockAndService(0);
146 EXPECT_FLOAT_EQ(0, timeline->currentTimeInternal());
147 EXPECT_FALSE(anim->isInEffect());
149 platformTiming->expectNoMoreActions();
150 updateClockAndService(100);
151 EXPECT_FLOAT_EQ(100, timeline->currentTimeInternal());
154 TEST_F(AnimationAnimationTimelineTest, EmptyForwardsKeyframeAnimation)
156 RefPtrWillBeRawPtr<AnimatableValueKeyframeEffectModel> effect = AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector());
157 timing.fillMode = Timing::FillModeForwards;
158 RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), effect, timing);
160 timeline->play(anim.get());
162 platformTiming->expectNoMoreActions();
163 updateClockAndService(0);
164 EXPECT_FLOAT_EQ(0, timeline->currentTimeInternal());
165 EXPECT_TRUE(anim->isInEffect());
167 platformTiming->expectNoMoreActions();
168 updateClockAndService(100);
169 EXPECT_FLOAT_EQ(100, timeline->currentTimeInternal());
172 TEST_F(AnimationAnimationTimelineTest, ZeroTime)
174 timeline = AnimationTimeline::create(document.get());
177 document->animationClock().updateTime(100);
178 EXPECT_EQ(100, timeline->currentTimeInternal());
179 EXPECT_EQ(100, timeline->currentTimeInternal(isNull));
180 EXPECT_FALSE(isNull);
182 document->animationClock().updateTime(200);
183 EXPECT_EQ(200, timeline->currentTimeInternal());
184 EXPECT_EQ(200, timeline->currentTimeInternal(isNull));
185 EXPECT_FALSE(isNull);
188 TEST_F(AnimationAnimationTimelineTest, PauseForTesting)
191 timing.fillMode = Timing::FillModeForwards;
192 RefPtrWillBeRawPtr<Animation> anim1 = Animation::create(element.get(), AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector()), timing);
193 RefPtrWillBeRawPtr<Animation> anim2 = Animation::create(element.get(), AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector()), timing);
194 AnimationPlayer* player1 = timeline->play(anim1.get());
195 AnimationPlayer* player2 = timeline->play(anim2.get());
196 timeline->pauseAnimationsForTesting(seekTime);
198 EXPECT_FLOAT_EQ(seekTime, player1->currentTimeInternal());
199 EXPECT_FLOAT_EQ(seekTime, player2->currentTimeInternal());
202 TEST_F(AnimationAnimationTimelineTest, DelayBeforeAnimationStart)
204 timing.iterationDuration = 2;
205 timing.startDelay = 5;
207 RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), nullptr, timing);
209 timeline->play(anim.get());
211 // TODO: Put the player startTime in the future when we add the capability to change player startTime
212 platformTiming->expectDelayedAction(timing.startDelay - minimumDelay());
213 updateClockAndService(0);
215 platformTiming->expectDelayedAction(timing.startDelay - minimumDelay() - 1.5);
216 updateClockAndService(1.5);
218 EXPECT_CALL(*platformTiming, serviceOnNextFrame());
221 platformTiming->expectNextFrameAction();
222 updateClockAndService(4.98);
225 TEST_F(AnimationAnimationTimelineTest, PlayAfterDocumentDeref)
227 timing.iterationDuration = 2;
228 timing.startDelay = 5;
230 timeline = &document->timeline();
234 RefPtrWillBeRawPtr<Animation> anim = Animation::create(0, nullptr, timing);
235 // Test passes if this does not crash.
236 timeline->play(anim.get());
239 TEST_F(AnimationAnimationTimelineTest, UseAnimationPlayerAfterTimelineDeref)
241 RefPtrWillBeRawPtr<AnimationPlayer> player = timeline->createAnimationPlayer(0);
243 // Test passes if this does not crash.
244 player->setStartTime(0);