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
3 // found in the LICENSE file.
5 #include "cc/animation/scroll_offset_animation_curve.h"
7 #include "cc/animation/timing_function.h"
8 #include "cc/base/time_util.h"
9 #include "cc/test/geometry_test_utils.h"
10 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(ScrollOffsetAnimationCurveTest
, Duration
) {
16 gfx::ScrollOffset
target_value(100.f
, 200.f
);
17 scoped_ptr
<ScrollOffsetAnimationCurve
> curve(
18 ScrollOffsetAnimationCurve::Create(
20 EaseInOutTimingFunction::Create().Pass()));
22 curve
->SetInitialValue(target_value
);
23 EXPECT_DOUBLE_EQ(0.0, curve
->Duration().InSecondsF());
25 // x decreases, y stays the same.
26 curve
->SetInitialValue(gfx::ScrollOffset(136.f
, 200.f
));
27 EXPECT_DOUBLE_EQ(0.1, curve
->Duration().InSecondsF());
29 // x increases, y stays the same.
30 curve
->SetInitialValue(gfx::ScrollOffset(19.f
, 200.f
));
31 EXPECT_DOUBLE_EQ(0.15, curve
->Duration().InSecondsF());
33 // x stays the same, y decreases.
34 curve
->SetInitialValue(gfx::ScrollOffset(100.f
, 344.f
));
35 EXPECT_DOUBLE_EQ(0.2, curve
->Duration().InSecondsF());
37 // x stays the same, y increases.
38 curve
->SetInitialValue(gfx::ScrollOffset(100.f
, 191.f
));
39 EXPECT_DOUBLE_EQ(0.05, curve
->Duration().InSecondsF());
41 // x decreases, y decreases.
42 curve
->SetInitialValue(gfx::ScrollOffset(32500.f
, 500.f
));
43 EXPECT_DOUBLE_EQ(3.0, curve
->Duration().InSecondsF());
45 // x decreases, y increases.
46 curve
->SetInitialValue(gfx::ScrollOffset(150.f
, 119.f
));
47 EXPECT_DOUBLE_EQ(0.15, curve
->Duration().InSecondsF());
49 // x increases, y decreases.
50 curve
->SetInitialValue(gfx::ScrollOffset(0.f
, 14600.f
));
51 EXPECT_DOUBLE_EQ(2.0, curve
->Duration().InSecondsF());
53 // x increases, y increases.
54 curve
->SetInitialValue(gfx::ScrollOffset(95.f
, 191.f
));
55 EXPECT_DOUBLE_EQ(0.05, curve
->Duration().InSecondsF());
58 TEST(ScrollOffsetAnimationCurveTest
, GetValue
) {
59 gfx::ScrollOffset
initial_value(2.f
, 40.f
);
60 gfx::ScrollOffset
target_value(10.f
, 20.f
);
61 scoped_ptr
<ScrollOffsetAnimationCurve
> curve(
62 ScrollOffsetAnimationCurve::Create(
64 EaseInOutTimingFunction::Create().Pass()));
65 curve
->SetInitialValue(initial_value
);
67 base::TimeDelta duration
= curve
->Duration();
68 EXPECT_GT(curve
->Duration().InSecondsF(), 0);
69 EXPECT_LT(curve
->Duration().InSecondsF(), 0.1);
71 EXPECT_EQ(AnimationCurve::ScrollOffset
, curve
->Type());
72 EXPECT_EQ(duration
, curve
->Duration());
74 EXPECT_VECTOR2DF_EQ(initial_value
,
75 curve
->GetValue(base::TimeDelta::FromSecondsD(-1.0)));
76 EXPECT_VECTOR2DF_EQ(initial_value
, curve
->GetValue(base::TimeDelta()));
77 EXPECT_VECTOR2DF_NEAR(gfx::ScrollOffset(6.f
, 30.f
),
78 curve
->GetValue(TimeUtil::Scale(duration
, 0.5f
)),
80 EXPECT_VECTOR2DF_EQ(target_value
, curve
->GetValue(duration
));
83 curve
->GetValue(duration
+ base::TimeDelta::FromSecondsD(1.0)));
85 // Verify that GetValue takes the timing function into account.
86 gfx::ScrollOffset value
= curve
->GetValue(TimeUtil::Scale(duration
, 0.25f
));
87 EXPECT_NEAR(3.0333f
, value
.x(), 0.0002f
);
88 EXPECT_NEAR(37.4168f
, value
.y(), 0.0002f
);
91 // Verify that a clone behaves exactly like the original.
92 TEST(ScrollOffsetAnimationCurveTest
, Clone
) {
93 gfx::ScrollOffset
initial_value(2.f
, 40.f
);
94 gfx::ScrollOffset
target_value(10.f
, 20.f
);
95 scoped_ptr
<ScrollOffsetAnimationCurve
> curve(
96 ScrollOffsetAnimationCurve::Create(
98 EaseInOutTimingFunction::Create().Pass()));
99 curve
->SetInitialValue(initial_value
);
100 base::TimeDelta duration
= curve
->Duration();
102 scoped_ptr
<AnimationCurve
> clone(curve
->Clone().Pass());
104 EXPECT_EQ(AnimationCurve::ScrollOffset
, clone
->Type());
105 EXPECT_EQ(duration
, clone
->Duration());
107 EXPECT_VECTOR2DF_EQ(initial_value
,
108 clone
->ToScrollOffsetAnimationCurve()->GetValue(
109 base::TimeDelta::FromSecondsD(-1.0)));
112 clone
->ToScrollOffsetAnimationCurve()->GetValue(base::TimeDelta()));
113 EXPECT_VECTOR2DF_NEAR(gfx::ScrollOffset(6.f
, 30.f
),
114 clone
->ToScrollOffsetAnimationCurve()->GetValue(
115 TimeUtil::Scale(duration
, 0.5f
)),
118 target_value
, clone
->ToScrollOffsetAnimationCurve()->GetValue(duration
));
119 EXPECT_VECTOR2DF_EQ(target_value
,
120 clone
->ToScrollOffsetAnimationCurve()->GetValue(
121 duration
+ base::TimeDelta::FromSecondsD(1.f
)));
123 // Verify that the timing function was cloned correctly.
124 gfx::ScrollOffset value
= clone
->ToScrollOffsetAnimationCurve()->GetValue(
125 TimeUtil::Scale(duration
, 0.25f
));
126 EXPECT_NEAR(3.0333f
, value
.x(), 0.0002f
);
127 EXPECT_NEAR(37.4168f
, value
.y(), 0.0002f
);
130 TEST(ScrollOffsetAnimationCurveTest
, UpdateTarget
) {
131 gfx::ScrollOffset
initial_value(0.f
, 0.f
);
132 gfx::ScrollOffset
target_value(0.f
, 3600.f
);
133 scoped_ptr
<ScrollOffsetAnimationCurve
> curve(
134 ScrollOffsetAnimationCurve::Create(
135 target_value
, EaseInOutTimingFunction::Create().Pass()));
136 curve
->SetInitialValue(initial_value
);
137 EXPECT_EQ(1.0, curve
->Duration().InSecondsF());
138 EXPECT_EQ(1800.0, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5)).y());
139 EXPECT_EQ(3600.0, curve
->GetValue(base::TimeDelta::FromSecondsD(1.0)).y());
141 curve
->UpdateTarget(0.5, gfx::ScrollOffset(0.0, 9900.0));
143 EXPECT_EQ(2.0, curve
->Duration().InSecondsF());
144 EXPECT_EQ(1800.0, curve
->GetValue(base::TimeDelta::FromSecondsD(0.5)).y());
145 EXPECT_NEAR(5566.49, curve
->GetValue(base::TimeDelta::FromSecondsD(1.0)).y(),
147 EXPECT_EQ(9900.0, curve
->GetValue(base::TimeDelta::FromSecondsD(2.0)).y());
149 curve
->UpdateTarget(1.0, gfx::ScrollOffset(0.0, 7200.0));
151 EXPECT_NEAR(1.674, curve
->Duration().InSecondsF(), 0.01);
152 EXPECT_NEAR(5566.49, curve
->GetValue(base::TimeDelta::FromSecondsD(1.0)).y(),
154 EXPECT_EQ(7200.0, curve
->GetValue(base::TimeDelta::FromSecondsD(1.674)).y());