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/WebTransformAnimationCurve.h> |
| 8 |
| 9 #include "CCTimingFunction.h" |
| 10 |
| 11 #include <gtest/gtest.h> |
| 12 #include <public/WebTransformOperations.h> |
| 13 #include <public/WebTransformationMatrix.h> |
| 14 #include <wtf/OwnPtr.h> |
| 15 #include <wtf/PassOwnPtr.h> |
| 16 |
| 17 using namespace WebKit; |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Tests that a transform animation with one keyframe works as expected. |
| 22 TEST(WebTransformAnimationCurveTest, OneTransformKeyframe) |
| 23 { |
| 24 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 25 WebKit::WebTransformOperations operations; |
| 26 operations.appendTranslate(2, 0, 0); |
| 27 curve->add(WebTransformKeyframe(0, operations), WebAnimationCurve::TimingFun
ctionTypeLinear); |
| 28 |
| 29 EXPECT_FLOAT_EQ(2, curve->getValue(-1).m41()); |
| 30 EXPECT_FLOAT_EQ(2, curve->getValue(0).m41()); |
| 31 EXPECT_FLOAT_EQ(2, curve->getValue(0.5).m41()); |
| 32 EXPECT_FLOAT_EQ(2, curve->getValue(1).m41()); |
| 33 EXPECT_FLOAT_EQ(2, curve->getValue(2).m41()); |
| 34 } |
| 35 |
| 36 // Tests that a transform animation with two keyframes works as expected. |
| 37 TEST(WebTransformAnimationCurveTest, TwoTransformKeyframe) |
| 38 { |
| 39 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 40 WebKit::WebTransformOperations operations1; |
| 41 operations1.appendTranslate(2, 0, 0); |
| 42 WebKit::WebTransformOperations operations2; |
| 43 operations2.appendTranslate(4, 0, 0); |
| 44 curve->add(WebTransformKeyframe(0, operations1), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 45 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 46 EXPECT_FLOAT_EQ(2, curve->getValue(-1).m41()); |
| 47 EXPECT_FLOAT_EQ(2, curve->getValue(0).m41()); |
| 48 EXPECT_FLOAT_EQ(3, curve->getValue(0.5).m41()); |
| 49 EXPECT_FLOAT_EQ(4, curve->getValue(1).m41()); |
| 50 EXPECT_FLOAT_EQ(4, curve->getValue(2).m41()); |
| 51 } |
| 52 |
| 53 // Tests that a transform animation with three keyframes works as expected. |
| 54 TEST(WebTransformAnimationCurveTest, ThreeTransformKeyframe) |
| 55 { |
| 56 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 57 WebKit::WebTransformOperations operations1; |
| 58 operations1.appendTranslate(2, 0, 0); |
| 59 WebKit::WebTransformOperations operations2; |
| 60 operations2.appendTranslate(4, 0, 0); |
| 61 WebKit::WebTransformOperations operations3; |
| 62 operations3.appendTranslate(8, 0, 0); |
| 63 curve->add(WebTransformKeyframe(0, operations1), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 64 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 65 curve->add(WebTransformKeyframe(2, operations3), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 66 EXPECT_FLOAT_EQ(2, curve->getValue(-1).m41()); |
| 67 EXPECT_FLOAT_EQ(2, curve->getValue(0).m41()); |
| 68 EXPECT_FLOAT_EQ(3, curve->getValue(0.5).m41()); |
| 69 EXPECT_FLOAT_EQ(4, curve->getValue(1).m41()); |
| 70 EXPECT_FLOAT_EQ(6, curve->getValue(1.5).m41()); |
| 71 EXPECT_FLOAT_EQ(8, curve->getValue(2).m41()); |
| 72 EXPECT_FLOAT_EQ(8, curve->getValue(3).m41()); |
| 73 } |
| 74 |
| 75 // Tests that a transform animation with multiple keys at a given time works san
ely. |
| 76 TEST(WebTransformAnimationCurveTest, RepeatedTransformKeyTimes) |
| 77 { |
| 78 // A step function. |
| 79 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 80 WebKit::WebTransformOperations operations1; |
| 81 operations1.appendTranslate(4, 0, 0); |
| 82 WebKit::WebTransformOperations operations2; |
| 83 operations2.appendTranslate(4, 0, 0); |
| 84 WebKit::WebTransformOperations operations3; |
| 85 operations3.appendTranslate(6, 0, 0); |
| 86 WebKit::WebTransformOperations operations4; |
| 87 operations4.appendTranslate(6, 0, 0); |
| 88 curve->add(WebTransformKeyframe(0, operations1), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 89 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 90 curve->add(WebTransformKeyframe(1, operations3), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 91 curve->add(WebTransformKeyframe(2, operations4), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 92 |
| 93 EXPECT_FLOAT_EQ(4, curve->getValue(-1).m41()); |
| 94 EXPECT_FLOAT_EQ(4, curve->getValue(0).m41()); |
| 95 EXPECT_FLOAT_EQ(4, curve->getValue(0.5).m41()); |
| 96 |
| 97 // There is a discontinuity at 1. Any value between 4 and 6 is valid. |
| 98 WebTransformationMatrix value = curve->getValue(1); |
| 99 EXPECT_TRUE(value.m41() >= 4 && value.m41() <= 6); |
| 100 |
| 101 EXPECT_FLOAT_EQ(6, curve->getValue(1.5).m41()); |
| 102 EXPECT_FLOAT_EQ(6, curve->getValue(2).m41()); |
| 103 EXPECT_FLOAT_EQ(6, curve->getValue(3).m41()); |
| 104 } |
| 105 |
| 106 // Tests that the keyframes may be added out of order. |
| 107 TEST(WebTransformAnimationCurveTest, UnsortedKeyframes) |
| 108 { |
| 109 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 110 WebKit::WebTransformOperations operations1; |
| 111 operations1.appendTranslate(2, 0, 0); |
| 112 WebKit::WebTransformOperations operations2; |
| 113 operations2.appendTranslate(4, 0, 0); |
| 114 WebKit::WebTransformOperations operations3; |
| 115 operations3.appendTranslate(8, 0, 0); |
| 116 curve->add(WebTransformKeyframe(2, operations3), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 117 curve->add(WebTransformKeyframe(0, operations1), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 118 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 119 |
| 120 EXPECT_FLOAT_EQ(2, curve->getValue(-1).m41()); |
| 121 EXPECT_FLOAT_EQ(2, curve->getValue(0).m41()); |
| 122 EXPECT_FLOAT_EQ(3, curve->getValue(0.5).m41()); |
| 123 EXPECT_FLOAT_EQ(4, curve->getValue(1).m41()); |
| 124 EXPECT_FLOAT_EQ(6, curve->getValue(1.5).m41()); |
| 125 EXPECT_FLOAT_EQ(8, curve->getValue(2).m41()); |
| 126 EXPECT_FLOAT_EQ(8, curve->getValue(3).m41()); |
| 127 } |
| 128 |
| 129 // Tests that a cubic bezier timing function works as expected. |
| 130 TEST(WebTransformAnimationCurveTest, CubicBezierTimingFunction) |
| 131 { |
| 132 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 133 WebKit::WebTransformOperations operations1; |
| 134 operations1.appendTranslate(0, 0, 0); |
| 135 WebKit::WebTransformOperations operations2; |
| 136 operations2.appendTranslate(1, 0, 0); |
| 137 curve->add(WebTransformKeyframe(0, operations1), 0.25, 0, 0.75, 1); |
| 138 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 139 EXPECT_FLOAT_EQ(0, curve->getValue(0).m41()); |
| 140 EXPECT_LT(0, curve->getValue(0.25).m41()); |
| 141 EXPECT_GT(0.25, curve->getValue(0.25).m41()); |
| 142 EXPECT_FLOAT_EQ(0.5, curve->getValue(0.5).m41()); |
| 143 EXPECT_LT(0.75, curve->getValue(0.75).m41()); |
| 144 EXPECT_GT(1, curve->getValue(0.75).m41()); |
| 145 EXPECT_FLOAT_EQ(1, curve->getValue(1).m41()); |
| 146 } |
| 147 |
| 148 // Tests that an ease timing function works as expected. |
| 149 TEST(WebTransformAnimationCurveTest, EaseTimingFunction) |
| 150 { |
| 151 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 152 WebKit::WebTransformOperations operations1; |
| 153 operations1.appendTranslate(0, 0, 0); |
| 154 WebKit::WebTransformOperations operations2; |
| 155 operations2.appendTranslate(1, 0, 0); |
| 156 curve->add(WebTransformKeyframe(0, operations1), WebAnimationCurve::TimingFu
nctionTypeEase); |
| 157 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 158 |
| 159 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseTimingFuncti
on::create()); |
| 160 for (int i = 0; i <= 4; ++i) { |
| 161 const double time = i * 0.25; |
| 162 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time).m4
1()); |
| 163 } |
| 164 } |
| 165 |
| 166 // Tests using a linear timing function. |
| 167 TEST(WebTransformAnimationCurveTest, LinearTimingFunction) |
| 168 { |
| 169 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 170 WebKit::WebTransformOperations operations1; |
| 171 operations1.appendTranslate(0, 0, 0); |
| 172 WebKit::WebTransformOperations operations2; |
| 173 operations2.appendTranslate(1, 0, 0); |
| 174 curve->add(WebTransformKeyframe(0, operations1), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 175 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 176 |
| 177 for (int i = 0; i <= 4; ++i) { |
| 178 const double time = i * 0.25; |
| 179 EXPECT_FLOAT_EQ(time, curve->getValue(time).m41()); |
| 180 } |
| 181 } |
| 182 |
| 183 // Tests that an ease in timing function works as expected. |
| 184 TEST(WebTransformAnimationCurveTest, EaseInTimingFunction) |
| 185 { |
| 186 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 187 WebKit::WebTransformOperations operations1; |
| 188 operations1.appendTranslate(0, 0, 0); |
| 189 WebKit::WebTransformOperations operations2; |
| 190 operations2.appendTranslate(1, 0, 0); |
| 191 curve->add(WebTransformKeyframe(0, operations1), WebAnimationCurve::TimingFu
nctionTypeEaseIn); |
| 192 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 193 |
| 194 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseInTimingFunc
tion::create()); |
| 195 for (int i = 0; i <= 4; ++i) { |
| 196 const double time = i * 0.25; |
| 197 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time).m4
1()); |
| 198 } |
| 199 } |
| 200 |
| 201 // Tests that an ease in timing function works as expected. |
| 202 TEST(WebTransformAnimationCurveTest, EaseOutTimingFunction) |
| 203 { |
| 204 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 205 WebKit::WebTransformOperations operations1; |
| 206 operations1.appendTranslate(0, 0, 0); |
| 207 WebKit::WebTransformOperations operations2; |
| 208 operations2.appendTranslate(1, 0, 0); |
| 209 curve->add(WebTransformKeyframe(0, operations1), WebAnimationCurve::TimingFu
nctionTypeEaseOut); |
| 210 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 211 |
| 212 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseOutTimingFun
ction::create()); |
| 213 for (int i = 0; i <= 4; ++i) { |
| 214 const double time = i * 0.25; |
| 215 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time).m4
1()); |
| 216 } |
| 217 } |
| 218 |
| 219 // Tests that an ease in timing function works as expected. |
| 220 TEST(WebTransformAnimationCurveTest, EaseInOutTimingFunction) |
| 221 { |
| 222 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 223 WebKit::WebTransformOperations operations1; |
| 224 operations1.appendTranslate(0, 0, 0); |
| 225 WebKit::WebTransformOperations operations2; |
| 226 operations2.appendTranslate(1, 0, 0); |
| 227 curve->add(WebTransformKeyframe(0, operations1), WebAnimationCurve::TimingFu
nctionTypeEaseInOut); |
| 228 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 229 |
| 230 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseInOutTimingF
unction::create()); |
| 231 for (int i = 0; i <= 4; ++i) { |
| 232 const double time = i * 0.25; |
| 233 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time).m4
1()); |
| 234 } |
| 235 } |
| 236 |
| 237 // Tests that an ease in timing function works as expected. |
| 238 TEST(WebTransformAnimationCurveTest, CustomBezierTimingFunction) |
| 239 { |
| 240 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 241 double x1 = 0.3; |
| 242 double y1 = 0.2; |
| 243 double x2 = 0.8; |
| 244 double y2 = 0.7; |
| 245 WebKit::WebTransformOperations operations1; |
| 246 operations1.appendTranslate(0, 0, 0); |
| 247 WebKit::WebTransformOperations operations2; |
| 248 operations2.appendTranslate(1, 0, 0); |
| 249 curve->add(WebTransformKeyframe(0, operations1), x1, y1, x2, y2); |
| 250 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 251 |
| 252 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCCubicBezierTimin
gFunction::create(x1, y1, x2, y2)); |
| 253 for (int i = 0; i <= 4; ++i) { |
| 254 const double time = i * 0.25; |
| 255 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time).m4
1()); |
| 256 } |
| 257 } |
| 258 |
| 259 // Tests that the default timing function is indeed ease. |
| 260 TEST(WebTransformAnimationCurveTest, DefaultTimingFunction) |
| 261 { |
| 262 OwnPtr<WebTransformAnimationCurve> curve = adoptPtr(WebTransformAnimationCur
ve::create()); |
| 263 WebKit::WebTransformOperations operations1; |
| 264 operations1.appendTranslate(0, 0, 0); |
| 265 WebKit::WebTransformOperations operations2; |
| 266 operations2.appendTranslate(1, 0, 0); |
| 267 curve->add(WebTransformKeyframe(0, operations1)); |
| 268 curve->add(WebTransformKeyframe(1, operations2), WebAnimationCurve::TimingFu
nctionTypeLinear); |
| 269 |
| 270 OwnPtr<WebCore::CCTimingFunction> timingFunction(WebCore::CCEaseTimingFuncti
on::create()); |
| 271 for (int i = 0; i <= 4; ++i) { |
| 272 const double time = i * 0.25; |
| 273 EXPECT_FLOAT_EQ(timingFunction->getValue(time), curve->getValue(time).m4
1()); |
| 274 } |
| 275 } |
| 276 |
| 277 } // namespace |
OLD | NEW |