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/InterpolationEffect.h"
8 #include <gtest/gtest.h>
12 const double duration = 1.0;
18 class AnimationInterpolationEffectTest : public ::testing::Test {
20 InterpolableValue* interpolationValue(Interpolation& interpolation)
22 return interpolation.getCachedValueForTesting();
25 double getInterpolableNumber(PassRefPtrWillBeRawPtr<Interpolation> value)
27 return toInterpolableNumber(interpolationValue(*value.get()))->value();
31 TEST_F(AnimationInterpolationEffectTest, SingleInterpolation)
33 RefPtrWillBeRawPtr<InterpolationEffect> interpolationEffect = InterpolationEffect::create();
34 interpolationEffect->addInterpolation(Interpolation::create(InterpolableNumber::create(0), InterpolableNumber::create(10)),
35 RefPtr<TimingFunction>(), 0, 1, -1, 2);
37 OwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > activeInterpolations = interpolationEffect->getActiveInterpolations(-2, duration);
38 EXPECT_EQ(0ul, activeInterpolations->size());
40 activeInterpolations = interpolationEffect->getActiveInterpolations(-0.5, duration);
41 EXPECT_EQ(1ul, activeInterpolations->size());
42 EXPECT_EQ(-5, getInterpolableNumber(activeInterpolations->at(0)));
44 activeInterpolations = interpolationEffect->getActiveInterpolations(0.5, duration);
45 EXPECT_EQ(1ul, activeInterpolations->size());
46 EXPECT_FLOAT_EQ(5, getInterpolableNumber(activeInterpolations->at(0)));
48 activeInterpolations = interpolationEffect->getActiveInterpolations(1.5, duration);
49 EXPECT_EQ(1ul, activeInterpolations->size());
50 EXPECT_FLOAT_EQ(15, getInterpolableNumber(activeInterpolations->at(0)));
52 activeInterpolations = interpolationEffect->getActiveInterpolations(3, duration);
53 EXPECT_EQ(0ul, activeInterpolations->size());
56 TEST_F(AnimationInterpolationEffectTest, MultipleInterpolations)
58 RefPtrWillBeRawPtr<InterpolationEffect> interpolationEffect = InterpolationEffect::create();
59 interpolationEffect->addInterpolation(Interpolation::create(InterpolableNumber::create(10), InterpolableNumber::create(15)),
60 RefPtr<TimingFunction>(), 1, 2, 1, 3);
61 interpolationEffect->addInterpolation(Interpolation::create(InterpolableNumber::create(0), InterpolableNumber::create(1)),
62 LinearTimingFunction::shared(), 0, 1, 0, 1);
63 interpolationEffect->addInterpolation(Interpolation::create(InterpolableNumber::create(1), InterpolableNumber::create(6)),
64 CubicBezierTimingFunction::preset(CubicBezierTimingFunction::Ease), 0.5, 1.5, 0.5, 1.5);
66 OwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > activeInterpolations = interpolationEffect->getActiveInterpolations(-0.5, duration);
67 EXPECT_EQ(0ul, activeInterpolations->size());
69 activeInterpolations = interpolationEffect->getActiveInterpolations(0, duration);
70 EXPECT_EQ(1ul, activeInterpolations->size());
71 EXPECT_FLOAT_EQ(0, getInterpolableNumber(activeInterpolations->at(0)));
73 activeInterpolations = interpolationEffect->getActiveInterpolations(0.5, duration);
74 EXPECT_EQ(2ul, activeInterpolations->size());
75 EXPECT_FLOAT_EQ(0.5f, getInterpolableNumber(activeInterpolations->at(0)));
76 EXPECT_FLOAT_EQ(1, getInterpolableNumber(activeInterpolations->at(1)));
78 activeInterpolations = interpolationEffect->getActiveInterpolations(1, duration);
79 EXPECT_EQ(2ul, activeInterpolations->size());
80 EXPECT_FLOAT_EQ(10, getInterpolableNumber(activeInterpolations->at(0)));
81 EXPECT_FLOAT_EQ(5.0282884f, getInterpolableNumber(activeInterpolations->at(1)));
83 activeInterpolations = interpolationEffect->getActiveInterpolations(1, duration * 1000);
84 EXPECT_EQ(2ul, activeInterpolations->size());
85 EXPECT_FLOAT_EQ(10, getInterpolableNumber(activeInterpolations->at(0)));
86 EXPECT_FLOAT_EQ(5.0120168f, getInterpolableNumber(activeInterpolations->at(1)));
88 activeInterpolations = interpolationEffect->getActiveInterpolations(1.5, duration);
89 EXPECT_EQ(1ul, activeInterpolations->size());
90 EXPECT_FLOAT_EQ(12.5f, getInterpolableNumber(activeInterpolations->at(0)));
92 activeInterpolations = interpolationEffect->getActiveInterpolations(2, duration);
93 EXPECT_EQ(1ul, activeInterpolations->size());
94 EXPECT_FLOAT_EQ(15, getInterpolableNumber(activeInterpolations->at(0)));