| 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 "base/memory/scoped_ptr.h" | |
| 6 #include "cc/timing_function.h" | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 #include "third_party/WebKit/Source/Platform/chromium/public/WebTransformationMa
trix.h" | |
| 9 #include "third_party/WebKit/Source/Platform/chromium/public/WebTransformOperati
ons.h" | |
| 10 #include "webkit/compositor_bindings/web_transform_animation_curve_impl.h" | |
| 11 #include "webkit/compositor_bindings/web_transform_operations_impl.h" | |
| 12 | |
| 13 using namespace WebKit; | |
| 14 | |
| 15 #if WEB_TRANSFORM_OPERATIONS_IS_VIRTUAL | |
| 16 using webkit::WebTransformOperationsImpl; | |
| 17 #endif | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Tests that a transform animation with one keyframe works as expected. | |
| 22 TEST(WebTransformAnimationCurveTest, OneTransformKeyframe) { | |
| 23 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 24 new WebTransformAnimationCurveImpl); | |
| 25 scoped_ptr<WebTransformOperations> operations( | |
| 26 new WebTransformOperationsImpl()); | |
| 27 operations->appendTranslate(2, 0, 0); | |
| 28 curve->add(WebTransformKeyframe(0, operations.release()), | |
| 29 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 30 | |
| 31 EXPECT_FLOAT_EQ(2, curve->getValue(-1).m41()); | |
| 32 EXPECT_FLOAT_EQ(2, curve->getValue(0).m41()); | |
| 33 EXPECT_FLOAT_EQ(2, curve->getValue(0.5).m41()); | |
| 34 EXPECT_FLOAT_EQ(2, curve->getValue(1).m41()); | |
| 35 EXPECT_FLOAT_EQ(2, curve->getValue(2).m41()); | |
| 36 } | |
| 37 | |
| 38 // Tests that a transform animation with two keyframes works as expected. | |
| 39 TEST(WebTransformAnimationCurveTest, TwoTransformKeyframe) { | |
| 40 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 41 new WebTransformAnimationCurveImpl); | |
| 42 scoped_ptr<WebTransformOperations> operations1( | |
| 43 new WebTransformOperationsImpl()); | |
| 44 operations1->appendTranslate(2, 0, 0); | |
| 45 scoped_ptr<WebTransformOperations> operations2( | |
| 46 new WebTransformOperationsImpl()); | |
| 47 operations2->appendTranslate(4, 0, 0); | |
| 48 curve->add(WebTransformKeyframe(0, operations1.release()), | |
| 49 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 50 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 51 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 52 EXPECT_FLOAT_EQ(2, curve->getValue(-1).m41()); | |
| 53 EXPECT_FLOAT_EQ(2, curve->getValue(0).m41()); | |
| 54 EXPECT_FLOAT_EQ(3, curve->getValue(0.5).m41()); | |
| 55 EXPECT_FLOAT_EQ(4, curve->getValue(1).m41()); | |
| 56 EXPECT_FLOAT_EQ(4, curve->getValue(2).m41()); | |
| 57 } | |
| 58 | |
| 59 // Tests that a transform animation with three keyframes works as expected. | |
| 60 TEST(WebTransformAnimationCurveTest, ThreeTransformKeyframe) { | |
| 61 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 62 new WebTransformAnimationCurveImpl); | |
| 63 scoped_ptr<WebTransformOperations> operations1( | |
| 64 new WebTransformOperationsImpl()); | |
| 65 operations1->appendTranslate(2, 0, 0); | |
| 66 scoped_ptr<WebTransformOperations> operations2( | |
| 67 new WebTransformOperationsImpl()); | |
| 68 operations2->appendTranslate(4, 0, 0); | |
| 69 scoped_ptr<WebTransformOperations> operations3( | |
| 70 new WebTransformOperationsImpl()); | |
| 71 operations3->appendTranslate(8, 0, 0); | |
| 72 curve->add(WebTransformKeyframe(0, operations1.release()), | |
| 73 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 74 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 75 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 76 curve->add(WebTransformKeyframe(2, operations3.release()), | |
| 77 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 78 EXPECT_FLOAT_EQ(2, curve->getValue(-1).m41()); | |
| 79 EXPECT_FLOAT_EQ(2, curve->getValue(0).m41()); | |
| 80 EXPECT_FLOAT_EQ(3, curve->getValue(0.5).m41()); | |
| 81 EXPECT_FLOAT_EQ(4, curve->getValue(1).m41()); | |
| 82 EXPECT_FLOAT_EQ(6, curve->getValue(1.5).m41()); | |
| 83 EXPECT_FLOAT_EQ(8, curve->getValue(2).m41()); | |
| 84 EXPECT_FLOAT_EQ(8, curve->getValue(3).m41()); | |
| 85 } | |
| 86 | |
| 87 // Tests that a transform animation with multiple keys at a given time works san
ely. | |
| 88 TEST(WebTransformAnimationCurveTest, RepeatedTransformKeyTimes) { | |
| 89 // A step function. | |
| 90 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 91 new WebTransformAnimationCurveImpl); | |
| 92 scoped_ptr<WebTransformOperations> operations1( | |
| 93 new WebTransformOperationsImpl()); | |
| 94 operations1->appendTranslate(4, 0, 0); | |
| 95 scoped_ptr<WebTransformOperations> operations2( | |
| 96 new WebTransformOperationsImpl()); | |
| 97 operations2->appendTranslate(4, 0, 0); | |
| 98 scoped_ptr<WebTransformOperations> operations3( | |
| 99 new WebTransformOperationsImpl()); | |
| 100 operations3->appendTranslate(6, 0, 0); | |
| 101 scoped_ptr<WebTransformOperations> operations4( | |
| 102 new WebTransformOperationsImpl()); | |
| 103 operations4->appendTranslate(6, 0, 0); | |
| 104 curve->add(WebTransformKeyframe(0, operations1.release()), | |
| 105 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 106 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 107 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 108 curve->add(WebTransformKeyframe(1, operations3.release()), | |
| 109 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 110 curve->add(WebTransformKeyframe(2, operations4.release()), | |
| 111 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 112 | |
| 113 EXPECT_FLOAT_EQ(4, curve->getValue(-1).m41()); | |
| 114 EXPECT_FLOAT_EQ(4, curve->getValue(0).m41()); | |
| 115 EXPECT_FLOAT_EQ(4, curve->getValue(0.5).m41()); | |
| 116 | |
| 117 // There is a discontinuity at 1. Any value between 4 and 6 is valid. | |
| 118 WebTransformationMatrix value = curve->getValue(1); | |
| 119 EXPECT_TRUE(value.m41() >= 4 && value.m41() <= 6); | |
| 120 | |
| 121 EXPECT_FLOAT_EQ(6, curve->getValue(1.5).m41()); | |
| 122 EXPECT_FLOAT_EQ(6, curve->getValue(2).m41()); | |
| 123 EXPECT_FLOAT_EQ(6, curve->getValue(3).m41()); | |
| 124 } | |
| 125 | |
| 126 // Tests that the keyframes may be added out of order. | |
| 127 TEST(WebTransformAnimationCurveTest, UnsortedKeyframes) { | |
| 128 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 129 new WebTransformAnimationCurveImpl); | |
| 130 scoped_ptr<WebTransformOperations> operations1( | |
| 131 new WebTransformOperationsImpl()); | |
| 132 operations1->appendTranslate(2, 0, 0); | |
| 133 scoped_ptr<WebTransformOperations> operations2( | |
| 134 new WebTransformOperationsImpl()); | |
| 135 operations2->appendTranslate(4, 0, 0); | |
| 136 scoped_ptr<WebTransformOperations> operations3( | |
| 137 new WebTransformOperationsImpl()); | |
| 138 operations3->appendTranslate(8, 0, 0); | |
| 139 curve->add(WebTransformKeyframe(2, operations3.release()), | |
| 140 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 141 curve->add(WebTransformKeyframe(0, operations1.release()), | |
| 142 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 143 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 144 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 145 | |
| 146 EXPECT_FLOAT_EQ(2, curve->getValue(-1).m41()); | |
| 147 EXPECT_FLOAT_EQ(2, curve->getValue(0).m41()); | |
| 148 EXPECT_FLOAT_EQ(3, curve->getValue(0.5).m41()); | |
| 149 EXPECT_FLOAT_EQ(4, curve->getValue(1).m41()); | |
| 150 EXPECT_FLOAT_EQ(6, curve->getValue(1.5).m41()); | |
| 151 EXPECT_FLOAT_EQ(8, curve->getValue(2).m41()); | |
| 152 EXPECT_FLOAT_EQ(8, curve->getValue(3).m41()); | |
| 153 } | |
| 154 | |
| 155 // Tests that a cubic bezier timing function works as expected. | |
| 156 TEST(WebTransformAnimationCurveTest, CubicBezierTimingFunction) { | |
| 157 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 158 new WebTransformAnimationCurveImpl); | |
| 159 scoped_ptr<WebTransformOperations> operations1( | |
| 160 new WebTransformOperationsImpl()); | |
| 161 operations1->appendTranslate(0, 0, 0); | |
| 162 scoped_ptr<WebTransformOperations> operations2( | |
| 163 new WebTransformOperationsImpl()); | |
| 164 operations2->appendTranslate(1, 0, 0); | |
| 165 curve->add(WebTransformKeyframe(0, operations1.release()), 0.25, 0, 0.75, 1); | |
| 166 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 167 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 168 EXPECT_FLOAT_EQ(0, curve->getValue(0).m41()); | |
| 169 EXPECT_LT(0, curve->getValue(0.25).m41()); | |
| 170 EXPECT_GT(0.25, curve->getValue(0.25).m41()); | |
| 171 EXPECT_NEAR(curve->getValue(0.5).m41(), 0.5, 0.00015); | |
| 172 EXPECT_LT(0.75, curve->getValue(0.75).m41()); | |
| 173 EXPECT_GT(1, curve->getValue(0.75).m41()); | |
| 174 EXPECT_FLOAT_EQ(1, curve->getValue(1).m41()); | |
| 175 } | |
| 176 | |
| 177 // Tests that an ease timing function works as expected. | |
| 178 TEST(WebTransformAnimationCurveTest, EaseTimingFunction) { | |
| 179 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 180 new WebTransformAnimationCurveImpl); | |
| 181 scoped_ptr<WebTransformOperations> operations1( | |
| 182 new WebTransformOperationsImpl()); | |
| 183 operations1->appendTranslate(0, 0, 0); | |
| 184 scoped_ptr<WebTransformOperations> operations2( | |
| 185 new WebTransformOperationsImpl()); | |
| 186 operations2->appendTranslate(1, 0, 0); | |
| 187 curve->add(WebTransformKeyframe(0, operations1.release()), | |
| 188 WebAnimationCurve::TimingFunctionTypeEase); | |
| 189 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 190 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 191 | |
| 192 scoped_ptr<cc::TimingFunction> timing_function( | |
| 193 cc::EaseTimingFunction::create()); | |
| 194 for (int i = 0; i <= 4; ++i) { | |
| 195 const double time = i * 0.25; | |
| 196 EXPECT_FLOAT_EQ(timing_function->getValue(time), | |
| 197 curve->getValue(time).m41()); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 // Tests using a linear timing function. | |
| 202 TEST(WebTransformAnimationCurveTest, LinearTimingFunction) { | |
| 203 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 204 new WebTransformAnimationCurveImpl); | |
| 205 scoped_ptr<WebTransformOperations> operations1( | |
| 206 new WebTransformOperationsImpl()); | |
| 207 operations1->appendTranslate(0, 0, 0); | |
| 208 scoped_ptr<WebTransformOperations> operations2( | |
| 209 new WebTransformOperationsImpl()); | |
| 210 operations2->appendTranslate(1, 0, 0); | |
| 211 curve->add(WebTransformKeyframe(0, operations1.release()), | |
| 212 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 213 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 214 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 215 | |
| 216 for (int i = 0; i <= 4; ++i) { | |
| 217 const double time = i * 0.25; | |
| 218 EXPECT_FLOAT_EQ(time, curve->getValue(time).m41()); | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 // Tests that an ease in timing function works as expected. | |
| 223 TEST(WebTransformAnimationCurveTest, EaseInTimingFunction) { | |
| 224 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 225 new WebTransformAnimationCurveImpl); | |
| 226 scoped_ptr<WebTransformOperations> operations1( | |
| 227 new WebTransformOperationsImpl()); | |
| 228 operations1->appendTranslate(0, 0, 0); | |
| 229 scoped_ptr<WebTransformOperations> operations2( | |
| 230 new WebTransformOperationsImpl()); | |
| 231 operations2->appendTranslate(1, 0, 0); | |
| 232 curve->add(WebTransformKeyframe(0, operations1.release()), | |
| 233 WebAnimationCurve::TimingFunctionTypeEaseIn); | |
| 234 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 235 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 236 | |
| 237 scoped_ptr<cc::TimingFunction> timing_function( | |
| 238 cc::EaseInTimingFunction::create()); | |
| 239 for (int i = 0; i <= 4; ++i) { | |
| 240 const double time = i * 0.25; | |
| 241 EXPECT_FLOAT_EQ(timing_function->getValue(time), | |
| 242 curve->getValue(time).m41()); | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 // Tests that an ease in timing function works as expected. | |
| 247 TEST(WebTransformAnimationCurveTest, EaseOutTimingFunction) { | |
| 248 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 249 new WebTransformAnimationCurveImpl); | |
| 250 scoped_ptr<WebTransformOperations> operations1( | |
| 251 new WebTransformOperationsImpl()); | |
| 252 operations1->appendTranslate(0, 0, 0); | |
| 253 scoped_ptr<WebTransformOperations> operations2( | |
| 254 new WebTransformOperationsImpl()); | |
| 255 operations2->appendTranslate(1, 0, 0); | |
| 256 curve->add(WebTransformKeyframe(0, operations1.release()), | |
| 257 WebAnimationCurve::TimingFunctionTypeEaseOut); | |
| 258 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 259 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 260 | |
| 261 scoped_ptr<cc::TimingFunction> timing_function( | |
| 262 cc::EaseOutTimingFunction::create()); | |
| 263 for (int i = 0; i <= 4; ++i) { | |
| 264 const double time = i * 0.25; | |
| 265 EXPECT_FLOAT_EQ(timing_function->getValue(time), | |
| 266 curve->getValue(time).m41()); | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 // Tests that an ease in timing function works as expected. | |
| 271 TEST(WebTransformAnimationCurveTest, EaseInOutTimingFunction) { | |
| 272 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 273 new WebTransformAnimationCurveImpl); | |
| 274 scoped_ptr<WebTransformOperations> operations1( | |
| 275 new WebTransformOperationsImpl()); | |
| 276 operations1->appendTranslate(0, 0, 0); | |
| 277 scoped_ptr<WebTransformOperations> operations2( | |
| 278 new WebTransformOperationsImpl()); | |
| 279 operations2->appendTranslate(1, 0, 0); | |
| 280 curve->add(WebTransformKeyframe(0, operations1.release()), | |
| 281 WebAnimationCurve::TimingFunctionTypeEaseInOut); | |
| 282 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 283 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 284 | |
| 285 scoped_ptr<cc::TimingFunction> timing_function( | |
| 286 cc::EaseInOutTimingFunction::create()); | |
| 287 for (int i = 0; i <= 4; ++i) { | |
| 288 const double time = i * 0.25; | |
| 289 EXPECT_FLOAT_EQ(timing_function->getValue(time), | |
| 290 curve->getValue(time).m41()); | |
| 291 } | |
| 292 } | |
| 293 | |
| 294 // Tests that an ease in timing function works as expected. | |
| 295 TEST(WebTransformAnimationCurveTest, CustomBezierTimingFunction) { | |
| 296 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 297 new WebTransformAnimationCurveImpl); | |
| 298 double x1 = 0.3; | |
| 299 double y1 = 0.2; | |
| 300 double x2 = 0.8; | |
| 301 double y2 = 0.7; | |
| 302 scoped_ptr<WebTransformOperations> operations1( | |
| 303 new WebTransformOperationsImpl()); | |
| 304 operations1->appendTranslate(0, 0, 0); | |
| 305 scoped_ptr<WebTransformOperations> operations2( | |
| 306 new WebTransformOperationsImpl()); | |
| 307 operations2->appendTranslate(1, 0, 0); | |
| 308 curve->add(WebTransformKeyframe(0, operations1.release()), x1, y1, x2, y2); | |
| 309 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 310 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 311 | |
| 312 scoped_ptr<cc::TimingFunction> timing_function( | |
| 313 cc::CubicBezierTimingFunction::create(x1, y1, x2, y2)); | |
| 314 for (int i = 0; i <= 4; ++i) { | |
| 315 const double time = i * 0.25; | |
| 316 EXPECT_FLOAT_EQ(timing_function->getValue(time), | |
| 317 curve->getValue(time).m41()); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 // Tests that the default timing function is indeed ease. | |
| 322 TEST(WebTransformAnimationCurveTest, DefaultTimingFunction) { | |
| 323 scoped_ptr<WebTransformAnimationCurve> curve( | |
| 324 new WebTransformAnimationCurveImpl); | |
| 325 scoped_ptr<WebTransformOperations> operations1( | |
| 326 new WebTransformOperationsImpl()); | |
| 327 operations1->appendTranslate(0, 0, 0); | |
| 328 scoped_ptr<WebTransformOperations> operations2( | |
| 329 new WebTransformOperationsImpl()); | |
| 330 operations2->appendTranslate(1, 0, 0); | |
| 331 curve->add(WebTransformKeyframe(0, operations1.release())); | |
| 332 curve->add(WebTransformKeyframe(1, operations2.release()), | |
| 333 WebAnimationCurve::TimingFunctionTypeLinear); | |
| 334 | |
| 335 scoped_ptr<cc::TimingFunction> timing_function( | |
| 336 cc::EaseTimingFunction::create()); | |
| 337 for (int i = 0; i <= 4; ++i) { | |
| 338 const double time = i * 0.25; | |
| 339 EXPECT_FLOAT_EQ(timing_function->getValue(time), | |
| 340 curve->getValue(time).m41()); | |
| 341 } | |
| 342 } | |
| 343 | |
| 344 } // namespace | |
| OLD | NEW |