| 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 "cc/keyframed_animation_curve.h" | |
| 6 | |
| 7 #include "cc/transform_operations.h" | |
| 8 #include "testing/gmock/include/gmock/gmock.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace cc { | |
| 12 namespace { | |
| 13 | |
| 14 void expectTranslateX(double translateX, const gfx::Transform& transform) | |
| 15 { | |
| 16 EXPECT_FLOAT_EQ(translateX, transform.matrix().getDouble(0, 3)); | |
| 17 } | |
| 18 | |
| 19 // Tests that a float animation with one keyframe works as expected. | |
| 20 TEST(KeyframedAnimationCurveTest, OneFloatKeyframe) | |
| 21 { | |
| 22 scoped_ptr<KeyframedFloatAnimationCurve> curve( | |
| 23 KeyframedFloatAnimationCurve::Create()); | |
| 24 curve->AddKeyframe( | |
| 25 FloatKeyframe::Create(0.0, 2.f, scoped_ptr<TimingFunction>())); | |
| 26 EXPECT_FLOAT_EQ(2.f, curve->GetValue(-1.f)); | |
| 27 EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.f)); | |
| 28 EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.5f)); | |
| 29 EXPECT_FLOAT_EQ(2.f, curve->GetValue(1.f)); | |
| 30 EXPECT_FLOAT_EQ(2.f, curve->GetValue(2.f)); | |
| 31 } | |
| 32 | |
| 33 // Tests that a float animation with two keyframes works as expected. | |
| 34 TEST(KeyframedAnimationCurveTest, TwoFloatKeyframe) | |
| 35 { | |
| 36 scoped_ptr<KeyframedFloatAnimationCurve> curve( | |
| 37 KeyframedFloatAnimationCurve::Create()); | |
| 38 curve->AddKeyframe( | |
| 39 FloatKeyframe::Create(0.0, 2.f, scoped_ptr<TimingFunction>())); | |
| 40 curve->AddKeyframe( | |
| 41 FloatKeyframe::Create(1.0, 4.f, scoped_ptr<TimingFunction>())); | |
| 42 EXPECT_FLOAT_EQ(2.f, curve->GetValue(-1.f)); | |
| 43 EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.f)); | |
| 44 EXPECT_FLOAT_EQ(3.f, curve->GetValue(0.5f)); | |
| 45 EXPECT_FLOAT_EQ(4.f, curve->GetValue(1.f)); | |
| 46 EXPECT_FLOAT_EQ(4.f, curve->GetValue(2.f)); | |
| 47 } | |
| 48 | |
| 49 // Tests that a float animation with three keyframes works as expected. | |
| 50 TEST(KeyframedAnimationCurveTest, ThreeFloatKeyframe) | |
| 51 { | |
| 52 scoped_ptr<KeyframedFloatAnimationCurve> curve( | |
| 53 KeyframedFloatAnimationCurve::Create()); | |
| 54 curve->AddKeyframe( | |
| 55 FloatKeyframe::Create(0.0, 2.f, scoped_ptr<TimingFunction>())); | |
| 56 curve->AddKeyframe( | |
| 57 FloatKeyframe::Create(1.0, 4.f, scoped_ptr<TimingFunction>())); | |
| 58 curve->AddKeyframe( | |
| 59 FloatKeyframe::Create(2.0, 8.f, scoped_ptr<TimingFunction>())); | |
| 60 EXPECT_FLOAT_EQ(2.f, curve->GetValue(-1.f)); | |
| 61 EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.f)); | |
| 62 EXPECT_FLOAT_EQ(3.f, curve->GetValue(0.5f)); | |
| 63 EXPECT_FLOAT_EQ(4.f, curve->GetValue(1.f)); | |
| 64 EXPECT_FLOAT_EQ(6.f, curve->GetValue(1.5f)); | |
| 65 EXPECT_FLOAT_EQ(8.f, curve->GetValue(2.f)); | |
| 66 EXPECT_FLOAT_EQ(8.f, curve->GetValue(3.f)); | |
| 67 } | |
| 68 | |
| 69 // Tests that a float animation with multiple keys at a given time works sanely. | |
| 70 TEST(KeyframedAnimationCurveTest, RepeatedFloatKeyTimes) | |
| 71 { | |
| 72 scoped_ptr<KeyframedFloatAnimationCurve> curve( | |
| 73 KeyframedFloatAnimationCurve::Create()); | |
| 74 curve->AddKeyframe( | |
| 75 FloatKeyframe::Create(0.0, 4.f, scoped_ptr<TimingFunction>())); | |
| 76 curve->AddKeyframe( | |
| 77 FloatKeyframe::Create(1.0, 4.f, scoped_ptr<TimingFunction>())); | |
| 78 curve->AddKeyframe( | |
| 79 FloatKeyframe::Create(1.0, 6.f, scoped_ptr<TimingFunction>())); | |
| 80 curve->AddKeyframe( | |
| 81 FloatKeyframe::Create(2.0, 6.f, scoped_ptr<TimingFunction>())); | |
| 82 | |
| 83 EXPECT_FLOAT_EQ(4.f, curve->GetValue(-1.f)); | |
| 84 EXPECT_FLOAT_EQ(4.f, curve->GetValue(0.f)); | |
| 85 EXPECT_FLOAT_EQ(4.f, curve->GetValue(0.5f)); | |
| 86 | |
| 87 // There is a discontinuity at 1. Any value between 4 and 6 is valid. | |
| 88 float value = curve->GetValue(1.f); | |
| 89 EXPECT_TRUE(value >= 4 && value <= 6); | |
| 90 | |
| 91 EXPECT_FLOAT_EQ(6.f, curve->GetValue(1.5f)); | |
| 92 EXPECT_FLOAT_EQ(6.f, curve->GetValue(2.f)); | |
| 93 EXPECT_FLOAT_EQ(6.f, curve->GetValue(3.f)); | |
| 94 } | |
| 95 | |
| 96 | |
| 97 // Tests that a transform animation with one keyframe works as expected. | |
| 98 TEST(KeyframedAnimationCurveTest, OneTransformKeyframe) | |
| 99 { | |
| 100 scoped_ptr<KeyframedTransformAnimationCurve> curve( | |
| 101 KeyframedTransformAnimationCurve::Create()); | |
| 102 TransformOperations operations; | |
| 103 operations.AppendTranslate(2.f, 0.f, 0.f); | |
| 104 curve->AddKeyframe( | |
| 105 TransformKeyframe::Create(0.f, operations, scoped_ptr<TimingFunction>())); | |
| 106 | |
| 107 expectTranslateX(2.f, curve->GetValue(-1.f)); | |
| 108 expectTranslateX(2.f, curve->GetValue(0.f)); | |
| 109 expectTranslateX(2.f, curve->GetValue(0.5f)); | |
| 110 expectTranslateX(2.f, curve->GetValue(1.f)); | |
| 111 expectTranslateX(2.f, curve->GetValue(2.f)); | |
| 112 } | |
| 113 | |
| 114 // Tests that a transform animation with two keyframes works as expected. | |
| 115 TEST(KeyframedAnimationCurveTest, TwoTransformKeyframe) | |
| 116 { | |
| 117 scoped_ptr<KeyframedTransformAnimationCurve> curve( | |
| 118 KeyframedTransformAnimationCurve::Create()); | |
| 119 TransformOperations operations1; | |
| 120 operations1.AppendTranslate(2.f, 0.f, 0.f); | |
| 121 TransformOperations operations2; | |
| 122 operations2.AppendTranslate(4.f, 0.f, 0.f); | |
| 123 | |
| 124 curve->AddKeyframe(TransformKeyframe::Create( | |
| 125 0.f, operations1, scoped_ptr<TimingFunction>())); | |
| 126 curve->AddKeyframe(TransformKeyframe::Create( | |
| 127 1.f, operations2, scoped_ptr<TimingFunction>())); | |
| 128 expectTranslateX(2.f, curve->GetValue(-1.f)); | |
| 129 expectTranslateX(2.f, curve->GetValue(0.f)); | |
| 130 expectTranslateX(3.f, curve->GetValue(0.5f)); | |
| 131 expectTranslateX(4.f, curve->GetValue(1.f)); | |
| 132 expectTranslateX(4.f, curve->GetValue(2.f)); | |
| 133 } | |
| 134 | |
| 135 // Tests that a transform animation with three keyframes works as expected. | |
| 136 TEST(KeyframedAnimationCurveTest, ThreeTransformKeyframe) | |
| 137 { | |
| 138 scoped_ptr<KeyframedTransformAnimationCurve> curve( | |
| 139 KeyframedTransformAnimationCurve::Create()); | |
| 140 TransformOperations operations1; | |
| 141 operations1.AppendTranslate(2.f, 0.f, 0.f); | |
| 142 TransformOperations operations2; | |
| 143 operations2.AppendTranslate(4.f, 0.f, 0.f); | |
| 144 TransformOperations operations3; | |
| 145 operations3.AppendTranslate(8.f, 0.f, 0.f); | |
| 146 curve->AddKeyframe(TransformKeyframe::Create( | |
| 147 0.f, operations1, scoped_ptr<TimingFunction>())); | |
| 148 curve->AddKeyframe(TransformKeyframe::Create( | |
| 149 1.f, operations2, scoped_ptr<TimingFunction>())); | |
| 150 curve->AddKeyframe(TransformKeyframe::Create( | |
| 151 2.f, operations3, scoped_ptr<TimingFunction>())); | |
| 152 expectTranslateX(2.f, curve->GetValue(-1.f)); | |
| 153 expectTranslateX(2.f, curve->GetValue(0.f)); | |
| 154 expectTranslateX(3.f, curve->GetValue(0.5f)); | |
| 155 expectTranslateX(4.f, curve->GetValue(1.f)); | |
| 156 expectTranslateX(6.f, curve->GetValue(1.5f)); | |
| 157 expectTranslateX(8.f, curve->GetValue(2.f)); | |
| 158 expectTranslateX(8.f, curve->GetValue(3.f)); | |
| 159 } | |
| 160 | |
| 161 // Tests that a transform animation with multiple keys at a given time works | |
| 162 // sanely. | |
| 163 TEST(KeyframedAnimationCurveTest, RepeatedTransformKeyTimes) | |
| 164 { | |
| 165 scoped_ptr<KeyframedTransformAnimationCurve> curve( | |
| 166 KeyframedTransformAnimationCurve::Create()); | |
| 167 // A step function. | |
| 168 TransformOperations operations1; | |
| 169 operations1.AppendTranslate(4.f, 0.f, 0.f); | |
| 170 TransformOperations operations2; | |
| 171 operations2.AppendTranslate(4.f, 0.f, 0.f); | |
| 172 TransformOperations operations3; | |
| 173 operations3.AppendTranslate(6.f, 0.f, 0.f); | |
| 174 TransformOperations operations4; | |
| 175 operations4.AppendTranslate(6.f, 0.f, 0.f); | |
| 176 curve->AddKeyframe(TransformKeyframe::Create( | |
| 177 0.f, operations1, scoped_ptr<TimingFunction>())); | |
| 178 curve->AddKeyframe(TransformKeyframe::Create( | |
| 179 1.f, operations2, scoped_ptr<TimingFunction>())); | |
| 180 curve->AddKeyframe(TransformKeyframe::Create( | |
| 181 1.f, operations3, scoped_ptr<TimingFunction>())); | |
| 182 curve->AddKeyframe(TransformKeyframe::Create( | |
| 183 2.f, operations4, scoped_ptr<TimingFunction>())); | |
| 184 | |
| 185 expectTranslateX(4.f, curve->GetValue(-1.f)); | |
| 186 expectTranslateX(4.f, curve->GetValue(0.f)); | |
| 187 expectTranslateX(4.f, curve->GetValue(0.5f)); | |
| 188 | |
| 189 // There is a discontinuity at 1. Any value between 4 and 6 is valid. | |
| 190 gfx::Transform value = curve->GetValue(1.f); | |
| 191 EXPECT_TRUE(value.matrix().getDouble(0.f, 3.f) >= 4); | |
| 192 EXPECT_TRUE(value.matrix().getDouble(0.f, 3.f) <= 6); | |
| 193 | |
| 194 expectTranslateX(6.f, curve->GetValue(1.5f)); | |
| 195 expectTranslateX(6.f, curve->GetValue(2.f)); | |
| 196 expectTranslateX(6.f, curve->GetValue(3.f)); | |
| 197 } | |
| 198 | |
| 199 // Tests that the keyframes may be added out of order. | |
| 200 TEST(KeyframedAnimationCurveTest, UnsortedKeyframes) | |
| 201 { | |
| 202 scoped_ptr<KeyframedFloatAnimationCurve> curve( | |
| 203 KeyframedFloatAnimationCurve::Create()); | |
| 204 curve->AddKeyframe( | |
| 205 FloatKeyframe::Create(2.0, 8.f, scoped_ptr<TimingFunction>())); | |
| 206 curve->AddKeyframe( | |
| 207 FloatKeyframe::Create(0.0, 2.f, scoped_ptr<TimingFunction>())); | |
| 208 curve->AddKeyframe( | |
| 209 FloatKeyframe::Create(1.0, 4.f, scoped_ptr<TimingFunction>())); | |
| 210 EXPECT_FLOAT_EQ(2.f, curve->GetValue(-1.f)); | |
| 211 EXPECT_FLOAT_EQ(2.f, curve->GetValue(0.f)); | |
| 212 EXPECT_FLOAT_EQ(3.f, curve->GetValue(0.5f)); | |
| 213 EXPECT_FLOAT_EQ(4.f, curve->GetValue(1.f)); | |
| 214 EXPECT_FLOAT_EQ(6.f, curve->GetValue(1.5f)); | |
| 215 EXPECT_FLOAT_EQ(8.f, curve->GetValue(2.f)); | |
| 216 EXPECT_FLOAT_EQ(8.f, curve->GetValue(3.f)); | |
| 217 } | |
| 218 | |
| 219 // Tests that a cubic bezier timing function works as expected. | |
| 220 TEST(KeyframedAnimationCurveTest, CubicBezierTimingFunction) | |
| 221 { | |
| 222 scoped_ptr<KeyframedFloatAnimationCurve> curve( | |
| 223 KeyframedFloatAnimationCurve::Create()); | |
| 224 curve->AddKeyframe( | |
| 225 FloatKeyframe::Create( | |
| 226 0.f, | |
| 227 0, | |
| 228 CubicBezierTimingFunction::create( | |
| 229 0.25f, 0.f, 0.75f, 1.f).PassAs<TimingFunction>())); | |
| 230 curve->AddKeyframe( | |
| 231 FloatKeyframe::Create(1.0, 1.f, scoped_ptr<TimingFunction>())); | |
| 232 | |
| 233 EXPECT_FLOAT_EQ(0.f, curve->GetValue(0.f)); | |
| 234 EXPECT_LT(0.f, curve->GetValue(0.25f)); | |
| 235 EXPECT_GT(0.25f, curve->GetValue(0.25f)); | |
| 236 EXPECT_NEAR(curve->GetValue(0.5f), 0.5f, 0.00015f); | |
| 237 EXPECT_LT(0.75f, curve->GetValue(0.75f)); | |
| 238 EXPECT_GT(1.f, curve->GetValue(0.75f)); | |
| 239 EXPECT_FLOAT_EQ(1.f, curve->GetValue(1.f)); | |
| 240 } | |
| 241 | |
| 242 } // namespace | |
| 243 } // namespace cc | |
| OLD | NEW |