1 // Copyright 2012 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.
5 #include "cc/animation/keyframed_animation_curve.h"
7 #include "cc/animation/transform_operations.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 void ExpectTranslateX(double translate_x
, const gfx::Transform
& transform
) {
15 EXPECT_FLOAT_EQ(translate_x
, transform
.matrix().getDouble(0, 3));
18 // Tests that a float animation with one keyframe works as expected.
19 TEST(KeyframedAnimationCurveTest
, OneFloatKeyframe
) {
20 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
21 KeyframedFloatAnimationCurve::Create());
23 FloatKeyframe::Create(0.0, 2.f
, scoped_ptr
<TimingFunction
>()));
24 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(-1.f
));
25 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(0.f
));
26 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(0.5f
));
27 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(1.f
));
28 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(2.f
));
31 // Tests that a float animation with two keyframes works as expected.
32 TEST(KeyframedAnimationCurveTest
, TwoFloatKeyframe
) {
33 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
34 KeyframedFloatAnimationCurve::Create());
36 FloatKeyframe::Create(0.0, 2.f
, scoped_ptr
<TimingFunction
>()));
38 FloatKeyframe::Create(1.0, 4.f
, scoped_ptr
<TimingFunction
>()));
39 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(-1.f
));
40 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(0.f
));
41 EXPECT_FLOAT_EQ(3.f
, curve
->GetValue(0.5f
));
42 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(1.f
));
43 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(2.f
));
46 // Tests that a float animation with three keyframes works as expected.
47 TEST(KeyframedAnimationCurveTest
, ThreeFloatKeyframe
) {
48 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
49 KeyframedFloatAnimationCurve::Create());
51 FloatKeyframe::Create(0.0, 2.f
, scoped_ptr
<TimingFunction
>()));
53 FloatKeyframe::Create(1.0, 4.f
, scoped_ptr
<TimingFunction
>()));
55 FloatKeyframe::Create(2.0, 8.f
, scoped_ptr
<TimingFunction
>()));
56 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(-1.f
));
57 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(0.f
));
58 EXPECT_FLOAT_EQ(3.f
, curve
->GetValue(0.5f
));
59 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(1.f
));
60 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(1.5f
));
61 EXPECT_FLOAT_EQ(8.f
, curve
->GetValue(2.f
));
62 EXPECT_FLOAT_EQ(8.f
, curve
->GetValue(3.f
));
65 // Tests that a float animation with multiple keys at a given time works sanely.
66 TEST(KeyframedAnimationCurveTest
, RepeatedFloatKeyTimes
) {
67 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
68 KeyframedFloatAnimationCurve::Create());
70 FloatKeyframe::Create(0.0, 4.f
, scoped_ptr
<TimingFunction
>()));
72 FloatKeyframe::Create(1.0, 4.f
, scoped_ptr
<TimingFunction
>()));
74 FloatKeyframe::Create(1.0, 6.f
, scoped_ptr
<TimingFunction
>()));
76 FloatKeyframe::Create(2.0, 6.f
, scoped_ptr
<TimingFunction
>()));
78 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(-1.f
));
79 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(0.f
));
80 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(0.5f
));
82 // There is a discontinuity at 1. Any value between 4 and 6 is valid.
83 float value
= curve
->GetValue(1.f
);
84 EXPECT_TRUE(value
>= 4 && value
<= 6);
86 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(1.5f
));
87 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(2.f
));
88 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(3.f
));
91 // Tests that a transform animation with one keyframe works as expected.
92 TEST(KeyframedAnimationCurveTest
, OneTransformKeyframe
) {
93 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
94 KeyframedTransformAnimationCurve::Create());
95 TransformOperations operations
;
96 operations
.AppendTranslate(2.f
, 0.f
, 0.f
);
98 TransformKeyframe::Create(0.f
, operations
, scoped_ptr
<TimingFunction
>()));
100 ExpectTranslateX(2.f
, curve
->GetValue(-1.f
));
101 ExpectTranslateX(2.f
, curve
->GetValue(0.f
));
102 ExpectTranslateX(2.f
, curve
->GetValue(0.5f
));
103 ExpectTranslateX(2.f
, curve
->GetValue(1.f
));
104 ExpectTranslateX(2.f
, curve
->GetValue(2.f
));
107 // Tests that a transform animation with two keyframes works as expected.
108 TEST(KeyframedAnimationCurveTest
, TwoTransformKeyframe
) {
109 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
110 KeyframedTransformAnimationCurve::Create());
111 TransformOperations operations1
;
112 operations1
.AppendTranslate(2.f
, 0.f
, 0.f
);
113 TransformOperations operations2
;
114 operations2
.AppendTranslate(4.f
, 0.f
, 0.f
);
116 curve
->AddKeyframe(TransformKeyframe::Create(
117 0.f
, operations1
, scoped_ptr
<TimingFunction
>()));
118 curve
->AddKeyframe(TransformKeyframe::Create(
119 1.f
, operations2
, scoped_ptr
<TimingFunction
>()));
120 ExpectTranslateX(2.f
, curve
->GetValue(-1.f
));
121 ExpectTranslateX(2.f
, curve
->GetValue(0.f
));
122 ExpectTranslateX(3.f
, curve
->GetValue(0.5f
));
123 ExpectTranslateX(4.f
, curve
->GetValue(1.f
));
124 ExpectTranslateX(4.f
, curve
->GetValue(2.f
));
127 // Tests that a transform animation with three keyframes works as expected.
128 TEST(KeyframedAnimationCurveTest
, ThreeTransformKeyframe
) {
129 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
130 KeyframedTransformAnimationCurve::Create());
131 TransformOperations operations1
;
132 operations1
.AppendTranslate(2.f
, 0.f
, 0.f
);
133 TransformOperations operations2
;
134 operations2
.AppendTranslate(4.f
, 0.f
, 0.f
);
135 TransformOperations operations3
;
136 operations3
.AppendTranslate(8.f
, 0.f
, 0.f
);
137 curve
->AddKeyframe(TransformKeyframe::Create(
138 0.f
, operations1
, scoped_ptr
<TimingFunction
>()));
139 curve
->AddKeyframe(TransformKeyframe::Create(
140 1.f
, operations2
, scoped_ptr
<TimingFunction
>()));
141 curve
->AddKeyframe(TransformKeyframe::Create(
142 2.f
, operations3
, scoped_ptr
<TimingFunction
>()));
143 ExpectTranslateX(2.f
, curve
->GetValue(-1.f
));
144 ExpectTranslateX(2.f
, curve
->GetValue(0.f
));
145 ExpectTranslateX(3.f
, curve
->GetValue(0.5f
));
146 ExpectTranslateX(4.f
, curve
->GetValue(1.f
));
147 ExpectTranslateX(6.f
, curve
->GetValue(1.5f
));
148 ExpectTranslateX(8.f
, curve
->GetValue(2.f
));
149 ExpectTranslateX(8.f
, curve
->GetValue(3.f
));
152 // Tests that a transform animation with multiple keys at a given time works
154 TEST(KeyframedAnimationCurveTest
, RepeatedTransformKeyTimes
) {
155 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
156 KeyframedTransformAnimationCurve::Create());
158 TransformOperations operations1
;
159 operations1
.AppendTranslate(4.f
, 0.f
, 0.f
);
160 TransformOperations operations2
;
161 operations2
.AppendTranslate(4.f
, 0.f
, 0.f
);
162 TransformOperations operations3
;
163 operations3
.AppendTranslate(6.f
, 0.f
, 0.f
);
164 TransformOperations operations4
;
165 operations4
.AppendTranslate(6.f
, 0.f
, 0.f
);
166 curve
->AddKeyframe(TransformKeyframe::Create(
167 0.f
, operations1
, scoped_ptr
<TimingFunction
>()));
168 curve
->AddKeyframe(TransformKeyframe::Create(
169 1.f
, operations2
, scoped_ptr
<TimingFunction
>()));
170 curve
->AddKeyframe(TransformKeyframe::Create(
171 1.f
, operations3
, scoped_ptr
<TimingFunction
>()));
172 curve
->AddKeyframe(TransformKeyframe::Create(
173 2.f
, operations4
, scoped_ptr
<TimingFunction
>()));
175 ExpectTranslateX(4.f
, curve
->GetValue(-1.f
));
176 ExpectTranslateX(4.f
, curve
->GetValue(0.f
));
177 ExpectTranslateX(4.f
, curve
->GetValue(0.5f
));
179 // There is a discontinuity at 1. Any value between 4 and 6 is valid.
180 gfx::Transform value
= curve
->GetValue(1.f
);
181 EXPECT_GE(value
.matrix().getDouble(0.f
, 3.f
), 4);
182 EXPECT_LE(value
.matrix().getDouble(0.f
, 3.f
), 6);
184 ExpectTranslateX(6.f
, curve
->GetValue(1.5f
));
185 ExpectTranslateX(6.f
, curve
->GetValue(2.f
));
186 ExpectTranslateX(6.f
, curve
->GetValue(3.f
));
189 // Tests that the keyframes may be added out of order.
190 TEST(KeyframedAnimationCurveTest
, UnsortedKeyframes
) {
191 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
192 KeyframedFloatAnimationCurve::Create());
194 FloatKeyframe::Create(2.0, 8.f
, scoped_ptr
<TimingFunction
>()));
196 FloatKeyframe::Create(0.0, 2.f
, scoped_ptr
<TimingFunction
>()));
198 FloatKeyframe::Create(1.0, 4.f
, scoped_ptr
<TimingFunction
>()));
199 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(-1.f
));
200 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(0.f
));
201 EXPECT_FLOAT_EQ(3.f
, curve
->GetValue(0.5f
));
202 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(1.f
));
203 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(1.5f
));
204 EXPECT_FLOAT_EQ(8.f
, curve
->GetValue(2.f
));
205 EXPECT_FLOAT_EQ(8.f
, curve
->GetValue(3.f
));
208 // Tests that a cubic bezier timing function works as expected.
209 TEST(KeyframedAnimationCurveTest
, CubicBezierTimingFunction
) {
210 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
211 KeyframedFloatAnimationCurve::Create());
212 curve
->AddKeyframe(FloatKeyframe::Create(
215 CubicBezierTimingFunction::Create(0.25f
, 0.f
, 0.75f
, 1.f
)
216 .PassAs
<TimingFunction
>()));
218 FloatKeyframe::Create(1.0, 1.f
, scoped_ptr
<TimingFunction
>()));
220 EXPECT_FLOAT_EQ(0.f
, curve
->GetValue(0.f
));
221 EXPECT_LT(0.f
, curve
->GetValue(0.25f
));
222 EXPECT_GT(0.25f
, curve
->GetValue(0.25f
));
223 EXPECT_NEAR(curve
->GetValue(0.5f
), 0.5f
, 0.00015f
);
224 EXPECT_LT(0.75f
, curve
->GetValue(0.75f
));
225 EXPECT_GT(1.f
, curve
->GetValue(0.75f
));
226 EXPECT_FLOAT_EQ(1.f
, curve
->GetValue(1.f
));