| 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 #include "ui/gfx/animation/tween.h" | 10 #include "ui/gfx/animation/tween.h" |
| 11 #include "ui/gfx/geometry/box_f.h" | 11 #include "ui/gfx/geometry/box_f.h" |
| 12 #include "ui/gfx/test/gfx_util.h" | 12 #include "ui/gfx/test/gfx_util.h" |
| 13 | 13 |
| 14 namespace cc { | 14 namespace cc { |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 void ExpectTranslateX(SkMScalar translate_x, const gfx::Transform& transform) { | 17 void ExpectTranslateX(SkMScalar translate_x, |
| 18 EXPECT_FLOAT_EQ(translate_x, transform.matrix().get(0, 3)); | 18 const TransformOperations& operations) { |
| 19 EXPECT_FLOAT_EQ(translate_x, operations.Apply().matrix().get(0, 3)); |
| 19 } | 20 } |
| 20 | 21 |
| 21 void ExpectBrightness(double brightness, const FilterOperations& filter) { | 22 void ExpectBrightness(double brightness, const FilterOperations& filter) { |
| 22 EXPECT_EQ(1u, filter.size()); | 23 EXPECT_EQ(1u, filter.size()); |
| 23 EXPECT_EQ(FilterOperation::BRIGHTNESS, filter.at(0).type()); | 24 EXPECT_EQ(FilterOperation::BRIGHTNESS, filter.at(0).type()); |
| 24 EXPECT_FLOAT_EQ(brightness, filter.at(0).amount()); | 25 EXPECT_FLOAT_EQ(brightness, filter.at(0).amount()); |
| 25 } | 26 } |
| 26 | 27 |
| 27 // Tests that a color animation with one keyframe works as expected. | 28 // Tests that a color animation with one keyframe works as expected. |
| 28 TEST(KeyframedAnimationCurveTest, OneColorKeyFrame) { | 29 TEST(KeyframedAnimationCurveTest, OneColorKeyFrame) { |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 curve->AddKeyframe(TransformKeyframe::Create( | 287 curve->AddKeyframe(TransformKeyframe::Create( |
| 287 base::TimeDelta::FromSecondsD(1.0), operations3, nullptr)); | 288 base::TimeDelta::FromSecondsD(1.0), operations3, nullptr)); |
| 288 curve->AddKeyframe(TransformKeyframe::Create( | 289 curve->AddKeyframe(TransformKeyframe::Create( |
| 289 base::TimeDelta::FromSecondsD(2.0), operations4, nullptr)); | 290 base::TimeDelta::FromSecondsD(2.0), operations4, nullptr)); |
| 290 | 291 |
| 291 ExpectTranslateX(4.f, curve->GetValue(base::TimeDelta::FromSecondsD(-1.f))); | 292 ExpectTranslateX(4.f, curve->GetValue(base::TimeDelta::FromSecondsD(-1.f))); |
| 292 ExpectTranslateX(4.f, curve->GetValue(base::TimeDelta::FromSecondsD(0.f))); | 293 ExpectTranslateX(4.f, curve->GetValue(base::TimeDelta::FromSecondsD(0.f))); |
| 293 ExpectTranslateX(4.f, curve->GetValue(base::TimeDelta::FromSecondsD(0.5f))); | 294 ExpectTranslateX(4.f, curve->GetValue(base::TimeDelta::FromSecondsD(0.5f))); |
| 294 | 295 |
| 295 // There is a discontinuity at 1. Any value between 4 and 6 is valid. | 296 // There is a discontinuity at 1. Any value between 4 and 6 is valid. |
| 296 gfx::Transform value = curve->GetValue(base::TimeDelta::FromSecondsD(1.f)); | 297 gfx::Transform value = |
| 298 curve->GetValue(base::TimeDelta::FromSecondsD(1.f)).Apply(); |
| 297 EXPECT_GE(value.matrix().get(0, 3), 4.f); | 299 EXPECT_GE(value.matrix().get(0, 3), 4.f); |
| 298 EXPECT_LE(value.matrix().get(0, 3), 6.f); | 300 EXPECT_LE(value.matrix().get(0, 3), 6.f); |
| 299 | 301 |
| 300 ExpectTranslateX(6.f, curve->GetValue(base::TimeDelta::FromSecondsD(1.5f))); | 302 ExpectTranslateX(6.f, curve->GetValue(base::TimeDelta::FromSecondsD(1.5f))); |
| 301 ExpectTranslateX(6.f, curve->GetValue(base::TimeDelta::FromSecondsD(2.f))); | 303 ExpectTranslateX(6.f, curve->GetValue(base::TimeDelta::FromSecondsD(2.f))); |
| 302 ExpectTranslateX(6.f, curve->GetValue(base::TimeDelta::FromSecondsD(3.f))); | 304 ExpectTranslateX(6.f, curve->GetValue(base::TimeDelta::FromSecondsD(3.f))); |
| 303 } | 305 } |
| 304 | 306 |
| 305 // Tests that a filter animation with one keyframe works as expected. | 307 // Tests that a filter animation with one keyframe works as expected. |
| 306 TEST(KeyframedAnimationCurveTest, OneFilterKeyframe) { | 308 TEST(KeyframedAnimationCurveTest, OneFilterKeyframe) { |
| (...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 979 curve->GetValue(base::TimeDelta::FromSecondsD(scale * 2.f))); | 981 curve->GetValue(base::TimeDelta::FromSecondsD(scale * 2.f))); |
| 980 EXPECT_NEAR(8.72f, | 982 EXPECT_NEAR(8.72f, |
| 981 curve->GetValue(base::TimeDelta::FromSecondsD(scale * 3.5f)), | 983 curve->GetValue(base::TimeDelta::FromSecondsD(scale * 3.5f)), |
| 982 0.01f); | 984 0.01f); |
| 983 EXPECT_FLOAT_EQ(9.f, | 985 EXPECT_FLOAT_EQ(9.f, |
| 984 curve->GetValue(base::TimeDelta::FromSecondsD(scale * 4.f))); | 986 curve->GetValue(base::TimeDelta::FromSecondsD(scale * 4.f))); |
| 985 EXPECT_FLOAT_EQ(9.f, | 987 EXPECT_FLOAT_EQ(9.f, |
| 986 curve->GetValue(base::TimeDelta::FromSecondsD(scale * 5.f))); | 988 curve->GetValue(base::TimeDelta::FromSecondsD(scale * 5.f))); |
| 987 } | 989 } |
| 988 | 990 |
| 991 // Tests that a color animation with one keyframe works as expected. |
| 992 TEST(KeyframedAnimationCurveTest, OneSizeKeyFrame) { |
| 993 gfx::SizeF size = gfx::SizeF(100, 100); |
| 994 std::unique_ptr<KeyframedSizeAnimationCurve> curve( |
| 995 KeyframedSizeAnimationCurve::Create()); |
| 996 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta(), size, nullptr)); |
| 997 |
| 998 EXPECT_SIZEF_EQ(size, curve->GetValue(base::TimeDelta::FromSecondsD(-1.f))); |
| 999 EXPECT_SIZEF_EQ(size, curve->GetValue(base::TimeDelta::FromSecondsD(0.f))); |
| 1000 EXPECT_SIZEF_EQ(size, curve->GetValue(base::TimeDelta::FromSecondsD(0.5f))); |
| 1001 EXPECT_SIZEF_EQ(size, curve->GetValue(base::TimeDelta::FromSecondsD(1.f))); |
| 1002 EXPECT_SIZEF_EQ(size, curve->GetValue(base::TimeDelta::FromSecondsD(2.f))); |
| 1003 } |
| 1004 |
| 1005 // Tests that a size animation with two keyframes works as expected. |
| 1006 TEST(KeyframedAnimationCurveTest, TwoSizeKeyFrame) { |
| 1007 gfx::SizeF size_a = gfx::SizeF(100, 100); |
| 1008 gfx::SizeF size_b = gfx::SizeF(100, 0); |
| 1009 gfx::SizeF size_midpoint = gfx::Tween::SizeValueBetween(0.5, size_a, size_b); |
| 1010 std::unique_ptr<KeyframedSizeAnimationCurve> curve( |
| 1011 KeyframedSizeAnimationCurve::Create()); |
| 1012 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta(), size_a, nullptr)); |
| 1013 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), |
| 1014 size_b, nullptr)); |
| 1015 |
| 1016 EXPECT_SIZEF_EQ(size_a, curve->GetValue(base::TimeDelta::FromSecondsD(-1.f))); |
| 1017 EXPECT_SIZEF_EQ(size_a, curve->GetValue(base::TimeDelta::FromSecondsD(0.f))); |
| 1018 EXPECT_SIZEF_EQ(size_midpoint, |
| 1019 curve->GetValue(base::TimeDelta::FromSecondsD(0.5f))); |
| 1020 EXPECT_SIZEF_EQ(size_b, curve->GetValue(base::TimeDelta::FromSecondsD(1.f))); |
| 1021 EXPECT_SIZEF_EQ(size_b, curve->GetValue(base::TimeDelta::FromSecondsD(2.f))); |
| 1022 } |
| 1023 |
| 1024 // Tests that a size animation with three keyframes works as expected. |
| 1025 TEST(KeyframedAnimationCurveTest, ThreeSizeKeyFrame) { |
| 1026 gfx::SizeF size_a = gfx::SizeF(100, 100); |
| 1027 gfx::SizeF size_b = gfx::SizeF(100, 0); |
| 1028 gfx::SizeF size_c = gfx::SizeF(100, 0); |
| 1029 gfx::SizeF size_midpoint1 = gfx::Tween::SizeValueBetween(0.5, size_a, size_b); |
| 1030 gfx::SizeF size_midpoint2 = gfx::Tween::SizeValueBetween(0.5, size_b, size_c); |
| 1031 std::unique_ptr<KeyframedSizeAnimationCurve> curve( |
| 1032 KeyframedSizeAnimationCurve::Create()); |
| 1033 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta(), size_a, nullptr)); |
| 1034 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), |
| 1035 size_b, nullptr)); |
| 1036 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta::FromSecondsD(2.0), |
| 1037 size_c, nullptr)); |
| 1038 |
| 1039 EXPECT_SIZEF_EQ(size_a, curve->GetValue(base::TimeDelta::FromSecondsD(-1.f))); |
| 1040 EXPECT_SIZEF_EQ(size_a, curve->GetValue(base::TimeDelta::FromSecondsD(0.f))); |
| 1041 EXPECT_SIZEF_EQ(size_midpoint1, |
| 1042 curve->GetValue(base::TimeDelta::FromSecondsD(0.5f))); |
| 1043 EXPECT_SIZEF_EQ(size_b, curve->GetValue(base::TimeDelta::FromSecondsD(1.f))); |
| 1044 EXPECT_SIZEF_EQ(size_midpoint2, |
| 1045 curve->GetValue(base::TimeDelta::FromSecondsD(1.5f))); |
| 1046 EXPECT_SIZEF_EQ(size_c, curve->GetValue(base::TimeDelta::FromSecondsD(2.f))); |
| 1047 EXPECT_SIZEF_EQ(size_c, curve->GetValue(base::TimeDelta::FromSecondsD(3.f))); |
| 1048 } |
| 1049 |
| 1050 // Tests that a size animation with multiple keys at a given time works sanely. |
| 1051 TEST(KeyframedAnimationCurveTest, RepeatedSizeKeyFrame) { |
| 1052 gfx::SizeF size_a = gfx::SizeF(100, 64); |
| 1053 gfx::SizeF size_b = gfx::SizeF(100, 192); |
| 1054 |
| 1055 std::unique_ptr<KeyframedSizeAnimationCurve> curve( |
| 1056 KeyframedSizeAnimationCurve::Create()); |
| 1057 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta(), size_a, nullptr)); |
| 1058 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), |
| 1059 size_a, nullptr)); |
| 1060 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), |
| 1061 size_b, nullptr)); |
| 1062 curve->AddKeyframe(SizeKeyframe::Create(base::TimeDelta::FromSecondsD(2.0), |
| 1063 size_b, nullptr)); |
| 1064 |
| 1065 EXPECT_SIZEF_EQ(size_a, curve->GetValue(base::TimeDelta::FromSecondsD(-1.f))); |
| 1066 EXPECT_SIZEF_EQ(size_a, curve->GetValue(base::TimeDelta::FromSecondsD(0.f))); |
| 1067 EXPECT_SIZEF_EQ(size_a, curve->GetValue(base::TimeDelta::FromSecondsD(0.5f))); |
| 1068 |
| 1069 gfx::SizeF value = curve->GetValue(base::TimeDelta::FromSecondsD(1.0f)); |
| 1070 EXPECT_FLOAT_EQ(100.0f, value.width()); |
| 1071 EXPECT_LE(64.0f, value.height()); |
| 1072 EXPECT_GE(192.0f, value.height()); |
| 1073 |
| 1074 EXPECT_SIZEF_EQ(size_b, curve->GetValue(base::TimeDelta::FromSecondsD(1.5f))); |
| 1075 EXPECT_SIZEF_EQ(size_b, curve->GetValue(base::TimeDelta::FromSecondsD(2.f))); |
| 1076 EXPECT_SIZEF_EQ(size_b, curve->GetValue(base::TimeDelta::FromSecondsD(3.f))); |
| 1077 } |
| 1078 |
| 989 } // namespace | 1079 } // namespace |
| 990 } // namespace cc | 1080 } // namespace cc |
| OLD | NEW |