Fix build break
[chromium-blink-merge.git] / ui / aura / gestures / gesture_recognizer_unittest.cc
blob1cf2fb9321067da1fbac9d69cac16872ba1bb949
1 // Copyright (c) 2012 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 "base/memory/scoped_vector.h"
6 #include "base/run_loop.h"
7 #include "base/string_number_conversions.h"
8 #include "base/timer.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/root_window.h"
12 #include "ui/aura/test/aura_test_base.h"
13 #include "ui/aura/test/event_generator.h"
14 #include "ui/aura/test/test_window_delegate.h"
15 #include "ui/aura/test/test_windows.h"
16 #include "ui/base/events/event.h"
17 #include "ui/base/events/event_utils.h"
18 #include "ui/base/gestures/gesture_configuration.h"
19 #include "ui/base/gestures/gesture_recognizer_impl.h"
20 #include "ui/base/gestures/gesture_sequence.h"
21 #include "ui/base/gestures/gesture_types.h"
22 #include "ui/base/hit_test.h"
23 #include "ui/gfx/point.h"
24 #include "ui/gfx/rect.h"
26 #include <queue>
28 namespace aura {
29 namespace test {
31 namespace {
33 std::string WindowIDAsString(ui::GestureConsumer* consumer) {
34 return consumer && !consumer->ignores_events() ?
35 base::IntToString(static_cast<Window*>(consumer)->id()) : "?";
38 #define EXPECT_0_EVENTS(events) \
39 EXPECT_EQ(0u, events.size())
41 #define EXPECT_1_EVENT(events, e0) \
42 EXPECT_EQ(1u, events.size()); \
43 EXPECT_EQ(e0, events[0])
45 #define EXPECT_2_EVENTS(events, e0, e1) \
46 EXPECT_EQ(2u, events.size()); \
47 EXPECT_EQ(e0, events[0]); \
48 EXPECT_EQ(e1, events[1])
50 #define EXPECT_3_EVENTS(events, e0, e1, e2) \
51 EXPECT_EQ(3u, events.size()); \
52 EXPECT_EQ(e0, events[0]); \
53 EXPECT_EQ(e1, events[1]); \
54 EXPECT_EQ(e2, events[2])
56 #define EXPECT_4_EVENTS(events, e0, e1, e2, e3) \
57 EXPECT_EQ(4u, events.size()); \
58 EXPECT_EQ(e0, events[0]); \
59 EXPECT_EQ(e1, events[1]); \
60 EXPECT_EQ(e2, events[2]); \
61 EXPECT_EQ(e3, events[3])
63 // A delegate that keeps track of gesture events.
64 class GestureEventConsumeDelegate : public TestWindowDelegate {
65 public:
66 GestureEventConsumeDelegate()
67 : tap_(false),
68 tap_down_(false),
69 tap_cancel_(false),
70 begin_(false),
71 end_(false),
72 scroll_begin_(false),
73 scroll_update_(false),
74 scroll_end_(false),
75 pinch_begin_(false),
76 pinch_update_(false),
77 pinch_end_(false),
78 long_press_(false),
79 fling_(false),
80 two_finger_tap_(false),
81 swipe_left_(false),
82 swipe_right_(false),
83 swipe_up_(false),
84 swipe_down_(false),
85 scroll_x_(0),
86 scroll_y_(0),
87 scroll_velocity_x_(0),
88 scroll_velocity_y_(0),
89 velocity_x_(0),
90 velocity_y_(0),
91 tap_count_(0),
92 wait_until_event_(ui::ET_UNKNOWN) {
95 virtual ~GestureEventConsumeDelegate() {}
97 void Reset() {
98 events_.clear();
99 tap_ = false;
100 tap_down_ = false;
101 tap_cancel_ = false;
102 begin_ = false;
103 end_ = false;
104 scroll_begin_ = false;
105 scroll_update_ = false;
106 scroll_end_ = false;
107 pinch_begin_ = false;
108 pinch_update_ = false;
109 pinch_end_ = false;
110 long_press_ = false;
111 fling_ = false;
112 two_finger_tap_ = false;
113 swipe_left_ = false;
114 swipe_right_ = false;
115 swipe_up_ = false;
116 swipe_down_ = false;
118 scroll_begin_position_.SetPoint(0, 0);
119 tap_location_.SetPoint(0, 0);
121 scroll_x_ = 0;
122 scroll_y_ = 0;
123 scroll_velocity_x_ = 0;
124 scroll_velocity_y_ = 0;
125 velocity_x_ = 0;
126 velocity_y_ = 0;
127 tap_count_ = 0;
130 const std::vector<ui::EventType>& events() const { return events_; };
132 bool tap() const { return tap_; }
133 bool tap_down() const { return tap_down_; }
134 bool tap_cancel() const { return tap_cancel_; }
135 bool begin() const { return begin_; }
136 bool end() const { return end_; }
137 bool scroll_begin() const { return scroll_begin_; }
138 bool scroll_update() const { return scroll_update_; }
139 bool scroll_end() const { return scroll_end_; }
140 bool pinch_begin() const { return pinch_begin_; }
141 bool pinch_update() const { return pinch_update_; }
142 bool pinch_end() const { return pinch_end_; }
143 bool long_press() const { return long_press_; }
144 bool long_tap() const { return long_tap_; }
145 bool fling() const { return fling_; }
146 bool two_finger_tap() const { return two_finger_tap_; }
147 bool swipe_left() const { return swipe_left_; }
148 bool swipe_right() const { return swipe_right_; }
149 bool swipe_up() const { return swipe_up_; }
150 bool swipe_down() const { return swipe_down_; }
152 const gfx::Point scroll_begin_position() const {
153 return scroll_begin_position_;
156 const gfx::Point tap_location() const {
157 return tap_location_;
160 float scroll_x() const { return scroll_x_; }
161 float scroll_y() const { return scroll_y_; }
162 float scroll_velocity_x() const { return scroll_velocity_x_; }
163 float scroll_velocity_y() const { return scroll_velocity_y_; }
164 int touch_id() const { return touch_id_; }
165 float velocity_x() const { return velocity_x_; }
166 float velocity_y() const { return velocity_y_; }
167 const gfx::Rect& bounding_box() const { return bounding_box_; }
168 int tap_count() const { return tap_count_; }
170 void WaitUntilReceivedGesture(ui::EventType type) {
171 wait_until_event_ = type;
172 run_loop_.reset(new base::RunLoop(
173 Env::GetInstance()->GetDispatcher()));
174 run_loop_->Run();
177 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
178 events_.push_back(gesture->type());
179 bounding_box_ = gesture->details().bounding_box();
180 switch (gesture->type()) {
181 case ui::ET_GESTURE_TAP:
182 tap_location_ = gesture->location();
183 tap_count_ = gesture->details().tap_count();
184 tap_ = true;
185 break;
186 case ui::ET_GESTURE_TAP_DOWN:
187 tap_down_ = true;
188 break;
189 case ui::ET_GESTURE_TAP_CANCEL:
190 tap_cancel_ = true;
191 break;
192 case ui::ET_GESTURE_BEGIN:
193 begin_ = true;
194 break;
195 case ui::ET_GESTURE_END:
196 end_ = true;
197 break;
198 case ui::ET_GESTURE_SCROLL_BEGIN:
199 scroll_begin_ = true;
200 scroll_begin_position_ = gesture->location();
201 break;
202 case ui::ET_GESTURE_SCROLL_UPDATE:
203 scroll_update_ = true;
204 scroll_x_ += gesture->details().scroll_x();
205 scroll_y_ += gesture->details().scroll_y();
206 scroll_velocity_x_ = gesture->details().velocity_x();
207 scroll_velocity_y_ = gesture->details().velocity_y();
208 break;
209 case ui::ET_GESTURE_SCROLL_END:
210 EXPECT_TRUE(velocity_x_ == 0 && velocity_y_ == 0);
211 scroll_end_ = true;
212 break;
213 case ui::ET_GESTURE_PINCH_BEGIN:
214 pinch_begin_ = true;
215 break;
216 case ui::ET_GESTURE_PINCH_UPDATE:
217 pinch_update_ = true;
218 break;
219 case ui::ET_GESTURE_PINCH_END:
220 pinch_end_ = true;
221 break;
222 case ui::ET_GESTURE_LONG_PRESS:
223 long_press_ = true;
224 touch_id_ = gesture->details().touch_id();
225 break;
226 case ui::ET_GESTURE_LONG_TAP:
227 long_tap_ = true;
228 break;
229 case ui::ET_SCROLL_FLING_START:
230 EXPECT_TRUE(gesture->details().velocity_x() != 0 ||
231 gesture->details().velocity_y() != 0);
232 EXPECT_FALSE(scroll_end_);
233 fling_ = true;
234 velocity_x_ = gesture->details().velocity_x();
235 velocity_y_ = gesture->details().velocity_y();
236 break;
237 case ui::ET_GESTURE_TWO_FINGER_TAP:
238 two_finger_tap_ = true;
239 break;
240 case ui::ET_GESTURE_MULTIFINGER_SWIPE:
241 swipe_left_ = gesture->details().swipe_left();
242 swipe_right_ = gesture->details().swipe_right();
243 swipe_up_ = gesture->details().swipe_up();
244 swipe_down_ = gesture->details().swipe_down();
245 break;
246 default:
247 NOTREACHED();
249 if (wait_until_event_ == gesture->type() && run_loop_.get()) {
250 run_loop_->Quit();
251 wait_until_event_ = ui::ET_UNKNOWN;
253 gesture->StopPropagation();
256 private:
257 scoped_ptr<base::RunLoop> run_loop_;
258 std::vector<ui::EventType> events_;
260 bool tap_;
261 bool tap_down_;
262 bool tap_cancel_;
263 bool begin_;
264 bool end_;
265 bool scroll_begin_;
266 bool scroll_update_;
267 bool scroll_end_;
268 bool pinch_begin_;
269 bool pinch_update_;
270 bool pinch_end_;
271 bool long_press_;
272 bool long_tap_;
273 bool fling_;
274 bool two_finger_tap_;
275 bool swipe_left_;
276 bool swipe_right_;
277 bool swipe_up_;
278 bool swipe_down_;
280 gfx::Point scroll_begin_position_;
281 gfx::Point tap_location_;
283 float scroll_x_;
284 float scroll_y_;
285 float scroll_velocity_x_;
286 float scroll_velocity_y_;
287 float velocity_x_;
288 float velocity_y_;
289 int touch_id_;
290 gfx::Rect bounding_box_;
291 int tap_count_;
293 ui::EventType wait_until_event_;
295 DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate);
298 class QueueTouchEventDelegate : public GestureEventConsumeDelegate {
299 public:
300 explicit QueueTouchEventDelegate(RootWindow* root_window)
301 : window_(NULL),
302 root_window_(root_window),
303 queue_events_(true) {
305 virtual ~QueueTouchEventDelegate() {}
307 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
308 if (queue_events_) {
309 queue_.push(new ui::TouchEvent(*event, window_, window_));
310 event->StopPropagation();
314 void ReceivedAck() {
315 ReceivedAckImpl(false);
318 void ReceivedAckPreventDefaulted() {
319 ReceivedAckImpl(true);
322 void set_window(Window* w) { window_ = w; }
323 void set_queue_events(bool queue) { queue_events_ = queue; }
325 private:
326 void ReceivedAckImpl(bool prevent_defaulted) {
327 scoped_ptr<ui::TouchEvent> event(queue_.front());
328 root_window_->ProcessedTouchEvent(event.get(), window_,
329 prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED);
330 queue_.pop();
333 std::queue<ui::TouchEvent*> queue_;
334 Window* window_;
335 RootWindow* root_window_;
336 bool queue_events_;
338 DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate);
341 // A delegate that ignores gesture events but keeps track of [synthetic] mouse
342 // events.
343 class GestureEventSynthDelegate : public TestWindowDelegate {
344 public:
345 GestureEventSynthDelegate()
346 : mouse_enter_(false),
347 mouse_exit_(false),
348 mouse_press_(false),
349 mouse_release_(false),
350 mouse_move_(false),
351 double_click_(false) {
354 void Reset() {
355 mouse_enter_ = false;
356 mouse_exit_ = false;
357 mouse_press_ = false;
358 mouse_release_ = false;
359 mouse_move_ = false;
360 double_click_ = false;
363 bool mouse_enter() const { return mouse_enter_; }
364 bool mouse_exit() const { return mouse_exit_; }
365 bool mouse_press() const { return mouse_press_; }
366 bool mouse_move() const { return mouse_move_; }
367 bool mouse_release() const { return mouse_release_; }
368 bool double_click() const { return double_click_; }
370 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
371 switch (event->type()) {
372 case ui::ET_MOUSE_PRESSED:
373 double_click_ = event->flags() & ui::EF_IS_DOUBLE_CLICK;
374 mouse_press_ = true;
375 break;
376 case ui::ET_MOUSE_RELEASED:
377 mouse_release_ = true;
378 break;
379 case ui::ET_MOUSE_MOVED:
380 mouse_move_ = true;
381 break;
382 case ui::ET_MOUSE_ENTERED:
383 mouse_enter_ = true;
384 break;
385 case ui::ET_MOUSE_EXITED:
386 mouse_exit_ = true;
387 break;
388 default:
389 NOTREACHED();
391 event->SetHandled();
394 private:
395 bool mouse_enter_;
396 bool mouse_exit_;
397 bool mouse_press_;
398 bool mouse_release_;
399 bool mouse_move_;
400 bool double_click_;
402 DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate);
405 class TestOneShotGestureSequenceTimer
406 : public base::OneShotTimer<ui::GestureSequence> {
407 public:
408 TestOneShotGestureSequenceTimer() {}
410 void ForceTimeout() {
411 if (IsRunning()) {
412 user_task().Run();
413 Stop();
417 private:
418 DISALLOW_COPY_AND_ASSIGN(TestOneShotGestureSequenceTimer);
421 class TimerTestGestureSequence : public ui::GestureSequence {
422 public:
423 explicit TimerTestGestureSequence(ui::GestureEventHelper* helper)
424 : ui::GestureSequence(helper) {
427 void ForceTimeout() {
428 static_cast<TestOneShotGestureSequenceTimer*>(
429 GetLongPressTimer())->ForceTimeout();
432 bool IsTimerRunning() {
433 return GetLongPressTimer()->IsRunning();
436 virtual base::OneShotTimer<ui::GestureSequence>* CreateTimer() OVERRIDE {
437 return new TestOneShotGestureSequenceTimer();
440 private:
441 DISALLOW_COPY_AND_ASSIGN(TimerTestGestureSequence);
444 class TestGestureRecognizer : public ui::GestureRecognizerImpl {
445 public:
446 explicit TestGestureRecognizer(RootWindow* root_window)
447 : GestureRecognizerImpl(root_window) {
450 ui::GestureSequence* GetGestureSequenceForTesting(Window* window) {
451 return GetGestureSequenceForConsumer(window);
454 private:
455 DISALLOW_COPY_AND_ASSIGN(TestGestureRecognizer);
458 class TimerTestGestureRecognizer : public TestGestureRecognizer {
459 public:
460 explicit TimerTestGestureRecognizer(RootWindow* root_window)
461 : TestGestureRecognizer(root_window) {
464 virtual ui::GestureSequence* CreateSequence(
465 ui::GestureEventHelper* helper) OVERRIDE {
466 return new TimerTestGestureSequence(helper);
469 private:
470 DISALLOW_COPY_AND_ASSIGN(TimerTestGestureRecognizer);
473 base::TimeDelta GetTime() {
474 return ui::EventTimeForNow();
477 class TimedEvents {
478 private:
479 int simulated_now_;
481 public:
482 TimedEvents() : simulated_now_(0) {
485 base::TimeDelta Now() {
486 base::TimeDelta t = base::TimeDelta::FromMilliseconds(simulated_now_);
487 simulated_now_++;
488 return t;
491 base::TimeDelta LeapForward(int time_in_millis) {
492 simulated_now_ += time_in_millis;
493 return base::TimeDelta::FromMilliseconds(simulated_now_);
496 base::TimeDelta InFuture(int time_in_millis) {
497 return base::TimeDelta::FromMilliseconds(simulated_now_ + time_in_millis);
500 void SendScrollEvents(RootWindow* root_window,
501 int x_start,
502 int y_start,
503 int dx,
504 int dy,
505 int touch_id,
506 int time_step,
507 int num_steps,
508 GestureEventConsumeDelegate* delegate) {
509 int x = x_start;
510 int y = y_start;
512 for (int i = 0; i < num_steps; i++) {
513 x += dx;
514 y += dy;
515 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
516 touch_id,
517 base::TimeDelta::FromMilliseconds(simulated_now_));
518 root_window->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
519 simulated_now_ += time_step;
523 void SendScrollEvent(RootWindow* root_window,
524 int x,
525 int y,
526 int touch_id,
527 GestureEventConsumeDelegate* delegate) {
528 delegate->Reset();
529 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
530 touch_id,
531 base::TimeDelta::FromMilliseconds(simulated_now_));
532 root_window->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
533 simulated_now_++;
537 } // namespace
539 typedef AuraTestBase GestureRecognizerTest;
541 // Check that appropriate touch events generate tap gesture events.
542 TEST_F(GestureRecognizerTest, GestureEventTap) {
543 scoped_ptr<GestureEventConsumeDelegate> delegate(
544 new GestureEventConsumeDelegate());
545 TimedEvents tes;
546 const int kWindowWidth = 123;
547 const int kWindowHeight = 45;
548 const int kTouchId = 2;
549 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
550 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
551 delegate.get(), -1234, bounds, root_window()));
553 delegate->Reset();
554 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
555 kTouchId, tes.Now());
556 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
557 EXPECT_FALSE(delegate->tap());
558 EXPECT_TRUE(delegate->tap_down());
559 EXPECT_FALSE(delegate->tap_cancel());
560 EXPECT_TRUE(delegate->begin());
561 EXPECT_FALSE(delegate->scroll_begin());
562 EXPECT_FALSE(delegate->scroll_update());
563 EXPECT_FALSE(delegate->scroll_end());
564 EXPECT_FALSE(delegate->long_press());
566 // Make sure there is enough delay before the touch is released so that it is
567 // recognized as a tap.
568 delegate->Reset();
569 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
570 kTouchId, tes.LeapForward(50));
572 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
573 EXPECT_TRUE(delegate->tap());
574 EXPECT_FALSE(delegate->tap_down());
575 EXPECT_FALSE(delegate->tap_cancel());
576 EXPECT_FALSE(delegate->begin());
577 EXPECT_TRUE(delegate->end());
578 EXPECT_FALSE(delegate->scroll_begin());
579 EXPECT_FALSE(delegate->scroll_update());
580 EXPECT_FALSE(delegate->scroll_end());
582 EXPECT_EQ(1, delegate->tap_count());
585 // Check that appropriate touch events generate tap gesture events
586 // when information about the touch radii are provided.
587 TEST_F(GestureRecognizerTest, GestureEventTapRegion) {
588 scoped_ptr<GestureEventConsumeDelegate> delegate(
589 new GestureEventConsumeDelegate());
590 TimedEvents tes;
591 const int kWindowWidth = 800;
592 const int kWindowHeight = 600;
593 const int kTouchId = 2;
594 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
595 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
596 delegate.get(), -1234, bounds, root_window()));
598 // Test with no ET_TOUCH_MOVED events.
600 delegate->Reset();
601 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
602 kTouchId, tes.Now());
603 press.set_radius_x(5);
604 press.set_radius_y(12);
605 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
606 EXPECT_FALSE(delegate->tap());
607 EXPECT_TRUE(delegate->tap_down());
608 EXPECT_FALSE(delegate->tap_cancel());
609 EXPECT_TRUE(delegate->begin());
610 EXPECT_FALSE(delegate->scroll_begin());
611 EXPECT_FALSE(delegate->scroll_update());
612 EXPECT_FALSE(delegate->scroll_end());
613 EXPECT_FALSE(delegate->long_press());
615 // Make sure there is enough delay before the touch is released so that it
616 // is recognized as a tap.
617 delegate->Reset();
618 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
619 kTouchId, tes.LeapForward(50));
620 release.set_radius_x(5);
621 release.set_radius_y(12);
623 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
624 EXPECT_TRUE(delegate->tap());
625 EXPECT_FALSE(delegate->tap_down());
626 EXPECT_FALSE(delegate->tap_cancel());
627 EXPECT_FALSE(delegate->begin());
628 EXPECT_TRUE(delegate->end());
629 EXPECT_FALSE(delegate->scroll_begin());
630 EXPECT_FALSE(delegate->scroll_update());
631 EXPECT_FALSE(delegate->scroll_end());
633 EXPECT_EQ(1, delegate->tap_count());
634 gfx::Point actual_point(delegate->tap_location());
635 EXPECT_EQ(24, delegate->bounding_box().width());
636 EXPECT_EQ(24, delegate->bounding_box().height());
637 EXPECT_EQ(101, actual_point.x());
638 EXPECT_EQ(201, actual_point.y());
641 // Test with no ET_TOUCH_MOVED events but different touch points and radii.
643 delegate->Reset();
644 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(365, 290),
645 kTouchId, tes.Now());
646 press.set_radius_x(8);
647 press.set_radius_y(14);
648 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
649 EXPECT_FALSE(delegate->tap());
650 EXPECT_TRUE(delegate->tap_down());
651 EXPECT_FALSE(delegate->tap_cancel());
652 EXPECT_TRUE(delegate->begin());
653 EXPECT_FALSE(delegate->scroll_begin());
654 EXPECT_FALSE(delegate->scroll_update());
655 EXPECT_FALSE(delegate->scroll_end());
656 EXPECT_FALSE(delegate->long_press());
658 delegate->Reset();
659 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(367, 291),
660 kTouchId, tes.LeapForward(50));
661 release.set_radius_x(20);
662 release.set_radius_y(13);
664 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
665 EXPECT_TRUE(delegate->tap());
666 EXPECT_FALSE(delegate->tap_down());
667 EXPECT_FALSE(delegate->tap_cancel());
668 EXPECT_FALSE(delegate->begin());
669 EXPECT_TRUE(delegate->end());
670 EXPECT_FALSE(delegate->scroll_begin());
671 EXPECT_FALSE(delegate->scroll_update());
672 EXPECT_FALSE(delegate->scroll_end());
674 EXPECT_EQ(1, delegate->tap_count());
675 gfx::Point actual_point(delegate->tap_location());
676 EXPECT_EQ(40, delegate->bounding_box().width());
677 EXPECT_EQ(40, delegate->bounding_box().height());
678 EXPECT_EQ(367, actual_point.x());
679 EXPECT_EQ(291, actual_point.y());
682 // Test with a single ET_TOUCH_MOVED event.
684 delegate->Reset();
685 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(46, 205),
686 kTouchId, tes.Now());
687 press.set_radius_x(6);
688 press.set_radius_y(10);
689 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
690 EXPECT_FALSE(delegate->tap());
691 EXPECT_TRUE(delegate->tap_down());
692 EXPECT_FALSE(delegate->tap_cancel());
693 EXPECT_TRUE(delegate->begin());
694 EXPECT_FALSE(delegate->tap_cancel());
695 EXPECT_FALSE(delegate->scroll_begin());
696 EXPECT_FALSE(delegate->scroll_update());
697 EXPECT_FALSE(delegate->scroll_end());
698 EXPECT_FALSE(delegate->long_press());
700 delegate->Reset();
701 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(49, 204),
702 kTouchId, tes.LeapForward(50));
703 move.set_radius_x(8);
704 move.set_radius_y(12);
705 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
706 EXPECT_FALSE(delegate->tap());
707 EXPECT_FALSE(delegate->tap_down());
708 EXPECT_FALSE(delegate->tap_cancel());
709 EXPECT_FALSE(delegate->begin());
710 EXPECT_FALSE(delegate->scroll_begin());
711 EXPECT_FALSE(delegate->scroll_update());
712 EXPECT_FALSE(delegate->scroll_end());
713 EXPECT_FALSE(delegate->long_press());
715 delegate->Reset();
716 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(49, 204),
717 kTouchId, tes.LeapForward(50));
718 release.set_radius_x(4);
719 release.set_radius_y(8);
721 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
722 EXPECT_TRUE(delegate->tap());
723 EXPECT_FALSE(delegate->tap_down());
724 EXPECT_FALSE(delegate->tap_cancel());
725 EXPECT_FALSE(delegate->begin());
726 EXPECT_TRUE(delegate->end());
727 EXPECT_FALSE(delegate->scroll_begin());
728 EXPECT_FALSE(delegate->scroll_update());
729 EXPECT_FALSE(delegate->scroll_end());
731 EXPECT_EQ(1, delegate->tap_count());
732 gfx::Point actual_point(delegate->tap_location());
733 EXPECT_EQ(25, delegate->bounding_box().width());
734 EXPECT_EQ(24, delegate->bounding_box().height());
735 EXPECT_EQ(48, actual_point.x());
736 EXPECT_EQ(204, actual_point.y());
739 // Test with a few ET_TOUCH_MOVED events.
741 delegate->Reset();
742 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(400, 150),
743 kTouchId, tes.Now());
744 press.set_radius_x(7);
745 press.set_radius_y(10);
746 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
747 EXPECT_FALSE(delegate->tap());
748 EXPECT_TRUE(delegate->tap_down());
749 EXPECT_FALSE(delegate->tap_cancel());
750 EXPECT_TRUE(delegate->begin());
751 EXPECT_FALSE(delegate->scroll_begin());
752 EXPECT_FALSE(delegate->scroll_update());
753 EXPECT_FALSE(delegate->scroll_end());
754 EXPECT_FALSE(delegate->long_press());
756 delegate->Reset();
757 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(397, 151),
758 kTouchId, tes.LeapForward(50));
759 move.set_radius_x(13);
760 move.set_radius_y(12);
761 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
762 EXPECT_FALSE(delegate->tap());
763 EXPECT_FALSE(delegate->tap_down());
764 EXPECT_FALSE(delegate->tap_cancel());
765 EXPECT_FALSE(delegate->begin());
766 EXPECT_FALSE(delegate->scroll_begin());
767 EXPECT_FALSE(delegate->scroll_update());
768 EXPECT_FALSE(delegate->scroll_end());
769 EXPECT_FALSE(delegate->long_press());
771 delegate->Reset();
772 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(397, 149),
773 kTouchId, tes.LeapForward(50));
774 move1.set_radius_x(16);
775 move1.set_radius_y(16);
776 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
777 EXPECT_FALSE(delegate->tap());
778 EXPECT_FALSE(delegate->tap_down());
779 EXPECT_FALSE(delegate->tap_cancel());
780 EXPECT_FALSE(delegate->begin());
781 EXPECT_FALSE(delegate->scroll_begin());
782 EXPECT_FALSE(delegate->scroll_update());
783 EXPECT_FALSE(delegate->scroll_end());
784 EXPECT_FALSE(delegate->long_press());
786 delegate->Reset();
787 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(400, 150),
788 kTouchId, tes.LeapForward(50));
789 move2.set_radius_x(14);
790 move2.set_radius_y(10);
791 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
792 EXPECT_FALSE(delegate->tap());
793 EXPECT_FALSE(delegate->tap_down());
794 EXPECT_FALSE(delegate->tap_cancel());
795 EXPECT_FALSE(delegate->begin());
796 EXPECT_FALSE(delegate->scroll_begin());
797 EXPECT_FALSE(delegate->scroll_update());
798 EXPECT_FALSE(delegate->scroll_end());
799 EXPECT_FALSE(delegate->long_press());
801 delegate->Reset();
802 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(401, 149),
803 kTouchId, tes.LeapForward(50));
804 release.set_radius_x(8);
805 release.set_radius_y(9);
807 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
808 EXPECT_TRUE(delegate->tap());
809 EXPECT_FALSE(delegate->tap_down());
810 EXPECT_FALSE(delegate->tap_cancel());
811 EXPECT_FALSE(delegate->begin());
812 EXPECT_TRUE(delegate->end());
813 EXPECT_FALSE(delegate->scroll_begin());
814 EXPECT_FALSE(delegate->scroll_update());
815 EXPECT_FALSE(delegate->scroll_end());
817 EXPECT_EQ(1, delegate->tap_count());
818 gfx::Point actual_point(delegate->tap_location());
819 EXPECT_EQ(33, delegate->bounding_box().width());
820 EXPECT_EQ(32, delegate->bounding_box().height());
821 EXPECT_EQ(397, actual_point.x());
822 EXPECT_EQ(149, actual_point.y());
826 // Check that appropriate touch events generate scroll gesture events.
827 TEST_F(GestureRecognizerTest, GestureEventScroll) {
828 scoped_ptr<GestureEventConsumeDelegate> delegate(
829 new GestureEventConsumeDelegate());
830 TimedEvents tes;
831 const int kWindowWidth = 123;
832 const int kWindowHeight = 45;
833 const int kTouchId = 5;
834 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
835 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
836 delegate.get(), -1234, bounds, root_window()));
838 delegate->Reset();
839 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
840 kTouchId, tes.Now());
841 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
842 EXPECT_2_EVENTS(delegate->events(),
843 ui::ET_GESTURE_BEGIN,
844 ui::ET_GESTURE_TAP_DOWN);
846 // Move the touch-point enough so that it is considered as a scroll. This
847 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
848 // The first movement is diagonal, to ensure that we have a free scroll,
849 // and not a rail scroll.
850 tes.SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
851 EXPECT_3_EVENTS(delegate->events(),
852 ui::ET_GESTURE_TAP_CANCEL,
853 ui::ET_GESTURE_SCROLL_BEGIN,
854 ui::ET_GESTURE_SCROLL_UPDATE);
855 EXPECT_EQ(29, delegate->scroll_x());
856 EXPECT_EQ(29, delegate->scroll_y());
857 EXPECT_EQ(gfx::Point(1, 1).ToString(),
858 delegate->scroll_begin_position().ToString());
860 // When scrolling with a single finger, the bounding box of the gesture should
861 // be empty, since it's a single point and the radius for testing is zero.
862 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
864 // Move some more to generate a few more scroll updates.
865 tes.SendScrollEvent(root_window(), 110, 211, kTouchId, delegate.get());
866 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
867 EXPECT_EQ(-20, delegate->scroll_x());
868 EXPECT_EQ(-19, delegate->scroll_y());
869 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
871 tes.SendScrollEvent(root_window(), 140, 215, kTouchId, delegate.get());
872 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
873 EXPECT_EQ(30, delegate->scroll_x());
874 EXPECT_EQ(4, delegate->scroll_y());
875 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
877 // Release the touch. This should end the scroll.
878 delegate->Reset();
879 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
880 kTouchId,
881 tes.LeapForward(50));
882 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
883 EXPECT_2_EVENTS(delegate->events(),
884 ui::ET_SCROLL_FLING_START,
885 ui::ET_GESTURE_END);
886 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
889 // Check that the bounding box during a scroll event is correct.
890 TEST_F(GestureRecognizerTest, GestureEventScrollBoundingBox) {
891 TimedEvents tes;
892 for (int radius = 1; radius <= 10; ++radius) {
893 ui::GestureConfiguration::set_default_radius(radius);
894 scoped_ptr<GestureEventConsumeDelegate> delegate(
895 new GestureEventConsumeDelegate());
896 const int kWindowWidth = 123;
897 const int kWindowHeight = 45;
898 const int kTouchId = 5;
899 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
900 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
901 delegate.get(), -1234, bounds, root_window()));
903 const int kPositionX = 101;
904 const int kPositionY = 201;
905 delegate->Reset();
906 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
907 gfx::Point(kPositionX, kPositionY),
908 kTouchId,
909 tes.Now());
910 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
911 EXPECT_EQ(gfx::Rect(kPositionX - radius,
912 kPositionY - radius,
913 radius * 2,
914 radius * 2).ToString(),
915 delegate->bounding_box().ToString());
917 const int kScrollAmount = 50;
918 tes.SendScrollEvents(root_window(), kPositionX, kPositionY,
919 1, 1, kTouchId, 1, kScrollAmount, delegate.get());
920 EXPECT_EQ(gfx::Point(1, 1).ToString(),
921 delegate->scroll_begin_position().ToString());
922 EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius,
923 kPositionY + kScrollAmount - radius,
924 radius * 2,
925 radius * 2).ToString(),
926 delegate->bounding_box().ToString());
928 // Release the touch. This should end the scroll.
929 delegate->Reset();
930 ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
931 gfx::Point(kPositionX + kScrollAmount,
932 kPositionY + kScrollAmount),
933 kTouchId, press.time_stamp() +
934 base::TimeDelta::FromMilliseconds(50));
935 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
936 EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius,
937 kPositionY + kScrollAmount - radius,
938 radius * 2,
939 radius * 2).ToString(),
940 delegate->bounding_box().ToString());
942 ui::GestureConfiguration::set_default_radius(0);
945 // Check Scroll End Events report correct velocities
946 // if the user was on a horizontal rail
947 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailFling) {
948 scoped_ptr<GestureEventConsumeDelegate> delegate(
949 new GestureEventConsumeDelegate());
950 TimedEvents tes;
951 const int kTouchId = 7;
952 gfx::Rect bounds(0, 0, 1000, 1000);
953 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
954 delegate.get(), -1234, bounds, root_window()));
956 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
957 kTouchId, tes.Now());
958 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
960 // Move the touch-point horizontally enough that it is considered a
961 // horizontal scroll.
962 tes.SendScrollEvent(root_window(), 20, 1, kTouchId, delegate.get());
963 EXPECT_EQ(0, delegate->scroll_y());
964 EXPECT_EQ(20, delegate->scroll_x());
966 // Get a high x velocity, while still staying on the rail
967 tes.SendScrollEvents(root_window(), 1, 1,
968 100, 10, kTouchId, 1,
969 ui::GestureConfiguration::points_buffered_for_velocity(),
970 delegate.get());
971 // The y-velocity during the scroll should be 0 since this is in a horizontal
972 // rail scroll.
973 EXPECT_GT(delegate->scroll_velocity_x(), 0);
974 EXPECT_EQ(0, delegate->scroll_velocity_y());
976 delegate->Reset();
977 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
978 kTouchId, tes.Now());
979 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
981 EXPECT_TRUE(delegate->fling());
982 EXPECT_FALSE(delegate->scroll_end());
983 EXPECT_GT(delegate->velocity_x(), 0);
984 EXPECT_EQ(0, delegate->velocity_y());
987 // Check Scroll End Events report correct velocities
988 // if the user was on a vertical rail
989 TEST_F(GestureRecognizerTest, GestureEventVerticalRailFling) {
990 scoped_ptr<GestureEventConsumeDelegate> delegate(
991 new GestureEventConsumeDelegate());
992 TimedEvents tes;
993 const int kTouchId = 7;
994 gfx::Rect bounds(0, 0, 1000, 1000);
995 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
996 delegate.get(), -1234, bounds, root_window()));
998 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
999 kTouchId, tes.Now());
1000 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1002 // Move the touch-point vertically enough that it is considered a
1003 // vertical scroll.
1004 tes.SendScrollEvent(root_window(), 1, 20, kTouchId, delegate.get());
1005 EXPECT_EQ(20, delegate->scroll_y());
1006 EXPECT_EQ(0, delegate->scroll_x());
1007 EXPECT_EQ(0, delegate->scroll_velocity_x());
1009 // Get a high y velocity, while still staying on the rail
1010 tes.SendScrollEvents(root_window(), 1, 1,
1011 10, 100, kTouchId, 1,
1012 ui::GestureConfiguration::points_buffered_for_velocity(),
1013 delegate.get());
1014 EXPECT_EQ(0, delegate->scroll_velocity_x());
1015 EXPECT_GT(delegate->scroll_velocity_y(), 0);
1017 delegate->Reset();
1018 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1019 kTouchId, tes.Now());
1020 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1022 EXPECT_TRUE(delegate->fling());
1023 EXPECT_FALSE(delegate->scroll_end());
1024 EXPECT_EQ(0, delegate->velocity_x());
1025 EXPECT_GT(delegate->velocity_y(), 0);
1028 // Check Scroll End Events reports zero velocities
1029 // if the user is not on a rail
1030 TEST_F(GestureRecognizerTest, GestureEventNonRailFling) {
1031 scoped_ptr<GestureEventConsumeDelegate> delegate(
1032 new GestureEventConsumeDelegate());
1033 TimedEvents tes;
1034 const int kTouchId = 7;
1035 gfx::Rect bounds(0, 0, 1000, 1000);
1036 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1037 delegate.get(), -1234, bounds, root_window()));
1039 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1040 kTouchId, tes.Now());
1041 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1043 // Move the touch-point such that a non-rail scroll begins
1044 tes.SendScrollEvent(root_window(), 20, 20, kTouchId, delegate.get());
1045 EXPECT_EQ(20, delegate->scroll_y());
1046 EXPECT_EQ(20, delegate->scroll_x());
1048 tes.SendScrollEvents(root_window(), 1, 1,
1049 10, 100, kTouchId, 1,
1050 ui::GestureConfiguration::points_buffered_for_velocity(),
1051 delegate.get());
1053 delegate->Reset();
1054 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1055 kTouchId, tes.Now());
1056 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1058 EXPECT_TRUE(delegate->fling());
1059 EXPECT_FALSE(delegate->scroll_end());
1060 EXPECT_GT(delegate->velocity_x(), 0);
1061 EXPECT_GT(delegate->velocity_y(), 0);
1064 // Check that appropriate touch events generate long press events
1065 TEST_F(GestureRecognizerTest, GestureEventLongPress) {
1066 scoped_ptr<GestureEventConsumeDelegate> delegate(
1067 new GestureEventConsumeDelegate());
1068 TimedEvents tes;
1069 const int kWindowWidth = 123;
1070 const int kWindowHeight = 45;
1071 const int kTouchId = 2;
1072 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1073 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1074 delegate.get(), -1234, bounds, root_window()));
1076 delegate->Reset();
1078 TimerTestGestureRecognizer* gesture_recognizer =
1079 new TimerTestGestureRecognizer(root_window());
1081 root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1083 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1084 kTouchId, tes.Now());
1085 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1086 EXPECT_TRUE(delegate->tap_down());
1087 EXPECT_TRUE(delegate->begin());
1088 EXPECT_FALSE(delegate->tap_cancel());
1090 // We haven't pressed long enough for a long press to occur
1091 EXPECT_FALSE(delegate->long_press());
1093 // Wait until the timer runs out
1094 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1095 EXPECT_TRUE(delegate->long_press());
1096 EXPECT_EQ(0, delegate->touch_id());
1097 EXPECT_FALSE(delegate->tap_cancel());
1099 delegate->Reset();
1100 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1101 kTouchId, tes.Now());
1102 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1103 EXPECT_FALSE(delegate->long_press());
1105 // Note the tap down isn't cancelled until the release
1106 EXPECT_TRUE(delegate->tap_cancel());
1109 // Check that scrolling cancels a long press
1110 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledByScroll) {
1111 scoped_ptr<GestureEventConsumeDelegate> delegate(
1112 new GestureEventConsumeDelegate());
1113 TimedEvents tes;
1114 ui::GestureConfiguration::set_long_press_time_in_seconds(.01);
1115 const int kWindowWidth = 123;
1116 const int kWindowHeight = 45;
1117 const int kTouchId = 6;
1118 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1119 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1120 delegate.get(), -1234, bounds, root_window()));
1122 delegate->Reset();
1124 TimerTestGestureRecognizer* gesture_recognizer =
1125 new TimerTestGestureRecognizer(root_window());
1126 TimerTestGestureSequence* gesture_sequence =
1127 static_cast<TimerTestGestureSequence*>(
1128 gesture_recognizer->GetGestureSequenceForTesting(window.get()));
1130 root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1132 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1133 kTouchId, tes.Now());
1134 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1135 EXPECT_TRUE(delegate->tap_down());
1137 // We haven't pressed long enough for a long press to occur
1138 EXPECT_FALSE(delegate->long_press());
1139 EXPECT_FALSE(delegate->tap_cancel());
1141 // Scroll around, to cancel the long press
1142 tes.SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
1143 // Wait until the timer runs out
1144 gesture_sequence->ForceTimeout();
1145 EXPECT_FALSE(delegate->long_press());
1146 EXPECT_TRUE(delegate->tap_cancel());
1148 delegate->Reset();
1149 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1150 kTouchId, tes.LeapForward(10));
1151 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1152 EXPECT_FALSE(delegate->long_press());
1153 EXPECT_FALSE(delegate->tap_cancel());
1156 // Check that appropriate touch events generate long tap events
1157 TEST_F(GestureRecognizerTest, GestureEventLongTap) {
1158 scoped_ptr<GestureEventConsumeDelegate> delegate(
1159 new GestureEventConsumeDelegate());
1160 TimedEvents tes;
1161 const int kWindowWidth = 123;
1162 const int kWindowHeight = 45;
1163 const int kTouchId = 2;
1164 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1165 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1166 delegate.get(), -1234, bounds, root_window()));
1168 delegate->Reset();
1170 TimerTestGestureRecognizer* gesture_recognizer =
1171 new TimerTestGestureRecognizer(root_window());
1173 root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1175 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1176 kTouchId, tes.Now());
1177 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1178 EXPECT_TRUE(delegate->tap_down());
1179 EXPECT_TRUE(delegate->begin());
1180 EXPECT_FALSE(delegate->tap_cancel());
1182 // We haven't pressed long enough for a long press to occur
1183 EXPECT_FALSE(delegate->long_press());
1185 // Wait until the timer runs out
1186 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1187 EXPECT_TRUE(delegate->long_press());
1188 EXPECT_EQ(0, delegate->touch_id());
1189 EXPECT_FALSE(delegate->tap_cancel());
1191 delegate->Reset();
1192 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1193 kTouchId, tes.Now());
1194 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1195 EXPECT_FALSE(delegate->long_press());
1196 EXPECT_TRUE(delegate->long_tap());
1198 // Note the tap down isn't cancelled until the release
1199 EXPECT_TRUE(delegate->tap_cancel());
1202 // Check that second tap cancels a long press
1203 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledBySecondTap) {
1204 scoped_ptr<GestureEventConsumeDelegate> delegate(
1205 new GestureEventConsumeDelegate());
1206 TimedEvents tes;
1207 ui::GestureConfiguration::set_long_press_time_in_seconds(.01);
1208 const int kWindowWidth = 300;
1209 const int kWindowHeight = 400;
1210 const int kTouchId1 = 8;
1211 const int kTouchId2 = 2;
1212 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1213 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1214 delegate.get(), -1234, bounds, root_window()));
1216 TimerTestGestureRecognizer* gesture_recognizer =
1217 new TimerTestGestureRecognizer(root_window());
1218 TimerTestGestureSequence* gesture_sequence =
1219 static_cast<TimerTestGestureSequence*>(
1220 gesture_recognizer->GetGestureSequenceForTesting(window.get()));
1222 root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1224 delegate->Reset();
1225 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1226 kTouchId1, tes.Now());
1227 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1228 EXPECT_TRUE(delegate->tap_down());
1229 EXPECT_TRUE(delegate->begin());
1231 // We haven't pressed long enough for a long press to occur
1232 EXPECT_FALSE(delegate->long_press());
1234 // Second tap, to cancel the long press
1235 delegate->Reset();
1236 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1237 kTouchId2, tes.Now());
1238 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1239 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
1240 EXPECT_TRUE(delegate->tap_cancel());
1241 EXPECT_TRUE(delegate->begin());
1243 // Wait until the timer runs out
1244 gesture_sequence->ForceTimeout();
1246 // No long press occurred
1247 EXPECT_FALSE(delegate->long_press());
1249 delegate->Reset();
1250 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1251 kTouchId1, tes.Now());
1252 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1253 EXPECT_FALSE(delegate->long_press());
1254 EXPECT_TRUE(delegate->two_finger_tap());
1255 EXPECT_FALSE(delegate->tap_cancel());
1258 // Check that horizontal scroll gestures cause scrolls on horizontal rails.
1259 // Also tests that horizontal rails can be broken.
1260 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailScroll) {
1261 scoped_ptr<GestureEventConsumeDelegate> delegate(
1262 new GestureEventConsumeDelegate());
1263 TimedEvents tes;
1264 const int kTouchId = 7;
1265 gfx::Rect bounds(0, 0, 1000, 1000);
1266 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1267 delegate.get(), -1234, bounds, root_window()));
1269 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1270 kTouchId, tes.Now());
1271 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1273 // Move the touch-point horizontally enough that it is considered a
1274 // horizontal scroll.
1275 tes.SendScrollEvent(root_window(), 20, 1, kTouchId, delegate.get());
1276 EXPECT_EQ(0, delegate->scroll_y());
1277 EXPECT_EQ(20, delegate->scroll_x());
1279 tes.SendScrollEvent(root_window(), 25, 6, kTouchId, delegate.get());
1280 EXPECT_TRUE(delegate->scroll_update());
1281 EXPECT_EQ(5, delegate->scroll_x());
1282 // y shouldn't change, as we're on a horizontal rail.
1283 EXPECT_EQ(0, delegate->scroll_y());
1285 // Send enough information that a velocity can be calculated for the gesture,
1286 // and we can break the rail
1287 tes.SendScrollEvents(root_window(), 1, 1,
1288 1, 100, kTouchId, 1,
1289 ui::GestureConfiguration::points_buffered_for_velocity(),
1290 delegate.get());
1291 // Since the scroll is not longer railing, the velocity should be set for both
1292 // axis.
1293 EXPECT_GT(delegate->scroll_velocity_x(), 0);
1294 EXPECT_GT(delegate->scroll_velocity_y(), 0);
1296 tes.SendScrollEvent(root_window(), 0, 0, kTouchId, delegate.get());
1297 tes.SendScrollEvent(root_window(), 5, 5, kTouchId, delegate.get());
1299 // The rail should be broken
1300 EXPECT_TRUE(delegate->scroll_update());
1301 EXPECT_EQ(5, delegate->scroll_x());
1302 EXPECT_EQ(5, delegate->scroll_y());
1305 // Check that vertical scroll gestures cause scrolls on vertical rails.
1306 // Also tests that vertical rails can be broken.
1307 TEST_F(GestureRecognizerTest, GestureEventVerticalRailScroll) {
1308 scoped_ptr<GestureEventConsumeDelegate> delegate(
1309 new GestureEventConsumeDelegate());
1310 TimedEvents tes;
1311 const int kTouchId = 7;
1312 gfx::Rect bounds(0, 0, 1000, 1000);
1313 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1314 delegate.get(), -1234, bounds, root_window()));
1316 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1317 kTouchId, tes.Now());
1318 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1320 // Move the touch-point vertically enough that it is considered a
1321 // vertical scroll.
1322 tes.SendScrollEvent(root_window(), 1, 20, kTouchId, delegate.get());
1323 EXPECT_EQ(0, delegate->scroll_x());
1324 EXPECT_EQ(20, delegate->scroll_y());
1326 tes.SendScrollEvent(root_window(), 6, 25, kTouchId, delegate.get());
1327 EXPECT_TRUE(delegate->scroll_update());
1328 EXPECT_EQ(5, delegate->scroll_y());
1329 // x shouldn't change, as we're on a vertical rail.
1330 EXPECT_EQ(0, delegate->scroll_x());
1331 EXPECT_EQ(0, delegate->scroll_velocity_x());
1333 // Send enough information that a velocity can be calculated for the gesture,
1334 // and we can break the rail
1335 tes.SendScrollEvents(root_window(), 1, 1,
1336 100, 1, kTouchId, 1,
1337 ui::GestureConfiguration::points_buffered_for_velocity(),
1338 delegate.get());
1339 EXPECT_GT(delegate->scroll_velocity_x(), 0);
1340 EXPECT_GT(delegate->scroll_velocity_y(), 0);
1342 tes.SendScrollEvent(root_window(), 0, 0, kTouchId, delegate.get());
1343 tes.SendScrollEvent(root_window(), 5, 5, kTouchId, delegate.get());
1345 // The rail should be broken
1346 EXPECT_TRUE(delegate->scroll_update());
1347 EXPECT_EQ(5, delegate->scroll_x());
1348 EXPECT_EQ(5, delegate->scroll_y());
1351 TEST_F(GestureRecognizerTest, GestureTapFollowedByScroll) {
1352 // First, tap. Then, do a scroll using the same touch-id.
1353 scoped_ptr<GestureEventConsumeDelegate> delegate(
1354 new GestureEventConsumeDelegate());
1355 TimedEvents tes;
1356 const int kWindowWidth = 123;
1357 const int kWindowHeight = 45;
1358 const int kTouchId = 3;
1359 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1360 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1361 delegate.get(), -1234, bounds, root_window()));
1363 delegate->Reset();
1364 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1365 kTouchId, tes.Now());
1366 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1367 EXPECT_FALSE(delegate->tap());
1368 EXPECT_TRUE(delegate->tap_down());
1369 EXPECT_FALSE(delegate->tap_cancel());
1370 EXPECT_FALSE(delegate->scroll_begin());
1371 EXPECT_FALSE(delegate->scroll_update());
1372 EXPECT_FALSE(delegate->scroll_end());
1374 // Make sure there is enough delay before the touch is released so that it is
1375 // recognized as a tap.
1376 delegate->Reset();
1377 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1378 kTouchId, tes.LeapForward(50));
1379 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1380 EXPECT_TRUE(delegate->tap());
1381 EXPECT_FALSE(delegate->tap_down());
1382 EXPECT_FALSE(delegate->tap_cancel());
1383 EXPECT_FALSE(delegate->scroll_begin());
1384 EXPECT_FALSE(delegate->scroll_update());
1385 EXPECT_FALSE(delegate->scroll_end());
1387 // Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger
1388 // a double-tap.
1389 delegate->Reset();
1390 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1391 kTouchId, tes.LeapForward(1000));
1392 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1393 EXPECT_FALSE(delegate->tap());
1394 EXPECT_TRUE(delegate->tap_down());
1395 EXPECT_FALSE(delegate->tap_cancel());
1396 EXPECT_FALSE(delegate->scroll_begin());
1397 EXPECT_FALSE(delegate->scroll_update());
1398 EXPECT_FALSE(delegate->scroll_end());
1400 // Move the touch-point enough so that it is considered as a scroll. This
1401 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1402 // The first movement is diagonal, to ensure that we have a free scroll,
1403 // and not a rail scroll.
1404 delegate->Reset();
1405 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 230),
1406 kTouchId, tes.Now());
1407 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
1408 EXPECT_FALSE(delegate->tap());
1409 EXPECT_FALSE(delegate->tap_down());
1410 EXPECT_TRUE(delegate->tap_cancel());
1411 EXPECT_TRUE(delegate->scroll_begin());
1412 EXPECT_TRUE(delegate->scroll_update());
1413 EXPECT_FALSE(delegate->scroll_end());
1414 EXPECT_EQ(29, delegate->scroll_x());
1415 EXPECT_EQ(29, delegate->scroll_y());
1417 // Move some more to generate a few more scroll updates.
1418 delegate->Reset();
1419 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(110, 211),
1420 kTouchId, tes.Now());
1421 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
1422 EXPECT_FALSE(delegate->tap());
1423 EXPECT_FALSE(delegate->tap_down());
1424 EXPECT_FALSE(delegate->tap_cancel());
1425 EXPECT_FALSE(delegate->scroll_begin());
1426 EXPECT_TRUE(delegate->scroll_update());
1427 EXPECT_FALSE(delegate->scroll_end());
1428 EXPECT_EQ(-20, delegate->scroll_x());
1429 EXPECT_EQ(-19, delegate->scroll_y());
1431 delegate->Reset();
1432 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(140, 215),
1433 kTouchId, tes.Now());
1434 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
1435 EXPECT_FALSE(delegate->tap());
1436 EXPECT_FALSE(delegate->tap_down());
1437 EXPECT_FALSE(delegate->tap_cancel());
1438 EXPECT_FALSE(delegate->scroll_begin());
1439 EXPECT_TRUE(delegate->scroll_update());
1440 EXPECT_FALSE(delegate->scroll_end());
1441 EXPECT_EQ(30, delegate->scroll_x());
1442 EXPECT_EQ(4, delegate->scroll_y());
1444 // Release the touch. This should end the scroll.
1445 delegate->Reset();
1446 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1447 kTouchId, tes.Now());
1448 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1449 EXPECT_FALSE(delegate->tap());
1450 EXPECT_FALSE(delegate->tap_down());
1451 EXPECT_FALSE(delegate->tap_cancel());
1452 EXPECT_FALSE(delegate->scroll_begin());
1453 EXPECT_FALSE(delegate->scroll_update());
1454 EXPECT_FALSE(delegate->scroll_end());
1455 EXPECT_TRUE(delegate->fling());
1458 TEST_F(GestureRecognizerTest, AsynchronousGestureRecognition) {
1459 scoped_ptr<QueueTouchEventDelegate> queued_delegate(
1460 new QueueTouchEventDelegate(root_window()));
1461 const int kWindowWidth = 123;
1462 const int kWindowHeight = 45;
1463 const int kTouchId1 = 6;
1464 const int kTouchId2 = 4;
1465 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1466 scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate(
1467 queued_delegate.get(), -1234, bounds, root_window()));
1469 queued_delegate->set_window(queue.get());
1471 // Touch down on the window. This should not generate any gesture event.
1472 queued_delegate->Reset();
1473 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1474 kTouchId1, GetTime());
1475 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1476 EXPECT_FALSE(queued_delegate->tap());
1477 EXPECT_FALSE(queued_delegate->tap_down());
1478 EXPECT_FALSE(queued_delegate->tap_cancel());
1479 EXPECT_FALSE(queued_delegate->begin());
1480 EXPECT_FALSE(queued_delegate->scroll_begin());
1481 EXPECT_FALSE(queued_delegate->scroll_update());
1482 EXPECT_FALSE(queued_delegate->scroll_end());
1484 // Introduce some delay before the touch is released so that it is recognized
1485 // as a tap. However, this still should not create any gesture events.
1486 queued_delegate->Reset();
1487 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1488 kTouchId1, press.time_stamp() +
1489 base::TimeDelta::FromMilliseconds(50));
1490 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1491 EXPECT_FALSE(queued_delegate->tap());
1492 EXPECT_FALSE(queued_delegate->tap_down());
1493 EXPECT_FALSE(queued_delegate->tap_cancel());
1494 EXPECT_FALSE(queued_delegate->begin());
1495 EXPECT_FALSE(queued_delegate->end());
1496 EXPECT_FALSE(queued_delegate->scroll_begin());
1497 EXPECT_FALSE(queued_delegate->scroll_update());
1498 EXPECT_FALSE(queued_delegate->scroll_end());
1500 // Create another window, and place a touch-down on it. This should create a
1501 // tap-down gesture.
1502 scoped_ptr<GestureEventConsumeDelegate> delegate(
1503 new GestureEventConsumeDelegate());
1504 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1505 delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), root_window()));
1506 delegate->Reset();
1507 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 20),
1508 kTouchId2, GetTime());
1509 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1510 EXPECT_FALSE(delegate->tap());
1511 EXPECT_TRUE(delegate->tap_down());
1512 EXPECT_FALSE(delegate->tap_cancel());
1513 EXPECT_FALSE(queued_delegate->begin());
1514 EXPECT_FALSE(queued_delegate->end());
1515 EXPECT_FALSE(delegate->scroll_begin());
1516 EXPECT_FALSE(delegate->scroll_update());
1517 EXPECT_FALSE(delegate->scroll_end());
1519 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(10, 20),
1520 kTouchId2, GetTime());
1521 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
1523 // Process the first queued event.
1524 queued_delegate->Reset();
1525 queued_delegate->ReceivedAck();
1526 EXPECT_FALSE(queued_delegate->tap());
1527 EXPECT_TRUE(queued_delegate->tap_down());
1528 EXPECT_TRUE(queued_delegate->begin());
1529 EXPECT_FALSE(queued_delegate->tap_cancel());
1530 EXPECT_FALSE(queued_delegate->end());
1531 EXPECT_FALSE(queued_delegate->scroll_begin());
1532 EXPECT_FALSE(queued_delegate->scroll_update());
1533 EXPECT_FALSE(queued_delegate->scroll_end());
1535 // Now, process the second queued event.
1536 queued_delegate->Reset();
1537 queued_delegate->ReceivedAck();
1538 EXPECT_TRUE(queued_delegate->tap());
1539 EXPECT_FALSE(queued_delegate->tap_down());
1540 EXPECT_FALSE(queued_delegate->tap_cancel());
1541 EXPECT_FALSE(queued_delegate->begin());
1542 EXPECT_TRUE(queued_delegate->end());
1543 EXPECT_FALSE(queued_delegate->scroll_begin());
1544 EXPECT_FALSE(queued_delegate->scroll_update());
1545 EXPECT_FALSE(queued_delegate->scroll_end());
1547 // Start all over. Press on the first window, then press again on the second
1548 // window. The second press should still go to the first window.
1549 queued_delegate->Reset();
1550 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1551 kTouchId1, GetTime());
1552 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press3);
1553 EXPECT_FALSE(queued_delegate->tap());
1554 EXPECT_FALSE(queued_delegate->tap_down());
1555 EXPECT_FALSE(queued_delegate->tap_cancel());
1556 EXPECT_FALSE(queued_delegate->begin());
1557 EXPECT_FALSE(queued_delegate->end());
1558 EXPECT_FALSE(queued_delegate->begin());
1559 EXPECT_FALSE(queued_delegate->end());
1560 EXPECT_FALSE(queued_delegate->scroll_begin());
1561 EXPECT_FALSE(queued_delegate->scroll_update());
1562 EXPECT_FALSE(queued_delegate->scroll_end());
1564 queued_delegate->Reset();
1565 delegate->Reset();
1566 ui::TouchEvent press4(ui::ET_TOUCH_PRESSED, gfx::Point(103, 203),
1567 kTouchId2, GetTime());
1568 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press4);
1569 EXPECT_FALSE(delegate->tap());
1570 EXPECT_FALSE(delegate->tap_down());
1571 EXPECT_FALSE(delegate->tap_cancel());
1572 EXPECT_FALSE(delegate->begin());
1573 EXPECT_FALSE(delegate->end());
1574 EXPECT_FALSE(delegate->scroll_begin());
1575 EXPECT_FALSE(delegate->scroll_update());
1576 EXPECT_FALSE(delegate->scroll_end());
1577 EXPECT_FALSE(queued_delegate->tap());
1578 EXPECT_FALSE(queued_delegate->tap_down());
1579 EXPECT_FALSE(queued_delegate->tap_cancel());
1580 EXPECT_FALSE(queued_delegate->begin());
1581 EXPECT_FALSE(queued_delegate->end());
1582 EXPECT_FALSE(queued_delegate->scroll_begin());
1583 EXPECT_FALSE(queued_delegate->scroll_update());
1584 EXPECT_FALSE(queued_delegate->scroll_end());
1586 // Move the second touch-point enough so that it is considered a pinch. This
1587 // should generate both SCROLL_BEGIN and PINCH_BEGIN gestures.
1588 queued_delegate->Reset();
1589 delegate->Reset();
1590 int x_move = ui::GestureConfiguration::max_touch_move_in_pixels_for_click();
1591 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103 + x_move, 203),
1592 kTouchId2, GetTime());
1593 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
1594 EXPECT_FALSE(delegate->tap());
1595 EXPECT_FALSE(delegate->tap_down());
1596 EXPECT_FALSE(delegate->tap_cancel());
1597 EXPECT_FALSE(delegate->begin());
1598 EXPECT_FALSE(delegate->scroll_begin());
1599 EXPECT_FALSE(delegate->scroll_update());
1600 EXPECT_FALSE(delegate->scroll_end());
1601 EXPECT_FALSE(queued_delegate->tap());
1602 EXPECT_FALSE(queued_delegate->tap_down());
1603 EXPECT_FALSE(queued_delegate->tap_cancel());
1604 EXPECT_FALSE(queued_delegate->begin());
1605 EXPECT_FALSE(queued_delegate->scroll_begin());
1606 EXPECT_FALSE(queued_delegate->scroll_update());
1607 EXPECT_FALSE(queued_delegate->scroll_end());
1609 queued_delegate->Reset();
1610 queued_delegate->ReceivedAck();
1611 EXPECT_FALSE(queued_delegate->tap());
1612 EXPECT_TRUE(queued_delegate->tap_down());
1613 EXPECT_TRUE(queued_delegate->begin());
1614 EXPECT_FALSE(queued_delegate->tap_cancel());
1615 EXPECT_FALSE(queued_delegate->end());
1616 EXPECT_FALSE(queued_delegate->scroll_begin());
1617 EXPECT_FALSE(queued_delegate->scroll_update());
1618 EXPECT_FALSE(queued_delegate->scroll_end());
1620 queued_delegate->Reset();
1621 queued_delegate->ReceivedAck();
1622 EXPECT_FALSE(queued_delegate->tap());
1623 EXPECT_FALSE(queued_delegate->tap_down()); // no touch down for second tap.
1624 EXPECT_TRUE(queued_delegate->tap_cancel());
1625 EXPECT_TRUE(queued_delegate->begin());
1626 EXPECT_FALSE(queued_delegate->end());
1627 EXPECT_FALSE(queued_delegate->scroll_begin());
1628 EXPECT_FALSE(queued_delegate->scroll_update());
1629 EXPECT_FALSE(queued_delegate->scroll_end());
1630 EXPECT_FALSE(queued_delegate->pinch_begin());
1631 EXPECT_FALSE(queued_delegate->pinch_update());
1632 EXPECT_FALSE(queued_delegate->pinch_end());
1634 queued_delegate->Reset();
1635 queued_delegate->ReceivedAck();
1636 EXPECT_FALSE(queued_delegate->tap());
1637 EXPECT_FALSE(queued_delegate->tap_down());
1638 EXPECT_FALSE(queued_delegate->tap_cancel());
1639 EXPECT_FALSE(queued_delegate->begin());
1640 EXPECT_FALSE(queued_delegate->end());
1641 EXPECT_TRUE(queued_delegate->scroll_begin());
1642 EXPECT_FALSE(queued_delegate->scroll_update());
1643 EXPECT_FALSE(queued_delegate->scroll_end());
1644 EXPECT_TRUE(queued_delegate->pinch_begin());
1645 EXPECT_FALSE(queued_delegate->pinch_update());
1646 EXPECT_FALSE(queued_delegate->pinch_end());
1649 // Check that appropriate touch events generate pinch gesture events.
1650 TEST_F(GestureRecognizerTest, GestureEventPinchFromScroll) {
1651 scoped_ptr<GestureEventConsumeDelegate> delegate(
1652 new GestureEventConsumeDelegate());
1653 TimedEvents tes;
1654 const int kWindowWidth = 300;
1655 const int kWindowHeight = 400;
1656 const int kTouchId1 = 5;
1657 const int kTouchId2 = 3;
1658 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1659 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1660 delegate.get(), -1234, bounds, root_window()));
1662 aura::RootWindow* root = root_window();
1664 delegate->Reset();
1665 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1666 kTouchId1, tes.Now());
1667 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1668 EXPECT_2_EVENTS(delegate->events(),
1669 ui::ET_GESTURE_BEGIN,
1670 ui::ET_GESTURE_TAP_DOWN);
1672 // Move the touch-point enough so that it is considered as a scroll. This
1673 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1674 delegate->Reset();
1675 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 301),
1676 kTouchId1, tes.Now());
1677 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
1678 EXPECT_3_EVENTS(delegate->events(),
1679 ui::ET_GESTURE_TAP_CANCEL,
1680 ui::ET_GESTURE_SCROLL_BEGIN,
1681 ui::ET_GESTURE_SCROLL_UPDATE);
1683 // Press the second finger. It should cause pinch-begin. Note that we will not
1684 // transition to two finger tap here because the touch points are far enough.
1685 delegate->Reset();
1686 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1687 kTouchId2, tes.Now());
1688 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1689 EXPECT_2_EVENTS(delegate->events(),
1690 ui::ET_GESTURE_BEGIN,
1691 ui::ET_GESTURE_PINCH_BEGIN);
1692 EXPECT_EQ(gfx::Rect(10, 10, 120, 291).ToString(),
1693 delegate->bounding_box().ToString());
1695 // Move the first finger.
1696 delegate->Reset();
1697 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(95, 201),
1698 kTouchId1, tes.Now());
1699 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move3);
1700 EXPECT_2_EVENTS(delegate->events(),
1701 ui::ET_GESTURE_PINCH_UPDATE,
1702 ui::ET_GESTURE_SCROLL_UPDATE);
1703 EXPECT_EQ(gfx::Rect(10, 10, 85, 191).ToString(),
1704 delegate->bounding_box().ToString());
1706 // Now move the second finger.
1707 delegate->Reset();
1708 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
1709 kTouchId2, tes.Now());
1710 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move4);
1711 EXPECT_2_EVENTS(delegate->events(),
1712 ui::ET_GESTURE_PINCH_UPDATE,
1713 ui::ET_GESTURE_SCROLL_UPDATE);
1714 EXPECT_EQ(gfx::Rect(55, 15, 40, 186).ToString(),
1715 delegate->bounding_box().ToString());
1717 // Release the first finger. This should end pinch.
1718 delegate->Reset();
1719 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1720 kTouchId1, tes.Now());
1721 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1722 EXPECT_2_EVENTS(delegate->events(),
1723 ui::ET_GESTURE_PINCH_END,
1724 ui::ET_GESTURE_END);
1725 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
1726 delegate->bounding_box().ToString());
1728 // Move the second finger. This should still generate a scroll.
1729 delegate->Reset();
1730 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
1731 kTouchId2, tes.Now());
1732 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move5);
1733 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1734 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1737 TEST_F(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) {
1738 scoped_ptr<GestureEventConsumeDelegate> delegate(
1739 new GestureEventConsumeDelegate());
1740 TimedEvents tes;
1741 const int kWindowWidth = 300;
1742 const int kWindowHeight = 400;
1743 const int kTouchId1 = 5;
1744 const int kTouchId2 = 3;
1745 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1746 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1747 delegate.get(), -1234, bounds, root_window()));
1749 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
1750 kTouchId1, tes.Now());
1751 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1752 delegate->Reset();
1753 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1754 kTouchId2, tes.Now());
1755 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1756 // Since the touch points are far enough we will go to pinch rather than two
1757 // finger tap.
1758 EXPECT_TRUE(delegate->pinch_begin());
1760 tes.SendScrollEvent(root_window(), 130, 230, kTouchId1, delegate.get());
1761 EXPECT_TRUE(delegate->pinch_update());
1763 // Pinch has started, now release the second finger
1764 delegate->Reset();
1765 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1766 kTouchId1, tes.Now());
1767 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1768 EXPECT_TRUE(delegate->pinch_end());
1770 tes.SendScrollEvent(root_window(), 130, 230, kTouchId2, delegate.get());
1771 EXPECT_TRUE(delegate->scroll_update());
1773 // Pinch again
1774 delegate->Reset();
1775 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1776 kTouchId1, tes.Now());
1777 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press3);
1778 // Now the touch points are close. So we will go into two finger tap.
1779 // Move the touch-point enough to break two-finger-tap and enter pinch.
1780 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 202),
1781 kTouchId1, tes.Now());
1782 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
1783 EXPECT_TRUE(delegate->pinch_begin());
1785 tes.SendScrollEvent(root_window(), 130, 230, kTouchId1, delegate.get());
1786 EXPECT_TRUE(delegate->pinch_update());
1789 TEST_F(GestureRecognizerTest, GestureEventPinchFromTap) {
1790 scoped_ptr<GestureEventConsumeDelegate> delegate(
1791 new GestureEventConsumeDelegate());
1792 TimedEvents tes;
1793 const int kWindowWidth = 300;
1794 const int kWindowHeight = 400;
1795 const int kTouchId1 = 3;
1796 const int kTouchId2 = 5;
1797 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1798 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1799 delegate.get(), -1234, bounds, root_window()));
1801 aura::RootWindow* root = root_window();
1803 delegate->Reset();
1804 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
1805 kTouchId1, tes.Now());
1806 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1807 EXPECT_2_EVENTS(delegate->events(),
1808 ui::ET_GESTURE_BEGIN,
1809 ui::ET_GESTURE_TAP_DOWN);
1810 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1812 // Press the second finger far enough to break two finger tap. It should
1813 // instead cause a scroll-begin and pinch-begin.
1814 delegate->Reset();
1815 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1816 kTouchId2, tes.Now());
1817 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1818 EXPECT_4_EVENTS(delegate->events(),
1819 ui::ET_GESTURE_TAP_CANCEL,
1820 ui::ET_GESTURE_BEGIN,
1821 ui::ET_GESTURE_PINCH_BEGIN,
1822 ui::ET_GESTURE_SCROLL_BEGIN);
1823 EXPECT_EQ(gfx::Rect(10, 10, 91, 291).ToString(),
1824 delegate->bounding_box().ToString());
1826 // Move the first finger.
1827 delegate->Reset();
1828 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
1829 kTouchId1, tes.Now());
1830 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move3);
1831 EXPECT_2_EVENTS(delegate->events(),
1832 ui::ET_GESTURE_PINCH_UPDATE,
1833 ui::ET_GESTURE_SCROLL_UPDATE);
1834 EXPECT_EQ(gfx::Rect(10, 10, 55, 191).ToString(),
1835 delegate->bounding_box().ToString());
1837 // Now move the second finger.
1838 delegate->Reset();
1839 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
1840 kTouchId2, tes.Now());
1841 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move4);
1842 EXPECT_2_EVENTS(delegate->events(),
1843 ui::ET_GESTURE_PINCH_UPDATE,
1844 ui::ET_GESTURE_SCROLL_UPDATE);
1845 EXPECT_EQ(gfx::Rect(55, 15, 10, 186).ToString(),
1846 delegate->bounding_box().ToString());
1848 // Release the first finger. This should end pinch.
1849 delegate->Reset();
1850 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1851 kTouchId1, tes.LeapForward(10));
1852 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1853 EXPECT_2_EVENTS(delegate->events(),
1854 ui::ET_GESTURE_PINCH_END,
1855 ui::ET_GESTURE_END);
1856 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
1857 delegate->bounding_box().ToString());
1859 // Move the second finger. This should still generate a scroll.
1860 delegate->Reset();
1861 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
1862 kTouchId2, tes.Now());
1863 root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move5);
1864 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1865 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1868 TEST_F(GestureRecognizerTest, GestureEventIgnoresDisconnectedEvents) {
1869 scoped_ptr<GestureEventConsumeDelegate> delegate(
1870 new GestureEventConsumeDelegate());
1871 TimedEvents tes;
1873 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1874 6, tes.Now());
1875 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1876 EXPECT_FALSE(delegate->tap());
1877 EXPECT_FALSE(delegate->tap_down());
1880 // Check that a touch is locked to the window of the closest current touch
1881 // within max_separation_for_gesture_touches_in_pixels
1882 TEST_F(GestureRecognizerTest, GestureEventTouchLockSelectsCorrectWindow) {
1883 ui::GestureRecognizer* gesture_recognizer =
1884 new ui::GestureRecognizerImpl(root_window());
1885 TimedEvents tes;
1886 root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1888 ui::GestureConsumer* target;
1889 const int kNumWindows = 4;
1891 scoped_ptr<GestureEventConsumeDelegate*[]> delegates(
1892 new GestureEventConsumeDelegate*[kNumWindows]);
1894 ui::GestureConfiguration::
1895 set_max_separation_for_gesture_touches_in_pixels(499);
1897 scoped_ptr<gfx::Rect[]> window_bounds(new gfx::Rect[kNumWindows]);
1898 window_bounds[0] = gfx::Rect(0, 0, 1, 1);
1899 window_bounds[1] = gfx::Rect(500, 0, 1, 1);
1900 window_bounds[2] = gfx::Rect(0, 500, 1, 1);
1901 window_bounds[3] = gfx::Rect(500, 500, 1, 1);
1903 scoped_ptr<aura::Window*[]> windows(new aura::Window*[kNumWindows]);
1905 // Instantiate windows with |window_bounds| and touch each window at
1906 // its origin.
1907 for (int i = 0; i < kNumWindows; ++i) {
1908 delegates[i] = new GestureEventConsumeDelegate();
1909 windows[i] = CreateTestWindowWithDelegate(
1910 delegates[i], i, window_bounds[i], root_window());
1911 windows[i]->set_id(i);
1912 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, window_bounds[i].origin(),
1913 i, tes.Now());
1914 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1917 // Touches should now be associated with the closest touch within
1918 // ui::GestureConfiguration::max_separation_for_gesture_touches_in_pixels
1919 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 11));
1920 EXPECT_EQ("0", WindowIDAsString(target));
1921 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 11));
1922 EXPECT_EQ("1", WindowIDAsString(target));
1923 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 511));
1924 EXPECT_EQ("2", WindowIDAsString(target));
1925 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 511));
1926 EXPECT_EQ("3", WindowIDAsString(target));
1928 // Add a touch in the middle associated with windows[2]
1929 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 500),
1930 kNumWindows, tes.Now());
1931 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1932 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(250, 250),
1933 kNumWindows, tes.Now());
1934 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
1936 target = gesture_recognizer->GetTargetForLocation(gfx::Point(250, 250));
1937 EXPECT_EQ("2", WindowIDAsString(target));
1939 // Make sure that ties are broken by distance to a current touch
1940 // Closer to the point in the bottom right.
1941 target = gesture_recognizer->GetTargetForLocation(gfx::Point(380, 380));
1942 EXPECT_EQ("3", WindowIDAsString(target));
1944 // This touch is closer to the point in the middle
1945 target = gesture_recognizer->GetTargetForLocation(gfx::Point(300, 300));
1946 EXPECT_EQ("2", WindowIDAsString(target));
1948 // A touch too far from other touches won't be locked to anything
1949 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000));
1950 EXPECT_TRUE(target == NULL);
1952 // Move a touch associated with windows[2] to 1000, 1000
1953 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(1000, 1000),
1954 kNumWindows, tes.Now());
1955 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
1957 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000));
1958 EXPECT_EQ("2", WindowIDAsString(target));
1960 for (int i = 0; i < kNumWindows; ++i) {
1961 // Delete windows before deleting delegates.
1962 delete windows[i];
1963 delete delegates[i];
1967 // Check that touch events outside the root window are still handled
1968 // by the root window's gesture sequence.
1969 TEST_F(GestureRecognizerTest, GestureEventOutsideRootWindowTap) {
1970 TestGestureRecognizer* gesture_recognizer =
1971 new TestGestureRecognizer(root_window());
1972 TimedEvents tes;
1973 root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1975 scoped_ptr<aura::Window> window(CreateTestWindowWithBounds(
1976 gfx::Rect(-100, -100, 2000, 2000), root_window()));
1978 ui::GestureSequence* window_gesture_sequence =
1979 gesture_recognizer->GetGestureSequenceForTesting(window.get());
1981 ui::GestureSequence* root_window_gesture_sequence =
1982 gesture_recognizer->GetGestureSequenceForTesting(root_window());
1984 gfx::Point pos1(-10, -10);
1985 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, pos1, 0, tes.Now());
1986 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1988 gfx::Point pos2(1000, 1000);
1989 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, pos2, 1, tes.Now());
1990 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1992 // As these presses were outside the root window, they should be
1993 // associated with the root window.
1994 EXPECT_EQ(0, window_gesture_sequence->point_count());
1995 EXPECT_EQ(2, root_window_gesture_sequence->point_count());
1998 TEST_F(GestureRecognizerTest, NoTapWithPreventDefaultedRelease) {
1999 scoped_ptr<QueueTouchEventDelegate> delegate(
2000 new QueueTouchEventDelegate(root_window()));
2001 TimedEvents tes;
2002 const int kTouchId = 2;
2003 gfx::Rect bounds(100, 200, 100, 100);
2004 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2005 delegate.get(), -1234, bounds, root_window()));
2006 delegate->set_window(window.get());
2008 delegate->Reset();
2009 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2010 kTouchId, tes.Now());
2011 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2012 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2013 kTouchId, tes.LeapForward(50));
2014 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2016 delegate->Reset();
2017 delegate->ReceivedAck();
2018 EXPECT_TRUE(delegate->tap_down());
2019 delegate->Reset();
2020 delegate->ReceivedAckPreventDefaulted();
2021 EXPECT_FALSE(delegate->tap());
2022 EXPECT_TRUE(delegate->tap_cancel());
2025 TEST_F(GestureRecognizerTest, PinchScrollWithPreventDefaultedRelease) {
2026 scoped_ptr<QueueTouchEventDelegate> delegate(
2027 new QueueTouchEventDelegate(root_window()));
2028 TimedEvents tes;
2029 const int kTouchId1 = 7;
2030 const int kTouchId2 = 5;
2031 gfx::Rect bounds(10, 20, 100, 100);
2032 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2033 delegate.get(), -1234, bounds, root_window()));
2034 delegate->set_window(window.get());
2037 delegate->Reset();
2038 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2039 tes.Now());
2040 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2041 tes.LeapForward(200));
2042 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2043 tes.LeapForward(50));
2044 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2045 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
2046 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2047 delegate->Reset();
2049 // Ack the press event.
2050 delegate->ReceivedAck();
2051 EXPECT_TRUE(delegate->tap_down());
2052 delegate->Reset();
2054 // Ack the move event.
2055 delegate->ReceivedAck();
2056 EXPECT_TRUE(delegate->tap_cancel());
2057 EXPECT_TRUE(delegate->scroll_begin());
2058 delegate->Reset();
2060 // Ack the release event. Although the release event has been processed, it
2061 // should still generate a scroll-end event.
2062 delegate->ReceivedAckPreventDefaulted();
2063 EXPECT_TRUE(delegate->scroll_end());
2066 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2067 tes.Now());
2068 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2069 tes.LeapForward(200));
2070 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2071 tes.LeapForward(50));
2072 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 25), kTouchId2,
2073 tes.Now());
2074 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(45, 85), kTouchId2,
2075 tes.LeapForward(1000));
2076 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(45, 85), kTouchId2,
2077 tes.LeapForward(14));
2079 // Do a pinch.
2080 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2081 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
2082 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2083 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
2084 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2085 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2087 // Ack the press and move events.
2088 delegate->Reset();
2089 delegate->ReceivedAck();
2090 EXPECT_TRUE(delegate->begin());
2091 EXPECT_TRUE(delegate->tap_down());
2093 delegate->Reset();
2094 delegate->ReceivedAck();
2095 EXPECT_TRUE(delegate->scroll_begin());
2097 delegate->Reset();
2098 delegate->ReceivedAck();
2099 EXPECT_TRUE(delegate->begin());
2100 EXPECT_FALSE(delegate->pinch_begin());
2102 delegate->Reset();
2103 delegate->ReceivedAck();
2104 EXPECT_TRUE(delegate->pinch_begin());
2106 // Ack the first release. Although the release is processed, it should still
2107 // generate a pinch-end event.
2108 delegate->Reset();
2109 delegate->ReceivedAckPreventDefaulted();
2110 EXPECT_TRUE(delegate->pinch_end());
2111 EXPECT_TRUE(delegate->end());
2113 delegate->Reset();
2114 delegate->ReceivedAckPreventDefaulted();
2115 EXPECT_TRUE(delegate->scroll_end());
2116 EXPECT_TRUE(delegate->end());
2119 TEST_F(GestureRecognizerTest, CaptureSendsGestureEnd) {
2120 scoped_ptr<GestureEventConsumeDelegate> delegate(
2121 new GestureEventConsumeDelegate());
2122 TestGestureRecognizer* gesture_recognizer =
2123 new TestGestureRecognizer(root_window());
2124 root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
2126 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2127 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2128 EventGenerator generator(root_window());
2130 generator.MoveMouseRelativeTo(window.get(), gfx::Point(10, 10));
2131 generator.PressTouch();
2132 RunAllPendingInMessageLoop();
2134 EXPECT_TRUE(delegate->tap_down());
2136 scoped_ptr<aura::Window> capture(CreateTestWindowWithBounds(
2137 gfx::Rect(10, 10, 200, 200), root_window()));
2138 capture->SetCapture();
2139 RunAllPendingInMessageLoop();
2141 EXPECT_TRUE(delegate->end());
2142 EXPECT_TRUE(delegate->tap_cancel());
2145 TEST_F(GestureRecognizerTest, PressDoesNotCrash) {
2146 scoped_ptr<GestureEventConsumeDelegate> delegate(
2147 new GestureEventConsumeDelegate());
2148 TestGestureRecognizer* gesture_recognizer =
2149 new TestGestureRecognizer(root_window());
2150 root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
2151 TimedEvents tes;
2153 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2154 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2156 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(45, 45), 7, tes.Now());
2157 press.set_radius_x(40);
2158 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2159 EXPECT_TRUE(delegate->tap_down());
2160 EXPECT_EQ(gfx::Rect(5, 5, 80, 80).ToString(),
2161 delegate->bounding_box().ToString());
2162 delegate->Reset();
2164 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 45), 7, tes.Now());
2165 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2167 // This new press should not generate a tap-down.
2168 EXPECT_FALSE(delegate->begin());
2169 EXPECT_FALSE(delegate->tap_down());
2170 EXPECT_FALSE(delegate->tap_cancel());
2171 EXPECT_FALSE(delegate->scroll_begin());
2174 TEST_F(GestureRecognizerTest, TwoFingerTap) {
2175 scoped_ptr<GestureEventConsumeDelegate> delegate(
2176 new GestureEventConsumeDelegate());
2177 const int kWindowWidth = 123;
2178 const int kWindowHeight = 45;
2179 const int kTouchId1 = 2;
2180 const int kTouchId2 = 3;
2181 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2182 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2183 delegate.get(), -1234, bounds, root_window()));
2184 TimedEvents tes;
2186 delegate->Reset();
2187 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2188 kTouchId1, tes.Now());
2189 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2190 EXPECT_FALSE(delegate->tap());
2191 EXPECT_TRUE(delegate->tap_down());
2192 EXPECT_FALSE(delegate->tap_cancel());
2193 EXPECT_FALSE(delegate->scroll_begin());
2194 EXPECT_FALSE(delegate->scroll_update());
2195 EXPECT_FALSE(delegate->scroll_end());
2196 EXPECT_FALSE(delegate->long_press());
2197 EXPECT_FALSE(delegate->two_finger_tap());
2199 delegate->Reset();
2200 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2201 kTouchId2, tes.Now());
2202 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2203 EXPECT_FALSE(delegate->tap());
2204 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
2205 EXPECT_TRUE(delegate->tap_cancel());
2206 EXPECT_FALSE(delegate->scroll_begin());
2207 EXPECT_FALSE(delegate->scroll_update());
2208 EXPECT_FALSE(delegate->scroll_end());
2209 EXPECT_FALSE(delegate->long_press());
2210 EXPECT_FALSE(delegate->two_finger_tap());
2212 // Little bit of touch move should not affect our state.
2213 delegate->Reset();
2214 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(102, 202),
2215 kTouchId1, tes.Now());
2216 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
2217 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(131, 202),
2218 kTouchId2, tes.Now());
2219 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
2220 EXPECT_FALSE(delegate->tap());
2221 EXPECT_FALSE(delegate->tap_down());
2222 EXPECT_FALSE(delegate->tap_cancel());
2223 EXPECT_FALSE(delegate->scroll_begin());
2224 EXPECT_FALSE(delegate->scroll_update());
2225 EXPECT_FALSE(delegate->scroll_end());
2226 EXPECT_FALSE(delegate->long_press());
2227 EXPECT_FALSE(delegate->two_finger_tap());
2229 // Make sure there is enough delay before the touch is released so that it is
2230 // recognized as a tap.
2231 delegate->Reset();
2232 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2233 kTouchId1, tes.LeapForward(50));
2235 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2236 EXPECT_FALSE(delegate->tap());
2237 EXPECT_FALSE(delegate->tap_down());
2238 EXPECT_FALSE(delegate->tap_cancel());
2239 EXPECT_FALSE(delegate->scroll_begin());
2240 EXPECT_FALSE(delegate->scroll_update());
2241 EXPECT_FALSE(delegate->scroll_end());
2242 EXPECT_TRUE(delegate->two_finger_tap());
2244 // Lift second finger.
2245 // Make sure there is enough delay before the touch is released so that it is
2246 // recognized as a tap.
2247 delegate->Reset();
2248 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2249 kTouchId2, tes.LeapForward(50));
2251 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2252 EXPECT_FALSE(delegate->tap());
2253 EXPECT_FALSE(delegate->tap_down());
2254 EXPECT_FALSE(delegate->tap_cancel());
2255 EXPECT_FALSE(delegate->scroll_begin());
2256 EXPECT_FALSE(delegate->scroll_update());
2257 EXPECT_FALSE(delegate->scroll_end());
2258 EXPECT_TRUE(delegate->fling());
2259 EXPECT_FALSE(delegate->two_finger_tap());
2262 TEST_F(GestureRecognizerTest, TwoFingerTapExpired) {
2263 scoped_ptr<GestureEventConsumeDelegate> delegate(
2264 new GestureEventConsumeDelegate());
2265 const int kWindowWidth = 123;
2266 const int kWindowHeight = 45;
2267 const int kTouchId1 = 2;
2268 const int kTouchId2 = 3;
2269 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2270 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2271 delegate.get(), -1234, bounds, root_window()));
2272 TimedEvents tes;
2274 delegate->Reset();
2275 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2276 kTouchId1, tes.Now());
2277 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2279 delegate->Reset();
2280 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2281 kTouchId2, tes.Now());
2282 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2284 // Send release event after sufficient delay so that two finger time expires.
2285 delegate->Reset();
2286 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2287 kTouchId1, tes.LeapForward(1000));
2289 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2290 EXPECT_FALSE(delegate->two_finger_tap());
2292 // Lift second finger.
2293 // Make sure there is enough delay before the touch is released so that it is
2294 // recognized as a tap.
2295 delegate->Reset();
2296 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2297 kTouchId2, tes.LeapForward(50));
2299 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2300 EXPECT_FALSE(delegate->two_finger_tap());
2303 TEST_F(GestureRecognizerTest, TwoFingerTapChangesToPinch) {
2304 scoped_ptr<GestureEventConsumeDelegate> delegate(
2305 new GestureEventConsumeDelegate());
2306 const int kWindowWidth = 123;
2307 const int kWindowHeight = 45;
2308 const int kTouchId1 = 2;
2309 const int kTouchId2 = 3;
2310 TimedEvents tes;
2312 // Test moving first finger
2314 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2315 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2316 delegate.get(), -1234, bounds, root_window()));
2318 delegate->Reset();
2319 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2320 kTouchId1, tes.Now());
2321 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2323 delegate->Reset();
2324 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2325 kTouchId2, tes.Now());
2326 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2328 tes.SendScrollEvent(root_window(), 130, 230, kTouchId1, delegate.get());
2329 EXPECT_FALSE(delegate->two_finger_tap());
2330 EXPECT_TRUE(delegate->pinch_begin());
2332 // Make sure there is enough delay before the touch is released so that it
2333 // is recognized as a tap.
2334 delegate->Reset();
2335 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2336 kTouchId2, tes.LeapForward(50));
2338 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2339 EXPECT_FALSE(delegate->two_finger_tap());
2340 EXPECT_TRUE(delegate->pinch_end());
2343 // Test moving second finger
2345 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2346 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2347 delegate.get(), -1234, bounds, root_window()));
2349 delegate->Reset();
2350 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2351 kTouchId1, tes.Now());
2352 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2354 delegate->Reset();
2355 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2356 kTouchId2, tes.Now());
2357 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2359 tes.SendScrollEvent(root_window(), 101, 230, kTouchId2, delegate.get());
2360 EXPECT_FALSE(delegate->two_finger_tap());
2361 EXPECT_TRUE(delegate->pinch_begin());
2363 // Make sure there is enough delay before the touch is released so that it
2364 // is recognized as a tap.
2365 delegate->Reset();
2366 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2367 kTouchId1, tes.LeapForward(50));
2369 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2370 EXPECT_FALSE(delegate->two_finger_tap());
2371 EXPECT_TRUE(delegate->pinch_end());
2375 TEST_F(GestureRecognizerTest, MultiFingerSwipe) {
2376 scoped_ptr<GestureEventConsumeDelegate> delegate(
2377 new GestureEventConsumeDelegate());
2378 const int kWindowWidth = 123;
2379 const int kWindowHeight = 45;
2381 gfx::Rect bounds(5, 10, kWindowWidth, kWindowHeight);
2382 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2383 delegate.get(), -1234, bounds, root_window()));
2385 const int kSteps = 15;
2386 const int kTouchPoints = 4;
2387 gfx::Point points[kTouchPoints] = {
2388 gfx::Point(10, 30),
2389 gfx::Point(30, 20),
2390 gfx::Point(50, 30),
2391 gfx::Point(80, 50)
2394 aura::test::EventGenerator generator(root_window(), window.get());
2396 for (int count = 2; count <= kTouchPoints; ++count) {
2397 generator.GestureMultiFingerScroll(count, points, 15, kSteps, 0, -150);
2398 EXPECT_TRUE(delegate->swipe_up());
2399 delegate->Reset();
2401 generator.GestureMultiFingerScroll(count, points, 15, kSteps, 0, 150);
2402 EXPECT_TRUE(delegate->swipe_down());
2403 delegate->Reset();
2405 generator.GestureMultiFingerScroll(count, points, 15, kSteps, -150, 0);
2406 EXPECT_TRUE(delegate->swipe_left());
2407 delegate->Reset();
2409 generator.GestureMultiFingerScroll(count, points, 15, kSteps, 150, 0);
2410 EXPECT_TRUE(delegate->swipe_right());
2411 delegate->Reset();
2415 TEST_F(GestureRecognizerTest, TwoFingerTapCancelled) {
2416 scoped_ptr<GestureEventConsumeDelegate> delegate(
2417 new GestureEventConsumeDelegate());
2418 const int kWindowWidth = 123;
2419 const int kWindowHeight = 45;
2420 const int kTouchId1 = 2;
2421 const int kTouchId2 = 3;
2422 TimedEvents tes;
2424 // Test canceling first finger.
2426 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2427 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2428 delegate.get(), -1234, bounds, root_window()));
2430 delegate->Reset();
2431 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2432 kTouchId1, tes.Now());
2433 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2435 delegate->Reset();
2436 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2437 kTouchId2, tes.Now());
2438 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2440 delegate->Reset();
2441 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2442 kTouchId1, tes.Now());
2443 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&cancel);
2444 EXPECT_FALSE(delegate->two_finger_tap());
2446 // Make sure there is enough delay before the touch is released so that it
2447 // is recognized as a tap.
2448 delegate->Reset();
2449 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2450 kTouchId2, tes.LeapForward(50));
2452 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2453 EXPECT_FALSE(delegate->two_finger_tap());
2456 // Test canceling second finger
2458 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2459 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2460 delegate.get(), -1234, bounds, root_window()));
2462 delegate->Reset();
2463 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2464 kTouchId1, tes.Now());
2465 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2467 delegate->Reset();
2468 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2469 kTouchId2, tes.Now());
2470 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2472 delegate->Reset();
2473 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2474 kTouchId2, tes.Now());
2475 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&cancel);
2476 EXPECT_FALSE(delegate->two_finger_tap());
2478 // Make sure there is enough delay before the touch is released so that it
2479 // is recognized as a tap.
2480 delegate->Reset();
2481 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2482 kTouchId1, tes.LeapForward(50));
2484 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2485 EXPECT_FALSE(delegate->two_finger_tap());
2489 TEST_F(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) {
2490 scoped_ptr<GestureEventConsumeDelegate> delegate(
2491 new GestureEventConsumeDelegate());
2492 const int kWindowWidth = 523;
2493 const int kWindowHeight = 45;
2494 const int kTouchId1 = 2;
2495 const int kTouchId2 = 3;
2496 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2497 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2498 delegate.get(), -1234, bounds, root_window()));
2499 TimedEvents tes;
2501 delegate->Reset();
2502 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2503 kTouchId1, tes.Now());
2504 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2505 EXPECT_FALSE(delegate->tap());
2506 EXPECT_TRUE(delegate->tap_down());
2507 EXPECT_FALSE(delegate->tap_cancel());
2508 EXPECT_FALSE(delegate->scroll_begin());
2509 EXPECT_FALSE(delegate->scroll_update());
2510 EXPECT_FALSE(delegate->scroll_end());
2511 EXPECT_FALSE(delegate->long_press());
2512 EXPECT_FALSE(delegate->two_finger_tap());
2514 delegate->Reset();
2515 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201),
2516 kTouchId2, tes.Now());
2517 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2518 EXPECT_FALSE(delegate->tap());
2519 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
2520 EXPECT_TRUE(delegate->tap_cancel());
2521 EXPECT_TRUE(delegate->scroll_begin());
2522 EXPECT_FALSE(delegate->scroll_update());
2523 EXPECT_FALSE(delegate->scroll_end());
2524 EXPECT_FALSE(delegate->long_press());
2525 EXPECT_FALSE(delegate->two_finger_tap());
2526 EXPECT_TRUE(delegate->pinch_begin());
2529 // Verifies if a window is the target of multiple touch-ids and we hide the
2530 // window everything is cleaned up correctly.
2531 TEST_F(GestureRecognizerTest, FlushAllOnHide) {
2532 scoped_ptr<GestureEventConsumeDelegate> delegate(
2533 new GestureEventConsumeDelegate());
2534 gfx::Rect bounds(0, 0, 200, 200);
2535 scoped_ptr<aura::Window> window(
2536 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2537 const int kTouchId1 = 8;
2538 const int kTouchId2 = 2;
2539 TimedEvents tes;
2541 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2542 kTouchId1, tes.Now());
2543 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2544 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2545 kTouchId2, tes.Now());
2546 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2547 window->Hide();
2548 EXPECT_EQ(NULL,
2549 root_window()->gesture_recognizer()->GetTouchLockedTarget(&press1));
2550 EXPECT_EQ(NULL,
2551 root_window()->gesture_recognizer()->GetTouchLockedTarget(&press2));
2554 TEST_F(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) {
2555 scoped_ptr<QueueTouchEventDelegate> delegate(
2556 new QueueTouchEventDelegate(root_window()));
2557 const int kTouchId = 2;
2558 gfx::Rect bounds(100, 200, 100, 100);
2559 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2560 delegate.get(), -1234, bounds, root_window()));
2561 delegate->set_window(window.get());
2562 TimedEvents tes;
2564 TimerTestGestureRecognizer* gesture_recognizer =
2565 new TimerTestGestureRecognizer(root_window());
2566 TimerTestGestureSequence* gesture_sequence =
2567 static_cast<TimerTestGestureSequence*>(
2568 gesture_recognizer->GetGestureSequenceForTesting(window.get()));
2570 root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
2572 delegate->Reset();
2573 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2574 kTouchId, tes.Now());
2575 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2576 // Scroll around, to cancel the long press
2577 tes.SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
2579 delegate->Reset();
2580 delegate->ReceivedAck();
2581 EXPECT_TRUE(delegate->tap_down());
2582 EXPECT_TRUE(gesture_sequence->IsTimerRunning());
2584 delegate->Reset();
2585 delegate->ReceivedAckPreventDefaulted();
2586 EXPECT_FALSE(gesture_sequence->IsTimerRunning());
2587 gesture_sequence->ForceTimeout();
2588 EXPECT_FALSE(delegate->long_press());
2591 // Same as GestureEventConsumeDelegate, but consumes all the touch-move events.
2592 class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate {
2593 public:
2594 ConsumesTouchMovesDelegate() : consume_touch_move_(true) {}
2595 virtual ~ConsumesTouchMovesDelegate() {}
2597 void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; }
2599 private:
2600 virtual void OnTouchEvent(ui::TouchEvent* touch) OVERRIDE {
2601 if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED)
2602 touch->SetHandled();
2603 else
2604 GestureEventConsumeDelegate::OnTouchEvent(touch);
2607 bool consume_touch_move_;
2609 DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate);
2612 // Same as GestureEventScroll, but tests that the behavior is the same
2613 // even if all the touch-move events are consumed.
2614 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) {
2615 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
2616 new ConsumesTouchMovesDelegate());
2617 const int kWindowWidth = 123;
2618 const int kWindowHeight = 45;
2619 const int kTouchId = 5;
2620 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2621 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2622 delegate.get(), -1234, bounds, root_window()));
2623 TimedEvents tes;
2625 delegate->Reset();
2626 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2627 kTouchId, tes.Now());
2628 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2629 EXPECT_FALSE(delegate->tap());
2630 EXPECT_TRUE(delegate->tap_down());
2631 EXPECT_FALSE(delegate->tap_cancel());
2632 EXPECT_TRUE(delegate->begin());
2633 EXPECT_FALSE(delegate->scroll_begin());
2634 EXPECT_FALSE(delegate->scroll_update());
2635 EXPECT_FALSE(delegate->scroll_end());
2637 // Move the touch-point enough so that it would normally be considered a
2638 // scroll. But since the touch-moves will be consumed, the scroll should not
2639 // start.
2640 tes.SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
2641 EXPECT_FALSE(delegate->tap());
2642 EXPECT_FALSE(delegate->tap_down());
2643 // TODO(rbyers): Really we should get the TapCancel here instead of below,
2644 // but this is a symptom of a larger issue: crbug.com/146397.
2645 EXPECT_FALSE(delegate->tap_cancel());
2646 EXPECT_FALSE(delegate->begin());
2647 EXPECT_FALSE(delegate->scroll_begin());
2648 EXPECT_FALSE(delegate->scroll_update());
2649 EXPECT_FALSE(delegate->scroll_end());
2651 // Release the touch back at the start point. This should end without causing
2652 // a tap.
2653 delegate->Reset();
2654 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230),
2655 kTouchId, tes.LeapForward(50));
2656 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2657 EXPECT_FALSE(delegate->tap());
2658 EXPECT_FALSE(delegate->tap_down());
2659 EXPECT_TRUE(delegate->tap_cancel());
2660 EXPECT_FALSE(delegate->begin());
2661 EXPECT_TRUE(delegate->end());
2662 EXPECT_FALSE(delegate->scroll_begin());
2663 EXPECT_FALSE(delegate->scroll_update());
2664 EXPECT_FALSE(delegate->scroll_end());
2667 // Like as GestureEventTouchMoveConsumed but tests the different behavior
2668 // depending on whether the events were consumed before or after the scroll
2669 // started.
2670 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) {
2671 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
2672 new ConsumesTouchMovesDelegate());
2673 const int kWindowWidth = 123;
2674 const int kWindowHeight = 45;
2675 const int kTouchId = 5;
2676 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2677 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2678 delegate.get(), -1234, bounds, root_window()));
2679 TimedEvents tes;
2681 delegate->Reset();
2682 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2683 kTouchId, tes.Now());
2684 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2685 EXPECT_FALSE(delegate->tap());
2686 EXPECT_TRUE(delegate->tap_down());
2687 EXPECT_FALSE(delegate->tap_cancel());
2688 EXPECT_TRUE(delegate->begin());
2689 EXPECT_FALSE(delegate->scroll_begin());
2690 EXPECT_FALSE(delegate->scroll_update());
2691 EXPECT_FALSE(delegate->scroll_end());
2693 // Move the touch-point enough so that it would normally be considered a
2694 // scroll. But since the touch-moves will be consumed, the scroll should not
2695 // start.
2696 tes.SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
2697 EXPECT_FALSE(delegate->tap());
2698 EXPECT_FALSE(delegate->tap_down());
2699 // TODO(rbyers): Really we should get the TapCancel here instead of below,
2700 // but this is a symptom of a larger issue: crbug.com/146397.
2701 EXPECT_FALSE(delegate->tap_cancel());
2702 EXPECT_FALSE(delegate->begin());
2703 EXPECT_FALSE(delegate->scroll_begin());
2704 EXPECT_FALSE(delegate->scroll_update());
2705 EXPECT_FALSE(delegate->scroll_end());
2707 // Now, stop consuming touch-move events, and move the touch-point again.
2708 delegate->set_consume_touch_move(false);
2709 tes.SendScrollEvent(root_window(), 159, 259, kTouchId, delegate.get());
2710 EXPECT_FALSE(delegate->tap());
2711 EXPECT_FALSE(delegate->tap_down());
2712 EXPECT_TRUE(delegate->tap_cancel());
2713 EXPECT_FALSE(delegate->begin());
2714 EXPECT_TRUE(delegate->scroll_begin());
2715 EXPECT_TRUE(delegate->scroll_update());
2716 EXPECT_FALSE(delegate->scroll_end());
2717 // Consuming move events doesn't effect what the ultimate scroll position
2718 // will be if scrolling is later allowed to happen.
2719 EXPECT_EQ(58, delegate->scroll_x());
2720 EXPECT_EQ(58, delegate->scroll_y());
2721 EXPECT_EQ(gfx::Point(1, 1).ToString(),
2722 delegate->scroll_begin_position().ToString());
2724 // Start consuming touch-move events again. However, since gesture-scroll has
2725 // already started, the touch-move events should still result in scroll-update
2726 // gestures.
2727 delegate->set_consume_touch_move(true);
2729 // Move some more to generate a few more scroll updates.
2730 tes.SendScrollEvent(root_window(), 110, 211, kTouchId, delegate.get());
2731 EXPECT_FALSE(delegate->tap());
2732 EXPECT_FALSE(delegate->tap_down());
2733 EXPECT_FALSE(delegate->tap_cancel());
2734 EXPECT_FALSE(delegate->begin());
2735 EXPECT_FALSE(delegate->scroll_begin());
2736 EXPECT_TRUE(delegate->scroll_update());
2737 EXPECT_FALSE(delegate->scroll_end());
2738 EXPECT_EQ(-49, delegate->scroll_x());
2739 EXPECT_EQ(-48, delegate->scroll_y());
2741 tes.SendScrollEvent(root_window(), 140, 215, kTouchId, delegate.get());
2742 EXPECT_FALSE(delegate->tap());
2743 EXPECT_FALSE(delegate->tap_down());
2744 EXPECT_FALSE(delegate->tap_cancel());
2745 EXPECT_FALSE(delegate->begin());
2746 EXPECT_FALSE(delegate->scroll_begin());
2747 EXPECT_TRUE(delegate->scroll_update());
2748 EXPECT_FALSE(delegate->scroll_end());
2749 EXPECT_EQ(30, delegate->scroll_x());
2750 EXPECT_EQ(4, delegate->scroll_y());
2752 // Release the touch. This should end the scroll.
2753 delegate->Reset();
2754 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2755 kTouchId, tes.LeapForward(50));
2756 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2757 EXPECT_FALSE(delegate->tap());
2758 EXPECT_FALSE(delegate->tap_down());
2759 EXPECT_FALSE(delegate->tap_cancel());
2760 EXPECT_FALSE(delegate->begin());
2761 EXPECT_TRUE(delegate->end());
2762 EXPECT_FALSE(delegate->scroll_begin());
2763 EXPECT_FALSE(delegate->scroll_update());
2764 EXPECT_FALSE(delegate->scroll_end());
2765 // Moves arrive without delays and hence have high velocity.
2766 EXPECT_TRUE(delegate->fling());
2769 // Check that appropriate touch events generate double tap gesture events.
2770 TEST_F(GestureRecognizerTest, GestureEventDoubleTap) {
2771 scoped_ptr<GestureEventConsumeDelegate> delegate(
2772 new GestureEventConsumeDelegate());
2773 const int kWindowWidth = 123;
2774 const int kWindowHeight = 45;
2775 const int kTouchId = 2;
2776 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2777 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2778 delegate.get(), -1234, bounds, root_window()));
2779 TimedEvents tes;
2781 // First tap (tested in GestureEventTap)
2782 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
2783 kTouchId, tes.Now());
2784 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2785 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
2786 kTouchId, tes.LeapForward(50));
2787 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2788 delegate->Reset();
2790 // Second tap
2791 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
2792 kTouchId, tes.LeapForward(200));
2793 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2794 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
2795 kTouchId, tes.LeapForward(50));
2796 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2798 EXPECT_TRUE(delegate->tap());
2799 EXPECT_TRUE(delegate->tap_down());
2800 EXPECT_FALSE(delegate->tap_cancel());
2801 EXPECT_TRUE(delegate->begin());
2802 EXPECT_TRUE(delegate->end());
2803 EXPECT_FALSE(delegate->scroll_begin());
2804 EXPECT_FALSE(delegate->scroll_update());
2805 EXPECT_FALSE(delegate->scroll_end());
2807 EXPECT_EQ(2, delegate->tap_count());
2810 // Check that we don't get a double tap when the two taps are far apart.
2811 TEST_F(GestureRecognizerTest, TwoTapsFarApart) {
2812 scoped_ptr<GestureEventConsumeDelegate> delegate(
2813 new GestureEventConsumeDelegate());
2814 const int kWindowWidth = 123;
2815 const int kWindowHeight = 45;
2816 const int kTouchId = 2;
2817 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2818 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2819 delegate.get(), -1234, bounds, root_window()));
2820 TimedEvents tes;
2822 // First tap (tested in GestureEventTap)
2823 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2824 kTouchId, tes.Now());
2825 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2826 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2827 kTouchId, tes.LeapForward(50));
2828 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2829 delegate->Reset();
2831 // Second tap, close in time but far in distance
2832 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201),
2833 kTouchId, tes.LeapForward(200));
2834 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2835 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201),
2836 kTouchId, tes.LeapForward(50));
2837 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2839 EXPECT_TRUE(delegate->tap());
2840 EXPECT_TRUE(delegate->tap_down());
2841 EXPECT_FALSE(delegate->tap_cancel());
2842 EXPECT_TRUE(delegate->begin());
2843 EXPECT_TRUE(delegate->end());
2844 EXPECT_FALSE(delegate->scroll_begin());
2845 EXPECT_FALSE(delegate->scroll_update());
2846 EXPECT_FALSE(delegate->scroll_end());
2848 EXPECT_EQ(1, delegate->tap_count());
2851 // Check that we don't get a double tap when the two taps have a long enough
2852 // delay in between.
2853 TEST_F(GestureRecognizerTest, TwoTapsWithDelayBetween) {
2854 scoped_ptr<GestureEventConsumeDelegate> delegate(
2855 new GestureEventConsumeDelegate());
2856 const int kWindowWidth = 123;
2857 const int kWindowHeight = 45;
2858 const int kTouchId = 2;
2859 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2860 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2861 delegate.get(), -1234, bounds, root_window()));
2862 TimedEvents tes;
2864 // First tap (tested in GestureEventTap)
2865 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2866 kTouchId, tes.Now());
2867 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2868 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2869 kTouchId, tes.LeapForward(50));
2870 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2871 delegate->Reset();
2873 // Second tap, close in distance but after some delay
2874 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2875 kTouchId, tes.LeapForward(2000));
2876 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2877 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2878 kTouchId, tes.LeapForward(50));
2879 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2881 EXPECT_TRUE(delegate->tap());
2882 EXPECT_TRUE(delegate->tap_down());
2883 EXPECT_FALSE(delegate->tap_cancel());
2884 EXPECT_TRUE(delegate->begin());
2885 EXPECT_TRUE(delegate->end());
2886 EXPECT_FALSE(delegate->scroll_begin());
2887 EXPECT_FALSE(delegate->scroll_update());
2888 EXPECT_FALSE(delegate->scroll_end());
2890 EXPECT_EQ(1, delegate->tap_count());
2893 // Checks that if the bounding-box of a gesture changes because of change in
2894 // radius of a touch-point, and not because of change in position, then there
2895 // are not gesture events from that.
2896 TEST_F(GestureRecognizerTest, BoundingBoxRadiusChange) {
2897 scoped_ptr<GestureEventConsumeDelegate> delegate(
2898 new GestureEventConsumeDelegate());
2899 const int kWindowWidth = 234;
2900 const int kWindowHeight = 345;
2901 const int kTouchId = 5, kTouchId2 = 7;
2902 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2903 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2904 delegate.get(), -1234, bounds, root_window()));
2905 TimedEvents tes;
2907 ui::TouchEvent press1(
2908 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, tes.Now());
2909 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2910 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2912 delegate->Reset();
2914 ui::TouchEvent press2(
2915 ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2,
2916 tes.LeapForward(400));
2917 press2.set_radius_x(5);
2918 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2919 EXPECT_FALSE(delegate->pinch_begin());
2920 EXPECT_EQ(gfx::Rect(101, 201, 100, 0).ToString(),
2921 delegate->bounding_box().ToString());
2923 delegate->Reset();
2925 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(141, 201), kTouchId,
2926 tes.LeapForward(40));
2927 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
2928 EXPECT_TRUE(delegate->pinch_begin());
2929 EXPECT_EQ(gfx::Rect(141, 201, 60, 0).ToString(),
2930 delegate->bounding_box().ToString());
2932 delegate->Reset();
2934 // The position doesn't move, but the radius changes.
2935 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 201), kTouchId,
2936 tes.LeapForward(40));
2937 move2.set_radius_x(50);
2938 move2.set_radius_y(60);
2939 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
2940 EXPECT_FALSE(delegate->tap());
2941 EXPECT_FALSE(delegate->tap_cancel());
2942 EXPECT_FALSE(delegate->scroll_update());
2943 EXPECT_FALSE(delegate->pinch_update());
2945 delegate->Reset();
2948 // Checks that slow scrolls deliver the correct deltas.
2949 // In particular, fix for http;//crbug.com/150573.
2950 TEST_F(GestureRecognizerTest, NoDriftInScroll) {
2951 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
2952 ui::GestureConfiguration::set_min_scroll_delta_squared(9);
2953 scoped_ptr<GestureEventConsumeDelegate> delegate(
2954 new GestureEventConsumeDelegate());
2955 const int kWindowWidth = 234;
2956 const int kWindowHeight = 345;
2957 const int kTouchId = 5;
2958 TimedEvents tes;
2959 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2960 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2961 delegate.get(), -1234, bounds, root_window()));
2963 ui::TouchEvent press1(
2964 ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, tes.Now());
2965 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2966 EXPECT_TRUE(delegate->begin());
2968 delegate->Reset();
2970 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId,
2971 tes.LeapForward(40));
2972 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
2973 EXPECT_FALSE(delegate->scroll_begin());
2975 delegate->Reset();
2977 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
2978 tes.LeapForward(40));
2979 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
2980 EXPECT_TRUE(delegate->tap_cancel());
2981 EXPECT_TRUE(delegate->scroll_begin());
2982 EXPECT_TRUE(delegate->scroll_update());
2983 EXPECT_EQ(-4, delegate->scroll_y());
2985 delegate->Reset();
2987 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
2988 tes.LeapForward(40));
2989 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move3);
2990 EXPECT_FALSE(delegate->scroll_update());
2992 delegate->Reset();
2994 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId,
2995 tes.LeapForward(40));
2996 root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move4);
2997 EXPECT_TRUE(delegate->scroll_update());
2998 EXPECT_EQ(-1, delegate->scroll_y());
3000 delegate->Reset();
3003 } // namespace test
3004 } // namespace aura