Revert of Output closure-compiled JavaScript files (patchset #10 id:180001 of https...
[chromium-blink-merge.git] / ui / events / android / scroller_unittest.cc
blob759984e9ed54223d301c02d2faaa2cba59269437
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/events/android/scroller.h"
9 namespace ui {
10 namespace {
12 const float kDefaultStartX = 7.f;
13 const float kDefaultStartY = 25.f;
14 const float kDefaultDeltaX = -20.f;
15 const float kDefaultDeltaY = 73.f;
16 const float kDefaultVelocityX = -350.f;
17 const float kDefaultVelocityY = 220.f;
18 const float kEpsilon = 1e-3f;
20 Scroller::Config DefaultConfig() {
21 return Scroller::Config();
24 } // namespace
26 class ScrollerTest : public testing::Test {};
28 TEST_F(ScrollerTest, Scroll) {
29 Scroller scroller(DefaultConfig());
30 base::TimeTicks start_time = base::TimeTicks::Now();
32 // Start a scroll and verify initialized values.
33 scroller.StartScroll(kDefaultStartX,
34 kDefaultStartY,
35 kDefaultDeltaX,
36 kDefaultDeltaY,
37 start_time);
39 EXPECT_EQ(kDefaultStartX, scroller.GetStartX());
40 EXPECT_EQ(kDefaultStartY, scroller.GetStartY());
41 EXPECT_EQ(kDefaultStartX, scroller.GetCurrX());
42 EXPECT_EQ(kDefaultStartY, scroller.GetCurrY());
43 EXPECT_EQ(kDefaultStartX + kDefaultDeltaX, scroller.GetFinalX());
44 EXPECT_EQ(kDefaultStartY + kDefaultDeltaY, scroller.GetFinalY());
45 EXPECT_FALSE(scroller.IsFinished());
46 EXPECT_EQ(base::TimeDelta(), scroller.GetTimePassed());
48 // Advance halfway through the scroll.
49 const base::TimeDelta scroll_duration = scroller.GetDuration();
50 gfx::Vector2dF offset, velocity;
51 EXPECT_TRUE(scroller.ComputeScrollOffset(
52 start_time + scroll_duration / 2, &offset, &velocity));
54 // Ensure we've moved in the direction of the delta, but have yet to reach
55 // the target.
56 EXPECT_GT(kDefaultStartX, offset.x());
57 EXPECT_LT(kDefaultStartY, offset.y());
58 EXPECT_LT(scroller.GetFinalX(), offset.x());
59 EXPECT_GT(scroller.GetFinalY(), offset.y());
60 EXPECT_FALSE(scroller.IsFinished());
62 // Ensure our velocity is non-zero and in the same direction as the delta.
63 EXPECT_GT(0.f, velocity.x() * kDefaultDeltaX);
64 EXPECT_GT(0.f, velocity.y() * kDefaultDeltaY);
65 EXPECT_TRUE(scroller.IsScrollingInDirection(kDefaultDeltaX, kDefaultDeltaY));
67 // Repeated offset computations at the same timestamp should yield identical
68 // results.
69 float curr_x = offset.x();
70 float curr_y = offset.y();
71 float curr_velocity_x = velocity.x();
72 float curr_velocity_y = velocity.y();
73 EXPECT_TRUE(scroller.ComputeScrollOffset(
74 start_time + scroll_duration / 2, &offset, &velocity));
75 EXPECT_EQ(curr_x, offset.x());
76 EXPECT_EQ(curr_y, offset.y());
77 EXPECT_EQ(curr_velocity_x, velocity.x());
78 EXPECT_EQ(curr_velocity_y, velocity.y());
80 // Advance to the end.
81 EXPECT_FALSE(scroller.ComputeScrollOffset(
82 start_time + scroll_duration, &offset, &velocity));
83 EXPECT_EQ(scroller.GetFinalX(), offset.x());
84 EXPECT_EQ(scroller.GetFinalY(), offset.y());
85 EXPECT_TRUE(scroller.IsFinished());
86 EXPECT_EQ(scroll_duration, scroller.GetTimePassed());
87 EXPECT_NEAR(0.f, velocity.x(), kEpsilon);
88 EXPECT_NEAR(0.f, velocity.y(), kEpsilon);
90 // Try to advance further; nothing should change.
91 EXPECT_FALSE(scroller.ComputeScrollOffset(
92 start_time + scroll_duration * 2, &offset, &velocity));
93 EXPECT_EQ(scroller.GetFinalX(), offset.x());
94 EXPECT_EQ(scroller.GetFinalY(), offset.y());
95 EXPECT_TRUE(scroller.IsFinished());
96 EXPECT_EQ(scroll_duration, scroller.GetTimePassed());
99 TEST_F(ScrollerTest, Fling) {
100 Scroller scroller(DefaultConfig());
101 base::TimeTicks start_time = base::TimeTicks::Now();
103 // Start a fling and verify initialized values.
104 scroller.Fling(kDefaultStartX,
105 kDefaultStartY,
106 kDefaultVelocityX,
107 kDefaultVelocityY,
108 INT_MIN,
109 INT_MAX,
110 INT_MIN,
111 INT_MAX,
112 start_time);
114 EXPECT_EQ(kDefaultStartX, scroller.GetStartX());
115 EXPECT_EQ(kDefaultStartY, scroller.GetStartY());
116 EXPECT_EQ(kDefaultStartX, scroller.GetCurrX());
117 EXPECT_EQ(kDefaultStartY, scroller.GetCurrY());
118 EXPECT_GT(kDefaultStartX, scroller.GetFinalX());
119 EXPECT_LT(kDefaultStartY, scroller.GetFinalY());
120 EXPECT_FALSE(scroller.IsFinished());
121 EXPECT_EQ(base::TimeDelta(), scroller.GetTimePassed());
123 // Advance halfway through the fling.
124 const base::TimeDelta scroll_duration = scroller.GetDuration();
125 gfx::Vector2dF offset, velocity;
126 scroller.ComputeScrollOffset(
127 start_time + scroll_duration / 2, &offset, &velocity);
129 // Ensure we've moved in the direction of the velocity, but have yet to reach
130 // the target.
131 EXPECT_GT(kDefaultStartX, offset.x());
132 EXPECT_LT(kDefaultStartY, offset.y());
133 EXPECT_LT(scroller.GetFinalX(), offset.x());
134 EXPECT_GT(scroller.GetFinalY(), offset.y());
135 EXPECT_FALSE(scroller.IsFinished());
137 // Ensure our velocity is non-zero and in the same direction as the original
138 // velocity.
139 EXPECT_LT(0.f, velocity.x() * kDefaultVelocityX);
140 EXPECT_LT(0.f, velocity.y() * kDefaultVelocityY);
141 EXPECT_TRUE(
142 scroller.IsScrollingInDirection(kDefaultVelocityX, kDefaultVelocityY));
144 // Repeated offset computations at the same timestamp should yield identical
145 // results.
146 float curr_x = offset.x();
147 float curr_y = offset.y();
148 float curr_velocity_x = velocity.x();
149 float curr_velocity_y = velocity.y();
150 EXPECT_TRUE(scroller.ComputeScrollOffset(
151 start_time + scroll_duration / 2, &offset, &velocity));
152 EXPECT_EQ(curr_x, offset.x());
153 EXPECT_EQ(curr_y, offset.y());
154 EXPECT_EQ(curr_velocity_x, velocity.x());
155 EXPECT_EQ(curr_velocity_y, velocity.y());
157 // Advance to the end.
158 EXPECT_FALSE(scroller.ComputeScrollOffset(
159 start_time + scroll_duration, &offset, &velocity));
160 EXPECT_EQ(scroller.GetFinalX(), offset.x());
161 EXPECT_EQ(scroller.GetFinalY(), offset.y());
162 EXPECT_TRUE(scroller.IsFinished());
163 EXPECT_EQ(scroll_duration, scroller.GetTimePassed());
164 EXPECT_NEAR(0.f, velocity.x(), kEpsilon);
165 EXPECT_NEAR(0.f, velocity.y(), kEpsilon);
167 // Try to advance further; nothing should change.
168 EXPECT_FALSE(scroller.ComputeScrollOffset(
169 start_time + scroll_duration * 2, &offset, &velocity));
170 EXPECT_EQ(scroller.GetFinalX(), offset.x());
171 EXPECT_EQ(scroller.GetFinalY(), offset.y());
172 EXPECT_TRUE(scroller.IsFinished());
173 EXPECT_EQ(scroll_duration, scroller.GetTimePassed());
176 } // namespace ui