| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/animation/keyframed_animation_curve.h" | 5 #include "cc/animation/keyframed_animation_curve.h" |
| 6 | 6 |
| 7 #include "cc/animation/transform_operations.h" | 7 #include "cc/animation/transform_operations.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace cc { | 11 namespace cc { |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 void expectTranslateX(double translateX, const gfx::Transform& transform) | 14 static void ExpectTranslateX(double translate_x, |
| 15 const gfx::Transform& transform) |
| 15 { | 16 { |
| 16 EXPECT_FLOAT_EQ(translateX, transform.matrix().getDouble(0, 3)); | 17 EXPECT_FLOAT_EQ(translate_x, transform.matrix().getDouble(0, 3)); |
| 17 } | 18 } |
| 18 | 19 |
| 19 // Tests that a float animation with one keyframe works as expected. | 20 // Tests that a float animation with one keyframe works as expected. |
| 20 TEST(KeyframedAnimationCurveTest, OneFloatKeyframe) | 21 TEST(KeyframedAnimationCurveTest, OneFloatKeyframe) |
| 21 { | 22 { |
| 22 scoped_ptr<KeyframedFloatAnimationCurve> curve( | 23 scoped_ptr<KeyframedFloatAnimationCurve> curve( |
| 23 KeyframedFloatAnimationCurve::Create()); | 24 KeyframedFloatAnimationCurve::Create()); |
| 24 curve->AddKeyframe( | 25 curve->AddKeyframe( |
| 25 FloatKeyframe::Create(0.0, 2.f, scoped_ptr<TimingFunction>())); | 26 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(-1.f)); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // Tests that a transform animation with one keyframe works as expected. | 98 // Tests that a transform animation with one keyframe works as expected. |
| 98 TEST(KeyframedAnimationCurveTest, OneTransformKeyframe) | 99 TEST(KeyframedAnimationCurveTest, OneTransformKeyframe) |
| 99 { | 100 { |
| 100 scoped_ptr<KeyframedTransformAnimationCurve> curve( | 101 scoped_ptr<KeyframedTransformAnimationCurve> curve( |
| 101 KeyframedTransformAnimationCurve::Create()); | 102 KeyframedTransformAnimationCurve::Create()); |
| 102 TransformOperations operations; | 103 TransformOperations operations; |
| 103 operations.AppendTranslate(2.f, 0.f, 0.f); | 104 operations.AppendTranslate(2.f, 0.f, 0.f); |
| 104 curve->AddKeyframe( | 105 curve->AddKeyframe( |
| 105 TransformKeyframe::Create(0.f, operations, scoped_ptr<TimingFunction>())); | 106 TransformKeyframe::Create(0.f, operations, scoped_ptr<TimingFunction>())); |
| 106 | 107 |
| 107 expectTranslateX(2.f, curve->GetValue(-1.f)); | 108 ExpectTranslateX(2.f, curve->GetValue(-1.f)); |
| 108 expectTranslateX(2.f, curve->GetValue(0.f)); | 109 ExpectTranslateX(2.f, curve->GetValue(0.f)); |
| 109 expectTranslateX(2.f, curve->GetValue(0.5f)); | 110 ExpectTranslateX(2.f, curve->GetValue(0.5f)); |
| 110 expectTranslateX(2.f, curve->GetValue(1.f)); | 111 ExpectTranslateX(2.f, curve->GetValue(1.f)); |
| 111 expectTranslateX(2.f, curve->GetValue(2.f)); | 112 ExpectTranslateX(2.f, curve->GetValue(2.f)); |
| 112 } | 113 } |
| 113 | 114 |
| 114 // Tests that a transform animation with two keyframes works as expected. | 115 // Tests that a transform animation with two keyframes works as expected. |
| 115 TEST(KeyframedAnimationCurveTest, TwoTransformKeyframe) | 116 TEST(KeyframedAnimationCurveTest, TwoTransformKeyframe) |
| 116 { | 117 { |
| 117 scoped_ptr<KeyframedTransformAnimationCurve> curve( | 118 scoped_ptr<KeyframedTransformAnimationCurve> curve( |
| 118 KeyframedTransformAnimationCurve::Create()); | 119 KeyframedTransformAnimationCurve::Create()); |
| 119 TransformOperations operations1; | 120 TransformOperations operations1; |
| 120 operations1.AppendTranslate(2.f, 0.f, 0.f); | 121 operations1.AppendTranslate(2.f, 0.f, 0.f); |
| 121 TransformOperations operations2; | 122 TransformOperations operations2; |
| 122 operations2.AppendTranslate(4.f, 0.f, 0.f); | 123 operations2.AppendTranslate(4.f, 0.f, 0.f); |
| 123 | 124 |
| 124 curve->AddKeyframe(TransformKeyframe::Create( | 125 curve->AddKeyframe(TransformKeyframe::Create( |
| 125 0.f, operations1, scoped_ptr<TimingFunction>())); | 126 0.f, operations1, scoped_ptr<TimingFunction>())); |
| 126 curve->AddKeyframe(TransformKeyframe::Create( | 127 curve->AddKeyframe(TransformKeyframe::Create( |
| 127 1.f, operations2, scoped_ptr<TimingFunction>())); | 128 1.f, operations2, scoped_ptr<TimingFunction>())); |
| 128 expectTranslateX(2.f, curve->GetValue(-1.f)); | 129 ExpectTranslateX(2.f, curve->GetValue(-1.f)); |
| 129 expectTranslateX(2.f, curve->GetValue(0.f)); | 130 ExpectTranslateX(2.f, curve->GetValue(0.f)); |
| 130 expectTranslateX(3.f, curve->GetValue(0.5f)); | 131 ExpectTranslateX(3.f, curve->GetValue(0.5f)); |
| 131 expectTranslateX(4.f, curve->GetValue(1.f)); | 132 ExpectTranslateX(4.f, curve->GetValue(1.f)); |
| 132 expectTranslateX(4.f, curve->GetValue(2.f)); | 133 ExpectTranslateX(4.f, curve->GetValue(2.f)); |
| 133 } | 134 } |
| 134 | 135 |
| 135 // Tests that a transform animation with three keyframes works as expected. | 136 // Tests that a transform animation with three keyframes works as expected. |
| 136 TEST(KeyframedAnimationCurveTest, ThreeTransformKeyframe) | 137 TEST(KeyframedAnimationCurveTest, ThreeTransformKeyframe) |
| 137 { | 138 { |
| 138 scoped_ptr<KeyframedTransformAnimationCurve> curve( | 139 scoped_ptr<KeyframedTransformAnimationCurve> curve( |
| 139 KeyframedTransformAnimationCurve::Create()); | 140 KeyframedTransformAnimationCurve::Create()); |
| 140 TransformOperations operations1; | 141 TransformOperations operations1; |
| 141 operations1.AppendTranslate(2.f, 0.f, 0.f); | 142 operations1.AppendTranslate(2.f, 0.f, 0.f); |
| 142 TransformOperations operations2; | 143 TransformOperations operations2; |
| 143 operations2.AppendTranslate(4.f, 0.f, 0.f); | 144 operations2.AppendTranslate(4.f, 0.f, 0.f); |
| 144 TransformOperations operations3; | 145 TransformOperations operations3; |
| 145 operations3.AppendTranslate(8.f, 0.f, 0.f); | 146 operations3.AppendTranslate(8.f, 0.f, 0.f); |
| 146 curve->AddKeyframe(TransformKeyframe::Create( | 147 curve->AddKeyframe(TransformKeyframe::Create( |
| 147 0.f, operations1, scoped_ptr<TimingFunction>())); | 148 0.f, operations1, scoped_ptr<TimingFunction>())); |
| 148 curve->AddKeyframe(TransformKeyframe::Create( | 149 curve->AddKeyframe(TransformKeyframe::Create( |
| 149 1.f, operations2, scoped_ptr<TimingFunction>())); | 150 1.f, operations2, scoped_ptr<TimingFunction>())); |
| 150 curve->AddKeyframe(TransformKeyframe::Create( | 151 curve->AddKeyframe(TransformKeyframe::Create( |
| 151 2.f, operations3, scoped_ptr<TimingFunction>())); | 152 2.f, operations3, scoped_ptr<TimingFunction>())); |
| 152 expectTranslateX(2.f, curve->GetValue(-1.f)); | 153 ExpectTranslateX(2.f, curve->GetValue(-1.f)); |
| 153 expectTranslateX(2.f, curve->GetValue(0.f)); | 154 ExpectTranslateX(2.f, curve->GetValue(0.f)); |
| 154 expectTranslateX(3.f, curve->GetValue(0.5f)); | 155 ExpectTranslateX(3.f, curve->GetValue(0.5f)); |
| 155 expectTranslateX(4.f, curve->GetValue(1.f)); | 156 ExpectTranslateX(4.f, curve->GetValue(1.f)); |
| 156 expectTranslateX(6.f, curve->GetValue(1.5f)); | 157 ExpectTranslateX(6.f, curve->GetValue(1.5f)); |
| 157 expectTranslateX(8.f, curve->GetValue(2.f)); | 158 ExpectTranslateX(8.f, curve->GetValue(2.f)); |
| 158 expectTranslateX(8.f, curve->GetValue(3.f)); | 159 ExpectTranslateX(8.f, curve->GetValue(3.f)); |
| 159 } | 160 } |
| 160 | 161 |
| 161 // Tests that a transform animation with multiple keys at a given time works | 162 // Tests that a transform animation with multiple keys at a given time works |
| 162 // sanely. | 163 // sanely. |
| 163 TEST(KeyframedAnimationCurveTest, RepeatedTransformKeyTimes) | 164 TEST(KeyframedAnimationCurveTest, RepeatedTransformKeyTimes) |
| 164 { | 165 { |
| 165 scoped_ptr<KeyframedTransformAnimationCurve> curve( | 166 scoped_ptr<KeyframedTransformAnimationCurve> curve( |
| 166 KeyframedTransformAnimationCurve::Create()); | 167 KeyframedTransformAnimationCurve::Create()); |
| 167 // A step function. | 168 // A step function. |
| 168 TransformOperations operations1; | 169 TransformOperations operations1; |
| 169 operations1.AppendTranslate(4.f, 0.f, 0.f); | 170 operations1.AppendTranslate(4.f, 0.f, 0.f); |
| 170 TransformOperations operations2; | 171 TransformOperations operations2; |
| 171 operations2.AppendTranslate(4.f, 0.f, 0.f); | 172 operations2.AppendTranslate(4.f, 0.f, 0.f); |
| 172 TransformOperations operations3; | 173 TransformOperations operations3; |
| 173 operations3.AppendTranslate(6.f, 0.f, 0.f); | 174 operations3.AppendTranslate(6.f, 0.f, 0.f); |
| 174 TransformOperations operations4; | 175 TransformOperations operations4; |
| 175 operations4.AppendTranslate(6.f, 0.f, 0.f); | 176 operations4.AppendTranslate(6.f, 0.f, 0.f); |
| 176 curve->AddKeyframe(TransformKeyframe::Create( | 177 curve->AddKeyframe(TransformKeyframe::Create( |
| 177 0.f, operations1, scoped_ptr<TimingFunction>())); | 178 0.f, operations1, scoped_ptr<TimingFunction>())); |
| 178 curve->AddKeyframe(TransformKeyframe::Create( | 179 curve->AddKeyframe(TransformKeyframe::Create( |
| 179 1.f, operations2, scoped_ptr<TimingFunction>())); | 180 1.f, operations2, scoped_ptr<TimingFunction>())); |
| 180 curve->AddKeyframe(TransformKeyframe::Create( | 181 curve->AddKeyframe(TransformKeyframe::Create( |
| 181 1.f, operations3, scoped_ptr<TimingFunction>())); | 182 1.f, operations3, scoped_ptr<TimingFunction>())); |
| 182 curve->AddKeyframe(TransformKeyframe::Create( | 183 curve->AddKeyframe(TransformKeyframe::Create( |
| 183 2.f, operations4, scoped_ptr<TimingFunction>())); | 184 2.f, operations4, scoped_ptr<TimingFunction>())); |
| 184 | 185 |
| 185 expectTranslateX(4.f, curve->GetValue(-1.f)); | 186 ExpectTranslateX(4.f, curve->GetValue(-1.f)); |
| 186 expectTranslateX(4.f, curve->GetValue(0.f)); | 187 ExpectTranslateX(4.f, curve->GetValue(0.f)); |
| 187 expectTranslateX(4.f, curve->GetValue(0.5f)); | 188 ExpectTranslateX(4.f, curve->GetValue(0.5f)); |
| 188 | 189 |
| 189 // There is a discontinuity at 1. Any value between 4 and 6 is valid. | 190 // There is a discontinuity at 1. Any value between 4 and 6 is valid. |
| 190 gfx::Transform value = curve->GetValue(1.f); | 191 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) >= 4); |
| 192 EXPECT_TRUE(value.matrix().getDouble(0.f, 3.f) <= 6); | 193 EXPECT_TRUE(value.matrix().getDouble(0.f, 3.f) <= 6); |
| 193 | 194 |
| 194 expectTranslateX(6.f, curve->GetValue(1.5f)); | 195 ExpectTranslateX(6.f, curve->GetValue(1.5f)); |
| 195 expectTranslateX(6.f, curve->GetValue(2.f)); | 196 ExpectTranslateX(6.f, curve->GetValue(2.f)); |
| 196 expectTranslateX(6.f, curve->GetValue(3.f)); | 197 ExpectTranslateX(6.f, curve->GetValue(3.f)); |
| 197 } | 198 } |
| 198 | 199 |
| 199 // Tests that the keyframes may be added out of order. | 200 // Tests that the keyframes may be added out of order. |
| 200 TEST(KeyframedAnimationCurveTest, UnsortedKeyframes) | 201 TEST(KeyframedAnimationCurveTest, UnsortedKeyframes) |
| 201 { | 202 { |
| 202 scoped_ptr<KeyframedFloatAnimationCurve> curve( | 203 scoped_ptr<KeyframedFloatAnimationCurve> curve( |
| 203 KeyframedFloatAnimationCurve::Create()); | 204 KeyframedFloatAnimationCurve::Create()); |
| 204 curve->AddKeyframe( | 205 curve->AddKeyframe( |
| 205 FloatKeyframe::Create(2.0, 8.f, scoped_ptr<TimingFunction>())); | 206 FloatKeyframe::Create(2.0, 8.f, scoped_ptr<TimingFunction>())); |
| 206 curve->AddKeyframe( | 207 curve->AddKeyframe( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 234 EXPECT_LT(0.f, curve->GetValue(0.25f)); | 235 EXPECT_LT(0.f, curve->GetValue(0.25f)); |
| 235 EXPECT_GT(0.25f, curve->GetValue(0.25f)); | 236 EXPECT_GT(0.25f, curve->GetValue(0.25f)); |
| 236 EXPECT_NEAR(curve->GetValue(0.5f), 0.5f, 0.00015f); | 237 EXPECT_NEAR(curve->GetValue(0.5f), 0.5f, 0.00015f); |
| 237 EXPECT_LT(0.75f, curve->GetValue(0.75f)); | 238 EXPECT_LT(0.75f, curve->GetValue(0.75f)); |
| 238 EXPECT_GT(1.f, curve->GetValue(0.75f)); | 239 EXPECT_GT(1.f, curve->GetValue(0.75f)); |
| 239 EXPECT_FLOAT_EQ(1.f, curve->GetValue(1.f)); | 240 EXPECT_FLOAT_EQ(1.f, curve->GetValue(1.f)); |
| 240 } | 241 } |
| 241 | 242 |
| 242 } // namespace | 243 } // namespace |
| 243 } // namespace cc | 244 } // namespace cc |
| OLD | NEW |