Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / AnimationStackTest.cpp
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/animation/AnimationStack.h"
7
8 #include "core/animation/ActiveAnimations.h"
9 #include "core/animation/AnimatableDouble.h"
10 #include "core/animation/AnimationClock.h"
11 #include "core/animation/DocumentTimeline.h"
12 #include "core/animation/KeyframeEffectModel.h"
13 #include <gtest/gtest.h>
14
15 namespace WebCore {
16
17 class AnimationAnimationStackTest : public ::testing::Test {
18 protected:
19     virtual void SetUp()
20     {
21         document = Document::create();
22         document->animationClock().resetTimeForTesting();
23         timeline = DocumentTimeline::create(document.get());
24         timeline->setZeroTime(0);
25         element = document->createElement("foo", ASSERT_NO_EXCEPTION);
26     }
27
28     AnimationPlayer* play(Animation* animation, double startTime)
29     {
30         AnimationPlayer* player = timeline->createAnimationPlayer(animation);
31         player->setStartTimeInternal(startTime);
32         player->update(TimingUpdateOnDemand);
33         return player;
34     }
35
36     void updateTimeline(double time)
37     {
38         document->animationClock().updateTime(time);
39         timeline->serviceAnimations(TimingUpdateForAnimationFrame);
40     }
41
42     const Vector<OwnPtr<SampledEffect> >& effects()
43     {
44         return element->ensureActiveAnimations().defaultStack().m_effects;
45     }
46
47     PassRefPtrWillBeRawPtr<AnimationEffect> makeAnimationEffect(CSSPropertyID id, PassRefPtrWillBeRawPtr<AnimatableValue> value)
48     {
49         AnimatableValueKeyframeVector keyframes(2);
50         keyframes[0] = AnimatableValueKeyframe::create();
51         keyframes[0]->setOffset(0.0);
52         keyframes[0]->setPropertyValue(id, value.get());
53         keyframes[1] = AnimatableValueKeyframe::create();
54         keyframes[1]->setOffset(1.0);
55         keyframes[1]->setPropertyValue(id, value.get());
56         return AnimatableValueKeyframeEffectModel::create(keyframes);
57     }
58
59     PassRefPtr<InertAnimation> makeInertAnimation(PassRefPtrWillBeRawPtr<AnimationEffect> effect)
60     {
61         Timing timing;
62         timing.fillMode = Timing::FillModeBoth;
63         return InertAnimation::create(effect, timing, false);
64     }
65
66     PassRefPtr<Animation> makeAnimation(PassRefPtrWillBeRawPtr<AnimationEffect> effect, double duration = 10)
67     {
68         Timing timing;
69         timing.fillMode = Timing::FillModeBoth;
70         timing.iterationDuration = duration;
71         return Animation::create(element.get(), effect, timing);
72     }
73
74     AnimatableValue* interpolationValue(Interpolation* interpolation)
75     {
76         return toLegacyStyleInterpolation(interpolation)->currentValue().get();
77     }
78
79     RefPtr<Document> document;
80     RefPtr<DocumentTimeline> timeline;
81     RefPtr<Element> element;
82 };
83
84 TEST_F(AnimationAnimationStackTest, ActiveAnimationsSorted)
85 {
86     play(makeAnimation(makeAnimationEffect(CSSPropertyFontSize, AnimatableDouble::create(1))).get(), 10);
87     play(makeAnimation(makeAnimationEffect(CSSPropertyFontSize, AnimatableDouble::create(2))).get(), 15);
88     play(makeAnimation(makeAnimationEffect(CSSPropertyFontSize, AnimatableDouble::create(3))).get(), 5);
89     WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > result = AnimationStack::activeInterpolations(&element->activeAnimations()->defaultStack(), 0, 0, Animation::DefaultPriority, 0);
90     EXPECT_EQ(1u, result.size());
91     EXPECT_TRUE(interpolationValue(result.get(CSSPropertyFontSize))->equals(AnimatableDouble::create(2).get()));
92 }
93
94 TEST_F(AnimationAnimationStackTest, NewAnimations)
95 {
96     play(makeAnimation(makeAnimationEffect(CSSPropertyFontSize, AnimatableDouble::create(1))).get(), 15);
97     play(makeAnimation(makeAnimationEffect(CSSPropertyZIndex, AnimatableDouble::create(2))).get(), 10);
98     Vector<InertAnimation*> newAnimations;
99     RefPtr<InertAnimation> inert1 = makeInertAnimation(makeAnimationEffect(CSSPropertyFontSize, AnimatableDouble::create(3)));
100     RefPtr<InertAnimation> inert2 = makeInertAnimation(makeAnimationEffect(CSSPropertyZIndex, AnimatableDouble::create(4)));
101     newAnimations.append(inert1.get());
102     newAnimations.append(inert2.get());
103     WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > result = AnimationStack::activeInterpolations(&element->activeAnimations()->defaultStack(), &newAnimations, 0, Animation::DefaultPriority, 10);
104     EXPECT_EQ(2u, result.size());
105     EXPECT_TRUE(interpolationValue(result.get(CSSPropertyFontSize))->equals(AnimatableDouble::create(1).get()));
106     EXPECT_TRUE(interpolationValue(result.get(CSSPropertyZIndex))->equals(AnimatableDouble::create(4).get()));
107 }
108
109 TEST_F(AnimationAnimationStackTest, CancelledAnimationPlayers)
110 {
111     HashSet<const AnimationPlayer*> cancelledAnimationPlayers;
112     RefPtr<AnimationPlayer> player = play(makeAnimation(makeAnimationEffect(CSSPropertyFontSize, AnimatableDouble::create(1))).get(), 0);
113     cancelledAnimationPlayers.add(player.get());
114     play(makeAnimation(makeAnimationEffect(CSSPropertyZIndex, AnimatableDouble::create(2))).get(), 0);
115     WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > result = AnimationStack::activeInterpolations(&element->activeAnimations()->defaultStack(), 0, &cancelledAnimationPlayers, Animation::DefaultPriority, 0);
116     EXPECT_EQ(1u, result.size());
117     EXPECT_TRUE(interpolationValue(result.get(CSSPropertyZIndex))->equals(AnimatableDouble::create(2).get()));
118 }
119
120 TEST_F(AnimationAnimationStackTest, ForwardsFillDiscarding)
121 {
122     play(makeAnimation(makeAnimationEffect(CSSPropertyFontSize, AnimatableDouble::create(1))).get(), 2);
123     play(makeAnimation(makeAnimationEffect(CSSPropertyFontSize, AnimatableDouble::create(2))).get(), 6);
124     play(makeAnimation(makeAnimationEffect(CSSPropertyFontSize, AnimatableDouble::create(3))).get(), 4);
125     document->compositorPendingAnimations().startPendingAnimations();
126     WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > interpolations;
127
128     updateTimeline(11);
129     interpolations = AnimationStack::activeInterpolations(&element->activeAnimations()->defaultStack(), 0, 0, Animation::DefaultPriority, 0);
130     EXPECT_TRUE(interpolationValue(interpolations.get(CSSPropertyFontSize))->equals(AnimatableDouble::create(2).get()));
131     EXPECT_EQ(3u, effects().size());
132     EXPECT_EQ(1u, interpolations.size());
133     EXPECT_EQ(2, effects()[0]->sortInfo().startTime());
134     EXPECT_EQ(4, effects()[1]->sortInfo().startTime());
135     EXPECT_EQ(6, effects()[2]->sortInfo().startTime());
136
137     updateTimeline(13);
138     interpolations = AnimationStack::activeInterpolations(&element->activeAnimations()->defaultStack(), 0, 0, Animation::DefaultPriority, 0);
139     EXPECT_TRUE(interpolationValue(interpolations.get(CSSPropertyFontSize))->equals(AnimatableDouble::create(2).get()));
140     EXPECT_EQ(3u, effects().size());
141     EXPECT_EQ(2, effects()[0]->sortInfo().startTime());
142     EXPECT_EQ(4, effects()[1]->sortInfo().startTime());
143     EXPECT_EQ(6, effects()[2]->sortInfo().startTime());
144
145     updateTimeline(15);
146     interpolations = AnimationStack::activeInterpolations(&element->activeAnimations()->defaultStack(), 0, 0, Animation::DefaultPriority, 0);
147     EXPECT_TRUE(interpolationValue(interpolations.get(CSSPropertyFontSize))->equals(AnimatableDouble::create(2).get()));
148     EXPECT_EQ(2u, effects().size());
149     EXPECT_EQ(4, effects()[0]->sortInfo().startTime());
150     EXPECT_EQ(6, effects()[1]->sortInfo().startTime());
151
152     updateTimeline(17);
153     interpolations = AnimationStack::activeInterpolations(&element->activeAnimations()->defaultStack(), 0, 0, Animation::DefaultPriority, 0);
154     EXPECT_TRUE(interpolationValue(interpolations.get(CSSPropertyFontSize))->equals(AnimatableDouble::create(2).get()));
155     EXPECT_EQ(1u, effects().size());
156     EXPECT_EQ(6, effects()[0]->sortInfo().startTime());
157 }
158
159 }