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 "athena/home/home_card_gesture_manager.h"
7 #include "athena/home/home_card_constants.h"
8 #include "athena/home/public/home_card.h"
9 #include "athena/test/base/athena_test_base.h"
10 #include "base/time/time.h"
11 #include "ui/events/event.h"
12 #include "ui/events/event_constants.h"
16 class HomeCardGestureManagerTest
: public test::AthenaTestBase
,
17 public HomeCardGestureManager::Delegate
{
19 HomeCardGestureManagerTest()
20 : final_state_(HomeCard::HIDDEN
),
21 last_from_state_(HomeCard::HIDDEN
),
22 last_to_state_(HomeCard::HIDDEN
),
28 ~HomeCardGestureManagerTest() override
{}
31 void SetUp() override
{
32 test::AthenaTestBase::SetUp();
33 gesture_manager_
.reset(new HomeCardGestureManager(this, screen_bounds()));
37 int GetEndCountAndReset() {
38 int result
= end_count_
;
42 int GetProgressCountAndReset() {
43 int result
= progress_count_
;
48 // Process a gesture event for our use case.
49 bool ProcessGestureEvent(ui::EventType type
, int y
) {
50 ui::GestureEventDetails details
;
51 if (type
== ui::ET_GESTURE_SCROLL_BEGIN
||
52 type
== ui::ET_GESTURE_SCROLL_UPDATE
)
53 details
= ui::GestureEventDetails(type
, 0, (y
- last_y_
));
55 details
= ui::GestureEventDetails(type
);
56 ui::GestureEvent
event(0, y
, ui::EF_NONE
, base::TimeDelta(), details
);
57 if (type
== ui::ET_GESTURE_SCROLL_BEGIN
) {
58 // Compute the position that the home card would have wrt to the top of
59 // the screen if the screen had screen_bounds().
60 HomeCard::State state
= HomeCard::Get()->GetState();
61 int home_card_top
= 0;
62 if (state
== HomeCard::VISIBLE_BOTTOM
)
63 home_card_top
= screen_bounds().height() - kHomeCardHeight
;
64 else if (state
== HomeCard::VISIBLE_MINIMIZED
)
65 home_card_top
= screen_bounds().height() - kHomeCardMinimizedHeight
;
67 gfx::Point location
= event
.location();
68 location
.set_y(location
.y() - home_card_top
);
69 event
.set_location(location
);
71 gesture_manager_
->ProcessGestureEvent(&event
);
73 return event
.handled();
76 void ProcessFlingGesture(float velocity
) {
77 ui::GestureEvent
event(0, last_y_
, ui::EF_NONE
, base::TimeDelta(),
78 ui::GestureEventDetails(
79 ui::ET_SCROLL_FLING_START
, 0, velocity
));
80 gesture_manager_
->ProcessGestureEvent(&event
);
83 int screen_height() const {
84 return screen_bounds().height();
87 HomeCard::State final_state_
;
88 HomeCard::State last_from_state_
;
89 HomeCard::State last_to_state_
;
94 gfx::Rect
screen_bounds() const {
95 return gfx::Rect(0, 0, 1280, 1024);
98 // HomeCardGestureManager::Delegate:
99 void OnGestureEnded(HomeCard::State final_state
, bool is_fling
) override
{
100 final_state_
= final_state
;
101 was_fling_
= is_fling
;
105 void OnGestureProgressed(HomeCard::State from_state
,
106 HomeCard::State to_state
,
107 float progress
) override
{
108 last_from_state_
= from_state
;
109 last_to_state_
= to_state
;
110 last_progress_
= progress
;
117 scoped_ptr
<HomeCardGestureManager
> gesture_manager_
;
119 DISALLOW_COPY_AND_ASSIGN(HomeCardGestureManagerTest
);
122 TEST_F(HomeCardGestureManagerTest
, Basic
) {
123 ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
125 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 1020));
126 EXPECT_EQ(0, GetEndCountAndReset());
127 EXPECT_EQ(0, GetProgressCountAndReset());
129 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1019);
130 EXPECT_EQ(1, GetProgressCountAndReset());
131 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, last_from_state_
);
132 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_to_state_
);
133 EXPECT_GT(1.0f
, last_progress_
);
135 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1020);
136 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1022);
137 EXPECT_EQ(2, GetProgressCountAndReset());
138 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_from_state_
);
139 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, last_to_state_
);
140 EXPECT_EQ(1.0f
, last_progress_
);
142 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1010);
143 float progress_1010
= last_progress_
;
144 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1008);
145 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1000);
146 EXPECT_EQ(3, GetProgressCountAndReset());
147 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, last_from_state_
);
148 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_to_state_
);
149 EXPECT_LT(progress_1010
, last_progress_
);
151 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 900);
152 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 800);
153 EXPECT_EQ(2, GetProgressCountAndReset());
154 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_from_state_
);
155 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, last_to_state_
);
156 float progress_800
= last_progress_
;
157 EXPECT_GT(1.0f
, last_progress_
);
158 EXPECT_LT(0.0f
, last_progress_
);
160 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 790);
161 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_from_state_
);
162 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, last_to_state_
);
163 EXPECT_LT(progress_800
, last_progress_
);
165 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 810);
166 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_from_state_
);
167 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, last_to_state_
);
168 EXPECT_GT(progress_800
, last_progress_
);
170 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END
, 810));
171 EXPECT_EQ(1, GetEndCountAndReset());
172 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, final_state_
);
173 EXPECT_FALSE(was_fling_
);
176 // Test gesture progress when the gesture is initiated when the home card is in
177 // the centered state.
178 TEST_F(HomeCardGestureManagerTest
, StartCentered
) {
179 HomeCard::Get()->SetState(HomeCard::VISIBLE_CENTERED
);
181 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 20));
183 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 10);
184 EXPECT_EQ(1, GetProgressCountAndReset());
185 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_from_state_
);
186 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, last_to_state_
);
187 EXPECT_EQ(1.0f
, last_progress_
);
189 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 700);
190 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 710);
191 EXPECT_EQ(2, GetProgressCountAndReset());
192 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_from_state_
);
193 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, last_to_state_
);
194 EXPECT_GT(1.0f
, last_progress_
);
195 EXPECT_LT(0.0f
, last_progress_
);
197 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 960);
198 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END
, 960));
199 EXPECT_EQ(1, GetEndCountAndReset());
200 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, final_state_
);
203 // Test gesture progress when the gesture is initiated when the home card is in
204 // the centered state.
205 TEST_F(HomeCardGestureManagerTest
, StartBottom
) {
206 HomeCard::Get()->SetState(HomeCard::VISIBLE_BOTTOM
);
208 // No changes for slight moves.
209 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 950));
210 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 960);
211 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END
, 960));
212 EXPECT_EQ(1, GetEndCountAndReset());
213 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, final_state_
);
215 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 950));
216 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END
, 800));
217 EXPECT_EQ(1, GetEndCountAndReset());
218 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, final_state_
);
220 // State change for the bigger moves.
221 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 850));
222 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1000);
223 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END
, 1000));
224 EXPECT_EQ(1, GetEndCountAndReset());
225 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, final_state_
);
227 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 850));
228 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 300);
229 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END
, 300));
230 EXPECT_EQ(1, GetEndCountAndReset());
231 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, final_state_
);
232 EXPECT_FALSE(was_fling_
);
235 TEST_F(HomeCardGestureManagerTest
, FlingUpAtEnd
) {
236 ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
238 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 1020));
239 EXPECT_EQ(0, GetEndCountAndReset());
240 EXPECT_EQ(0, GetProgressCountAndReset());
242 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1010);
243 ProcessFlingGesture(-150.0f
);
244 EXPECT_EQ(1, GetEndCountAndReset());
245 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, final_state_
);
246 EXPECT_TRUE(was_fling_
);
249 TEST_F(HomeCardGestureManagerTest
, FlingDownAtEnd
) {
250 HomeCard::Get()->SetState(HomeCard::VISIBLE_CENTERED
);
252 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 20));
253 EXPECT_EQ(0, GetEndCountAndReset());
254 EXPECT_EQ(0, GetProgressCountAndReset());
256 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 30);
257 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 100);
258 ProcessFlingGesture(150.0f
);
259 EXPECT_EQ(1, GetEndCountAndReset());
260 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, final_state_
);
261 EXPECT_TRUE(was_fling_
);
264 TEST_F(HomeCardGestureManagerTest
, WeakFling
) {
265 ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
267 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 1020));
268 EXPECT_EQ(0, GetEndCountAndReset());
269 EXPECT_EQ(0, GetProgressCountAndReset());
271 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1010);
272 ProcessFlingGesture(-30.0f
);
273 EXPECT_EQ(1, GetEndCountAndReset());
274 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, final_state_
);
275 EXPECT_FALSE(was_fling_
);
278 // Test the situation where the user intends a single fling but the finger
279 // touches the screen long enough, so that the home card becomes bigger than the
280 // height of VISIBLE_BOTTOM state due to the scroll events.
281 // In this case the fling event should not change the final state from
282 // VISIBLE_BOTTOM to VISIBLE_CENTERED because the user's intention was a single
283 // fling. See http://crbug.com/415211
284 TEST_F(HomeCardGestureManagerTest
, FastFling
) {
285 ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
287 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 1020));
288 EXPECT_EQ(0, GetEndCountAndReset());
289 EXPECT_EQ(0, GetProgressCountAndReset());
291 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1010);
292 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
,
293 screen_height() - kHomeCardHeight
);
294 ProcessFlingGesture(-150.0f
);
295 EXPECT_EQ(1, GetEndCountAndReset());
296 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, final_state_
);
297 EXPECT_TRUE(was_fling_
);
300 } // namespace athena