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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
// 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);
};
class QueueTouchEventDelegate : public GestureEventConsumeDelegate {
public:
QueueTouchEventDelegate() : window_(NULL) {}
virtual ~QueueTouchEventDelegate() {}
virtual ui::TouchStatus OnTouchEvent(TouchEvent* event) OVERRIDE {
return ui::TOUCH_STATUS_QUEUED;
}
void ReceivedAck() {
RootWindow::GetInstance()->AdvanceQueuedTouchEvent(window_, false);
}
void set_window(Window* w) { window_ = w; }
private:
Window* window_;
DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate);
};
// 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());
// Move some more to generate a few more scroll updates.
delegate->Reset();
TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(110, 211), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&move1);
EXPECT_FALSE(delegate->tap());
EXPECT_FALSE(delegate->tap_down());
EXPECT_FALSE(delegate->double_tap());
EXPECT_FALSE(delegate->scroll_begin());
EXPECT_TRUE(delegate->scroll_update());
EXPECT_FALSE(delegate->scroll_end());
EXPECT_EQ(-20, delegate->scroll_x());
EXPECT_EQ(10, delegate->scroll_y());
delegate->Reset();
TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(140, 215), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&move2);
EXPECT_FALSE(delegate->tap());
EXPECT_FALSE(delegate->tap_down());
EXPECT_FALSE(delegate->double_tap());
EXPECT_FALSE(delegate->scroll_begin());
EXPECT_TRUE(delegate->scroll_update());
EXPECT_FALSE(delegate->scroll_end());
EXPECT_EQ(30, delegate->scroll_x());
EXPECT_EQ(4, 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());
}
TEST_F(GestureRecognizerTest, GestureTapFollowedByScroll) {
// First, tap. Then, do a scroll using the same touch-id.
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());
// Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger
// a double-tap.
delegate->Reset();
TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 0);
Event::TestApi test_release1(&press1);
test_release1.set_time_stamp(release.time_stamp() +
base::TimeDelta::FromMilliseconds(1000));
RootWindow::GetInstance()->DispatchTouchEvent(&press1);
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());
// Move some more to generate a few more scroll updates.
delegate->Reset();
TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(110, 211), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&move1);
EXPECT_FALSE(delegate->tap());
EXPECT_FALSE(delegate->tap_down());
EXPECT_FALSE(delegate->double_tap());
EXPECT_FALSE(delegate->scroll_begin());
EXPECT_TRUE(delegate->scroll_update());
EXPECT_FALSE(delegate->scroll_end());
EXPECT_EQ(-20, delegate->scroll_x());
EXPECT_EQ(10, delegate->scroll_y());
delegate->Reset();
TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(140, 215), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&move2);
EXPECT_FALSE(delegate->tap());
EXPECT_FALSE(delegate->tap_down());
EXPECT_FALSE(delegate->double_tap());
EXPECT_FALSE(delegate->scroll_begin());
EXPECT_TRUE(delegate->scroll_update());
EXPECT_FALSE(delegate->scroll_end());
EXPECT_EQ(30, delegate->scroll_x());
EXPECT_EQ(4, delegate->scroll_y());
// Release the touch. This should end the scroll.
delegate->Reset();
TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&release1);
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());
}
TEST_F(GestureRecognizerTest, AsynchronousGestureRecognition) {
scoped_ptr<QueueTouchEventDelegate> queued_delegate(
new QueueTouchEventDelegate());
const int kWindowWidth = 123;
const int kWindowHeight = 45;
gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate(
queued_delegate.get(), -1234, bounds, NULL));
queued_delegate->set_window(queue.get());
// Touch down on the window. This should not generate any gesture event.
queued_delegate->Reset();
TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&press);
EXPECT_FALSE(queued_delegate->tap());
EXPECT_FALSE(queued_delegate->tap_down());
EXPECT_FALSE(queued_delegate->double_tap());
EXPECT_FALSE(queued_delegate->scroll_begin());
EXPECT_FALSE(queued_delegate->scroll_update());
EXPECT_FALSE(queued_delegate->scroll_end());
// Create another window, and place a touch-down on it. This should create a
// tap-down gesture.
scoped_ptr<GestureEventConsumeDelegate> delegate(
new GestureEventConsumeDelegate());
scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), NULL));
delegate->Reset();
TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 20), 0);
RootWindow::GetInstance()->DispatchTouchEvent(&press2);
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());
// Introduce some delay before the touch is released so that it is recognized
// as a tap. However, this still should not create any gesture events.
queued_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(queued_delegate->tap());
EXPECT_FALSE(queued_delegate->tap_down());
EXPECT_FALSE(queued_delegate->double_tap());
EXPECT_FALSE(queued_delegate->scroll_begin());
EXPECT_FALSE(queued_delegate->scroll_update());
EXPECT_FALSE(queued_delegate->scroll_end());
// Process the first queued event.
queued_delegate->Reset();
queued_delegate->ReceivedAck();
EXPECT_FALSE(queued_delegate->tap());
EXPECT_TRUE(queued_delegate->tap_down());
EXPECT_FALSE(queued_delegate->double_tap());
EXPECT_FALSE(queued_delegate->scroll_begin());
EXPECT_FALSE(queued_delegate->scroll_update());
EXPECT_FALSE(queued_delegate->scroll_end());
// Now, process the second queued event.
queued_delegate->Reset();
queued_delegate->ReceivedAck();
EXPECT_TRUE(queued_delegate->tap());
EXPECT_FALSE(queued_delegate->tap_down());
EXPECT_FALSE(queued_delegate->double_tap());
EXPECT_FALSE(queued_delegate->scroll_begin());
EXPECT_FALSE(queued_delegate->scroll_update());
EXPECT_FALSE(queued_delegate->scroll_end());
}
} // namespace test
} // namespace aura
|