hunspell: Cleanup to fix the header include guards under google/ directory.
[chromium-blink-merge.git] / cc / animation / keyframed_animation_curve_unittest.cc
blobdc4e8173f1c538c1aaadb1ba59110a267a951ab0
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"
14 namespace cc {
15 namespace {
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());
50 curve->AddKeyframe(
51 ColorKeyframe::Create(base::TimeDelta(), color_a, nullptr));
52 curve->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
53 color_b, nullptr));
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());
78 curve->AddKeyframe(
79 ColorKeyframe::Create(base::TimeDelta(), color_a, nullptr));
80 curve->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
81 color_b, nullptr));
82 curve->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(2.0),
83 color_c, nullptr));
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());
108 curve->AddKeyframe(
109 ColorKeyframe::Create(base::TimeDelta(), color_a, nullptr));
110 curve->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
111 color_a, nullptr));
112 curve->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
113 color_b, nullptr));
114 curve->AddKeyframe(ColorKeyframe::Create(base::TimeDelta::FromSecondsD(2.0),
115 color_b, nullptr));
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));
155 curve->AddKeyframe(
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));
169 curve->AddKeyframe(
170 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 4.f, nullptr));
171 curve->AddKeyframe(
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));
187 curve->AddKeyframe(
188 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 4.f, nullptr));
189 curve->AddKeyframe(
190 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 6.f, nullptr));
191 curve->AddKeyframe(
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);
213 curve->AddKeyframe(
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);
232 curve->AddKeyframe(
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);
253 curve->AddKeyframe(
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
269 // sanely.
270 TEST(KeyframedAnimationCurveTest, RepeatedTransformKeyTimes) {
271 scoped_ptr<KeyframedTransformAnimationCurve> curve(
272 KeyframedTransformAnimationCurve::Create());
273 // A step function.
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);
282 curve->AddKeyframe(
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));
311 curve->AddKeyframe(
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));
330 curve->AddKeyframe(
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));
351 curve->AddKeyframe(
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
367 // sanely.
368 TEST(KeyframedAnimationCurveTest, RepeatedFilterKeyTimes) {
369 scoped_ptr<KeyframedFilterAnimationCurve> curve(
370 KeyframedFilterAnimationCurve::Create());
371 // A step function.
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));
380 curve->AddKeyframe(
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());
409 curve->AddKeyframe(
410 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(2.f), 8.f, nullptr));
411 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 2.f, nullptr));
412 curve->AddKeyframe(
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)));
430 curve->AddKeyframe(
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,
437 0.00015f);
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 a step timing function if the change of values occur at the start.
444 TEST(KeyframedAnimationCurveTest, StepsTimingFunctionStepAtStart) {
445 scoped_ptr<KeyframedFloatAnimationCurve> curve(
446 KeyframedFloatAnimationCurve::Create());
447 const int num_steps = 36;
448 const float steps_start_offset = 1.0f;
449 curve->AddKeyframe(FloatKeyframe::Create(
450 base::TimeDelta(), 0.f,
451 StepsTimingFunction::Create(num_steps, steps_start_offset)));
452 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
453 num_steps, nullptr));
455 const float time_threshold = 0.0001f;
457 for (float i = 0.f; i < num_steps; i += 1.f) {
458 const base::TimeDelta time1 =
459 base::TimeDelta::FromSecondsD(i / num_steps - time_threshold);
460 const base::TimeDelta time2 =
461 base::TimeDelta::FromSecondsD(i / num_steps + time_threshold);
462 EXPECT_FLOAT_EQ(std::ceil(i), curve->GetValue(time1));
463 EXPECT_FLOAT_EQ(std::ceil(i) + 1.f, curve->GetValue(time2));
465 EXPECT_FLOAT_EQ(num_steps,
466 curve->GetValue(base::TimeDelta::FromSecondsD(1.0)));
468 for (float i = 0.5f; i <= num_steps; i += 1.0f) {
469 const base::TimeDelta time = base::TimeDelta::FromSecondsD(i / num_steps);
470 EXPECT_FLOAT_EQ(std::ceil(i), curve->GetValue(time));
474 // Tests a step timing function if the change of values occur at the middle.
475 TEST(KeyframedAnimationCurveTest, StepsTimingFunctionStepAtMiddle) {
476 scoped_ptr<KeyframedFloatAnimationCurve> curve(
477 KeyframedFloatAnimationCurve::Create());
478 const int num_steps = 36;
479 const float steps_start_offset = 0.5f;
480 curve->AddKeyframe(FloatKeyframe::Create(
481 base::TimeDelta(), 0.f,
482 StepsTimingFunction::Create(num_steps, steps_start_offset)));
483 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
484 num_steps, nullptr));
486 const float time_threshold = 0.0001f;
488 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta()));
489 for (float i = 0.5f; i < num_steps; i += 1.f) {
490 const base::TimeDelta time1 =
491 base::TimeDelta::FromSecondsD(i / num_steps - time_threshold);
492 const base::TimeDelta time2 =
493 base::TimeDelta::FromSecondsD(i / num_steps + time_threshold);
494 EXPECT_FLOAT_EQ(std::floor(i), curve->GetValue(time1));
495 EXPECT_FLOAT_EQ(std::floor(i) + 1.f, curve->GetValue(time2));
497 EXPECT_FLOAT_EQ(num_steps,
498 curve->GetValue(base::TimeDelta::FromSecondsD(1.0)));
500 for (float i = 0.25f; i <= num_steps; i += 1.0f) {
501 const base::TimeDelta time = base::TimeDelta::FromSecondsD(i / num_steps);
502 EXPECT_FLOAT_EQ(std::floor(i), curve->GetValue(time));
506 // Tests a step timing function if the change of values occur at the end.
507 TEST(KeyframedAnimationCurveTest, StepsTimingFunctionStepAtEnd) {
508 scoped_ptr<KeyframedFloatAnimationCurve> curve(
509 KeyframedFloatAnimationCurve::Create());
510 const int num_steps = 36;
511 const float steps_start_offset = 0.0f;
512 curve->AddKeyframe(FloatKeyframe::Create(
513 base::TimeDelta(), 0.f,
514 StepsTimingFunction::Create(num_steps, steps_start_offset)));
515 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
516 num_steps, nullptr));
518 const float time_threshold = 0.0001f;
520 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta()));
521 for (float i = 1.f; i <= num_steps; i += 1.f) {
522 const base::TimeDelta time1 =
523 base::TimeDelta::FromSecondsD(i / num_steps - time_threshold);
524 const base::TimeDelta time2 =
525 base::TimeDelta::FromSecondsD(i / num_steps + time_threshold);
526 EXPECT_FLOAT_EQ(std::floor(i) - 1.f, curve->GetValue(time1));
527 EXPECT_FLOAT_EQ(std::floor(i), curve->GetValue(time2));
529 EXPECT_FLOAT_EQ(num_steps,
530 curve->GetValue(base::TimeDelta::FromSecondsD(1.0)));
532 for (float i = 0.5f; i <= num_steps; i += 1.0f) {
533 const base::TimeDelta time = base::TimeDelta::FromSecondsD(i / num_steps);
534 EXPECT_FLOAT_EQ(std::floor(i), curve->GetValue(time));
538 // Tests that animated bounds are computed as expected.
539 TEST(KeyframedAnimationCurveTest, AnimatedBounds) {
540 scoped_ptr<KeyframedTransformAnimationCurve> curve(
541 KeyframedTransformAnimationCurve::Create());
543 TransformOperations operations1;
544 curve->AddKeyframe(
545 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
546 operations1.AppendTranslate(2.0, 3.0, -1.0);
547 curve->AddKeyframe(TransformKeyframe::Create(
548 base::TimeDelta::FromSecondsD(0.5f), operations1, nullptr));
549 TransformOperations operations2;
550 operations2.AppendTranslate(4.0, 1.0, 2.0);
551 curve->AddKeyframe(
552 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(1.f), operations2,
553 EaseTimingFunction::Create()));
555 gfx::BoxF box(2.f, 3.f, 4.f, 1.f, 3.f, 2.f);
556 gfx::BoxF bounds;
558 EXPECT_TRUE(curve->AnimatedBoundsForBox(box, &bounds));
559 EXPECT_EQ(gfx::BoxF(2.f, 3.f, 3.f, 5.f, 6.f, 5.f).ToString(),
560 bounds.ToString());
563 // Tests that animations that affect scale are correctly identified.
564 TEST(KeyframedAnimationCurveTest, AffectsScale) {
565 scoped_ptr<KeyframedTransformAnimationCurve> curve(
566 KeyframedTransformAnimationCurve::Create());
568 TransformOperations operations1;
569 curve->AddKeyframe(
570 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
571 operations1.AppendTranslate(2.0, 3.0, -1.0);
572 TransformOperations operations2;
573 operations2.AppendTranslate(4.0, 1.0, 2.0);
574 curve->AddKeyframe(TransformKeyframe::Create(
575 base::TimeDelta::FromSecondsD(1.f), operations2, nullptr));
577 EXPECT_FALSE(curve->AffectsScale());
579 TransformOperations operations3;
580 operations3.AppendScale(2.f, 2.f, 2.f);
581 curve->AddKeyframe(TransformKeyframe::Create(
582 base::TimeDelta::FromSecondsD(2.f), operations3, nullptr));
584 EXPECT_TRUE(curve->AffectsScale());
586 TransformOperations operations4;
587 operations3.AppendTranslate(2.f, 2.f, 2.f);
588 curve->AddKeyframe(TransformKeyframe::Create(
589 base::TimeDelta::FromSecondsD(3.f), operations4, nullptr));
591 EXPECT_TRUE(curve->AffectsScale());
594 // Tests that animations that are translations are correctly identified.
595 TEST(KeyframedAnimationCurveTest, IsTranslation) {
596 scoped_ptr<KeyframedTransformAnimationCurve> curve(
597 KeyframedTransformAnimationCurve::Create());
599 TransformOperations operations1;
600 curve->AddKeyframe(
601 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
602 operations1.AppendTranslate(2.0, 3.0, -1.0);
603 TransformOperations operations2;
604 operations2.AppendTranslate(4.0, 1.0, 2.0);
605 curve->AddKeyframe(TransformKeyframe::Create(
606 base::TimeDelta::FromSecondsD(1.f), operations2, nullptr));
608 EXPECT_TRUE(curve->IsTranslation());
610 TransformOperations operations3;
611 operations3.AppendScale(2.f, 2.f, 2.f);
612 curve->AddKeyframe(TransformKeyframe::Create(
613 base::TimeDelta::FromSecondsD(2.f), operations3, nullptr));
615 EXPECT_FALSE(curve->IsTranslation());
617 TransformOperations operations4;
618 operations3.AppendTranslate(2.f, 2.f, 2.f);
619 curve->AddKeyframe(TransformKeyframe::Create(
620 base::TimeDelta::FromSecondsD(3.f), operations4, nullptr));
622 EXPECT_FALSE(curve->IsTranslation());
625 // Tests that maximum target scale is computed as expected.
626 TEST(KeyframedAnimationCurveTest, MaximumTargetScale) {
627 scoped_ptr<KeyframedTransformAnimationCurve> curve(
628 KeyframedTransformAnimationCurve::Create());
630 TransformOperations operations1;
631 curve->AddKeyframe(
632 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
633 operations1.AppendScale(2.f, -3.f, 1.f);
634 curve->AddKeyframe(
635 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(1.f), operations1,
636 EaseTimingFunction::Create()));
638 float maximum_scale = 0.f;
639 EXPECT_TRUE(curve->MaximumTargetScale(true, &maximum_scale));
640 EXPECT_EQ(3.f, maximum_scale);
642 TransformOperations operations2;
643 operations2.AppendScale(6.f, 3.f, 2.f);
644 curve->AddKeyframe(
645 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(2.f), operations2,
646 EaseTimingFunction::Create()));
648 EXPECT_TRUE(curve->MaximumTargetScale(true, &maximum_scale));
649 EXPECT_EQ(6.f, maximum_scale);
651 TransformOperations operations3;
652 operations3.AppendRotate(1.f, 0.f, 0.f, 90.f);
653 curve->AddKeyframe(
654 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(3.f), operations3,
655 EaseTimingFunction::Create()));
657 EXPECT_FALSE(curve->MaximumTargetScale(true, &maximum_scale));
659 // The original scale is not used in computing the max.
660 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
661 KeyframedTransformAnimationCurve::Create());
663 TransformOperations operations4;
664 operations4.AppendScale(0.4f, 0.2f, 0.6f);
665 curve2->AddKeyframe(TransformKeyframe::Create(base::TimeDelta(), operations4,
666 EaseTimingFunction::Create()));
667 TransformOperations operations5;
668 operations5.AppendScale(0.5f, 0.3f, -0.8f);
669 curve2->AddKeyframe(
670 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(1.f), operations5,
671 EaseTimingFunction::Create()));
673 EXPECT_TRUE(curve2->MaximumTargetScale(true, &maximum_scale));
674 EXPECT_EQ(0.8f, maximum_scale);
676 EXPECT_TRUE(curve2->MaximumTargetScale(false, &maximum_scale));
677 EXPECT_EQ(0.6f, maximum_scale);
680 // Tests that starting animation scale is computed as expected.
681 TEST(KeyframedAnimationCurveTest, AnimationStartScale) {
682 scoped_ptr<KeyframedTransformAnimationCurve> curve(
683 KeyframedTransformAnimationCurve::Create());
685 TransformOperations operations1;
686 curve->AddKeyframe(
687 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
688 operations1.AppendScale(2.f, -3.f, 1.f);
689 curve->AddKeyframe(
690 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(1.f), operations1,
691 EaseTimingFunction::Create()));
693 float start_scale = 0.f;
695 // Forward direction
696 EXPECT_TRUE(curve->AnimationStartScale(true, &start_scale));
697 EXPECT_EQ(1.f, start_scale);
699 // Backward direction
700 EXPECT_TRUE(curve->AnimationStartScale(false, &start_scale));
701 EXPECT_EQ(3.f, start_scale);
703 TransformOperations operations2;
704 operations2.AppendScale(6.f, 3.f, 2.f);
705 curve->AddKeyframe(
706 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(2.f), operations2,
707 EaseTimingFunction::Create()));
709 // Backward direction
710 EXPECT_TRUE(curve->AnimationStartScale(true, &start_scale));
711 EXPECT_EQ(1.f, start_scale);
713 // Backward direction
714 EXPECT_TRUE(curve->AnimationStartScale(false, &start_scale));
715 EXPECT_EQ(6.f, start_scale);
717 TransformOperations operations3;
718 operations3.AppendRotate(1.f, 0.f, 0.f, 90.f);
719 curve->AddKeyframe(
720 TransformKeyframe::Create(base::TimeDelta::FromSecondsD(3.f), operations3,
721 EaseTimingFunction::Create()));
723 EXPECT_FALSE(curve->AnimationStartScale(false, &start_scale));
724 EXPECT_EQ(0.f, start_scale);
727 // Tests that an animation with a curve timing function works as expected.
728 TEST(KeyframedAnimationCurveTest, CurveTiming) {
729 scoped_ptr<KeyframedFloatAnimationCurve> curve(
730 KeyframedFloatAnimationCurve::Create());
731 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.f, nullptr));
732 curve->AddKeyframe(
733 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.f), 1.f, nullptr));
734 curve->SetTimingFunction(
735 CubicBezierTimingFunction::Create(0.75f, 0.f, 0.25f, 1.f).Pass());
736 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta::FromSecondsD(-1.f)));
737 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta::FromSecondsD(0.f)));
738 EXPECT_NEAR(0.05f, curve->GetValue(base::TimeDelta::FromSecondsD(0.25f)),
739 0.005f);
740 EXPECT_FLOAT_EQ(0.5f, curve->GetValue(base::TimeDelta::FromSecondsD(0.5f)));
741 EXPECT_NEAR(0.95f, curve->GetValue(base::TimeDelta::FromSecondsD(0.75f)),
742 0.005f);
743 EXPECT_FLOAT_EQ(1.f, curve->GetValue(base::TimeDelta::FromSecondsD(1.f)));
744 EXPECT_FLOAT_EQ(1.f, curve->GetValue(base::TimeDelta::FromSecondsD(2.f)));
747 // Tests that an animation with a curve and keyframe timing function works as
748 // expected.
749 TEST(KeyframedAnimationCurveTest, CurveAndKeyframeTiming) {
750 scoped_ptr<KeyframedFloatAnimationCurve> curve(
751 KeyframedFloatAnimationCurve::Create());
752 curve->AddKeyframe(FloatKeyframe::Create(
753 base::TimeDelta(), 0.f,
754 CubicBezierTimingFunction::Create(0.35f, 0.f, 0.65f, 1.f).Pass()));
755 curve->AddKeyframe(
756 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.f), 1.f, nullptr));
757 // Curve timing function producing outputs outside of range [0,1].
758 curve->SetTimingFunction(
759 CubicBezierTimingFunction::Create(0.5f, -0.5f, 0.5f, 1.5f).Pass());
760 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta::FromSecondsD(-1.f)));
761 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta::FromSecondsD(0.f)));
762 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta::FromSecondsD(
763 0.25f))); // Clamped. c(.25) < 0
764 EXPECT_NEAR(0.17f, curve->GetValue(base::TimeDelta::FromSecondsD(0.42f)),
765 0.005f); // c(.42)=.27, k(.27)=.17
766 EXPECT_FLOAT_EQ(0.5f, curve->GetValue(base::TimeDelta::FromSecondsD(0.5f)));
767 EXPECT_NEAR(0.83f, curve->GetValue(base::TimeDelta::FromSecondsD(0.58f)),
768 0.005f); // c(.58)=.73, k(.73)=.83
769 EXPECT_FLOAT_EQ(1.f, curve->GetValue(base::TimeDelta::FromSecondsD(
770 0.75f))); // Clamped. c(.75) > 1
771 EXPECT_FLOAT_EQ(1.f, curve->GetValue(base::TimeDelta::FromSecondsD(1.f)));
772 EXPECT_FLOAT_EQ(1.f, curve->GetValue(base::TimeDelta::FromSecondsD(2.f)));
775 // Tests that a linear timing function works as expected for inputs outside of
776 // range [0,1]
777 TEST(KeyframedAnimationCurveTest, LinearTimingInputsOutsideZeroOneRange) {
778 scoped_ptr<KeyframedFloatAnimationCurve> curve(
779 KeyframedFloatAnimationCurve::Create());
780 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.f, nullptr));
781 curve->AddKeyframe(
782 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 2.f, nullptr));
783 // Curve timing function producing timing outputs outside of range [0,1].
784 curve->SetTimingFunction(
785 CubicBezierTimingFunction::Create(0.5f, -0.5f, 0.5f, 1.5f).Pass());
787 EXPECT_NEAR(-0.076f, curve->GetValue(base::TimeDelta::FromSecondsD(0.25f)),
788 0.001f);
789 EXPECT_NEAR(2.076f, curve->GetValue(base::TimeDelta::FromSecondsD(0.75f)),
790 0.001f);
793 // If a curve cubic-bezier timing function produces timing outputs outside
794 // the range [0, 1] then a keyframe cubic-bezier timing function
795 // should consume that input properly (using end-point gradients).
796 TEST(KeyframedAnimationCurveTest, CurveTimingInputsOutsideZeroOneRange) {
797 scoped_ptr<KeyframedFloatAnimationCurve> curve(
798 KeyframedFloatAnimationCurve::Create());
799 // Keyframe timing function with 0.5 gradients at each end.
800 curve->AddKeyframe(FloatKeyframe::Create(
801 base::TimeDelta(), 0.f,
802 CubicBezierTimingFunction::Create(0.5f, 0.25f, 0.5f, 0.75f).Pass()));
803 curve->AddKeyframe(
804 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.f), 1.f, nullptr));
805 // Curve timing function producing timing outputs outside of range [0,1].
806 curve->SetTimingFunction(
807 CubicBezierTimingFunction::Create(0.5f, -0.5f, 0.5f, 1.5f).Pass());
809 EXPECT_NEAR(-0.02f, curve->GetValue(base::TimeDelta::FromSecondsD(0.25f)),
810 0.002f); // c(.25)=-.04, -.04*0.5=-0.02
811 EXPECT_NEAR(0.33f, curve->GetValue(base::TimeDelta::FromSecondsD(0.46f)),
812 0.002f); // c(.46)=.38, k(.38)=.33
814 EXPECT_NEAR(0.67f, curve->GetValue(base::TimeDelta::FromSecondsD(0.54f)),
815 0.002f); // c(.54)=.62, k(.62)=.67
816 EXPECT_NEAR(1.02f, curve->GetValue(base::TimeDelta::FromSecondsD(0.75f)),
817 0.002f); // c(.75)=1.04 1+.04*0.5=1.02
820 // Tests that a step timing function works as expected for inputs outside of
821 // range [0,1]
822 TEST(KeyframedAnimationCurveTest, StepsTimingInputsOutsideZeroOneRange) {
823 scoped_ptr<KeyframedFloatAnimationCurve> curve(
824 KeyframedFloatAnimationCurve::Create());
825 curve->AddKeyframe(FloatKeyframe::Create(
826 base::TimeDelta(), 0.f, StepsTimingFunction::Create(4, 0.5f)));
827 curve->AddKeyframe(
828 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 2.f, nullptr));
829 // Curve timing function producing timing outputs outside of range [0,1].
830 curve->SetTimingFunction(
831 CubicBezierTimingFunction::Create(0.5f, -0.5f, 0.5f, 1.5f).Pass());
833 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta::FromSecondsD(0.25f)));
834 EXPECT_FLOAT_EQ(2.f, curve->GetValue(base::TimeDelta::FromSecondsD(0.75f)));
837 // Tests that an animation with a curve timing function and multiple keyframes
838 // works as expected.
839 TEST(KeyframedAnimationCurveTest, CurveTimingMultipleKeyframes) {
840 scoped_ptr<KeyframedFloatAnimationCurve> curve(
841 KeyframedFloatAnimationCurve::Create());
842 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.f, nullptr));
843 curve->AddKeyframe(
844 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.f), 1.f, nullptr));
845 curve->AddKeyframe(
846 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(2.f), 3.f, nullptr));
847 curve->AddKeyframe(
848 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(3.f), 6.f, nullptr));
849 curve->AddKeyframe(
850 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(4.f), 9.f, nullptr));
851 curve->SetTimingFunction(
852 CubicBezierTimingFunction::Create(0.5f, 0.f, 0.5f, 1.f).Pass());
853 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta::FromSecondsD(-1.f)));
854 EXPECT_FLOAT_EQ(0.f, curve->GetValue(base::TimeDelta::FromSecondsD(0.f)));
855 EXPECT_NEAR(0.42f, curve->GetValue(base::TimeDelta::FromSecondsD(1.f)),
856 0.005f);
857 EXPECT_NEAR(1.f, curve->GetValue(base::TimeDelta::FromSecondsD(1.455f)),
858 0.005f);
859 EXPECT_FLOAT_EQ(3.f, curve->GetValue(base::TimeDelta::FromSecondsD(2.f)));
860 EXPECT_NEAR(8.72f, curve->GetValue(base::TimeDelta::FromSecondsD(3.5f)),
861 0.01f);
862 EXPECT_FLOAT_EQ(9.f, curve->GetValue(base::TimeDelta::FromSecondsD(4.f)));
863 EXPECT_FLOAT_EQ(9.f, curve->GetValue(base::TimeDelta::FromSecondsD(5.f)));
866 // Tests that an animation with a curve timing function that overshoots works as
867 // expected.
868 TEST(KeyframedAnimationCurveTest, CurveTimingOvershootMultipeKeyframes) {
869 scoped_ptr<KeyframedFloatAnimationCurve> curve(
870 KeyframedFloatAnimationCurve::Create());
871 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.f, nullptr));
872 curve->AddKeyframe(
873 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 1.f, nullptr));
874 curve->AddKeyframe(
875 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(2.0), 3.f, nullptr));
876 curve->AddKeyframe(
877 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(3.0), 6.f, nullptr));
878 curve->AddKeyframe(
879 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(4.0), 9.f, nullptr));
880 // Curve timing function producing outputs outside of range [0,1].
881 curve->SetTimingFunction(
882 CubicBezierTimingFunction::Create(0.5f, -0.5f, 0.5f, 1.5f).Pass());
883 EXPECT_LE(curve->GetValue(base::TimeDelta::FromSecondsD(1.f)),
884 0.f); // c(.25) < 0
885 EXPECT_GE(curve->GetValue(base::TimeDelta::FromSecondsD(3.f)),
886 9.f); // c(.75) > 1
889 } // namespace
890 } // namespace cc