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.
6 #include "core/animation/AnimationStack.h"
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>
17 class AnimationAnimationStackTest : public ::testing::Test {
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);
28 AnimationPlayer* play(Animation* animation, double startTime)
30 AnimationPlayer* player = timeline->createAnimationPlayer(animation);
31 player->setStartTimeInternal(startTime);
32 player->update(TimingUpdateOnDemand);
36 void updateTimeline(double time)
38 document->animationClock().updateTime(time);
39 timeline->serviceAnimations(TimingUpdateForAnimationFrame);
42 const Vector<OwnPtr<SampledEffect> >& effects()
44 return element->ensureActiveAnimations().defaultStack().m_effects;
47 PassRefPtrWillBeRawPtr<AnimationEffect> makeAnimationEffect(CSSPropertyID id, PassRefPtrWillBeRawPtr<AnimatableValue> value)
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);
59 PassRefPtr<InertAnimation> makeInertAnimation(PassRefPtrWillBeRawPtr<AnimationEffect> effect)
62 timing.fillMode = Timing::FillModeBoth;
63 return InertAnimation::create(effect, timing, false);
66 PassRefPtr<Animation> makeAnimation(PassRefPtrWillBeRawPtr<AnimationEffect> effect, double duration = 10)
69 timing.fillMode = Timing::FillModeBoth;
70 timing.iterationDuration = duration;
71 return Animation::create(element.get(), effect, timing);
74 AnimatableValue* interpolationValue(Interpolation* interpolation)
76 return toLegacyStyleInterpolation(interpolation)->currentValue().get();
79 RefPtr<Document> document;
80 RefPtr<DocumentTimeline> timeline;
81 RefPtr<Element> element;
84 TEST_F(AnimationAnimationStackTest, ActiveAnimationsSorted)
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()));
94 TEST_F(AnimationAnimationStackTest, NewAnimations)
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()));
109 TEST_F(AnimationAnimationStackTest, CancelledAnimationPlayers)
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()));
120 TEST_F(AnimationAnimationStackTest, ForwardsFillDiscarding)
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;
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());
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());
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());
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());