1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "cc/animation/timing_function.h"
7 #include "cc/blink/web_float_animation_curve_impl.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using blink::WebCompositorAnimationCurve
;
11 using blink::WebFloatAnimationCurve
;
12 using blink::WebFloatKeyframe
;
17 // Tests that a float animation with one keyframe works as expected.
18 TEST(WebFloatAnimationCurveTest
, OneFloatKeyframe
) {
19 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
20 curve
->add(WebFloatKeyframe(0, 2),
21 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
22 EXPECT_FLOAT_EQ(2, curve
->getValue(-1));
23 EXPECT_FLOAT_EQ(2, curve
->getValue(0));
24 EXPECT_FLOAT_EQ(2, curve
->getValue(0.5));
25 EXPECT_FLOAT_EQ(2, curve
->getValue(1));
26 EXPECT_FLOAT_EQ(2, curve
->getValue(2));
29 // Tests that a float animation with two keyframes works as expected.
30 TEST(WebFloatAnimationCurveTest
, TwoFloatKeyframe
) {
31 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
32 curve
->add(WebFloatKeyframe(0, 2),
33 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
34 curve
->add(WebFloatKeyframe(1, 4),
35 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
36 EXPECT_FLOAT_EQ(2, curve
->getValue(-1));
37 EXPECT_FLOAT_EQ(2, curve
->getValue(0));
38 EXPECT_FLOAT_EQ(3, curve
->getValue(0.5));
39 EXPECT_FLOAT_EQ(4, curve
->getValue(1));
40 EXPECT_FLOAT_EQ(4, curve
->getValue(2));
43 // Tests that a float animation with three keyframes works as expected.
44 TEST(WebFloatAnimationCurveTest
, ThreeFloatKeyframe
) {
45 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
46 curve
->add(WebFloatKeyframe(0, 2),
47 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
48 curve
->add(WebFloatKeyframe(1, 4),
49 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
50 curve
->add(WebFloatKeyframe(2, 8),
51 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
52 EXPECT_FLOAT_EQ(2, curve
->getValue(-1));
53 EXPECT_FLOAT_EQ(2, curve
->getValue(0));
54 EXPECT_FLOAT_EQ(3, curve
->getValue(0.5));
55 EXPECT_FLOAT_EQ(4, curve
->getValue(1));
56 EXPECT_FLOAT_EQ(6, curve
->getValue(1.5));
57 EXPECT_FLOAT_EQ(8, curve
->getValue(2));
58 EXPECT_FLOAT_EQ(8, curve
->getValue(3));
61 // Tests that a float animation with multiple keys at a given time works sanely.
62 TEST(WebFloatAnimationCurveTest
, RepeatedFloatKeyTimes
) {
63 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
64 curve
->add(WebFloatKeyframe(0, 4),
65 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
66 curve
->add(WebFloatKeyframe(1, 4),
67 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
68 curve
->add(WebFloatKeyframe(1, 6),
69 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
70 curve
->add(WebFloatKeyframe(2, 6),
71 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
73 EXPECT_FLOAT_EQ(4, curve
->getValue(-1));
74 EXPECT_FLOAT_EQ(4, curve
->getValue(0));
75 EXPECT_FLOAT_EQ(4, curve
->getValue(0.5));
77 // There is a discontinuity at 1. Any value between 4 and 6 is valid.
78 float value
= curve
->getValue(1);
79 EXPECT_TRUE(value
>= 4 && value
<= 6);
81 EXPECT_FLOAT_EQ(6, curve
->getValue(1.5));
82 EXPECT_FLOAT_EQ(6, curve
->getValue(2));
83 EXPECT_FLOAT_EQ(6, curve
->getValue(3));
86 // Tests that the keyframes may be added out of order.
87 TEST(WebFloatAnimationCurveTest
, UnsortedKeyframes
) {
88 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
89 curve
->add(WebFloatKeyframe(2, 8),
90 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
91 curve
->add(WebFloatKeyframe(0, 2),
92 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
93 curve
->add(WebFloatKeyframe(1, 4),
94 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
96 EXPECT_FLOAT_EQ(2, curve
->getValue(-1));
97 EXPECT_FLOAT_EQ(2, curve
->getValue(0));
98 EXPECT_FLOAT_EQ(3, curve
->getValue(0.5));
99 EXPECT_FLOAT_EQ(4, curve
->getValue(1));
100 EXPECT_FLOAT_EQ(6, curve
->getValue(1.5));
101 EXPECT_FLOAT_EQ(8, curve
->getValue(2));
102 EXPECT_FLOAT_EQ(8, curve
->getValue(3));
105 // Tests that a cubic bezier timing function works as expected.
106 TEST(WebFloatAnimationCurveTest
, CubicBezierTimingFunction
) {
107 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
108 curve
->add(WebFloatKeyframe(0, 0), 0.25, 0, 0.75, 1);
109 curve
->add(WebFloatKeyframe(1, 1),
110 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
112 EXPECT_FLOAT_EQ(0, curve
->getValue(0));
113 EXPECT_LT(0, curve
->getValue(0.25));
114 EXPECT_GT(0.25, curve
->getValue(0.25));
115 EXPECT_NEAR(curve
->getValue(0.5), 0.5, 0.00015);
116 EXPECT_LT(0.75, curve
->getValue(0.75));
117 EXPECT_GT(1, curve
->getValue(0.75));
118 EXPECT_FLOAT_EQ(1, curve
->getValue(1));
121 // Tests that an ease timing function works as expected.
122 TEST(WebFloatAnimationCurveTest
, EaseTimingFunction
) {
123 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
124 curve
->add(WebFloatKeyframe(0, 0),
125 WebCompositorAnimationCurve::TimingFunctionTypeEase
);
126 curve
->add(WebFloatKeyframe(1, 1),
127 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
129 scoped_ptr
<cc::TimingFunction
> timing_function(
130 cc::EaseTimingFunction::Create());
131 for (int i
= 0; i
<= 4; ++i
) {
132 const double time
= i
* 0.25;
133 EXPECT_FLOAT_EQ(timing_function
->GetValue(time
), curve
->getValue(time
));
137 // Tests using a linear timing function.
138 TEST(WebFloatAnimationCurveTest
, LinearTimingFunction
) {
139 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
140 curve
->add(WebFloatKeyframe(0, 0),
141 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
142 curve
->add(WebFloatKeyframe(1, 1),
143 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
145 for (int i
= 0; i
<= 4; ++i
) {
146 const double time
= i
* 0.25;
147 EXPECT_FLOAT_EQ(time
, curve
->getValue(time
));
151 // Tests that an ease in timing function works as expected.
152 TEST(WebFloatAnimationCurveTest
, EaseInTimingFunction
) {
153 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
154 curve
->add(WebFloatKeyframe(0, 0),
155 WebCompositorAnimationCurve::TimingFunctionTypeEaseIn
);
156 curve
->add(WebFloatKeyframe(1, 1),
157 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
159 scoped_ptr
<cc::TimingFunction
> timing_function(
160 cc::EaseInTimingFunction::Create());
161 for (int i
= 0; i
<= 4; ++i
) {
162 const double time
= i
* 0.25;
163 EXPECT_FLOAT_EQ(timing_function
->GetValue(time
), curve
->getValue(time
));
167 // Tests that an ease in timing function works as expected.
168 TEST(WebFloatAnimationCurveTest
, EaseOutTimingFunction
) {
169 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
170 curve
->add(WebFloatKeyframe(0, 0),
171 WebCompositorAnimationCurve::TimingFunctionTypeEaseOut
);
172 curve
->add(WebFloatKeyframe(1, 1),
173 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
175 scoped_ptr
<cc::TimingFunction
> timing_function(
176 cc::EaseOutTimingFunction::Create());
177 for (int i
= 0; i
<= 4; ++i
) {
178 const double time
= i
* 0.25;
179 EXPECT_FLOAT_EQ(timing_function
->GetValue(time
), curve
->getValue(time
));
183 // Tests that an ease in timing function works as expected.
184 TEST(WebFloatAnimationCurveTest
, EaseInOutTimingFunction
) {
185 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
186 curve
->add(WebFloatKeyframe(0, 0),
187 WebCompositorAnimationCurve::TimingFunctionTypeEaseInOut
);
188 curve
->add(WebFloatKeyframe(1, 1),
189 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
191 scoped_ptr
<cc::TimingFunction
> timing_function(
192 cc::EaseInOutTimingFunction::Create());
193 for (int i
= 0; i
<= 4; ++i
) {
194 const double time
= i
* 0.25;
195 EXPECT_FLOAT_EQ(timing_function
->GetValue(time
), curve
->getValue(time
));
199 // Tests that an ease in timing function works as expected.
200 TEST(WebFloatAnimationCurveTest
, CustomBezierTimingFunction
) {
201 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
206 curve
->add(WebFloatKeyframe(0, 0), x1
, y1
, x2
, y2
);
207 curve
->add(WebFloatKeyframe(1, 1),
208 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
210 scoped_ptr
<cc::TimingFunction
> timing_function(
211 cc::CubicBezierTimingFunction::Create(x1
, y1
, x2
, y2
));
212 for (int i
= 0; i
<= 4; ++i
) {
213 const double time
= i
* 0.25;
214 EXPECT_FLOAT_EQ(timing_function
->GetValue(time
), curve
->getValue(time
));
218 // Tests that the default timing function is indeed ease.
219 TEST(WebFloatAnimationCurveTest
, DefaultTimingFunction
) {
220 scoped_ptr
<WebFloatAnimationCurve
> curve(new WebFloatAnimationCurveImpl
);
221 curve
->add(WebFloatKeyframe(0, 0));
222 curve
->add(WebFloatKeyframe(1, 1),
223 WebCompositorAnimationCurve::TimingFunctionTypeLinear
);
225 scoped_ptr
<cc::TimingFunction
> timing_function(
226 cc::EaseTimingFunction::Create());
227 for (int i
= 0; i
<= 4; ++i
) {
228 const double time
= i
* 0.25;
229 EXPECT_FLOAT_EQ(timing_function
->GetValue(time
), curve
->getValue(time
));
234 } // namespace cc_blink