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.
4
5#include "cc/animation/keyframed_animation_curve.h"
6
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/box_f.h"
12#include "ui/gfx/test/gfx_util.h"
13
14namespace cc {
15namespace {
16
17void ExpectTranslateX(SkMScalar translate_x, const gfx::Transform& transform) {
18  EXPECT_FLOAT_EQ(translate_x, transform.matrix().get(0, 3));
19}
20
21void 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());
25}
26
27// Tests that a color animation with one keyframe works as expected.
28TEST(KeyframedAnimationCurveTest, OneColorKeyFrame) {
29  SkColor color = SkColorSetARGB(255, 255, 255, 255);
30  scoped_ptr<KeyframedColorAnimationCurve> curve(
31      KeyframedColorAnimationCurve::Create());
32  curve->AddKeyframe(
33      ColorKeyframe::Create(0.0, color, scoped_ptr<TimingFunction>()));
34
35  EXPECT_SKCOLOR_EQ(color, curve->GetValue(-1.f));
36  EXPECT_SKCOLOR_EQ(color, curve->GetValue(0.f));
37  EXPECT_SKCOLOR_EQ(color, curve->GetValue(0.5f));
38  EXPECT_SKCOLOR_EQ(color, curve->GetValue(1.f));
39  EXPECT_SKCOLOR_EQ(color, curve->GetValue(2.f));
40}
41
42// Tests that a color animation with two keyframes works as expected.
43TEST(KeyframedAnimationCurveTest, TwoColorKeyFrame) {
44  SkColor color_a = SkColorSetARGB(255, 255, 0, 0);
45  SkColor color_b = SkColorSetARGB(255, 0, 255, 0);
46  SkColor color_midpoint = gfx::Tween::ColorValueBetween(0.5, color_a, color_b);
47  scoped_ptr<KeyframedColorAnimationCurve> curve(
48      KeyframedColorAnimationCurve::Create());
49  curve->AddKeyframe(
50      ColorKeyframe::Create(0.0, color_a, scoped_ptr<TimingFunction>()));
51  curve->AddKeyframe(
52      ColorKeyframe::Create(1.0, color_b, scoped_ptr<TimingFunction>()));
53
54  EXPECT_SKCOLOR_EQ(color_a, curve->GetValue(-1.f));
55  EXPECT_SKCOLOR_EQ(color_a, curve->GetValue(0.f));
56  EXPECT_SKCOLOR_EQ(color_midpoint, curve->GetValue(0.5f));
57  EXPECT_SKCOLOR_EQ(color_b, curve->GetValue(1.f));
58  EXPECT_SKCOLOR_EQ(color_b, curve->GetValue(2.f));
59}
60
61// Tests that a color animation with three keyframes works as expected.
62TEST(KeyframedAnimationCurveTest, ThreeColorKeyFrame) {
63  SkColor color_a = SkColorSetARGB(255, 255, 0, 0);
64  SkColor color_b = SkColorSetARGB(255, 0, 255, 0);
65  SkColor color_c = SkColorSetARGB(255, 0, 0, 255);
66  SkColor color_midpoint1 =
67      gfx::Tween::ColorValueBetween(0.5, color_a, color_b);
68  SkColor color_midpoint2 =
69      gfx::Tween::ColorValueBetween(0.5, color_b, color_c);
70  scoped_ptr<KeyframedColorAnimationCurve> curve(
71      KeyframedColorAnimationCurve::Create());
72  curve->AddKeyframe(
73      ColorKeyframe::Create(0.0, color_a, scoped_ptr<TimingFunction>()));
74  curve->AddKeyframe(
75      ColorKeyframe::Create(1.0, color_b, scoped_ptr<TimingFunction>()));
76  curve->AddKeyframe(
77      ColorKeyframe::Create(2.0, color_c, scoped_ptr<TimingFunction>()));
78
79  EXPECT_SKCOLOR_EQ(color_a, curve->GetValue(-1.f));
80  EXPECT_SKCOLOR_EQ(color_a, curve->GetValue(0.f));
81  EXPECT_SKCOLOR_EQ(color_midpoint1, curve->GetValue(0.5f));
82  EXPECT_SKCOLOR_EQ(color_b, curve->GetValue(1.f));
83  EXPECT_SKCOLOR_EQ(color_midpoint2, curve->GetValue(1.5f));
84  EXPECT_SKCOLOR_EQ(color_c, curve->GetValue(2.f));
85  EXPECT_SKCOLOR_EQ(color_c, curve->GetValue(3.f));
86}
87
88// Tests that a colro animation with multiple keys at a given time works sanely.
89TEST(KeyframedAnimationCurveTest, RepeatedColorKeyFrame) {
90  SkColor color_a = SkColorSetARGB(255, 64, 0, 0);
91  SkColor color_b = SkColorSetARGB(255, 192, 0, 0);
92
93  scoped_ptr<KeyframedColorAnimationCurve> curve(
94      KeyframedColorAnimationCurve::Create());
95  curve->AddKeyframe(
96      ColorKeyframe::Create(0.0, color_a, scoped_ptr<TimingFunction>()));
97  curve->AddKeyframe(
98      ColorKeyframe::Create(1.0, color_a, scoped_ptr<TimingFunction>()));
99  curve->AddKeyframe(
100      ColorKeyframe::Create(1.0, color_b, scoped_ptr<TimingFunction>()));
101  curve->AddKeyframe(
102      ColorKeyframe::Create(2.0, color_b, scoped_ptr<TimingFunction>()));
103
104  EXPECT_SKCOLOR_EQ(color_a, curve->GetValue(-1.f));
105  EXPECT_SKCOLOR_EQ(color_a, curve->GetValue(0.f));
106  EXPECT_SKCOLOR_EQ(color_a, curve->GetValue(0.5f));
107
108  SkColor value = curve->GetValue(1.0f);
109  EXPECT_EQ(255u, SkColorGetA(value));
110  int red_value = SkColorGetR(value);
111  EXPECT_LE(64, red_value);
112  EXPECT_GE(192, red_value);
113
114  EXPECT_SKCOLOR_EQ(color_b, curve->GetValue(1.5f));
115  EXPECT_SKCOLOR_EQ(color_b, curve->GetValue(2.f));
116  EXPECT_SKCOLOR_EQ(color_b, curve->GetValue(3.f));
117}
118
119// Tests that a float animation with one keyframe works as expected.
120TEST(KeyframedAnimationCurveTest, OneFloatKeyframe) {
121  scoped_ptr<KeyframedFloatAnimationCurve> curve(
122      KeyframedFloatAnimationCurve::Create());
123  curve->AddKeyframe(
124      FloatKeyframe::Create(0.0, 2.f, scoped_ptr<TimingFunction>()));
125  EXPECT_FLOAT_EQ(2.f, curve->GetValue(-1.f));
126  EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.f));
127  EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.5f));
128  EXPECT_FLOAT_EQ(2.f, curve->GetValue(1.f));
129  EXPECT_FLOAT_EQ(2.f, curve->GetValue(2.f));
130}
131
132// Tests that a float animation with two keyframes works as expected.
133TEST(KeyframedAnimationCurveTest, TwoFloatKeyframe) {
134  scoped_ptr<KeyframedFloatAnimationCurve> curve(
135      KeyframedFloatAnimationCurve::Create());
136  curve->AddKeyframe(
137      FloatKeyframe::Create(0.0, 2.f, scoped_ptr<TimingFunction>()));
138  curve->AddKeyframe(
139      FloatKeyframe::Create(1.0, 4.f, scoped_ptr<TimingFunction>()));
140  EXPECT_FLOAT_EQ(2.f, curve->GetValue(-1.f));
141  EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.f));
142  EXPECT_FLOAT_EQ(3.f, curve->GetValue(0.5f));
143  EXPECT_FLOAT_EQ(4.f, curve->GetValue(1.f));
144  EXPECT_FLOAT_EQ(4.f, curve->GetValue(2.f));
145}
146
147// Tests that a float animation with three keyframes works as expected.
148TEST(KeyframedAnimationCurveTest, ThreeFloatKeyframe) {
149  scoped_ptr<KeyframedFloatAnimationCurve> curve(
150      KeyframedFloatAnimationCurve::Create());
151  curve->AddKeyframe(
152      FloatKeyframe::Create(0.0, 2.f, scoped_ptr<TimingFunction>()));
153  curve->AddKeyframe(
154      FloatKeyframe::Create(1.0, 4.f, scoped_ptr<TimingFunction>()));
155  curve->AddKeyframe(
156      FloatKeyframe::Create(2.0, 8.f, scoped_ptr<TimingFunction>()));
157  EXPECT_FLOAT_EQ(2.f, curve->GetValue(-1.f));
158  EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.f));
159  EXPECT_FLOAT_EQ(3.f, curve->GetValue(0.5f));
160  EXPECT_FLOAT_EQ(4.f, curve->GetValue(1.f));
161  EXPECT_FLOAT_EQ(6.f, curve->GetValue(1.5f));
162  EXPECT_FLOAT_EQ(8.f, curve->GetValue(2.f));
163  EXPECT_FLOAT_EQ(8.f, curve->GetValue(3.f));
164}
165
166// Tests that a float animation with multiple keys at a given time works sanely.
167TEST(KeyframedAnimationCurveTest, RepeatedFloatKeyTimes) {
168  scoped_ptr<KeyframedFloatAnimationCurve> curve(
169      KeyframedFloatAnimationCurve::Create());
170  curve->AddKeyframe(
171      FloatKeyframe::Create(0.0, 4.f, scoped_ptr<TimingFunction>()));
172  curve->AddKeyframe(
173      FloatKeyframe::Create(1.0, 4.f, scoped_ptr<TimingFunction>()));
174  curve->AddKeyframe(
175      FloatKeyframe::Create(1.0, 6.f, scoped_ptr<TimingFunction>()));
176  curve->AddKeyframe(
177      FloatKeyframe::Create(2.0, 6.f, scoped_ptr<TimingFunction>()));
178
179  EXPECT_FLOAT_EQ(4.f, curve->GetValue(-1.f));
180  EXPECT_FLOAT_EQ(4.f, curve->GetValue(0.f));
181  EXPECT_FLOAT_EQ(4.f, curve->GetValue(0.5f));
182
183  // There is a discontinuity at 1. Any value between 4 and 6 is valid.
184  float value = curve->GetValue(1.f);
185  EXPECT_TRUE(value >= 4 && value <= 6);
186
187  EXPECT_FLOAT_EQ(6.f, curve->GetValue(1.5f));
188  EXPECT_FLOAT_EQ(6.f, curve->GetValue(2.f));
189  EXPECT_FLOAT_EQ(6.f, curve->GetValue(3.f));
190}
191
192// Tests that a transform animation with one keyframe works as expected.
193TEST(KeyframedAnimationCurveTest, OneTransformKeyframe) {
194  scoped_ptr<KeyframedTransformAnimationCurve> curve(
195      KeyframedTransformAnimationCurve::Create());
196  TransformOperations operations;
197  operations.AppendTranslate(2.f, 0.f, 0.f);
198  curve->AddKeyframe(
199      TransformKeyframe::Create(0.f, operations, scoped_ptr<TimingFunction>()));
200
201  ExpectTranslateX(2.f, curve->GetValue(-1.f));
202  ExpectTranslateX(2.f, curve->GetValue(0.f));
203  ExpectTranslateX(2.f, curve->GetValue(0.5f));
204  ExpectTranslateX(2.f, curve->GetValue(1.f));
205  ExpectTranslateX(2.f, curve->GetValue(2.f));
206}
207
208// Tests that a transform animation with two keyframes works as expected.
209TEST(KeyframedAnimationCurveTest, TwoTransformKeyframe) {
210  scoped_ptr<KeyframedTransformAnimationCurve> curve(
211      KeyframedTransformAnimationCurve::Create());
212  TransformOperations operations1;
213  operations1.AppendTranslate(2.f, 0.f, 0.f);
214  TransformOperations operations2;
215  operations2.AppendTranslate(4.f, 0.f, 0.f);
216
217  curve->AddKeyframe(TransformKeyframe::Create(
218      0.f, operations1, scoped_ptr<TimingFunction>()));
219  curve->AddKeyframe(TransformKeyframe::Create(
220      1.f, operations2, scoped_ptr<TimingFunction>()));
221  ExpectTranslateX(2.f, curve->GetValue(-1.f));
222  ExpectTranslateX(2.f, curve->GetValue(0.f));
223  ExpectTranslateX(3.f, curve->GetValue(0.5f));
224  ExpectTranslateX(4.f, curve->GetValue(1.f));
225  ExpectTranslateX(4.f, curve->GetValue(2.f));
226}
227
228// Tests that a transform animation with three keyframes works as expected.
229TEST(KeyframedAnimationCurveTest, ThreeTransformKeyframe) {
230  scoped_ptr<KeyframedTransformAnimationCurve> curve(
231      KeyframedTransformAnimationCurve::Create());
232  TransformOperations operations1;
233  operations1.AppendTranslate(2.f, 0.f, 0.f);
234  TransformOperations operations2;
235  operations2.AppendTranslate(4.f, 0.f, 0.f);
236  TransformOperations operations3;
237  operations3.AppendTranslate(8.f, 0.f, 0.f);
238  curve->AddKeyframe(TransformKeyframe::Create(
239      0.f, operations1, scoped_ptr<TimingFunction>()));
240  curve->AddKeyframe(TransformKeyframe::Create(
241      1.f, operations2, scoped_ptr<TimingFunction>()));
242  curve->AddKeyframe(TransformKeyframe::Create(
243      2.f, operations3, scoped_ptr<TimingFunction>()));
244  ExpectTranslateX(2.f, curve->GetValue(-1.f));
245  ExpectTranslateX(2.f, curve->GetValue(0.f));
246  ExpectTranslateX(3.f, curve->GetValue(0.5f));
247  ExpectTranslateX(4.f, curve->GetValue(1.f));
248  ExpectTranslateX(6.f, curve->GetValue(1.5f));
249  ExpectTranslateX(8.f, curve->GetValue(2.f));
250  ExpectTranslateX(8.f, curve->GetValue(3.f));
251}
252
253// Tests that a transform animation with multiple keys at a given time works
254// sanely.
255TEST(KeyframedAnimationCurveTest, RepeatedTransformKeyTimes) {
256  scoped_ptr<KeyframedTransformAnimationCurve> curve(
257      KeyframedTransformAnimationCurve::Create());
258  // A step function.
259  TransformOperations operations1;
260  operations1.AppendTranslate(4.f, 0.f, 0.f);
261  TransformOperations operations2;
262  operations2.AppendTranslate(4.f, 0.f, 0.f);
263  TransformOperations operations3;
264  operations3.AppendTranslate(6.f, 0.f, 0.f);
265  TransformOperations operations4;
266  operations4.AppendTranslate(6.f, 0.f, 0.f);
267  curve->AddKeyframe(TransformKeyframe::Create(
268      0.f, operations1, scoped_ptr<TimingFunction>()));
269  curve->AddKeyframe(TransformKeyframe::Create(
270      1.f, operations2, scoped_ptr<TimingFunction>()));
271  curve->AddKeyframe(TransformKeyframe::Create(
272      1.f, operations3, scoped_ptr<TimingFunction>()));
273  curve->AddKeyframe(TransformKeyframe::Create(
274      2.f, operations4, scoped_ptr<TimingFunction>()));
275
276  ExpectTranslateX(4.f, curve->GetValue(-1.f));
277  ExpectTranslateX(4.f, curve->GetValue(0.f));
278  ExpectTranslateX(4.f, curve->GetValue(0.5f));
279
280  // There is a discontinuity at 1. Any value between 4 and 6 is valid.
281  gfx::Transform value = curve->GetValue(1.f);
282  EXPECT_GE(value.matrix().get(0, 3), 4.f);
283  EXPECT_LE(value.matrix().get(0, 3), 6.f);
284
285  ExpectTranslateX(6.f, curve->GetValue(1.5f));
286  ExpectTranslateX(6.f, curve->GetValue(2.f));
287  ExpectTranslateX(6.f, curve->GetValue(3.f));
288}
289
290// Tests that a filter animation with one keyframe works as expected.
291TEST(KeyframedAnimationCurveTest, OneFilterKeyframe) {
292  scoped_ptr<KeyframedFilterAnimationCurve> curve(
293      KeyframedFilterAnimationCurve::Create());
294  FilterOperations operations;
295  operations.Append(FilterOperation::CreateBrightnessFilter(2.f));
296  curve->AddKeyframe(
297      FilterKeyframe::Create(0.f, operations, scoped_ptr<TimingFunction>()));
298
299  ExpectBrightness(2.f, curve->GetValue(-1.f));
300  ExpectBrightness(2.f, curve->GetValue(0.f));
301  ExpectBrightness(2.f, curve->GetValue(0.5f));
302  ExpectBrightness(2.f, curve->GetValue(1.f));
303  ExpectBrightness(2.f, curve->GetValue(2.f));
304}
305
306// Tests that a filter animation with two keyframes works as expected.
307TEST(KeyframedAnimationCurveTest, TwoFilterKeyframe) {
308  scoped_ptr<KeyframedFilterAnimationCurve> curve(
309      KeyframedFilterAnimationCurve::Create());
310  FilterOperations operations1;
311  operations1.Append(FilterOperation::CreateBrightnessFilter(2.f));
312  FilterOperations operations2;
313  operations2.Append(FilterOperation::CreateBrightnessFilter(4.f));
314
315  curve->AddKeyframe(FilterKeyframe::Create(
316      0.f, operations1, scoped_ptr<TimingFunction>()));
317  curve->AddKeyframe(FilterKeyframe::Create(
318      1.f, operations2, scoped_ptr<TimingFunction>()));
319  ExpectBrightness(2.f, curve->GetValue(-1.f));
320  ExpectBrightness(2.f, curve->GetValue(0.f));
321  ExpectBrightness(3.f, curve->GetValue(0.5f));
322  ExpectBrightness(4.f, curve->GetValue(1.f));
323  ExpectBrightness(4.f, curve->GetValue(2.f));
324}
325
326// Tests that a filter animation with three keyframes works as expected.
327TEST(KeyframedAnimationCurveTest, ThreeFilterKeyframe) {
328  scoped_ptr<KeyframedFilterAnimationCurve> curve(
329      KeyframedFilterAnimationCurve::Create());
330  FilterOperations operations1;
331  operations1.Append(FilterOperation::CreateBrightnessFilter(2.f));
332  FilterOperations operations2;
333  operations2.Append(FilterOperation::CreateBrightnessFilter(4.f));
334  FilterOperations operations3;
335  operations3.Append(FilterOperation::CreateBrightnessFilter(8.f));
336  curve->AddKeyframe(FilterKeyframe::Create(
337      0.f, operations1, scoped_ptr<TimingFunction>()));
338  curve->AddKeyframe(FilterKeyframe::Create(
339      1.f, operations2, scoped_ptr<TimingFunction>()));
340  curve->AddKeyframe(FilterKeyframe::Create(
341      2.f, operations3, scoped_ptr<TimingFunction>()));
342  ExpectBrightness(2.f, curve->GetValue(-1.f));
343  ExpectBrightness(2.f, curve->GetValue(0.f));
344  ExpectBrightness(3.f, curve->GetValue(0.5f));
345  ExpectBrightness(4.f, curve->GetValue(1.f));
346  ExpectBrightness(6.f, curve->GetValue(1.5f));
347  ExpectBrightness(8.f, curve->GetValue(2.f));
348  ExpectBrightness(8.f, curve->GetValue(3.f));
349}
350
351// Tests that a filter animation with multiple keys at a given time works
352// sanely.
353TEST(KeyframedAnimationCurveTest, RepeatedFilterKeyTimes) {
354  scoped_ptr<KeyframedFilterAnimationCurve> curve(
355      KeyframedFilterAnimationCurve::Create());
356  // A step function.
357  FilterOperations operations1;
358  operations1.Append(FilterOperation::CreateBrightnessFilter(4.f));
359  FilterOperations operations2;
360  operations2.Append(FilterOperation::CreateBrightnessFilter(4.f));
361  FilterOperations operations3;
362  operations3.Append(FilterOperation::CreateBrightnessFilter(6.f));
363  FilterOperations operations4;
364  operations4.Append(FilterOperation::CreateBrightnessFilter(6.f));
365  curve->AddKeyframe(FilterKeyframe::Create(
366      0.f, operations1, scoped_ptr<TimingFunction>()));
367  curve->AddKeyframe(FilterKeyframe::Create(
368      1.f, operations2, scoped_ptr<TimingFunction>()));
369  curve->AddKeyframe(FilterKeyframe::Create(
370      1.f, operations3, scoped_ptr<TimingFunction>()));
371  curve->AddKeyframe(FilterKeyframe::Create(
372      2.f, operations4, scoped_ptr<TimingFunction>()));
373
374  ExpectBrightness(4.f, curve->GetValue(-1.f));
375  ExpectBrightness(4.f, curve->GetValue(0.f));
376  ExpectBrightness(4.f, curve->GetValue(0.5f));
377
378  // There is a discontinuity at 1. Any value between 4 and 6 is valid.
379  FilterOperations value = curve->GetValue(1.f);
380  EXPECT_EQ(1u, value.size());
381  EXPECT_EQ(FilterOperation::BRIGHTNESS, value.at(0).type());
382  EXPECT_GE(value.at(0).amount(), 4);
383  EXPECT_LE(value.at(0).amount(), 6);
384
385  ExpectBrightness(6.f, curve->GetValue(1.5f));
386  ExpectBrightness(6.f, curve->GetValue(2.f));
387  ExpectBrightness(6.f, curve->GetValue(3.f));
388}
389
390// Tests that the keyframes may be added out of order.
391TEST(KeyframedAnimationCurveTest, UnsortedKeyframes) {
392  scoped_ptr<KeyframedFloatAnimationCurve> curve(
393      KeyframedFloatAnimationCurve::Create());
394  curve->AddKeyframe(
395      FloatKeyframe::Create(2.0, 8.f, scoped_ptr<TimingFunction>()));
396  curve->AddKeyframe(
397      FloatKeyframe::Create(0.0, 2.f, scoped_ptr<TimingFunction>()));
398  curve->AddKeyframe(
399      FloatKeyframe::Create(1.0, 4.f, scoped_ptr<TimingFunction>()));
400  EXPECT_FLOAT_EQ(2.f, curve->GetValue(-1.f));
401  EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.f));
402  EXPECT_FLOAT_EQ(3.f, curve->GetValue(0.5f));
403  EXPECT_FLOAT_EQ(4.f, curve->GetValue(1.f));
404  EXPECT_FLOAT_EQ(6.f, curve->GetValue(1.5f));
405  EXPECT_FLOAT_EQ(8.f, curve->GetValue(2.f));
406  EXPECT_FLOAT_EQ(8.f, curve->GetValue(3.f));
407}
408
409// Tests that a cubic bezier timing function works as expected.
410TEST(KeyframedAnimationCurveTest, CubicBezierTimingFunction) {
411  scoped_ptr<KeyframedFloatAnimationCurve> curve(
412      KeyframedFloatAnimationCurve::Create());
413  curve->AddKeyframe(FloatKeyframe::Create(
414      0.0,
415      0.f,
416      CubicBezierTimingFunction::Create(0.25f, 0.f, 0.75f, 1.f)
417          .PassAs<TimingFunction>()));
418  curve->AddKeyframe(
419      FloatKeyframe::Create(1.0, 1.f, scoped_ptr<TimingFunction>()));
420
421  EXPECT_FLOAT_EQ(0.f, curve->GetValue(0.f));
422  EXPECT_LT(0.f, curve->GetValue(0.25f));
423  EXPECT_GT(0.25f, curve->GetValue(0.25f));
424  EXPECT_NEAR(curve->GetValue(0.5f), 0.5f, 0.00015f);
425  EXPECT_LT(0.75f, curve->GetValue(0.75f));
426  EXPECT_GT(1.f, curve->GetValue(0.75f));
427  EXPECT_FLOAT_EQ(1.f, curve->GetValue(1.f));
428}
429
430// Tests that animated bounds are computed as expected.
431TEST(KeyframedAnimationCurveTest, AnimatedBounds) {
432  scoped_ptr<KeyframedTransformAnimationCurve> curve(
433      KeyframedTransformAnimationCurve::Create());
434
435  TransformOperations operations1;
436  curve->AddKeyframe(TransformKeyframe::Create(
437      0.0, operations1, scoped_ptr<TimingFunction>()));
438  operations1.AppendTranslate(2.0, 3.0, -1.0);
439  curve->AddKeyframe(TransformKeyframe::Create(
440      0.5, operations1, scoped_ptr<TimingFunction>()));
441  TransformOperations operations2;
442  operations2.AppendTranslate(4.0, 1.0, 2.0);
443  curve->AddKeyframe(TransformKeyframe::Create(
444      1.0, operations2, EaseTimingFunction::Create()));
445
446  gfx::BoxF box(2.f, 3.f, 4.f, 1.f, 3.f, 2.f);
447  gfx::BoxF bounds;
448
449  EXPECT_TRUE(curve->AnimatedBoundsForBox(box, &bounds));
450  EXPECT_EQ(gfx::BoxF(2.f, 3.f, 3.f, 5.f, 6.f, 5.f).ToString(),
451            bounds.ToString());
452}
453
454// Tests that animations that affect scale are correctly identified.
455TEST(KeyframedAnimationCurveTest, AffectsScale) {
456  scoped_ptr<KeyframedTransformAnimationCurve> curve(
457      KeyframedTransformAnimationCurve::Create());
458
459  TransformOperations operations1;
460  curve->AddKeyframe(TransformKeyframe::Create(
461      0.0, operations1, scoped_ptr<TimingFunction>()));
462  operations1.AppendTranslate(2.0, 3.0, -1.0);
463  TransformOperations operations2;
464  operations2.AppendTranslate(4.0, 1.0, 2.0);
465  curve->AddKeyframe(TransformKeyframe::Create(
466      1.0, operations2, scoped_ptr<TimingFunction>()));
467
468  EXPECT_FALSE(curve->AffectsScale());
469
470  TransformOperations operations3;
471  operations3.AppendScale(2.f, 2.f, 2.f);
472  curve->AddKeyframe(TransformKeyframe::Create(
473      2.0, operations3, scoped_ptr<TimingFunction>()));
474
475  EXPECT_TRUE(curve->AffectsScale());
476
477  TransformOperations operations4;
478  operations3.AppendTranslate(2.f, 2.f, 2.f);
479  curve->AddKeyframe(TransformKeyframe::Create(
480      3.0, operations4, scoped_ptr<TimingFunction>()));
481
482  EXPECT_TRUE(curve->AffectsScale());
483}
484
485// Tests that animations that are translations are correctly identified.
486TEST(KeyframedAnimationCurveTest, IsTranslation) {
487  scoped_ptr<KeyframedTransformAnimationCurve> curve(
488      KeyframedTransformAnimationCurve::Create());
489
490  TransformOperations operations1;
491  curve->AddKeyframe(TransformKeyframe::Create(
492      0.0, operations1, scoped_ptr<TimingFunction>()));
493  operations1.AppendTranslate(2.0, 3.0, -1.0);
494  TransformOperations operations2;
495  operations2.AppendTranslate(4.0, 1.0, 2.0);
496  curve->AddKeyframe(TransformKeyframe::Create(
497      1.0, operations2, scoped_ptr<TimingFunction>()));
498
499  EXPECT_TRUE(curve->IsTranslation());
500
501  TransformOperations operations3;
502  operations3.AppendScale(2.f, 2.f, 2.f);
503  curve->AddKeyframe(TransformKeyframe::Create(
504      2.0, operations3, scoped_ptr<TimingFunction>()));
505
506  EXPECT_FALSE(curve->IsTranslation());
507
508  TransformOperations operations4;
509  operations3.AppendTranslate(2.f, 2.f, 2.f);
510  curve->AddKeyframe(TransformKeyframe::Create(
511      3.0, operations4, scoped_ptr<TimingFunction>()));
512
513  EXPECT_FALSE(curve->IsTranslation());
514}
515
516// Tests that maximum target scale is computed as expected.
517TEST(KeyframedAnimationCurveTest, MaximumTargetScale) {
518  scoped_ptr<KeyframedTransformAnimationCurve> curve(
519      KeyframedTransformAnimationCurve::Create());
520
521  TransformOperations operations1;
522  curve->AddKeyframe(TransformKeyframe::Create(
523      0.0, operations1, scoped_ptr<TimingFunction>()));
524  operations1.AppendScale(2.f, -3.f, 1.f);
525  curve->AddKeyframe(TransformKeyframe::Create(
526      1.0, operations1, EaseTimingFunction::Create()));
527
528  float maximum_scale = 0.f;
529  EXPECT_TRUE(curve->MaximumTargetScale(true, &maximum_scale));
530  EXPECT_EQ(3.f, maximum_scale);
531
532  TransformOperations operations2;
533  operations2.AppendScale(6.f, 3.f, 2.f);
534  curve->AddKeyframe(TransformKeyframe::Create(
535      2.0, operations2, EaseTimingFunction::Create()));
536
537  EXPECT_TRUE(curve->MaximumTargetScale(true, &maximum_scale));
538  EXPECT_EQ(6.f, maximum_scale);
539
540  TransformOperations operations3;
541  operations3.AppendRotate(1.f, 0.f, 0.f, 90.f);
542  curve->AddKeyframe(TransformKeyframe::Create(
543      3.0, operations3, EaseTimingFunction::Create()));
544
545  EXPECT_FALSE(curve->MaximumTargetScale(true, &maximum_scale));
546
547  // The original scale is not used in computing the max.
548  scoped_ptr<KeyframedTransformAnimationCurve> curve2(
549      KeyframedTransformAnimationCurve::Create());
550
551  TransformOperations operations4;
552  operations4.AppendScale(0.4f, 0.2f, 0.6f);
553  curve2->AddKeyframe(TransformKeyframe::Create(
554      0.0, operations4, EaseTimingFunction::Create()));
555  TransformOperations operations5;
556  operations5.AppendScale(0.5f, 0.3f, -0.8f);
557  curve2->AddKeyframe(TransformKeyframe::Create(
558      1.0, operations5, EaseTimingFunction::Create()));
559
560  EXPECT_TRUE(curve2->MaximumTargetScale(true, &maximum_scale));
561  EXPECT_EQ(0.8f, maximum_scale);
562
563  EXPECT_TRUE(curve2->MaximumTargetScale(false, &maximum_scale));
564  EXPECT_EQ(0.6f, maximum_scale);
565}
566
567}  // namespace
568}  // namespace cc
569