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"
10 #include "ui/gfx/animation/tween.h"
11 #include "ui/gfx/geometry/box_f.h"
12 #include "ui/gfx/test/gfx_util.h"
17 void ExpectTranslateX(SkMScalar translate_x
, const gfx::Transform
& transform
) {
18 EXPECT_FLOAT_EQ(translate_x
, transform
.matrix().get(0, 3));
21 void ExpectBrightness(double brightness
, const FilterOperations
& filter
) {
22 EXPECT_EQ(1u, filter
.size());
23 EXPECT_EQ(FilterOperation::BRIGHTNESS
, filter
.at(0).type());
24 EXPECT_FLOAT_EQ(brightness
, filter
.at(0).amount());
27 // Tests that a color animation with one keyframe works as expected.
28 TEST(KeyframedAnimationCurveTest
, OneColorKeyFrame
) {
29 SkColor color
= SkColorSetARGB(255, 255, 255, 255);
30 scoped_ptr
<KeyframedColorAnimationCurve
> curve(
31 KeyframedColorAnimationCurve::Create());
32 curve
->AddKeyframe(ColorKeyframe::Create(base::TimeDelta(), color
, nullptr));
34 EXPECT_SKCOLOR_EQ(color
,
35 curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
36 EXPECT_SKCOLOR_EQ(color
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
37 EXPECT_SKCOLOR_EQ(color
,
38 curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
39 EXPECT_SKCOLOR_EQ(color
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
40 EXPECT_SKCOLOR_EQ(color
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
43 // Tests that a color animation with two keyframes works as expected.
44 TEST(KeyframedAnimationCurveTest
, TwoColorKeyFrame
) {
45 SkColor color_a
= SkColorSetARGB(255, 255, 0, 0);
46 SkColor color_b
= SkColorSetARGB(255, 0, 255, 0);
47 SkColor color_midpoint
= gfx::Tween::ColorValueBetween(0.5, color_a
, color_b
);
48 scoped_ptr
<KeyframedColorAnimationCurve
> curve(
49 KeyframedColorAnimationCurve::Create());
51 ColorKeyframe::Create(base::TimeDelta(), color_a
, nullptr));
52 curve
->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
55 EXPECT_SKCOLOR_EQ(color_a
,
56 curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
57 EXPECT_SKCOLOR_EQ(color_a
,
58 curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
59 EXPECT_SKCOLOR_EQ(color_midpoint
,
60 curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
61 EXPECT_SKCOLOR_EQ(color_b
,
62 curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
63 EXPECT_SKCOLOR_EQ(color_b
,
64 curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
67 // Tests that a color animation with three keyframes works as expected.
68 TEST(KeyframedAnimationCurveTest
, ThreeColorKeyFrame
) {
69 SkColor color_a
= SkColorSetARGB(255, 255, 0, 0);
70 SkColor color_b
= SkColorSetARGB(255, 0, 255, 0);
71 SkColor color_c
= SkColorSetARGB(255, 0, 0, 255);
72 SkColor color_midpoint1
=
73 gfx::Tween::ColorValueBetween(0.5, color_a
, color_b
);
74 SkColor color_midpoint2
=
75 gfx::Tween::ColorValueBetween(0.5, color_b
, color_c
);
76 scoped_ptr
<KeyframedColorAnimationCurve
> curve(
77 KeyframedColorAnimationCurve::Create());
79 ColorKeyframe::Create(base::TimeDelta(), color_a
, nullptr));
80 curve
->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
82 curve
->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(2.0),
85 EXPECT_SKCOLOR_EQ(color_a
,
86 curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
87 EXPECT_SKCOLOR_EQ(color_a
,
88 curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
89 EXPECT_SKCOLOR_EQ(color_midpoint1
,
90 curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
91 EXPECT_SKCOLOR_EQ(color_b
,
92 curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
93 EXPECT_SKCOLOR_EQ(color_midpoint2
,
94 curve
->GetValue(base::TimeDelta::FromSecondsD(1.5f
)));
95 EXPECT_SKCOLOR_EQ(color_c
,
96 curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
97 EXPECT_SKCOLOR_EQ(color_c
,
98 curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)));
101 // Tests that a colro animation with multiple keys at a given time works sanely.
102 TEST(KeyframedAnimationCurveTest
, RepeatedColorKeyFrame
) {
103 SkColor color_a
= SkColorSetARGB(255, 64, 0, 0);
104 SkColor color_b
= SkColorSetARGB(255, 192, 0, 0);
106 scoped_ptr
<KeyframedColorAnimationCurve
> curve(
107 KeyframedColorAnimationCurve::Create());
109 ColorKeyframe::Create(base::TimeDelta(), color_a
, nullptr));
110 curve
->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
112 curve
->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
114 curve
->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(2.0),
117 EXPECT_SKCOLOR_EQ(color_a
,
118 curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
119 EXPECT_SKCOLOR_EQ(color_a
,
120 curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
121 EXPECT_SKCOLOR_EQ(color_a
,
122 curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
124 SkColor value
= curve
->GetValue(base::TimeDelta::FromSecondsD(1.0f
));
125 EXPECT_EQ(255u, SkColorGetA(value
));
126 int red_value
= SkColorGetR(value
);
127 EXPECT_LE(64, red_value
);
128 EXPECT_GE(192, red_value
);
130 EXPECT_SKCOLOR_EQ(color_b
,
131 curve
->GetValue(base::TimeDelta::FromSecondsD(1.5f
)));
132 EXPECT_SKCOLOR_EQ(color_b
,
133 curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
134 EXPECT_SKCOLOR_EQ(color_b
,
135 curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)));
138 // Tests that a float animation with one keyframe works as expected.
139 TEST(KeyframedAnimationCurveTest
, OneFloatKeyframe
) {
140 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
141 KeyframedFloatAnimationCurve::Create());
142 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 2.f
, nullptr));
143 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
144 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
145 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
146 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
147 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
150 // Tests that a float animation with two keyframes works as expected.
151 TEST(KeyframedAnimationCurveTest
, TwoFloatKeyframe
) {
152 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
153 KeyframedFloatAnimationCurve::Create());
154 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 2.f
, nullptr));
156 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 4.f
, nullptr));
157 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
158 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
159 EXPECT_FLOAT_EQ(3.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
160 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
161 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
164 // Tests that a float animation with three keyframes works as expected.
165 TEST(KeyframedAnimationCurveTest
, ThreeFloatKeyframe
) {
166 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
167 KeyframedFloatAnimationCurve::Create());
168 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 2.f
, nullptr));
170 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 4.f
, nullptr));
172 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(2.0), 8.f
, nullptr));
173 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
174 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
175 EXPECT_FLOAT_EQ(3.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
176 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
177 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.5f
)));
178 EXPECT_FLOAT_EQ(8.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
179 EXPECT_FLOAT_EQ(8.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)));
182 // Tests that a float animation with multiple keys at a given time works sanely.
183 TEST(KeyframedAnimationCurveTest
, RepeatedFloatKeyTimes
) {
184 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
185 KeyframedFloatAnimationCurve::Create());
186 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 4.f
, nullptr));
188 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 4.f
, nullptr));
190 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 6.f
, nullptr));
192 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(2.0), 6.f
, nullptr));
194 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
195 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
196 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
198 // There is a discontinuity at 1. Any value between 4 and 6 is valid.
199 float value
= curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
));
200 EXPECT_TRUE(value
>= 4 && value
<= 6);
202 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.5f
)));
203 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
204 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)));
207 // Tests that a transform animation with one keyframe works as expected.
208 TEST(KeyframedAnimationCurveTest
, OneTransformKeyframe
) {
209 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
210 KeyframedTransformAnimationCurve::Create());
211 TransformOperations operations
;
212 operations
.AppendTranslate(2.f
, 0.f
, 0.f
);
214 TransformKeyframe::Create(base::TimeDelta(), operations
, nullptr));
216 ExpectTranslateX(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
217 ExpectTranslateX(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
218 ExpectTranslateX(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
219 ExpectTranslateX(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
220 ExpectTranslateX(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
223 // Tests that a transform animation with two keyframes works as expected.
224 TEST(KeyframedAnimationCurveTest
, TwoTransformKeyframe
) {
225 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
226 KeyframedTransformAnimationCurve::Create());
227 TransformOperations operations1
;
228 operations1
.AppendTranslate(2.f
, 0.f
, 0.f
);
229 TransformOperations operations2
;
230 operations2
.AppendTranslate(4.f
, 0.f
, 0.f
);
233 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
234 curve
->AddKeyframe(TransformKeyframe::Create(
235 base::TimeDelta::FromSecondsD(1.0), operations2
, nullptr));
236 ExpectTranslateX(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
237 ExpectTranslateX(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
238 ExpectTranslateX(3.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
239 ExpectTranslateX(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
240 ExpectTranslateX(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
243 // Tests that a transform animation with three keyframes works as expected.
244 TEST(KeyframedAnimationCurveTest
, ThreeTransformKeyframe
) {
245 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
246 KeyframedTransformAnimationCurve::Create());
247 TransformOperations operations1
;
248 operations1
.AppendTranslate(2.f
, 0.f
, 0.f
);
249 TransformOperations operations2
;
250 operations2
.AppendTranslate(4.f
, 0.f
, 0.f
);
251 TransformOperations operations3
;
252 operations3
.AppendTranslate(8.f
, 0.f
, 0.f
);
254 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
255 curve
->AddKeyframe(TransformKeyframe::Create(
256 base::TimeDelta::FromSecondsD(1.0), operations2
, nullptr));
257 curve
->AddKeyframe(TransformKeyframe::Create(
258 base::TimeDelta::FromSecondsD(2.0), operations3
, nullptr));
259 ExpectTranslateX(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
260 ExpectTranslateX(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
261 ExpectTranslateX(3.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
262 ExpectTranslateX(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
263 ExpectTranslateX(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.5f
)));
264 ExpectTranslateX(8.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
265 ExpectTranslateX(8.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)));
268 // Tests that a transform animation with multiple keys at a given time works
270 TEST(KeyframedAnimationCurveTest
, RepeatedTransformKeyTimes
) {
271 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
272 KeyframedTransformAnimationCurve::Create());
274 TransformOperations operations1
;
275 operations1
.AppendTranslate(4.f
, 0.f
, 0.f
);
276 TransformOperations operations2
;
277 operations2
.AppendTranslate(4.f
, 0.f
, 0.f
);
278 TransformOperations operations3
;
279 operations3
.AppendTranslate(6.f
, 0.f
, 0.f
);
280 TransformOperations operations4
;
281 operations4
.AppendTranslate(6.f
, 0.f
, 0.f
);
283 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
284 curve
->AddKeyframe(TransformKeyframe::Create(
285 base::TimeDelta::FromSecondsD(1.0), operations2
, nullptr));
286 curve
->AddKeyframe(TransformKeyframe::Create(
287 base::TimeDelta::FromSecondsD(1.0), operations3
, nullptr));
288 curve
->AddKeyframe(TransformKeyframe::Create(
289 base::TimeDelta::FromSecondsD(2.0), operations4
, nullptr));
291 ExpectTranslateX(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
292 ExpectTranslateX(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
293 ExpectTranslateX(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
295 // There is a discontinuity at 1. Any value between 4 and 6 is valid.
296 gfx::Transform value
= curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
));
297 EXPECT_GE(value
.matrix().get(0, 3), 4.f
);
298 EXPECT_LE(value
.matrix().get(0, 3), 6.f
);
300 ExpectTranslateX(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.5f
)));
301 ExpectTranslateX(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
302 ExpectTranslateX(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)));
305 // Tests that a filter animation with one keyframe works as expected.
306 TEST(KeyframedAnimationCurveTest
, OneFilterKeyframe
) {
307 scoped_ptr
<KeyframedFilterAnimationCurve
> curve(
308 KeyframedFilterAnimationCurve::Create());
309 FilterOperations operations
;
310 operations
.Append(FilterOperation::CreateBrightnessFilter(2.f
));
312 FilterKeyframe::Create(base::TimeDelta(), operations
, nullptr));
314 ExpectBrightness(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
315 ExpectBrightness(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
316 ExpectBrightness(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
317 ExpectBrightness(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
318 ExpectBrightness(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
321 // Tests that a filter animation with two keyframes works as expected.
322 TEST(KeyframedAnimationCurveTest
, TwoFilterKeyframe
) {
323 scoped_ptr
<KeyframedFilterAnimationCurve
> curve(
324 KeyframedFilterAnimationCurve::Create());
325 FilterOperations operations1
;
326 operations1
.Append(FilterOperation::CreateBrightnessFilter(2.f
));
327 FilterOperations operations2
;
328 operations2
.Append(FilterOperation::CreateBrightnessFilter(4.f
));
331 FilterKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
332 curve
->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
),
333 operations2
, nullptr));
334 ExpectBrightness(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
335 ExpectBrightness(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
336 ExpectBrightness(3.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
337 ExpectBrightness(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
338 ExpectBrightness(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
341 // Tests that a filter animation with three keyframes works as expected.
342 TEST(KeyframedAnimationCurveTest
, ThreeFilterKeyframe
) {
343 scoped_ptr
<KeyframedFilterAnimationCurve
> curve(
344 KeyframedFilterAnimationCurve::Create());
345 FilterOperations operations1
;
346 operations1
.Append(FilterOperation::CreateBrightnessFilter(2.f
));
347 FilterOperations operations2
;
348 operations2
.Append(FilterOperation::CreateBrightnessFilter(4.f
));
349 FilterOperations operations3
;
350 operations3
.Append(FilterOperation::CreateBrightnessFilter(8.f
));
352 FilterKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
353 curve
->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
),
354 operations2
, nullptr));
355 curve
->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(2.f
),
356 operations3
, nullptr));
357 ExpectBrightness(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
358 ExpectBrightness(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
359 ExpectBrightness(3.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
360 ExpectBrightness(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
361 ExpectBrightness(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.5f
)));
362 ExpectBrightness(8.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
363 ExpectBrightness(8.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)));
366 // Tests that a filter animation with multiple keys at a given time works
368 TEST(KeyframedAnimationCurveTest
, RepeatedFilterKeyTimes
) {
369 scoped_ptr
<KeyframedFilterAnimationCurve
> curve(
370 KeyframedFilterAnimationCurve::Create());
372 FilterOperations operations1
;
373 operations1
.Append(FilterOperation::CreateBrightnessFilter(4.f
));
374 FilterOperations operations2
;
375 operations2
.Append(FilterOperation::CreateBrightnessFilter(4.f
));
376 FilterOperations operations3
;
377 operations3
.Append(FilterOperation::CreateBrightnessFilter(6.f
));
378 FilterOperations operations4
;
379 operations4
.Append(FilterOperation::CreateBrightnessFilter(6.f
));
381 FilterKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
382 curve
->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
),
383 operations2
, nullptr));
384 curve
->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
),
385 operations3
, nullptr));
386 curve
->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(2.f
),
387 operations4
, nullptr));
389 ExpectBrightness(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
390 ExpectBrightness(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
391 ExpectBrightness(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
393 // There is a discontinuity at 1. Any value between 4 and 6 is valid.
394 FilterOperations value
= curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
));
395 EXPECT_EQ(1u, value
.size());
396 EXPECT_EQ(FilterOperation::BRIGHTNESS
, value
.at(0).type());
397 EXPECT_GE(value
.at(0).amount(), 4);
398 EXPECT_LE(value
.at(0).amount(), 6);
400 ExpectBrightness(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.5f
)));
401 ExpectBrightness(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
402 ExpectBrightness(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)));
405 // Tests that the keyframes may be added out of order.
406 TEST(KeyframedAnimationCurveTest
, UnsortedKeyframes
) {
407 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
408 KeyframedFloatAnimationCurve::Create());
410 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(2.f
), 8.f
, nullptr));
411 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 2.f
, nullptr));
413 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
), 4.f
, nullptr));
414 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
415 EXPECT_FLOAT_EQ(2.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
416 EXPECT_FLOAT_EQ(3.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
417 EXPECT_FLOAT_EQ(4.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
418 EXPECT_FLOAT_EQ(6.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.5f
)));
419 EXPECT_FLOAT_EQ(8.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
420 EXPECT_FLOAT_EQ(8.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)));
423 // Tests that a cubic bezier timing function works as expected.
424 TEST(KeyframedAnimationCurveTest
, CubicBezierTimingFunction
) {
425 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
426 KeyframedFloatAnimationCurve::Create());
427 curve
->AddKeyframe(FloatKeyframe::Create(
428 base::TimeDelta(), 0.f
,
429 CubicBezierTimingFunction::Create(0.25f
, 0.f
, 0.75f
, 1.f
)));
431 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 1.f
, nullptr));
433 EXPECT_FLOAT_EQ(0.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
434 EXPECT_LT(0.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.25f
)));
435 EXPECT_GT(0.25f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.25f
)));
436 EXPECT_NEAR(curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)), 0.5f
,
438 EXPECT_LT(0.75f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.75f
)));
439 EXPECT_GT(1.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.75f
)));
440 EXPECT_FLOAT_EQ(1.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
443 // Tests that animated bounds are computed as expected.
444 TEST(KeyframedAnimationCurveTest
, AnimatedBounds
) {
445 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
446 KeyframedTransformAnimationCurve::Create());
448 TransformOperations operations1
;
450 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
451 operations1
.AppendTranslate(2.0, 3.0, -1.0);
452 curve
->AddKeyframe(TransformKeyframe::Create(
453 base::TimeDelta::FromSecondsD(0.5f
), operations1
, nullptr));
454 TransformOperations operations2
;
455 operations2
.AppendTranslate(4.0, 1.0, 2.0);
457 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
), operations2
,
458 EaseTimingFunction::Create()));
460 gfx::BoxF
box(2.f
, 3.f
, 4.f
, 1.f
, 3.f
, 2.f
);
463 EXPECT_TRUE(curve
->AnimatedBoundsForBox(box
, &bounds
));
464 EXPECT_EQ(gfx::BoxF(2.f
, 3.f
, 3.f
, 5.f
, 6.f
, 5.f
).ToString(),
468 // Tests that animations that affect scale are correctly identified.
469 TEST(KeyframedAnimationCurveTest
, AffectsScale
) {
470 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
471 KeyframedTransformAnimationCurve::Create());
473 TransformOperations operations1
;
475 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
476 operations1
.AppendTranslate(2.0, 3.0, -1.0);
477 TransformOperations operations2
;
478 operations2
.AppendTranslate(4.0, 1.0, 2.0);
479 curve
->AddKeyframe(TransformKeyframe::Create(
480 base::TimeDelta::FromSecondsD(1.f
), operations2
, nullptr));
482 EXPECT_FALSE(curve
->AffectsScale());
484 TransformOperations operations3
;
485 operations3
.AppendScale(2.f
, 2.f
, 2.f
);
486 curve
->AddKeyframe(TransformKeyframe::Create(
487 base::TimeDelta::FromSecondsD(2.f
), operations3
, nullptr));
489 EXPECT_TRUE(curve
->AffectsScale());
491 TransformOperations operations4
;
492 operations3
.AppendTranslate(2.f
, 2.f
, 2.f
);
493 curve
->AddKeyframe(TransformKeyframe::Create(
494 base::TimeDelta::FromSecondsD(3.f
), operations4
, nullptr));
496 EXPECT_TRUE(curve
->AffectsScale());
499 // Tests that animations that are translations are correctly identified.
500 TEST(KeyframedAnimationCurveTest
, IsTranslation
) {
501 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
502 KeyframedTransformAnimationCurve::Create());
504 TransformOperations operations1
;
506 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
507 operations1
.AppendTranslate(2.0, 3.0, -1.0);
508 TransformOperations operations2
;
509 operations2
.AppendTranslate(4.0, 1.0, 2.0);
510 curve
->AddKeyframe(TransformKeyframe::Create(
511 base::TimeDelta::FromSecondsD(1.f
), operations2
, nullptr));
513 EXPECT_TRUE(curve
->IsTranslation());
515 TransformOperations operations3
;
516 operations3
.AppendScale(2.f
, 2.f
, 2.f
);
517 curve
->AddKeyframe(TransformKeyframe::Create(
518 base::TimeDelta::FromSecondsD(2.f
), operations3
, nullptr));
520 EXPECT_FALSE(curve
->IsTranslation());
522 TransformOperations operations4
;
523 operations3
.AppendTranslate(2.f
, 2.f
, 2.f
);
524 curve
->AddKeyframe(TransformKeyframe::Create(
525 base::TimeDelta::FromSecondsD(3.f
), operations4
, nullptr));
527 EXPECT_FALSE(curve
->IsTranslation());
530 // Tests that maximum target scale is computed as expected.
531 TEST(KeyframedAnimationCurveTest
, MaximumTargetScale
) {
532 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
533 KeyframedTransformAnimationCurve::Create());
535 TransformOperations operations1
;
537 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
538 operations1
.AppendScale(2.f
, -3.f
, 1.f
);
540 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
), operations1
,
541 EaseTimingFunction::Create()));
543 float maximum_scale
= 0.f
;
544 EXPECT_TRUE(curve
->MaximumTargetScale(true, &maximum_scale
));
545 EXPECT_EQ(3.f
, maximum_scale
);
547 TransformOperations operations2
;
548 operations2
.AppendScale(6.f
, 3.f
, 2.f
);
550 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(2.f
), operations2
,
551 EaseTimingFunction::Create()));
553 EXPECT_TRUE(curve
->MaximumTargetScale(true, &maximum_scale
));
554 EXPECT_EQ(6.f
, maximum_scale
);
556 TransformOperations operations3
;
557 operations3
.AppendRotate(1.f
, 0.f
, 0.f
, 90.f
);
559 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(3.f
), operations3
,
560 EaseTimingFunction::Create()));
562 EXPECT_FALSE(curve
->MaximumTargetScale(true, &maximum_scale
));
564 // The original scale is not used in computing the max.
565 scoped_ptr
<KeyframedTransformAnimationCurve
> curve2(
566 KeyframedTransformAnimationCurve::Create());
568 TransformOperations operations4
;
569 operations4
.AppendScale(0.4f
, 0.2f
, 0.6f
);
570 curve2
->AddKeyframe(TransformKeyframe::Create(base::TimeDelta(), operations4
,
571 EaseTimingFunction::Create()));
572 TransformOperations operations5
;
573 operations5
.AppendScale(0.5f
, 0.3f
, -0.8f
);
575 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
), operations5
,
576 EaseTimingFunction::Create()));
578 EXPECT_TRUE(curve2
->MaximumTargetScale(true, &maximum_scale
));
579 EXPECT_EQ(0.8f
, maximum_scale
);
581 EXPECT_TRUE(curve2
->MaximumTargetScale(false, &maximum_scale
));
582 EXPECT_EQ(0.6f
, maximum_scale
);
585 // Tests that an animation with a curve timing function works as expected.
586 TEST(KeyframedAnimationCurveTest
, CurveTiming
) {
587 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
588 KeyframedFloatAnimationCurve::Create());
589 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.f
, nullptr));
591 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
), 1.f
, nullptr));
592 curve
->SetTimingFunction(
593 CubicBezierTimingFunction::Create(0.75f
, 0.f
, 0.25f
, 1.f
).Pass());
594 EXPECT_FLOAT_EQ(0.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
595 EXPECT_FLOAT_EQ(0.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
596 EXPECT_NEAR(0.05f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.25f
)),
598 EXPECT_FLOAT_EQ(0.5f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
599 EXPECT_NEAR(0.95f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.75f
)),
601 EXPECT_FLOAT_EQ(1.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
602 EXPECT_FLOAT_EQ(1.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
605 // Tests that an animation with a curve and keyframe timing function works as
607 TEST(KeyframedAnimationCurveTest
, CurveAndKeyframeTiming
) {
608 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
609 KeyframedFloatAnimationCurve::Create());
610 curve
->AddKeyframe(FloatKeyframe::Create(
611 base::TimeDelta(), 0.f
,
612 CubicBezierTimingFunction::Create(0.35f
, 0.f
, 0.65f
, 1.f
).Pass()));
614 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
), 1.f
, nullptr));
615 // Curve timing function producing outputs outside of range [0,1].
616 curve
->SetTimingFunction(
617 CubicBezierTimingFunction::Create(0.5f
, -0.5f
, 0.5f
, 1.5f
).Pass());
618 EXPECT_FLOAT_EQ(0.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
619 EXPECT_FLOAT_EQ(0.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
620 EXPECT_FLOAT_EQ(0.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(
621 0.25f
))); // Clamped. c(.25) < 0
622 EXPECT_NEAR(0.17f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.42f
)),
623 0.005f
); // c(.42)=.27, k(.27)=.17
624 EXPECT_FLOAT_EQ(0.5f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5f
)));
625 EXPECT_NEAR(0.83f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.58f
)),
626 0.005f
); // c(.58)=.73, k(.73)=.83
627 EXPECT_FLOAT_EQ(1.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(
628 0.75f
))); // Clamped. c(.75) > 1
629 EXPECT_FLOAT_EQ(1.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)));
630 EXPECT_FLOAT_EQ(1.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
633 // Tests that an animation with a curve timing function and multiple keyframes
634 // works as expected.
635 TEST(KeyframedAnimationCurveTest
, CurveTimingMultipleKeyframes
) {
636 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
637 KeyframedFloatAnimationCurve::Create());
638 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.f
, nullptr));
640 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.f
), 1.f
, nullptr));
642 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(2.f
), 3.f
, nullptr));
644 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(3.f
), 6.f
, nullptr));
646 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(4.f
), 9.f
, nullptr));
647 curve
->SetTimingFunction(
648 CubicBezierTimingFunction::Create(0.5f
, 0.f
, 0.5f
, 1.f
).Pass());
649 EXPECT_FLOAT_EQ(0.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(-1.f
)));
650 EXPECT_FLOAT_EQ(0.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(0.f
)));
651 EXPECT_NEAR(0.42f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)),
653 EXPECT_NEAR(1.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(1.455f
)),
655 EXPECT_FLOAT_EQ(3.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(2.f
)));
656 EXPECT_NEAR(8.72f
, curve
->GetValue(base::TimeDelta::FromSecondsD(3.5f
)),
658 EXPECT_FLOAT_EQ(9.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(4.f
)));
659 EXPECT_FLOAT_EQ(9.f
, curve
->GetValue(base::TimeDelta::FromSecondsD(5.f
)));
662 // Tests that an animation with a curve timing function that overshoots works as
664 TEST(KeyframedAnimationCurveTest
, CurveTimingOvershootMultipeKeyframes
) {
665 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
666 KeyframedFloatAnimationCurve::Create());
667 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.f
, nullptr));
669 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 1.f
, nullptr));
671 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(2.0), 3.f
, nullptr));
673 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(3.0), 6.f
, nullptr));
675 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(4.0), 9.f
, nullptr));
676 // Curve timing function producing outputs outside of range [0,1].
677 curve
->SetTimingFunction(
678 CubicBezierTimingFunction::Create(0.5f
, -0.5f
, 0.5f
, 1.5f
).Pass());
679 EXPECT_LE(curve
->GetValue(base::TimeDelta::FromSecondsD(1.f
)),
681 EXPECT_GE(curve
->GetValue(base::TimeDelta::FromSecondsD(3.f
)),