OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/scroll_offset_animation_curve.h" | 5 #include "cc/animation/scroll_offset_animation_curve.h" |
6 | 6 |
7 #include "cc/animation/timing_function.h" | 7 #include "cc/animation/timing_function.h" |
8 #include "cc/test/geometry_test_utils.h" | 8 #include "cc/test/geometry_test_utils.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 TEST(ScrollOffsetAnimationCurveTest, Duration) { |
| 15 gfx::Vector2dF target_value(100.f, 200.f); |
| 16 scoped_ptr<ScrollOffsetAnimationCurve> curve( |
| 17 ScrollOffsetAnimationCurve::Create( |
| 18 target_value, |
| 19 EaseInOutTimingFunction::Create().Pass())); |
| 20 |
| 21 curve->SetInitialValue(target_value); |
| 22 EXPECT_DOUBLE_EQ(0.0, curve->Duration()); |
| 23 |
| 24 // x decreases, y stays the same. |
| 25 curve->SetInitialValue(gfx::Vector2dF(136.f, 200.f)); |
| 26 EXPECT_DOUBLE_EQ(0.1, curve->Duration()); |
| 27 |
| 28 // x increases, y stays the same. |
| 29 curve->SetInitialValue(gfx::Vector2dF(19.f, 200.f)); |
| 30 EXPECT_DOUBLE_EQ(0.15, curve->Duration()); |
| 31 |
| 32 // x stays the same, y decreases. |
| 33 curve->SetInitialValue(gfx::Vector2dF(100.f, 344.f)); |
| 34 EXPECT_DOUBLE_EQ(0.2, curve->Duration()); |
| 35 |
| 36 // x stays the same, y increases. |
| 37 curve->SetInitialValue(gfx::Vector2dF(100.f, 191.f)); |
| 38 EXPECT_DOUBLE_EQ(0.05, curve->Duration()); |
| 39 |
| 40 // x decreases, y decreases. |
| 41 curve->SetInitialValue(gfx::Vector2dF(32500.f, 500.f)); |
| 42 EXPECT_DOUBLE_EQ(3.0, curve->Duration()); |
| 43 |
| 44 // x decreases, y increases. |
| 45 curve->SetInitialValue(gfx::Vector2dF(150.f, 119.f)); |
| 46 EXPECT_DOUBLE_EQ(0.15, curve->Duration()); |
| 47 |
| 48 // x increases, y decreases. |
| 49 curve->SetInitialValue(gfx::Vector2dF(0.f, 14600.f)); |
| 50 EXPECT_DOUBLE_EQ(2.0, curve->Duration()); |
| 51 |
| 52 // x increases, y increases. |
| 53 curve->SetInitialValue(gfx::Vector2dF(95.f, 191.f)); |
| 54 EXPECT_DOUBLE_EQ(0.05, curve->Duration()); |
| 55 } |
| 56 |
14 TEST(ScrollOffsetAnimationCurveTest, GetValue) { | 57 TEST(ScrollOffsetAnimationCurveTest, GetValue) { |
15 gfx::Vector2dF initial_value(2.f, 40.f); | 58 gfx::Vector2dF initial_value(2.f, 40.f); |
16 gfx::Vector2dF target_value(10.f, 20.f); | 59 gfx::Vector2dF target_value(10.f, 20.f); |
17 double duration = 10.0; | |
18 scoped_ptr<ScrollOffsetAnimationCurve> curve( | 60 scoped_ptr<ScrollOffsetAnimationCurve> curve( |
19 ScrollOffsetAnimationCurve::Create( | 61 ScrollOffsetAnimationCurve::Create( |
20 target_value, | 62 target_value, |
21 EaseInOutTimingFunction::Create().Pass())); | 63 EaseInOutTimingFunction::Create().Pass())); |
22 curve->set_duration(duration); | 64 curve->SetInitialValue(initial_value); |
23 curve->set_initial_value(initial_value); | 65 |
| 66 double duration = curve->Duration(); |
| 67 EXPECT_GT(curve->Duration(), 0); |
| 68 EXPECT_LT(curve->Duration(), 0.1); |
24 | 69 |
25 EXPECT_EQ(AnimationCurve::ScrollOffset, curve->Type()); | 70 EXPECT_EQ(AnimationCurve::ScrollOffset, curve->Type()); |
26 EXPECT_EQ(duration, curve->Duration()); | 71 EXPECT_EQ(duration, curve->Duration()); |
27 | 72 |
28 EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(-1.0)); | 73 EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(-1.0)); |
29 EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(0.0)); | 74 EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(0.0)); |
30 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(6.f, 30.f), curve->GetValue(duration/2.0)); | 75 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(6.f, 30.f), curve->GetValue(duration/2.0)); |
31 EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration)); | 76 EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration)); |
32 EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration+1.0)); | 77 EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration+1.0)); |
33 | 78 |
34 // Verify that GetValue takes the timing function into account. | 79 // Verify that GetValue takes the timing function into account. |
35 gfx::Vector2dF value = curve->GetValue(duration/4.0); | 80 gfx::Vector2dF value = curve->GetValue(duration/4.0); |
36 EXPECT_NEAR(3.0333f, value.x(), 0.00015f); | 81 EXPECT_NEAR(3.0333f, value.x(), 0.00015f); |
37 EXPECT_NEAR(37.4168f, value.y(), 0.00015f); | 82 EXPECT_NEAR(37.4168f, value.y(), 0.00015f); |
38 } | 83 } |
39 | 84 |
40 // Verify that a clone behaves exactly like the original. | 85 // Verify that a clone behaves exactly like the original. |
41 TEST(ScrollOffsetAnimationCurveTest, Clone) { | 86 TEST(ScrollOffsetAnimationCurveTest, Clone) { |
42 gfx::Vector2dF initial_value(2.f, 40.f); | 87 gfx::Vector2dF initial_value(2.f, 40.f); |
43 gfx::Vector2dF target_value(10.f, 20.f); | 88 gfx::Vector2dF target_value(10.f, 20.f); |
44 double duration = 10.0; | |
45 scoped_ptr<ScrollOffsetAnimationCurve> curve( | 89 scoped_ptr<ScrollOffsetAnimationCurve> curve( |
46 ScrollOffsetAnimationCurve::Create( | 90 ScrollOffsetAnimationCurve::Create( |
47 target_value, | 91 target_value, |
48 EaseInOutTimingFunction::Create().Pass())); | 92 EaseInOutTimingFunction::Create().Pass())); |
49 curve->set_duration(duration); | 93 curve->SetInitialValue(initial_value); |
50 curve->set_initial_value(initial_value); | 94 double duration = curve->Duration(); |
51 | 95 |
52 scoped_ptr<AnimationCurve> clone(curve->Clone().Pass()); | 96 scoped_ptr<AnimationCurve> clone(curve->Clone().Pass()); |
53 | 97 |
54 EXPECT_EQ(AnimationCurve::ScrollOffset, clone->Type()); | 98 EXPECT_EQ(AnimationCurve::ScrollOffset, clone->Type()); |
55 EXPECT_EQ(duration, clone->Duration()); | 99 EXPECT_EQ(duration, clone->Duration()); |
56 | 100 |
57 EXPECT_VECTOR2DF_EQ(initial_value, | 101 EXPECT_VECTOR2DF_EQ(initial_value, |
58 clone->ToScrollOffsetAnimationCurve()->GetValue(-1.0)); | 102 clone->ToScrollOffsetAnimationCurve()->GetValue(-1.0)); |
59 EXPECT_VECTOR2DF_EQ(initial_value, | 103 EXPECT_VECTOR2DF_EQ(initial_value, |
60 clone->ToScrollOffsetAnimationCurve()->GetValue(0.0)); | 104 clone->ToScrollOffsetAnimationCurve()->GetValue(0.0)); |
61 EXPECT_VECTOR2DF_EQ( | 105 EXPECT_VECTOR2DF_EQ( |
62 gfx::Vector2dF(6.f, 30.f), | 106 gfx::Vector2dF(6.f, 30.f), |
63 clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 2.0)); | 107 clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 2.0)); |
64 EXPECT_VECTOR2DF_EQ( | 108 EXPECT_VECTOR2DF_EQ( |
65 target_value, | 109 target_value, |
66 clone->ToScrollOffsetAnimationCurve()->GetValue(duration)); | 110 clone->ToScrollOffsetAnimationCurve()->GetValue(duration)); |
67 EXPECT_VECTOR2DF_EQ( | 111 EXPECT_VECTOR2DF_EQ( |
68 target_value, | 112 target_value, |
69 clone->ToScrollOffsetAnimationCurve()->GetValue(duration + 1.0)); | 113 clone->ToScrollOffsetAnimationCurve()->GetValue(duration + 1.0)); |
70 | 114 |
71 // Verify that the timing function was cloned correctly. | 115 // Verify that the timing function was cloned correctly. |
72 gfx::Vector2dF value = | 116 gfx::Vector2dF value = |
73 clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 4.0); | 117 clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 4.0); |
74 EXPECT_NEAR(3.0333f, value.x(), 0.00015f); | 118 EXPECT_NEAR(3.0333f, value.x(), 0.00015f); |
75 EXPECT_NEAR(37.4168f, value.y(), 0.00015f); | 119 EXPECT_NEAR(37.4168f, value.y(), 0.00015f); |
76 } | 120 } |
77 | 121 |
78 | |
79 } // namespace | 122 } // namespace |
80 } // namespace cc | 123 } // namespace cc |
OLD | NEW |