OLD | NEW |
(Empty) | |
| 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 "config.h" |
| 6 |
| 7 #include <public/WebFloatAnimationCurve.h> |
| 8 |
| 9 #include "CCTimingFunction.h" |
| 10 |
| 11 #include <gtest/gtest.h> |
| 12 #include <wtf/OwnPtr.h> |
| 13 #include <wtf/PassOwnPtr.h> |
| 14 |
| 15 using namespace WebKit; |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Tests that a float animation with one keyframe works as expected. |
| 20 TEST(WebFloatAnimationCurveTest, OneFloatKeyframe) |
| 21 { |
| 22 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 23 curve->add(WebFloatKeyframe(0, 2), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 24 EXPECT_FLOAT_EQ(2, curve->getValue(-1)); |
| 25 EXPECT_FLOAT_EQ(2, curve->getValue(0)); |
| 26 EXPECT_FLOAT_EQ(2, curve->getValue(0.5)); |
| 27 EXPECT_FLOAT_EQ(2, curve->getValue(1)); |
| 28 EXPECT_FLOAT_EQ(2, curve->getValue(2)); |
| 29 } |
| 30 |
| 31 // Tests that a float animation with two keyframes works as expected. |
| 32 TEST(WebFloatAnimationCurveTest, TwoFloatKeyframe) |
| 33 { |
| 34 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 35 curve->add(WebFloatKeyframe(0, 2), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 36 curve->add(WebFloatKeyframe(1, 4), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 37 EXPECT_FLOAT_EQ(2, curve->getValue(-1)); |
| 38 EXPECT_FLOAT_EQ(2, curve->getValue(0)); |
| 39 EXPECT_FLOAT_EQ(3, curve->getValue(0.5)); |
| 40 EXPECT_FLOAT_EQ(4, curve->getValue(1)); |
| 41 EXPECT_FLOAT_EQ(4, curve->getValue(2)); |
| 42 } |
| 43 |
| 44 // Tests that a float animation with three keyframes works as expected. |
| 45 TEST(WebFloatAnimationCurveTest, ThreeFloatKeyframe) |
| 46 { |
| 47 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 48 curve->add(WebFloatKeyframe(0, 2), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 49 curve->add(WebFloatKeyframe(1, 4), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 50 curve->add(WebFloatKeyframe(2, 8), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 51 EXPECT_FLOAT_EQ(2, curve->getValue(-1)); |
| 52 EXPECT_FLOAT_EQ(2, curve->getValue(0)); |
| 53 EXPECT_FLOAT_EQ(3, curve->getValue(0.5)); |
| 54 EXPECT_FLOAT_EQ(4, curve->getValue(1)); |
| 55 EXPECT_FLOAT_EQ(6, curve->getValue(1.5)); |
| 56 EXPECT_FLOAT_EQ(8, curve->getValue(2)); |
| 57 EXPECT_FLOAT_EQ(8, curve->getValue(3)); |
| 58 } |
| 59 |
| 60 // Tests that a float animation with multiple keys at a given time works sanely. |
| 61 TEST(WebFloatAnimationCurveTest, RepeatedFloatKeyTimes) |
| 62 { |
| 63 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 64 curve->add(WebFloatKeyframe(0, 4), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 65 curve->add(WebFloatKeyframe(1, 4), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 66 curve->add(WebFloatKeyframe(1, 6), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 67 curve->add(WebFloatKeyframe(2, 6), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 68 |
| 69 EXPECT_FLOAT_EQ(4, curve->getValue(-1)); |
| 70 EXPECT_FLOAT_EQ(4, curve->getValue(0)); |
| 71 EXPECT_FLOAT_EQ(4, curve->getValue(0.5)); |
| 72 |
| 73 // There is a discontinuity at 1. Any value between 4 and 6 is valid. |
| 74 float value = curve->getValue(1); |
| 75 EXPECT_TRUE(value >= 4 && value <= 6); |
| 76 |
| 77 EXPECT_FLOAT_EQ(6, curve->getValue(1.5)); |
| 78 EXPECT_FLOAT_EQ(6, curve->getValue(2)); |
| 79 EXPECT_FLOAT_EQ(6, curve->getValue(3)); |
| 80 } |
| 81 |
| 82 // Tests that the keyframes may be added out of order. |
| 83 TEST(WebFloatAnimationCurveTest, UnsortedKeyframes) |
| 84 { |
| 85 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 86 curve->add(WebFloatKeyframe(2, 8), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 87 curve->add(WebFloatKeyframe(0, 2), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 88 curve->add(WebFloatKeyframe(1, 4), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 89 |
| 90 EXPECT_FLOAT_EQ(2, curve->getValue(-1)); |
| 91 EXPECT_FLOAT_EQ(2, curve->getValue(0)); |
| 92 EXPECT_FLOAT_EQ(3, curve->getValue(0.5)); |
| 93 EXPECT_FLOAT_EQ(4, curve->getValue(1)); |
| 94 EXPECT_FLOAT_EQ(6, curve->getValue(1.5)); |
| 95 EXPECT_FLOAT_EQ(8, curve->getValue(2)); |
| 96 EXPECT_FLOAT_EQ(8, curve->getValue(3)); |
| 97 } |
| 98 |
| 99 // Tests that a cubic bezier timing function works as expected. |
| 100 TEST(WebFloatAnimationCurveTest, CubicBezierTimingFunction) |
| 101 { |
| 102 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 103 curve->add(WebFloatKeyframe(0, 0), 0.25, 0, 0.75, 1); |
| 104 curve->add(WebFloatKeyframe(1, 1), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 105 |
| 106 EXPECT_FLOAT_EQ(0, curve->getValue(0)); |
| 107 EXPECT_LT(0, curve->getValue(0.25)); |
| 108 EXPECT_GT(0.25, curve->getValue(0.25)); |
| 109 EXPECT_FLOAT_EQ(0.5, curve->getValue(0.5)); |
| 110 EXPECT_LT(0.75, curve->getValue(0.75)); |
| 111 EXPECT_GT(1, curve->getValue(0.75)); |
| 112 EXPECT_FLOAT_EQ(1, curve->getValue(1)); |
| 113 } |
| 114 |
| 115 // Tests that an ease timing function works as expected. |
| 116 TEST(WebFloatAnimationCurveTest, EaseTimingFunction) |
| 117 { |
| 118 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 119 curve->add(WebFloatKeyframe(0, 0), WebAnimationCurve::TimingFunctionTypeEase
); |
| 120 curve->add(WebFloatKeyframe(1, 1), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 121 |
| 122 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseTimingFuncti
on::create()); |
| 123 for (int i = 0; i <= 4; ++i) { |
| 124 const double time = i * 0.25; |
| 125 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time)); |
| 126 } |
| 127 } |
| 128 |
| 129 // Tests using a linear timing function. |
| 130 TEST(WebFloatAnimationCurveTest, LinearTimingFunction) |
| 131 { |
| 132 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 133 curve->add(WebFloatKeyframe(0, 0), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 134 curve->add(WebFloatKeyframe(1, 1), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 135 |
| 136 for (int i = 0; i <= 4; ++i) { |
| 137 const double time = i * 0.25; |
| 138 EXPECT_FLOAT_EQ(time, curve->getValue(time)); |
| 139 } |
| 140 } |
| 141 |
| 142 // Tests that an ease in timing function works as expected. |
| 143 TEST(WebFloatAnimationCurveTest, EaseInTimingFunction) |
| 144 { |
| 145 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 146 curve->add(WebFloatKeyframe(0, 0), WebAnimationCurve::TimingFunctionTypeEase
In); |
| 147 curve->add(WebFloatKeyframe(1, 1), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 148 |
| 149 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseInTimingFunc
tion::create()); |
| 150 for (int i = 0; i <= 4; ++i) { |
| 151 const double time = i * 0.25; |
| 152 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time)); |
| 153 } |
| 154 } |
| 155 |
| 156 // Tests that an ease in timing function works as expected. |
| 157 TEST(WebFloatAnimationCurveTest, EaseOutTimingFunction) |
| 158 { |
| 159 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 160 curve->add(WebFloatKeyframe(0, 0), WebAnimationCurve::TimingFunctionTypeEase
Out); |
| 161 curve->add(WebFloatKeyframe(1, 1), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 162 |
| 163 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseOutTimingFun
ction::create()); |
| 164 for (int i = 0; i <= 4; ++i) { |
| 165 const double time = i * 0.25; |
| 166 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time)); |
| 167 } |
| 168 } |
| 169 |
| 170 // Tests that an ease in timing function works as expected. |
| 171 TEST(WebFloatAnimationCurveTest, EaseInOutTimingFunction) |
| 172 { |
| 173 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 174 curve->add(WebFloatKeyframe(0, 0), WebAnimationCurve::TimingFunctionTypeEase
InOut); |
| 175 curve->add(WebFloatKeyframe(1, 1), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 176 |
| 177 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseInOutTimingF
unction::create()); |
| 178 for (int i = 0; i <= 4; ++i) { |
| 179 const double time = i * 0.25; |
| 180 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time)); |
| 181 } |
| 182 } |
| 183 |
| 184 // Tests that an ease in timing function works as expected. |
| 185 TEST(WebFloatAnimationCurveTest, CustomBezierTimingFunction) |
| 186 { |
| 187 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 188 double x1 = 0.3; |
| 189 double y1 = 0.2; |
| 190 double x2 = 0.8; |
| 191 double y2 = 0.7; |
| 192 curve->add(WebFloatKeyframe(0, 0), x1, y1, x2, y2); |
| 193 curve->add(WebFloatKeyframe(1, 1), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 194 |
| 195 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCCubicBezierTimin
gFunction::create(x1, y1, x2, y2)); |
| 196 for (int i = 0; i <= 4; ++i) { |
| 197 const double time = i * 0.25; |
| 198 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time)); |
| 199 } |
| 200 } |
| 201 |
| 202 // Tests that the default timing function is indeed ease. |
| 203 TEST(WebFloatAnimationCurveTest, DefaultTimingFunction) |
| 204 { |
| 205 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(WebFloatAnimationCurve::crea
te()); |
| 206 curve->add(WebFloatKeyframe(0, 0)); |
| 207 curve->add(WebFloatKeyframe(1, 1), WebAnimationCurve::TimingFunctionTypeLine
ar); |
| 208 |
| 209 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseTimingFuncti
on::create()); |
| 210 for (int i = 0; i <= 4; ++i) { |
| 211 const double time = i * 0.25; |
| 212 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time)); |
| 213 } |
| 214 } |
| 215 |
| 216 } // namespace |
OLD | NEW |