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/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
),
27 virtual ~HomeCardGestureManagerTest() {}
30 virtual void SetUp() OVERRIDE
{
31 test::AthenaTestBase::SetUp();
32 gesture_manager_
.reset(new HomeCardGestureManager(this, screen_bounds()));
36 int GetEndCountAndReset() {
37 int result
= end_count_
;
41 int GetProgressCountAndReset() {
42 int result
= progress_count_
;
47 // Process a gesture event for our use case.
48 bool ProcessGestureEvent(ui::EventType type
, int y
) {
49 ui::GestureEvent
event(0, y
, ui::EF_NONE
, base::TimeDelta(),
50 ui::GestureEventDetails(type
, 0, (y
- last_y_
)));
51 // Assumes the initial location is based on minimized height.
52 if (type
== ui::ET_GESTURE_SCROLL_BEGIN
) {
53 gfx::Point location
= event
.location();
55 location
.y() - (screen_bounds().bottom() - kHomeCardMinimizedHeight
));
56 event
.set_location(location
);
58 gesture_manager_
->ProcessGestureEvent(&event
);
60 return event
.handled();
62 void ProcessFlingGesture(float velocity
) {
63 ui::GestureEvent
event(0, last_y_
, ui::EF_NONE
, base::TimeDelta(),
64 ui::GestureEventDetails(
65 ui::ET_SCROLL_FLING_START
, 0, velocity
));
66 gesture_manager_
->ProcessGestureEvent(&event
);
69 HomeCard::State final_state_
;
70 HomeCard::State last_from_state_
;
71 HomeCard::State last_to_state_
;
75 gfx::Rect
screen_bounds() const {
76 return gfx::Rect(0, 0, 1280, 1024);
79 // HomeCardGestureManager::Delegate:
80 virtual void OnGestureEnded(HomeCard::State final_state
) OVERRIDE
{
81 final_state_
= final_state
;
85 virtual void OnGestureProgressed(HomeCard::State from_state
,
86 HomeCard::State to_state
,
87 float progress
) OVERRIDE
{
88 last_from_state_
= from_state
;
89 last_to_state_
= to_state
;
90 last_progress_
= progress
;
97 scoped_ptr
<HomeCardGestureManager
> gesture_manager_
;
99 DISALLOW_COPY_AND_ASSIGN(HomeCardGestureManagerTest
);
102 TEST_F(HomeCardGestureManagerTest
, Basic
) {
103 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 1020));
104 EXPECT_EQ(0, GetEndCountAndReset());
105 EXPECT_EQ(0, GetProgressCountAndReset());
107 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1019);
108 EXPECT_EQ(1, GetProgressCountAndReset());
109 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, last_from_state_
);
110 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_to_state_
);
111 EXPECT_GT(1.0f
, last_progress_
);
113 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1010);
114 float progress_1010
= last_progress_
;
115 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1008);
116 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1000);
117 EXPECT_EQ(3, GetProgressCountAndReset());
118 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, last_from_state_
);
119 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_to_state_
);
120 EXPECT_LT(progress_1010
, last_progress_
);
122 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 900);
123 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 800);
124 EXPECT_EQ(2, GetProgressCountAndReset());
125 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_from_state_
);
126 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, last_to_state_
);
127 float progress_800
= last_progress_
;
128 EXPECT_GT(1.0f
, last_progress_
);
129 EXPECT_LT(0.0f
, last_progress_
);
131 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 790);
132 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_from_state_
);
133 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, last_to_state_
);
134 EXPECT_LT(progress_800
, last_progress_
);
136 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 810);
137 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, last_from_state_
);
138 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, last_to_state_
);
139 EXPECT_GT(progress_800
, (1.0f
- last_progress_
));
141 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END
, 810));
142 EXPECT_EQ(1, GetEndCountAndReset());
143 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, final_state_
);
146 TEST_F(HomeCardGestureManagerTest
, FlingUpAtEnd
) {
147 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 1020));
148 EXPECT_EQ(0, GetEndCountAndReset());
149 EXPECT_EQ(0, GetProgressCountAndReset());
151 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1010);
152 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 800);
153 ProcessFlingGesture(-150.0f
);
154 EXPECT_EQ(1, GetEndCountAndReset());
155 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, final_state_
);
158 TEST_F(HomeCardGestureManagerTest
, FlingDownAtEnd
) {
159 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 1020));
160 EXPECT_EQ(0, GetEndCountAndReset());
161 EXPECT_EQ(0, GetProgressCountAndReset());
163 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1010);
164 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 800);
165 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 200);
166 ProcessFlingGesture(150.0f
);
167 EXPECT_EQ(1, GetEndCountAndReset());
168 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, final_state_
);
171 TEST_F(HomeCardGestureManagerTest
, WeakFling
) {
172 EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN
, 1020));
173 EXPECT_EQ(0, GetEndCountAndReset());
174 EXPECT_EQ(0, GetProgressCountAndReset());
176 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 1010);
177 ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE
, 800);
178 ProcessFlingGesture(-30.0f
);
179 EXPECT_EQ(1, GetEndCountAndReset());
180 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, final_state_
);
183 } // namespace athena