Previously our handling of ignored touches would put the gesture recognizer into...
[chromium-blink-merge.git] / ui / aura / gestures / gesture_recognizer_unittest.cc
blob7f86bfe7e98ea4d3cd82e1f17bbbb87a4922b004
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/command_line.h"
6 #include "base/memory/scoped_vector.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/timer/timer.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/test/aura_test_base.h"
13 #include "ui/aura/test/test_window_delegate.h"
14 #include "ui/aura/test/test_windows.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/base/hit_test.h"
18 #include "ui/base/ui_base_switches.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_switches.h"
21 #include "ui/events/event_utils.h"
22 #include "ui/events/gestures/gesture_configuration.h"
23 #include "ui/events/gestures/gesture_recognizer_impl.h"
24 #include "ui/events/gestures/gesture_types.h"
25 #include "ui/events/test/event_generator.h"
26 #include "ui/events/test/events_test_utils.h"
27 #include "ui/gfx/point.h"
28 #include "ui/gfx/rect.h"
30 #include <queue>
32 namespace aura {
33 namespace test {
35 namespace {
37 std::string WindowIDAsString(ui::GestureConsumer* consumer) {
38 return consumer ?
39 base::IntToString(static_cast<Window*>(consumer)->id()) : "?";
42 #define EXPECT_0_EVENTS(events) \
43 EXPECT_EQ(0u, events.size())
45 #define EXPECT_1_EVENT(events, e0) \
46 EXPECT_EQ(1u, events.size()); \
47 EXPECT_EQ(e0, events[0])
49 #define EXPECT_2_EVENTS(events, e0, e1) \
50 EXPECT_EQ(2u, events.size()); \
51 EXPECT_EQ(e0, events[0]); \
52 EXPECT_EQ(e1, events[1])
54 #define EXPECT_3_EVENTS(events, e0, e1, e2) \
55 EXPECT_EQ(3u, events.size()); \
56 EXPECT_EQ(e0, events[0]); \
57 EXPECT_EQ(e1, events[1]); \
58 EXPECT_EQ(e2, events[2])
60 #define EXPECT_4_EVENTS(events, e0, e1, e2, e3) \
61 EXPECT_EQ(4u, events.size()); \
62 EXPECT_EQ(e0, events[0]); \
63 EXPECT_EQ(e1, events[1]); \
64 EXPECT_EQ(e2, events[2]); \
65 EXPECT_EQ(e3, events[3])
67 // A delegate that keeps track of gesture events.
68 class GestureEventConsumeDelegate : public TestWindowDelegate {
69 public:
70 GestureEventConsumeDelegate()
71 : tap_(false),
72 tap_down_(false),
73 tap_cancel_(false),
74 begin_(false),
75 end_(false),
76 scroll_begin_(false),
77 scroll_update_(false),
78 scroll_end_(false),
79 pinch_begin_(false),
80 pinch_update_(false),
81 pinch_end_(false),
82 long_press_(false),
83 fling_(false),
84 two_finger_tap_(false),
85 show_press_(false),
86 swipe_left_(false),
87 swipe_right_(false),
88 swipe_up_(false),
89 swipe_down_(false),
90 scroll_x_(0),
91 scroll_y_(0),
92 scroll_velocity_x_(0),
93 scroll_velocity_y_(0),
94 velocity_x_(0),
95 velocity_y_(0),
96 scroll_x_hint_(0),
97 scroll_y_hint_(0),
98 tap_count_(0),
99 flags_(0),
100 wait_until_event_(ui::ET_UNKNOWN) {}
102 virtual ~GestureEventConsumeDelegate() {}
104 void Reset() {
105 events_.clear();
106 tap_ = false;
107 tap_down_ = false;
108 tap_cancel_ = false;
109 begin_ = false;
110 end_ = false;
111 scroll_begin_ = false;
112 scroll_update_ = false;
113 scroll_end_ = false;
114 pinch_begin_ = false;
115 pinch_update_ = false;
116 pinch_end_ = false;
117 long_press_ = false;
118 fling_ = false;
119 two_finger_tap_ = false;
120 show_press_ = false;
121 swipe_left_ = false;
122 swipe_right_ = false;
123 swipe_up_ = false;
124 swipe_down_ = false;
126 scroll_begin_position_.SetPoint(0, 0);
127 tap_location_.SetPoint(0, 0);
128 gesture_end_location_.SetPoint(0, 0);
130 scroll_x_ = 0;
131 scroll_y_ = 0;
132 scroll_velocity_x_ = 0;
133 scroll_velocity_y_ = 0;
134 velocity_x_ = 0;
135 velocity_y_ = 0;
136 scroll_x_hint_ = 0;
137 scroll_y_hint_ = 0;
138 tap_count_ = 0;
139 scale_ = 0;
140 flags_ = 0;
141 latency_info_.Clear();
144 const std::vector<ui::EventType>& events() const { return events_; };
146 bool tap() const { return tap_; }
147 bool tap_down() const { return tap_down_; }
148 bool tap_cancel() const { return tap_cancel_; }
149 bool begin() const { return begin_; }
150 bool end() const { return end_; }
151 bool scroll_begin() const { return scroll_begin_; }
152 bool scroll_update() const { return scroll_update_; }
153 bool scroll_end() const { return scroll_end_; }
154 bool pinch_begin() const { return pinch_begin_; }
155 bool pinch_update() const { return pinch_update_; }
156 bool pinch_end() const { return pinch_end_; }
157 bool long_press() const { return long_press_; }
158 bool long_tap() const { return long_tap_; }
159 bool fling() const { return fling_; }
160 bool two_finger_tap() const { return two_finger_tap_; }
161 bool show_press() const { return show_press_; }
162 bool swipe_left() const { return swipe_left_; }
163 bool swipe_right() const { return swipe_right_; }
164 bool swipe_up() const { return swipe_up_; }
165 bool swipe_down() const { return swipe_down_; }
167 const gfx::Point& scroll_begin_position() const {
168 return scroll_begin_position_;
171 const gfx::Point& tap_location() const {
172 return tap_location_;
175 const gfx::Point& gesture_end_location() const {
176 return gesture_end_location_;
179 float scroll_x() const { return scroll_x_; }
180 float scroll_y() const { return scroll_y_; }
181 float scroll_velocity_x() const { return scroll_velocity_x_; }
182 float scroll_velocity_y() const { return scroll_velocity_y_; }
183 float velocity_x() const { return velocity_x_; }
184 float velocity_y() const { return velocity_y_; }
185 float scroll_x_hint() const { return scroll_x_hint_; }
186 float scroll_y_hint() const { return scroll_y_hint_; }
187 float scale() const { return scale_; }
188 const gfx::Rect& bounding_box() const { return bounding_box_; }
189 int tap_count() const { return tap_count_; }
190 int flags() const { return flags_; }
191 const ui::LatencyInfo& latency_info() const { return latency_info_; }
193 void WaitUntilReceivedGesture(ui::EventType type) {
194 wait_until_event_ = type;
195 run_loop_.reset(new base::RunLoop());
196 run_loop_->Run();
199 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
200 events_.push_back(gesture->type());
201 bounding_box_ = gesture->details().bounding_box();
202 flags_ = gesture->flags();
203 latency_info_ = *gesture->latency();
204 switch (gesture->type()) {
205 case ui::ET_GESTURE_TAP:
206 tap_location_ = gesture->location();
207 tap_count_ = gesture->details().tap_count();
208 tap_ = true;
209 break;
210 case ui::ET_GESTURE_TAP_DOWN:
211 tap_down_ = true;
212 break;
213 case ui::ET_GESTURE_TAP_CANCEL:
214 tap_cancel_ = true;
215 break;
216 case ui::ET_GESTURE_BEGIN:
217 begin_ = true;
218 break;
219 case ui::ET_GESTURE_END:
220 end_ = true;
221 gesture_end_location_ = gesture->location();
222 break;
223 case ui::ET_GESTURE_SCROLL_BEGIN:
224 scroll_begin_ = true;
225 scroll_begin_position_ = gesture->location();
226 scroll_x_hint_ = gesture->details().scroll_x_hint();
227 scroll_y_hint_ = gesture->details().scroll_y_hint();
228 break;
229 case ui::ET_GESTURE_SCROLL_UPDATE:
230 scroll_update_ = true;
231 scroll_x_ += gesture->details().scroll_x();
232 scroll_y_ += gesture->details().scroll_y();
233 break;
234 case ui::ET_GESTURE_SCROLL_END:
235 EXPECT_TRUE(velocity_x_ == 0 && velocity_y_ == 0);
236 scroll_end_ = true;
237 break;
238 case ui::ET_GESTURE_PINCH_BEGIN:
239 pinch_begin_ = true;
240 break;
241 case ui::ET_GESTURE_PINCH_UPDATE:
242 pinch_update_ = true;
243 scale_ = gesture->details().scale();
244 break;
245 case ui::ET_GESTURE_PINCH_END:
246 pinch_end_ = true;
247 break;
248 case ui::ET_GESTURE_LONG_PRESS:
249 long_press_ = true;
250 break;
251 case ui::ET_GESTURE_LONG_TAP:
252 long_tap_ = true;
253 break;
254 case ui::ET_SCROLL_FLING_START:
255 EXPECT_TRUE(gesture->details().velocity_x() != 0 ||
256 gesture->details().velocity_y() != 0);
257 EXPECT_FALSE(scroll_end_);
258 fling_ = true;
259 velocity_x_ = gesture->details().velocity_x();
260 velocity_y_ = gesture->details().velocity_y();
261 break;
262 case ui::ET_GESTURE_TWO_FINGER_TAP:
263 two_finger_tap_ = true;
264 break;
265 case ui::ET_GESTURE_SHOW_PRESS:
266 show_press_ = true;
267 break;
268 case ui::ET_GESTURE_SWIPE:
269 swipe_left_ = gesture->details().swipe_left();
270 swipe_right_ = gesture->details().swipe_right();
271 swipe_up_ = gesture->details().swipe_up();
272 swipe_down_ = gesture->details().swipe_down();
273 break;
274 case ui::ET_SCROLL_FLING_CANCEL:
275 // Only used in unified gesture detection.
276 break;
277 default:
278 NOTREACHED();
280 if (wait_until_event_ == gesture->type() && run_loop_) {
281 run_loop_->Quit();
282 wait_until_event_ = ui::ET_UNKNOWN;
284 gesture->StopPropagation();
287 private:
288 scoped_ptr<base::RunLoop> run_loop_;
289 std::vector<ui::EventType> events_;
291 bool tap_;
292 bool tap_down_;
293 bool tap_cancel_;
294 bool begin_;
295 bool end_;
296 bool scroll_begin_;
297 bool scroll_update_;
298 bool scroll_end_;
299 bool pinch_begin_;
300 bool pinch_update_;
301 bool pinch_end_;
302 bool long_press_;
303 bool long_tap_;
304 bool fling_;
305 bool two_finger_tap_;
306 bool show_press_;
307 bool swipe_left_;
308 bool swipe_right_;
309 bool swipe_up_;
310 bool swipe_down_;
312 gfx::Point scroll_begin_position_;
313 gfx::Point tap_location_;
314 gfx::Point gesture_end_location_;
316 float scroll_x_;
317 float scroll_y_;
318 float scroll_velocity_x_;
319 float scroll_velocity_y_;
320 float velocity_x_;
321 float velocity_y_;
322 float scroll_x_hint_;
323 float scroll_y_hint_;
324 float scale_;
325 gfx::Rect bounding_box_;
326 int tap_count_;
327 int flags_;
328 ui::LatencyInfo latency_info_;
330 ui::EventType wait_until_event_;
332 DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate);
335 class QueueTouchEventDelegate : public GestureEventConsumeDelegate {
336 public:
337 explicit QueueTouchEventDelegate(WindowEventDispatcher* dispatcher)
338 : window_(NULL),
339 dispatcher_(dispatcher),
340 queue_events_(true) {
342 virtual ~QueueTouchEventDelegate() {
343 while(!queue_.empty()) {
344 delete queue_.front();
345 queue_.pop();
349 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
350 if (queue_events_) {
351 queue_.push(new ui::TouchEvent(*event, window_, window_));
352 event->StopPropagation();
356 void ReceivedAck() {
357 ReceivedAckImpl(false);
360 void ReceivedAckPreventDefaulted() {
361 ReceivedAckImpl(true);
364 void set_window(Window* w) { window_ = w; }
365 void set_queue_events(bool queue) { queue_events_ = queue; }
367 private:
368 void ReceivedAckImpl(bool prevent_defaulted) {
369 scoped_ptr<ui::TouchEvent> event(queue_.front());
370 dispatcher_->ProcessedTouchEvent(event.get(), window_,
371 prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED);
372 queue_.pop();
375 std::queue<ui::TouchEvent*> queue_;
376 Window* window_;
377 WindowEventDispatcher* dispatcher_;
378 bool queue_events_;
380 DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate);
383 // A delegate that ignores gesture events but keeps track of [synthetic] mouse
384 // events.
385 class GestureEventSynthDelegate : public TestWindowDelegate {
386 public:
387 GestureEventSynthDelegate()
388 : mouse_enter_(false),
389 mouse_exit_(false),
390 mouse_press_(false),
391 mouse_release_(false),
392 mouse_move_(false),
393 double_click_(false) {
396 void Reset() {
397 mouse_enter_ = false;
398 mouse_exit_ = false;
399 mouse_press_ = false;
400 mouse_release_ = false;
401 mouse_move_ = false;
402 double_click_ = false;
405 bool mouse_enter() const { return mouse_enter_; }
406 bool mouse_exit() const { return mouse_exit_; }
407 bool mouse_press() const { return mouse_press_; }
408 bool mouse_move() const { return mouse_move_; }
409 bool mouse_release() const { return mouse_release_; }
410 bool double_click() const { return double_click_; }
412 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
413 switch (event->type()) {
414 case ui::ET_MOUSE_PRESSED:
415 double_click_ = event->flags() & ui::EF_IS_DOUBLE_CLICK;
416 mouse_press_ = true;
417 break;
418 case ui::ET_MOUSE_RELEASED:
419 mouse_release_ = true;
420 break;
421 case ui::ET_MOUSE_MOVED:
422 mouse_move_ = true;
423 break;
424 case ui::ET_MOUSE_ENTERED:
425 mouse_enter_ = true;
426 break;
427 case ui::ET_MOUSE_EXITED:
428 mouse_exit_ = true;
429 break;
430 default:
431 NOTREACHED();
433 event->SetHandled();
436 private:
437 bool mouse_enter_;
438 bool mouse_exit_;
439 bool mouse_press_;
440 bool mouse_release_;
441 bool mouse_move_;
442 bool double_click_;
444 DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate);
447 class ScopedGestureRecognizerSetter {
448 public:
449 // Takes ownership of |new_gr|.
450 explicit ScopedGestureRecognizerSetter(ui::GestureRecognizer* new_gr)
451 : new_gr_(new_gr) {
452 original_gr_ = ui::GestureRecognizer::Get();
453 ui::SetGestureRecognizerForTesting(new_gr_.get());
456 virtual ~ScopedGestureRecognizerSetter() {
457 ui::SetGestureRecognizerForTesting(original_gr_);
460 private:
461 ui::GestureRecognizer* original_gr_;
462 scoped_ptr<ui::GestureRecognizer> new_gr_;
464 DISALLOW_COPY_AND_ASSIGN(ScopedGestureRecognizerSetter);
467 class TimedEvents {
468 private:
469 int simulated_now_;
471 public:
472 // Use a non-zero start time to pass DCHECKs which ensure events have had a
473 // time assigned.
474 TimedEvents() : simulated_now_(1) {
477 base::TimeDelta Now() {
478 base::TimeDelta t = base::TimeDelta::FromMilliseconds(simulated_now_);
479 simulated_now_++;
480 return t;
483 base::TimeDelta LeapForward(int time_in_millis) {
484 simulated_now_ += time_in_millis;
485 return base::TimeDelta::FromMilliseconds(simulated_now_);
488 base::TimeDelta InFuture(int time_in_millis) {
489 return base::TimeDelta::FromMilliseconds(simulated_now_ + time_in_millis);
492 void SendScrollEvents(ui::EventProcessor* dispatcher,
493 float x_start,
494 float y_start,
495 int dx,
496 int dy,
497 int touch_id,
498 int time_step,
499 int num_steps,
500 GestureEventConsumeDelegate* delegate) {
501 int x = x_start;
502 int y = y_start;
504 for (int i = 0; i < num_steps; i++) {
505 x += dx;
506 y += dy;
507 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
508 touch_id,
509 base::TimeDelta::FromMilliseconds(simulated_now_));
510 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
511 ASSERT_FALSE(details.dispatcher_destroyed);
512 simulated_now_ += time_step;
516 void SendScrollEvent(ui::EventProcessor* dispatcher,
517 float x,
518 float y,
519 int touch_id,
520 GestureEventConsumeDelegate* delegate) {
521 delegate->Reset();
522 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
523 touch_id,
524 base::TimeDelta::FromMilliseconds(simulated_now_));
525 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
526 ASSERT_FALSE(details.dispatcher_destroyed);
527 simulated_now_++;
531 // An event handler to keep track of events.
532 class TestEventHandler : public ui::EventHandler {
533 public:
534 TestEventHandler() : touch_released_count_(0),
535 touch_pressed_count_(0),
536 touch_moved_count_(0),
537 touch_cancelled_count_(0) {
540 virtual ~TestEventHandler() {}
542 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
543 switch (event->type()) {
544 case ui::ET_TOUCH_RELEASED:
545 touch_released_count_++;
546 break;
547 case ui::ET_TOUCH_PRESSED:
548 touch_pressed_count_++;
549 break;
550 case ui::ET_TOUCH_MOVED:
551 touch_moved_count_++;
552 break;
553 case ui::ET_TOUCH_CANCELLED:
554 touch_cancelled_count_++;
555 break;
556 default:
557 break;
561 void Reset() {
562 touch_released_count_ = 0;
563 touch_pressed_count_ = 0;
564 touch_moved_count_ = 0;
565 touch_cancelled_count_ = 0;
568 int touch_released_count() const { return touch_released_count_; }
569 int touch_pressed_count() const { return touch_pressed_count_; }
570 int touch_moved_count() const { return touch_moved_count_; }
571 int touch_cancelled_count() const { return touch_cancelled_count_; }
573 private:
574 int touch_released_count_;
575 int touch_pressed_count_;
576 int touch_moved_count_;
577 int touch_cancelled_count_;
579 DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
582 // Removes the target window from its parent when it receives a touch-cancel
583 // event.
584 class RemoveOnTouchCancelHandler : public TestEventHandler {
585 public:
586 RemoveOnTouchCancelHandler() {}
587 virtual ~RemoveOnTouchCancelHandler() {}
589 private:
590 // ui::EventHandler:
591 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
592 TestEventHandler::OnTouchEvent(event);
593 if (event->type() == ui::ET_TOUCH_CANCELLED) {
594 Window* target = static_cast<Window*>(event->target());
595 // This is tiptoeing around crbug.com/310172. If this event handler isn't
596 // removed, we enter an infinite loop.
597 target->RemovePreTargetHandler(this);
598 target->parent()->RemoveChild(target);
602 DISALLOW_COPY_AND_ASSIGN(RemoveOnTouchCancelHandler);
605 void DelayByLongPressTimeout() {
606 ui::GestureProvider::Config config;
607 base::RunLoop run_loop;
608 base::MessageLoop::current()->PostDelayedTask(
609 FROM_HERE,
610 run_loop.QuitClosure(),
611 config.gesture_detector_config.longpress_timeout * 2);
612 run_loop.Run();
615 void DelayByShowPressTimeout() {
616 ui::GestureProvider::Config config;
617 base::RunLoop run_loop;
618 base::MessageLoop::current()->PostDelayedTask(
619 FROM_HERE,
620 run_loop.QuitClosure(),
621 config.gesture_detector_config.showpress_timeout * 2);
622 run_loop.Run();
625 } // namespace
627 class GestureRecognizerTest : public AuraTestBase,
628 public ::testing::WithParamInterface<bool> {
629 public:
630 GestureRecognizerTest() {}
632 virtual void SetUp() OVERRIDE {
633 AuraTestBase::SetUp();
634 ui::GestureConfiguration::set_min_touch_down_duration_in_seconds_for_click(
635 0.001);
636 ui::GestureConfiguration::set_show_press_delay_in_ms(2);
637 ui::GestureConfiguration::set_long_press_time_in_seconds(0.003);
640 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerTest);
643 // Check that appropriate touch events generate tap gesture events.
644 TEST_F(GestureRecognizerTest, GestureEventTap) {
645 scoped_ptr<GestureEventConsumeDelegate> delegate(
646 new GestureEventConsumeDelegate());
647 TimedEvents tes;
648 const int kWindowWidth = 123;
649 const int kWindowHeight = 45;
650 const int kTouchId = 2;
651 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
652 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
653 delegate.get(), -1234, bounds, root_window()));
655 delegate->Reset();
656 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
657 kTouchId, tes.Now());
658 DispatchEventUsingWindowDispatcher(&press);
659 EXPECT_FALSE(delegate->tap());
660 EXPECT_FALSE(delegate->show_press());
661 EXPECT_TRUE(delegate->tap_down());
662 EXPECT_FALSE(delegate->tap_cancel());
663 EXPECT_TRUE(delegate->begin());
664 EXPECT_FALSE(delegate->scroll_begin());
665 EXPECT_FALSE(delegate->scroll_update());
666 EXPECT_FALSE(delegate->scroll_end());
667 EXPECT_FALSE(delegate->long_press());
669 delegate->Reset();
670 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
671 EXPECT_TRUE(delegate->show_press());
672 EXPECT_FALSE(delegate->tap_down());
674 // Make sure there is enough delay before the touch is released so that it is
675 // recognized as a tap.
676 delegate->Reset();
677 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
678 kTouchId, tes.LeapForward(50));
680 DispatchEventUsingWindowDispatcher(&release);
681 EXPECT_TRUE(delegate->tap());
682 EXPECT_FALSE(delegate->tap_down());
683 EXPECT_FALSE(delegate->tap_cancel());
684 EXPECT_FALSE(delegate->begin());
685 EXPECT_TRUE(delegate->end());
686 EXPECT_FALSE(delegate->scroll_begin());
687 EXPECT_FALSE(delegate->scroll_update());
688 EXPECT_FALSE(delegate->scroll_end());
690 EXPECT_EQ(1, delegate->tap_count());
693 // Check that appropriate touch events generate tap gesture events
694 // when information about the touch radii are provided.
695 TEST_F(GestureRecognizerTest, GestureEventTapRegion) {
696 scoped_ptr<GestureEventConsumeDelegate> delegate(
697 new GestureEventConsumeDelegate());
698 TimedEvents tes;
699 const int kWindowWidth = 800;
700 const int kWindowHeight = 600;
701 const int kTouchId = 2;
702 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
703 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
704 delegate.get(), -1234, bounds, root_window()));
706 // Test with no ET_TOUCH_MOVED events.
708 delegate->Reset();
709 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
710 kTouchId, tes.Now());
711 press.set_radius_x(5);
712 press.set_radius_y(12);
713 DispatchEventUsingWindowDispatcher(&press);
714 EXPECT_FALSE(delegate->tap());
715 EXPECT_TRUE(delegate->tap_down());
716 EXPECT_FALSE(delegate->tap_cancel());
717 EXPECT_TRUE(delegate->begin());
718 EXPECT_FALSE(delegate->scroll_begin());
719 EXPECT_FALSE(delegate->scroll_update());
720 EXPECT_FALSE(delegate->scroll_end());
721 EXPECT_FALSE(delegate->long_press());
723 // Make sure there is enough delay before the touch is released so that it
724 // is recognized as a tap.
725 delegate->Reset();
726 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
727 kTouchId, tes.LeapForward(50));
728 release.set_radius_x(5);
729 release.set_radius_y(12);
731 DispatchEventUsingWindowDispatcher(&release);
732 EXPECT_TRUE(delegate->tap());
733 EXPECT_FALSE(delegate->tap_down());
734 EXPECT_FALSE(delegate->tap_cancel());
735 EXPECT_FALSE(delegate->begin());
736 EXPECT_TRUE(delegate->end());
737 EXPECT_FALSE(delegate->scroll_begin());
738 EXPECT_FALSE(delegate->scroll_update());
739 EXPECT_FALSE(delegate->scroll_end());
741 EXPECT_EQ(1, delegate->tap_count());
742 gfx::Point actual_point(delegate->tap_location());
743 EXPECT_EQ(24, delegate->bounding_box().width());
744 EXPECT_EQ(24, delegate->bounding_box().height());
745 EXPECT_EQ(101, actual_point.x());
746 EXPECT_EQ(201, actual_point.y());
749 // Test with no ET_TOUCH_MOVED events but different touch points and radii.
751 delegate->Reset();
752 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(365, 290),
753 kTouchId, tes.Now());
754 press.set_radius_x(8);
755 press.set_radius_y(14);
756 DispatchEventUsingWindowDispatcher(&press);
757 EXPECT_FALSE(delegate->tap());
758 EXPECT_TRUE(delegate->tap_down());
759 EXPECT_FALSE(delegate->tap_cancel());
760 EXPECT_TRUE(delegate->begin());
761 EXPECT_FALSE(delegate->scroll_begin());
762 EXPECT_FALSE(delegate->scroll_update());
763 EXPECT_FALSE(delegate->scroll_end());
764 EXPECT_FALSE(delegate->long_press());
766 delegate->Reset();
767 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(367, 291),
768 kTouchId, tes.LeapForward(50));
769 release.set_radius_x(20);
770 release.set_radius_y(13);
772 DispatchEventUsingWindowDispatcher(&release);
773 EXPECT_TRUE(delegate->tap());
774 EXPECT_FALSE(delegate->tap_down());
775 EXPECT_FALSE(delegate->tap_cancel());
776 EXPECT_FALSE(delegate->begin());
777 EXPECT_TRUE(delegate->end());
778 EXPECT_FALSE(delegate->scroll_begin());
779 EXPECT_FALSE(delegate->scroll_update());
780 EXPECT_FALSE(delegate->scroll_end());
782 EXPECT_EQ(1, delegate->tap_count());
783 gfx::Point actual_point(delegate->tap_location());
784 EXPECT_EQ(40, delegate->bounding_box().width());
785 EXPECT_EQ(40, delegate->bounding_box().height());
786 EXPECT_EQ(367, actual_point.x());
787 EXPECT_EQ(291, actual_point.y());
790 // Test with a single ET_TOUCH_MOVED event.
792 delegate->Reset();
793 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(46, 205),
794 kTouchId, tes.Now());
795 press.set_radius_x(6);
796 press.set_radius_y(10);
797 DispatchEventUsingWindowDispatcher(&press);
798 EXPECT_FALSE(delegate->tap());
799 EXPECT_TRUE(delegate->tap_down());
800 EXPECT_FALSE(delegate->tap_cancel());
801 EXPECT_TRUE(delegate->begin());
802 EXPECT_FALSE(delegate->tap_cancel());
803 EXPECT_FALSE(delegate->scroll_begin());
804 EXPECT_FALSE(delegate->scroll_update());
805 EXPECT_FALSE(delegate->scroll_end());
806 EXPECT_FALSE(delegate->long_press());
808 delegate->Reset();
809 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(49, 204),
810 kTouchId, tes.LeapForward(50));
811 move.set_radius_x(8);
812 move.set_radius_y(12);
813 DispatchEventUsingWindowDispatcher(&move);
814 EXPECT_FALSE(delegate->tap());
815 EXPECT_FALSE(delegate->tap_down());
816 EXPECT_FALSE(delegate->tap_cancel());
817 EXPECT_FALSE(delegate->begin());
818 EXPECT_FALSE(delegate->scroll_begin());
819 EXPECT_FALSE(delegate->scroll_update());
820 EXPECT_FALSE(delegate->scroll_end());
821 EXPECT_FALSE(delegate->long_press());
823 delegate->Reset();
824 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(49, 204),
825 kTouchId, tes.LeapForward(50));
826 release.set_radius_x(4);
827 release.set_radius_y(8);
829 DispatchEventUsingWindowDispatcher(&release);
830 EXPECT_TRUE(delegate->tap());
831 EXPECT_FALSE(delegate->tap_down());
832 EXPECT_FALSE(delegate->tap_cancel());
833 EXPECT_FALSE(delegate->begin());
834 EXPECT_TRUE(delegate->end());
835 EXPECT_FALSE(delegate->scroll_begin());
836 EXPECT_FALSE(delegate->scroll_update());
837 EXPECT_FALSE(delegate->scroll_end());
839 EXPECT_EQ(1, delegate->tap_count());
840 gfx::Point actual_point(delegate->tap_location());
841 EXPECT_EQ(16, delegate->bounding_box().width());
842 EXPECT_EQ(16, delegate->bounding_box().height());
843 EXPECT_EQ(49, actual_point.x());
844 EXPECT_EQ(204, actual_point.y());
847 // Test with a few ET_TOUCH_MOVED events.
849 delegate->Reset();
850 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(400, 150),
851 kTouchId, tes.Now());
852 press.set_radius_x(7);
853 press.set_radius_y(10);
854 DispatchEventUsingWindowDispatcher(&press);
855 EXPECT_FALSE(delegate->tap());
856 EXPECT_TRUE(delegate->tap_down());
857 EXPECT_FALSE(delegate->tap_cancel());
858 EXPECT_TRUE(delegate->begin());
859 EXPECT_FALSE(delegate->scroll_begin());
860 EXPECT_FALSE(delegate->scroll_update());
861 EXPECT_FALSE(delegate->scroll_end());
862 EXPECT_FALSE(delegate->long_press());
864 delegate->Reset();
865 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(397, 151),
866 kTouchId, tes.LeapForward(50));
867 move.set_radius_x(13);
868 move.set_radius_y(12);
869 DispatchEventUsingWindowDispatcher(&move);
870 EXPECT_FALSE(delegate->tap());
871 EXPECT_FALSE(delegate->tap_down());
872 EXPECT_FALSE(delegate->tap_cancel());
873 EXPECT_FALSE(delegate->begin());
874 EXPECT_FALSE(delegate->scroll_begin());
875 EXPECT_FALSE(delegate->scroll_update());
876 EXPECT_FALSE(delegate->scroll_end());
877 EXPECT_FALSE(delegate->long_press());
879 delegate->Reset();
880 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(397, 149),
881 kTouchId, tes.LeapForward(50));
882 move1.set_radius_x(16);
883 move1.set_radius_y(16);
884 DispatchEventUsingWindowDispatcher(&move1);
885 EXPECT_FALSE(delegate->tap());
886 EXPECT_FALSE(delegate->tap_down());
887 EXPECT_FALSE(delegate->tap_cancel());
888 EXPECT_FALSE(delegate->begin());
889 EXPECT_FALSE(delegate->scroll_begin());
890 EXPECT_FALSE(delegate->scroll_update());
891 EXPECT_FALSE(delegate->scroll_end());
892 EXPECT_FALSE(delegate->long_press());
894 delegate->Reset();
895 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(400, 150),
896 kTouchId, tes.LeapForward(50));
897 move2.set_radius_x(14);
898 move2.set_radius_y(10);
899 DispatchEventUsingWindowDispatcher(&move2);
900 EXPECT_FALSE(delegate->tap());
901 EXPECT_FALSE(delegate->tap_down());
902 EXPECT_FALSE(delegate->tap_cancel());
903 EXPECT_FALSE(delegate->begin());
904 EXPECT_FALSE(delegate->scroll_begin());
905 EXPECT_FALSE(delegate->scroll_update());
906 EXPECT_FALSE(delegate->scroll_end());
907 EXPECT_FALSE(delegate->long_press());
909 delegate->Reset();
910 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(401, 149),
911 kTouchId, tes.LeapForward(50));
912 release.set_radius_x(8);
913 release.set_radius_y(9);
915 DispatchEventUsingWindowDispatcher(&release);
916 EXPECT_TRUE(delegate->tap());
917 EXPECT_FALSE(delegate->tap_down());
918 EXPECT_FALSE(delegate->tap_cancel());
919 EXPECT_FALSE(delegate->begin());
920 EXPECT_TRUE(delegate->end());
921 EXPECT_FALSE(delegate->scroll_begin());
922 EXPECT_FALSE(delegate->scroll_update());
923 EXPECT_FALSE(delegate->scroll_end());
925 EXPECT_EQ(1, delegate->tap_count());
926 gfx::Point actual_point(delegate->tap_location());
927 EXPECT_EQ(18, delegate->bounding_box().width());
928 EXPECT_EQ(18, delegate->bounding_box().height());
929 EXPECT_EQ(401, actual_point.x());
930 EXPECT_EQ(149, actual_point.y());
934 // Check that appropriate touch events generate scroll gesture events.
935 TEST_F(GestureRecognizerTest, GestureEventScroll) {
936 // We'll start by moving the touch point by (10.5, 10.5). We want 5 dips of
937 // that distance to be consumed by the slop, so we set the slop radius to
938 // sqrt(5 * 5 + 5 * 5).
939 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(
940 sqrt(static_cast<double>(5 * 5 + 5 * 5)));
941 scoped_ptr<GestureEventConsumeDelegate> delegate(
942 new GestureEventConsumeDelegate());
943 TimedEvents tes;
944 const int kWindowWidth = 123;
945 const int kWindowHeight = 45;
946 const int kTouchId = 5;
947 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
948 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
949 delegate.get(), -1234, bounds, root_window()));
951 delegate->Reset();
952 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
953 kTouchId, tes.Now());
954 DispatchEventUsingWindowDispatcher(&press);
955 EXPECT_2_EVENTS(delegate->events(),
956 ui::ET_GESTURE_BEGIN,
957 ui::ET_GESTURE_TAP_DOWN);
959 // Move the touch-point enough so that it is considered as a scroll. This
960 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
961 // The first movement is diagonal, to ensure that we have a free scroll,
962 // and not a rail scroll.
963 tes.SendScrollEvent(event_processor(), 111.5, 211.5, kTouchId,
964 delegate.get());
965 EXPECT_3_EVENTS(delegate->events(),
966 ui::ET_GESTURE_TAP_CANCEL,
967 ui::ET_GESTURE_SCROLL_BEGIN,
968 ui::ET_GESTURE_SCROLL_UPDATE);
969 // The slop consumed 5 dips
970 EXPECT_FLOAT_EQ(5.5, delegate->scroll_x());
971 EXPECT_FLOAT_EQ(5.5, delegate->scroll_y());
972 EXPECT_EQ(gfx::Point(1, 1).ToString(),
973 delegate->scroll_begin_position().ToString());
975 // When scrolling with a single finger, the bounding box of the gesture should
976 // be empty, since it's a single point and the radius for testing is zero.
977 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
979 // Move some more to generate a few more scroll updates. Make sure that we get
980 // out of the snap channel for the unified GR.
981 tes.SendScrollEvent(event_processor(), 20, 120, kTouchId, delegate.get());
982 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
983 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_x());
984 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_y());
985 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
987 tes.SendScrollEvent(event_processor(), 50, 124, kTouchId, delegate.get());
988 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
989 EXPECT_EQ(30, delegate->scroll_x());
990 EXPECT_EQ(4, delegate->scroll_y());
991 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
993 // Release the touch. This should end the scroll.
994 delegate->Reset();
995 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
996 kTouchId,
997 tes.LeapForward(50));
998 DispatchEventUsingWindowDispatcher(&release);
999 EXPECT_2_EVENTS(delegate->events(),
1000 ui::ET_SCROLL_FLING_START,
1001 ui::ET_GESTURE_END);
1002 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1005 // Check that predicted scroll update positions are correct.
1006 TEST_F(GestureRecognizerTest, GestureEventScrollPrediction) {
1007 const double prediction_interval = 0.03;
1008 ui::GestureConfiguration::set_scroll_prediction_seconds(prediction_interval);
1009 // We'll start by moving the touch point by (5, 5). We want all of that
1010 // distance to be consumed by the slop, so we set the slop radius to
1011 // sqrt(5 * 5 + 5 * 5).
1012 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(
1013 sqrt(static_cast<double>(5 * 5 + 5 * 5)));
1015 scoped_ptr<GestureEventConsumeDelegate> delegate(
1016 new GestureEventConsumeDelegate());
1017 TimedEvents tes;
1018 const int kWindowWidth = 123;
1019 const int kWindowHeight = 45;
1020 const int kTouchId = 5;
1021 gfx::Rect bounds(95, 195, kWindowWidth, kWindowHeight);
1022 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1023 delegate.get(), -1234, bounds, root_window()));
1025 delegate->Reset();
1026 // Tracks the total scroll since we want to verify that the correct position
1027 // will be scrolled to throughout the prediction.
1028 gfx::Vector2dF total_scroll;
1029 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(96, 196),
1030 kTouchId, tes.Now());
1031 DispatchEventUsingWindowDispatcher(&press);
1032 EXPECT_2_EVENTS(delegate->events(),
1033 ui::ET_GESTURE_BEGIN,
1034 ui::ET_GESTURE_TAP_DOWN);
1035 delegate->Reset();
1037 // Get rid of touch slop.
1038 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(111, 211),
1039 kTouchId, tes.Now());
1040 DispatchEventUsingWindowDispatcher(&move);
1041 EXPECT_3_EVENTS(delegate->events(),
1042 ui::ET_GESTURE_TAP_CANCEL,
1043 ui::ET_GESTURE_SCROLL_BEGIN,
1044 ui::ET_GESTURE_SCROLL_UPDATE);
1045 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1046 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1048 // Move the touch-point enough so that it is considered as a scroll. This
1049 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1050 // The first movement is diagonal, to ensure that we have a free scroll,
1051 // and not a rail scroll.
1052 tes.LeapForward(30);
1053 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1054 EXPECT_1_EVENT(delegate->events(),
1055 ui::ET_GESTURE_SCROLL_UPDATE);
1056 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1057 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1059 // Move some more to generate a few more scroll updates.
1060 tes.LeapForward(30);
1061 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
1062 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1063 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1064 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1066 tes.LeapForward(30);
1067 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
1068 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1069 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1070 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1072 // Release the touch. This should end the scroll.
1073 delegate->Reset();
1074 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1075 kTouchId,
1076 tes.LeapForward(50));
1077 DispatchEventUsingWindowDispatcher(&release);
1080 // Check that the bounding box during a scroll event is correct.
1081 TEST_F(GestureRecognizerTest, GestureEventScrollBoundingBox) {
1082 TimedEvents tes;
1083 for (int radius = 1; radius <= 10; ++radius) {
1084 ui::GestureConfiguration::set_default_radius(radius);
1085 scoped_ptr<GestureEventConsumeDelegate> delegate(
1086 new GestureEventConsumeDelegate());
1087 const int kWindowWidth = 123;
1088 const int kWindowHeight = 45;
1089 const int kTouchId = 5;
1090 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1091 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1092 delegate.get(), -1234, bounds, root_window()));
1094 const int kPositionX = 101;
1095 const int kPositionY = 201;
1096 delegate->Reset();
1097 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
1098 gfx::Point(kPositionX, kPositionY),
1099 kTouchId,
1100 tes.Now());
1101 DispatchEventUsingWindowDispatcher(&press);
1102 EXPECT_EQ(gfx::Rect(kPositionX - radius,
1103 kPositionY - radius,
1104 radius * 2,
1105 radius * 2).ToString(),
1106 delegate->bounding_box().ToString());
1108 const int kScrollAmount = 50;
1109 tes.SendScrollEvents(event_processor(), kPositionX, kPositionY,
1110 1, 1, kTouchId, 1, kScrollAmount, delegate.get());
1111 EXPECT_EQ(gfx::Point(1, 1).ToString(),
1112 delegate->scroll_begin_position().ToString());
1113 EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius,
1114 kPositionY + kScrollAmount - radius,
1115 radius * 2,
1116 radius * 2).ToString(),
1117 delegate->bounding_box().ToString());
1119 // Release the touch. This should end the scroll.
1120 delegate->Reset();
1121 ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
1122 gfx::Point(kPositionX + kScrollAmount,
1123 kPositionY + kScrollAmount),
1124 kTouchId, press.time_stamp() +
1125 base::TimeDelta::FromMilliseconds(50));
1126 DispatchEventUsingWindowDispatcher(&release);
1127 EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius,
1128 kPositionY + kScrollAmount - radius,
1129 radius * 2,
1130 radius * 2).ToString(),
1131 delegate->bounding_box().ToString());
1133 ui::GestureConfiguration::set_default_radius(0);
1136 // Check Scroll End Events report correct velocities
1137 // if the user was on a horizontal rail
1138 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailFling) {
1139 scoped_ptr<GestureEventConsumeDelegate> delegate(
1140 new GestureEventConsumeDelegate());
1141 TimedEvents tes;
1142 const int kTouchId = 7;
1143 gfx::Rect bounds(0, 0, 1000, 1000);
1144 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1145 delegate.get(), -1234, bounds, root_window()));
1147 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1148 kTouchId, tes.Now());
1149 DispatchEventUsingWindowDispatcher(&press);
1151 // Get rid of touch slop.
1152 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 0),
1153 kTouchId, tes.Now());
1154 DispatchEventUsingWindowDispatcher(&move);
1155 delegate->Reset();
1158 // Move the touch-point horizontally enough that it is considered a
1159 // horizontal scroll.
1160 tes.SendScrollEvent(event_processor(), 30, 1, kTouchId, delegate.get());
1161 EXPECT_FLOAT_EQ(0, delegate->scroll_y());
1162 EXPECT_FLOAT_EQ(20, delegate->scroll_x());
1164 // Get a high x velocity, while still staying on the rail
1165 tes.SendScrollEvents(event_processor(), 1, 1,
1166 100, 10, kTouchId, 1,
1167 ui::GestureConfiguration::points_buffered_for_velocity(),
1168 delegate.get());
1170 delegate->Reset();
1171 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1172 kTouchId, tes.Now());
1173 DispatchEventUsingWindowDispatcher(&release);
1175 EXPECT_TRUE(delegate->fling());
1176 EXPECT_FALSE(delegate->scroll_end());
1177 EXPECT_GT(delegate->velocity_x(), 0);
1178 EXPECT_EQ(0, delegate->velocity_y());
1181 // Check Scroll End Events report correct velocities
1182 // if the user was on a vertical rail
1183 TEST_F(GestureRecognizerTest, GestureEventVerticalRailFling) {
1184 scoped_ptr<GestureEventConsumeDelegate> delegate(
1185 new GestureEventConsumeDelegate());
1186 TimedEvents tes;
1187 const int kTouchId = 7;
1188 gfx::Rect bounds(0, 0, 1000, 1000);
1189 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1190 delegate.get(), -1234, bounds, root_window()));
1192 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1193 kTouchId, tes.Now());
1194 DispatchEventUsingWindowDispatcher(&press);
1196 // Get rid of touch slop.
1197 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 10),
1198 kTouchId, tes.Now());
1199 DispatchEventUsingWindowDispatcher(&move);
1200 delegate->Reset();
1202 // Move the touch-point vertically enough that it is considered a
1203 // vertical scroll.
1204 tes.SendScrollEvent(event_processor(), 1, 30, kTouchId, delegate.get());
1205 EXPECT_EQ(20, delegate->scroll_y());
1206 EXPECT_EQ(0, delegate->scroll_x());
1207 EXPECT_EQ(0, delegate->scroll_velocity_x());
1209 // Get a high y velocity, while still staying on the rail
1210 tes.SendScrollEvents(event_processor(), 1, 6,
1211 10, 100, kTouchId, 1,
1212 ui::GestureConfiguration::points_buffered_for_velocity(),
1213 delegate.get());
1214 EXPECT_EQ(0, delegate->scroll_velocity_x());
1216 delegate->Reset();
1217 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 206),
1218 kTouchId, tes.Now());
1219 DispatchEventUsingWindowDispatcher(&release);
1221 EXPECT_TRUE(delegate->fling());
1222 EXPECT_FALSE(delegate->scroll_end());
1223 EXPECT_EQ(0, delegate->velocity_x());
1224 EXPECT_GT(delegate->velocity_y(), 0);
1227 // Check Scroll End Events report non-zero velocities if the user is not on a
1228 // rail
1229 TEST_F(GestureRecognizerTest, GestureEventNonRailFling) {
1230 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(0);
1231 scoped_ptr<GestureEventConsumeDelegate> delegate(
1232 new GestureEventConsumeDelegate());
1233 TimedEvents tes;
1234 const int kTouchId = 7;
1235 gfx::Rect bounds(0, 0, 1000, 1000);
1236 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1237 delegate.get(), -1234, bounds, root_window()));
1239 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1240 kTouchId, tes.Now());
1241 DispatchEventUsingWindowDispatcher(&press);
1243 // Move the touch-point such that a non-rail scroll begins, and we're outside
1244 // the snap channel for the unified GR.
1245 tes.SendScrollEvent(event_processor(), 50, 50, kTouchId, delegate.get());
1246 EXPECT_EQ(50, delegate->scroll_y());
1247 EXPECT_EQ(50, delegate->scroll_x());
1249 tes.SendScrollEvents(event_processor(), 1, 1,
1250 10, 100, kTouchId, 1,
1251 ui::GestureConfiguration::points_buffered_for_velocity(),
1252 delegate.get());
1254 delegate->Reset();
1255 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1256 kTouchId, tes.Now());
1257 DispatchEventUsingWindowDispatcher(&release);
1259 EXPECT_TRUE(delegate->fling());
1260 EXPECT_FALSE(delegate->scroll_end());
1261 EXPECT_GT(delegate->velocity_x(), 0);
1262 EXPECT_GT(delegate->velocity_y(), 0);
1265 // Check that appropriate touch events generate long press events
1266 TEST_F(GestureRecognizerTest, GestureEventLongPress) {
1267 scoped_ptr<GestureEventConsumeDelegate> delegate(
1268 new GestureEventConsumeDelegate());
1269 const int kWindowWidth = 123;
1270 const int kWindowHeight = 45;
1271 const int kTouchId = 2;
1272 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1273 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1274 delegate.get(), -1234, bounds, root_window()));
1276 delegate->Reset();
1278 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1279 gfx::Point(101, 201),
1280 kTouchId,
1281 ui::EventTimeForNow());
1282 DispatchEventUsingWindowDispatcher(&press1);
1283 EXPECT_TRUE(delegate->tap_down());
1284 EXPECT_TRUE(delegate->begin());
1285 EXPECT_FALSE(delegate->tap_cancel());
1287 // We haven't pressed long enough for a long press to occur
1288 EXPECT_FALSE(delegate->long_press());
1290 // Wait until the timer runs out
1291 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1292 EXPECT_TRUE(delegate->long_press());
1293 EXPECT_FALSE(delegate->tap_cancel());
1295 delegate->Reset();
1296 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1297 gfx::Point(101, 201),
1298 kTouchId,
1299 ui::EventTimeForNow());
1300 DispatchEventUsingWindowDispatcher(&release1);
1301 EXPECT_FALSE(delegate->long_press());
1303 // Note the tap cancel isn't dispatched until the release
1304 EXPECT_TRUE(delegate->tap_cancel());
1305 EXPECT_FALSE(delegate->tap());
1308 // Check that scrolling prevents a long press.
1309 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledByScroll) {
1310 scoped_ptr<GestureEventConsumeDelegate> delegate(
1311 new GestureEventConsumeDelegate());
1312 TimedEvents tes;
1313 const int kWindowWidth = 123;
1314 const int kWindowHeight = 45;
1315 const int kTouchId = 6;
1316 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1317 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1318 delegate.get(), -1234, bounds, root_window()));
1320 delegate->Reset();
1322 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1323 kTouchId, tes.Now());
1324 DispatchEventUsingWindowDispatcher(&press1);
1325 EXPECT_TRUE(delegate->tap_down());
1327 // We haven't pressed long enough for a long press to occur
1328 EXPECT_FALSE(delegate->long_press());
1329 EXPECT_FALSE(delegate->tap_cancel());
1331 // Scroll around, to cancel the long press
1332 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1334 // Wait until a long press event would have fired, if it hadn't been
1335 // cancelled.
1336 DelayByLongPressTimeout();
1338 EXPECT_FALSE(delegate->long_press());
1339 EXPECT_TRUE(delegate->tap_cancel());
1341 delegate->Reset();
1342 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1343 kTouchId, tes.LeapForward(10));
1344 DispatchEventUsingWindowDispatcher(&release1);
1345 EXPECT_FALSE(delegate->long_press());
1346 EXPECT_FALSE(delegate->tap_cancel());
1349 // Check that appropriate touch events generate long tap events
1350 TEST_F(GestureRecognizerTest, GestureEventLongTap) {
1351 ui::GestureConfiguration::set_max_touch_down_duration_in_seconds_for_click(
1352 0.0025);
1353 scoped_ptr<GestureEventConsumeDelegate> delegate(
1354 new GestureEventConsumeDelegate());
1355 const int kWindowWidth = 123;
1356 const int kWindowHeight = 45;
1357 const int kTouchId = 2;
1358 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1359 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1360 delegate.get(), -1234, bounds, root_window()));
1362 delegate->Reset();
1364 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1365 gfx::Point(101, 201),
1366 kTouchId,
1367 ui::EventTimeForNow());
1368 DispatchEventUsingWindowDispatcher(&press1);
1369 EXPECT_TRUE(delegate->tap_down());
1370 EXPECT_TRUE(delegate->begin());
1371 EXPECT_FALSE(delegate->tap_cancel());
1373 // We haven't pressed long enough for a long press to occur
1374 EXPECT_FALSE(delegate->long_press());
1376 // Wait until the timer runs out
1377 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1378 EXPECT_TRUE(delegate->long_press());
1379 EXPECT_FALSE(delegate->tap_cancel());
1381 delegate->Reset();
1382 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1383 gfx::Point(101, 201),
1384 kTouchId,
1385 ui::EventTimeForNow());
1386 DispatchEventUsingWindowDispatcher(&release1);
1387 EXPECT_FALSE(delegate->long_press());
1388 EXPECT_TRUE(delegate->long_tap());
1390 // Note the tap cancel isn't dispatched until the release
1391 EXPECT_TRUE(delegate->tap_cancel());
1392 EXPECT_FALSE(delegate->tap());
1395 // Check that second tap cancels a long press
1396 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledBySecondTap) {
1397 scoped_ptr<GestureEventConsumeDelegate> delegate(
1398 new GestureEventConsumeDelegate());
1399 TimedEvents tes;
1400 const int kWindowWidth = 300;
1401 const int kWindowHeight = 400;
1402 const int kTouchId1 = 8;
1403 const int kTouchId2 = 2;
1404 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1405 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1406 delegate.get(), -1234, bounds, root_window()));
1408 delegate->Reset();
1409 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1410 kTouchId1, tes.Now());
1411 DispatchEventUsingWindowDispatcher(&press);
1412 EXPECT_TRUE(delegate->tap_down());
1413 EXPECT_TRUE(delegate->begin());
1415 // We haven't pressed long enough for a long press to occur
1416 EXPECT_FALSE(delegate->long_press());
1418 // Second tap, to cancel the long press
1419 delegate->Reset();
1420 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1421 kTouchId2, tes.Now());
1422 DispatchEventUsingWindowDispatcher(&press2);
1423 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
1424 EXPECT_TRUE(delegate->tap_cancel());
1425 EXPECT_TRUE(delegate->begin());
1427 // Wait until the timer runs out
1428 DelayByLongPressTimeout();
1430 // No long press occurred
1431 EXPECT_FALSE(delegate->long_press());
1433 delegate->Reset();
1434 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1435 kTouchId1, tes.Now());
1436 DispatchEventUsingWindowDispatcher(&release1);
1437 EXPECT_FALSE(delegate->long_press());
1438 EXPECT_TRUE(delegate->two_finger_tap());
1439 EXPECT_FALSE(delegate->tap_cancel());
1442 // Check that horizontal scroll gestures cause scrolls on horizontal rails.
1443 // Also tests that horizontal rails can be broken.
1444 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailScroll) {
1445 scoped_ptr<GestureEventConsumeDelegate> delegate(
1446 new GestureEventConsumeDelegate());
1447 TimedEvents tes;
1448 const int kTouchId = 7;
1449 gfx::Rect bounds(0, 0, 1000, 1000);
1450 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1451 delegate.get(), -1234, bounds, root_window()));
1453 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1454 kTouchId, tes.Now());
1455 DispatchEventUsingWindowDispatcher(&press);
1457 // Get rid of touch slop.
1458 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(5, 0),
1459 kTouchId, tes.Now());
1461 DispatchEventUsingWindowDispatcher(&move);
1462 delegate->Reset();
1464 // Move the touch-point horizontally enough that it is considered a
1465 // horizontal scroll.
1466 tes.SendScrollEvent(event_processor(), 25, 0, kTouchId, delegate.get());
1467 EXPECT_EQ(0, delegate->scroll_y());
1468 EXPECT_EQ(20, delegate->scroll_x());
1470 tes.SendScrollEvent(event_processor(), 30, 6, kTouchId, delegate.get());
1471 EXPECT_TRUE(delegate->scroll_update());
1472 EXPECT_EQ(5, delegate->scroll_x());
1473 // y shouldn't change, as we're on a horizontal rail.
1474 EXPECT_EQ(0, delegate->scroll_y());
1476 // Send enough information that a velocity can be calculated for the gesture,
1477 // and we can break the rail
1478 tes.SendScrollEvents(event_processor(), 1, 1,
1479 6, 100, kTouchId, 1,
1480 ui::GestureConfiguration::points_buffered_for_velocity(),
1481 delegate.get());
1483 tes.SendScrollEvent(event_processor(), 5, 0, kTouchId, delegate.get());
1484 tes.SendScrollEvent(event_processor(), 10, 5, kTouchId, delegate.get());
1486 // The rail should be broken
1487 EXPECT_TRUE(delegate->scroll_update());
1488 EXPECT_EQ(5, delegate->scroll_x());
1489 EXPECT_EQ(5, delegate->scroll_y());
1492 // Check that vertical scroll gestures cause scrolls on vertical rails.
1493 // Also tests that vertical rails can be broken.
1494 TEST_F(GestureRecognizerTest, GestureEventVerticalRailScroll) {
1495 scoped_ptr<GestureEventConsumeDelegate> delegate(
1496 new GestureEventConsumeDelegate());
1497 TimedEvents tes;
1498 const int kTouchId = 7;
1499 gfx::Rect bounds(0, 0, 1000, 1000);
1500 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1501 delegate.get(), -1234, bounds, root_window()));
1503 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1504 kTouchId, tes.Now());
1505 DispatchEventUsingWindowDispatcher(&press);
1507 // Get rid of touch slop.
1508 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 5),
1509 kTouchId, tes.Now());
1510 DispatchEventUsingWindowDispatcher(&move);
1511 delegate->Reset();
1513 // Move the touch-point vertically enough that it is considered a
1514 // vertical scroll.
1515 tes.SendScrollEvent(event_processor(), 0, 25, kTouchId, delegate.get());
1516 EXPECT_EQ(0, delegate->scroll_x());
1517 EXPECT_EQ(20, delegate->scroll_y());
1519 tes.SendScrollEvent(event_processor(), 6, 30, kTouchId, delegate.get());
1520 EXPECT_TRUE(delegate->scroll_update());
1521 EXPECT_EQ(5, delegate->scroll_y());
1522 // x shouldn't change, as we're on a vertical rail.
1523 EXPECT_EQ(0, delegate->scroll_x());
1524 EXPECT_EQ(0, delegate->scroll_velocity_x());
1526 // Send enough information that a velocity can be calculated for the gesture,
1527 // and we can break the rail
1528 tes.SendScrollEvents(event_processor(), 1, 6,
1529 100, 1, kTouchId, 1,
1530 ui::GestureConfiguration::points_buffered_for_velocity(),
1531 delegate.get());
1533 tes.SendScrollEvent(event_processor(), 0, 5, kTouchId, delegate.get());
1534 tes.SendScrollEvent(event_processor(), 5, 10, kTouchId, delegate.get());
1536 // The rail should be broken
1537 EXPECT_TRUE(delegate->scroll_update());
1538 EXPECT_EQ(5, delegate->scroll_x());
1539 EXPECT_EQ(5, delegate->scroll_y());
1542 TEST_F(GestureRecognizerTest, GestureTapFollowedByScroll) {
1543 // We'll start by moving the touch point by (5, 5). We want all of that
1544 // distance to be consumed by the slop, so we set the slop radius to
1545 // sqrt(5 * 5 + 5 * 5).
1546 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(
1547 sqrt(static_cast<double>(5 * 5 + 5 * 5)));
1549 // First, tap. Then, do a scroll using the same touch-id.
1550 scoped_ptr<GestureEventConsumeDelegate> delegate(
1551 new GestureEventConsumeDelegate());
1552 TimedEvents tes;
1553 const int kWindowWidth = 123;
1554 const int kWindowHeight = 45;
1555 const int kTouchId = 3;
1556 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1557 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1558 delegate.get(), -1234, bounds, root_window()));
1560 delegate->Reset();
1561 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1562 kTouchId, tes.Now());
1563 DispatchEventUsingWindowDispatcher(&press);
1564 EXPECT_FALSE(delegate->tap());
1565 EXPECT_TRUE(delegate->tap_down());
1566 EXPECT_FALSE(delegate->tap_cancel());
1567 EXPECT_FALSE(delegate->scroll_begin());
1568 EXPECT_FALSE(delegate->scroll_update());
1569 EXPECT_FALSE(delegate->scroll_end());
1571 // Make sure there is enough delay before the touch is released so that it is
1572 // recognized as a tap.
1573 delegate->Reset();
1574 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1575 kTouchId, tes.LeapForward(50));
1576 DispatchEventUsingWindowDispatcher(&release);
1577 EXPECT_TRUE(delegate->tap());
1578 EXPECT_FALSE(delegate->tap_down());
1579 EXPECT_FALSE(delegate->tap_cancel());
1580 EXPECT_FALSE(delegate->scroll_begin());
1581 EXPECT_FALSE(delegate->scroll_update());
1582 EXPECT_FALSE(delegate->scroll_end());
1584 // Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger
1585 // a double-tap.
1586 delegate->Reset();
1587 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1588 kTouchId, tes.LeapForward(1000));
1589 DispatchEventUsingWindowDispatcher(&press1);
1590 EXPECT_FALSE(delegate->tap());
1591 EXPECT_TRUE(delegate->tap_down());
1592 EXPECT_FALSE(delegate->tap_cancel());
1593 EXPECT_FALSE(delegate->scroll_begin());
1594 EXPECT_FALSE(delegate->scroll_update());
1595 EXPECT_FALSE(delegate->scroll_end());
1597 // Get rid of touch slop.
1598 ui::TouchEvent move_remove_slop(ui::ET_TOUCH_MOVED, gfx::Point(116, 216),
1599 kTouchId, tes.Now());
1600 DispatchEventUsingWindowDispatcher(&move_remove_slop);
1601 EXPECT_TRUE(delegate->tap_cancel());
1602 EXPECT_TRUE(delegate->scroll_begin());
1603 EXPECT_TRUE(delegate->scroll_update());
1604 EXPECT_EQ(15, delegate->scroll_x_hint());
1605 EXPECT_EQ(15, delegate->scroll_y_hint());
1607 delegate->Reset();
1609 // Move the touch-point enough so that it is considered as a scroll. This
1610 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1611 // The first movement is diagonal, to ensure that we have a free scroll,
1612 // and not a rail scroll.
1613 delegate->Reset();
1614 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(135, 235),
1615 kTouchId, tes.Now());
1616 DispatchEventUsingWindowDispatcher(&move);
1617 EXPECT_FALSE(delegate->tap());
1618 EXPECT_FALSE(delegate->tap_down());
1619 EXPECT_FALSE(delegate->tap_cancel());
1620 EXPECT_FALSE(delegate->scroll_begin());
1621 EXPECT_TRUE(delegate->scroll_update());
1622 EXPECT_FALSE(delegate->scroll_end());
1623 EXPECT_EQ(19, delegate->scroll_x());
1624 EXPECT_EQ(19, delegate->scroll_y());
1626 // Move some more to generate a few more scroll updates.
1627 delegate->Reset();
1628 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(115, 216),
1629 kTouchId, tes.Now());
1630 DispatchEventUsingWindowDispatcher(&move1);
1631 EXPECT_FALSE(delegate->tap());
1632 EXPECT_FALSE(delegate->tap_down());
1633 EXPECT_FALSE(delegate->tap_cancel());
1634 EXPECT_FALSE(delegate->scroll_begin());
1635 EXPECT_TRUE(delegate->scroll_update());
1636 EXPECT_FALSE(delegate->scroll_end());
1637 EXPECT_EQ(-20, delegate->scroll_x());
1638 EXPECT_EQ(-19, delegate->scroll_y());
1639 EXPECT_EQ(0, delegate->scroll_x_hint());
1640 EXPECT_EQ(0, delegate->scroll_y_hint());
1642 delegate->Reset();
1643 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 220),
1644 kTouchId, tes.Now());
1645 DispatchEventUsingWindowDispatcher(&move2);
1646 EXPECT_FALSE(delegate->tap());
1647 EXPECT_FALSE(delegate->tap_down());
1648 EXPECT_FALSE(delegate->tap_cancel());
1649 EXPECT_FALSE(delegate->scroll_begin());
1650 EXPECT_TRUE(delegate->scroll_update());
1651 EXPECT_FALSE(delegate->scroll_end());
1652 EXPECT_EQ(30, delegate->scroll_x());
1653 EXPECT_EQ(4, delegate->scroll_y());
1655 // Release the touch. This should end the scroll.
1656 delegate->Reset();
1657 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1658 kTouchId, tes.Now());
1659 DispatchEventUsingWindowDispatcher(&release1);
1660 EXPECT_FALSE(delegate->tap());
1661 EXPECT_FALSE(delegate->tap_down());
1662 EXPECT_FALSE(delegate->tap_cancel());
1663 EXPECT_FALSE(delegate->scroll_begin());
1664 EXPECT_FALSE(delegate->scroll_update());
1665 EXPECT_FALSE(delegate->scroll_end());
1666 EXPECT_TRUE(delegate->fling());
1669 TEST_F(GestureRecognizerTest, AsynchronousGestureRecognition) {
1670 scoped_ptr<QueueTouchEventDelegate> queued_delegate(
1671 new QueueTouchEventDelegate(host()->dispatcher()));
1672 TimedEvents tes;
1673 const int kWindowWidth = 123;
1674 const int kWindowHeight = 45;
1675 const int kTouchId1 = 6;
1676 const int kTouchId2 = 4;
1677 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1678 scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate(
1679 queued_delegate.get(), -1234, bounds, root_window()));
1681 queued_delegate->set_window(queue.get());
1683 // Touch down on the window. This should not generate any gesture event.
1684 queued_delegate->Reset();
1685 ui::TouchEvent press(
1686 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
1687 DispatchEventUsingWindowDispatcher(&press);
1688 EXPECT_FALSE(queued_delegate->tap());
1689 EXPECT_FALSE(queued_delegate->tap_down());
1690 EXPECT_FALSE(queued_delegate->tap_cancel());
1691 EXPECT_FALSE(queued_delegate->begin());
1692 EXPECT_FALSE(queued_delegate->scroll_begin());
1693 EXPECT_FALSE(queued_delegate->scroll_update());
1694 EXPECT_FALSE(queued_delegate->scroll_end());
1696 // Introduce some delay before the touch is released so that it is recognized
1697 // as a tap. However, this still should not create any gesture events.
1698 queued_delegate->Reset();
1699 ui::TouchEvent release(
1700 ui::ET_TOUCH_RELEASED,
1701 gfx::Point(101, 201),
1702 kTouchId1,
1703 press.time_stamp() + base::TimeDelta::FromMilliseconds(50));
1704 DispatchEventUsingWindowDispatcher(&release);
1705 EXPECT_FALSE(queued_delegate->tap());
1706 EXPECT_FALSE(queued_delegate->tap_down());
1707 EXPECT_FALSE(queued_delegate->tap_cancel());
1708 EXPECT_FALSE(queued_delegate->begin());
1709 EXPECT_FALSE(queued_delegate->end());
1710 EXPECT_FALSE(queued_delegate->scroll_begin());
1711 EXPECT_FALSE(queued_delegate->scroll_update());
1712 EXPECT_FALSE(queued_delegate->scroll_end());
1714 // Create another window, and place a touch-down on it. This should create a
1715 // tap-down gesture.
1716 scoped_ptr<GestureEventConsumeDelegate> delegate(
1717 new GestureEventConsumeDelegate());
1718 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1719 delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), root_window()));
1720 delegate->Reset();
1721 ui::TouchEvent press2(
1722 ui::ET_TOUCH_PRESSED, gfx::Point(10, 20), kTouchId2, tes.Now());
1723 DispatchEventUsingWindowDispatcher(&press2);
1724 EXPECT_FALSE(delegate->tap());
1725 EXPECT_TRUE(delegate->tap_down());
1726 EXPECT_FALSE(delegate->tap_cancel());
1727 EXPECT_FALSE(queued_delegate->begin());
1728 EXPECT_FALSE(queued_delegate->end());
1729 EXPECT_FALSE(delegate->scroll_begin());
1730 EXPECT_FALSE(delegate->scroll_update());
1731 EXPECT_FALSE(delegate->scroll_end());
1733 ui::TouchEvent release2(
1734 ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), kTouchId2, tes.Now());
1735 DispatchEventUsingWindowDispatcher(&release2);
1737 // Process the first queued event.
1738 queued_delegate->Reset();
1739 queued_delegate->ReceivedAck();
1740 EXPECT_FALSE(queued_delegate->tap());
1741 EXPECT_TRUE(queued_delegate->tap_down());
1742 EXPECT_TRUE(queued_delegate->begin());
1743 EXPECT_FALSE(queued_delegate->tap_cancel());
1744 EXPECT_FALSE(queued_delegate->end());
1745 EXPECT_FALSE(queued_delegate->scroll_begin());
1746 EXPECT_FALSE(queued_delegate->scroll_update());
1747 EXPECT_FALSE(queued_delegate->scroll_end());
1749 // Now, process the second queued event.
1750 queued_delegate->Reset();
1751 queued_delegate->ReceivedAck();
1752 EXPECT_TRUE(queued_delegate->tap());
1753 EXPECT_FALSE(queued_delegate->tap_down());
1754 EXPECT_FALSE(queued_delegate->tap_cancel());
1755 EXPECT_FALSE(queued_delegate->begin());
1756 EXPECT_TRUE(queued_delegate->end());
1757 EXPECT_FALSE(queued_delegate->scroll_begin());
1758 EXPECT_FALSE(queued_delegate->scroll_update());
1759 EXPECT_FALSE(queued_delegate->scroll_end());
1761 // Start all over. Press on the first window, then press again on the second
1762 // window. The second press should still go to the first window.
1763 queued_delegate->Reset();
1764 ui::TouchEvent press3(
1765 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
1766 DispatchEventUsingWindowDispatcher(&press3);
1767 EXPECT_FALSE(queued_delegate->tap());
1768 EXPECT_FALSE(queued_delegate->tap_down());
1769 EXPECT_FALSE(queued_delegate->tap_cancel());
1770 EXPECT_FALSE(queued_delegate->begin());
1771 EXPECT_FALSE(queued_delegate->end());
1772 EXPECT_FALSE(queued_delegate->begin());
1773 EXPECT_FALSE(queued_delegate->end());
1774 EXPECT_FALSE(queued_delegate->scroll_begin());
1775 EXPECT_FALSE(queued_delegate->scroll_update());
1776 EXPECT_FALSE(queued_delegate->scroll_end());
1778 queued_delegate->Reset();
1779 delegate->Reset();
1780 ui::TouchEvent press4(
1781 ui::ET_TOUCH_PRESSED, gfx::Point(103, 203), kTouchId2, tes.Now());
1782 DispatchEventUsingWindowDispatcher(&press4);
1783 EXPECT_FALSE(delegate->tap());
1784 EXPECT_FALSE(delegate->tap_down());
1785 EXPECT_FALSE(delegate->tap_cancel());
1786 EXPECT_FALSE(delegate->begin());
1787 EXPECT_FALSE(delegate->end());
1788 EXPECT_FALSE(delegate->scroll_begin());
1789 EXPECT_FALSE(delegate->scroll_update());
1790 EXPECT_FALSE(delegate->scroll_end());
1791 EXPECT_FALSE(queued_delegate->tap());
1792 EXPECT_FALSE(queued_delegate->tap_down());
1793 EXPECT_FALSE(queued_delegate->tap_cancel());
1794 EXPECT_FALSE(queued_delegate->begin());
1795 EXPECT_FALSE(queued_delegate->end());
1796 EXPECT_FALSE(queued_delegate->scroll_begin());
1797 EXPECT_FALSE(queued_delegate->scroll_update());
1798 EXPECT_FALSE(queued_delegate->scroll_end());
1800 // Move the second touch-point enough so that it is considered a pinch. This
1801 // should generate both SCROLL_BEGIN and PINCH_BEGIN gestures.
1802 queued_delegate->Reset();
1803 delegate->Reset();
1804 int x_move = ui::GestureConfiguration::max_touch_move_in_pixels_for_click();
1805 ui::TouchEvent move(
1806 ui::ET_TOUCH_MOVED, gfx::Point(203 + x_move, 303), kTouchId2, tes.Now());
1807 DispatchEventUsingWindowDispatcher(&move);
1808 EXPECT_FALSE(delegate->tap());
1809 EXPECT_FALSE(delegate->tap_down());
1810 EXPECT_FALSE(delegate->tap_cancel());
1811 EXPECT_FALSE(delegate->begin());
1812 EXPECT_FALSE(delegate->scroll_begin());
1813 EXPECT_FALSE(delegate->scroll_update());
1814 EXPECT_FALSE(delegate->scroll_end());
1815 EXPECT_FALSE(queued_delegate->tap());
1816 EXPECT_FALSE(queued_delegate->tap_down());
1817 EXPECT_FALSE(queued_delegate->tap_cancel());
1818 EXPECT_FALSE(queued_delegate->begin());
1819 EXPECT_FALSE(queued_delegate->scroll_begin());
1820 EXPECT_FALSE(queued_delegate->scroll_update());
1821 EXPECT_FALSE(queued_delegate->scroll_end());
1823 queued_delegate->Reset();
1824 queued_delegate->ReceivedAck();
1825 EXPECT_FALSE(queued_delegate->tap());
1826 EXPECT_TRUE(queued_delegate->tap_down());
1827 EXPECT_TRUE(queued_delegate->begin());
1828 EXPECT_FALSE(queued_delegate->tap_cancel());
1829 EXPECT_FALSE(queued_delegate->end());
1830 EXPECT_FALSE(queued_delegate->scroll_begin());
1831 EXPECT_FALSE(queued_delegate->scroll_update());
1832 EXPECT_FALSE(queued_delegate->scroll_end());
1834 queued_delegate->Reset();
1835 queued_delegate->ReceivedAck();
1836 EXPECT_FALSE(queued_delegate->tap());
1837 EXPECT_FALSE(queued_delegate->tap_down()); // no touch down for second tap.
1838 EXPECT_TRUE(queued_delegate->tap_cancel());
1839 EXPECT_TRUE(queued_delegate->begin());
1840 EXPECT_FALSE(queued_delegate->end());
1841 EXPECT_FALSE(queued_delegate->scroll_begin());
1842 EXPECT_FALSE(queued_delegate->scroll_update());
1843 EXPECT_FALSE(queued_delegate->scroll_end());
1844 EXPECT_FALSE(queued_delegate->pinch_begin());
1845 EXPECT_FALSE(queued_delegate->pinch_update());
1846 EXPECT_FALSE(queued_delegate->pinch_end());
1848 queued_delegate->Reset();
1849 queued_delegate->ReceivedAck();
1850 EXPECT_FALSE(queued_delegate->tap());
1851 EXPECT_FALSE(queued_delegate->tap_down());
1852 EXPECT_FALSE(queued_delegate->tap_cancel());
1853 EXPECT_FALSE(queued_delegate->begin());
1854 EXPECT_FALSE(queued_delegate->end());
1855 EXPECT_TRUE(queued_delegate->scroll_begin());
1857 EXPECT_TRUE(queued_delegate->scroll_update());
1858 EXPECT_FALSE(queued_delegate->scroll_end());
1859 EXPECT_TRUE(queued_delegate->pinch_begin());
1860 EXPECT_FALSE(queued_delegate->pinch_update());
1861 EXPECT_FALSE(queued_delegate->pinch_end());
1864 // Check that appropriate touch events generate pinch gesture events.
1865 TEST_F(GestureRecognizerTest, GestureEventPinchFromScroll) {
1866 scoped_ptr<GestureEventConsumeDelegate> delegate(
1867 new GestureEventConsumeDelegate());
1868 TimedEvents tes;
1869 const int kWindowWidth = 300;
1870 const int kWindowHeight = 400;
1871 const int kTouchId1 = 5;
1872 const int kTouchId2 = 3;
1873 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1874 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1875 delegate.get(), -1234, bounds, root_window()));
1877 delegate->Reset();
1878 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1879 kTouchId1, tes.Now());
1880 DispatchEventUsingWindowDispatcher(&press);
1881 EXPECT_2_EVENTS(delegate->events(),
1882 ui::ET_GESTURE_BEGIN,
1883 ui::ET_GESTURE_TAP_DOWN);
1885 // Move the touch-point enough so that it is considered as a scroll. This
1886 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1887 delegate->Reset();
1888 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 301),
1889 kTouchId1, tes.Now());
1890 DispatchEventUsingWindowDispatcher(&move);
1891 EXPECT_3_EVENTS(delegate->events(),
1892 ui::ET_GESTURE_TAP_CANCEL,
1893 ui::ET_GESTURE_SCROLL_BEGIN,
1894 ui::ET_GESTURE_SCROLL_UPDATE);
1896 // Press the second finger. It should cause pinch-begin. Note that we will not
1897 // transition to two finger tap here because the touch points are far enough.
1898 delegate->Reset();
1899 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1900 kTouchId2, tes.Now());
1901 DispatchEventUsingWindowDispatcher(&press2);
1902 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_BEGIN);
1903 EXPECT_EQ(gfx::Rect(10, 10, 120, 291).ToString(),
1904 delegate->bounding_box().ToString());
1906 // Move the first finger.
1907 delegate->Reset();
1908 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(95, 201),
1909 kTouchId1, tes.Now());
1910 DispatchEventUsingWindowDispatcher(&move3);
1911 EXPECT_2_EVENTS(delegate->events(),
1912 ui::ET_GESTURE_SCROLL_UPDATE,
1913 ui::ET_GESTURE_PINCH_BEGIN);
1914 EXPECT_EQ(gfx::Rect(10, 10, 85, 191).ToString(),
1915 delegate->bounding_box().ToString());
1917 // Now move the second finger.
1918 delegate->Reset();
1919 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
1920 kTouchId2, tes.Now());
1921 DispatchEventUsingWindowDispatcher(&move4);
1922 EXPECT_2_EVENTS(delegate->events(),
1923 ui::ET_GESTURE_SCROLL_UPDATE,
1924 ui::ET_GESTURE_PINCH_UPDATE);
1925 EXPECT_EQ(gfx::Rect(55, 15, 40, 186).ToString(),
1926 delegate->bounding_box().ToString());
1928 // Release the first finger. This should end pinch.
1929 delegate->Reset();
1930 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1931 kTouchId1, tes.Now());
1932 DispatchEventUsingWindowDispatcher(&release);
1933 EXPECT_2_EVENTS(delegate->events(),
1934 ui::ET_GESTURE_PINCH_END,
1935 ui::ET_GESTURE_END);
1936 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
1937 delegate->bounding_box().ToString());
1939 // Move the second finger. This should still generate a scroll.
1940 delegate->Reset();
1941 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
1942 kTouchId2, tes.Now());
1943 DispatchEventUsingWindowDispatcher(&move5);
1944 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1945 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1948 TEST_F(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) {
1949 scoped_ptr<GestureEventConsumeDelegate> delegate(
1950 new GestureEventConsumeDelegate());
1951 TimedEvents tes;
1952 const int kWindowWidth = 300;
1953 const int kWindowHeight = 400;
1954 const int kTouchId1 = 5;
1955 const int kTouchId2 = 3;
1956 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1957 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1958 delegate.get(), -1234, bounds, root_window()));
1960 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
1961 kTouchId1, tes.Now());
1962 DispatchEventUsingWindowDispatcher(&press);
1963 delegate->Reset();
1964 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1965 kTouchId2, tes.Now());
1966 DispatchEventUsingWindowDispatcher(&press2);
1967 EXPECT_FALSE(delegate->pinch_begin());
1969 // Touch move triggers pinch begin.
1970 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
1971 EXPECT_TRUE(delegate->pinch_begin());
1972 EXPECT_FALSE(delegate->pinch_update());
1974 // Touch move triggers pinch update.
1975 tes.SendScrollEvent(event_processor(), 160, 200, kTouchId1, delegate.get());
1976 EXPECT_FALSE(delegate->pinch_begin());
1977 EXPECT_TRUE(delegate->pinch_update());
1979 // Pinch has started, now release the second finger
1980 delegate->Reset();
1981 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1982 kTouchId1, tes.Now());
1983 DispatchEventUsingWindowDispatcher(&release);
1984 EXPECT_TRUE(delegate->pinch_end());
1986 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId2, delegate.get());
1987 EXPECT_TRUE(delegate->scroll_update());
1989 // Pinch again
1990 delegate->Reset();
1991 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1992 kTouchId1, tes.Now());
1993 DispatchEventUsingWindowDispatcher(&press3);
1994 // Now the touch points are close. So we will go into two finger tap.
1995 // Move the touch-point enough to break two-finger-tap and enter pinch.
1996 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 50),
1997 kTouchId1, tes.Now());
1998 DispatchEventUsingWindowDispatcher(&move2);
1999 EXPECT_TRUE(delegate->pinch_begin());
2001 tes.SendScrollEvent(event_processor(), 350, 350, kTouchId1, delegate.get());
2002 EXPECT_TRUE(delegate->pinch_update());
2005 TEST_F(GestureRecognizerTest, GestureEventPinchFromTap) {
2006 scoped_ptr<GestureEventConsumeDelegate> delegate(
2007 new GestureEventConsumeDelegate());
2008 TimedEvents tes;
2009 const int kWindowWidth = 300;
2010 const int kWindowHeight = 400;
2011 const int kTouchId1 = 3;
2012 const int kTouchId2 = 5;
2013 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2014 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2015 delegate.get(), -1234, bounds, root_window()));
2017 delegate->Reset();
2018 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2019 kTouchId1, tes.Now());
2020 DispatchEventUsingWindowDispatcher(&press);
2021 EXPECT_2_EVENTS(delegate->events(),
2022 ui::ET_GESTURE_BEGIN,
2023 ui::ET_GESTURE_TAP_DOWN);
2024 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2026 // Press the second finger far enough to break two finger tap.
2027 delegate->Reset();
2028 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2029 kTouchId2, tes.Now());
2030 DispatchEventUsingWindowDispatcher(&press2);
2031 EXPECT_2_EVENTS(delegate->events(),
2032 ui::ET_GESTURE_TAP_CANCEL,
2033 ui::ET_GESTURE_BEGIN);
2034 EXPECT_EQ(gfx::Rect(10, 10, 91, 291).ToString(),
2035 delegate->bounding_box().ToString());
2037 // Move the first finger.
2038 delegate->Reset();
2039 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
2040 kTouchId1, tes.Now());
2041 DispatchEventUsingWindowDispatcher(&move3);
2042 EXPECT_3_EVENTS(delegate->events(),
2043 ui::ET_GESTURE_SCROLL_BEGIN,
2044 ui::ET_GESTURE_SCROLL_UPDATE,
2045 ui::ET_GESTURE_PINCH_BEGIN);
2046 EXPECT_EQ(gfx::Rect(10, 10, 55, 191).ToString(),
2047 delegate->bounding_box().ToString());
2049 // Now move the second finger.
2050 delegate->Reset();
2051 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
2052 kTouchId2, tes.Now());
2053 DispatchEventUsingWindowDispatcher(&move4);
2054 EXPECT_2_EVENTS(delegate->events(),
2055 ui::ET_GESTURE_SCROLL_UPDATE,
2056 ui::ET_GESTURE_PINCH_UPDATE);
2057 EXPECT_EQ(gfx::Rect(55, 15, 10, 186).ToString(),
2058 delegate->bounding_box().ToString());
2060 // Release the first finger. This should end pinch.
2061 delegate->Reset();
2062 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2063 kTouchId1, tes.LeapForward(10));
2064 DispatchEventUsingWindowDispatcher(&release);
2065 EXPECT_2_EVENTS(delegate->events(),
2066 ui::ET_GESTURE_PINCH_END,
2067 ui::ET_GESTURE_END);
2068 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
2069 delegate->bounding_box().ToString());
2071 // Move the second finger. This should still generate a scroll.
2072 delegate->Reset();
2073 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2074 kTouchId2, tes.Now());
2075 DispatchEventUsingWindowDispatcher(&move5);
2076 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2077 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2080 TEST_F(GestureRecognizerTest, GestureEventIgnoresDisconnectedEvents) {
2081 scoped_ptr<GestureEventConsumeDelegate> delegate(
2082 new GestureEventConsumeDelegate());
2083 TimedEvents tes;
2085 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2086 6, tes.Now());
2087 DispatchEventUsingWindowDispatcher(&release1);
2088 EXPECT_FALSE(delegate->tap());
2089 EXPECT_FALSE(delegate->tap_down());
2092 // Check that a touch is locked to the window of the closest current touch
2093 // within max_separation_for_gesture_touches_in_pixels
2094 TEST_F(GestureRecognizerTest, GestureEventTouchLockSelectsCorrectWindow) {
2095 ui::GestureRecognizer* gesture_recognizer = new ui::GestureRecognizerImpl();
2096 TimedEvents tes;
2097 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2099 ui::GestureConsumer* target;
2100 const int kNumWindows = 4;
2102 scoped_ptr<GestureEventConsumeDelegate*[]> delegates(
2103 new GestureEventConsumeDelegate*[kNumWindows]);
2105 ui::GestureConfiguration::
2106 set_max_separation_for_gesture_touches_in_pixels(499);
2108 scoped_ptr<gfx::Rect[]> window_bounds(new gfx::Rect[kNumWindows]);
2109 window_bounds[0] = gfx::Rect(0, 0, 1, 1);
2110 window_bounds[1] = gfx::Rect(500, 0, 1, 1);
2111 window_bounds[2] = gfx::Rect(0, 500, 1, 1);
2112 window_bounds[3] = gfx::Rect(500, 500, 1, 1);
2114 scoped_ptr<aura::Window*[]> windows(new aura::Window*[kNumWindows]);
2116 // Instantiate windows with |window_bounds| and touch each window at
2117 // its origin.
2118 for (int i = 0; i < kNumWindows; ++i) {
2119 delegates[i] = new GestureEventConsumeDelegate();
2120 windows[i] = CreateTestWindowWithDelegate(
2121 delegates[i], i, window_bounds[i], root_window());
2122 windows[i]->set_id(i);
2123 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, window_bounds[i].origin(),
2124 i, tes.Now());
2125 DispatchEventUsingWindowDispatcher(&press);
2128 // Touches should now be associated with the closest touch within
2129 // ui::GestureConfiguration::max_separation_for_gesture_touches_in_pixels
2130 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 11), -1);
2131 EXPECT_EQ("0", WindowIDAsString(target));
2132 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 11), -1);
2133 EXPECT_EQ("1", WindowIDAsString(target));
2134 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 511), -1);
2135 EXPECT_EQ("2", WindowIDAsString(target));
2136 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 511), -1);
2137 EXPECT_EQ("3", WindowIDAsString(target));
2139 // Add a touch in the middle associated with windows[2]
2140 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 500),
2141 kNumWindows, tes.Now());
2142 DispatchEventUsingWindowDispatcher(&press);
2143 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(250, 250),
2144 kNumWindows, tes.Now());
2145 DispatchEventUsingWindowDispatcher(&move);
2147 target = gesture_recognizer->GetTargetForLocation(gfx::Point(250, 250), -1);
2148 EXPECT_EQ("2", WindowIDAsString(target));
2150 // Make sure that ties are broken by distance to a current touch
2151 // Closer to the point in the bottom right.
2152 target = gesture_recognizer->GetTargetForLocation(gfx::Point(380, 380), -1);
2153 EXPECT_EQ("3", WindowIDAsString(target));
2155 // This touch is closer to the point in the middle
2156 target = gesture_recognizer->GetTargetForLocation(gfx::Point(300, 300), -1);
2157 EXPECT_EQ("2", WindowIDAsString(target));
2159 // A touch too far from other touches won't be locked to anything
2160 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2161 EXPECT_TRUE(target == NULL);
2163 // Move a touch associated with windows[2] to 1000, 1000
2164 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(1000, 1000),
2165 kNumWindows, tes.Now());
2166 DispatchEventUsingWindowDispatcher(&move2);
2168 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2169 EXPECT_EQ("2", WindowIDAsString(target));
2171 for (int i = 0; i < kNumWindows; ++i) {
2172 // Delete windows before deleting delegates.
2173 delete windows[i];
2174 delete delegates[i];
2178 // Check that a touch's target will not be effected by a touch on a different
2179 // screen.
2180 TEST_F(GestureRecognizerTest, GestureEventTouchLockIgnoresOtherScreens) {
2181 scoped_ptr<GestureEventConsumeDelegate> delegate(
2182 new GestureEventConsumeDelegate());
2183 gfx::Rect bounds(0, 0, 10, 10);
2184 scoped_ptr<aura::Window> window(
2185 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2187 const int kTouchId1 = 8;
2188 const int kTouchId2 = 2;
2189 TimedEvents tes;
2191 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(5, 5),
2192 kTouchId1, tes.Now());
2193 ui::EventTestApi test_press1(&press1);
2194 test_press1.set_source_device_id(1);
2195 DispatchEventUsingWindowDispatcher(&press1);
2197 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2198 kTouchId2, tes.Now());
2199 ui::EventTestApi test_press2(&press2);
2200 test_press2.set_source_device_id(2);
2201 DispatchEventUsingWindowDispatcher(&press2);
2203 // The second press should not have been locked to the same target as the
2204 // first, as they occured on different displays.
2205 EXPECT_NE(
2206 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1),
2207 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2210 // Check that touch events outside the root window are still handled
2211 // by the root window's gesture sequence.
2212 TEST_F(GestureRecognizerTest, GestureEventOutsideRootWindowTap) {
2213 TimedEvents tes;
2214 scoped_ptr<aura::Window> window(CreateTestWindowWithBounds(
2215 gfx::Rect(-100, -100, 2000, 2000), root_window()));
2217 gfx::Point pos1(-10, -10);
2218 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, pos1, 0, tes.Now());
2219 DispatchEventUsingWindowDispatcher(&press1);
2221 gfx::Point pos2(1000, 1000);
2222 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, pos2, 1, tes.Now());
2223 DispatchEventUsingWindowDispatcher(&press2);
2225 // As these presses were outside the root window, they should be
2226 // associated with the root window.
2227 EXPECT_EQ(root_window(),
2228 static_cast<aura::Window*>(
2229 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1)));
2230 EXPECT_EQ(root_window(),
2231 static_cast<aura::Window*>(
2232 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2)));
2235 TEST_F(GestureRecognizerTest, NoTapWithPreventDefaultedRelease) {
2236 scoped_ptr<QueueTouchEventDelegate> delegate(
2237 new QueueTouchEventDelegate(host()->dispatcher()));
2238 TimedEvents tes;
2239 const int kTouchId = 2;
2240 gfx::Rect bounds(100, 200, 100, 100);
2241 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2242 delegate.get(), -1234, bounds, root_window()));
2243 delegate->set_window(window.get());
2245 delegate->Reset();
2246 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2247 kTouchId, tes.Now());
2248 DispatchEventUsingWindowDispatcher(&press);
2249 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2250 kTouchId, tes.LeapForward(50));
2251 DispatchEventUsingWindowDispatcher(&release);
2253 delegate->Reset();
2254 delegate->ReceivedAck();
2255 EXPECT_TRUE(delegate->tap_down());
2256 delegate->Reset();
2257 delegate->ReceivedAckPreventDefaulted();
2258 EXPECT_FALSE(delegate->tap());
2259 EXPECT_TRUE(delegate->tap_cancel());
2262 TEST_F(GestureRecognizerTest, PinchScrollWithPreventDefaultedRelease) {
2263 scoped_ptr<QueueTouchEventDelegate> delegate(
2264 new QueueTouchEventDelegate(host()->dispatcher()));
2265 TimedEvents tes;
2266 const int kTouchId1 = 7;
2267 const int kTouchId2 = 5;
2268 gfx::Rect bounds(10, 20, 100, 100);
2269 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2270 delegate.get(), -1234, bounds, root_window()));
2271 delegate->set_window(window.get());
2274 delegate->Reset();
2275 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2276 tes.Now());
2277 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2278 tes.LeapForward(200));
2279 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2280 tes.LeapForward(50));
2281 DispatchEventUsingWindowDispatcher(&press);
2282 DispatchEventUsingWindowDispatcher(&move);
2283 DispatchEventUsingWindowDispatcher(&release);
2284 delegate->Reset();
2286 // Ack the press event.
2287 delegate->ReceivedAck();
2288 EXPECT_2_EVENTS(
2289 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2290 delegate->Reset();
2292 // Ack the move event.
2293 delegate->ReceivedAck();
2294 EXPECT_3_EVENTS(delegate->events(),
2295 ui::ET_GESTURE_TAP_CANCEL,
2296 ui::ET_GESTURE_SCROLL_BEGIN,
2297 ui::ET_GESTURE_SCROLL_UPDATE);
2298 delegate->Reset();
2300 // Ack the release event. Although the release event has been processed, it
2301 // should still generate a scroll-end event.
2302 delegate->ReceivedAckPreventDefaulted();
2303 EXPECT_2_EVENTS(
2304 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2307 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2308 tes.Now());
2309 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2310 tes.LeapForward(200));
2311 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2312 tes.LeapForward(50));
2313 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 25), kTouchId2,
2314 tes.Now());
2315 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 85), kTouchId2,
2316 tes.LeapForward(1000));
2317 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(145, 85), kTouchId2,
2318 tes.LeapForward(14));
2320 // Do a pinch.
2321 DispatchEventUsingWindowDispatcher(&press);
2322 DispatchEventUsingWindowDispatcher(&move);
2323 DispatchEventUsingWindowDispatcher(&press2);
2324 DispatchEventUsingWindowDispatcher(&move2);
2325 DispatchEventUsingWindowDispatcher(&release);
2326 DispatchEventUsingWindowDispatcher(&release2);
2328 // Ack the press and move events.
2329 delegate->Reset();
2330 delegate->ReceivedAck();
2331 EXPECT_2_EVENTS(
2332 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2334 delegate->Reset();
2335 delegate->ReceivedAck();
2336 EXPECT_3_EVENTS(delegate->events(),
2337 ui::ET_GESTURE_TAP_CANCEL,
2338 ui::ET_GESTURE_SCROLL_BEGIN,
2339 ui::ET_GESTURE_SCROLL_UPDATE);
2341 delegate->Reset();
2342 delegate->ReceivedAck();
2343 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_BEGIN);
2345 delegate->Reset();
2346 delegate->ReceivedAck();
2347 EXPECT_2_EVENTS(delegate->events(),
2348 ui::ET_GESTURE_SCROLL_UPDATE,
2349 ui::ET_GESTURE_PINCH_BEGIN);
2351 // Ack the first release. Although the release is processed, it should still
2352 // generate a pinch-end event.
2353 delegate->Reset();
2354 delegate->ReceivedAckPreventDefaulted();
2355 EXPECT_2_EVENTS(
2356 delegate->events(), ui::ET_GESTURE_PINCH_END, ui::ET_GESTURE_END);
2358 delegate->Reset();
2359 delegate->ReceivedAckPreventDefaulted();
2360 EXPECT_2_EVENTS(
2361 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2364 TEST_F(GestureRecognizerTest, GestureEndLocation) {
2365 GestureEventConsumeDelegate delegate;
2366 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2367 &delegate, -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2368 ui::test::EventGenerator generator(root_window(), window.get());
2369 const gfx::Point begin(20, 20);
2370 const gfx::Point end(150, 150);
2371 const gfx::Vector2d window_offset =
2372 window->bounds().origin().OffsetFromOrigin();
2373 generator.GestureScrollSequence(begin, end,
2374 base::TimeDelta::FromMilliseconds(20),
2375 10);
2376 EXPECT_EQ((begin - window_offset).ToString(),
2377 delegate.scroll_begin_position().ToString());
2378 EXPECT_EQ((end - window_offset).ToString(),
2379 delegate.gesture_end_location().ToString());
2382 TEST_F(GestureRecognizerTest, CaptureSendsGestureEnd) {
2383 scoped_ptr<GestureEventConsumeDelegate> delegate(
2384 new GestureEventConsumeDelegate());
2385 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2386 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2387 ui::test::EventGenerator generator(root_window());
2389 generator.MoveMouseRelativeTo(window.get(), gfx::Point(10, 10));
2390 generator.PressTouch();
2391 RunAllPendingInMessageLoop();
2393 EXPECT_TRUE(delegate->tap_down());
2395 scoped_ptr<aura::Window> capture(CreateTestWindowWithBounds(
2396 gfx::Rect(10, 10, 200, 200), root_window()));
2397 capture->SetCapture();
2398 RunAllPendingInMessageLoop();
2400 EXPECT_TRUE(delegate->end());
2401 EXPECT_TRUE(delegate->tap_cancel());
2404 // Check that previous touch actions that are completely finished (either
2405 // released or cancelled), do not receive extra synthetic cancels upon change of
2406 // capture.
2407 TEST_F(GestureRecognizerTest, CaptureDoesNotCancelFinishedTouches) {
2408 scoped_ptr<GestureEventConsumeDelegate> delegate(
2409 new GestureEventConsumeDelegate());
2410 scoped_ptr<TestEventHandler> handler(new TestEventHandler);
2411 root_window()->AddPreTargetHandler(handler.get());
2413 // Create a window and set it as the capture window.
2414 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(delegate.get(),
2415 -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2416 window1->SetCapture();
2418 ui::test::EventGenerator generator(root_window());
2419 TimedEvents tes;
2421 // Generate two touch-press events on the window.
2422 scoped_ptr<ui::TouchEvent> touch0(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2423 gfx::Point(20, 20), 0,
2424 tes.Now()));
2425 scoped_ptr<ui::TouchEvent> touch1(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2426 gfx::Point(30, 30), 1,
2427 tes.Now()));
2428 generator.Dispatch(touch0.get());
2429 generator.Dispatch(touch1.get());
2430 RunAllPendingInMessageLoop();
2431 EXPECT_EQ(2, handler->touch_pressed_count());
2433 // Advance time.
2434 tes.LeapForward(1000);
2436 // End the two touches, one by a touch-release and one by a touch-cancel; to
2437 // cover both cases.
2438 touch0.reset(new ui::TouchEvent(ui::ET_TOUCH_RELEASED, gfx::Point(20, 20), 0,
2439 tes.Now()));
2440 touch1.reset(new ui::TouchEvent(ui::ET_TOUCH_CANCELLED, gfx::Point(30, 30), 1,
2441 tes.Now()));
2442 generator.Dispatch(touch0.get());
2443 generator.Dispatch(touch1.get());
2444 RunAllPendingInMessageLoop();
2445 EXPECT_EQ(1, handler->touch_released_count());
2446 EXPECT_EQ(1, handler->touch_cancelled_count());
2448 // Create a new window and set it as the new capture window.
2449 scoped_ptr<aura::Window> window2(CreateTestWindowWithBounds(
2450 gfx::Rect(100, 100, 300, 300), root_window()));
2451 window2->SetCapture();
2452 RunAllPendingInMessageLoop();
2453 // Check that setting capture does not generate any synthetic touch-cancels
2454 // for the two previously finished touch actions.
2455 EXPECT_EQ(1, handler->touch_cancelled_count());
2457 root_window()->RemovePreTargetHandler(handler.get());
2460 // Tests that a press with the same touch id as an existing touch is ignored.
2461 TEST_F(GestureRecognizerTest, PressDoesNotCrash) {
2462 scoped_ptr<GestureEventConsumeDelegate> delegate(
2463 new GestureEventConsumeDelegate());
2464 TimedEvents tes;
2466 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2467 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2469 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(45, 45), 7, tes.Now());
2470 press.set_radius_x(40);
2471 DispatchEventUsingWindowDispatcher(&press);
2472 EXPECT_TRUE(delegate->tap_down());
2473 EXPECT_EQ(gfx::Rect(5, 5, 80, 80).ToString(),
2474 delegate->bounding_box().ToString());
2475 delegate->Reset();
2477 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 45), 7, tes.Now());
2478 DispatchEventUsingWindowDispatcher(&press2);
2480 // This new press should not generate a tap-down.
2481 EXPECT_FALSE(delegate->begin());
2482 EXPECT_FALSE(delegate->tap_down());
2483 EXPECT_FALSE(delegate->tap_cancel());
2484 EXPECT_FALSE(delegate->scroll_begin());
2487 TEST_F(GestureRecognizerTest, TwoFingerTap) {
2488 scoped_ptr<GestureEventConsumeDelegate> delegate(
2489 new GestureEventConsumeDelegate());
2490 const int kWindowWidth = 123;
2491 const int kWindowHeight = 45;
2492 const int kTouchId1 = 2;
2493 const int kTouchId2 = 3;
2494 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2495 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2496 delegate.get(), -1234, bounds, root_window()));
2497 TimedEvents tes;
2499 delegate->Reset();
2500 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2501 kTouchId1, tes.Now());
2502 DispatchEventUsingWindowDispatcher(&press1);
2503 EXPECT_2_EVENTS(
2504 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2506 delegate->Reset();
2507 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2508 kTouchId2, tes.Now());
2509 DispatchEventUsingWindowDispatcher(&press2);
2510 EXPECT_2_EVENTS(
2511 delegate->events(), ui::ET_GESTURE_TAP_CANCEL, ui::ET_GESTURE_BEGIN);
2513 // Little bit of touch move should not affect our state.
2514 delegate->Reset();
2515 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(102, 202),
2516 kTouchId1, tes.Now());
2517 DispatchEventUsingWindowDispatcher(&move1);
2518 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(131, 202),
2519 kTouchId2, tes.Now());
2520 DispatchEventUsingWindowDispatcher(&move2);
2521 EXPECT_2_EVENTS(delegate->events(),
2522 ui::ET_GESTURE_SCROLL_BEGIN,
2523 ui::ET_GESTURE_SCROLL_UPDATE);
2525 // Make sure there is enough delay before the touch is released so that it is
2526 // recognized as a tap.
2527 delegate->Reset();
2528 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2529 kTouchId1, tes.LeapForward(50));
2531 DispatchEventUsingWindowDispatcher(&release1);
2532 EXPECT_2_EVENTS(
2533 delegate->events(), ui::ET_GESTURE_TWO_FINGER_TAP, ui::ET_GESTURE_END);
2535 // Lift second finger.
2536 // Make sure there is enough delay before the touch is released so that it is
2537 // recognized as a tap.
2538 delegate->Reset();
2539 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2540 kTouchId2, tes.LeapForward(50));
2542 DispatchEventUsingWindowDispatcher(&release2);
2543 EXPECT_2_EVENTS(
2544 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2547 TEST_F(GestureRecognizerTest, TwoFingerTapExpired) {
2548 scoped_ptr<GestureEventConsumeDelegate> delegate(
2549 new GestureEventConsumeDelegate());
2550 const int kWindowWidth = 123;
2551 const int kWindowHeight = 45;
2552 const int kTouchId1 = 2;
2553 const int kTouchId2 = 3;
2554 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2555 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2556 delegate.get(), -1234, bounds, root_window()));
2557 TimedEvents tes;
2559 delegate->Reset();
2560 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2561 kTouchId1, tes.Now());
2562 DispatchEventUsingWindowDispatcher(&press1);
2564 delegate->Reset();
2565 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2566 kTouchId2, tes.Now());
2567 DispatchEventUsingWindowDispatcher(&press2);
2569 // Send release event after sufficient delay so that two finger time expires.
2570 delegate->Reset();
2571 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2572 kTouchId1, tes.LeapForward(1000));
2574 DispatchEventUsingWindowDispatcher(&release1);
2575 EXPECT_FALSE(delegate->two_finger_tap());
2577 // Lift second finger.
2578 // Make sure there is enough delay before the touch is released so that it is
2579 // recognized as a tap.
2580 delegate->Reset();
2581 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2582 kTouchId2, tes.LeapForward(50));
2584 DispatchEventUsingWindowDispatcher(&release2);
2585 EXPECT_FALSE(delegate->two_finger_tap());
2588 TEST_F(GestureRecognizerTest, TwoFingerTapChangesToPinch) {
2589 scoped_ptr<GestureEventConsumeDelegate> delegate(
2590 new GestureEventConsumeDelegate());
2591 const int kWindowWidth = 123;
2592 const int kWindowHeight = 45;
2593 const int kTouchId1 = 2;
2594 const int kTouchId2 = 3;
2595 TimedEvents tes;
2597 // Test moving first finger
2599 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2600 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2601 delegate.get(), -1234, bounds, root_window()));
2603 delegate->Reset();
2604 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2605 kTouchId1, tes.Now());
2606 DispatchEventUsingWindowDispatcher(&press1);
2608 delegate->Reset();
2609 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2610 kTouchId2, tes.Now());
2611 DispatchEventUsingWindowDispatcher(&press2);
2613 tes.SendScrollEvent(event_processor(), 230, 330, kTouchId1, delegate.get());
2614 EXPECT_FALSE(delegate->two_finger_tap());
2615 EXPECT_TRUE(delegate->pinch_begin());
2617 // Make sure there is enough delay before the touch is released so that it
2618 // is recognized as a tap.
2619 delegate->Reset();
2620 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2621 kTouchId2, tes.LeapForward(50));
2623 DispatchEventUsingWindowDispatcher(&release);
2624 EXPECT_FALSE(delegate->two_finger_tap());
2625 EXPECT_TRUE(delegate->pinch_end());
2628 // Test moving second finger
2630 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2631 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2632 delegate.get(), -1234, bounds, root_window()));
2634 delegate->Reset();
2635 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2636 kTouchId1, tes.Now());
2637 DispatchEventUsingWindowDispatcher(&press1);
2639 delegate->Reset();
2640 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2641 kTouchId2, tes.Now());
2642 DispatchEventUsingWindowDispatcher(&press2);
2644 tes.SendScrollEvent(event_processor(), 301, 230, kTouchId2, delegate.get());
2645 EXPECT_FALSE(delegate->two_finger_tap());
2646 EXPECT_TRUE(delegate->pinch_begin());
2648 // Make sure there is enough delay before the touch is released so that it
2649 // is recognized as a tap.
2650 delegate->Reset();
2651 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2652 kTouchId1, tes.LeapForward(50));
2654 DispatchEventUsingWindowDispatcher(&release);
2655 EXPECT_FALSE(delegate->two_finger_tap());
2656 EXPECT_TRUE(delegate->pinch_end());
2660 TEST_F(GestureRecognizerTest, NoTwoFingerTapWhenFirstFingerHasScrolled) {
2661 scoped_ptr<GestureEventConsumeDelegate> delegate(
2662 new GestureEventConsumeDelegate());
2663 const int kWindowWidth = 123;
2664 const int kWindowHeight = 45;
2665 const int kTouchId1 = 2;
2666 const int kTouchId2 = 3;
2667 TimedEvents tes;
2669 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2670 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2671 delegate.get(), -1234, bounds, root_window()));
2673 delegate->Reset();
2674 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2675 kTouchId1, tes.Now());
2676 DispatchEventUsingWindowDispatcher(&press1);
2677 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2679 delegate->Reset();
2680 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2681 kTouchId2, tes.Now());
2682 DispatchEventUsingWindowDispatcher(&press2);
2684 EXPECT_FALSE(delegate->pinch_begin());
2686 // Make sure there is enough delay before the touch is released so that it
2687 // is recognized as a tap.
2688 delegate->Reset();
2689 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2690 kTouchId2, tes.LeapForward(50));
2692 DispatchEventUsingWindowDispatcher(&release);
2693 EXPECT_FALSE(delegate->two_finger_tap());
2694 EXPECT_FALSE(delegate->pinch_end());
2697 TEST_F(GestureRecognizerTest, MultiFingerSwipe) {
2698 scoped_ptr<GestureEventConsumeDelegate> delegate(
2699 new GestureEventConsumeDelegate());
2700 const int kWindowWidth = 123;
2701 const int kWindowHeight = 45;
2703 gfx::Rect bounds(5, 10, kWindowWidth, kWindowHeight);
2704 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2705 delegate.get(), -1234, bounds, root_window()));
2707 const int kSteps = 15;
2708 const int kTouchPoints = 4;
2709 gfx::Point points[kTouchPoints] = {
2710 gfx::Point(10, 30),
2711 gfx::Point(30, 20),
2712 gfx::Point(50, 30),
2713 gfx::Point(80, 50)
2716 ui::test::EventGenerator generator(root_window(), window.get());
2718 // The unified gesture recognizer assumes a finger has stopped if it hasn't
2719 // moved for too long. See ui/events/gesture_detection/velocity_tracker.cc's
2720 // kAssumePointerStoppedTimeMs.
2721 for (int count = 2; count <= kTouchPoints; ++count) {
2722 generator.GestureMultiFingerScroll(
2723 count, points, 10, kSteps, 0, -11 * kSteps);
2724 EXPECT_TRUE(delegate->swipe_up());
2725 delegate->Reset();
2727 generator.GestureMultiFingerScroll(
2728 count, points, 10, kSteps, 0, 11 * kSteps);
2729 EXPECT_TRUE(delegate->swipe_down());
2730 delegate->Reset();
2732 generator.GestureMultiFingerScroll(
2733 count, points, 10, kSteps, -11 * kSteps, 0);
2734 EXPECT_TRUE(delegate->swipe_left());
2735 delegate->Reset();
2737 generator.GestureMultiFingerScroll(
2738 count, points, 10, kSteps, 11 * kSteps, 0);
2739 EXPECT_TRUE(delegate->swipe_right());
2740 delegate->Reset();
2742 generator.GestureMultiFingerScroll(
2743 count, points, 10, kSteps, 5 * kSteps, 12 * kSteps);
2744 EXPECT_FALSE(delegate->swipe_down());
2745 delegate->Reset();
2747 generator.GestureMultiFingerScroll(
2748 count, points, 10, kSteps, 4 * kSteps, 12 * kSteps);
2749 EXPECT_TRUE(delegate->swipe_down());
2750 delegate->Reset();
2752 generator.GestureMultiFingerScroll(
2753 count, points, 10, kSteps, 3 * kSteps, 12 * kSteps);
2754 EXPECT_TRUE(delegate->swipe_down());
2755 delegate->Reset();
2759 TEST_F(GestureRecognizerTest, TwoFingerTapCancelled) {
2760 scoped_ptr<GestureEventConsumeDelegate> delegate(
2761 new GestureEventConsumeDelegate());
2762 const int kWindowWidth = 123;
2763 const int kWindowHeight = 45;
2764 const int kTouchId1 = 2;
2765 const int kTouchId2 = 3;
2766 TimedEvents tes;
2768 // Test canceling first finger.
2770 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2771 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2772 delegate.get(), -1234, bounds, root_window()));
2774 delegate->Reset();
2775 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2776 kTouchId1, tes.Now());
2777 DispatchEventUsingWindowDispatcher(&press1);
2779 delegate->Reset();
2780 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2781 kTouchId2, tes.Now());
2782 DispatchEventUsingWindowDispatcher(&press2);
2784 delegate->Reset();
2785 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2786 kTouchId1, tes.Now());
2787 DispatchEventUsingWindowDispatcher(&cancel);
2788 EXPECT_FALSE(delegate->two_finger_tap());
2790 // Make sure there is enough delay before the touch is released so that it
2791 // is recognized as a tap.
2792 delegate->Reset();
2793 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2794 kTouchId2, tes.LeapForward(50));
2796 DispatchEventUsingWindowDispatcher(&release);
2797 EXPECT_FALSE(delegate->two_finger_tap());
2800 // Test canceling second finger
2802 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2803 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2804 delegate.get(), -1234, bounds, root_window()));
2806 delegate->Reset();
2807 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2808 kTouchId1, tes.Now());
2809 DispatchEventUsingWindowDispatcher(&press1);
2811 delegate->Reset();
2812 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2813 kTouchId2, tes.Now());
2814 DispatchEventUsingWindowDispatcher(&press2);
2816 delegate->Reset();
2817 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2818 kTouchId2, tes.Now());
2819 DispatchEventUsingWindowDispatcher(&cancel);
2820 EXPECT_FALSE(delegate->two_finger_tap());
2822 // Make sure there is enough delay before the touch is released so that it
2823 // is recognized as a tap.
2824 delegate->Reset();
2825 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2826 kTouchId1, tes.LeapForward(50));
2828 DispatchEventUsingWindowDispatcher(&release);
2829 EXPECT_FALSE(delegate->two_finger_tap());
2833 TEST_F(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) {
2834 scoped_ptr<GestureEventConsumeDelegate> delegate(
2835 new GestureEventConsumeDelegate());
2836 const int kWindowWidth = 523;
2837 const int kWindowHeight = 45;
2838 const int kTouchId1 = 2;
2839 const int kTouchId2 = 3;
2840 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2841 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2842 delegate.get(), -1234, bounds, root_window()));
2843 TimedEvents tes;
2845 delegate->Reset();
2846 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2847 kTouchId1, tes.Now());
2848 DispatchEventUsingWindowDispatcher(&press1);
2849 EXPECT_FALSE(delegate->tap());
2850 EXPECT_TRUE(delegate->tap_down());
2851 EXPECT_FALSE(delegate->tap_cancel());
2852 EXPECT_FALSE(delegate->scroll_begin());
2853 EXPECT_FALSE(delegate->scroll_update());
2854 EXPECT_FALSE(delegate->scroll_end());
2855 EXPECT_FALSE(delegate->long_press());
2856 EXPECT_FALSE(delegate->two_finger_tap());
2858 delegate->Reset();
2859 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201),
2860 kTouchId2, tes.Now());
2861 DispatchEventUsingWindowDispatcher(&press2);
2862 EXPECT_FALSE(delegate->tap());
2863 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
2864 EXPECT_TRUE(delegate->tap_cancel());
2865 EXPECT_FALSE(delegate->scroll_begin());
2866 EXPECT_FALSE(delegate->scroll_update());
2867 EXPECT_FALSE(delegate->scroll_end());
2868 EXPECT_FALSE(delegate->long_press());
2869 EXPECT_FALSE(delegate->two_finger_tap());
2870 EXPECT_FALSE(delegate->pinch_begin());
2872 delegate->Reset();
2873 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(530, 301),
2874 kTouchId2, tes.Now());
2875 DispatchEventUsingWindowDispatcher(&move2);
2876 EXPECT_FALSE(delegate->tap());
2877 EXPECT_FALSE(delegate->tap_down());
2878 EXPECT_FALSE(delegate->tap_cancel());
2879 // Pinch & Scroll only when there is enough movement.
2880 EXPECT_TRUE(delegate->scroll_begin());
2881 EXPECT_TRUE(delegate->scroll_update());
2882 EXPECT_FALSE(delegate->scroll_end());
2883 EXPECT_FALSE(delegate->long_press());
2884 EXPECT_FALSE(delegate->two_finger_tap());
2885 EXPECT_TRUE(delegate->pinch_begin());
2888 // Verifies if a window is the target of multiple touch-ids and we hide the
2889 // window everything is cleaned up correctly.
2890 TEST_F(GestureRecognizerTest, FlushAllOnHide) {
2891 scoped_ptr<GestureEventConsumeDelegate> delegate(
2892 new GestureEventConsumeDelegate());
2893 gfx::Rect bounds(0, 0, 200, 200);
2894 scoped_ptr<aura::Window> window(
2895 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2896 const int kTouchId1 = 8;
2897 const int kTouchId2 = 2;
2898 TimedEvents tes;
2900 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2901 kTouchId1, tes.Now());
2902 DispatchEventUsingWindowDispatcher(&press1);
2903 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2904 kTouchId2, tes.Now());
2905 DispatchEventUsingWindowDispatcher(&press2);
2906 window->Hide();
2907 EXPECT_EQ(NULL,
2908 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1));
2909 EXPECT_EQ(NULL,
2910 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2913 TEST_F(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) {
2914 scoped_ptr<QueueTouchEventDelegate> delegate(
2915 new QueueTouchEventDelegate(host()->dispatcher()));
2916 const int kTouchId = 2;
2917 gfx::Rect bounds(100, 200, 100, 100);
2918 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2919 delegate.get(), -1234, bounds, root_window()));
2920 delegate->set_window(window.get());
2921 TimedEvents tes;
2923 delegate->Reset();
2924 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2925 kTouchId, tes.Now());
2926 DispatchEventUsingWindowDispatcher(&press);
2927 // Scroll around, to cancel the long press
2928 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
2930 delegate->Reset();
2931 delegate->ReceivedAck();
2932 EXPECT_TRUE(delegate->tap_down());
2934 // Wait long enough that long press would have fired if the touchmove hadn't
2935 // prevented it.
2936 DelayByLongPressTimeout();
2938 delegate->Reset();
2939 delegate->ReceivedAckPreventDefaulted();
2940 EXPECT_FALSE(delegate->long_press());
2943 // Same as GestureEventConsumeDelegate, but consumes all the touch-move events.
2944 class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate {
2945 public:
2946 ConsumesTouchMovesDelegate() : consume_touch_move_(true) {}
2947 virtual ~ConsumesTouchMovesDelegate() {}
2949 void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; }
2951 private:
2952 virtual void OnTouchEvent(ui::TouchEvent* touch) OVERRIDE {
2953 if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED)
2954 touch->SetHandled();
2955 else
2956 GestureEventConsumeDelegate::OnTouchEvent(touch);
2959 bool consume_touch_move_;
2961 DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate);
2964 // Same as GestureEventScroll, but tests that the behavior is the same
2965 // even if all the touch-move events are consumed.
2966 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) {
2967 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
2968 new ConsumesTouchMovesDelegate());
2969 const int kWindowWidth = 123;
2970 const int kWindowHeight = 45;
2971 const int kTouchId = 5;
2972 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2973 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2974 delegate.get(), -1234, bounds, root_window()));
2975 TimedEvents tes;
2977 delegate->Reset();
2978 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2979 kTouchId, tes.Now());
2980 DispatchEventUsingWindowDispatcher(&press);
2981 EXPECT_FALSE(delegate->tap());
2982 EXPECT_TRUE(delegate->tap_down());
2983 EXPECT_FALSE(delegate->tap_cancel());
2984 EXPECT_TRUE(delegate->begin());
2985 EXPECT_FALSE(delegate->scroll_begin());
2986 EXPECT_FALSE(delegate->scroll_update());
2987 EXPECT_FALSE(delegate->scroll_end());
2989 // Move the touch-point enough so that it would normally be considered a
2990 // scroll. But since the touch-moves will be consumed, the scroll should not
2991 // start.
2992 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
2993 EXPECT_FALSE(delegate->tap());
2994 EXPECT_FALSE(delegate->tap_down());
2995 EXPECT_TRUE(delegate->tap_cancel());
2996 EXPECT_FALSE(delegate->begin());
2997 EXPECT_FALSE(delegate->scroll_update());
2998 EXPECT_FALSE(delegate->scroll_end());
3000 EXPECT_TRUE(delegate->scroll_begin());
3002 // Release the touch back at the start point. This should end without causing
3003 // a tap.
3004 delegate->Reset();
3005 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230),
3006 kTouchId, tes.LeapForward(50));
3007 DispatchEventUsingWindowDispatcher(&release);
3008 EXPECT_FALSE(delegate->tap());
3009 EXPECT_FALSE(delegate->tap_down());
3010 EXPECT_FALSE(delegate->tap_cancel());
3011 EXPECT_FALSE(delegate->begin());
3012 EXPECT_TRUE(delegate->end());
3013 EXPECT_FALSE(delegate->scroll_begin());
3014 EXPECT_FALSE(delegate->scroll_update());
3016 EXPECT_TRUE(delegate->scroll_end());
3019 // Tests the behavior of 2F scroll when some of the touch-move events are
3020 // consumed.
3021 TEST_F(GestureRecognizerTest, GestureEventScrollTwoFingerTouchMoveConsumed) {
3022 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3023 new ConsumesTouchMovesDelegate());
3024 const int kWindowWidth = 123;
3025 const int kWindowHeight = 100;
3026 const int kTouchId1 = 2;
3027 const int kTouchId2 = 3;
3028 TimedEvents tes;
3030 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3031 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3032 delegate.get(), -1234, bounds, root_window()));
3034 delegate->Reset();
3035 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3036 kTouchId1, tes.Now());
3037 DispatchEventUsingWindowDispatcher(&press1);
3038 tes.SendScrollEvent(event_processor(), 131, 231, kTouchId1, delegate.get());
3040 EXPECT_2_EVENTS(delegate->events(),
3041 ui::ET_GESTURE_TAP_CANCEL,
3042 ui::ET_GESTURE_SCROLL_BEGIN);
3044 delegate->Reset();
3045 // Second finger touches down and moves.
3046 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
3047 kTouchId2, tes.LeapForward(50));
3048 DispatchEventUsingWindowDispatcher(&press2);
3049 tes.SendScrollEvent(event_processor(), 161, 231, kTouchId2, delegate.get());
3050 EXPECT_0_EVENTS(delegate->events());
3052 delegate->Reset();
3053 // Move first finger again, no PinchUpdate & ScrollUpdate.
3054 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId1, delegate.get());
3055 EXPECT_0_EVENTS(delegate->events());
3057 // Stops consuming touch-move.
3058 delegate->set_consume_touch_move(false);
3060 delegate->Reset();
3061 // Making a pinch gesture.
3062 tes.SendScrollEvent(event_processor(), 161, 260, kTouchId1, delegate.get());
3063 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
3065 delegate->Reset();
3066 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId2, delegate.get());
3067 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
3069 delegate->Reset();
3070 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3071 kTouchId1, tes.Now());
3072 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
3073 kTouchId2, tes.Now());
3074 DispatchEventUsingWindowDispatcher(&release1);
3075 DispatchEventUsingWindowDispatcher(&release2);
3077 EXPECT_3_EVENTS(delegate->events(),
3078 ui::ET_GESTURE_END,
3079 ui::ET_GESTURE_SCROLL_END,
3080 ui::ET_GESTURE_END);
3083 // Like as GestureEventTouchMoveConsumed but tests the different behavior
3084 // depending on whether the events were consumed before or after the scroll
3085 // started.
3086 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) {
3087 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3088 new ConsumesTouchMovesDelegate());
3089 const int kWindowWidth = 123;
3090 const int kWindowHeight = 45;
3091 const int kTouchId = 5;
3092 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3093 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3094 delegate.get(), -1234, bounds, root_window()));
3095 TimedEvents tes;
3097 delegate->Reset();
3098 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3099 kTouchId, tes.Now());
3100 DispatchEventUsingWindowDispatcher(&press);
3101 EXPECT_FALSE(delegate->tap());
3102 EXPECT_TRUE(delegate->tap_down());
3103 EXPECT_FALSE(delegate->tap_cancel());
3104 EXPECT_TRUE(delegate->begin());
3105 EXPECT_FALSE(delegate->scroll_begin());
3106 EXPECT_FALSE(delegate->scroll_update());
3107 EXPECT_FALSE(delegate->scroll_end());
3109 // Move the touch-point enough so that it would normally be considered a
3110 // scroll. But since the touch-moves will be consumed, the scroll should not
3111 // start.
3112 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3113 EXPECT_FALSE(delegate->tap());
3114 EXPECT_FALSE(delegate->tap_down());
3115 EXPECT_TRUE(delegate->tap_cancel());
3116 EXPECT_FALSE(delegate->begin());
3117 EXPECT_FALSE(delegate->scroll_update());
3118 EXPECT_FALSE(delegate->scroll_end());
3120 // Consuming the first touch move event won't prevent all future scrolling.
3121 EXPECT_TRUE(delegate->scroll_begin());
3123 // Now, stop consuming touch-move events, and move the touch-point again.
3124 delegate->set_consume_touch_move(false);
3125 tes.SendScrollEvent(event_processor(), 159, 259, kTouchId, delegate.get());
3126 EXPECT_FALSE(delegate->tap());
3127 EXPECT_FALSE(delegate->tap_down());
3128 EXPECT_FALSE(delegate->tap_cancel());
3129 EXPECT_FALSE(delegate->begin());
3130 EXPECT_FALSE(delegate->scroll_begin());
3131 EXPECT_FALSE(delegate->scroll_end());
3133 // Scroll not prevented by consumed first touch move.
3134 EXPECT_TRUE(delegate->scroll_update());
3135 EXPECT_EQ(29, delegate->scroll_x());
3136 EXPECT_EQ(29, delegate->scroll_y());
3137 EXPECT_EQ(gfx::Point(0, 0).ToString(),
3138 delegate->scroll_begin_position().ToString());
3140 // Start consuming touch-move events again.
3141 delegate->set_consume_touch_move(true);
3143 // Move some more to generate a few more scroll updates.
3144 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
3145 EXPECT_FALSE(delegate->tap());
3146 EXPECT_FALSE(delegate->tap_down());
3147 EXPECT_FALSE(delegate->tap_cancel());
3148 EXPECT_FALSE(delegate->begin());
3149 EXPECT_FALSE(delegate->scroll_begin());
3150 EXPECT_FALSE(delegate->scroll_update());
3151 EXPECT_FALSE(delegate->scroll_end());
3152 EXPECT_EQ(0, delegate->scroll_x());
3153 EXPECT_EQ(0, delegate->scroll_y());
3155 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
3156 EXPECT_FALSE(delegate->tap());
3157 EXPECT_FALSE(delegate->tap_down());
3158 EXPECT_FALSE(delegate->tap_cancel());
3159 EXPECT_FALSE(delegate->begin());
3160 EXPECT_FALSE(delegate->scroll_begin());
3161 EXPECT_FALSE(delegate->scroll_update());
3162 EXPECT_FALSE(delegate->scroll_end());
3163 EXPECT_EQ(0, delegate->scroll_x());
3164 EXPECT_EQ(0, delegate->scroll_y());
3166 // Release the touch.
3167 delegate->Reset();
3168 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3169 kTouchId, tes.LeapForward(50));
3170 DispatchEventUsingWindowDispatcher(&release);
3171 EXPECT_FALSE(delegate->tap());
3172 EXPECT_FALSE(delegate->tap_down());
3173 EXPECT_FALSE(delegate->tap_cancel());
3174 EXPECT_FALSE(delegate->begin());
3175 EXPECT_TRUE(delegate->end());
3176 EXPECT_FALSE(delegate->scroll_begin());
3177 EXPECT_FALSE(delegate->scroll_update());
3178 EXPECT_FALSE(delegate->fling());
3180 EXPECT_TRUE(delegate->scroll_end());
3183 // Check that appropriate touch events generate double tap gesture events.
3184 TEST_F(GestureRecognizerTest, GestureEventDoubleTap) {
3185 scoped_ptr<GestureEventConsumeDelegate> delegate(
3186 new GestureEventConsumeDelegate());
3187 const int kWindowWidth = 123;
3188 const int kWindowHeight = 45;
3189 const int kTouchId = 2;
3190 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3191 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3192 delegate.get(), -1234, bounds, root_window()));
3193 TimedEvents tes;
3195 // First tap (tested in GestureEventTap)
3196 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3197 kTouchId, tes.Now());
3198 DispatchEventUsingWindowDispatcher(&press1);
3199 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3200 kTouchId, tes.LeapForward(50));
3201 DispatchEventUsingWindowDispatcher(&release1);
3202 delegate->Reset();
3204 // Second tap
3205 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3206 kTouchId, tes.LeapForward(200));
3207 DispatchEventUsingWindowDispatcher(&press2);
3208 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3209 kTouchId, tes.LeapForward(50));
3210 DispatchEventUsingWindowDispatcher(&release2);
3212 EXPECT_TRUE(delegate->tap());
3213 EXPECT_TRUE(delegate->tap_down());
3214 EXPECT_FALSE(delegate->tap_cancel());
3215 EXPECT_TRUE(delegate->begin());
3216 EXPECT_TRUE(delegate->end());
3217 EXPECT_FALSE(delegate->scroll_begin());
3218 EXPECT_FALSE(delegate->scroll_update());
3219 EXPECT_FALSE(delegate->scroll_end());
3221 EXPECT_EQ(2, delegate->tap_count());
3224 // Check that appropriate touch events generate triple tap gesture events.
3225 TEST_F(GestureRecognizerTest, GestureEventTripleTap) {
3226 scoped_ptr<GestureEventConsumeDelegate> delegate(
3227 new GestureEventConsumeDelegate());
3228 const int kWindowWidth = 123;
3229 const int kWindowHeight = 45;
3230 const int kTouchId = 2;
3231 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3232 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3233 delegate.get(), -1234, bounds, root_window()));
3234 TimedEvents tes;
3236 // First tap (tested in GestureEventTap)
3237 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3238 kTouchId, tes.Now());
3239 DispatchEventUsingWindowDispatcher(&press1);
3240 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3241 kTouchId, tes.LeapForward(50));
3242 DispatchEventUsingWindowDispatcher(&release1);
3244 EXPECT_EQ(1, delegate->tap_count());
3245 delegate->Reset();
3247 // Second tap (tested in GestureEventDoubleTap)
3248 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3249 kTouchId, tes.LeapForward(200));
3250 DispatchEventUsingWindowDispatcher(&press2);
3251 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3252 kTouchId, tes.LeapForward(50));
3253 DispatchEventUsingWindowDispatcher(&release2);
3255 EXPECT_EQ(2, delegate->tap_count());
3256 delegate->Reset();
3258 // Third tap
3259 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(102, 206),
3260 kTouchId, tes.LeapForward(200));
3261 DispatchEventUsingWindowDispatcher(&press3);
3262 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3263 kTouchId, tes.LeapForward(50));
3264 DispatchEventUsingWindowDispatcher(&release3);
3266 // Third, Fourth and Fifth Taps. Taps after the third should have their
3267 // |tap_count| wrap around back to 1.
3268 for (int i = 3; i < 5; ++i) {
3269 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED,
3270 gfx::Point(102, 206),
3271 kTouchId,
3272 tes.LeapForward(200));
3273 DispatchEventUsingWindowDispatcher(&press3);
3274 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED,
3275 gfx::Point(102, 206),
3276 kTouchId,
3277 tes.LeapForward(50));
3278 DispatchEventUsingWindowDispatcher(&release3);
3280 EXPECT_TRUE(delegate->tap());
3281 EXPECT_TRUE(delegate->tap_down());
3282 EXPECT_FALSE(delegate->tap_cancel());
3283 EXPECT_TRUE(delegate->begin());
3284 EXPECT_TRUE(delegate->end());
3285 EXPECT_FALSE(delegate->scroll_begin());
3286 EXPECT_FALSE(delegate->scroll_update());
3287 EXPECT_FALSE(delegate->scroll_end());
3288 EXPECT_EQ(1 + (i % 3), delegate->tap_count());
3292 // Check that we don't get a double tap when the two taps are far apart.
3293 TEST_F(GestureRecognizerTest, TwoTapsFarApart) {
3294 scoped_ptr<GestureEventConsumeDelegate> delegate(
3295 new GestureEventConsumeDelegate());
3296 const int kWindowWidth = 123;
3297 const int kWindowHeight = 45;
3298 const int kTouchId = 2;
3299 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3300 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3301 delegate.get(), -1234, bounds, root_window()));
3302 TimedEvents tes;
3304 // First tap (tested in GestureEventTap)
3305 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3306 kTouchId, tes.Now());
3307 DispatchEventUsingWindowDispatcher(&press1);
3308 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3309 kTouchId, tes.LeapForward(50));
3310 DispatchEventUsingWindowDispatcher(&release1);
3311 delegate->Reset();
3313 // Second tap, close in time but far in distance
3314 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201),
3315 kTouchId, tes.LeapForward(200));
3316 DispatchEventUsingWindowDispatcher(&press2);
3317 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201),
3318 kTouchId, tes.LeapForward(50));
3319 DispatchEventUsingWindowDispatcher(&release2);
3321 EXPECT_TRUE(delegate->tap());
3322 EXPECT_TRUE(delegate->tap_down());
3323 EXPECT_FALSE(delegate->tap_cancel());
3324 EXPECT_TRUE(delegate->begin());
3325 EXPECT_TRUE(delegate->end());
3326 EXPECT_FALSE(delegate->scroll_begin());
3327 EXPECT_FALSE(delegate->scroll_update());
3328 EXPECT_FALSE(delegate->scroll_end());
3330 EXPECT_EQ(1, delegate->tap_count());
3333 // Check that we don't get a double tap when the two taps have a long enough
3334 // delay in between.
3335 TEST_F(GestureRecognizerTest, TwoTapsWithDelayBetween) {
3336 scoped_ptr<GestureEventConsumeDelegate> delegate(
3337 new GestureEventConsumeDelegate());
3338 const int kWindowWidth = 123;
3339 const int kWindowHeight = 45;
3340 const int kTouchId = 2;
3341 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3342 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3343 delegate.get(), -1234, bounds, root_window()));
3344 TimedEvents tes;
3346 // First tap (tested in GestureEventTap)
3347 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3348 kTouchId, tes.Now());
3349 DispatchEventUsingWindowDispatcher(&press1);
3350 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3351 kTouchId, tes.LeapForward(50));
3352 DispatchEventUsingWindowDispatcher(&release1);
3353 delegate->Reset();
3355 // Second tap, close in distance but after some delay
3356 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3357 kTouchId, tes.LeapForward(2000));
3358 DispatchEventUsingWindowDispatcher(&press2);
3359 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3360 kTouchId, tes.LeapForward(50));
3361 DispatchEventUsingWindowDispatcher(&release2);
3363 EXPECT_TRUE(delegate->tap());
3364 EXPECT_TRUE(delegate->tap_down());
3365 EXPECT_FALSE(delegate->tap_cancel());
3366 EXPECT_TRUE(delegate->begin());
3367 EXPECT_TRUE(delegate->end());
3368 EXPECT_FALSE(delegate->scroll_begin());
3369 EXPECT_FALSE(delegate->scroll_update());
3370 EXPECT_FALSE(delegate->scroll_end());
3372 EXPECT_EQ(1, delegate->tap_count());
3375 // Checks that if the bounding-box of a gesture changes because of change in
3376 // radius of a touch-point, and not because of change in position, then there
3377 // are not gesture events from that.
3378 TEST_F(GestureRecognizerTest, BoundingBoxRadiusChange) {
3379 scoped_ptr<GestureEventConsumeDelegate> delegate(
3380 new GestureEventConsumeDelegate());
3381 const int kWindowWidth = 234;
3382 const int kWindowHeight = 345;
3383 const int kTouchId = 5, kTouchId2 = 7;
3384 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3385 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3386 delegate.get(), -1234, bounds, root_window()));
3387 TimedEvents tes;
3389 ui::TouchEvent press1(
3390 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, tes.Now());
3391 DispatchEventUsingWindowDispatcher(&press1);
3392 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
3394 delegate->Reset();
3396 ui::TouchEvent press2(
3397 ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2,
3398 tes.LeapForward(400));
3399 press2.set_radius_x(5);
3400 DispatchEventUsingWindowDispatcher(&press2);
3401 EXPECT_FALSE(delegate->pinch_begin());
3402 EXPECT_EQ(gfx::Rect(101, 196, 105, 10).ToString(),
3403 delegate->bounding_box().ToString());
3405 delegate->Reset();
3407 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(50, 50), kTouchId,
3408 tes.LeapForward(40));
3409 DispatchEventUsingWindowDispatcher(&move1);
3410 EXPECT_TRUE(delegate->pinch_begin());
3411 EXPECT_EQ(gfx::Rect(50, 50, 156, 156).ToString(),
3412 delegate->bounding_box().ToString());
3414 delegate->Reset();
3416 // The position doesn't move, but the radius changes.
3417 ui::TouchEvent move2(
3418 ui::ET_TOUCH_MOVED, gfx::Point(50, 50), kTouchId, tes.LeapForward(40));
3419 move2.set_radius_x(50);
3420 move2.set_radius_y(60);
3421 DispatchEventUsingWindowDispatcher(&move2);
3422 EXPECT_FALSE(delegate->tap());
3423 EXPECT_FALSE(delegate->tap_cancel());
3424 EXPECT_FALSE(delegate->scroll_update());
3425 EXPECT_FALSE(delegate->pinch_update());
3427 delegate->Reset();
3430 // Checks that slow scrolls deliver the correct deltas.
3431 // In particular, fix for http;//crbug.com/150573.
3432 TEST_F(GestureRecognizerTest, NoDriftInScroll) {
3433 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
3434 ui::GestureConfiguration::set_min_scroll_delta_squared(9);
3435 scoped_ptr<GestureEventConsumeDelegate> delegate(
3436 new GestureEventConsumeDelegate());
3437 const int kWindowWidth = 234;
3438 const int kWindowHeight = 345;
3439 const int kTouchId = 5;
3440 TimedEvents tes;
3441 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3442 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3443 delegate.get(), -1234, bounds, root_window()));
3445 ui::TouchEvent press1(
3446 ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, tes.Now());
3447 DispatchEventUsingWindowDispatcher(&press1);
3448 EXPECT_TRUE(delegate->begin());
3450 delegate->Reset();
3452 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId,
3453 tes.LeapForward(40));
3454 DispatchEventUsingWindowDispatcher(&move1);
3455 EXPECT_FALSE(delegate->scroll_begin());
3457 delegate->Reset();
3459 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3460 tes.LeapForward(40));
3461 DispatchEventUsingWindowDispatcher(&move2);
3462 EXPECT_TRUE(delegate->tap_cancel());
3463 EXPECT_TRUE(delegate->scroll_begin());
3464 EXPECT_TRUE(delegate->scroll_update());
3465 // 3 px consumed by touch slop region.
3466 EXPECT_EQ(-1, delegate->scroll_y());
3467 EXPECT_EQ(-4, delegate->scroll_y_hint());
3469 delegate->Reset();
3471 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3472 tes.LeapForward(40));
3473 DispatchEventUsingWindowDispatcher(&move3);
3474 EXPECT_FALSE(delegate->scroll_update());
3476 delegate->Reset();
3478 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId,
3479 tes.LeapForward(40));
3480 DispatchEventUsingWindowDispatcher(&move4);
3481 EXPECT_TRUE(delegate->scroll_update());
3482 EXPECT_EQ(-1, delegate->scroll_y());
3484 delegate->Reset();
3487 // Ensure that move events which are preventDefaulted will cause a tap
3488 // cancel gesture event to be fired if the move would normally cause a
3489 // scroll. See bug http://crbug.com/146397.
3490 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveCanFireTapCancel) {
3491 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3492 new ConsumesTouchMovesDelegate());
3493 const int kTouchId = 5;
3494 gfx::Rect bounds(100, 200, 123, 45);
3495 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3496 delegate.get(), -1234, bounds, root_window()));
3497 TimedEvents tes;
3499 delegate->Reset();
3500 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3501 kTouchId, tes.Now());
3503 delegate->set_consume_touch_move(false);
3504 DispatchEventUsingWindowDispatcher(&press);
3505 delegate->set_consume_touch_move(true);
3506 delegate->Reset();
3507 // Move the touch-point enough so that it would normally be considered a
3508 // scroll. But since the touch-moves will be consumed, no scrolling should
3509 // occur.
3510 // With the unified gesture detector, we will receive a scroll begin gesture,
3511 // whereas with the aura gesture recognizer we won't.
3512 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3513 EXPECT_FALSE(delegate->tap());
3514 EXPECT_FALSE(delegate->tap_down());
3515 EXPECT_TRUE(delegate->tap_cancel());
3516 EXPECT_FALSE(delegate->begin());
3517 EXPECT_FALSE(delegate->scroll_update());
3518 EXPECT_FALSE(delegate->scroll_end());
3521 TEST_F(GestureRecognizerTest,
3522 TransferEventDispatchesTouchCancel) {
3523 scoped_ptr<GestureEventConsumeDelegate> delegate(
3524 new GestureEventConsumeDelegate());
3525 TimedEvents tes;
3526 const int kWindowWidth = 800;
3527 const int kWindowHeight = 600;
3528 const int kTouchId = 2;
3529 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3530 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3531 delegate.get(), -1234, bounds, root_window()));
3532 scoped_ptr<RemoveOnTouchCancelHandler>
3533 handler(new RemoveOnTouchCancelHandler());
3534 window->AddPreTargetHandler(handler.get());
3536 // Start a gesture sequence on |window|. Then transfer the events to NULL.
3537 // Make sure |window| receives a touch-cancel event.
3538 delegate->Reset();
3539 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3540 kTouchId, tes.Now());
3541 ui::TouchEvent p2(ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), 1, tes.Now());
3542 DispatchEventUsingWindowDispatcher(&press);
3543 DispatchEventUsingWindowDispatcher(&p2);
3544 EXPECT_FALSE(delegate->tap());
3545 EXPECT_TRUE(delegate->tap_down());
3546 EXPECT_TRUE(delegate->tap_cancel());
3547 EXPECT_TRUE(delegate->begin());
3548 EXPECT_EQ(2, handler->touch_pressed_count());
3549 delegate->Reset();
3550 handler->Reset();
3552 ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get();
3553 EXPECT_EQ(window.get(),
3554 gesture_recognizer->GetTouchLockedTarget(press));
3555 gesture_recognizer->TransferEventsTo(window.get(), NULL);
3556 EXPECT_EQ(NULL,
3557 gesture_recognizer->GetTouchLockedTarget(press));
3558 // The event-handler removes |window| from its parent on the first
3559 // touch-cancel event, so it won't receive the second touch-cancel event.
3560 EXPECT_EQ(1, handler->touch_cancelled_count());
3563 // Check that appropriate touch events generate show press events
3564 TEST_F(GestureRecognizerTest, GestureEventShowPress) {
3565 scoped_ptr<GestureEventConsumeDelegate> delegate(
3566 new GestureEventConsumeDelegate());
3567 TimedEvents tes;
3568 const int kWindowWidth = 123;
3569 const int kWindowHeight = 45;
3570 const int kTouchId = 2;
3571 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3572 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3573 delegate.get(), -1234, bounds, root_window()));
3575 delegate->Reset();
3577 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3578 kTouchId, tes.Now());
3579 DispatchEventUsingWindowDispatcher(&press1);
3580 EXPECT_TRUE(delegate->tap_down());
3581 EXPECT_TRUE(delegate->begin());
3582 EXPECT_FALSE(delegate->tap_cancel());
3584 // We haven't pressed long enough for a show press to occur
3585 EXPECT_FALSE(delegate->show_press());
3587 // Wait until the timer runs out
3588 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
3589 EXPECT_TRUE(delegate->show_press());
3590 EXPECT_FALSE(delegate->tap_cancel());
3592 delegate->Reset();
3593 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3594 kTouchId, tes.Now());
3595 DispatchEventUsingWindowDispatcher(&release1);
3596 EXPECT_FALSE(delegate->long_press());
3598 // Note the tap isn't dispatched until the release
3599 EXPECT_FALSE(delegate->tap_cancel());
3600 EXPECT_TRUE(delegate->tap());
3603 // Check that scrolling cancels a show press
3604 TEST_F(GestureRecognizerTest, GestureEventShowPressCancelledByScroll) {
3605 scoped_ptr<GestureEventConsumeDelegate> delegate(
3606 new GestureEventConsumeDelegate());
3607 TimedEvents tes;
3608 const int kWindowWidth = 123;
3609 const int kWindowHeight = 45;
3610 const int kTouchId = 6;
3611 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3612 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3613 delegate.get(), -1234, bounds, root_window()));
3615 delegate->Reset();
3617 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3618 kTouchId, tes.Now());
3619 DispatchEventUsingWindowDispatcher(&press1);
3620 EXPECT_TRUE(delegate->tap_down());
3622 // We haven't pressed long enough for a show press to occur
3623 EXPECT_FALSE(delegate->show_press());
3624 EXPECT_FALSE(delegate->tap_cancel());
3626 // Scroll around, to cancel the show press
3627 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3628 // Wait until the timer runs out
3629 DelayByShowPressTimeout();
3630 EXPECT_FALSE(delegate->show_press());
3631 EXPECT_TRUE(delegate->tap_cancel());
3633 delegate->Reset();
3634 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3635 kTouchId, tes.LeapForward(10));
3636 DispatchEventUsingWindowDispatcher(&release1);
3637 EXPECT_FALSE(delegate->show_press());
3638 EXPECT_FALSE(delegate->tap_cancel());
3641 // Test that show press events are sent immediately on tap
3642 TEST_F(GestureRecognizerTest, GestureEventShowPressSentOnTap) {
3643 scoped_ptr<GestureEventConsumeDelegate> delegate(
3644 new GestureEventConsumeDelegate());
3645 TimedEvents tes;
3646 const int kWindowWidth = 123;
3647 const int kWindowHeight = 45;
3648 const int kTouchId = 6;
3649 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3650 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3651 delegate.get(), -1234, bounds, root_window()));
3653 delegate->Reset();
3655 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3656 kTouchId, tes.Now());
3657 DispatchEventUsingWindowDispatcher(&press1);
3658 EXPECT_TRUE(delegate->tap_down());
3660 // We haven't pressed long enough for a show press to occur
3661 EXPECT_FALSE(delegate->show_press());
3662 EXPECT_FALSE(delegate->tap_cancel());
3664 delegate->Reset();
3665 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3666 kTouchId, tes.LeapForward(50));
3667 DispatchEventUsingWindowDispatcher(&release1);
3668 EXPECT_TRUE(delegate->show_press());
3669 EXPECT_FALSE(delegate->tap_cancel());
3670 EXPECT_TRUE(delegate->tap());
3673 // Test that consuming the first move touch event prevents a scroll.
3674 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveScrollTest) {
3675 scoped_ptr<QueueTouchEventDelegate> delegate(
3676 new QueueTouchEventDelegate(host()->dispatcher()));
3677 TimedEvents tes;
3678 const int kTouchId = 7;
3679 gfx::Rect bounds(0, 0, 1000, 1000);
3680 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3681 delegate.get(), -1234, bounds, root_window()));
3682 delegate->set_window(window.get());
3684 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3685 kTouchId, tes.Now());
3686 DispatchEventUsingWindowDispatcher(&press);
3687 delegate->ReceivedAck();
3689 // A touch move within the slop region is never consumed in web contents. The
3690 // unified GR won't prevent scroll if a touch move within the slop region is
3691 // consumed, so make sure this touch move exceeds the slop region.
3692 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(10, 10),
3693 kTouchId, tes.Now());
3694 DispatchEventUsingWindowDispatcher(&move1);
3695 delegate->ReceivedAckPreventDefaulted();
3697 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3698 kTouchId, tes.Now());
3699 DispatchEventUsingWindowDispatcher(&move2);
3700 delegate->ReceivedAck();
3702 // With the unified gesture detector, consuming the first touch move event
3703 // won't prevent all future scrolling.
3704 EXPECT_TRUE(delegate->scroll_begin());
3705 EXPECT_TRUE(delegate->scroll_update());
3708 // Test that consuming the first move touch doesn't prevent a tap.
3709 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveTapTest) {
3710 scoped_ptr<QueueTouchEventDelegate> delegate(
3711 new QueueTouchEventDelegate(host()->dispatcher()));
3712 TimedEvents tes;
3713 const int kTouchId = 7;
3714 gfx::Rect bounds(0, 0, 1000, 1000);
3715 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3716 delegate.get(), -1234, bounds, root_window()));
3717 delegate->set_window(window.get());
3719 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3720 kTouchId, tes.Now());
3721 DispatchEventUsingWindowDispatcher(&press);
3722 delegate->ReceivedAck();
3724 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
3725 kTouchId, tes.Now());
3726 DispatchEventUsingWindowDispatcher(&move);
3727 delegate->ReceivedAckPreventDefaulted();
3729 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(2, 2),
3730 kTouchId, tes.LeapForward(50));
3731 DispatchEventUsingWindowDispatcher(&release);
3732 delegate->ReceivedAck();
3734 EXPECT_TRUE(delegate->tap());
3737 // Test that consuming the first move touch doesn't prevent a long press.
3738 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveLongPressTest) {
3739 scoped_ptr<QueueTouchEventDelegate> delegate(
3740 new QueueTouchEventDelegate(host()->dispatcher()));
3741 TimedEvents tes;
3742 const int kWindowWidth = 123;
3743 const int kWindowHeight = 45;
3744 const int kTouchId = 2;
3745 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3746 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3747 delegate.get(), -1234, bounds, root_window()));
3748 delegate->set_window(window.get());
3750 delegate->Reset();
3752 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3753 kTouchId, tes.Now());
3754 DispatchEventUsingWindowDispatcher(&press1);
3755 delegate->ReceivedAck();
3757 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103, 203),
3758 kTouchId, tes.Now());
3759 DispatchEventUsingWindowDispatcher(&move);
3760 delegate->ReceivedAckPreventDefaulted();
3762 // Wait until the timer runs out
3763 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
3764 EXPECT_TRUE(delegate->long_press());
3767 // Tests that the deltas are correct when leaving the slop region very slowly.
3768 TEST_F(GestureRecognizerTest, TestExceedingSlopSlowly) {
3769 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
3770 scoped_ptr<GestureEventConsumeDelegate> delegate(
3771 new GestureEventConsumeDelegate());
3772 const int kWindowWidth = 234;
3773 const int kWindowHeight = 345;
3774 const int kTouchId = 5;
3775 TimedEvents tes;
3776 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3777 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3778 delegate.get(), -1234, bounds, root_window()));
3780 ui::TouchEvent press(
3781 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), kTouchId, tes.Now());
3782 DispatchEventUsingWindowDispatcher(&press);
3783 EXPECT_FALSE(delegate->scroll_begin());
3784 EXPECT_FALSE(delegate->scroll_update());
3785 delegate->Reset();
3787 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(11, 10), kTouchId,
3788 tes.LeapForward(40));
3789 DispatchEventUsingWindowDispatcher(&move1);
3790 EXPECT_FALSE(delegate->scroll_begin());
3791 EXPECT_FALSE(delegate->scroll_update());
3792 EXPECT_EQ(0, delegate->scroll_x());
3793 EXPECT_EQ(0, delegate->scroll_x_hint());
3794 delegate->Reset();
3796 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(12, 10), kTouchId,
3797 tes.LeapForward(40));
3798 DispatchEventUsingWindowDispatcher(&move2);
3799 EXPECT_FALSE(delegate->scroll_begin());
3800 EXPECT_FALSE(delegate->scroll_update());
3801 EXPECT_EQ(0, delegate->scroll_x());
3802 EXPECT_EQ(0, delegate->scroll_x_hint());
3803 delegate->Reset();
3806 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::PointF(13.1f, 10.f), kTouchId,
3807 tes.LeapForward(40));
3808 DispatchEventUsingWindowDispatcher(&move3);
3809 EXPECT_TRUE(delegate->scroll_begin());
3810 EXPECT_TRUE(delegate->scroll_update());
3811 EXPECT_NEAR(0.1, delegate->scroll_x(), 0.0001);
3812 EXPECT_FLOAT_EQ(3.1f, delegate->scroll_x_hint());
3813 delegate->Reset();
3815 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(14, 10), kTouchId,
3816 tes.LeapForward(40));
3817 DispatchEventUsingWindowDispatcher(&move4);
3818 EXPECT_FALSE(delegate->scroll_begin());
3819 EXPECT_TRUE(delegate->scroll_update());
3820 EXPECT_NEAR(0.9, delegate->scroll_x(), 0.0001);
3821 EXPECT_EQ(0.f, delegate->scroll_x_hint());
3822 delegate->Reset();
3825 TEST_F(GestureRecognizerTest, ScrollAlternatelyConsumedTest) {
3826 scoped_ptr<QueueTouchEventDelegate> delegate(
3827 new QueueTouchEventDelegate(host()->dispatcher()));
3828 TimedEvents tes;
3829 const int kWindowWidth = 3000;
3830 const int kWindowHeight = 3000;
3831 const int kTouchId = 2;
3832 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3833 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3834 delegate.get(), -1234, bounds, root_window()));
3835 delegate->set_window(window.get());
3837 delegate->Reset();
3839 int x = 0;
3840 int y = 0;
3842 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
3843 kTouchId, tes.Now());
3844 DispatchEventUsingWindowDispatcher(&press1);
3845 delegate->ReceivedAck();
3846 EXPECT_FALSE(delegate->scroll_begin());
3847 EXPECT_FALSE(delegate->scroll_update());
3848 delegate->Reset();
3850 x += 100;
3851 y += 100;
3852 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
3853 kTouchId, tes.Now());
3854 DispatchEventUsingWindowDispatcher(&move1);
3855 delegate->ReceivedAck();
3856 EXPECT_TRUE(delegate->scroll_begin());
3857 EXPECT_TRUE(delegate->scroll_update());
3858 delegate->Reset();
3860 for (int i = 0; i < 3; ++i) {
3861 x += 10;
3862 y += 10;
3863 ui::TouchEvent move2(
3864 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
3865 DispatchEventUsingWindowDispatcher(&move2);
3866 delegate->ReceivedAck();
3867 EXPECT_FALSE(delegate->scroll_begin());
3868 EXPECT_TRUE(delegate->scroll_update());
3869 EXPECT_EQ(10, delegate->scroll_x());
3870 EXPECT_EQ(10, delegate->scroll_y());
3871 delegate->Reset();
3873 x += 20;
3874 y += 20;
3875 ui::TouchEvent move3(
3876 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
3877 DispatchEventUsingWindowDispatcher(&move3);
3878 delegate->ReceivedAckPreventDefaulted();
3879 EXPECT_FALSE(delegate->scroll_begin());
3880 EXPECT_FALSE(delegate->scroll_update());
3881 delegate->Reset();
3885 TEST_F(GestureRecognizerTest, PinchAlternatelyConsumedTest) {
3886 scoped_ptr<QueueTouchEventDelegate> delegate(
3887 new QueueTouchEventDelegate(host()->dispatcher()));
3888 TimedEvents tes;
3889 const int kWindowWidth = 3000;
3890 const int kWindowHeight = 3000;
3891 const int kTouchId1 = 5;
3892 const int kTouchId2 = 7;
3893 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3894 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3895 delegate.get(), -1234, bounds, root_window()));
3896 delegate->set_window(window.get());
3897 delegate->Reset();
3899 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3900 kTouchId1, tes.Now());
3901 DispatchEventUsingWindowDispatcher(&press1);
3902 delegate->ReceivedAck();
3903 EXPECT_FALSE(delegate->scroll_begin());
3904 EXPECT_FALSE(delegate->scroll_update());
3905 delegate->Reset();
3907 int x = 0;
3908 int y = 0;
3910 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
3911 kTouchId2, tes.Now());
3912 DispatchEventUsingWindowDispatcher(&press2);
3913 delegate->ReceivedAck();
3914 EXPECT_FALSE(delegate->scroll_begin());
3915 EXPECT_FALSE(delegate->scroll_update());
3916 EXPECT_FALSE(delegate->pinch_begin());
3917 EXPECT_FALSE(delegate->pinch_update());
3919 delegate->Reset();
3921 x += 100;
3922 y += 100;
3923 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
3924 kTouchId2, tes.Now());
3925 DispatchEventUsingWindowDispatcher(&move1);
3926 delegate->ReceivedAck();
3927 EXPECT_TRUE(delegate->scroll_begin());
3928 EXPECT_TRUE(delegate->scroll_update());
3929 EXPECT_TRUE(delegate->pinch_begin());
3930 EXPECT_FALSE(delegate->pinch_update());
3931 delegate->Reset();
3933 const float expected_scales[] = {1.5f, 1.2f, 1.125f};
3935 for (int i = 0; i < 3; ++i) {
3936 x += 50;
3937 y += 50;
3938 ui::TouchEvent move2(
3939 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
3940 DispatchEventUsingWindowDispatcher(&move2);
3941 delegate->ReceivedAck();
3942 EXPECT_FALSE(delegate->scroll_begin());
3943 EXPECT_TRUE(delegate->scroll_update());
3944 EXPECT_FALSE(delegate->scroll_end());
3945 EXPECT_FALSE(delegate->pinch_begin());
3946 EXPECT_TRUE(delegate->pinch_update());
3947 EXPECT_FALSE(delegate->pinch_end());
3948 EXPECT_EQ(25, delegate->scroll_x());
3949 EXPECT_EQ(25, delegate->scroll_y());
3950 EXPECT_FLOAT_EQ(expected_scales[i], delegate->scale());
3951 delegate->Reset();
3953 x += 100;
3954 y += 100;
3955 ui::TouchEvent move3(
3956 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
3957 DispatchEventUsingWindowDispatcher(&move3);
3958 delegate->ReceivedAckPreventDefaulted();
3959 EXPECT_FALSE(delegate->scroll_begin());
3960 EXPECT_FALSE(delegate->scroll_update());
3961 EXPECT_FALSE(delegate->scroll_end());
3962 EXPECT_FALSE(delegate->pinch_begin());
3963 EXPECT_FALSE(delegate->pinch_update());
3964 EXPECT_FALSE(delegate->pinch_end());
3965 delegate->Reset();
3969 // Test that touch event flags are passed through to the gesture event.
3970 TEST_F(GestureRecognizerTest, GestureEventFlagsPassedFromTouchEvent) {
3971 scoped_ptr<GestureEventConsumeDelegate> delegate(
3972 new GestureEventConsumeDelegate());
3973 TimedEvents tes;
3974 const int kWindowWidth = 123;
3975 const int kWindowHeight = 45;
3976 const int kTouchId = 6;
3977 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3978 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3979 delegate.get(), -1234, bounds, root_window()));
3981 delegate->Reset();
3983 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3984 kTouchId, tes.Now());
3985 DispatchEventUsingWindowDispatcher(&press1);
3986 EXPECT_TRUE(delegate->tap_down());
3988 int default_flags = delegate->flags();
3990 ui::TouchEvent move1(
3991 ui::ET_TOUCH_MOVED, gfx::Point(397, 149), kTouchId, tes.LeapForward(50));
3992 move1.set_flags(992);
3994 DispatchEventUsingWindowDispatcher(&move1);
3995 EXPECT_NE(default_flags, delegate->flags());
3998 // Test that latency info is passed through to the gesture event.
3999 TEST_F(GestureRecognizerTest, LatencyPassedFromTouchEvent) {
4000 scoped_ptr<GestureEventConsumeDelegate> delegate(
4001 new GestureEventConsumeDelegate());
4002 TimedEvents tes;
4003 const int kWindowWidth = 123;
4004 const int kWindowHeight = 45;
4005 const int kTouchId = 6;
4007 const base::TimeTicks time_original = base::TimeTicks::FromInternalValue(100);
4008 const base::TimeTicks time_ui = base::TimeTicks::FromInternalValue(200);
4009 const base::TimeTicks time_acked = base::TimeTicks::FromInternalValue(300);
4011 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4012 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4013 delegate.get(), -1234, bounds, root_window()));
4015 delegate->Reset();
4017 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4018 kTouchId, tes.Now());
4020 // Ensure the only components around are the ones we add.
4021 press1.latency()->Clear();
4023 press1.latency()->AddLatencyNumberWithTimestamp(
4024 ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, 0, time_original, 1);
4026 press1.latency()->AddLatencyNumberWithTimestamp(
4027 ui::INPUT_EVENT_LATENCY_UI_COMPONENT, 0, 0, time_ui, 1);
4029 press1.latency()->AddLatencyNumberWithTimestamp(
4030 ui::INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT, 0, 0, time_acked, 1);
4032 DispatchEventUsingWindowDispatcher(&press1);
4033 EXPECT_TRUE(delegate->tap_down());
4035 ui::LatencyInfo::LatencyComponent component;
4037 EXPECT_EQ(3U, delegate->latency_info().latency_components.size());
4038 ASSERT_TRUE(delegate->latency_info().FindLatency(
4039 ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, &component));
4040 EXPECT_EQ(time_original, component.event_time);
4042 ASSERT_TRUE(delegate->latency_info().FindLatency(
4043 ui::INPUT_EVENT_LATENCY_UI_COMPONENT, 0, &component));
4044 EXPECT_EQ(time_ui, component.event_time);
4046 ASSERT_TRUE(delegate->latency_info().FindLatency(
4047 ui::INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT, 0, &component));
4048 EXPECT_EQ(time_acked, component.event_time);
4050 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
4051 EXPECT_TRUE(delegate->show_press());
4052 EXPECT_EQ(0U, delegate->latency_info().latency_components.size());
4055 // A delegate that deletes a window on long press.
4056 class GestureEventDeleteWindowOnLongPress : public GestureEventConsumeDelegate {
4057 public:
4058 GestureEventDeleteWindowOnLongPress()
4059 : window_(NULL) {}
4061 void set_window(aura::Window** window) { window_ = window; }
4063 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
4064 GestureEventConsumeDelegate::OnGestureEvent(gesture);
4065 if (gesture->type() != ui::ET_GESTURE_LONG_PRESS)
4066 return;
4067 ui::GestureRecognizer::Get()->CleanupStateForConsumer(*window_);
4068 delete *window_;
4069 *window_ = NULL;
4072 private:
4073 aura::Window** window_;
4074 DISALLOW_COPY_AND_ASSIGN(GestureEventDeleteWindowOnLongPress);
4077 // Check that deleting the window in response to a long press gesture doesn't
4078 // crash.
4079 TEST_F(GestureRecognizerTest, GestureEventLongPressDeletingWindow) {
4080 GestureEventDeleteWindowOnLongPress delegate;
4081 const int kWindowWidth = 123;
4082 const int kWindowHeight = 45;
4083 const int kTouchId = 2;
4084 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4085 aura::Window* window(CreateTestWindowWithDelegate(
4086 &delegate, -1234, bounds, root_window()));
4087 delegate.set_window(&window);
4089 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
4090 gfx::Point(101, 201),
4091 kTouchId,
4092 ui::EventTimeForNow());
4093 DispatchEventUsingWindowDispatcher(&press1);
4094 EXPECT_TRUE(window != NULL);
4096 // Wait until the timer runs out.
4097 delegate.WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4098 EXPECT_EQ(NULL, window);
4101 TEST_F(GestureRecognizerTest, GestureEventSmallPinchDisabled) {
4102 CommandLine::ForCurrentProcess()->AppendSwitch(
4103 switches::kCompensateForUnstablePinchZoom);
4105 scoped_ptr<GestureEventConsumeDelegate> delegate(
4106 new GestureEventConsumeDelegate());
4107 TimedEvents tes;
4108 const int kWindowWidth = 300;
4109 const int kWindowHeight = 400;
4110 const int kTouchId1 = 3;
4111 const int kTouchId2 = 5;
4112 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4113 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4114 delegate.get(), -1234, bounds, root_window()));
4116 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4117 kTouchId1, tes.Now());
4118 DispatchEventUsingWindowDispatcher(&press1);
4119 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4120 kTouchId2, tes.Now());
4121 DispatchEventUsingWindowDispatcher(&press2);
4123 // Move the first finger.
4124 delegate->Reset();
4125 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4126 kTouchId1, tes.Now());
4127 DispatchEventUsingWindowDispatcher(&move1);
4129 EXPECT_3_EVENTS(delegate->events(),
4130 ui::ET_GESTURE_SCROLL_BEGIN,
4131 ui::ET_GESTURE_SCROLL_UPDATE,
4132 ui::ET_GESTURE_PINCH_BEGIN);
4134 // No pinch update occurs, as kCompensateForUnstablePinchZoom is on, and this
4135 // is a very small pinch.
4136 delegate->Reset();
4137 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4138 kTouchId1, tes.Now());
4139 DispatchEventUsingWindowDispatcher(&move2);
4140 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4143 TEST_F(GestureRecognizerTest, GestureEventSmallPinchEnabled) {
4144 scoped_ptr<GestureEventConsumeDelegate> delegate(
4145 new GestureEventConsumeDelegate());
4146 TimedEvents tes;
4147 const int kWindowWidth = 300;
4148 const int kWindowHeight = 400;
4149 const int kTouchId1 = 3;
4150 const int kTouchId2 = 5;
4151 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4152 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4153 delegate.get(), -1234, bounds, root_window()));
4155 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4156 kTouchId1, tes.Now());
4157 DispatchEventUsingWindowDispatcher(&press1);
4158 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4159 kTouchId2, tes.Now());
4160 DispatchEventUsingWindowDispatcher(&press2);
4162 // Move the first finger.
4163 delegate->Reset();
4164 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4165 kTouchId1, tes.Now());
4166 DispatchEventUsingWindowDispatcher(&move1);
4168 EXPECT_3_EVENTS(delegate->events(),
4169 ui::ET_GESTURE_SCROLL_BEGIN,
4170 ui::ET_GESTURE_SCROLL_UPDATE,
4171 ui::ET_GESTURE_PINCH_BEGIN);
4173 delegate->Reset();
4174 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4175 kTouchId1, tes.Now());
4176 DispatchEventUsingWindowDispatcher(&move2);
4177 EXPECT_2_EVENTS(delegate->events(),
4178 ui::ET_GESTURE_SCROLL_UPDATE,
4179 ui::ET_GESTURE_PINCH_UPDATE);
4182 // Tests that delaying the ack of a touch release doesn't trigger a long press
4183 // gesture.
4184 TEST_F(GestureRecognizerTest, EagerGestureDetection) {
4185 scoped_ptr<QueueTouchEventDelegate> delegate(
4186 new QueueTouchEventDelegate(host()->dispatcher()));
4187 TimedEvents tes;
4188 const int kTouchId = 2;
4189 gfx::Rect bounds(100, 200, 100, 100);
4190 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4191 delegate.get(), -1234, bounds, root_window()));
4192 delegate->set_window(window.get());
4194 delegate->Reset();
4195 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4196 kTouchId, tes.Now());
4197 DispatchEventUsingWindowDispatcher(&press);
4198 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
4199 kTouchId, tes.LeapForward(50));
4200 DispatchEventUsingWindowDispatcher(&release);
4202 delegate->Reset();
4203 // Ack the touch press.
4204 delegate->ReceivedAck();
4205 EXPECT_TRUE(delegate->tap_down());
4207 delegate->Reset();
4208 // Wait until the long press event would fire (if we weren't eager).
4209 DelayByLongPressTimeout();
4211 // Ack the touch release.
4212 delegate->ReceivedAck();
4213 EXPECT_TRUE(delegate->tap());
4214 EXPECT_FALSE(delegate->long_press());
4217 // This tests crbug.com/405519, in which events which the gesture detector
4218 // ignores cause future events to also be thrown away.
4219 TEST_F(GestureRecognizerTest, IgnoredEventsDontPreventFutureEvents) {
4220 scoped_ptr<QueueTouchEventDelegate> delegate(
4221 new QueueTouchEventDelegate(host()->dispatcher()));
4222 TimedEvents tes;
4223 const int kWindowWidth = 300;
4224 const int kWindowHeight = 400;
4225 const int kTouchId1 = 3;
4226 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4227 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4228 delegate.get(), -1234, bounds, root_window()));
4229 delegate->set_window(window.get());
4231 ui::TouchEvent press1(
4232 ui::ET_TOUCH_PRESSED, gfx::Point(101, 301), kTouchId1, tes.Now());
4233 DispatchEventUsingWindowDispatcher(&press1);
4234 delegate->ReceivedAck();
4236 EXPECT_2_EVENTS(
4237 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
4239 // Move the first finger.
4240 delegate->Reset();
4241 ui::TouchEvent move1(
4242 ui::ET_TOUCH_MOVED, gfx::Point(65, 201), kTouchId1, tes.Now());
4243 DispatchEventUsingWindowDispatcher(&move1);
4244 delegate->ReceivedAck();
4246 EXPECT_3_EVENTS(delegate->events(),
4247 ui::ET_GESTURE_TAP_CANCEL,
4248 ui::ET_GESTURE_SCROLL_BEGIN,
4249 ui::ET_GESTURE_SCROLL_UPDATE);
4251 delegate->Reset();
4252 ui::TouchEvent move2(
4253 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4254 DispatchEventUsingWindowDispatcher(&move2);
4256 // Send a touchmove event at the same location as the previous touchmove
4257 // event. This shouldn't do anything.
4258 ui::TouchEvent move3(
4259 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4260 DispatchEventUsingWindowDispatcher(&move3);
4262 delegate->ReceivedAck();
4263 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4266 } // namespace test
4267 } // namespace aura