Record MTU discovery packets in net-internals log.
[chromium-blink-merge.git] / ui / aura / gestures / gesture_recognizer_unittest.cc
blobd76658b6b54cd01ea4defc28bda73db71bcac9a8
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 <list>
7 #include "base/command_line.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/run_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/timer/timer.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/test/aura_test_base.h"
15 #include "ui/aura/test/test_window_delegate.h"
16 #include "ui/aura/test/test_windows.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/base/hit_test.h"
20 #include "ui/base/ui_base_switches.h"
21 #include "ui/events/event.h"
22 #include "ui/events/event_switches.h"
23 #include "ui/events/event_utils.h"
24 #include "ui/events/gesture_detection/gesture_configuration.h"
25 #include "ui/events/gestures/gesture_recognizer_impl.h"
26 #include "ui/events/gestures/gesture_types.h"
27 #include "ui/events/test/event_generator.h"
28 #include "ui/events/test/events_test_utils.h"
29 #include "ui/gfx/geometry/point.h"
30 #include "ui/gfx/geometry/rect.h"
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 ~GestureEventConsumeDelegate() override {}
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;
143 const std::vector<ui::EventType>& events() const { return events_; };
145 bool tap() const { return tap_; }
146 bool tap_down() const { return tap_down_; }
147 bool tap_cancel() const { return tap_cancel_; }
148 bool begin() const { return begin_; }
149 bool end() const { return end_; }
150 bool scroll_begin() const { return scroll_begin_; }
151 bool scroll_update() const { return scroll_update_; }
152 bool scroll_end() const { return scroll_end_; }
153 bool pinch_begin() const { return pinch_begin_; }
154 bool pinch_update() const { return pinch_update_; }
155 bool pinch_end() const { return pinch_end_; }
156 bool long_press() const { return long_press_; }
157 bool long_tap() const { return long_tap_; }
158 bool fling() const { return fling_; }
159 bool two_finger_tap() const { return two_finger_tap_; }
160 bool show_press() const { return show_press_; }
161 bool swipe_left() const { return swipe_left_; }
162 bool swipe_right() const { return swipe_right_; }
163 bool swipe_up() const { return swipe_up_; }
164 bool swipe_down() const { return swipe_down_; }
166 const gfx::Point& scroll_begin_position() const {
167 return scroll_begin_position_;
170 const gfx::Point& tap_location() const {
171 return tap_location_;
174 const gfx::Point& gesture_end_location() const {
175 return gesture_end_location_;
178 float scroll_x() const { return scroll_x_; }
179 float scroll_y() const { return scroll_y_; }
180 float scroll_velocity_x() const { return scroll_velocity_x_; }
181 float scroll_velocity_y() const { return scroll_velocity_y_; }
182 float velocity_x() const { return velocity_x_; }
183 float velocity_y() const { return velocity_y_; }
184 float scroll_x_hint() const { return scroll_x_hint_; }
185 float scroll_y_hint() const { return scroll_y_hint_; }
186 float scale() const { return scale_; }
187 const gfx::Rect& bounding_box() const { return bounding_box_; }
188 int tap_count() const { return tap_count_; }
189 int flags() const { return flags_; }
191 void WaitUntilReceivedGesture(ui::EventType type) {
192 wait_until_event_ = type;
193 run_loop_.reset(new base::RunLoop());
194 run_loop_->Run();
197 void OnGestureEvent(ui::GestureEvent* gesture) override {
198 events_.push_back(gesture->type());
199 bounding_box_ = gesture->details().bounding_box();
200 flags_ = gesture->flags();
201 switch (gesture->type()) {
202 case ui::ET_GESTURE_TAP:
203 tap_location_ = gesture->location();
204 tap_count_ = gesture->details().tap_count();
205 tap_ = true;
206 break;
207 case ui::ET_GESTURE_TAP_DOWN:
208 tap_down_ = true;
209 break;
210 case ui::ET_GESTURE_TAP_CANCEL:
211 tap_cancel_ = true;
212 break;
213 case ui::ET_GESTURE_BEGIN:
214 begin_ = true;
215 break;
216 case ui::ET_GESTURE_END:
217 end_ = true;
218 gesture_end_location_ = gesture->location();
219 break;
220 case ui::ET_GESTURE_SCROLL_BEGIN:
221 scroll_begin_ = true;
222 scroll_begin_position_ = gesture->location();
223 scroll_x_hint_ = gesture->details().scroll_x_hint();
224 scroll_y_hint_ = gesture->details().scroll_y_hint();
225 break;
226 case ui::ET_GESTURE_SCROLL_UPDATE:
227 scroll_update_ = true;
228 scroll_x_ += gesture->details().scroll_x();
229 scroll_y_ += gesture->details().scroll_y();
230 break;
231 case ui::ET_GESTURE_SCROLL_END:
232 EXPECT_TRUE(velocity_x_ == 0 && velocity_y_ == 0);
233 scroll_end_ = true;
234 break;
235 case ui::ET_GESTURE_PINCH_BEGIN:
236 pinch_begin_ = true;
237 break;
238 case ui::ET_GESTURE_PINCH_UPDATE:
239 pinch_update_ = true;
240 scale_ = gesture->details().scale();
241 break;
242 case ui::ET_GESTURE_PINCH_END:
243 pinch_end_ = true;
244 break;
245 case ui::ET_GESTURE_LONG_PRESS:
246 long_press_ = true;
247 break;
248 case ui::ET_GESTURE_LONG_TAP:
249 long_tap_ = true;
250 break;
251 case ui::ET_SCROLL_FLING_START:
252 EXPECT_TRUE(gesture->details().velocity_x() != 0 ||
253 gesture->details().velocity_y() != 0);
254 EXPECT_FALSE(scroll_end_);
255 fling_ = true;
256 velocity_x_ = gesture->details().velocity_x();
257 velocity_y_ = gesture->details().velocity_y();
258 break;
259 case ui::ET_GESTURE_TWO_FINGER_TAP:
260 two_finger_tap_ = true;
261 break;
262 case ui::ET_GESTURE_SHOW_PRESS:
263 show_press_ = true;
264 break;
265 case ui::ET_GESTURE_SWIPE:
266 swipe_left_ = gesture->details().swipe_left();
267 swipe_right_ = gesture->details().swipe_right();
268 swipe_up_ = gesture->details().swipe_up();
269 swipe_down_ = gesture->details().swipe_down();
270 break;
271 case ui::ET_SCROLL_FLING_CANCEL:
272 // Only used in unified gesture detection.
273 break;
274 default:
275 NOTREACHED();
277 if (wait_until_event_ == gesture->type() && run_loop_) {
278 run_loop_->Quit();
279 wait_until_event_ = ui::ET_UNKNOWN;
281 gesture->StopPropagation();
284 private:
285 scoped_ptr<base::RunLoop> run_loop_;
286 std::vector<ui::EventType> events_;
288 bool tap_;
289 bool tap_down_;
290 bool tap_cancel_;
291 bool begin_;
292 bool end_;
293 bool scroll_begin_;
294 bool scroll_update_;
295 bool scroll_end_;
296 bool pinch_begin_;
297 bool pinch_update_;
298 bool pinch_end_;
299 bool long_press_;
300 bool long_tap_;
301 bool fling_;
302 bool two_finger_tap_;
303 bool show_press_;
304 bool swipe_left_;
305 bool swipe_right_;
306 bool swipe_up_;
307 bool swipe_down_;
309 gfx::Point scroll_begin_position_;
310 gfx::Point tap_location_;
311 gfx::Point gesture_end_location_;
313 float scroll_x_;
314 float scroll_y_;
315 float scroll_velocity_x_;
316 float scroll_velocity_y_;
317 float velocity_x_;
318 float velocity_y_;
319 float scroll_x_hint_;
320 float scroll_y_hint_;
321 float scale_;
322 gfx::Rect bounding_box_;
323 int tap_count_;
324 int flags_;
326 ui::EventType wait_until_event_;
328 DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate);
331 class QueueTouchEventDelegate : public GestureEventConsumeDelegate {
332 public:
333 explicit QueueTouchEventDelegate(WindowEventDispatcher* dispatcher)
334 : window_(NULL),
335 dispatcher_(dispatcher),
336 synchronous_ack_for_next_event_(AckState::PENDING) {}
338 ~QueueTouchEventDelegate() override {}
340 void OnTouchEvent(ui::TouchEvent* event) override {
341 event->DisableSynchronousHandling();
342 if (synchronous_ack_for_next_event_ != AckState::PENDING) {
343 ui::GestureRecognizer::Get()->AckTouchEvent(
344 event->unique_event_id(),
345 synchronous_ack_for_next_event_ == AckState::CONSUMED
346 ? ui::ER_CONSUMED
347 : ui::ER_UNHANDLED,
348 window_);
349 synchronous_ack_for_next_event_ = AckState::PENDING;
350 } else {
351 sent_events_ids_.push_back(event->unique_event_id());
355 void ReceivedAck() {
356 ReceivedAckImpl(false);
359 void ReceivedAckPreventDefaulted() {
360 ReceivedAckImpl(true);
363 void set_window(Window* w) { window_ = w; }
364 void set_synchronous_ack_for_next_event(bool consumed) {
365 DCHECK(synchronous_ack_for_next_event_ == AckState::PENDING);
366 synchronous_ack_for_next_event_ =
367 consumed ? AckState::CONSUMED : AckState::UNCONSUMED;
370 private:
371 enum class AckState {
372 PENDING,
373 CONSUMED,
374 UNCONSUMED,
377 void ReceivedAckImpl(bool prevent_defaulted) {
378 DCHECK(!sent_events_ids_.empty());
379 if (sent_events_ids_.empty())
380 return;
381 uint32 sent_event_id = sent_events_ids_.front();
382 sent_events_ids_.pop_front();
383 dispatcher_->ProcessedTouchEvent(
384 sent_event_id, window_,
385 prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED);
388 Window* window_;
389 WindowEventDispatcher* dispatcher_;
390 AckState synchronous_ack_for_next_event_;
391 std::list<uint32> sent_events_ids_;
393 DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate);
396 // A delegate that ignores gesture events but keeps track of [synthetic] mouse
397 // events.
398 class GestureEventSynthDelegate : public TestWindowDelegate {
399 public:
400 GestureEventSynthDelegate()
401 : mouse_enter_(false),
402 mouse_exit_(false),
403 mouse_press_(false),
404 mouse_release_(false),
405 mouse_move_(false),
406 double_click_(false) {
409 void Reset() {
410 mouse_enter_ = false;
411 mouse_exit_ = false;
412 mouse_press_ = false;
413 mouse_release_ = false;
414 mouse_move_ = false;
415 double_click_ = false;
418 bool mouse_enter() const { return mouse_enter_; }
419 bool mouse_exit() const { return mouse_exit_; }
420 bool mouse_press() const { return mouse_press_; }
421 bool mouse_move() const { return mouse_move_; }
422 bool mouse_release() const { return mouse_release_; }
423 bool double_click() const { return double_click_; }
425 void OnMouseEvent(ui::MouseEvent* event) override {
426 switch (event->type()) {
427 case ui::ET_MOUSE_PRESSED:
428 double_click_ = event->flags() & ui::EF_IS_DOUBLE_CLICK;
429 mouse_press_ = true;
430 break;
431 case ui::ET_MOUSE_RELEASED:
432 mouse_release_ = true;
433 break;
434 case ui::ET_MOUSE_MOVED:
435 mouse_move_ = true;
436 break;
437 case ui::ET_MOUSE_ENTERED:
438 mouse_enter_ = true;
439 break;
440 case ui::ET_MOUSE_EXITED:
441 mouse_exit_ = true;
442 break;
443 default:
444 NOTREACHED();
446 event->SetHandled();
449 private:
450 bool mouse_enter_;
451 bool mouse_exit_;
452 bool mouse_press_;
453 bool mouse_release_;
454 bool mouse_move_;
455 bool double_click_;
457 DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate);
460 class ScopedGestureRecognizerSetter {
461 public:
462 // Takes ownership of |new_gr|.
463 explicit ScopedGestureRecognizerSetter(ui::GestureRecognizer* new_gr)
464 : new_gr_(new_gr) {
465 original_gr_ = ui::GestureRecognizer::Get();
466 ui::SetGestureRecognizerForTesting(new_gr_.get());
469 virtual ~ScopedGestureRecognizerSetter() {
470 ui::SetGestureRecognizerForTesting(original_gr_);
473 private:
474 ui::GestureRecognizer* original_gr_;
475 scoped_ptr<ui::GestureRecognizer> new_gr_;
477 DISALLOW_COPY_AND_ASSIGN(ScopedGestureRecognizerSetter);
480 class TimedEvents {
481 private:
482 int simulated_now_;
484 public:
485 // Use a non-zero start time to pass DCHECKs which ensure events have had a
486 // time assigned.
487 TimedEvents() : simulated_now_(1) {
490 base::TimeDelta Now() {
491 base::TimeDelta t = base::TimeDelta::FromMilliseconds(simulated_now_);
492 simulated_now_++;
493 return t;
496 base::TimeDelta LeapForward(int time_in_millis) {
497 simulated_now_ += time_in_millis;
498 return base::TimeDelta::FromMilliseconds(simulated_now_);
501 base::TimeDelta InFuture(int time_in_millis) {
502 return base::TimeDelta::FromMilliseconds(simulated_now_ + time_in_millis);
505 void SendScrollEvents(ui::EventProcessor* dispatcher,
506 float x_start,
507 float y_start,
508 int dx,
509 int dy,
510 int touch_id,
511 int time_step,
512 int num_steps,
513 GestureEventConsumeDelegate* delegate) {
514 float x = x_start;
515 float y = y_start;
517 for (int i = 0; i < num_steps; i++) {
518 x += dx;
519 y += dy;
520 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
521 touch_id,
522 base::TimeDelta::FromMilliseconds(simulated_now_));
523 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
524 ASSERT_FALSE(details.dispatcher_destroyed);
525 simulated_now_ += time_step;
529 void SendScrollEvent(ui::EventProcessor* dispatcher,
530 float x,
531 float y,
532 int touch_id,
533 GestureEventConsumeDelegate* delegate) {
534 delegate->Reset();
535 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
536 touch_id,
537 base::TimeDelta::FromMilliseconds(simulated_now_));
538 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
539 ASSERT_FALSE(details.dispatcher_destroyed);
540 simulated_now_++;
544 // An event handler to keep track of events.
545 class TestEventHandler : public ui::EventHandler {
546 public:
547 TestEventHandler()
548 : touch_released_count_(0),
549 touch_pressed_count_(0),
550 touch_moved_count_(0) {}
552 ~TestEventHandler() override {}
554 void OnTouchEvent(ui::TouchEvent* event) override {
555 switch (event->type()) {
556 case ui::ET_TOUCH_RELEASED:
557 touch_released_count_++;
558 break;
559 case ui::ET_TOUCH_PRESSED:
560 touch_pressed_count_++;
561 break;
562 case ui::ET_TOUCH_MOVED:
563 touch_moved_count_++;
564 break;
565 case ui::ET_TOUCH_CANCELLED:
566 cancelled_touch_points_.push_back(event->location());
567 break;
568 default:
569 break;
573 void Reset() {
574 touch_released_count_ = 0;
575 touch_pressed_count_ = 0;
576 touch_moved_count_ = 0;
577 cancelled_touch_points_.clear();
580 int touch_released_count() const { return touch_released_count_; }
581 int touch_pressed_count() const { return touch_pressed_count_; }
582 int touch_moved_count() const { return touch_moved_count_; }
583 int touch_cancelled_count() const {
584 return static_cast<int>(cancelled_touch_points_.size());
586 const std::vector<gfx::PointF>& cancelled_touch_points() const {
587 return cancelled_touch_points_;
590 private:
591 int touch_released_count_;
592 int touch_pressed_count_;
593 int touch_moved_count_;
594 std::vector<gfx::PointF> cancelled_touch_points_;
596 DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
599 // Removes the target window from its parent when it receives a touch-cancel
600 // event.
601 class RemoveOnTouchCancelHandler : public TestEventHandler {
602 public:
603 RemoveOnTouchCancelHandler() {}
604 ~RemoveOnTouchCancelHandler() override {}
606 private:
607 // ui::EventHandler:
608 void OnTouchEvent(ui::TouchEvent* event) override {
609 TestEventHandler::OnTouchEvent(event);
610 if (event->type() == ui::ET_TOUCH_CANCELLED) {
611 Window* target = static_cast<Window*>(event->target());
612 target->parent()->RemoveChild(target);
616 DISALLOW_COPY_AND_ASSIGN(RemoveOnTouchCancelHandler);
619 void DelayByLongPressTimeout() {
620 ui::GestureProvider::Config config;
621 base::RunLoop run_loop;
622 base::MessageLoop::current()->PostDelayedTask(
623 FROM_HERE,
624 run_loop.QuitClosure(),
625 config.gesture_detector_config.longpress_timeout * 2);
626 run_loop.Run();
629 void DelayByShowPressTimeout() {
630 ui::GestureProvider::Config config;
631 base::RunLoop run_loop;
632 base::MessageLoop::current()->PostDelayedTask(
633 FROM_HERE,
634 run_loop.QuitClosure(),
635 config.gesture_detector_config.showpress_timeout * 2);
636 run_loop.Run();
639 } // namespace
641 class GestureRecognizerTest : public AuraTestBase,
642 public ::testing::WithParamInterface<bool> {
643 public:
644 GestureRecognizerTest() {}
646 void SetUp() override {
647 AuraTestBase::SetUp();
648 ui::GestureConfiguration::GetInstance()->set_show_press_delay_in_ms(2);
649 ui::GestureConfiguration::GetInstance()->set_long_press_time_in_ms(3);
652 private:
653 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerTest);
656 class GestureRecognizerWithSwitchTest : public GestureRecognizerTest {
657 public:
658 GestureRecognizerWithSwitchTest() {}
660 void SetUp() override {
661 GestureRecognizerTest::SetUp();
662 base::CommandLine::ForCurrentProcess()->AppendSwitch(
663 switches::kCompensateForUnstablePinchZoom);
664 ui::GestureConfiguration::GetInstance()->set_min_pinch_update_span_delta(5);
667 private:
668 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerWithSwitchTest);
671 // Check that appropriate touch events generate tap gesture events.
672 TEST_F(GestureRecognizerTest, GestureEventTap) {
673 scoped_ptr<GestureEventConsumeDelegate> delegate(
674 new GestureEventConsumeDelegate());
675 TimedEvents tes;
676 const int kWindowWidth = 123;
677 const int kWindowHeight = 45;
678 const int kTouchId = 2;
679 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
680 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
681 delegate.get(), -1234, bounds, root_window()));
683 delegate->Reset();
684 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
685 kTouchId, tes.Now());
686 DispatchEventUsingWindowDispatcher(&press);
687 EXPECT_FALSE(delegate->tap());
688 EXPECT_FALSE(delegate->show_press());
689 EXPECT_TRUE(delegate->tap_down());
690 EXPECT_FALSE(delegate->tap_cancel());
691 EXPECT_TRUE(delegate->begin());
692 EXPECT_FALSE(delegate->scroll_begin());
693 EXPECT_FALSE(delegate->scroll_update());
694 EXPECT_FALSE(delegate->scroll_end());
695 EXPECT_FALSE(delegate->long_press());
697 delegate->Reset();
698 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
699 EXPECT_TRUE(delegate->show_press());
700 EXPECT_FALSE(delegate->tap_down());
702 // Make sure there is enough delay before the touch is released so that it is
703 // recognized as a tap.
704 delegate->Reset();
705 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
706 kTouchId, tes.LeapForward(50));
708 DispatchEventUsingWindowDispatcher(&release);
709 EXPECT_TRUE(delegate->tap());
710 EXPECT_FALSE(delegate->tap_down());
711 EXPECT_FALSE(delegate->tap_cancel());
712 EXPECT_FALSE(delegate->begin());
713 EXPECT_TRUE(delegate->end());
714 EXPECT_FALSE(delegate->scroll_begin());
715 EXPECT_FALSE(delegate->scroll_update());
716 EXPECT_FALSE(delegate->scroll_end());
718 EXPECT_EQ(1, delegate->tap_count());
721 // Check that appropriate touch events generate tap gesture events
722 // when information about the touch radii are provided.
723 TEST_F(GestureRecognizerTest, GestureEventTapRegion) {
724 scoped_ptr<GestureEventConsumeDelegate> delegate(
725 new GestureEventConsumeDelegate());
726 TimedEvents tes;
727 const int kWindowWidth = 800;
728 const int kWindowHeight = 600;
729 const int kTouchId = 2;
730 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
731 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
732 delegate.get(), -1234, bounds, root_window()));
734 // Test with no ET_TOUCH_MOVED events.
736 delegate->Reset();
737 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
738 kTouchId, tes.Now());
739 press.set_radius_x(5);
740 press.set_radius_y(12);
741 DispatchEventUsingWindowDispatcher(&press);
742 EXPECT_FALSE(delegate->tap());
743 EXPECT_TRUE(delegate->tap_down());
744 EXPECT_FALSE(delegate->tap_cancel());
745 EXPECT_TRUE(delegate->begin());
746 EXPECT_FALSE(delegate->scroll_begin());
747 EXPECT_FALSE(delegate->scroll_update());
748 EXPECT_FALSE(delegate->scroll_end());
749 EXPECT_FALSE(delegate->long_press());
751 // Make sure there is enough delay before the touch is released so that it
752 // is recognized as a tap.
753 delegate->Reset();
754 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
755 kTouchId, tes.LeapForward(50));
756 release.set_radius_x(5);
757 release.set_radius_y(12);
759 DispatchEventUsingWindowDispatcher(&release);
760 EXPECT_TRUE(delegate->tap());
761 EXPECT_FALSE(delegate->tap_down());
762 EXPECT_FALSE(delegate->tap_cancel());
763 EXPECT_FALSE(delegate->begin());
764 EXPECT_TRUE(delegate->end());
765 EXPECT_FALSE(delegate->scroll_begin());
766 EXPECT_FALSE(delegate->scroll_update());
767 EXPECT_FALSE(delegate->scroll_end());
769 EXPECT_EQ(1, delegate->tap_count());
770 gfx::Point actual_point(delegate->tap_location());
771 EXPECT_EQ(24, delegate->bounding_box().width());
772 EXPECT_EQ(24, delegate->bounding_box().height());
773 EXPECT_EQ(101, actual_point.x());
774 EXPECT_EQ(201, actual_point.y());
777 // Test with no ET_TOUCH_MOVED events but different touch points and radii.
779 delegate->Reset();
780 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(365, 290),
781 kTouchId, tes.Now());
782 press.set_radius_x(8);
783 press.set_radius_y(14);
784 DispatchEventUsingWindowDispatcher(&press);
785 EXPECT_FALSE(delegate->tap());
786 EXPECT_TRUE(delegate->tap_down());
787 EXPECT_FALSE(delegate->tap_cancel());
788 EXPECT_TRUE(delegate->begin());
789 EXPECT_FALSE(delegate->scroll_begin());
790 EXPECT_FALSE(delegate->scroll_update());
791 EXPECT_FALSE(delegate->scroll_end());
792 EXPECT_FALSE(delegate->long_press());
794 delegate->Reset();
795 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(367, 291),
796 kTouchId, tes.LeapForward(50));
797 release.set_radius_x(20);
798 release.set_radius_y(13);
800 DispatchEventUsingWindowDispatcher(&release);
801 EXPECT_TRUE(delegate->tap());
802 EXPECT_FALSE(delegate->tap_down());
803 EXPECT_FALSE(delegate->tap_cancel());
804 EXPECT_FALSE(delegate->begin());
805 EXPECT_TRUE(delegate->end());
806 EXPECT_FALSE(delegate->scroll_begin());
807 EXPECT_FALSE(delegate->scroll_update());
808 EXPECT_FALSE(delegate->scroll_end());
810 EXPECT_EQ(1, delegate->tap_count());
811 gfx::Point actual_point(delegate->tap_location());
812 EXPECT_EQ(40, delegate->bounding_box().width());
813 EXPECT_EQ(40, delegate->bounding_box().height());
814 EXPECT_EQ(367, actual_point.x());
815 EXPECT_EQ(291, actual_point.y());
818 // Test with a single ET_TOUCH_MOVED event.
820 delegate->Reset();
821 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(46, 205),
822 kTouchId, tes.Now());
823 press.set_radius_x(6);
824 press.set_radius_y(10);
825 DispatchEventUsingWindowDispatcher(&press);
826 EXPECT_FALSE(delegate->tap());
827 EXPECT_TRUE(delegate->tap_down());
828 EXPECT_FALSE(delegate->tap_cancel());
829 EXPECT_TRUE(delegate->begin());
830 EXPECT_FALSE(delegate->tap_cancel());
831 EXPECT_FALSE(delegate->scroll_begin());
832 EXPECT_FALSE(delegate->scroll_update());
833 EXPECT_FALSE(delegate->scroll_end());
834 EXPECT_FALSE(delegate->long_press());
836 delegate->Reset();
837 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(49, 204),
838 kTouchId, tes.LeapForward(50));
839 move.set_radius_x(8);
840 move.set_radius_y(12);
841 DispatchEventUsingWindowDispatcher(&move);
842 EXPECT_FALSE(delegate->tap());
843 EXPECT_FALSE(delegate->tap_down());
844 EXPECT_FALSE(delegate->tap_cancel());
845 EXPECT_FALSE(delegate->begin());
846 EXPECT_FALSE(delegate->scroll_begin());
847 EXPECT_FALSE(delegate->scroll_update());
848 EXPECT_FALSE(delegate->scroll_end());
849 EXPECT_FALSE(delegate->long_press());
851 delegate->Reset();
852 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(49, 204),
853 kTouchId, tes.LeapForward(50));
854 release.set_radius_x(4);
855 release.set_radius_y(8);
857 DispatchEventUsingWindowDispatcher(&release);
858 EXPECT_TRUE(delegate->tap());
859 EXPECT_FALSE(delegate->tap_down());
860 EXPECT_FALSE(delegate->tap_cancel());
861 EXPECT_FALSE(delegate->begin());
862 EXPECT_TRUE(delegate->end());
863 EXPECT_FALSE(delegate->scroll_begin());
864 EXPECT_FALSE(delegate->scroll_update());
865 EXPECT_FALSE(delegate->scroll_end());
867 EXPECT_EQ(1, delegate->tap_count());
868 gfx::Point actual_point(delegate->tap_location());
869 EXPECT_EQ(16, delegate->bounding_box().width());
870 EXPECT_EQ(16, delegate->bounding_box().height());
871 EXPECT_EQ(49, actual_point.x());
872 EXPECT_EQ(204, actual_point.y());
875 // Test with a few ET_TOUCH_MOVED events.
877 delegate->Reset();
878 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(400, 150),
879 kTouchId, tes.Now());
880 press.set_radius_x(7);
881 press.set_radius_y(10);
882 DispatchEventUsingWindowDispatcher(&press);
883 EXPECT_FALSE(delegate->tap());
884 EXPECT_TRUE(delegate->tap_down());
885 EXPECT_FALSE(delegate->tap_cancel());
886 EXPECT_TRUE(delegate->begin());
887 EXPECT_FALSE(delegate->scroll_begin());
888 EXPECT_FALSE(delegate->scroll_update());
889 EXPECT_FALSE(delegate->scroll_end());
890 EXPECT_FALSE(delegate->long_press());
892 delegate->Reset();
893 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(397, 151),
894 kTouchId, tes.LeapForward(50));
895 move.set_radius_x(13);
896 move.set_radius_y(12);
897 DispatchEventUsingWindowDispatcher(&move);
898 EXPECT_FALSE(delegate->tap());
899 EXPECT_FALSE(delegate->tap_down());
900 EXPECT_FALSE(delegate->tap_cancel());
901 EXPECT_FALSE(delegate->begin());
902 EXPECT_FALSE(delegate->scroll_begin());
903 EXPECT_FALSE(delegate->scroll_update());
904 EXPECT_FALSE(delegate->scroll_end());
905 EXPECT_FALSE(delegate->long_press());
907 delegate->Reset();
908 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(397, 149),
909 kTouchId, tes.LeapForward(50));
910 move1.set_radius_x(16);
911 move1.set_radius_y(16);
912 DispatchEventUsingWindowDispatcher(&move1);
913 EXPECT_FALSE(delegate->tap());
914 EXPECT_FALSE(delegate->tap_down());
915 EXPECT_FALSE(delegate->tap_cancel());
916 EXPECT_FALSE(delegate->begin());
917 EXPECT_FALSE(delegate->scroll_begin());
918 EXPECT_FALSE(delegate->scroll_update());
919 EXPECT_FALSE(delegate->scroll_end());
920 EXPECT_FALSE(delegate->long_press());
922 delegate->Reset();
923 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(400, 150),
924 kTouchId, tes.LeapForward(50));
925 move2.set_radius_x(14);
926 move2.set_radius_y(10);
927 DispatchEventUsingWindowDispatcher(&move2);
928 EXPECT_FALSE(delegate->tap());
929 EXPECT_FALSE(delegate->tap_down());
930 EXPECT_FALSE(delegate->tap_cancel());
931 EXPECT_FALSE(delegate->begin());
932 EXPECT_FALSE(delegate->scroll_begin());
933 EXPECT_FALSE(delegate->scroll_update());
934 EXPECT_FALSE(delegate->scroll_end());
935 EXPECT_FALSE(delegate->long_press());
937 delegate->Reset();
938 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(401, 149),
939 kTouchId, tes.LeapForward(50));
940 release.set_radius_x(8);
941 release.set_radius_y(9);
943 DispatchEventUsingWindowDispatcher(&release);
944 EXPECT_TRUE(delegate->tap());
945 EXPECT_FALSE(delegate->tap_down());
946 EXPECT_FALSE(delegate->tap_cancel());
947 EXPECT_FALSE(delegate->begin());
948 EXPECT_TRUE(delegate->end());
949 EXPECT_FALSE(delegate->scroll_begin());
950 EXPECT_FALSE(delegate->scroll_update());
951 EXPECT_FALSE(delegate->scroll_end());
953 EXPECT_EQ(1, delegate->tap_count());
954 gfx::Point actual_point(delegate->tap_location());
955 EXPECT_EQ(18, delegate->bounding_box().width());
956 EXPECT_EQ(18, delegate->bounding_box().height());
957 EXPECT_EQ(401, actual_point.x());
958 EXPECT_EQ(149, actual_point.y());
962 // Check that appropriate touch events generate scroll gesture events.
963 TEST_F(GestureRecognizerTest, GestureEventScroll) {
964 // We'll start by moving the touch point by (10.5, 10.5). We want 5 dips of
965 // that distance to be consumed by the slop, so we set the slop radius to
966 // sqrt(5 * 5 + 5 * 5).
967 ui::GestureConfiguration::GetInstance()
968 ->set_max_touch_move_in_pixels_for_click(sqrt(5.f * 5 + 5 * 5));
969 scoped_ptr<GestureEventConsumeDelegate> delegate(
970 new GestureEventConsumeDelegate());
971 TimedEvents tes;
972 const int kWindowWidth = 123;
973 const int kWindowHeight = 45;
974 const int kTouchId = 5;
975 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
976 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
977 delegate.get(), -1234, bounds, root_window()));
979 delegate->Reset();
980 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
981 kTouchId, tes.Now());
982 DispatchEventUsingWindowDispatcher(&press);
983 EXPECT_2_EVENTS(delegate->events(),
984 ui::ET_GESTURE_BEGIN,
985 ui::ET_GESTURE_TAP_DOWN);
987 // Move the touch-point enough so that it is considered as a scroll. This
988 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
989 // The first movement is diagonal, to ensure that we have a free scroll,
990 // and not a rail scroll.
991 tes.SendScrollEvent(event_processor(), 111.5, 211.5, kTouchId,
992 delegate.get());
993 EXPECT_3_EVENTS(delegate->events(),
994 ui::ET_GESTURE_TAP_CANCEL,
995 ui::ET_GESTURE_SCROLL_BEGIN,
996 ui::ET_GESTURE_SCROLL_UPDATE);
997 // The slop consumed 5 dips
998 EXPECT_FLOAT_EQ(5.5, delegate->scroll_x());
999 EXPECT_FLOAT_EQ(5.5, delegate->scroll_y());
1000 EXPECT_EQ(gfx::Point(1, 1).ToString(),
1001 delegate->scroll_begin_position().ToString());
1003 // When scrolling with a single finger, the bounding box of the gesture should
1004 // be empty, since it's a single point and the radius for testing is zero.
1005 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1007 // Move some more to generate a few more scroll updates. Make sure that we get
1008 // out of the snap channel for the unified GR.
1009 tes.SendScrollEvent(event_processor(), 20, 120, kTouchId, delegate.get());
1010 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1011 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_x());
1012 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_y());
1013 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1015 tes.SendScrollEvent(event_processor(), 50, 124, kTouchId, delegate.get());
1016 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1017 EXPECT_EQ(30, delegate->scroll_x());
1018 EXPECT_EQ(4, delegate->scroll_y());
1019 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1021 // Release the touch. This should end the scroll.
1022 delegate->Reset();
1023 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1024 kTouchId,
1025 tes.LeapForward(50));
1026 DispatchEventUsingWindowDispatcher(&release);
1027 EXPECT_2_EVENTS(delegate->events(),
1028 ui::ET_SCROLL_FLING_START,
1029 ui::ET_GESTURE_END);
1030 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1033 // Check that predicted scroll update positions are correct.
1034 TEST_F(GestureRecognizerTest, GestureEventScrollPrediction) {
1035 // We'll start by moving the touch point by (5, 5). We want all of that
1036 // distance to be consumed by the slop, so we set the slop radius to
1037 // sqrt(5 * 5 + 5 * 5).
1038 ui::GestureConfiguration::GetInstance()
1039 ->set_max_touch_move_in_pixels_for_click(sqrt(5.f * 5 + 5 * 5));
1041 scoped_ptr<GestureEventConsumeDelegate> delegate(
1042 new GestureEventConsumeDelegate());
1043 TimedEvents tes;
1044 const int kWindowWidth = 123;
1045 const int kWindowHeight = 45;
1046 const int kTouchId = 5;
1047 gfx::Rect bounds(95, 195, kWindowWidth, kWindowHeight);
1048 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1049 delegate.get(), -1234, bounds, root_window()));
1051 delegate->Reset();
1052 // Tracks the total scroll since we want to verify that the correct position
1053 // will be scrolled to throughout the prediction.
1054 gfx::Vector2dF total_scroll;
1055 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(96, 196),
1056 kTouchId, tes.Now());
1057 DispatchEventUsingWindowDispatcher(&press);
1058 EXPECT_2_EVENTS(delegate->events(),
1059 ui::ET_GESTURE_BEGIN,
1060 ui::ET_GESTURE_TAP_DOWN);
1061 delegate->Reset();
1063 // Get rid of touch slop.
1064 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(111, 211),
1065 kTouchId, tes.Now());
1066 DispatchEventUsingWindowDispatcher(&move);
1067 EXPECT_3_EVENTS(delegate->events(),
1068 ui::ET_GESTURE_TAP_CANCEL,
1069 ui::ET_GESTURE_SCROLL_BEGIN,
1070 ui::ET_GESTURE_SCROLL_UPDATE);
1071 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1072 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1074 // Move the touch-point enough so that it is considered as a scroll. This
1075 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1076 // The first movement is diagonal, to ensure that we have a free scroll,
1077 // and not a rail scroll.
1078 tes.LeapForward(30);
1079 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1080 EXPECT_1_EVENT(delegate->events(),
1081 ui::ET_GESTURE_SCROLL_UPDATE);
1082 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1083 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1085 // Move some more to generate a few more scroll updates.
1086 tes.LeapForward(30);
1087 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
1088 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1089 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1090 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1092 tes.LeapForward(30);
1093 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
1094 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1095 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1096 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1098 // Release the touch. This should end the scroll.
1099 delegate->Reset();
1100 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1101 kTouchId,
1102 tes.LeapForward(50));
1103 DispatchEventUsingWindowDispatcher(&release);
1106 // Check that the bounding box during a scroll event is correct.
1107 TEST_F(GestureRecognizerTest, GestureEventScrollBoundingBox) {
1108 TimedEvents tes;
1109 for (float radius = 1; radius <= 10; ++radius) {
1110 ui::GestureConfiguration::GetInstance()->set_default_radius(radius);
1111 scoped_ptr<GestureEventConsumeDelegate> delegate(
1112 new GestureEventConsumeDelegate());
1113 const int kWindowWidth = 123;
1114 const int kWindowHeight = 45;
1115 const int kTouchId = 5;
1116 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1117 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1118 delegate.get(), -1234, bounds, root_window()));
1120 const float kPositionX = 101;
1121 const float kPositionY = 201;
1122 delegate->Reset();
1123 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
1124 gfx::PointF(kPositionX, kPositionY),
1125 kTouchId,
1126 tes.Now());
1127 DispatchEventUsingWindowDispatcher(&press);
1128 EXPECT_EQ(gfx::RectF(kPositionX - radius,
1129 kPositionY - radius,
1130 radius * 2,
1131 radius * 2),
1132 delegate->bounding_box());
1134 const int kScrollAmount = 50;
1135 tes.SendScrollEvents(event_processor(), kPositionX, kPositionY,
1136 1, 1, kTouchId, 1, kScrollAmount, delegate.get());
1137 EXPECT_EQ(gfx::Point(1, 1).ToString(),
1138 delegate->scroll_begin_position().ToString());
1139 EXPECT_EQ(gfx::RectF(kPositionX + kScrollAmount - radius,
1140 kPositionY + kScrollAmount - radius,
1141 radius * 2,
1142 radius * 2),
1143 delegate->bounding_box());
1145 // Release the touch. This should end the scroll.
1146 delegate->Reset();
1147 ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
1148 gfx::PointF(kPositionX + kScrollAmount,
1149 kPositionY + kScrollAmount),
1150 kTouchId, press.time_stamp() +
1151 base::TimeDelta::FromMilliseconds(50));
1152 DispatchEventUsingWindowDispatcher(&release);
1153 EXPECT_EQ(gfx::RectF(kPositionX + kScrollAmount - radius,
1154 kPositionY + kScrollAmount - radius,
1155 radius * 2,
1156 radius * 2),
1157 delegate->bounding_box());
1159 ui::GestureConfiguration::GetInstance()->set_default_radius(0);
1162 // Check Scroll End Events report correct velocities
1163 // if the user was on a horizontal rail
1164 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailFling) {
1165 scoped_ptr<GestureEventConsumeDelegate> delegate(
1166 new GestureEventConsumeDelegate());
1167 TimedEvents tes;
1168 const int kTouchId = 7;
1169 gfx::Rect bounds(0, 0, 1000, 1000);
1170 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1171 delegate.get(), -1234, bounds, root_window()));
1173 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1174 kTouchId, tes.Now());
1175 DispatchEventUsingWindowDispatcher(&press);
1177 // Get rid of touch slop.
1178 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 0),
1179 kTouchId, tes.Now());
1180 DispatchEventUsingWindowDispatcher(&move);
1181 delegate->Reset();
1184 // Move the touch-point horizontally enough that it is considered a
1185 // horizontal scroll.
1186 tes.SendScrollEvent(event_processor(), 30, 1, kTouchId, delegate.get());
1187 EXPECT_FLOAT_EQ(0, delegate->scroll_y());
1188 EXPECT_FLOAT_EQ(20, delegate->scroll_x());
1190 // Get a high x velocity, while still staying on the rail
1191 const int kScrollAmount = 8;
1192 tes.SendScrollEvents(event_processor(),
1195 100,
1197 kTouchId,
1199 kScrollAmount,
1200 delegate.get());
1202 delegate->Reset();
1203 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1204 kTouchId, tes.Now());
1205 DispatchEventUsingWindowDispatcher(&release);
1207 EXPECT_TRUE(delegate->fling());
1208 EXPECT_FALSE(delegate->scroll_end());
1209 EXPECT_GT(delegate->velocity_x(), 0);
1210 EXPECT_EQ(0, delegate->velocity_y());
1213 // Check Scroll End Events report correct velocities
1214 // if the user was on a vertical rail
1215 TEST_F(GestureRecognizerTest, GestureEventVerticalRailFling) {
1216 scoped_ptr<GestureEventConsumeDelegate> delegate(
1217 new GestureEventConsumeDelegate());
1218 TimedEvents tes;
1219 const int kTouchId = 7;
1220 gfx::Rect bounds(0, 0, 1000, 1000);
1221 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1222 delegate.get(), -1234, bounds, root_window()));
1224 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1225 kTouchId, tes.Now());
1226 DispatchEventUsingWindowDispatcher(&press);
1228 // Get rid of touch slop.
1229 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 10),
1230 kTouchId, tes.Now());
1231 DispatchEventUsingWindowDispatcher(&move);
1232 delegate->Reset();
1234 // Move the touch-point vertically enough that it is considered a
1235 // vertical scroll.
1236 tes.SendScrollEvent(event_processor(), 1, 30, kTouchId, delegate.get());
1237 EXPECT_EQ(20, delegate->scroll_y());
1238 EXPECT_EQ(0, delegate->scroll_x());
1239 EXPECT_EQ(0, delegate->scroll_velocity_x());
1241 // Get a high y velocity, while still staying on the rail
1242 const int kScrollAmount = 8;
1243 tes.SendScrollEvents(event_processor(),
1247 100,
1248 kTouchId,
1250 kScrollAmount,
1251 delegate.get());
1252 EXPECT_EQ(0, delegate->scroll_velocity_x());
1254 delegate->Reset();
1255 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 206),
1256 kTouchId, tes.Now());
1257 DispatchEventUsingWindowDispatcher(&release);
1259 EXPECT_TRUE(delegate->fling());
1260 EXPECT_FALSE(delegate->scroll_end());
1261 EXPECT_EQ(0, delegate->velocity_x());
1262 EXPECT_GT(delegate->velocity_y(), 0);
1265 // Check Scroll End Events report non-zero velocities if the user is not on a
1266 // rail
1267 TEST_F(GestureRecognizerTest, GestureEventNonRailFling) {
1268 ui::GestureConfiguration::GetInstance()
1269 ->set_max_touch_move_in_pixels_for_click(0);
1270 scoped_ptr<GestureEventConsumeDelegate> delegate(
1271 new GestureEventConsumeDelegate());
1272 TimedEvents tes;
1273 const int kTouchId = 7;
1274 gfx::Rect bounds(0, 0, 1000, 1000);
1275 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1276 delegate.get(), -1234, bounds, root_window()));
1278 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1279 kTouchId, tes.Now());
1280 DispatchEventUsingWindowDispatcher(&press);
1282 // Move the touch-point such that a non-rail scroll begins, and we're outside
1283 // the snap channel for the unified GR.
1284 tes.SendScrollEvent(event_processor(), 50, 50, kTouchId, delegate.get());
1285 EXPECT_EQ(50, delegate->scroll_y());
1286 EXPECT_EQ(50, delegate->scroll_x());
1288 const int kScrollAmount = 8;
1289 tes.SendScrollEvents(event_processor(),
1293 100,
1294 kTouchId,
1296 kScrollAmount,
1297 delegate.get());
1299 delegate->Reset();
1300 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1301 kTouchId, tes.Now());
1302 DispatchEventUsingWindowDispatcher(&release);
1304 EXPECT_TRUE(delegate->fling());
1305 EXPECT_FALSE(delegate->scroll_end());
1306 EXPECT_GT(delegate->velocity_x(), 0);
1307 EXPECT_GT(delegate->velocity_y(), 0);
1310 // Check that appropriate touch events generate long press events
1311 TEST_F(GestureRecognizerTest, GestureEventLongPress) {
1312 scoped_ptr<GestureEventConsumeDelegate> delegate(
1313 new GestureEventConsumeDelegate());
1314 const int kWindowWidth = 123;
1315 const int kWindowHeight = 45;
1316 const int kTouchId = 2;
1317 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1318 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1319 delegate.get(), -1234, bounds, root_window()));
1321 delegate->Reset();
1323 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1324 gfx::Point(101, 201),
1325 kTouchId,
1326 ui::EventTimeForNow());
1327 DispatchEventUsingWindowDispatcher(&press1);
1328 EXPECT_TRUE(delegate->tap_down());
1329 EXPECT_TRUE(delegate->begin());
1330 EXPECT_FALSE(delegate->tap_cancel());
1332 // We haven't pressed long enough for a long press to occur
1333 EXPECT_FALSE(delegate->long_press());
1335 // Wait until the timer runs out
1336 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1337 EXPECT_TRUE(delegate->long_press());
1338 EXPECT_FALSE(delegate->tap_cancel());
1340 delegate->Reset();
1341 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1342 gfx::Point(101, 201),
1343 kTouchId,
1344 ui::EventTimeForNow());
1345 DispatchEventUsingWindowDispatcher(&release1);
1346 EXPECT_FALSE(delegate->long_press());
1348 // Note the tap cancel isn't dispatched until the release
1349 EXPECT_TRUE(delegate->tap_cancel());
1350 EXPECT_FALSE(delegate->tap());
1353 // Check that scrolling prevents a long press.
1354 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledByScroll) {
1355 scoped_ptr<GestureEventConsumeDelegate> delegate(
1356 new GestureEventConsumeDelegate());
1357 TimedEvents tes;
1358 const int kWindowWidth = 123;
1359 const int kWindowHeight = 45;
1360 const int kTouchId = 6;
1361 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1362 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1363 delegate.get(), -1234, bounds, root_window()));
1365 delegate->Reset();
1367 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1368 kTouchId, tes.Now());
1369 DispatchEventUsingWindowDispatcher(&press1);
1370 EXPECT_TRUE(delegate->tap_down());
1372 // We haven't pressed long enough for a long press to occur
1373 EXPECT_FALSE(delegate->long_press());
1374 EXPECT_FALSE(delegate->tap_cancel());
1376 // Scroll around, to cancel the long press
1377 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1379 // Wait until a long press event would have fired, if it hadn't been
1380 // cancelled.
1381 DelayByLongPressTimeout();
1383 EXPECT_FALSE(delegate->long_press());
1384 EXPECT_TRUE(delegate->tap_cancel());
1386 delegate->Reset();
1387 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1388 kTouchId, tes.LeapForward(10));
1389 DispatchEventUsingWindowDispatcher(&release1);
1390 EXPECT_FALSE(delegate->long_press());
1391 EXPECT_FALSE(delegate->tap_cancel());
1394 // Check that appropriate touch events generate long tap events
1395 TEST_F(GestureRecognizerTest, GestureEventLongTap) {
1396 ui::GestureConfiguration::GetInstance()
1397 ->set_max_touch_down_duration_for_click_in_ms(3);
1398 scoped_ptr<GestureEventConsumeDelegate> delegate(
1399 new GestureEventConsumeDelegate());
1400 const int kWindowWidth = 123;
1401 const int kWindowHeight = 45;
1402 const int kTouchId = 2;
1403 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1404 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1405 delegate.get(), -1234, bounds, root_window()));
1407 delegate->Reset();
1409 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1410 gfx::Point(101, 201),
1411 kTouchId,
1412 ui::EventTimeForNow());
1413 DispatchEventUsingWindowDispatcher(&press1);
1414 EXPECT_TRUE(delegate->tap_down());
1415 EXPECT_TRUE(delegate->begin());
1416 EXPECT_FALSE(delegate->tap_cancel());
1418 // We haven't pressed long enough for a long press to occur
1419 EXPECT_FALSE(delegate->long_press());
1421 // Wait until the timer runs out
1422 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1423 EXPECT_TRUE(delegate->long_press());
1424 EXPECT_FALSE(delegate->tap_cancel());
1426 delegate->Reset();
1427 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1428 gfx::Point(101, 201),
1429 kTouchId,
1430 ui::EventTimeForNow());
1431 DispatchEventUsingWindowDispatcher(&release1);
1432 EXPECT_FALSE(delegate->long_press());
1433 EXPECT_TRUE(delegate->long_tap());
1435 // Note the tap cancel isn't dispatched until the release
1436 EXPECT_TRUE(delegate->tap_cancel());
1437 EXPECT_FALSE(delegate->tap());
1440 // Check that second tap cancels a long press
1441 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledBySecondTap) {
1442 scoped_ptr<GestureEventConsumeDelegate> delegate(
1443 new GestureEventConsumeDelegate());
1444 TimedEvents tes;
1445 const int kWindowWidth = 300;
1446 const int kWindowHeight = 400;
1447 const int kTouchId1 = 8;
1448 const int kTouchId2 = 2;
1449 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1450 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1451 delegate.get(), -1234, bounds, root_window()));
1453 delegate->Reset();
1454 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1455 kTouchId1, tes.Now());
1456 DispatchEventUsingWindowDispatcher(&press);
1457 EXPECT_TRUE(delegate->tap_down());
1458 EXPECT_TRUE(delegate->begin());
1460 // We haven't pressed long enough for a long press to occur
1461 EXPECT_FALSE(delegate->long_press());
1463 // Second tap, to cancel the long press
1464 delegate->Reset();
1465 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1466 kTouchId2, tes.Now());
1467 DispatchEventUsingWindowDispatcher(&press2);
1468 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
1469 EXPECT_TRUE(delegate->tap_cancel());
1470 EXPECT_TRUE(delegate->begin());
1472 // Wait until the timer runs out
1473 DelayByLongPressTimeout();
1475 // No long press occurred
1476 EXPECT_FALSE(delegate->long_press());
1478 delegate->Reset();
1479 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1480 kTouchId1, tes.Now());
1481 DispatchEventUsingWindowDispatcher(&release1);
1482 EXPECT_FALSE(delegate->long_press());
1483 EXPECT_TRUE(delegate->two_finger_tap());
1484 EXPECT_FALSE(delegate->tap_cancel());
1487 // Check that horizontal scroll gestures cause scrolls on horizontal rails.
1488 // Also tests that horizontal rails can be broken.
1489 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailScroll) {
1490 scoped_ptr<GestureEventConsumeDelegate> delegate(
1491 new GestureEventConsumeDelegate());
1492 TimedEvents tes;
1493 const int kTouchId = 7;
1494 gfx::Rect bounds(0, 0, 1000, 1000);
1495 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1496 delegate.get(), -1234, bounds, root_window()));
1498 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1499 kTouchId, tes.Now());
1500 DispatchEventUsingWindowDispatcher(&press);
1502 // Get rid of touch slop.
1503 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(5, 0),
1504 kTouchId, tes.Now());
1506 DispatchEventUsingWindowDispatcher(&move);
1507 delegate->Reset();
1509 // Move the touch-point horizontally enough that it is considered a
1510 // horizontal scroll.
1511 tes.SendScrollEvent(event_processor(), 25, 0, kTouchId, delegate.get());
1512 EXPECT_EQ(0, delegate->scroll_y());
1513 EXPECT_EQ(20, delegate->scroll_x());
1515 tes.SendScrollEvent(event_processor(), 30, 6, kTouchId, delegate.get());
1516 EXPECT_TRUE(delegate->scroll_update());
1517 EXPECT_EQ(5, delegate->scroll_x());
1518 // y shouldn't change, as we're on a horizontal rail.
1519 EXPECT_EQ(0, delegate->scroll_y());
1521 // Send enough information that a velocity can be calculated for the gesture,
1522 // and we can break the rail
1523 const int kScrollAmount = 8;
1524 tes.SendScrollEvents(event_processor(),
1528 100,
1529 kTouchId,
1531 kScrollAmount,
1532 delegate.get());
1534 tes.SendScrollEvent(event_processor(), 5, 0, kTouchId, delegate.get());
1535 tes.SendScrollEvent(event_processor(), 10, 5, kTouchId, delegate.get());
1537 // The rail should be broken
1538 EXPECT_TRUE(delegate->scroll_update());
1539 EXPECT_EQ(5, delegate->scroll_x());
1540 EXPECT_EQ(5, delegate->scroll_y());
1543 // Check that vertical scroll gestures cause scrolls on vertical rails.
1544 // Also tests that vertical rails can be broken.
1545 TEST_F(GestureRecognizerTest, GestureEventVerticalRailScroll) {
1546 scoped_ptr<GestureEventConsumeDelegate> delegate(
1547 new GestureEventConsumeDelegate());
1548 TimedEvents tes;
1549 const int kTouchId = 7;
1550 gfx::Rect bounds(0, 0, 1000, 1000);
1551 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1552 delegate.get(), -1234, bounds, root_window()));
1554 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1555 kTouchId, tes.Now());
1556 DispatchEventUsingWindowDispatcher(&press);
1558 // Get rid of touch slop.
1559 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 5),
1560 kTouchId, tes.Now());
1561 DispatchEventUsingWindowDispatcher(&move);
1562 delegate->Reset();
1564 // Move the touch-point vertically enough that it is considered a
1565 // vertical scroll.
1566 tes.SendScrollEvent(event_processor(), 0, 25, kTouchId, delegate.get());
1567 EXPECT_EQ(0, delegate->scroll_x());
1568 EXPECT_EQ(20, delegate->scroll_y());
1570 tes.SendScrollEvent(event_processor(), 6, 30, kTouchId, delegate.get());
1571 EXPECT_TRUE(delegate->scroll_update());
1572 EXPECT_EQ(5, delegate->scroll_y());
1573 // x shouldn't change, as we're on a vertical rail.
1574 EXPECT_EQ(0, delegate->scroll_x());
1575 EXPECT_EQ(0, delegate->scroll_velocity_x());
1577 // Send enough information that a velocity can be calculated for the gesture,
1578 // and we can break the rail
1579 const int kScrollAmount = 8;
1580 tes.SendScrollEvents(event_processor(),
1583 100,
1585 kTouchId,
1587 kScrollAmount,
1588 delegate.get());
1590 tes.SendScrollEvent(event_processor(), 0, 5, kTouchId, delegate.get());
1591 tes.SendScrollEvent(event_processor(), 5, 10, kTouchId, delegate.get());
1593 // The rail should be broken
1594 EXPECT_TRUE(delegate->scroll_update());
1595 EXPECT_EQ(5, delegate->scroll_x());
1596 EXPECT_EQ(5, delegate->scroll_y());
1599 TEST_F(GestureRecognizerTest, GestureTapFollowedByScroll) {
1600 // We'll start by moving the touch point by (5, 5). We want all of that
1601 // distance to be consumed by the slop, so we set the slop radius to
1602 // sqrt(5 * 5 + 5 * 5).
1603 ui::GestureConfiguration::GetInstance()
1604 ->set_max_touch_move_in_pixels_for_click(sqrt(5.f * 5 + 5 * 5));
1606 // First, tap. Then, do a scroll using the same touch-id.
1607 scoped_ptr<GestureEventConsumeDelegate> delegate(
1608 new GestureEventConsumeDelegate());
1609 TimedEvents tes;
1610 const int kWindowWidth = 123;
1611 const int kWindowHeight = 45;
1612 const int kTouchId = 3;
1613 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1614 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1615 delegate.get(), -1234, bounds, root_window()));
1617 delegate->Reset();
1618 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1619 kTouchId, tes.Now());
1620 DispatchEventUsingWindowDispatcher(&press);
1621 EXPECT_FALSE(delegate->tap());
1622 EXPECT_TRUE(delegate->tap_down());
1623 EXPECT_FALSE(delegate->tap_cancel());
1624 EXPECT_FALSE(delegate->scroll_begin());
1625 EXPECT_FALSE(delegate->scroll_update());
1626 EXPECT_FALSE(delegate->scroll_end());
1628 // Make sure there is enough delay before the touch is released so that it is
1629 // recognized as a tap.
1630 delegate->Reset();
1631 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1632 kTouchId, tes.LeapForward(50));
1633 DispatchEventUsingWindowDispatcher(&release);
1634 EXPECT_TRUE(delegate->tap());
1635 EXPECT_FALSE(delegate->tap_down());
1636 EXPECT_FALSE(delegate->tap_cancel());
1637 EXPECT_FALSE(delegate->scroll_begin());
1638 EXPECT_FALSE(delegate->scroll_update());
1639 EXPECT_FALSE(delegate->scroll_end());
1641 // Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger
1642 // a double-tap.
1643 delegate->Reset();
1644 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1645 kTouchId, tes.LeapForward(1000));
1646 DispatchEventUsingWindowDispatcher(&press1);
1647 EXPECT_FALSE(delegate->tap());
1648 EXPECT_TRUE(delegate->tap_down());
1649 EXPECT_FALSE(delegate->tap_cancel());
1650 EXPECT_FALSE(delegate->scroll_begin());
1651 EXPECT_FALSE(delegate->scroll_update());
1652 EXPECT_FALSE(delegate->scroll_end());
1654 // Get rid of touch slop.
1655 ui::TouchEvent move_remove_slop(ui::ET_TOUCH_MOVED, gfx::Point(116, 216),
1656 kTouchId, tes.Now());
1657 DispatchEventUsingWindowDispatcher(&move_remove_slop);
1658 EXPECT_TRUE(delegate->tap_cancel());
1659 EXPECT_TRUE(delegate->scroll_begin());
1660 EXPECT_TRUE(delegate->scroll_update());
1661 EXPECT_EQ(15, delegate->scroll_x_hint());
1662 EXPECT_EQ(15, delegate->scroll_y_hint());
1664 delegate->Reset();
1666 // Move the touch-point enough so that it is considered as a scroll. This
1667 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1668 // The first movement is diagonal, to ensure that we have a free scroll,
1669 // and not a rail scroll.
1670 delegate->Reset();
1671 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(135, 235),
1672 kTouchId, tes.Now());
1673 DispatchEventUsingWindowDispatcher(&move);
1674 EXPECT_FALSE(delegate->tap());
1675 EXPECT_FALSE(delegate->tap_down());
1676 EXPECT_FALSE(delegate->tap_cancel());
1677 EXPECT_FALSE(delegate->scroll_begin());
1678 EXPECT_TRUE(delegate->scroll_update());
1679 EXPECT_FALSE(delegate->scroll_end());
1680 EXPECT_EQ(19, delegate->scroll_x());
1681 EXPECT_EQ(19, delegate->scroll_y());
1683 // Move some more to generate a few more scroll updates.
1684 delegate->Reset();
1685 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(115, 216),
1686 kTouchId, tes.Now());
1687 DispatchEventUsingWindowDispatcher(&move1);
1688 EXPECT_FALSE(delegate->tap());
1689 EXPECT_FALSE(delegate->tap_down());
1690 EXPECT_FALSE(delegate->tap_cancel());
1691 EXPECT_FALSE(delegate->scroll_begin());
1692 EXPECT_TRUE(delegate->scroll_update());
1693 EXPECT_FALSE(delegate->scroll_end());
1694 EXPECT_EQ(-20, delegate->scroll_x());
1695 EXPECT_EQ(-19, delegate->scroll_y());
1696 EXPECT_EQ(0, delegate->scroll_x_hint());
1697 EXPECT_EQ(0, delegate->scroll_y_hint());
1699 delegate->Reset();
1700 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 220),
1701 kTouchId, tes.Now());
1702 DispatchEventUsingWindowDispatcher(&move2);
1703 EXPECT_FALSE(delegate->tap());
1704 EXPECT_FALSE(delegate->tap_down());
1705 EXPECT_FALSE(delegate->tap_cancel());
1706 EXPECT_FALSE(delegate->scroll_begin());
1707 EXPECT_TRUE(delegate->scroll_update());
1708 EXPECT_FALSE(delegate->scroll_end());
1709 EXPECT_EQ(30, delegate->scroll_x());
1710 EXPECT_EQ(4, delegate->scroll_y());
1712 // Release the touch. This should end the scroll.
1713 delegate->Reset();
1714 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1715 kTouchId, tes.Now());
1716 DispatchEventUsingWindowDispatcher(&release1);
1717 EXPECT_FALSE(delegate->tap());
1718 EXPECT_FALSE(delegate->tap_down());
1719 EXPECT_FALSE(delegate->tap_cancel());
1720 EXPECT_FALSE(delegate->scroll_begin());
1721 EXPECT_FALSE(delegate->scroll_update());
1722 EXPECT_FALSE(delegate->scroll_end());
1723 EXPECT_TRUE(delegate->fling());
1726 TEST_F(GestureRecognizerTest, AsynchronousGestureRecognition) {
1727 scoped_ptr<QueueTouchEventDelegate> queued_delegate(
1728 new QueueTouchEventDelegate(host()->dispatcher()));
1729 TimedEvents tes;
1730 const int kWindowWidth = 123;
1731 const int kWindowHeight = 45;
1732 const int kTouchId1 = 6;
1733 const int kTouchId2 = 4;
1734 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1735 scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate(
1736 queued_delegate.get(), -1234, bounds, root_window()));
1738 queued_delegate->set_window(queue.get());
1740 // Touch down on the window. This should not generate any gesture event.
1741 queued_delegate->Reset();
1742 ui::TouchEvent press(
1743 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
1744 DispatchEventUsingWindowDispatcher(&press);
1745 EXPECT_FALSE(queued_delegate->tap());
1746 EXPECT_FALSE(queued_delegate->tap_down());
1747 EXPECT_FALSE(queued_delegate->tap_cancel());
1748 EXPECT_FALSE(queued_delegate->begin());
1749 EXPECT_FALSE(queued_delegate->scroll_begin());
1750 EXPECT_FALSE(queued_delegate->scroll_update());
1751 EXPECT_FALSE(queued_delegate->scroll_end());
1753 // Introduce some delay before the touch is released so that it is recognized
1754 // as a tap. However, this still should not create any gesture events.
1755 queued_delegate->Reset();
1756 ui::TouchEvent release(
1757 ui::ET_TOUCH_RELEASED,
1758 gfx::Point(101, 201),
1759 kTouchId1,
1760 press.time_stamp() + base::TimeDelta::FromMilliseconds(50));
1761 DispatchEventUsingWindowDispatcher(&release);
1762 EXPECT_FALSE(queued_delegate->tap());
1763 EXPECT_FALSE(queued_delegate->tap_down());
1764 EXPECT_FALSE(queued_delegate->tap_cancel());
1765 EXPECT_FALSE(queued_delegate->begin());
1766 EXPECT_FALSE(queued_delegate->end());
1767 EXPECT_FALSE(queued_delegate->scroll_begin());
1768 EXPECT_FALSE(queued_delegate->scroll_update());
1769 EXPECT_FALSE(queued_delegate->scroll_end());
1771 // Create another window, and place a touch-down on it. This should create a
1772 // tap-down gesture.
1773 scoped_ptr<GestureEventConsumeDelegate> delegate(
1774 new GestureEventConsumeDelegate());
1775 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1776 delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), root_window()));
1777 delegate->Reset();
1778 ui::TouchEvent press2(
1779 ui::ET_TOUCH_PRESSED, gfx::Point(10, 20), kTouchId2, tes.Now());
1780 DispatchEventUsingWindowDispatcher(&press2);
1781 EXPECT_FALSE(delegate->tap());
1782 EXPECT_TRUE(delegate->tap_down());
1783 EXPECT_FALSE(delegate->tap_cancel());
1784 EXPECT_FALSE(queued_delegate->begin());
1785 EXPECT_FALSE(queued_delegate->end());
1786 EXPECT_FALSE(delegate->scroll_begin());
1787 EXPECT_FALSE(delegate->scroll_update());
1788 EXPECT_FALSE(delegate->scroll_end());
1790 ui::TouchEvent release2(
1791 ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), kTouchId2, tes.Now());
1792 DispatchEventUsingWindowDispatcher(&release2);
1794 // Process the first queued event.
1795 queued_delegate->Reset();
1796 queued_delegate->ReceivedAck();
1797 EXPECT_FALSE(queued_delegate->tap());
1798 EXPECT_TRUE(queued_delegate->tap_down());
1799 EXPECT_TRUE(queued_delegate->begin());
1800 EXPECT_FALSE(queued_delegate->tap_cancel());
1801 EXPECT_FALSE(queued_delegate->end());
1802 EXPECT_FALSE(queued_delegate->scroll_begin());
1803 EXPECT_FALSE(queued_delegate->scroll_update());
1804 EXPECT_FALSE(queued_delegate->scroll_end());
1806 // Now, process the second queued event.
1807 queued_delegate->Reset();
1808 queued_delegate->ReceivedAck();
1809 EXPECT_TRUE(queued_delegate->tap());
1810 EXPECT_FALSE(queued_delegate->tap_down());
1811 EXPECT_FALSE(queued_delegate->tap_cancel());
1812 EXPECT_FALSE(queued_delegate->begin());
1813 EXPECT_TRUE(queued_delegate->end());
1814 EXPECT_FALSE(queued_delegate->scroll_begin());
1815 EXPECT_FALSE(queued_delegate->scroll_update());
1816 EXPECT_FALSE(queued_delegate->scroll_end());
1818 // Start all over. Press on the first window, then press again on the second
1819 // window. The second press should still go to the first window.
1820 queued_delegate->Reset();
1821 ui::TouchEvent press3(
1822 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
1823 DispatchEventUsingWindowDispatcher(&press3);
1824 EXPECT_FALSE(queued_delegate->tap());
1825 EXPECT_FALSE(queued_delegate->tap_down());
1826 EXPECT_FALSE(queued_delegate->tap_cancel());
1827 EXPECT_FALSE(queued_delegate->begin());
1828 EXPECT_FALSE(queued_delegate->end());
1829 EXPECT_FALSE(queued_delegate->begin());
1830 EXPECT_FALSE(queued_delegate->end());
1831 EXPECT_FALSE(queued_delegate->scroll_begin());
1832 EXPECT_FALSE(queued_delegate->scroll_update());
1833 EXPECT_FALSE(queued_delegate->scroll_end());
1835 queued_delegate->Reset();
1836 delegate->Reset();
1837 ui::TouchEvent press4(
1838 ui::ET_TOUCH_PRESSED, gfx::Point(103, 203), kTouchId2, tes.Now());
1839 DispatchEventUsingWindowDispatcher(&press4);
1840 EXPECT_FALSE(delegate->tap());
1841 EXPECT_FALSE(delegate->tap_down());
1842 EXPECT_FALSE(delegate->tap_cancel());
1843 EXPECT_FALSE(delegate->begin());
1844 EXPECT_FALSE(delegate->end());
1845 EXPECT_FALSE(delegate->scroll_begin());
1846 EXPECT_FALSE(delegate->scroll_update());
1847 EXPECT_FALSE(delegate->scroll_end());
1848 EXPECT_FALSE(queued_delegate->tap());
1849 EXPECT_FALSE(queued_delegate->tap_down());
1850 EXPECT_FALSE(queued_delegate->tap_cancel());
1851 EXPECT_FALSE(queued_delegate->begin());
1852 EXPECT_FALSE(queued_delegate->end());
1853 EXPECT_FALSE(queued_delegate->scroll_begin());
1854 EXPECT_FALSE(queued_delegate->scroll_update());
1855 EXPECT_FALSE(queued_delegate->scroll_end());
1857 // Move the second touch-point enough so that it is considered a pinch. This
1858 // should generate both SCROLL_BEGIN and PINCH_BEGIN gestures.
1859 queued_delegate->Reset();
1860 delegate->Reset();
1861 ui::TouchEvent move(
1862 ui::ET_TOUCH_MOVED,
1863 gfx::PointF(203 +
1864 ui::GestureConfiguration::GetInstance()
1865 ->max_touch_move_in_pixels_for_click(),
1866 303),
1867 kTouchId2,
1868 tes.Now());
1869 DispatchEventUsingWindowDispatcher(&move);
1870 EXPECT_FALSE(delegate->tap());
1871 EXPECT_FALSE(delegate->tap_down());
1872 EXPECT_FALSE(delegate->tap_cancel());
1873 EXPECT_FALSE(delegate->begin());
1874 EXPECT_FALSE(delegate->scroll_begin());
1875 EXPECT_FALSE(delegate->scroll_update());
1876 EXPECT_FALSE(delegate->scroll_end());
1877 EXPECT_FALSE(queued_delegate->tap());
1878 EXPECT_FALSE(queued_delegate->tap_down());
1879 EXPECT_FALSE(queued_delegate->tap_cancel());
1880 EXPECT_FALSE(queued_delegate->begin());
1881 EXPECT_FALSE(queued_delegate->scroll_begin());
1882 EXPECT_FALSE(queued_delegate->scroll_update());
1883 EXPECT_FALSE(queued_delegate->scroll_end());
1885 queued_delegate->Reset();
1886 queued_delegate->ReceivedAck();
1887 EXPECT_FALSE(queued_delegate->tap());
1888 EXPECT_TRUE(queued_delegate->tap_down());
1889 EXPECT_TRUE(queued_delegate->begin());
1890 EXPECT_FALSE(queued_delegate->tap_cancel());
1891 EXPECT_FALSE(queued_delegate->end());
1892 EXPECT_FALSE(queued_delegate->scroll_begin());
1893 EXPECT_FALSE(queued_delegate->scroll_update());
1894 EXPECT_FALSE(queued_delegate->scroll_end());
1896 queued_delegate->Reset();
1897 queued_delegate->ReceivedAck();
1898 EXPECT_FALSE(queued_delegate->tap());
1899 EXPECT_FALSE(queued_delegate->tap_down()); // no touch down for second tap.
1900 EXPECT_TRUE(queued_delegate->tap_cancel());
1901 EXPECT_TRUE(queued_delegate->begin());
1902 EXPECT_FALSE(queued_delegate->end());
1903 EXPECT_FALSE(queued_delegate->scroll_begin());
1904 EXPECT_FALSE(queued_delegate->scroll_update());
1905 EXPECT_FALSE(queued_delegate->scroll_end());
1906 EXPECT_FALSE(queued_delegate->pinch_begin());
1907 EXPECT_FALSE(queued_delegate->pinch_update());
1908 EXPECT_FALSE(queued_delegate->pinch_end());
1910 queued_delegate->Reset();
1911 queued_delegate->ReceivedAck();
1912 EXPECT_FALSE(queued_delegate->tap());
1913 EXPECT_FALSE(queued_delegate->tap_down());
1914 EXPECT_FALSE(queued_delegate->tap_cancel());
1915 EXPECT_FALSE(queued_delegate->begin());
1916 EXPECT_FALSE(queued_delegate->end());
1917 EXPECT_TRUE(queued_delegate->scroll_begin());
1919 EXPECT_TRUE(queued_delegate->scroll_update());
1920 EXPECT_FALSE(queued_delegate->scroll_end());
1921 EXPECT_TRUE(queued_delegate->pinch_begin());
1922 EXPECT_FALSE(queued_delegate->pinch_update());
1923 EXPECT_FALSE(queued_delegate->pinch_end());
1926 // Check that appropriate touch events generate pinch gesture events.
1927 TEST_F(GestureRecognizerTest, GestureEventPinchFromScroll) {
1928 scoped_ptr<GestureEventConsumeDelegate> delegate(
1929 new GestureEventConsumeDelegate());
1930 TimedEvents tes;
1931 const int kWindowWidth = 300;
1932 const int kWindowHeight = 400;
1933 const int kTouchId1 = 5;
1934 const int kTouchId2 = 3;
1935 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1936 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1937 delegate.get(), -1234, bounds, root_window()));
1939 delegate->Reset();
1940 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1941 kTouchId1, tes.Now());
1942 DispatchEventUsingWindowDispatcher(&press);
1943 EXPECT_2_EVENTS(delegate->events(),
1944 ui::ET_GESTURE_BEGIN,
1945 ui::ET_GESTURE_TAP_DOWN);
1947 // Move the touch-point enough so that it is considered as a scroll. This
1948 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1949 delegate->Reset();
1950 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 301),
1951 kTouchId1, tes.Now());
1952 DispatchEventUsingWindowDispatcher(&move);
1953 EXPECT_3_EVENTS(delegate->events(),
1954 ui::ET_GESTURE_TAP_CANCEL,
1955 ui::ET_GESTURE_SCROLL_BEGIN,
1956 ui::ET_GESTURE_SCROLL_UPDATE);
1958 // Press the second finger. It should cause pinch-begin. Note that we will not
1959 // transition to two finger tap here because the touch points are far enough.
1960 delegate->Reset();
1961 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1962 kTouchId2, tes.Now());
1963 DispatchEventUsingWindowDispatcher(&press2);
1964 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_BEGIN);
1965 EXPECT_EQ(gfx::Rect(10, 10, 120, 291).ToString(),
1966 delegate->bounding_box().ToString());
1968 // Move the first finger.
1969 delegate->Reset();
1970 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(95, 201),
1971 kTouchId1, tes.Now());
1972 DispatchEventUsingWindowDispatcher(&move3);
1973 EXPECT_2_EVENTS(delegate->events(),
1974 ui::ET_GESTURE_SCROLL_UPDATE,
1975 ui::ET_GESTURE_PINCH_BEGIN);
1976 EXPECT_EQ(gfx::Rect(10, 10, 85, 191).ToString(),
1977 delegate->bounding_box().ToString());
1979 // Now move the second finger.
1980 delegate->Reset();
1981 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
1982 kTouchId2, tes.Now());
1983 DispatchEventUsingWindowDispatcher(&move4);
1984 EXPECT_2_EVENTS(delegate->events(),
1985 ui::ET_GESTURE_SCROLL_UPDATE,
1986 ui::ET_GESTURE_PINCH_UPDATE);
1987 EXPECT_EQ(gfx::Rect(55, 15, 40, 186).ToString(),
1988 delegate->bounding_box().ToString());
1990 // Release the first finger. This should end pinch.
1991 delegate->Reset();
1992 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1993 kTouchId1, tes.Now());
1994 DispatchEventUsingWindowDispatcher(&release);
1995 EXPECT_2_EVENTS(delegate->events(),
1996 ui::ET_GESTURE_PINCH_END,
1997 ui::ET_GESTURE_END);
1998 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
1999 delegate->bounding_box().ToString());
2001 // Move the second finger. This should still generate a scroll.
2002 delegate->Reset();
2003 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2004 kTouchId2, tes.Now());
2005 DispatchEventUsingWindowDispatcher(&move5);
2006 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2007 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2010 TEST_F(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) {
2011 scoped_ptr<GestureEventConsumeDelegate> delegate(
2012 new GestureEventConsumeDelegate());
2013 TimedEvents tes;
2014 const int kWindowWidth = 300;
2015 const int kWindowHeight = 400;
2016 const int kTouchId1 = 5;
2017 const int kTouchId2 = 3;
2018 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2019 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2020 delegate.get(), -1234, bounds, root_window()));
2022 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2023 kTouchId1, tes.Now());
2024 DispatchEventUsingWindowDispatcher(&press);
2025 delegate->Reset();
2026 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2027 kTouchId2, tes.Now());
2028 DispatchEventUsingWindowDispatcher(&press2);
2029 EXPECT_FALSE(delegate->pinch_begin());
2031 // Touch move triggers pinch begin.
2032 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2033 EXPECT_TRUE(delegate->pinch_begin());
2034 EXPECT_FALSE(delegate->pinch_update());
2036 // Touch move triggers pinch update.
2037 tes.SendScrollEvent(event_processor(), 160, 200, kTouchId1, delegate.get());
2038 EXPECT_FALSE(delegate->pinch_begin());
2039 EXPECT_TRUE(delegate->pinch_update());
2041 // Pinch has started, now release the second finger
2042 delegate->Reset();
2043 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2044 kTouchId1, tes.Now());
2045 DispatchEventUsingWindowDispatcher(&release);
2046 EXPECT_TRUE(delegate->pinch_end());
2048 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId2, delegate.get());
2049 EXPECT_TRUE(delegate->scroll_update());
2051 // Pinch again
2052 delegate->Reset();
2053 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2054 kTouchId1, tes.Now());
2055 DispatchEventUsingWindowDispatcher(&press3);
2056 // Now the touch points are close. So we will go into two finger tap.
2057 // Move the touch-point enough to break two-finger-tap and enter pinch.
2058 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 50),
2059 kTouchId1, tes.Now());
2060 DispatchEventUsingWindowDispatcher(&move2);
2061 EXPECT_TRUE(delegate->pinch_begin());
2063 tes.SendScrollEvent(event_processor(), 350, 350, kTouchId1, delegate.get());
2064 EXPECT_TRUE(delegate->pinch_update());
2067 TEST_F(GestureRecognizerTest, GestureEventPinchFromTap) {
2068 scoped_ptr<GestureEventConsumeDelegate> delegate(
2069 new GestureEventConsumeDelegate());
2070 TimedEvents tes;
2071 const int kWindowWidth = 300;
2072 const int kWindowHeight = 400;
2073 const int kTouchId1 = 3;
2074 const int kTouchId2 = 5;
2075 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2076 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2077 delegate.get(), -1234, bounds, root_window()));
2079 delegate->Reset();
2080 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2081 kTouchId1, tes.Now());
2082 DispatchEventUsingWindowDispatcher(&press);
2083 EXPECT_2_EVENTS(delegate->events(),
2084 ui::ET_GESTURE_BEGIN,
2085 ui::ET_GESTURE_TAP_DOWN);
2086 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2088 // Press the second finger far enough to break two finger tap.
2089 delegate->Reset();
2090 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2091 kTouchId2, tes.Now());
2092 DispatchEventUsingWindowDispatcher(&press2);
2093 EXPECT_2_EVENTS(delegate->events(),
2094 ui::ET_GESTURE_TAP_CANCEL,
2095 ui::ET_GESTURE_BEGIN);
2096 EXPECT_EQ(gfx::Rect(10, 10, 91, 291).ToString(),
2097 delegate->bounding_box().ToString());
2099 // Move the first finger.
2100 delegate->Reset();
2101 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
2102 kTouchId1, tes.Now());
2103 DispatchEventUsingWindowDispatcher(&move3);
2104 EXPECT_3_EVENTS(delegate->events(),
2105 ui::ET_GESTURE_SCROLL_BEGIN,
2106 ui::ET_GESTURE_SCROLL_UPDATE,
2107 ui::ET_GESTURE_PINCH_BEGIN);
2108 EXPECT_EQ(gfx::Rect(10, 10, 55, 191).ToString(),
2109 delegate->bounding_box().ToString());
2111 // Now move the second finger.
2112 delegate->Reset();
2113 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
2114 kTouchId2, tes.Now());
2115 DispatchEventUsingWindowDispatcher(&move4);
2116 EXPECT_2_EVENTS(delegate->events(),
2117 ui::ET_GESTURE_SCROLL_UPDATE,
2118 ui::ET_GESTURE_PINCH_UPDATE);
2119 EXPECT_EQ(gfx::Rect(55, 15, 10, 186).ToString(),
2120 delegate->bounding_box().ToString());
2122 // Release the first finger. This should end pinch.
2123 delegate->Reset();
2124 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2125 kTouchId1, tes.LeapForward(10));
2126 DispatchEventUsingWindowDispatcher(&release);
2127 EXPECT_2_EVENTS(delegate->events(),
2128 ui::ET_GESTURE_PINCH_END,
2129 ui::ET_GESTURE_END);
2130 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
2131 delegate->bounding_box().ToString());
2133 // Move the second finger. This should still generate a scroll.
2134 delegate->Reset();
2135 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2136 kTouchId2, tes.Now());
2137 DispatchEventUsingWindowDispatcher(&move5);
2138 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2139 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2142 TEST_F(GestureRecognizerTest, GestureEventIgnoresDisconnectedEvents) {
2143 scoped_ptr<GestureEventConsumeDelegate> delegate(
2144 new GestureEventConsumeDelegate());
2145 TimedEvents tes;
2147 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2148 6, tes.Now());
2149 DispatchEventUsingWindowDispatcher(&release1);
2150 EXPECT_FALSE(delegate->tap());
2151 EXPECT_FALSE(delegate->tap_down());
2154 // Check that a touch is locked to the window of the closest current touch
2155 // within max_separation_for_gesture_touches_in_pixels
2156 TEST_F(GestureRecognizerTest, GestureEventTouchLockSelectsCorrectWindow) {
2157 ui::GestureRecognizer* gesture_recognizer = new ui::GestureRecognizerImpl();
2158 TimedEvents tes;
2159 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2161 ui::GestureConsumer* target;
2162 const int kNumWindows = 4;
2164 scoped_ptr<GestureEventConsumeDelegate*[]> delegates(
2165 new GestureEventConsumeDelegate*[kNumWindows]);
2167 ui::GestureConfiguration::GetInstance()
2168 ->set_max_separation_for_gesture_touches_in_pixels(499);
2170 scoped_ptr<gfx::Rect[]> window_bounds(new gfx::Rect[kNumWindows]);
2171 window_bounds[0] = gfx::Rect(0, 0, 1, 1);
2172 window_bounds[1] = gfx::Rect(500, 0, 1, 1);
2173 window_bounds[2] = gfx::Rect(0, 500, 1, 1);
2174 window_bounds[3] = gfx::Rect(500, 500, 1, 1);
2176 scoped_ptr<aura::Window*[]> windows(new aura::Window*[kNumWindows]);
2178 // Instantiate windows with |window_bounds| and touch each window at
2179 // its origin.
2180 for (int i = 0; i < kNumWindows; ++i) {
2181 delegates[i] = new GestureEventConsumeDelegate();
2182 windows[i] = CreateTestWindowWithDelegate(
2183 delegates[i], i, window_bounds[i], root_window());
2184 windows[i]->set_id(i);
2185 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, window_bounds[i].origin(),
2186 i, tes.Now());
2187 DispatchEventUsingWindowDispatcher(&press);
2190 // Touches should now be associated with the closest touch within
2191 // ui::GestureConfiguration::max_separation_for_gesture_touches_in_pixels
2192 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 11), -1);
2193 EXPECT_EQ("0", WindowIDAsString(target));
2194 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 11), -1);
2195 EXPECT_EQ("1", WindowIDAsString(target));
2196 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 511), -1);
2197 EXPECT_EQ("2", WindowIDAsString(target));
2198 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 511), -1);
2199 EXPECT_EQ("3", WindowIDAsString(target));
2201 // Add a touch in the middle associated with windows[2]
2202 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 500),
2203 kNumWindows, tes.Now());
2204 DispatchEventUsingWindowDispatcher(&press);
2205 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(250, 250),
2206 kNumWindows, tes.Now());
2207 DispatchEventUsingWindowDispatcher(&move);
2209 target = gesture_recognizer->GetTargetForLocation(gfx::Point(250, 250), -1);
2210 EXPECT_EQ("2", WindowIDAsString(target));
2212 // Make sure that ties are broken by distance to a current touch
2213 // Closer to the point in the bottom right.
2214 target = gesture_recognizer->GetTargetForLocation(gfx::Point(380, 380), -1);
2215 EXPECT_EQ("3", WindowIDAsString(target));
2217 // This touch is closer to the point in the middle
2218 target = gesture_recognizer->GetTargetForLocation(gfx::Point(300, 300), -1);
2219 EXPECT_EQ("2", WindowIDAsString(target));
2221 // A touch too far from other touches won't be locked to anything
2222 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2223 EXPECT_TRUE(target == NULL);
2225 // Move a touch associated with windows[2] to 1000, 1000
2226 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(1000, 1000),
2227 kNumWindows, tes.Now());
2228 DispatchEventUsingWindowDispatcher(&move2);
2230 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2231 EXPECT_EQ("2", WindowIDAsString(target));
2233 for (int i = 0; i < kNumWindows; ++i) {
2234 // Delete windows before deleting delegates.
2235 delete windows[i];
2236 delete delegates[i];
2240 // Check that a touch's target will not be effected by a touch on a different
2241 // screen.
2242 TEST_F(GestureRecognizerTest, GestureEventTouchLockIgnoresOtherScreens) {
2243 scoped_ptr<GestureEventConsumeDelegate> delegate(
2244 new GestureEventConsumeDelegate());
2245 gfx::Rect bounds(0, 0, 10, 10);
2246 scoped_ptr<aura::Window> window(
2247 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2249 const int kTouchId1 = 8;
2250 const int kTouchId2 = 2;
2251 TimedEvents tes;
2253 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(5, 5),
2254 kTouchId1, tes.Now());
2255 ui::EventTestApi test_press1(&press1);
2256 test_press1.set_source_device_id(1);
2257 DispatchEventUsingWindowDispatcher(&press1);
2259 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2260 kTouchId2, tes.Now());
2261 ui::EventTestApi test_press2(&press2);
2262 test_press2.set_source_device_id(2);
2263 DispatchEventUsingWindowDispatcher(&press2);
2265 // The second press should not have been locked to the same target as the
2266 // first, as they occured on different displays.
2267 EXPECT_NE(
2268 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1),
2269 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2272 // Check that touch events outside the root window are still handled
2273 // by the root window's gesture sequence.
2274 TEST_F(GestureRecognizerTest, GestureEventOutsideRootWindowTap) {
2275 TimedEvents tes;
2276 scoped_ptr<aura::Window> window(CreateTestWindowWithBounds(
2277 gfx::Rect(-100, -100, 2000, 2000), root_window()));
2279 gfx::Point pos1(-10, -10);
2280 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, pos1, 0, tes.Now());
2281 DispatchEventUsingWindowDispatcher(&press1);
2283 gfx::Point pos2(1000, 1000);
2284 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, pos2, 1, tes.Now());
2285 DispatchEventUsingWindowDispatcher(&press2);
2287 // As these presses were outside the root window, they should be
2288 // associated with the root window.
2289 EXPECT_EQ(root_window(),
2290 static_cast<aura::Window*>(
2291 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1)));
2292 EXPECT_EQ(root_window(),
2293 static_cast<aura::Window*>(
2294 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2)));
2297 TEST_F(GestureRecognizerTest, NoTapWithPreventDefaultedRelease) {
2298 scoped_ptr<QueueTouchEventDelegate> delegate(
2299 new QueueTouchEventDelegate(host()->dispatcher()));
2300 TimedEvents tes;
2301 const int kTouchId = 2;
2302 gfx::Rect bounds(100, 200, 100, 100);
2303 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2304 delegate.get(), -1234, bounds, root_window()));
2305 delegate->set_window(window.get());
2307 delegate->Reset();
2308 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2309 kTouchId, tes.Now());
2310 DispatchEventUsingWindowDispatcher(&press);
2311 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2312 kTouchId, tes.LeapForward(50));
2313 DispatchEventUsingWindowDispatcher(&release);
2315 delegate->Reset();
2316 delegate->ReceivedAck();
2317 EXPECT_TRUE(delegate->tap_down());
2318 delegate->Reset();
2319 delegate->ReceivedAckPreventDefaulted();
2320 EXPECT_FALSE(delegate->tap());
2321 EXPECT_TRUE(delegate->tap_cancel());
2324 TEST_F(GestureRecognizerTest, PinchScrollWithPreventDefaultedRelease) {
2325 scoped_ptr<QueueTouchEventDelegate> delegate(
2326 new QueueTouchEventDelegate(host()->dispatcher()));
2327 TimedEvents tes;
2328 const int kTouchId1 = 7;
2329 const int kTouchId2 = 5;
2330 gfx::Rect bounds(10, 20, 100, 100);
2331 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2332 delegate.get(), -1234, bounds, root_window()));
2333 delegate->set_window(window.get());
2336 delegate->Reset();
2337 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2338 tes.Now());
2339 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2340 tes.LeapForward(200));
2341 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2342 tes.LeapForward(50));
2343 DispatchEventUsingWindowDispatcher(&press);
2344 DispatchEventUsingWindowDispatcher(&move);
2345 DispatchEventUsingWindowDispatcher(&release);
2346 delegate->Reset();
2348 // Ack the press event.
2349 delegate->ReceivedAck();
2350 EXPECT_2_EVENTS(
2351 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2352 delegate->Reset();
2354 // Ack the move event.
2355 delegate->ReceivedAck();
2356 EXPECT_3_EVENTS(delegate->events(),
2357 ui::ET_GESTURE_TAP_CANCEL,
2358 ui::ET_GESTURE_SCROLL_BEGIN,
2359 ui::ET_GESTURE_SCROLL_UPDATE);
2360 delegate->Reset();
2362 // Ack the release event. Although the release event has been processed, it
2363 // should still generate a scroll-end event.
2364 delegate->ReceivedAckPreventDefaulted();
2365 EXPECT_2_EVENTS(
2366 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2369 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2370 tes.Now());
2371 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2372 tes.LeapForward(200));
2373 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2374 tes.LeapForward(50));
2375 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 25), kTouchId2,
2376 tes.Now());
2377 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 85), kTouchId2,
2378 tes.LeapForward(1000));
2379 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(145, 85), kTouchId2,
2380 tes.LeapForward(14));
2382 // Do a pinch.
2383 DispatchEventUsingWindowDispatcher(&press);
2384 DispatchEventUsingWindowDispatcher(&move);
2385 DispatchEventUsingWindowDispatcher(&press2);
2386 DispatchEventUsingWindowDispatcher(&move2);
2387 DispatchEventUsingWindowDispatcher(&release);
2388 DispatchEventUsingWindowDispatcher(&release2);
2390 // Ack the press and move events.
2391 delegate->Reset();
2392 delegate->ReceivedAck();
2393 EXPECT_2_EVENTS(
2394 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2396 delegate->Reset();
2397 delegate->ReceivedAck();
2398 EXPECT_3_EVENTS(delegate->events(),
2399 ui::ET_GESTURE_TAP_CANCEL,
2400 ui::ET_GESTURE_SCROLL_BEGIN,
2401 ui::ET_GESTURE_SCROLL_UPDATE);
2403 delegate->Reset();
2404 delegate->ReceivedAck();
2405 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_BEGIN);
2407 delegate->Reset();
2408 delegate->ReceivedAck();
2409 EXPECT_2_EVENTS(delegate->events(),
2410 ui::ET_GESTURE_SCROLL_UPDATE,
2411 ui::ET_GESTURE_PINCH_BEGIN);
2413 // Ack the first release. Although the release is processed, it should still
2414 // generate a pinch-end event.
2415 delegate->Reset();
2416 delegate->ReceivedAckPreventDefaulted();
2417 EXPECT_2_EVENTS(
2418 delegate->events(), ui::ET_GESTURE_PINCH_END, ui::ET_GESTURE_END);
2420 delegate->Reset();
2421 delegate->ReceivedAckPreventDefaulted();
2422 EXPECT_2_EVENTS(
2423 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2426 TEST_F(GestureRecognizerTest, GestureEndLocation) {
2427 GestureEventConsumeDelegate delegate;
2428 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2429 &delegate, -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2430 ui::test::EventGenerator generator(root_window(), window.get());
2431 const gfx::Point begin(20, 20);
2432 const gfx::Point end(150, 150);
2433 const gfx::Vector2d window_offset =
2434 window->bounds().origin().OffsetFromOrigin();
2435 generator.GestureScrollSequence(begin, end,
2436 base::TimeDelta::FromMilliseconds(20),
2437 10);
2438 EXPECT_EQ((begin - window_offset).ToString(),
2439 delegate.scroll_begin_position().ToString());
2440 EXPECT_EQ((end - window_offset).ToString(),
2441 delegate.gesture_end_location().ToString());
2444 TEST_F(GestureRecognizerTest, CaptureSendsGestureEnd) {
2445 scoped_ptr<GestureEventConsumeDelegate> delegate(
2446 new GestureEventConsumeDelegate());
2447 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2448 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2449 ui::test::EventGenerator generator(root_window());
2451 generator.MoveMouseRelativeTo(window.get(), gfx::Point(10, 10));
2452 generator.PressTouch();
2453 RunAllPendingInMessageLoop();
2455 EXPECT_TRUE(delegate->tap_down());
2457 scoped_ptr<aura::Window> capture(CreateTestWindowWithBounds(
2458 gfx::Rect(10, 10, 200, 200), root_window()));
2459 capture->SetCapture();
2460 RunAllPendingInMessageLoop();
2462 EXPECT_TRUE(delegate->end());
2463 EXPECT_TRUE(delegate->tap_cancel());
2466 // Check that previous touch actions that are completely finished (either
2467 // released or cancelled), do not receive extra synthetic cancels upon change of
2468 // capture.
2469 TEST_F(GestureRecognizerTest, CaptureDoesNotCancelFinishedTouches) {
2470 scoped_ptr<GestureEventConsumeDelegate> delegate(
2471 new GestureEventConsumeDelegate());
2472 scoped_ptr<TestEventHandler> handler(new TestEventHandler);
2473 root_window()->AddPreTargetHandler(handler.get());
2475 // Create a window and set it as the capture window.
2476 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(delegate.get(),
2477 -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2478 window1->SetCapture();
2480 ui::test::EventGenerator generator(root_window());
2481 TimedEvents tes;
2483 // Generate two touch-press events on the window.
2484 scoped_ptr<ui::TouchEvent> touch0(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2485 gfx::Point(20, 20), 0,
2486 tes.Now()));
2487 scoped_ptr<ui::TouchEvent> touch1(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2488 gfx::Point(30, 30), 1,
2489 tes.Now()));
2490 generator.Dispatch(touch0.get());
2491 generator.Dispatch(touch1.get());
2492 RunAllPendingInMessageLoop();
2493 EXPECT_EQ(2, handler->touch_pressed_count());
2495 // Advance time.
2496 tes.LeapForward(1000);
2498 // End the two touches, one by a touch-release and one by a touch-cancel; to
2499 // cover both cases.
2500 touch0.reset(new ui::TouchEvent(ui::ET_TOUCH_RELEASED, gfx::Point(20, 20), 0,
2501 tes.Now()));
2502 touch1.reset(new ui::TouchEvent(ui::ET_TOUCH_CANCELLED, gfx::Point(30, 30), 1,
2503 tes.Now()));
2504 generator.Dispatch(touch0.get());
2505 generator.Dispatch(touch1.get());
2506 RunAllPendingInMessageLoop();
2507 EXPECT_EQ(1, handler->touch_released_count());
2508 EXPECT_EQ(1, handler->touch_cancelled_count());
2510 // Create a new window and set it as the new capture window.
2511 scoped_ptr<aura::Window> window2(CreateTestWindowWithBounds(
2512 gfx::Rect(100, 100, 300, 300), root_window()));
2513 window2->SetCapture();
2514 RunAllPendingInMessageLoop();
2515 // Check that setting capture does not generate any synthetic touch-cancels
2516 // for the two previously finished touch actions.
2517 EXPECT_EQ(1, handler->touch_cancelled_count());
2519 root_window()->RemovePreTargetHandler(handler.get());
2522 // Tests that a press with the same touch id as an existing touch is ignored.
2523 TEST_F(GestureRecognizerTest, PressDoesNotCrash) {
2524 scoped_ptr<GestureEventConsumeDelegate> delegate(
2525 new GestureEventConsumeDelegate());
2526 TimedEvents tes;
2528 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2529 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2531 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(45, 45), 7, tes.Now());
2532 press.set_radius_x(40);
2533 DispatchEventUsingWindowDispatcher(&press);
2534 EXPECT_TRUE(delegate->tap_down());
2535 EXPECT_EQ(gfx::Rect(5, 5, 80, 80).ToString(),
2536 delegate->bounding_box().ToString());
2537 delegate->Reset();
2539 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 45), 7, tes.Now());
2540 DispatchEventUsingWindowDispatcher(&press2);
2542 // FIXME(tdresser): this should not generate a tap down; however,
2543 // there is at least one case where we need to allow a touch press
2544 // from a currently used touch id. See crbug.com/373125 for details.
2545 EXPECT_TRUE(delegate->begin());
2546 EXPECT_TRUE(delegate->tap_down());
2547 EXPECT_TRUE(delegate->tap_cancel());
2548 EXPECT_FALSE(delegate->scroll_begin());
2551 TEST_F(GestureRecognizerTest, TwoFingerTap) {
2552 scoped_ptr<GestureEventConsumeDelegate> delegate(
2553 new GestureEventConsumeDelegate());
2554 const int kWindowWidth = 123;
2555 const int kWindowHeight = 45;
2556 const int kTouchId1 = 2;
2557 const int kTouchId2 = 3;
2558 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2559 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2560 delegate.get(), -1234, bounds, root_window()));
2561 TimedEvents tes;
2563 delegate->Reset();
2564 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2565 kTouchId1, tes.Now());
2566 DispatchEventUsingWindowDispatcher(&press1);
2567 EXPECT_2_EVENTS(
2568 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2570 delegate->Reset();
2571 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2572 kTouchId2, tes.Now());
2573 DispatchEventUsingWindowDispatcher(&press2);
2574 EXPECT_2_EVENTS(
2575 delegate->events(), ui::ET_GESTURE_TAP_CANCEL, ui::ET_GESTURE_BEGIN);
2577 // Little bit of touch move should not affect our state.
2578 delegate->Reset();
2579 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(102, 202),
2580 kTouchId1, tes.Now());
2581 DispatchEventUsingWindowDispatcher(&move1);
2582 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(131, 202),
2583 kTouchId2, tes.Now());
2584 DispatchEventUsingWindowDispatcher(&move2);
2585 EXPECT_3_EVENTS(delegate->events(), ui::ET_GESTURE_SCROLL_BEGIN,
2586 ui::ET_GESTURE_SCROLL_UPDATE, ui::ET_GESTURE_SCROLL_UPDATE);
2588 // Make sure there is enough delay before the touch is released so that it is
2589 // recognized as a tap.
2590 delegate->Reset();
2591 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2592 kTouchId1, tes.LeapForward(50));
2594 DispatchEventUsingWindowDispatcher(&release1);
2595 EXPECT_2_EVENTS(
2596 delegate->events(), ui::ET_GESTURE_TWO_FINGER_TAP, ui::ET_GESTURE_END);
2598 // Lift second finger.
2599 // Make sure there is enough delay before the touch is released so that it is
2600 // recognized as a tap.
2601 delegate->Reset();
2602 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2603 kTouchId2, tes.LeapForward(50));
2605 DispatchEventUsingWindowDispatcher(&release2);
2606 EXPECT_2_EVENTS(
2607 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2610 TEST_F(GestureRecognizerTest, TwoFingerTapExpired) {
2611 scoped_ptr<GestureEventConsumeDelegate> delegate(
2612 new GestureEventConsumeDelegate());
2613 const int kWindowWidth = 123;
2614 const int kWindowHeight = 45;
2615 const int kTouchId1 = 2;
2616 const int kTouchId2 = 3;
2617 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2618 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2619 delegate.get(), -1234, bounds, root_window()));
2620 TimedEvents tes;
2622 delegate->Reset();
2623 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2624 kTouchId1, tes.Now());
2625 DispatchEventUsingWindowDispatcher(&press1);
2627 delegate->Reset();
2628 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2629 kTouchId2, tes.Now());
2630 DispatchEventUsingWindowDispatcher(&press2);
2632 // Send release event after sufficient delay so that two finger time expires.
2633 delegate->Reset();
2634 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2635 kTouchId1, tes.LeapForward(1000));
2637 DispatchEventUsingWindowDispatcher(&release1);
2638 EXPECT_FALSE(delegate->two_finger_tap());
2640 // Lift second finger.
2641 // Make sure there is enough delay before the touch is released so that it is
2642 // recognized as a tap.
2643 delegate->Reset();
2644 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2645 kTouchId2, tes.LeapForward(50));
2647 DispatchEventUsingWindowDispatcher(&release2);
2648 EXPECT_FALSE(delegate->two_finger_tap());
2651 TEST_F(GestureRecognizerTest, TwoFingerTapChangesToPinch) {
2652 scoped_ptr<GestureEventConsumeDelegate> delegate(
2653 new GestureEventConsumeDelegate());
2654 const int kWindowWidth = 123;
2655 const int kWindowHeight = 45;
2656 const int kTouchId1 = 2;
2657 const int kTouchId2 = 3;
2658 TimedEvents tes;
2660 // Test moving first finger
2662 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2663 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2664 delegate.get(), -1234, bounds, root_window()));
2666 delegate->Reset();
2667 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2668 kTouchId1, tes.Now());
2669 DispatchEventUsingWindowDispatcher(&press1);
2671 delegate->Reset();
2672 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2673 kTouchId2, tes.Now());
2674 DispatchEventUsingWindowDispatcher(&press2);
2676 tes.SendScrollEvent(event_processor(), 230, 330, kTouchId1, delegate.get());
2677 EXPECT_FALSE(delegate->two_finger_tap());
2678 EXPECT_TRUE(delegate->pinch_begin());
2680 // Make sure there is enough delay before the touch is released so that it
2681 // is recognized as a tap.
2682 delegate->Reset();
2683 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2684 kTouchId2, tes.LeapForward(50));
2686 DispatchEventUsingWindowDispatcher(&release);
2687 EXPECT_FALSE(delegate->two_finger_tap());
2688 EXPECT_TRUE(delegate->pinch_end());
2691 // Test moving second finger
2693 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2694 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2695 delegate.get(), -1234, bounds, root_window()));
2697 delegate->Reset();
2698 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2699 kTouchId1, tes.Now());
2700 DispatchEventUsingWindowDispatcher(&press1);
2702 delegate->Reset();
2703 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2704 kTouchId2, tes.Now());
2705 DispatchEventUsingWindowDispatcher(&press2);
2707 tes.SendScrollEvent(event_processor(), 301, 230, kTouchId2, delegate.get());
2708 EXPECT_FALSE(delegate->two_finger_tap());
2709 EXPECT_TRUE(delegate->pinch_begin());
2711 // Make sure there is enough delay before the touch is released so that it
2712 // is recognized as a tap.
2713 delegate->Reset();
2714 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2715 kTouchId1, tes.LeapForward(50));
2717 DispatchEventUsingWindowDispatcher(&release);
2718 EXPECT_FALSE(delegate->two_finger_tap());
2719 EXPECT_TRUE(delegate->pinch_end());
2723 TEST_F(GestureRecognizerTest, NoTwoFingerTapWhenFirstFingerHasScrolled) {
2724 scoped_ptr<GestureEventConsumeDelegate> delegate(
2725 new GestureEventConsumeDelegate());
2726 const int kWindowWidth = 123;
2727 const int kWindowHeight = 45;
2728 const int kTouchId1 = 2;
2729 const int kTouchId2 = 3;
2730 TimedEvents tes;
2732 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2733 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2734 delegate.get(), -1234, bounds, root_window()));
2736 delegate->Reset();
2737 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2738 kTouchId1, tes.Now());
2739 DispatchEventUsingWindowDispatcher(&press1);
2740 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2742 delegate->Reset();
2743 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2744 kTouchId2, tes.Now());
2745 DispatchEventUsingWindowDispatcher(&press2);
2747 EXPECT_FALSE(delegate->pinch_begin());
2749 // Make sure there is enough delay before the touch is released so that it
2750 // is recognized as a tap.
2751 delegate->Reset();
2752 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2753 kTouchId2, tes.LeapForward(50));
2755 DispatchEventUsingWindowDispatcher(&release);
2756 EXPECT_FALSE(delegate->two_finger_tap());
2757 EXPECT_FALSE(delegate->pinch_end());
2760 TEST_F(GestureRecognizerTest, MultiFingerSwipe) {
2761 scoped_ptr<GestureEventConsumeDelegate> delegate(
2762 new GestureEventConsumeDelegate());
2763 const int kWindowWidth = 123;
2764 const int kWindowHeight = 45;
2766 gfx::Rect bounds(5, 10, kWindowWidth, kWindowHeight);
2767 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2768 delegate.get(), -1234, bounds, root_window()));
2770 const int kSteps = 15;
2771 const int kTouchPoints = 4;
2772 gfx::Point points[kTouchPoints] = {
2773 gfx::Point(10, 30),
2774 gfx::Point(30, 20),
2775 gfx::Point(50, 30),
2776 gfx::Point(80, 50)
2779 ui::test::EventGenerator generator(root_window(), window.get());
2781 // The unified gesture recognizer assumes a finger has stopped if it hasn't
2782 // moved for too long. See ui/events/gesture_detection/velocity_tracker.cc's
2783 // kAssumePointerStoppedTimeMs.
2784 for (int count = 2; count <= kTouchPoints; ++count) {
2785 generator.GestureMultiFingerScroll(
2786 count, points, 10, kSteps, 0, -11 * kSteps);
2787 EXPECT_TRUE(delegate->swipe_up());
2788 delegate->Reset();
2790 generator.GestureMultiFingerScroll(
2791 count, points, 10, kSteps, 0, 11 * kSteps);
2792 EXPECT_TRUE(delegate->swipe_down());
2793 delegate->Reset();
2795 generator.GestureMultiFingerScroll(
2796 count, points, 10, kSteps, -11 * kSteps, 0);
2797 EXPECT_TRUE(delegate->swipe_left());
2798 delegate->Reset();
2800 generator.GestureMultiFingerScroll(
2801 count, points, 10, kSteps, 11 * kSteps, 0);
2802 EXPECT_TRUE(delegate->swipe_right());
2803 delegate->Reset();
2805 generator.GestureMultiFingerScroll(
2806 count, points, 10, kSteps, 5 * kSteps, 12 * kSteps);
2807 EXPECT_FALSE(delegate->swipe_down());
2808 delegate->Reset();
2810 generator.GestureMultiFingerScroll(
2811 count, points, 10, kSteps, 4 * kSteps, 12 * kSteps);
2812 EXPECT_TRUE(delegate->swipe_down());
2813 delegate->Reset();
2815 generator.GestureMultiFingerScroll(
2816 count, points, 10, kSteps, 3 * kSteps, 12 * kSteps);
2817 EXPECT_TRUE(delegate->swipe_down());
2818 delegate->Reset();
2822 TEST_F(GestureRecognizerTest, TwoFingerTapCancelled) {
2823 scoped_ptr<GestureEventConsumeDelegate> delegate(
2824 new GestureEventConsumeDelegate());
2825 const int kWindowWidth = 123;
2826 const int kWindowHeight = 45;
2827 const int kTouchId1 = 2;
2828 const int kTouchId2 = 3;
2829 TimedEvents tes;
2831 // Test canceling first finger.
2833 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2834 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2835 delegate.get(), -1234, bounds, root_window()));
2837 delegate->Reset();
2838 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2839 kTouchId1, tes.Now());
2840 DispatchEventUsingWindowDispatcher(&press1);
2842 delegate->Reset();
2843 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2844 kTouchId2, tes.Now());
2845 DispatchEventUsingWindowDispatcher(&press2);
2847 delegate->Reset();
2848 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2849 kTouchId1, tes.Now());
2850 DispatchEventUsingWindowDispatcher(&cancel);
2851 EXPECT_FALSE(delegate->two_finger_tap());
2853 // Make sure there is enough delay before the touch is released so that it
2854 // is recognized as a tap.
2855 delegate->Reset();
2856 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2857 kTouchId2, tes.LeapForward(50));
2859 DispatchEventUsingWindowDispatcher(&release);
2860 EXPECT_FALSE(delegate->two_finger_tap());
2863 // Test canceling second finger
2865 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2866 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2867 delegate.get(), -1234, bounds, root_window()));
2869 delegate->Reset();
2870 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2871 kTouchId1, tes.Now());
2872 DispatchEventUsingWindowDispatcher(&press1);
2874 delegate->Reset();
2875 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2876 kTouchId2, tes.Now());
2877 DispatchEventUsingWindowDispatcher(&press2);
2879 delegate->Reset();
2880 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2881 kTouchId2, tes.Now());
2882 DispatchEventUsingWindowDispatcher(&cancel);
2883 EXPECT_FALSE(delegate->two_finger_tap());
2885 // Make sure there is enough delay before the touch is released so that it
2886 // is recognized as a tap.
2887 delegate->Reset();
2888 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2889 kTouchId1, tes.LeapForward(50));
2891 DispatchEventUsingWindowDispatcher(&release);
2892 EXPECT_FALSE(delegate->two_finger_tap());
2896 TEST_F(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) {
2897 scoped_ptr<GestureEventConsumeDelegate> delegate(
2898 new GestureEventConsumeDelegate());
2899 const int kWindowWidth = 523;
2900 const int kWindowHeight = 45;
2901 const int kTouchId1 = 2;
2902 const int kTouchId2 = 3;
2903 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2904 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2905 delegate.get(), -1234, bounds, root_window()));
2906 TimedEvents tes;
2908 delegate->Reset();
2909 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2910 kTouchId1, tes.Now());
2911 DispatchEventUsingWindowDispatcher(&press1);
2912 EXPECT_FALSE(delegate->tap());
2913 EXPECT_TRUE(delegate->tap_down());
2914 EXPECT_FALSE(delegate->tap_cancel());
2915 EXPECT_FALSE(delegate->scroll_begin());
2916 EXPECT_FALSE(delegate->scroll_update());
2917 EXPECT_FALSE(delegate->scroll_end());
2918 EXPECT_FALSE(delegate->long_press());
2919 EXPECT_FALSE(delegate->two_finger_tap());
2921 delegate->Reset();
2922 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201),
2923 kTouchId2, tes.Now());
2924 DispatchEventUsingWindowDispatcher(&press2);
2925 EXPECT_FALSE(delegate->tap());
2926 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
2927 EXPECT_TRUE(delegate->tap_cancel());
2928 EXPECT_FALSE(delegate->scroll_begin());
2929 EXPECT_FALSE(delegate->scroll_update());
2930 EXPECT_FALSE(delegate->scroll_end());
2931 EXPECT_FALSE(delegate->long_press());
2932 EXPECT_FALSE(delegate->two_finger_tap());
2933 EXPECT_FALSE(delegate->pinch_begin());
2935 delegate->Reset();
2936 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(530, 301),
2937 kTouchId2, tes.Now());
2938 DispatchEventUsingWindowDispatcher(&move2);
2939 EXPECT_FALSE(delegate->tap());
2940 EXPECT_FALSE(delegate->tap_down());
2941 EXPECT_FALSE(delegate->tap_cancel());
2942 // Pinch & Scroll only when there is enough movement.
2943 EXPECT_TRUE(delegate->scroll_begin());
2944 EXPECT_TRUE(delegate->scroll_update());
2945 EXPECT_FALSE(delegate->scroll_end());
2946 EXPECT_FALSE(delegate->long_press());
2947 EXPECT_FALSE(delegate->two_finger_tap());
2948 EXPECT_TRUE(delegate->pinch_begin());
2951 // Verifies if a window is the target of multiple touch-ids and we hide the
2952 // window everything is cleaned up correctly.
2953 TEST_F(GestureRecognizerTest, FlushAllOnHide) {
2954 scoped_ptr<GestureEventConsumeDelegate> delegate(
2955 new GestureEventConsumeDelegate());
2956 gfx::Rect bounds(0, 0, 200, 200);
2957 scoped_ptr<aura::Window> window(
2958 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2959 const int kTouchId1 = 8;
2960 const int kTouchId2 = 2;
2961 TimedEvents tes;
2963 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2964 kTouchId1, tes.Now());
2965 DispatchEventUsingWindowDispatcher(&press1);
2966 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2967 kTouchId2, tes.Now());
2968 DispatchEventUsingWindowDispatcher(&press2);
2969 window->Hide();
2970 EXPECT_EQ(NULL,
2971 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1));
2972 EXPECT_EQ(NULL,
2973 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2976 TEST_F(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) {
2977 scoped_ptr<QueueTouchEventDelegate> delegate(
2978 new QueueTouchEventDelegate(host()->dispatcher()));
2979 const int kTouchId = 2;
2980 gfx::Rect bounds(100, 200, 100, 100);
2981 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2982 delegate.get(), -1234, bounds, root_window()));
2983 delegate->set_window(window.get());
2984 TimedEvents tes;
2986 delegate->Reset();
2987 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2988 kTouchId, tes.Now());
2989 DispatchEventUsingWindowDispatcher(&press);
2990 // Scroll around, to cancel the long press
2991 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
2993 delegate->Reset();
2994 delegate->ReceivedAck();
2995 EXPECT_TRUE(delegate->tap_down());
2997 // Wait long enough that long press would have fired if the touchmove hadn't
2998 // prevented it.
2999 DelayByLongPressTimeout();
3001 delegate->Reset();
3002 delegate->ReceivedAckPreventDefaulted();
3003 EXPECT_FALSE(delegate->long_press());
3006 // Same as GestureEventConsumeDelegate, but consumes all the touch-move events.
3007 class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate {
3008 public:
3009 ConsumesTouchMovesDelegate() : consume_touch_move_(true) {}
3010 ~ConsumesTouchMovesDelegate() override {}
3012 void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; }
3014 private:
3015 void OnTouchEvent(ui::TouchEvent* touch) override {
3016 if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED)
3017 touch->SetHandled();
3018 else
3019 GestureEventConsumeDelegate::OnTouchEvent(touch);
3022 bool consume_touch_move_;
3024 DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate);
3027 // Same as GestureEventScroll, but tests that the behavior is the same
3028 // even if all the touch-move events are consumed.
3029 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) {
3030 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3031 new ConsumesTouchMovesDelegate());
3032 const int kWindowWidth = 123;
3033 const int kWindowHeight = 45;
3034 const int kTouchId = 5;
3035 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3036 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3037 delegate.get(), -1234, bounds, root_window()));
3038 TimedEvents tes;
3040 delegate->Reset();
3041 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3042 kTouchId, tes.Now());
3043 DispatchEventUsingWindowDispatcher(&press);
3044 EXPECT_FALSE(delegate->tap());
3045 EXPECT_TRUE(delegate->tap_down());
3046 EXPECT_FALSE(delegate->tap_cancel());
3047 EXPECT_TRUE(delegate->begin());
3048 EXPECT_FALSE(delegate->scroll_begin());
3049 EXPECT_FALSE(delegate->scroll_update());
3050 EXPECT_FALSE(delegate->scroll_end());
3052 // Move the touch-point enough so that it would normally be considered a
3053 // scroll. But since the touch-moves will be consumed, the scroll should not
3054 // start.
3055 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3056 EXPECT_FALSE(delegate->tap());
3057 EXPECT_FALSE(delegate->tap_down());
3058 EXPECT_TRUE(delegate->tap_cancel());
3059 EXPECT_FALSE(delegate->begin());
3060 EXPECT_FALSE(delegate->scroll_update());
3061 EXPECT_FALSE(delegate->scroll_end());
3063 EXPECT_TRUE(delegate->scroll_begin());
3065 // Release the touch back at the start point. This should end without causing
3066 // a tap.
3067 delegate->Reset();
3068 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230),
3069 kTouchId, tes.LeapForward(50));
3070 DispatchEventUsingWindowDispatcher(&release);
3071 EXPECT_FALSE(delegate->tap());
3072 EXPECT_FALSE(delegate->tap_down());
3073 EXPECT_FALSE(delegate->tap_cancel());
3074 EXPECT_FALSE(delegate->begin());
3075 EXPECT_TRUE(delegate->end());
3076 EXPECT_FALSE(delegate->scroll_begin());
3077 EXPECT_FALSE(delegate->scroll_update());
3079 EXPECT_TRUE(delegate->scroll_end());
3082 // Tests the behavior of 2F scroll when some of the touch-move events are
3083 // consumed.
3084 TEST_F(GestureRecognizerTest, GestureEventScrollTwoFingerTouchMoveConsumed) {
3085 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3086 new ConsumesTouchMovesDelegate());
3087 const int kWindowWidth = 123;
3088 const int kWindowHeight = 100;
3089 const int kTouchId1 = 2;
3090 const int kTouchId2 = 3;
3091 TimedEvents tes;
3093 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3094 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3095 delegate.get(), -1234, bounds, root_window()));
3097 delegate->Reset();
3098 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3099 kTouchId1, tes.Now());
3100 DispatchEventUsingWindowDispatcher(&press1);
3101 tes.SendScrollEvent(event_processor(), 131, 231, kTouchId1, delegate.get());
3103 EXPECT_2_EVENTS(delegate->events(),
3104 ui::ET_GESTURE_TAP_CANCEL,
3105 ui::ET_GESTURE_SCROLL_BEGIN);
3107 delegate->Reset();
3108 // Second finger touches down and moves.
3109 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
3110 kTouchId2, tes.LeapForward(50));
3111 DispatchEventUsingWindowDispatcher(&press2);
3112 tes.SendScrollEvent(event_processor(), 161, 231, kTouchId2, delegate.get());
3113 EXPECT_0_EVENTS(delegate->events());
3115 delegate->Reset();
3116 // Move first finger again, no PinchUpdate & ScrollUpdate.
3117 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId1, delegate.get());
3118 EXPECT_0_EVENTS(delegate->events());
3120 // Stops consuming touch-move.
3121 delegate->set_consume_touch_move(false);
3123 delegate->Reset();
3124 // Making a pinch gesture.
3125 tes.SendScrollEvent(event_processor(), 161, 260, kTouchId1, delegate.get());
3126 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
3128 delegate->Reset();
3129 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId2, delegate.get());
3130 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
3132 delegate->Reset();
3133 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3134 kTouchId1, tes.Now());
3135 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
3136 kTouchId2, tes.Now());
3137 DispatchEventUsingWindowDispatcher(&release1);
3138 DispatchEventUsingWindowDispatcher(&release2);
3140 EXPECT_3_EVENTS(delegate->events(),
3141 ui::ET_GESTURE_END,
3142 ui::ET_SCROLL_FLING_START,
3143 ui::ET_GESTURE_END);
3146 // Like as GestureEventTouchMoveConsumed but tests the different behavior
3147 // depending on whether the events were consumed before or after the scroll
3148 // started.
3149 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) {
3150 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3151 new ConsumesTouchMovesDelegate());
3152 const int kWindowWidth = 123;
3153 const int kWindowHeight = 45;
3154 const int kTouchId = 5;
3155 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3156 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3157 delegate.get(), -1234, bounds, root_window()));
3158 TimedEvents tes;
3160 delegate->Reset();
3161 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3162 kTouchId, tes.Now());
3163 DispatchEventUsingWindowDispatcher(&press);
3164 EXPECT_FALSE(delegate->tap());
3165 EXPECT_TRUE(delegate->tap_down());
3166 EXPECT_FALSE(delegate->tap_cancel());
3167 EXPECT_TRUE(delegate->begin());
3168 EXPECT_FALSE(delegate->scroll_begin());
3169 EXPECT_FALSE(delegate->scroll_update());
3170 EXPECT_FALSE(delegate->scroll_end());
3172 // Move the touch-point enough so that it would normally be considered a
3173 // scroll. But since the touch-moves will be consumed, the scroll should not
3174 // start.
3175 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3176 EXPECT_FALSE(delegate->tap());
3177 EXPECT_FALSE(delegate->tap_down());
3178 EXPECT_TRUE(delegate->tap_cancel());
3179 EXPECT_FALSE(delegate->begin());
3180 EXPECT_FALSE(delegate->scroll_update());
3181 EXPECT_FALSE(delegate->scroll_end());
3183 // Consuming the first touch move event won't prevent all future scrolling.
3184 EXPECT_TRUE(delegate->scroll_begin());
3186 // Now, stop consuming touch-move events, and move the touch-point again.
3187 delegate->set_consume_touch_move(false);
3188 tes.SendScrollEvent(event_processor(), 159, 259, kTouchId, delegate.get());
3189 EXPECT_FALSE(delegate->tap());
3190 EXPECT_FALSE(delegate->tap_down());
3191 EXPECT_FALSE(delegate->tap_cancel());
3192 EXPECT_FALSE(delegate->begin());
3193 EXPECT_FALSE(delegate->scroll_begin());
3194 EXPECT_FALSE(delegate->scroll_end());
3196 // Scroll not prevented by consumed first touch move.
3197 EXPECT_TRUE(delegate->scroll_update());
3198 EXPECT_EQ(29, delegate->scroll_x());
3199 EXPECT_EQ(29, delegate->scroll_y());
3200 EXPECT_EQ(gfx::Point(0, 0).ToString(),
3201 delegate->scroll_begin_position().ToString());
3203 // Start consuming touch-move events again.
3204 delegate->set_consume_touch_move(true);
3206 // Move some more to generate a few more scroll updates.
3207 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
3208 EXPECT_FALSE(delegate->tap());
3209 EXPECT_FALSE(delegate->tap_down());
3210 EXPECT_FALSE(delegate->tap_cancel());
3211 EXPECT_FALSE(delegate->begin());
3212 EXPECT_FALSE(delegate->scroll_begin());
3213 EXPECT_FALSE(delegate->scroll_update());
3214 EXPECT_FALSE(delegate->scroll_end());
3215 EXPECT_EQ(0, delegate->scroll_x());
3216 EXPECT_EQ(0, delegate->scroll_y());
3218 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
3219 EXPECT_FALSE(delegate->tap());
3220 EXPECT_FALSE(delegate->tap_down());
3221 EXPECT_FALSE(delegate->tap_cancel());
3222 EXPECT_FALSE(delegate->begin());
3223 EXPECT_FALSE(delegate->scroll_begin());
3224 EXPECT_FALSE(delegate->scroll_update());
3225 EXPECT_FALSE(delegate->scroll_end());
3226 EXPECT_EQ(0, delegate->scroll_x());
3227 EXPECT_EQ(0, delegate->scroll_y());
3229 // Release the touch.
3230 delegate->Reset();
3231 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3232 kTouchId, tes.LeapForward(50));
3233 DispatchEventUsingWindowDispatcher(&release);
3234 EXPECT_FALSE(delegate->tap());
3235 EXPECT_FALSE(delegate->tap_down());
3236 EXPECT_FALSE(delegate->tap_cancel());
3237 EXPECT_FALSE(delegate->begin());
3238 EXPECT_TRUE(delegate->end());
3239 EXPECT_FALSE(delegate->scroll_begin());
3240 EXPECT_FALSE(delegate->scroll_update());
3241 EXPECT_FALSE(delegate->fling());
3243 EXPECT_TRUE(delegate->scroll_end());
3246 // Check that appropriate touch events generate double tap gesture events.
3247 TEST_F(GestureRecognizerTest, GestureEventDoubleTap) {
3248 scoped_ptr<GestureEventConsumeDelegate> delegate(
3249 new GestureEventConsumeDelegate());
3250 const int kWindowWidth = 123;
3251 const int kWindowHeight = 45;
3252 const int kTouchId = 2;
3253 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3254 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3255 delegate.get(), -1234, bounds, root_window()));
3256 TimedEvents tes;
3258 // First tap (tested in GestureEventTap)
3259 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3260 kTouchId, tes.Now());
3261 DispatchEventUsingWindowDispatcher(&press1);
3262 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3263 kTouchId, tes.LeapForward(50));
3264 DispatchEventUsingWindowDispatcher(&release1);
3265 delegate->Reset();
3267 // Second tap
3268 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3269 kTouchId, tes.LeapForward(200));
3270 DispatchEventUsingWindowDispatcher(&press2);
3271 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3272 kTouchId, tes.LeapForward(50));
3273 DispatchEventUsingWindowDispatcher(&release2);
3275 EXPECT_TRUE(delegate->tap());
3276 EXPECT_TRUE(delegate->tap_down());
3277 EXPECT_FALSE(delegate->tap_cancel());
3278 EXPECT_TRUE(delegate->begin());
3279 EXPECT_TRUE(delegate->end());
3280 EXPECT_FALSE(delegate->scroll_begin());
3281 EXPECT_FALSE(delegate->scroll_update());
3282 EXPECT_FALSE(delegate->scroll_end());
3284 EXPECT_EQ(2, delegate->tap_count());
3287 // Check that appropriate touch events generate triple tap gesture events.
3288 TEST_F(GestureRecognizerTest, GestureEventTripleTap) {
3289 scoped_ptr<GestureEventConsumeDelegate> delegate(
3290 new GestureEventConsumeDelegate());
3291 const int kWindowWidth = 123;
3292 const int kWindowHeight = 45;
3293 const int kTouchId = 2;
3294 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3295 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3296 delegate.get(), -1234, bounds, root_window()));
3297 TimedEvents tes;
3299 // First tap (tested in GestureEventTap)
3300 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3301 kTouchId, tes.Now());
3302 DispatchEventUsingWindowDispatcher(&press1);
3303 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3304 kTouchId, tes.LeapForward(50));
3305 DispatchEventUsingWindowDispatcher(&release1);
3307 EXPECT_EQ(1, delegate->tap_count());
3308 delegate->Reset();
3310 // Second tap (tested in GestureEventDoubleTap)
3311 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3312 kTouchId, tes.LeapForward(200));
3313 DispatchEventUsingWindowDispatcher(&press2);
3314 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3315 kTouchId, tes.LeapForward(50));
3316 DispatchEventUsingWindowDispatcher(&release2);
3318 EXPECT_EQ(2, delegate->tap_count());
3319 delegate->Reset();
3321 // Third tap
3322 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(102, 206),
3323 kTouchId, tes.LeapForward(200));
3324 DispatchEventUsingWindowDispatcher(&press3);
3325 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3326 kTouchId, tes.LeapForward(50));
3327 DispatchEventUsingWindowDispatcher(&release3);
3329 // Third, Fourth and Fifth Taps. Taps after the third should have their
3330 // |tap_count| wrap around back to 1.
3331 for (int i = 3; i < 5; ++i) {
3332 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED,
3333 gfx::Point(102, 206),
3334 kTouchId,
3335 tes.LeapForward(200));
3336 DispatchEventUsingWindowDispatcher(&press3);
3337 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED,
3338 gfx::Point(102, 206),
3339 kTouchId,
3340 tes.LeapForward(50));
3341 DispatchEventUsingWindowDispatcher(&release3);
3343 EXPECT_TRUE(delegate->tap());
3344 EXPECT_TRUE(delegate->tap_down());
3345 EXPECT_FALSE(delegate->tap_cancel());
3346 EXPECT_TRUE(delegate->begin());
3347 EXPECT_TRUE(delegate->end());
3348 EXPECT_FALSE(delegate->scroll_begin());
3349 EXPECT_FALSE(delegate->scroll_update());
3350 EXPECT_FALSE(delegate->scroll_end());
3351 EXPECT_EQ(1 + (i % 3), delegate->tap_count());
3355 // Check that we don't get a double tap when the two taps are far apart.
3356 TEST_F(GestureRecognizerTest, TwoTapsFarApart) {
3357 scoped_ptr<GestureEventConsumeDelegate> delegate(
3358 new GestureEventConsumeDelegate());
3359 const int kWindowWidth = 123;
3360 const int kWindowHeight = 45;
3361 const int kTouchId = 2;
3362 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3363 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3364 delegate.get(), -1234, bounds, root_window()));
3365 TimedEvents tes;
3367 // First tap (tested in GestureEventTap)
3368 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3369 kTouchId, tes.Now());
3370 DispatchEventUsingWindowDispatcher(&press1);
3371 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3372 kTouchId, tes.LeapForward(50));
3373 DispatchEventUsingWindowDispatcher(&release1);
3374 delegate->Reset();
3376 // Second tap, close in time but far in distance
3377 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201),
3378 kTouchId, tes.LeapForward(200));
3379 DispatchEventUsingWindowDispatcher(&press2);
3380 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201),
3381 kTouchId, tes.LeapForward(50));
3382 DispatchEventUsingWindowDispatcher(&release2);
3384 EXPECT_TRUE(delegate->tap());
3385 EXPECT_TRUE(delegate->tap_down());
3386 EXPECT_FALSE(delegate->tap_cancel());
3387 EXPECT_TRUE(delegate->begin());
3388 EXPECT_TRUE(delegate->end());
3389 EXPECT_FALSE(delegate->scroll_begin());
3390 EXPECT_FALSE(delegate->scroll_update());
3391 EXPECT_FALSE(delegate->scroll_end());
3393 EXPECT_EQ(1, delegate->tap_count());
3396 // Check that we don't get a double tap when the two taps have a long enough
3397 // delay in between.
3398 TEST_F(GestureRecognizerTest, TwoTapsWithDelayBetween) {
3399 scoped_ptr<GestureEventConsumeDelegate> delegate(
3400 new GestureEventConsumeDelegate());
3401 const int kWindowWidth = 123;
3402 const int kWindowHeight = 45;
3403 const int kTouchId = 2;
3404 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3405 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3406 delegate.get(), -1234, bounds, root_window()));
3407 TimedEvents tes;
3409 // First tap (tested in GestureEventTap)
3410 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3411 kTouchId, tes.Now());
3412 DispatchEventUsingWindowDispatcher(&press1);
3413 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3414 kTouchId, tes.LeapForward(50));
3415 DispatchEventUsingWindowDispatcher(&release1);
3416 delegate->Reset();
3418 // Second tap, close in distance but after some delay
3419 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3420 kTouchId, tes.LeapForward(2000));
3421 DispatchEventUsingWindowDispatcher(&press2);
3422 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3423 kTouchId, tes.LeapForward(50));
3424 DispatchEventUsingWindowDispatcher(&release2);
3426 EXPECT_TRUE(delegate->tap());
3427 EXPECT_TRUE(delegate->tap_down());
3428 EXPECT_FALSE(delegate->tap_cancel());
3429 EXPECT_TRUE(delegate->begin());
3430 EXPECT_TRUE(delegate->end());
3431 EXPECT_FALSE(delegate->scroll_begin());
3432 EXPECT_FALSE(delegate->scroll_update());
3433 EXPECT_FALSE(delegate->scroll_end());
3435 EXPECT_EQ(1, delegate->tap_count());
3438 // Checks that if the bounding-box of a gesture changes because of change in
3439 // radius of a touch-point, and not because of change in position, then there
3440 // are not gesture events from that.
3441 TEST_F(GestureRecognizerTest, BoundingBoxRadiusChange) {
3442 scoped_ptr<GestureEventConsumeDelegate> delegate(
3443 new GestureEventConsumeDelegate());
3444 const int kWindowWidth = 234;
3445 const int kWindowHeight = 345;
3446 const int kTouchId = 5, kTouchId2 = 7;
3447 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3448 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3449 delegate.get(), -1234, bounds, root_window()));
3450 TimedEvents tes;
3452 ui::TouchEvent press1(
3453 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, tes.Now());
3454 DispatchEventUsingWindowDispatcher(&press1);
3455 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
3457 delegate->Reset();
3459 ui::TouchEvent press2(
3460 ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2,
3461 tes.LeapForward(400));
3462 press2.set_radius_x(5);
3463 DispatchEventUsingWindowDispatcher(&press2);
3464 EXPECT_FALSE(delegate->pinch_begin());
3465 EXPECT_EQ(gfx::Rect(101, 196, 105, 10).ToString(),
3466 delegate->bounding_box().ToString());
3468 delegate->Reset();
3470 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(50, 50), kTouchId,
3471 tes.LeapForward(40));
3472 DispatchEventUsingWindowDispatcher(&move1);
3473 EXPECT_TRUE(delegate->pinch_begin());
3474 EXPECT_EQ(gfx::Rect(50, 50, 156, 156).ToString(),
3475 delegate->bounding_box().ToString());
3477 delegate->Reset();
3479 // The position doesn't move, but the radius changes.
3480 ui::TouchEvent move2(
3481 ui::ET_TOUCH_MOVED, gfx::Point(50, 50), kTouchId, tes.LeapForward(40));
3482 move2.set_radius_x(50);
3483 move2.set_radius_y(60);
3484 DispatchEventUsingWindowDispatcher(&move2);
3485 EXPECT_FALSE(delegate->tap());
3486 EXPECT_FALSE(delegate->tap_cancel());
3487 EXPECT_FALSE(delegate->scroll_update());
3488 EXPECT_FALSE(delegate->pinch_update());
3490 delegate->Reset();
3493 // Checks that slow scrolls deliver the correct deltas.
3494 // In particular, fix for http;//crbug.com/150573.
3495 TEST_F(GestureRecognizerTest, NoDriftInScroll) {
3496 ui::GestureConfiguration::GetInstance()
3497 ->set_max_touch_move_in_pixels_for_click(3);
3498 scoped_ptr<GestureEventConsumeDelegate> delegate(
3499 new GestureEventConsumeDelegate());
3500 const int kWindowWidth = 234;
3501 const int kWindowHeight = 345;
3502 const int kTouchId = 5;
3503 TimedEvents tes;
3504 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3505 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3506 delegate.get(), -1234, bounds, root_window()));
3508 ui::TouchEvent press1(
3509 ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, tes.Now());
3510 DispatchEventUsingWindowDispatcher(&press1);
3511 EXPECT_TRUE(delegate->begin());
3513 delegate->Reset();
3515 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId,
3516 tes.LeapForward(40));
3517 DispatchEventUsingWindowDispatcher(&move1);
3518 EXPECT_FALSE(delegate->scroll_begin());
3520 delegate->Reset();
3522 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3523 tes.LeapForward(40));
3524 DispatchEventUsingWindowDispatcher(&move2);
3525 EXPECT_TRUE(delegate->tap_cancel());
3526 EXPECT_TRUE(delegate->scroll_begin());
3527 EXPECT_TRUE(delegate->scroll_update());
3528 // 3 px consumed by touch slop region.
3529 EXPECT_EQ(-1, delegate->scroll_y());
3530 EXPECT_EQ(-4, delegate->scroll_y_hint());
3532 delegate->Reset();
3534 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3535 tes.LeapForward(40));
3536 DispatchEventUsingWindowDispatcher(&move3);
3537 EXPECT_FALSE(delegate->scroll_update());
3539 delegate->Reset();
3541 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId,
3542 tes.LeapForward(40));
3543 DispatchEventUsingWindowDispatcher(&move4);
3544 EXPECT_TRUE(delegate->scroll_update());
3545 EXPECT_EQ(-1, delegate->scroll_y());
3547 delegate->Reset();
3550 // Ensure that move events which are preventDefaulted will cause a tap
3551 // cancel gesture event to be fired if the move would normally cause a
3552 // scroll. See bug http://crbug.com/146397.
3553 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveCanFireTapCancel) {
3554 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3555 new ConsumesTouchMovesDelegate());
3556 const int kTouchId = 5;
3557 gfx::Rect bounds(100, 200, 123, 45);
3558 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3559 delegate.get(), -1234, bounds, root_window()));
3560 TimedEvents tes;
3562 delegate->Reset();
3563 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3564 kTouchId, tes.Now());
3566 delegate->set_consume_touch_move(false);
3567 DispatchEventUsingWindowDispatcher(&press);
3568 delegate->set_consume_touch_move(true);
3569 delegate->Reset();
3570 // Move the touch-point enough so that it would normally be considered a
3571 // scroll. But since the touch-moves will be consumed, no scrolling should
3572 // occur.
3573 // With the unified gesture detector, we will receive a scroll begin gesture,
3574 // whereas with the aura gesture recognizer we won't.
3575 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3576 EXPECT_FALSE(delegate->tap());
3577 EXPECT_FALSE(delegate->tap_down());
3578 EXPECT_TRUE(delegate->tap_cancel());
3579 EXPECT_FALSE(delegate->begin());
3580 EXPECT_FALSE(delegate->scroll_update());
3581 EXPECT_FALSE(delegate->scroll_end());
3584 TEST_F(GestureRecognizerTest, CancelAllActiveTouches) {
3585 scoped_ptr<GestureEventConsumeDelegate> delegate(
3586 new GestureEventConsumeDelegate());
3587 TimedEvents tes;
3588 const int kWindowWidth = 800;
3589 const int kWindowHeight = 600;
3590 const int kTouchId1 = 1;
3591 const int kTouchId2 = 2;
3592 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3593 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3594 delegate.get(), -1234, bounds, root_window()));
3595 scoped_ptr<TestEventHandler> handler(new TestEventHandler());
3596 window->AddPreTargetHandler(handler.get());
3598 // Start a gesture sequence on |window|. Then cancel all touches.
3599 // Make sure |window| receives a touch-cancel event.
3600 delegate->Reset();
3601 ui::TouchEvent press(
3602 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
3603 DispatchEventUsingWindowDispatcher(&press);
3604 EXPECT_2_EVENTS(
3605 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
3606 delegate->Reset();
3607 ui::TouchEvent p2(
3608 ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), kTouchId2, tes.Now());
3609 DispatchEventUsingWindowDispatcher(&p2);
3610 EXPECT_2_EVENTS(
3611 delegate->events(), ui::ET_GESTURE_TAP_CANCEL, ui::ET_GESTURE_BEGIN);
3612 delegate->Reset();
3613 ui::TouchEvent move(
3614 ui::ET_TOUCH_MOVED, gfx::Point(350, 300), kTouchId2, tes.Now());
3615 DispatchEventUsingWindowDispatcher(&move);
3616 EXPECT_3_EVENTS(delegate->events(),
3617 ui::ET_GESTURE_SCROLL_BEGIN,
3618 ui::ET_GESTURE_SCROLL_UPDATE,
3619 ui::ET_GESTURE_PINCH_BEGIN);
3620 EXPECT_EQ(2, handler->touch_pressed_count());
3621 delegate->Reset();
3622 handler->Reset();
3624 ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get();
3625 EXPECT_EQ(window.get(),
3626 gesture_recognizer->GetTouchLockedTarget(press));
3628 ui::GestureRecognizer::Get()->CancelActiveTouchesExcept(nullptr);
3630 EXPECT_EQ(NULL, gesture_recognizer->GetTouchLockedTarget(press));
3631 EXPECT_4_EVENTS(delegate->events(),
3632 ui::ET_GESTURE_PINCH_END,
3633 ui::ET_GESTURE_SCROLL_END,
3634 ui::ET_GESTURE_END,
3635 ui::ET_GESTURE_END);
3636 const std::vector<gfx::PointF>& points = handler->cancelled_touch_points();
3637 EXPECT_EQ(2U, points.size());
3638 EXPECT_EQ(gfx::Point(101, 201), points[0]);
3639 EXPECT_EQ(gfx::Point(350, 300), points[1]);
3642 // Check that appropriate touch events generate show press events
3643 TEST_F(GestureRecognizerTest, GestureEventShowPress) {
3644 scoped_ptr<GestureEventConsumeDelegate> delegate(
3645 new GestureEventConsumeDelegate());
3646 TimedEvents tes;
3647 const int kWindowWidth = 123;
3648 const int kWindowHeight = 45;
3649 const int kTouchId = 2;
3650 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3651 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3652 delegate.get(), -1234, bounds, root_window()));
3654 delegate->Reset();
3656 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3657 kTouchId, tes.Now());
3658 DispatchEventUsingWindowDispatcher(&press1);
3659 EXPECT_TRUE(delegate->tap_down());
3660 EXPECT_TRUE(delegate->begin());
3661 EXPECT_FALSE(delegate->tap_cancel());
3663 // We haven't pressed long enough for a show press to occur
3664 EXPECT_FALSE(delegate->show_press());
3666 // Wait until the timer runs out
3667 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
3668 EXPECT_TRUE(delegate->show_press());
3669 EXPECT_FALSE(delegate->tap_cancel());
3671 delegate->Reset();
3672 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3673 kTouchId, tes.Now());
3674 DispatchEventUsingWindowDispatcher(&release1);
3675 EXPECT_FALSE(delegate->long_press());
3677 // Note the tap isn't dispatched until the release
3678 EXPECT_FALSE(delegate->tap_cancel());
3679 EXPECT_TRUE(delegate->tap());
3682 // Check that scrolling cancels a show press
3683 TEST_F(GestureRecognizerTest, GestureEventShowPressCancelledByScroll) {
3684 scoped_ptr<GestureEventConsumeDelegate> delegate(
3685 new GestureEventConsumeDelegate());
3686 TimedEvents tes;
3687 const int kWindowWidth = 123;
3688 const int kWindowHeight = 45;
3689 const int kTouchId = 6;
3690 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3691 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3692 delegate.get(), -1234, bounds, root_window()));
3694 delegate->Reset();
3696 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3697 kTouchId, tes.Now());
3698 DispatchEventUsingWindowDispatcher(&press1);
3699 EXPECT_TRUE(delegate->tap_down());
3701 // We haven't pressed long enough for a show press to occur
3702 EXPECT_FALSE(delegate->show_press());
3703 EXPECT_FALSE(delegate->tap_cancel());
3705 // Scroll around, to cancel the show press
3706 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3707 // Wait until the timer runs out
3708 DelayByShowPressTimeout();
3709 EXPECT_FALSE(delegate->show_press());
3710 EXPECT_TRUE(delegate->tap_cancel());
3712 delegate->Reset();
3713 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3714 kTouchId, tes.LeapForward(10));
3715 DispatchEventUsingWindowDispatcher(&release1);
3716 EXPECT_FALSE(delegate->show_press());
3717 EXPECT_FALSE(delegate->tap_cancel());
3720 // Test that show press events are sent immediately on tap
3721 TEST_F(GestureRecognizerTest, GestureEventShowPressSentOnTap) {
3722 scoped_ptr<GestureEventConsumeDelegate> delegate(
3723 new GestureEventConsumeDelegate());
3724 TimedEvents tes;
3725 const int kWindowWidth = 123;
3726 const int kWindowHeight = 45;
3727 const int kTouchId = 6;
3728 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3729 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3730 delegate.get(), -1234, bounds, root_window()));
3732 delegate->Reset();
3734 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3735 kTouchId, tes.Now());
3736 DispatchEventUsingWindowDispatcher(&press1);
3737 EXPECT_TRUE(delegate->tap_down());
3739 // We haven't pressed long enough for a show press to occur
3740 EXPECT_FALSE(delegate->show_press());
3741 EXPECT_FALSE(delegate->tap_cancel());
3743 delegate->Reset();
3744 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3745 kTouchId, tes.LeapForward(50));
3746 DispatchEventUsingWindowDispatcher(&release1);
3747 EXPECT_TRUE(delegate->show_press());
3748 EXPECT_FALSE(delegate->tap_cancel());
3749 EXPECT_TRUE(delegate->tap());
3752 // Test that consuming the first move touch event prevents a scroll.
3753 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveScrollTest) {
3754 scoped_ptr<QueueTouchEventDelegate> delegate(
3755 new QueueTouchEventDelegate(host()->dispatcher()));
3756 TimedEvents tes;
3757 const int kTouchId = 7;
3758 gfx::Rect bounds(0, 0, 1000, 1000);
3759 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3760 delegate.get(), -1234, bounds, root_window()));
3761 delegate->set_window(window.get());
3763 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3764 kTouchId, tes.Now());
3765 DispatchEventUsingWindowDispatcher(&press);
3766 delegate->ReceivedAck();
3768 // A touch move within the slop region is never consumed in web contents. The
3769 // unified GR won't prevent scroll if a touch move within the slop region is
3770 // consumed, so make sure this touch move exceeds the slop region.
3771 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(10, 10),
3772 kTouchId, tes.Now());
3773 DispatchEventUsingWindowDispatcher(&move1);
3774 delegate->ReceivedAckPreventDefaulted();
3776 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3777 kTouchId, tes.Now());
3778 DispatchEventUsingWindowDispatcher(&move2);
3779 delegate->ReceivedAck();
3781 // With the unified gesture detector, consuming the first touch move event
3782 // won't prevent all future scrolling.
3783 EXPECT_TRUE(delegate->scroll_begin());
3784 EXPECT_TRUE(delegate->scroll_update());
3787 // Test that consuming the first move touch doesn't prevent a tap.
3788 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveTapTest) {
3789 scoped_ptr<QueueTouchEventDelegate> delegate(
3790 new QueueTouchEventDelegate(host()->dispatcher()));
3791 TimedEvents tes;
3792 const int kTouchId = 7;
3793 gfx::Rect bounds(0, 0, 1000, 1000);
3794 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3795 delegate.get(), -1234, bounds, root_window()));
3796 delegate->set_window(window.get());
3798 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3799 kTouchId, tes.Now());
3800 DispatchEventUsingWindowDispatcher(&press);
3801 delegate->ReceivedAck();
3803 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
3804 kTouchId, tes.Now());
3805 DispatchEventUsingWindowDispatcher(&move);
3806 delegate->ReceivedAckPreventDefaulted();
3808 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(2, 2),
3809 kTouchId, tes.LeapForward(50));
3810 DispatchEventUsingWindowDispatcher(&release);
3811 delegate->ReceivedAck();
3813 EXPECT_TRUE(delegate->tap());
3816 // Test that consuming the first move touch doesn't prevent a long press.
3817 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveLongPressTest) {
3818 scoped_ptr<QueueTouchEventDelegate> delegate(
3819 new QueueTouchEventDelegate(host()->dispatcher()));
3820 TimedEvents tes;
3821 const int kWindowWidth = 123;
3822 const int kWindowHeight = 45;
3823 const int kTouchId = 2;
3824 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3825 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3826 delegate.get(), -1234, bounds, root_window()));
3827 delegate->set_window(window.get());
3829 delegate->Reset();
3831 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3832 kTouchId, tes.Now());
3833 DispatchEventUsingWindowDispatcher(&press1);
3834 delegate->ReceivedAck();
3836 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103, 203),
3837 kTouchId, tes.Now());
3838 DispatchEventUsingWindowDispatcher(&move);
3839 delegate->ReceivedAckPreventDefaulted();
3841 // Wait until the timer runs out
3842 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
3843 EXPECT_TRUE(delegate->long_press());
3846 // Tests that the deltas are correct when leaving the slop region very slowly.
3847 TEST_F(GestureRecognizerTest, TestExceedingSlopSlowly) {
3848 ui::GestureConfiguration::GetInstance()
3849 ->set_max_touch_move_in_pixels_for_click(3);
3850 scoped_ptr<GestureEventConsumeDelegate> delegate(
3851 new GestureEventConsumeDelegate());
3852 const int kWindowWidth = 234;
3853 const int kWindowHeight = 345;
3854 const int kTouchId = 5;
3855 TimedEvents tes;
3856 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3857 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3858 delegate.get(), -1234, bounds, root_window()));
3860 ui::TouchEvent press(
3861 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), kTouchId, tes.Now());
3862 DispatchEventUsingWindowDispatcher(&press);
3863 EXPECT_FALSE(delegate->scroll_begin());
3864 EXPECT_FALSE(delegate->scroll_update());
3865 delegate->Reset();
3867 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(11, 10), kTouchId,
3868 tes.LeapForward(40));
3869 DispatchEventUsingWindowDispatcher(&move1);
3870 EXPECT_FALSE(delegate->scroll_begin());
3871 EXPECT_FALSE(delegate->scroll_update());
3872 EXPECT_EQ(0, delegate->scroll_x());
3873 EXPECT_EQ(0, delegate->scroll_x_hint());
3874 delegate->Reset();
3876 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(12, 10), kTouchId,
3877 tes.LeapForward(40));
3878 DispatchEventUsingWindowDispatcher(&move2);
3879 EXPECT_FALSE(delegate->scroll_begin());
3880 EXPECT_FALSE(delegate->scroll_update());
3881 EXPECT_EQ(0, delegate->scroll_x());
3882 EXPECT_EQ(0, delegate->scroll_x_hint());
3883 delegate->Reset();
3886 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::PointF(13.1f, 10.f), kTouchId,
3887 tes.LeapForward(40));
3888 DispatchEventUsingWindowDispatcher(&move3);
3889 EXPECT_TRUE(delegate->scroll_begin());
3890 EXPECT_TRUE(delegate->scroll_update());
3891 EXPECT_NEAR(0.1, delegate->scroll_x(), 0.0001);
3892 EXPECT_FLOAT_EQ(3.1f, delegate->scroll_x_hint());
3893 delegate->Reset();
3895 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(14, 10), kTouchId,
3896 tes.LeapForward(40));
3897 DispatchEventUsingWindowDispatcher(&move4);
3898 EXPECT_FALSE(delegate->scroll_begin());
3899 EXPECT_TRUE(delegate->scroll_update());
3900 EXPECT_NEAR(0.9, delegate->scroll_x(), 0.0001);
3901 EXPECT_EQ(0.f, delegate->scroll_x_hint());
3902 delegate->Reset();
3905 TEST_F(GestureRecognizerTest, ScrollAlternatelyConsumedTest) {
3906 scoped_ptr<QueueTouchEventDelegate> delegate(
3907 new QueueTouchEventDelegate(host()->dispatcher()));
3908 TimedEvents tes;
3909 const int kWindowWidth = 3000;
3910 const int kWindowHeight = 3000;
3911 const int kTouchId = 2;
3912 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3913 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3914 delegate.get(), -1234, bounds, root_window()));
3915 delegate->set_window(window.get());
3917 delegate->Reset();
3919 int x = 0;
3920 int y = 0;
3922 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
3923 kTouchId, tes.Now());
3924 DispatchEventUsingWindowDispatcher(&press1);
3925 delegate->ReceivedAck();
3926 EXPECT_FALSE(delegate->scroll_begin());
3927 EXPECT_FALSE(delegate->scroll_update());
3928 delegate->Reset();
3930 x += 100;
3931 y += 100;
3932 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
3933 kTouchId, tes.Now());
3934 DispatchEventUsingWindowDispatcher(&move1);
3935 delegate->ReceivedAck();
3936 EXPECT_TRUE(delegate->scroll_begin());
3937 EXPECT_TRUE(delegate->scroll_update());
3938 delegate->Reset();
3940 for (int i = 0; i < 3; ++i) {
3941 x += 10;
3942 y += 10;
3943 ui::TouchEvent move2(
3944 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
3945 DispatchEventUsingWindowDispatcher(&move2);
3946 delegate->ReceivedAck();
3947 EXPECT_FALSE(delegate->scroll_begin());
3948 EXPECT_TRUE(delegate->scroll_update());
3949 EXPECT_EQ(10, delegate->scroll_x());
3950 EXPECT_EQ(10, delegate->scroll_y());
3951 delegate->Reset();
3953 x += 20;
3954 y += 20;
3955 ui::TouchEvent move3(
3956 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
3957 DispatchEventUsingWindowDispatcher(&move3);
3958 delegate->ReceivedAckPreventDefaulted();
3959 EXPECT_FALSE(delegate->scroll_begin());
3960 EXPECT_FALSE(delegate->scroll_update());
3961 delegate->Reset();
3965 TEST_F(GestureRecognizerTest, PinchAlternatelyConsumedTest) {
3966 scoped_ptr<QueueTouchEventDelegate> delegate(
3967 new QueueTouchEventDelegate(host()->dispatcher()));
3968 TimedEvents tes;
3969 const int kWindowWidth = 3000;
3970 const int kWindowHeight = 3000;
3971 const int kTouchId1 = 5;
3972 const int kTouchId2 = 7;
3973 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3974 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3975 delegate.get(), -1234, bounds, root_window()));
3976 delegate->set_window(window.get());
3977 delegate->Reset();
3979 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3980 kTouchId1, tes.Now());
3981 DispatchEventUsingWindowDispatcher(&press1);
3982 delegate->ReceivedAck();
3983 EXPECT_FALSE(delegate->scroll_begin());
3984 EXPECT_FALSE(delegate->scroll_update());
3985 delegate->Reset();
3987 int x = 0;
3988 int y = 0;
3990 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
3991 kTouchId2, tes.Now());
3992 DispatchEventUsingWindowDispatcher(&press2);
3993 delegate->ReceivedAck();
3994 EXPECT_FALSE(delegate->scroll_begin());
3995 EXPECT_FALSE(delegate->scroll_update());
3996 EXPECT_FALSE(delegate->pinch_begin());
3997 EXPECT_FALSE(delegate->pinch_update());
3999 delegate->Reset();
4001 x += 100;
4002 y += 100;
4003 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
4004 kTouchId2, tes.Now());
4005 DispatchEventUsingWindowDispatcher(&move1);
4006 delegate->ReceivedAck();
4007 EXPECT_TRUE(delegate->scroll_begin());
4008 EXPECT_TRUE(delegate->scroll_update());
4009 EXPECT_TRUE(delegate->pinch_begin());
4010 EXPECT_FALSE(delegate->pinch_update());
4011 delegate->Reset();
4013 const float expected_scales[] = {1.5f, 1.2f, 1.125f};
4015 for (int i = 0; i < 3; ++i) {
4016 x += 50;
4017 y += 50;
4018 ui::TouchEvent move2(
4019 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4020 DispatchEventUsingWindowDispatcher(&move2);
4021 delegate->ReceivedAck();
4022 EXPECT_FALSE(delegate->scroll_begin());
4023 EXPECT_TRUE(delegate->scroll_update());
4024 EXPECT_FALSE(delegate->scroll_end());
4025 EXPECT_FALSE(delegate->pinch_begin());
4026 EXPECT_TRUE(delegate->pinch_update());
4027 EXPECT_FALSE(delegate->pinch_end());
4028 EXPECT_EQ(25, delegate->scroll_x());
4029 EXPECT_EQ(25, delegate->scroll_y());
4030 EXPECT_FLOAT_EQ(expected_scales[i], delegate->scale());
4031 delegate->Reset();
4033 x += 100;
4034 y += 100;
4035 ui::TouchEvent move3(
4036 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4037 DispatchEventUsingWindowDispatcher(&move3);
4038 delegate->ReceivedAckPreventDefaulted();
4039 EXPECT_FALSE(delegate->scroll_begin());
4040 EXPECT_FALSE(delegate->scroll_update());
4041 EXPECT_FALSE(delegate->scroll_end());
4042 EXPECT_FALSE(delegate->pinch_begin());
4043 EXPECT_FALSE(delegate->pinch_update());
4044 EXPECT_FALSE(delegate->pinch_end());
4045 delegate->Reset();
4049 // Test that touch event flags are passed through to the gesture event.
4050 TEST_F(GestureRecognizerTest, GestureEventFlagsPassedFromTouchEvent) {
4051 scoped_ptr<GestureEventConsumeDelegate> delegate(
4052 new GestureEventConsumeDelegate());
4053 TimedEvents tes;
4054 const int kWindowWidth = 123;
4055 const int kWindowHeight = 45;
4056 const int kTouchId = 6;
4057 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4058 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4059 delegate.get(), -1234, bounds, root_window()));
4061 delegate->Reset();
4063 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4064 kTouchId, tes.Now());
4065 DispatchEventUsingWindowDispatcher(&press1);
4066 EXPECT_TRUE(delegate->tap_down());
4068 int default_flags = delegate->flags();
4070 ui::TouchEvent move1(
4071 ui::ET_TOUCH_MOVED, gfx::Point(397, 149), kTouchId, tes.LeapForward(50));
4072 move1.set_flags(992);
4074 DispatchEventUsingWindowDispatcher(&move1);
4075 EXPECT_NE(default_flags, delegate->flags());
4078 // A delegate that deletes a window on long press.
4079 class GestureEventDeleteWindowOnLongPress : public GestureEventConsumeDelegate {
4080 public:
4081 GestureEventDeleteWindowOnLongPress()
4082 : window_(NULL) {}
4084 void set_window(aura::Window** window) { window_ = window; }
4086 void OnGestureEvent(ui::GestureEvent* gesture) override {
4087 GestureEventConsumeDelegate::OnGestureEvent(gesture);
4088 if (gesture->type() != ui::ET_GESTURE_LONG_PRESS)
4089 return;
4090 ui::GestureRecognizer::Get()->CleanupStateForConsumer(*window_);
4091 delete *window_;
4092 *window_ = NULL;
4095 private:
4096 aura::Window** window_;
4097 DISALLOW_COPY_AND_ASSIGN(GestureEventDeleteWindowOnLongPress);
4100 // Check that deleting the window in response to a long press gesture doesn't
4101 // crash.
4102 TEST_F(GestureRecognizerTest, GestureEventLongPressDeletingWindow) {
4103 GestureEventDeleteWindowOnLongPress delegate;
4104 const int kWindowWidth = 123;
4105 const int kWindowHeight = 45;
4106 const int kTouchId = 2;
4107 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4108 aura::Window* window(CreateTestWindowWithDelegate(
4109 &delegate, -1234, bounds, root_window()));
4110 delegate.set_window(&window);
4112 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
4113 gfx::Point(101, 201),
4114 kTouchId,
4115 ui::EventTimeForNow());
4116 DispatchEventUsingWindowDispatcher(&press1);
4117 EXPECT_TRUE(window != NULL);
4119 // Wait until the timer runs out.
4120 delegate.WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4121 EXPECT_EQ(NULL, window);
4124 TEST_F(GestureRecognizerWithSwitchTest, GestureEventSmallPinchDisabled) {
4125 scoped_ptr<GestureEventConsumeDelegate> delegate(
4126 new GestureEventConsumeDelegate());
4127 TimedEvents tes;
4128 const int kWindowWidth = 300;
4129 const int kWindowHeight = 400;
4130 const int kTouchId1 = 3;
4131 const int kTouchId2 = 5;
4132 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4133 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4134 delegate.get(), -1234, bounds, root_window()));
4136 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4137 kTouchId1, tes.Now());
4138 DispatchEventUsingWindowDispatcher(&press1);
4139 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4140 kTouchId2, tes.Now());
4141 DispatchEventUsingWindowDispatcher(&press2);
4143 // Move the first finger.
4144 delegate->Reset();
4145 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4146 kTouchId1, tes.Now());
4147 DispatchEventUsingWindowDispatcher(&move1);
4149 EXPECT_3_EVENTS(delegate->events(),
4150 ui::ET_GESTURE_SCROLL_BEGIN,
4151 ui::ET_GESTURE_SCROLL_UPDATE,
4152 ui::ET_GESTURE_PINCH_BEGIN);
4154 // No pinch update occurs, as kCompensateForUnstablePinchZoom is on and
4155 // |min_pinch_update_span_delta| was nonzero, and this is a very small pinch.
4156 delegate->Reset();
4157 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4158 kTouchId1, tes.Now());
4159 DispatchEventUsingWindowDispatcher(&move2);
4160 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4163 TEST_F(GestureRecognizerTest, GestureEventSmallPinchEnabled) {
4164 scoped_ptr<GestureEventConsumeDelegate> delegate(
4165 new GestureEventConsumeDelegate());
4166 TimedEvents tes;
4167 const int kWindowWidth = 300;
4168 const int kWindowHeight = 400;
4169 const int kTouchId1 = 3;
4170 const int kTouchId2 = 5;
4171 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4172 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4173 delegate.get(), -1234, bounds, root_window()));
4175 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4176 kTouchId1, tes.Now());
4177 DispatchEventUsingWindowDispatcher(&press1);
4178 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4179 kTouchId2, tes.Now());
4180 DispatchEventUsingWindowDispatcher(&press2);
4182 // Move the first finger.
4183 delegate->Reset();
4184 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4185 kTouchId1, tes.Now());
4186 DispatchEventUsingWindowDispatcher(&move1);
4188 EXPECT_3_EVENTS(delegate->events(),
4189 ui::ET_GESTURE_SCROLL_BEGIN,
4190 ui::ET_GESTURE_SCROLL_UPDATE,
4191 ui::ET_GESTURE_PINCH_BEGIN);
4193 delegate->Reset();
4194 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4195 kTouchId1, tes.Now());
4196 DispatchEventUsingWindowDispatcher(&move2);
4197 EXPECT_2_EVENTS(delegate->events(),
4198 ui::ET_GESTURE_SCROLL_UPDATE,
4199 ui::ET_GESTURE_PINCH_UPDATE);
4202 // Tests that delaying the ack of a touch release doesn't trigger a long press
4203 // gesture.
4204 TEST_F(GestureRecognizerTest, EagerGestureDetection) {
4205 scoped_ptr<QueueTouchEventDelegate> delegate(
4206 new QueueTouchEventDelegate(host()->dispatcher()));
4207 TimedEvents tes;
4208 const int kTouchId = 2;
4209 gfx::Rect bounds(100, 200, 100, 100);
4210 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4211 delegate.get(), -1234, bounds, root_window()));
4212 delegate->set_window(window.get());
4214 delegate->Reset();
4215 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4216 kTouchId, tes.Now());
4217 DispatchEventUsingWindowDispatcher(&press);
4218 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
4219 kTouchId, tes.LeapForward(50));
4220 DispatchEventUsingWindowDispatcher(&release);
4222 delegate->Reset();
4223 // Ack the touch press.
4224 delegate->ReceivedAck();
4225 EXPECT_TRUE(delegate->tap_down());
4227 delegate->Reset();
4228 // Wait until the long press event would fire (if we weren't eager).
4229 DelayByLongPressTimeout();
4231 // Ack the touch release.
4232 delegate->ReceivedAck();
4233 EXPECT_TRUE(delegate->tap());
4234 EXPECT_FALSE(delegate->long_press());
4237 // This tests crbug.com/405519, in which touch events which the gesture detector
4238 // ignores interfere with gesture recognition.
4239 TEST_F(GestureRecognizerTest, IgnoredEventsDontBreakGestureRecognition) {
4240 scoped_ptr<QueueTouchEventDelegate> delegate(
4241 new QueueTouchEventDelegate(host()->dispatcher()));
4242 TimedEvents tes;
4243 const int kWindowWidth = 300;
4244 const int kWindowHeight = 400;
4245 const int kTouchId1 = 3;
4246 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4247 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4248 delegate.get(), -1234, bounds, root_window()));
4249 delegate->set_window(window.get());
4251 ui::TouchEvent press1(
4252 ui::ET_TOUCH_PRESSED, gfx::Point(101, 301), kTouchId1, tes.Now());
4253 DispatchEventUsingWindowDispatcher(&press1);
4254 delegate->ReceivedAck();
4256 EXPECT_2_EVENTS(
4257 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
4259 // Move the first finger.
4260 delegate->Reset();
4261 ui::TouchEvent move1(
4262 ui::ET_TOUCH_MOVED, gfx::Point(65, 201), kTouchId1, tes.Now());
4263 DispatchEventUsingWindowDispatcher(&move1);
4264 delegate->ReceivedAck();
4266 EXPECT_3_EVENTS(delegate->events(),
4267 ui::ET_GESTURE_TAP_CANCEL,
4268 ui::ET_GESTURE_SCROLL_BEGIN,
4269 ui::ET_GESTURE_SCROLL_UPDATE);
4271 delegate->Reset();
4273 // Send a valid event, but don't ack it.
4274 ui::TouchEvent move2(
4275 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4276 DispatchEventUsingWindowDispatcher(&move2);
4277 EXPECT_0_EVENTS(delegate->events());
4279 // Send a touchmove event at the same location as the previous touchmove
4280 // event. This shouldn't do anything.
4281 ui::TouchEvent move3(
4282 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4283 DispatchEventUsingWindowDispatcher(&move3);
4285 // Ack the previous valid event. The intermediary invalid event shouldn't
4286 // interfere.
4287 delegate->ReceivedAck();
4288 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4291 // Tests that an event stream can have a mix of sync and async acks.
4292 TEST_F(GestureRecognizerTest,
4293 MixedSyncAndAsyncAcksDontCauseOutOfOrderDispatch) {
4294 scoped_ptr<QueueTouchEventDelegate> delegate(
4295 new QueueTouchEventDelegate(host()->dispatcher()));
4296 TimedEvents tes;
4297 const int kWindowWidth = 300;
4298 const int kWindowHeight = 400;
4299 const int kTouchId1 = 3;
4300 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4301 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4302 delegate.get(), -1234, bounds, root_window()));
4303 delegate->set_window(window.get());
4305 // Start a scroll gesture.
4306 ui::TouchEvent press1(
4307 ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), kTouchId1, tes.Now());
4308 DispatchEventUsingWindowDispatcher(&press1);
4309 delegate->ReceivedAck();
4311 ui::TouchEvent move1(
4312 ui::ET_TOUCH_MOVED, gfx::Point(100, 100), kTouchId1, tes.Now());
4313 DispatchEventUsingWindowDispatcher(&move1);
4314 delegate->ReceivedAck();
4316 delegate->Reset();
4317 // Dispatch a synchronously consumed touch move, which should be ignored.
4318 delegate->set_synchronous_ack_for_next_event(true);
4319 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(200, 200), kTouchId1,
4320 tes.Now());
4321 DispatchEventUsingWindowDispatcher(&move2);
4322 EXPECT_0_EVENTS(delegate->events());
4324 // Dispatch a touch move, but don't ack it.
4325 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(300, 300), kTouchId1,
4326 tes.Now());
4327 DispatchEventUsingWindowDispatcher(&move3);
4329 // Dispatch two synchronously consumed touch moves, which should be ignored.
4330 delegate->set_synchronous_ack_for_next_event(true);
4331 ui::TouchEvent move4(
4332 ui::ET_TOUCH_MOVED, gfx::Point(400, 400), kTouchId1, tes.Now());
4333 DispatchEventUsingWindowDispatcher(&move4);
4335 delegate->set_synchronous_ack_for_next_event(true);
4336 ui::TouchEvent move5(
4337 ui::ET_TOUCH_MOVED, gfx::Point(500, 500), kTouchId1, tes.Now());
4338 DispatchEventUsingWindowDispatcher(&move5);
4340 EXPECT_0_EVENTS(delegate->events());
4341 EXPECT_EQ(100, delegate->bounding_box().x());
4342 // Ack the pending touch move, and ensure the most recent gesture event
4343 // used its co-ordinates.
4344 delegate->ReceivedAck();
4345 EXPECT_EQ(300, delegate->bounding_box().x());
4346 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4348 // Dispatch a touch move, but don't ack it.
4349 delegate->Reset();
4350 ui::TouchEvent move6(ui::ET_TOUCH_MOVED, gfx::Point(600, 600), kTouchId1,
4351 tes.Now());
4352 DispatchEventUsingWindowDispatcher(&move6);
4354 // Dispatch a synchronously unconsumed touch move.
4355 delegate->set_synchronous_ack_for_next_event(false);
4356 ui::TouchEvent move7(
4357 ui::ET_TOUCH_MOVED, gfx::Point(700, 700), kTouchId1, tes.Now());
4358 DispatchEventUsingWindowDispatcher(&move7);
4360 // The synchronous ack is stuck behind the pending touch move.
4361 EXPECT_0_EVENTS(delegate->events());
4363 delegate->ReceivedAck();
4364 EXPECT_2_EVENTS(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE,
4365 ui::ET_GESTURE_SCROLL_UPDATE);
4368 } // namespace test
4369 } // namespace aura