1 // Copyright 2014 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 "base/time/time.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/gfx/android/scroller.h"
13 const float kDefaultStartX
= 7.f
;
14 const float kDefaultStartY
= 25.f
;
15 const float kDefaultDeltaX
= -20.f
;
16 const float kDefaultDeltaY
= 73.f
;
17 const float kDefaultVelocityX
= -35.f
;
18 const float kDefaultVelocityY
= 22.f
;
19 const float kEpsilon
= 1e-3f
;
21 Scroller::Config
DefaultConfig() {
22 return Scroller::Config();
27 class ScrollerTest
: public testing::Test
{};
29 TEST_F(ScrollerTest
, Scroll
) {
30 Scroller
scroller(DefaultConfig());
31 base::TimeTicks start_time
= base::TimeTicks::Now();
33 // Start a scroll and verify initialized values.
34 scroller
.StartScroll(kDefaultStartX
,
40 EXPECT_EQ(kDefaultStartX
, scroller
.GetStartX());
41 EXPECT_EQ(kDefaultStartY
, scroller
.GetStartY());
42 EXPECT_EQ(kDefaultStartX
, scroller
.GetCurrX());
43 EXPECT_EQ(kDefaultStartY
, scroller
.GetCurrY());
44 EXPECT_EQ(kDefaultStartX
+ kDefaultDeltaX
, scroller
.GetFinalX());
45 EXPECT_EQ(kDefaultStartY
+ kDefaultDeltaY
, scroller
.GetFinalY());
46 EXPECT_FALSE(scroller
.IsFinished());
47 EXPECT_EQ(base::TimeDelta(), scroller
.GetTimePassed());
49 // Advance halfway through the scroll.
50 const base::TimeDelta scroll_duration
= scroller
.GetDuration();
51 scroller
.ComputeScrollOffset(start_time
+ scroll_duration
/ 2);
53 // Ensure we've moved in the direction of the delta, but have yet to reach
55 EXPECT_GT(kDefaultStartX
, scroller
.GetCurrX());
56 EXPECT_LT(kDefaultStartY
, scroller
.GetCurrY());
57 EXPECT_LT(scroller
.GetFinalX(), scroller
.GetCurrX());
58 EXPECT_GT(scroller
.GetFinalY(), scroller
.GetCurrY());
59 EXPECT_FALSE(scroller
.IsFinished());
61 // Ensure our velocity is non-zero and in the same direction as the delta.
62 EXPECT_GT(0.f
, scroller
.GetCurrVelocityX() * kDefaultDeltaX
);
63 EXPECT_GT(0.f
, scroller
.GetCurrVelocityY() * kDefaultDeltaY
);
64 EXPECT_TRUE(scroller
.IsScrollingInDirection(kDefaultDeltaX
, kDefaultDeltaY
));
66 // Repeated offset computations at the same timestamp should yield identical
68 float curr_x
= scroller
.GetCurrX();
69 float curr_y
= scroller
.GetCurrY();
70 float curr_velocity_x
= scroller
.GetCurrVelocityX();
71 float curr_velocity_y
= scroller
.GetCurrVelocityY();
72 scroller
.ComputeScrollOffset(start_time
+ scroll_duration
/ 2);
73 EXPECT_EQ(curr_x
, scroller
.GetCurrX());
74 EXPECT_EQ(curr_y
, scroller
.GetCurrY());
75 EXPECT_EQ(curr_velocity_x
, scroller
.GetCurrVelocityX());
76 EXPECT_EQ(curr_velocity_y
, scroller
.GetCurrVelocityY());
78 // Advance to the end.
79 scroller
.ComputeScrollOffset(start_time
+ scroll_duration
);
80 EXPECT_EQ(scroller
.GetFinalX(), scroller
.GetCurrX());
81 EXPECT_EQ(scroller
.GetFinalY(), scroller
.GetCurrY());
82 EXPECT_TRUE(scroller
.IsFinished());
83 EXPECT_EQ(scroll_duration
, scroller
.GetTimePassed());
84 EXPECT_NEAR(0.f
, scroller
.GetCurrVelocityX(), kEpsilon
);
85 EXPECT_NEAR(0.f
, scroller
.GetCurrVelocityY(), kEpsilon
);
87 // Try to advance further; nothing should change.
88 scroller
.ComputeScrollOffset(start_time
+ scroll_duration
* 2);
89 EXPECT_EQ(scroller
.GetFinalX(), scroller
.GetCurrX());
90 EXPECT_EQ(scroller
.GetFinalY(), scroller
.GetCurrY());
91 EXPECT_TRUE(scroller
.IsFinished());
92 EXPECT_EQ(scroll_duration
, scroller
.GetTimePassed());
95 TEST_F(ScrollerTest
, Fling
) {
96 Scroller
scroller(DefaultConfig());
97 base::TimeTicks start_time
= base::TimeTicks::Now();
99 // Start a fling and verify initialized values.
100 scroller
.Fling(kDefaultStartX
,
110 EXPECT_EQ(kDefaultStartX
, scroller
.GetStartX());
111 EXPECT_EQ(kDefaultStartY
, scroller
.GetStartY());
112 EXPECT_EQ(kDefaultStartX
, scroller
.GetCurrX());
113 EXPECT_EQ(kDefaultStartY
, scroller
.GetCurrY());
114 EXPECT_GT(kDefaultStartX
, scroller
.GetFinalX());
115 EXPECT_LT(kDefaultStartY
, scroller
.GetFinalY());
116 EXPECT_FALSE(scroller
.IsFinished());
117 EXPECT_EQ(base::TimeDelta(), scroller
.GetTimePassed());
119 // Advance halfway through the fling.
120 const base::TimeDelta scroll_duration
= scroller
.GetDuration();
121 scroller
.ComputeScrollOffset(start_time
+ scroll_duration
/ 2);
123 // Ensure we've moved in the direction of the velocity, but have yet to reach
125 EXPECT_GT(kDefaultStartX
, scroller
.GetCurrX());
126 EXPECT_LT(kDefaultStartY
, scroller
.GetCurrY());
127 EXPECT_LT(scroller
.GetFinalX(), scroller
.GetCurrX());
128 EXPECT_GT(scroller
.GetFinalY(), scroller
.GetCurrY());
129 EXPECT_FALSE(scroller
.IsFinished());
131 // Ensure our velocity is non-zero and in the same direction as the original
133 EXPECT_LT(0.f
, scroller
.GetCurrVelocityX() * kDefaultVelocityX
);
134 EXPECT_LT(0.f
, scroller
.GetCurrVelocityY() * kDefaultVelocityY
);
136 scroller
.IsScrollingInDirection(kDefaultVelocityX
, kDefaultVelocityY
));
138 // Repeated offset computations at the same timestamp should yield identical
140 float curr_x
= scroller
.GetCurrX();
141 float curr_y
= scroller
.GetCurrY();
142 float curr_velocity_x
= scroller
.GetCurrVelocityX();
143 float curr_velocity_y
= scroller
.GetCurrVelocityY();
144 scroller
.ComputeScrollOffset(start_time
+ scroll_duration
/ 2);
145 EXPECT_EQ(curr_x
, scroller
.GetCurrX());
146 EXPECT_EQ(curr_y
, scroller
.GetCurrY());
147 EXPECT_EQ(curr_velocity_x
, scroller
.GetCurrVelocityX());
148 EXPECT_EQ(curr_velocity_y
, scroller
.GetCurrVelocityY());
150 // Advance to the end.
151 scroller
.ComputeScrollOffset(start_time
+ scroll_duration
);
152 EXPECT_EQ(scroller
.GetFinalX(), scroller
.GetCurrX());
153 EXPECT_EQ(scroller
.GetFinalY(), scroller
.GetCurrY());
154 EXPECT_TRUE(scroller
.IsFinished());
155 EXPECT_EQ(scroll_duration
, scroller
.GetTimePassed());
156 EXPECT_NEAR(0.f
, scroller
.GetCurrVelocityX(), kEpsilon
);
157 EXPECT_NEAR(0.f
, scroller
.GetCurrVelocityY(), kEpsilon
);
159 // Try to advance further; nothing should change.
160 scroller
.ComputeScrollOffset(start_time
+ scroll_duration
* 2);
161 EXPECT_EQ(scroller
.GetFinalX(), scroller
.GetCurrX());
162 EXPECT_EQ(scroller
.GetFinalY(), scroller
.GetCurrY());
163 EXPECT_TRUE(scroller
.IsFinished());
164 EXPECT_EQ(scroll_duration
, scroller
.GetTimePassed());