1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "athena/home/home_card_gesture_manager.h"
#include "athena/home/home_card_constants.h"
#include "athena/home/public/home_card.h"
#include "athena/test/athena_test_base.h"
#include "base/time/time.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
namespace athena {
class HomeCardGestureManagerTest : public test::AthenaTestBase,
public HomeCardGestureManager::Delegate {
public:
HomeCardGestureManagerTest()
: final_state_(HomeCard::HIDDEN),
last_from_state_(HomeCard::HIDDEN),
last_to_state_(HomeCard::HIDDEN),
last_progress_(0.0f),
last_y_(0),
progress_count_(0),
end_count_(0) {}
virtual ~HomeCardGestureManagerTest() {}
// testing::Test:
virtual void SetUp() OVERRIDE {
test::AthenaTestBase::SetUp();
gesture_manager_.reset(new HomeCardGestureManager(this, screen_bounds()));
}
protected:
int GetEndCountAndReset() {
int result = end_count_;
end_count_ = 0;
return result;
}
int GetProgressCountAndReset() {
int result = progress_count_;
progress_count_ = 0;
return result;
}
// Process a gesture event for our use case.
bool ProcessGestureEvent(ui::EventType type, int y) {
ui::GestureEventDetails details;
if (type == ui::ET_GESTURE_SCROLL_BEGIN ||
type == ui::ET_GESTURE_SCROLL_UPDATE)
details = ui::GestureEventDetails(type, 0, (y - last_y_));
else
details = ui::GestureEventDetails(type);
ui::GestureEvent event(0, y, ui::EF_NONE, base::TimeDelta(), details);
if (type == ui::ET_GESTURE_SCROLL_BEGIN) {
// Compute the position that the home card would have wrt to the top of
// the screen if the screen had screen_bounds().
HomeCard::State state = HomeCard::Get()->GetState();
int home_card_top = 0;
if (state == HomeCard::VISIBLE_BOTTOM)
home_card_top = screen_bounds().height() - kHomeCardHeight;
else if (state == HomeCard::VISIBLE_MINIMIZED)
home_card_top = screen_bounds().height() - kHomeCardMinimizedHeight;
gfx::Point location = event.location();
location.set_y(location.y() - home_card_top);
event.set_location(location);
}
gesture_manager_->ProcessGestureEvent(&event);
last_y_ = y;
return event.handled();
}
void ProcessFlingGesture(float velocity) {
ui::GestureEvent event(0, last_y_, ui::EF_NONE, base::TimeDelta(),
ui::GestureEventDetails(
ui::ET_SCROLL_FLING_START, 0, velocity));
gesture_manager_->ProcessGestureEvent(&event);
}
HomeCard::State final_state_;
HomeCard::State last_from_state_;
HomeCard::State last_to_state_;
float last_progress_;
private:
gfx::Rect screen_bounds() const {
return gfx::Rect(0, 0, 1280, 1024);
}
// HomeCardGestureManager::Delegate:
virtual void OnGestureEnded(HomeCard::State final_state) OVERRIDE {
final_state_ = final_state;
++end_count_;
}
virtual void OnGestureProgressed(HomeCard::State from_state,
HomeCard::State to_state,
float progress) OVERRIDE {
last_from_state_ = from_state;
last_to_state_ = to_state;
last_progress_ = progress;
++progress_count_;
}
int last_y_;
int progress_count_;
int end_count_;
scoped_ptr<HomeCardGestureManager> gesture_manager_;
DISALLOW_COPY_AND_ASSIGN(HomeCardGestureManagerTest);
};
TEST_F(HomeCardGestureManagerTest, Basic) {
ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED, HomeCard::Get()->GetState());
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020));
EXPECT_EQ(0, GetEndCountAndReset());
EXPECT_EQ(0, GetProgressCountAndReset());
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1019);
EXPECT_EQ(1, GetProgressCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_to_state_);
EXPECT_GT(1.0f, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1020);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1022);
EXPECT_EQ(2, GetProgressCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED, last_to_state_);
EXPECT_EQ(1.0f, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1010);
float progress_1010 = last_progress_;
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1008);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1000);
EXPECT_EQ(3, GetProgressCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_to_state_);
EXPECT_LT(progress_1010, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 900);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 800);
EXPECT_EQ(2, GetProgressCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_CENTERED, last_to_state_);
float progress_800 = last_progress_;
EXPECT_GT(1.0f, last_progress_);
EXPECT_LT(0.0f, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 790);
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_CENTERED, last_to_state_);
EXPECT_LT(progress_800, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 810);
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_CENTERED, last_to_state_);
EXPECT_GT(progress_800, last_progress_);
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END, 810));
EXPECT_EQ(1, GetEndCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, final_state_);
}
// Test gesture progress when the gesture is initiated when the home card is in
// the centered state.
TEST_F(HomeCardGestureManagerTest, StartCentered) {
HomeCard::Get()->SetState(HomeCard::VISIBLE_CENTERED);
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 20));
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 10);
EXPECT_EQ(1, GetProgressCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_CENTERED, last_to_state_);
EXPECT_EQ(1.0f, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 900);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 910);
EXPECT_EQ(2, GetProgressCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_CENTERED, last_to_state_);
EXPECT_GT(1.0f, last_progress_);
EXPECT_LT(0.0f, last_progress_);
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END, 1000));
EXPECT_EQ(1, GetEndCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, final_state_);
}
TEST_F(HomeCardGestureManagerTest, FlingUpAtEnd) {
ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED, HomeCard::Get()->GetState());
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020));
EXPECT_EQ(0, GetEndCountAndReset());
EXPECT_EQ(0, GetProgressCountAndReset());
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1010);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 800);
ProcessFlingGesture(-150.0f);
EXPECT_EQ(1, GetEndCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_CENTERED, final_state_);
}
TEST_F(HomeCardGestureManagerTest, FlingDownAtEnd) {
ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED, HomeCard::Get()->GetState());
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020));
EXPECT_EQ(0, GetEndCountAndReset());
EXPECT_EQ(0, GetProgressCountAndReset());
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1010);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 800);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 200);
ProcessFlingGesture(150.0f);
EXPECT_EQ(1, GetEndCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, final_state_);
}
TEST_F(HomeCardGestureManagerTest, WeakFling) {
ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED, HomeCard::Get()->GetState());
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020));
EXPECT_EQ(0, GetEndCountAndReset());
EXPECT_EQ(0, GetProgressCountAndReset());
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1010);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 800);
ProcessFlingGesture(-30.0f);
EXPECT_EQ(1, GetEndCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, final_state_);
}
} // namespace athena
|