Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / AnimationTimelineTest.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/AnimationTimeline.h"
33
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"
42
43 #include <gmock/gmock.h>
44 #include <gtest/gtest.h>
45
46 namespace blink {
47
48 class MockPlatformTiming : public AnimationTimeline::PlatformTiming {
49 public:
50
51     MOCK_METHOD1(wakeAfter, void(double));
52     MOCK_METHOD0(cancelWake, void());
53     MOCK_METHOD0(serviceOnNextFrame, void());
54
55     /**
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)
60      */
61
62     void expectNoMoreActions()
63     {
64         EXPECT_CALL(*this, cancelWake());
65     }
66
67     void expectNextFrameAction()
68     {
69         ::testing::Sequence sequence;
70         EXPECT_CALL(*this, cancelWake()).InSequence(sequence);
71         EXPECT_CALL(*this, serviceOnNextFrame()).InSequence(sequence);
72     }
73
74     void expectDelayedAction(double when)
75     {
76         ::testing::Sequence sequence;
77         EXPECT_CALL(*this, cancelWake()).InSequence(sequence);
78         EXPECT_CALL(*this, wakeAfter(when)).InSequence(sequence);
79     }
80
81     void trace(Visitor* visitor)
82     {
83         AnimationTimeline::PlatformTiming::trace(visitor);
84     }
85 };
86
87 class AnimationAnimationTimelineTest : public ::testing::Test {
88 protected:
89     virtual void SetUp()
90     {
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());
97     }
98
99     virtual void TearDown()
100     {
101         document.release();
102         element.release();
103         timeline.release();
104 #if ENABLE(OILPAN)
105         Heap::collectAllGarbage();
106 #endif
107     }
108
109     void updateClockAndService(double time)
110     {
111         document->animationClock().updateTime(time);
112         timeline->serviceAnimations(TimingUpdateForAnimationFrame);
113     }
114
115     RefPtrWillBePersistent<Document> document;
116     RefPtrWillBePersistent<Element> element;
117     RefPtrWillBePersistent<AnimationTimeline> timeline;
118     Timing timing;
119     MockPlatformTiming* platformTiming;
120
121     void wake()
122     {
123         timeline->wake();
124     }
125
126     double minimumDelay()
127     {
128         return AnimationTimeline::s_minimumDelay;
129     }
130 };
131
132 TEST_F(AnimationAnimationTimelineTest, HasStarted)
133 {
134     timeline = AnimationTimeline::create(document.get());
135 }
136
137 TEST_F(AnimationAnimationTimelineTest, EmptyKeyframeAnimation)
138 {
139     RefPtrWillBeRawPtr<AnimatableValueKeyframeEffectModel> effect = AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector());
140     RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), effect, timing);
141
142     timeline->play(anim.get());
143
144     platformTiming->expectNoMoreActions();
145     updateClockAndService(0);
146     EXPECT_FLOAT_EQ(0, timeline->currentTimeInternal());
147     EXPECT_FALSE(anim->isInEffect());
148
149     platformTiming->expectNoMoreActions();
150     updateClockAndService(100);
151     EXPECT_FLOAT_EQ(100, timeline->currentTimeInternal());
152 }
153
154 TEST_F(AnimationAnimationTimelineTest, EmptyForwardsKeyframeAnimation)
155 {
156     RefPtrWillBeRawPtr<AnimatableValueKeyframeEffectModel> effect = AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector());
157     timing.fillMode = Timing::FillModeForwards;
158     RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), effect, timing);
159
160     timeline->play(anim.get());
161
162     platformTiming->expectNoMoreActions();
163     updateClockAndService(0);
164     EXPECT_FLOAT_EQ(0, timeline->currentTimeInternal());
165     EXPECT_TRUE(anim->isInEffect());
166
167     platformTiming->expectNoMoreActions();
168     updateClockAndService(100);
169     EXPECT_FLOAT_EQ(100, timeline->currentTimeInternal());
170 }
171
172 TEST_F(AnimationAnimationTimelineTest, ZeroTime)
173 {
174     timeline = AnimationTimeline::create(document.get());
175     bool isNull;
176
177     document->animationClock().updateTime(100);
178     EXPECT_EQ(100, timeline->currentTimeInternal());
179     EXPECT_EQ(100, timeline->currentTimeInternal(isNull));
180     EXPECT_FALSE(isNull);
181
182     document->animationClock().updateTime(200);
183     EXPECT_EQ(200, timeline->currentTimeInternal());
184     EXPECT_EQ(200, timeline->currentTimeInternal(isNull));
185     EXPECT_FALSE(isNull);
186 }
187
188 TEST_F(AnimationAnimationTimelineTest, PauseForTesting)
189 {
190     float seekTime = 1;
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);
197
198     EXPECT_FLOAT_EQ(seekTime, player1->currentTimeInternal());
199     EXPECT_FLOAT_EQ(seekTime, player2->currentTimeInternal());
200 }
201
202 TEST_F(AnimationAnimationTimelineTest, DelayBeforeAnimationStart)
203 {
204     timing.iterationDuration = 2;
205     timing.startDelay = 5;
206
207     RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), nullptr, timing);
208
209     timeline->play(anim.get());
210
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);
214
215     platformTiming->expectDelayedAction(timing.startDelay - minimumDelay() - 1.5);
216     updateClockAndService(1.5);
217
218     EXPECT_CALL(*platformTiming, serviceOnNextFrame());
219     wake();
220
221     platformTiming->expectNextFrameAction();
222     updateClockAndService(4.98);
223 }
224
225 TEST_F(AnimationAnimationTimelineTest, PlayAfterDocumentDeref)
226 {
227     timing.iterationDuration = 2;
228     timing.startDelay = 5;
229
230     timeline = &document->timeline();
231     element = nullptr;
232     document = nullptr;
233
234     RefPtrWillBeRawPtr<Animation> anim = Animation::create(0, nullptr, timing);
235     // Test passes if this does not crash.
236     timeline->play(anim.get());
237 }
238
239 TEST_F(AnimationAnimationTimelineTest, UseAnimationPlayerAfterTimelineDeref)
240 {
241     RefPtrWillBeRawPtr<AnimationPlayer> player = timeline->createAnimationPlayer(0);
242     timeline.clear();
243     // Test passes if this does not crash.
244     player->setStartTime(0);
245 }
246
247 }