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