Upstream version 10.39.225.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         document->compositorPendingAnimations().update(false);
113         timeline->serviceAnimations(TimingUpdateForAnimationFrame);
114     }
115
116     RefPtrWillBePersistent<Document> document;
117     RefPtrWillBePersistent<Element> element;
118     RefPtrWillBePersistent<AnimationTimeline> timeline;
119     Timing timing;
120     MockPlatformTiming* platformTiming;
121
122     void wake()
123     {
124         timeline->wake();
125     }
126
127     double minimumDelay()
128     {
129         return AnimationTimeline::s_minimumDelay;
130     }
131 };
132
133 TEST_F(AnimationAnimationTimelineTest, HasStarted)
134 {
135     timeline = AnimationTimeline::create(document.get());
136 }
137
138 TEST_F(AnimationAnimationTimelineTest, EmptyKeyframeAnimation)
139 {
140     RefPtrWillBeRawPtr<AnimatableValueKeyframeEffectModel> effect = AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector());
141     RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), effect, timing);
142
143     timeline->play(anim.get());
144
145     platformTiming->expectNoMoreActions();
146     updateClockAndService(0);
147     EXPECT_FLOAT_EQ(0, timeline->currentTimeInternal());
148     EXPECT_FALSE(anim->isInEffect());
149
150     platformTiming->expectNoMoreActions();
151     updateClockAndService(100);
152     EXPECT_FLOAT_EQ(100, timeline->currentTimeInternal());
153 }
154
155 TEST_F(AnimationAnimationTimelineTest, EmptyForwardsKeyframeAnimation)
156 {
157     RefPtrWillBeRawPtr<AnimatableValueKeyframeEffectModel> effect = AnimatableValueKeyframeEffectModel::create(AnimatableValueKeyframeVector());
158     timing.fillMode = Timing::FillModeForwards;
159     RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), effect, timing);
160
161     timeline->play(anim.get());
162
163     platformTiming->expectNoMoreActions();
164     updateClockAndService(0);
165     EXPECT_FLOAT_EQ(0, timeline->currentTimeInternal());
166     EXPECT_TRUE(anim->isInEffect());
167
168     platformTiming->expectNoMoreActions();
169     updateClockAndService(100);
170     EXPECT_FLOAT_EQ(100, timeline->currentTimeInternal());
171 }
172
173 TEST_F(AnimationAnimationTimelineTest, ZeroTime)
174 {
175     timeline = AnimationTimeline::create(document.get());
176     bool isNull;
177
178     document->animationClock().updateTime(100);
179     EXPECT_EQ(100, timeline->currentTimeInternal());
180     EXPECT_EQ(100, timeline->currentTimeInternal(isNull));
181     EXPECT_FALSE(isNull);
182
183     document->animationClock().updateTime(200);
184     EXPECT_EQ(200, timeline->currentTimeInternal());
185     EXPECT_EQ(200, timeline->currentTimeInternal(isNull));
186     EXPECT_FALSE(isNull);
187 }
188
189 TEST_F(AnimationAnimationTimelineTest, PauseForTesting)
190 {
191     float seekTime = 1;
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);
198
199     EXPECT_FLOAT_EQ(seekTime, player1->currentTimeInternal());
200     EXPECT_FLOAT_EQ(seekTime, player2->currentTimeInternal());
201 }
202
203 TEST_F(AnimationAnimationTimelineTest, DelayBeforeAnimationStart)
204 {
205     timing.iterationDuration = 2;
206     timing.startDelay = 5;
207
208     RefPtrWillBeRawPtr<Animation> anim = Animation::create(element.get(), nullptr, timing);
209
210     timeline->play(anim.get());
211
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);
215
216     platformTiming->expectDelayedAction(timing.startDelay - minimumDelay() - 1.5);
217     updateClockAndService(1.5);
218
219     EXPECT_CALL(*platformTiming, serviceOnNextFrame());
220     wake();
221
222     platformTiming->expectNextFrameAction();
223     updateClockAndService(4.98);
224 }
225
226 TEST_F(AnimationAnimationTimelineTest, PlayAfterDocumentDeref)
227 {
228     timing.iterationDuration = 2;
229     timing.startDelay = 5;
230
231     timeline = &document->timeline();
232     element = nullptr;
233     document = nullptr;
234
235     RefPtrWillBeRawPtr<Animation> anim = Animation::create(0, nullptr, timing);
236     // Test passes if this does not crash.
237     timeline->play(anim.get());
238 }
239
240 TEST_F(AnimationAnimationTimelineTest, UseAnimationPlayerAfterTimelineDeref)
241 {
242     RefPtrWillBeRawPtr<AnimationPlayer> player = timeline->createAnimationPlayer(0);
243     timeline.clear();
244     // Test passes if this does not crash.
245     player->setStartTime(0);
246 }
247
248 }