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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
// Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/event.h"
#include "ui/aura/root_window.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_windows.h"
#include "ui/base/hit_test.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
namespace aura {
namespace test {
namespace {
// A delegate that keeps track of gesture events.
class GestureEventConsumeDelegate : public TestWindowDelegate {
public:
GestureEventConsumeDelegate()
: tap_(false),
tap_down_(false),
double_tap_(false),
scroll_begin_(false),
scroll_update_(false),
scroll_end_(false),
scroll_x_(0),
scroll_y_(0) {
}
virtual ~GestureEventConsumeDelegate() {}
void Reset() {
tap_ = false;
tap_down_ = false;
double_tap_ = false;
scroll_begin_ = false;
scroll_update_ = false;
scroll_end_ = false;
scroll_x_ = 0;
scroll_y_ = 0;
}
bool tap() const { return tap_; }
bool tap_down() const { return tap_down_; }
bool double_tap() const { return double_tap_; }
bool scroll_begin() const { return scroll_begin_; }
bool scroll_update() const { return scroll_update_; }
bool scroll_end() const { return scroll_end_; }
float scroll_x() const { return scroll_x_; }
float scroll_y() const { return scroll_y_; }
virtual ui::GestureStatus OnGestureEvent(GestureEvent* gesture) OVERRIDE {
switch (gesture->type()) {
case ui::ET_GESTURE_TAP:
tap_ = true;
break;
case ui::ET_GESTURE_TAP_DOWN:
tap_down_ = true;
break;
case ui::ET_GESTURE_DOUBLE_TAP:
double_tap_ = true;
break;
case ui::ET_GESTURE_SCROLL_BEGIN:
scroll_begin_ = true;
break;
case ui::ET_GESTURE_SCROLL_UPDATE:
scroll_update_ = true;
scroll_x_ += gesture->delta_x();
scroll_y_ += gesture->delta_y();
break;
case ui::ET_GESTURE_SCROLL_END:
scroll_end_ = true;
break;
default:
NOTREACHED();
}
return ui::GESTURE_STATUS_CONSUMED;
}
private:
bool tap_;
bool tap_down_;
bool double_tap_;
bool scroll_begin_;
bool scroll_update_;
bool scroll_end_;
float scroll_x_;
float scroll_y_;
DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate);
};
// A delegate that ignores gesture events but keeps track of [synthetic] mouse
// events.
class GestureEventSynthDelegate : public TestWindowDelegate {
public:
GestureEventSynthDelegate()
: mouse_enter_(false),
mouse_exit_(false),
mouse_press_(false),
mouse_release_(false),
mouse_move_(false) {
}
void Reset() {
mouse_enter_ = false;
mouse_exit_ = false;
mouse_press_ = false;
mouse_release_ = false;
mouse_move_ = false;
}
bool mouse_enter() const { return mouse_enter_; }
bool mouse_exit() const { return mouse_exit_; }
bool mouse_press() const { return mouse_press_; }
bool mouse_move() const { return mouse_move_; }
bool mouse_release() const { return mouse_release_; }
virtual bool OnMouseEvent(MouseEvent* event) OVERRIDE {
switch (event->type()) {
case ui::ET_MOUSE_PRESSED:
mouse_press_ = true;
break;
case ui::ET_MOUSE_RELEASED:
mouse_release_ = true;
break;
case ui::ET_MOUSE_MOVED:
mouse_move_ = true;
break;
case ui::ET_MOUSE_ENTERED:
mouse_enter_ = true;
break;
case ui::ET_MOUSE_EXITED:
mouse_exit_ = true;
break;
default:
NOTREACHED();
}
return true;
}
private:
bool mouse_enter_;
bool mouse_exit_;
bool mouse_press_;
bool mouse_release_;
bool mouse_move_;
DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate);
};
} // namespace
typedef AuraTestBase GestureRecognizerTest;
// Check that appropriate touch events generate tap gesture events.
TEST_F(GestureRecognizerTest, GestureEventTap) {
scoped_ptr<GestureEventConsumeDelegate> delegate(
new GestureEventConsumeDelegate());
const int kWindowWidth = 123;
const int kWindowHeight = 45;
gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
delegate.get(), -1234, bounds, NULL));
delegate->Reset();
TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&press);
EXPECT_FALSE(delegate->tap());
EXPECT_TRUE(delegate->tap_down());
EXPECT_FALSE(delegate->double_tap());
EXPECT_FALSE(delegate->scroll_begin());
EXPECT_FALSE(delegate->scroll_update());
EXPECT_FALSE(delegate->scroll_end());
// Make sure there is enough delay before the touch is released so that it is
// recognized as a tap.
delegate->Reset();
TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 0);
Event::TestApi test_release(&release);
test_release.set_time_stamp(press.time_stamp() +
base::TimeDelta::FromMilliseconds(50));
RootWindow::GetInstance()->DispatchTouchEvent(&release);
EXPECT_TRUE(delegate->tap());
EXPECT_FALSE(delegate->tap_down());
EXPECT_FALSE(delegate->double_tap());
EXPECT_FALSE(delegate->scroll_begin());
EXPECT_FALSE(delegate->scroll_update());
EXPECT_FALSE(delegate->scroll_end());
}
// Check that appropriate touch events generate scroll gesture events.
TEST_F(GestureRecognizerTest, GestureEventScroll) {
scoped_ptr<GestureEventConsumeDelegate> delegate(
new GestureEventConsumeDelegate());
const int kWindowWidth = 123;
const int kWindowHeight = 45;
gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
delegate.get(), -1234, bounds, NULL));
delegate->Reset();
TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&press);
EXPECT_FALSE(delegate->tap());
EXPECT_TRUE(delegate->tap_down());
EXPECT_FALSE(delegate->double_tap());
EXPECT_FALSE(delegate->scroll_begin());
EXPECT_FALSE(delegate->scroll_update());
EXPECT_FALSE(delegate->scroll_end());
// Move the touch-point enough so that it is considered as a scroll. This
// should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
delegate->Reset();
TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 201), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&move);
EXPECT_FALSE(delegate->tap());
EXPECT_FALSE(delegate->tap_down());
EXPECT_FALSE(delegate->double_tap());
EXPECT_TRUE(delegate->scroll_begin());
EXPECT_TRUE(delegate->scroll_update());
EXPECT_FALSE(delegate->scroll_end());
EXPECT_EQ(29, delegate->scroll_x());
EXPECT_EQ(0, delegate->scroll_y());
// Release the touch. This should end the scroll.
delegate->Reset();
TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 0);
Event::TestApi test_release(&release);
test_release.set_time_stamp(press.time_stamp() +
base::TimeDelta::FromMilliseconds(50));
RootWindow::GetInstance()->DispatchTouchEvent(&release);
EXPECT_FALSE(delegate->tap());
EXPECT_FALSE(delegate->tap_down());
EXPECT_FALSE(delegate->double_tap());
EXPECT_FALSE(delegate->scroll_begin());
EXPECT_FALSE(delegate->scroll_update());
EXPECT_TRUE(delegate->scroll_end());
}
// Check that unprocessed gesture events generate appropriate synthetic mouse
// events.
TEST_F(GestureRecognizerTest, GestureTapSyntheticMouse) {
scoped_ptr<GestureEventSynthDelegate> delegate(
new GestureEventSynthDelegate());
scoped_ptr<Window> window(CreateTestWindowWithDelegate(delegate.get(), -1234,
gfx::Rect(0, 0, 123, 45), NULL));
delegate->Reset();
GestureEvent tap(ui::ET_GESTURE_TAP, 20, 20, 0, base::Time::Now(), 0, 0);
RootWindow::GetInstance()->DispatchGestureEvent(&tap);
EXPECT_TRUE(delegate->mouse_enter());
EXPECT_TRUE(delegate->mouse_press());
EXPECT_TRUE(delegate->mouse_release());
EXPECT_TRUE(delegate->mouse_exit());
}
} // namespace test
} // namespace aura
|