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) { | 14 TEST(ScrollOffsetAnimationCurveTest, Duration) { |
15 gfx::ScrollOffset target_value(100.f, 200.f); | 15 gfx::ScrollOffset target_value(100.f, 200.f); |
16 scoped_ptr<ScrollOffsetAnimationCurve> curve( | 16 scoped_ptr<ScrollOffsetAnimationCurve> curve( |
17 ScrollOffsetAnimationCurve::Create( | 17 ScrollOffsetAnimationCurve::Create( |
18 target_value, | 18 target_value, |
19 EaseInOutTimingFunction::Create().Pass())); | 19 EaseInOutTimingFunction::Create().Pass())); |
20 | 20 |
21 curve->SetInitialValue(target_value); | 21 curve->SetInitialValue(target_value); |
22 EXPECT_DOUBLE_EQ(0.0, curve->Duration()); | 22 EXPECT_DOUBLE_EQ(0.0, curve->Duration().InSecondsF()); |
23 | 23 |
24 // x decreases, y stays the same. | 24 // x decreases, y stays the same. |
25 curve->SetInitialValue(gfx::ScrollOffset(136.f, 200.f)); | 25 curve->SetInitialValue(gfx::ScrollOffset(136.f, 200.f)); |
26 EXPECT_DOUBLE_EQ(0.1, curve->Duration()); | 26 EXPECT_DOUBLE_EQ(0.1, curve->Duration().InSecondsF()); |
27 | 27 |
28 // x increases, y stays the same. | 28 // x increases, y stays the same. |
29 curve->SetInitialValue(gfx::ScrollOffset(19.f, 200.f)); | 29 curve->SetInitialValue(gfx::ScrollOffset(19.f, 200.f)); |
30 EXPECT_DOUBLE_EQ(0.15, curve->Duration()); | 30 EXPECT_DOUBLE_EQ(0.15, curve->Duration().InSecondsF()); |
31 | 31 |
32 // x stays the same, y decreases. | 32 // x stays the same, y decreases. |
33 curve->SetInitialValue(gfx::ScrollOffset(100.f, 344.f)); | 33 curve->SetInitialValue(gfx::ScrollOffset(100.f, 344.f)); |
34 EXPECT_DOUBLE_EQ(0.2, curve->Duration()); | 34 EXPECT_DOUBLE_EQ(0.2, curve->Duration().InSecondsF()); |
35 | 35 |
36 // x stays the same, y increases. | 36 // x stays the same, y increases. |
37 curve->SetInitialValue(gfx::ScrollOffset(100.f, 191.f)); | 37 curve->SetInitialValue(gfx::ScrollOffset(100.f, 191.f)); |
38 EXPECT_DOUBLE_EQ(0.05, curve->Duration()); | 38 EXPECT_DOUBLE_EQ(0.05, curve->Duration().InSecondsF()); |
39 | 39 |
40 // x decreases, y decreases. | 40 // x decreases, y decreases. |
41 curve->SetInitialValue(gfx::ScrollOffset(32500.f, 500.f)); | 41 curve->SetInitialValue(gfx::ScrollOffset(32500.f, 500.f)); |
42 EXPECT_DOUBLE_EQ(3.0, curve->Duration()); | 42 EXPECT_DOUBLE_EQ(3.0, curve->Duration().InSecondsF()); |
43 | 43 |
44 // x decreases, y increases. | 44 // x decreases, y increases. |
45 curve->SetInitialValue(gfx::ScrollOffset(150.f, 119.f)); | 45 curve->SetInitialValue(gfx::ScrollOffset(150.f, 119.f)); |
46 EXPECT_DOUBLE_EQ(0.15, curve->Duration()); | 46 EXPECT_DOUBLE_EQ(0.15, curve->Duration().InSecondsF()); |
47 | 47 |
48 // x increases, y decreases. | 48 // x increases, y decreases. |
49 curve->SetInitialValue(gfx::ScrollOffset(0.f, 14600.f)); | 49 curve->SetInitialValue(gfx::ScrollOffset(0.f, 14600.f)); |
50 EXPECT_DOUBLE_EQ(2.0, curve->Duration()); | 50 EXPECT_DOUBLE_EQ(2.0, curve->Duration().InSecondsF()); |
51 | 51 |
52 // x increases, y increases. | 52 // x increases, y increases. |
53 curve->SetInitialValue(gfx::ScrollOffset(95.f, 191.f)); | 53 curve->SetInitialValue(gfx::ScrollOffset(95.f, 191.f)); |
54 EXPECT_DOUBLE_EQ(0.05, curve->Duration()); | 54 EXPECT_DOUBLE_EQ(0.05, curve->Duration().InSecondsF()); |
55 } | 55 } |
56 | 56 |
57 TEST(ScrollOffsetAnimationCurveTest, GetValue) { | 57 TEST(ScrollOffsetAnimationCurveTest, GetValue) { |
58 gfx::ScrollOffset initial_value(2.f, 40.f); | 58 gfx::ScrollOffset initial_value(2.f, 40.f); |
59 gfx::ScrollOffset target_value(10.f, 20.f); | 59 gfx::ScrollOffset target_value(10.f, 20.f); |
60 scoped_ptr<ScrollOffsetAnimationCurve> curve( | 60 scoped_ptr<ScrollOffsetAnimationCurve> curve( |
61 ScrollOffsetAnimationCurve::Create( | 61 ScrollOffsetAnimationCurve::Create( |
62 target_value, | 62 target_value, |
63 EaseInOutTimingFunction::Create().Pass())); | 63 EaseInOutTimingFunction::Create().Pass())); |
64 curve->SetInitialValue(initial_value); | 64 curve->SetInitialValue(initial_value); |
65 | 65 |
66 double duration = curve->Duration(); | 66 double duration_in_seconds = curve->Duration().InSecondsF(); |
67 EXPECT_GT(curve->Duration(), 0); | 67 EXPECT_GT(curve->Duration().InSecondsF(), 0); |
68 EXPECT_LT(curve->Duration(), 0.1); | 68 EXPECT_LT(curve->Duration().InSecondsF(), 0.1); |
69 | 69 |
70 EXPECT_EQ(AnimationCurve::ScrollOffset, curve->Type()); | 70 EXPECT_EQ(AnimationCurve::ScrollOffset, curve->Type()); |
71 EXPECT_EQ(duration, curve->Duration()); | 71 EXPECT_EQ(duration_in_seconds, curve->Duration().InSecondsF()); |
72 | 72 |
73 EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(-1.0)); | 73 EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(-1.0)); |
74 EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(0.0)); | 74 EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(0.0)); |
75 EXPECT_VECTOR2DF_EQ(gfx::ScrollOffset(6.f, 30.f), | 75 EXPECT_VECTOR2DF_EQ(gfx::ScrollOffset(6.f, 30.f), |
76 curve->GetValue(duration/2.0)); | 76 curve->GetValue(duration_in_seconds / 2.0)); |
77 EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration)); | 77 EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration_in_seconds)); |
78 EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration+1.0)); | 78 EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration_in_seconds + 1.0)); |
79 | 79 |
80 // Verify that GetValue takes the timing function into account. | 80 // Verify that GetValue takes the timing function into account. |
81 gfx::ScrollOffset value = curve->GetValue(duration/4.0); | 81 gfx::ScrollOffset value = curve->GetValue(duration_in_seconds / 4.0); |
82 EXPECT_NEAR(3.0333f, value.x(), 0.00015f); | 82 EXPECT_NEAR(3.0333f, value.x(), 0.00015f); |
83 EXPECT_NEAR(37.4168f, value.y(), 0.00015f); | 83 EXPECT_NEAR(37.4168f, value.y(), 0.00015f); |
84 } | 84 } |
85 | 85 |
86 // Verify that a clone behaves exactly like the original. | 86 // Verify that a clone behaves exactly like the original. |
87 TEST(ScrollOffsetAnimationCurveTest, Clone) { | 87 TEST(ScrollOffsetAnimationCurveTest, Clone) { |
88 gfx::ScrollOffset initial_value(2.f, 40.f); | 88 gfx::ScrollOffset initial_value(2.f, 40.f); |
89 gfx::ScrollOffset target_value(10.f, 20.f); | 89 gfx::ScrollOffset target_value(10.f, 20.f); |
90 scoped_ptr<ScrollOffsetAnimationCurve> curve( | 90 scoped_ptr<ScrollOffsetAnimationCurve> curve( |
91 ScrollOffsetAnimationCurve::Create( | 91 ScrollOffsetAnimationCurve::Create( |
92 target_value, | 92 target_value, |
93 EaseInOutTimingFunction::Create().Pass())); | 93 EaseInOutTimingFunction::Create().Pass())); |
94 curve->SetInitialValue(initial_value); | 94 curve->SetInitialValue(initial_value); |
95 double duration = curve->Duration(); | 95 double duration_in_seconds = curve->Duration().InSecondsF(); |
96 | 96 |
97 scoped_ptr<AnimationCurve> clone(curve->Clone().Pass()); | 97 scoped_ptr<AnimationCurve> clone(curve->Clone().Pass()); |
98 | 98 |
99 EXPECT_EQ(AnimationCurve::ScrollOffset, clone->Type()); | 99 EXPECT_EQ(AnimationCurve::ScrollOffset, clone->Type()); |
100 EXPECT_EQ(duration, clone->Duration()); | 100 EXPECT_EQ(duration_in_seconds, clone->Duration().InSecondsF()); |
101 | 101 |
102 EXPECT_VECTOR2DF_EQ(initial_value, | 102 EXPECT_VECTOR2DF_EQ(initial_value, |
103 clone->ToScrollOffsetAnimationCurve()->GetValue(-1.0)); | 103 clone->ToScrollOffsetAnimationCurve()->GetValue(-1.0)); |
104 EXPECT_VECTOR2DF_EQ(initial_value, | 104 EXPECT_VECTOR2DF_EQ(initial_value, |
105 clone->ToScrollOffsetAnimationCurve()->GetValue(0.0)); | 105 clone->ToScrollOffsetAnimationCurve()->GetValue(0.0)); |
106 EXPECT_VECTOR2DF_EQ( | 106 EXPECT_VECTOR2DF_EQ(gfx::ScrollOffset(6.f, 30.f), |
107 gfx::ScrollOffset(6.f, 30.f), | 107 clone->ToScrollOffsetAnimationCurve()->GetValue( |
108 clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 2.0)); | 108 duration_in_seconds / 2.0)); |
109 EXPECT_VECTOR2DF_EQ( | 109 EXPECT_VECTOR2DF_EQ( |
110 target_value, | 110 target_value, |
111 clone->ToScrollOffsetAnimationCurve()->GetValue(duration)); | 111 clone->ToScrollOffsetAnimationCurve()->GetValue(duration_in_seconds)); |
112 EXPECT_VECTOR2DF_EQ( | 112 EXPECT_VECTOR2DF_EQ(target_value, |
113 target_value, | 113 clone->ToScrollOffsetAnimationCurve()->GetValue( |
114 clone->ToScrollOffsetAnimationCurve()->GetValue(duration + 1.0)); | 114 duration_in_seconds + 1.0)); |
115 | 115 |
116 // Verify that the timing function was cloned correctly. | 116 // Verify that the timing function was cloned correctly. |
117 gfx::ScrollOffset value = | 117 gfx::ScrollOffset value = clone->ToScrollOffsetAnimationCurve()->GetValue( |
118 clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 4.0); | 118 duration_in_seconds / 4.0); |
119 EXPECT_NEAR(3.0333f, value.x(), 0.00015f); | 119 EXPECT_NEAR(3.0333f, value.x(), 0.00015f); |
120 EXPECT_NEAR(37.4168f, value.y(), 0.00015f); | 120 EXPECT_NEAR(37.4168f, value.y(), 0.00015f); |
121 } | 121 } |
122 | 122 |
123 TEST(ScrollOffsetAnimationCurveTest, UpdateTarget) { | 123 TEST(ScrollOffsetAnimationCurveTest, UpdateTarget) { |
124 gfx::ScrollOffset initial_value(0.f, 0.f); | 124 gfx::ScrollOffset initial_value(0.f, 0.f); |
125 gfx::ScrollOffset target_value(0.f, 3600.f); | 125 gfx::ScrollOffset target_value(0.f, 3600.f); |
126 scoped_ptr<ScrollOffsetAnimationCurve> curve( | 126 scoped_ptr<ScrollOffsetAnimationCurve> curve( |
127 ScrollOffsetAnimationCurve::Create( | 127 ScrollOffsetAnimationCurve::Create( |
128 target_value, EaseInOutTimingFunction::Create().Pass())); | 128 target_value, EaseInOutTimingFunction::Create().Pass())); |
129 curve->SetInitialValue(initial_value); | 129 curve->SetInitialValue(initial_value); |
130 EXPECT_EQ(1.0, curve->Duration()); | 130 EXPECT_EQ(1.0, curve->Duration().InSecondsF()); |
131 EXPECT_EQ(1800.0, curve->GetValue(0.5).y()); | 131 EXPECT_EQ(1800.0, curve->GetValue(0.5).y()); |
132 EXPECT_EQ(3600.0, curve->GetValue(1.0).y()); | 132 EXPECT_EQ(3600.0, curve->GetValue(1.0).y()); |
133 | 133 |
134 curve->UpdateTarget(0.5, gfx::ScrollOffset(0.0, 9900.0)); | 134 curve->UpdateTarget(0.5, gfx::ScrollOffset(0.0, 9900.0)); |
135 | 135 |
136 EXPECT_EQ(2.0, curve->Duration()); | 136 EXPECT_EQ(2.0, curve->Duration().InSecondsF()); |
137 EXPECT_EQ(1800.0, curve->GetValue(0.5).y()); | 137 EXPECT_EQ(1800.0, curve->GetValue(0.5).y()); |
138 EXPECT_NEAR(5566.49, curve->GetValue(1.0).y(), 0.01); | 138 EXPECT_NEAR(5566.49, curve->GetValue(1.0).y(), 0.01); |
139 EXPECT_EQ(9900.0, curve->GetValue(2.0).y()); | 139 EXPECT_EQ(9900.0, curve->GetValue(2.0).y()); |
140 | 140 |
141 curve->UpdateTarget(1.0, gfx::ScrollOffset(0.0, 7200.0)); | 141 curve->UpdateTarget(1.0, gfx::ScrollOffset(0.0, 7200.0)); |
142 | 142 |
143 EXPECT_NEAR(1.674, curve->Duration(), 0.01); | 143 EXPECT_NEAR(1.674, curve->Duration().InSecondsF(), 0.01); |
144 EXPECT_NEAR(5566.49, curve->GetValue(1.0).y(), 0.01); | 144 EXPECT_NEAR(5566.49, curve->GetValue(1.0).y(), 0.01); |
145 EXPECT_EQ(7200.0, curve->GetValue(1.674).y()); | 145 EXPECT_EQ(7200.0, curve->GetValue(1.674).y()); |
146 } | 146 } |
147 | 147 |
148 } // namespace | 148 } // namespace |
149 } // namespace cc | 149 } // namespace cc |
OLD | NEW |