Update V8 to version 4.7.19.
[chromium-blink-merge.git] / ui / aura / gestures / gesture_recognizer_unittest.cc
blobff7ffeb769446650d4d7519ef712da33228cca86
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/run_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/timer/timer.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/aura/env.h"
13 #include "ui/aura/test/aura_test_base.h"
14 #include "ui/aura/test/test_window_delegate.h"
15 #include "ui/aura/test/test_windows.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/base/hit_test.h"
19 #include "ui/base/ui_base_switches.h"
20 #include "ui/events/event.h"
21 #include "ui/events/event_switches.h"
22 #include "ui/events/event_utils.h"
23 #include "ui/events/gesture_detection/gesture_configuration.h"
24 #include "ui/events/gestures/gesture_recognizer_impl.h"
25 #include "ui/events/gestures/gesture_types.h"
26 #include "ui/events/test/event_generator.h"
27 #include "ui/events/test/events_test_utils.h"
28 #include "ui/gfx/geometry/point.h"
29 #include "ui/gfx/geometry/rect.h"
31 namespace aura {
32 namespace test {
34 namespace {
36 std::string WindowIDAsString(ui::GestureConsumer* consumer) {
37 return consumer ?
38 base::IntToString(static_cast<Window*>(consumer)->id()) : "?";
41 #define EXPECT_0_EVENTS(events) \
42 EXPECT_EQ(0u, events.size())
44 #define EXPECT_1_EVENT(events, e0) \
45 EXPECT_EQ(1u, events.size()); \
46 EXPECT_EQ(e0, events[0])
48 #define EXPECT_2_EVENTS(events, e0, e1) \
49 EXPECT_EQ(2u, events.size()); \
50 EXPECT_EQ(e0, events[0]); \
51 EXPECT_EQ(e1, events[1])
53 #define EXPECT_3_EVENTS(events, e0, e1, e2) \
54 EXPECT_EQ(3u, events.size()); \
55 EXPECT_EQ(e0, events[0]); \
56 EXPECT_EQ(e1, events[1]); \
57 EXPECT_EQ(e2, events[2])
59 #define EXPECT_4_EVENTS(events, e0, e1, e2, e3) \
60 EXPECT_EQ(4u, events.size()); \
61 EXPECT_EQ(e0, events[0]); \
62 EXPECT_EQ(e1, events[1]); \
63 EXPECT_EQ(e2, events[2]); \
64 EXPECT_EQ(e3, events[3])
66 // A delegate that keeps track of gesture events.
67 class GestureEventConsumeDelegate : public TestWindowDelegate {
68 public:
69 GestureEventConsumeDelegate()
70 : tap_(false),
71 tap_down_(false),
72 tap_cancel_(false),
73 begin_(false),
74 end_(false),
75 scroll_begin_(false),
76 scroll_update_(false),
77 scroll_end_(false),
78 pinch_begin_(false),
79 pinch_update_(false),
80 pinch_end_(false),
81 long_press_(false),
82 fling_(false),
83 two_finger_tap_(false),
84 show_press_(false),
85 swipe_left_(false),
86 swipe_right_(false),
87 swipe_up_(false),
88 swipe_down_(false),
89 scroll_x_(0),
90 scroll_y_(0),
91 scroll_velocity_x_(0),
92 scroll_velocity_y_(0),
93 velocity_x_(0),
94 velocity_y_(0),
95 scroll_x_hint_(0),
96 scroll_y_hint_(0),
97 tap_count_(0),
98 flags_(0),
99 wait_until_event_(ui::ET_UNKNOWN) {}
101 ~GestureEventConsumeDelegate() override {}
103 void Reset() {
104 events_.clear();
105 tap_ = false;
106 tap_down_ = false;
107 tap_cancel_ = false;
108 begin_ = false;
109 end_ = false;
110 scroll_begin_ = false;
111 scroll_update_ = false;
112 scroll_end_ = false;
113 pinch_begin_ = false;
114 pinch_update_ = false;
115 pinch_end_ = false;
116 long_press_ = false;
117 fling_ = false;
118 two_finger_tap_ = false;
119 show_press_ = false;
120 swipe_left_ = false;
121 swipe_right_ = false;
122 swipe_up_ = false;
123 swipe_down_ = false;
125 scroll_begin_position_.SetPoint(0, 0);
126 tap_location_.SetPoint(0, 0);
127 gesture_end_location_.SetPoint(0, 0);
129 scroll_x_ = 0;
130 scroll_y_ = 0;
131 scroll_velocity_x_ = 0;
132 scroll_velocity_y_ = 0;
133 velocity_x_ = 0;
134 velocity_y_ = 0;
135 scroll_x_hint_ = 0;
136 scroll_y_hint_ = 0;
137 tap_count_ = 0;
138 scale_ = 0;
139 flags_ = 0;
142 const std::vector<ui::EventType>& events() const { return events_; };
144 bool tap() const { return tap_; }
145 bool tap_down() const { return tap_down_; }
146 bool tap_cancel() const { return tap_cancel_; }
147 bool begin() const { return begin_; }
148 bool end() const { return end_; }
149 bool scroll_begin() const { return scroll_begin_; }
150 bool scroll_update() const { return scroll_update_; }
151 bool scroll_end() const { return scroll_end_; }
152 bool pinch_begin() const { return pinch_begin_; }
153 bool pinch_update() const { return pinch_update_; }
154 bool pinch_end() const { return pinch_end_; }
155 bool long_press() const { return long_press_; }
156 bool long_tap() const { return long_tap_; }
157 bool fling() const { return fling_; }
158 bool two_finger_tap() const { return two_finger_tap_; }
159 bool show_press() const { return show_press_; }
160 bool swipe_left() const { return swipe_left_; }
161 bool swipe_right() const { return swipe_right_; }
162 bool swipe_up() const { return swipe_up_; }
163 bool swipe_down() const { return swipe_down_; }
165 const gfx::Point& scroll_begin_position() const {
166 return scroll_begin_position_;
169 const gfx::Point& tap_location() const {
170 return tap_location_;
173 const gfx::Point& gesture_end_location() const {
174 return gesture_end_location_;
177 float scroll_x() const { return scroll_x_; }
178 float scroll_y() const { return scroll_y_; }
179 float scroll_velocity_x() const { return scroll_velocity_x_; }
180 float scroll_velocity_y() const { return scroll_velocity_y_; }
181 float velocity_x() const { return velocity_x_; }
182 float velocity_y() const { return velocity_y_; }
183 float scroll_x_hint() const { return scroll_x_hint_; }
184 float scroll_y_hint() const { return scroll_y_hint_; }
185 float scale() const { return scale_; }
186 const gfx::Rect& bounding_box() const { return bounding_box_; }
187 int tap_count() const { return tap_count_; }
188 int flags() const { return flags_; }
190 void WaitUntilReceivedGesture(ui::EventType type) {
191 wait_until_event_ = type;
192 run_loop_.reset(new base::RunLoop());
193 run_loop_->Run();
196 void OnGestureEvent(ui::GestureEvent* gesture) override {
197 events_.push_back(gesture->type());
198 bounding_box_ = gesture->details().bounding_box();
199 flags_ = gesture->flags();
200 switch (gesture->type()) {
201 case ui::ET_GESTURE_TAP:
202 tap_location_ = gesture->location();
203 tap_count_ = gesture->details().tap_count();
204 tap_ = true;
205 break;
206 case ui::ET_GESTURE_TAP_DOWN:
207 tap_down_ = true;
208 break;
209 case ui::ET_GESTURE_TAP_CANCEL:
210 tap_cancel_ = true;
211 break;
212 case ui::ET_GESTURE_BEGIN:
213 begin_ = true;
214 break;
215 case ui::ET_GESTURE_END:
216 end_ = true;
217 gesture_end_location_ = gesture->location();
218 break;
219 case ui::ET_GESTURE_SCROLL_BEGIN:
220 scroll_begin_ = true;
221 scroll_begin_position_ = gesture->location();
222 scroll_x_hint_ = gesture->details().scroll_x_hint();
223 scroll_y_hint_ = gesture->details().scroll_y_hint();
224 break;
225 case ui::ET_GESTURE_SCROLL_UPDATE:
226 scroll_update_ = true;
227 scroll_x_ += gesture->details().scroll_x();
228 scroll_y_ += gesture->details().scroll_y();
229 break;
230 case ui::ET_GESTURE_SCROLL_END:
231 EXPECT_TRUE(velocity_x_ == 0 && velocity_y_ == 0);
232 scroll_end_ = true;
233 break;
234 case ui::ET_GESTURE_PINCH_BEGIN:
235 pinch_begin_ = true;
236 break;
237 case ui::ET_GESTURE_PINCH_UPDATE:
238 pinch_update_ = true;
239 scale_ = gesture->details().scale();
240 break;
241 case ui::ET_GESTURE_PINCH_END:
242 pinch_end_ = true;
243 break;
244 case ui::ET_GESTURE_LONG_PRESS:
245 long_press_ = true;
246 break;
247 case ui::ET_GESTURE_LONG_TAP:
248 long_tap_ = true;
249 break;
250 case ui::ET_SCROLL_FLING_START:
251 EXPECT_TRUE(gesture->details().velocity_x() != 0 ||
252 gesture->details().velocity_y() != 0);
253 EXPECT_FALSE(scroll_end_);
254 fling_ = true;
255 velocity_x_ = gesture->details().velocity_x();
256 velocity_y_ = gesture->details().velocity_y();
257 break;
258 case ui::ET_GESTURE_TWO_FINGER_TAP:
259 two_finger_tap_ = true;
260 break;
261 case ui::ET_GESTURE_SHOW_PRESS:
262 show_press_ = true;
263 break;
264 case ui::ET_GESTURE_SWIPE:
265 swipe_left_ = gesture->details().swipe_left();
266 swipe_right_ = gesture->details().swipe_right();
267 swipe_up_ = gesture->details().swipe_up();
268 swipe_down_ = gesture->details().swipe_down();
269 break;
270 case ui::ET_SCROLL_FLING_CANCEL:
271 // Only used in unified gesture detection.
272 break;
273 default:
274 NOTREACHED();
276 if (wait_until_event_ == gesture->type() && run_loop_) {
277 run_loop_->Quit();
278 wait_until_event_ = ui::ET_UNKNOWN;
280 gesture->StopPropagation();
283 private:
284 scoped_ptr<base::RunLoop> run_loop_;
285 std::vector<ui::EventType> events_;
287 bool tap_;
288 bool tap_down_;
289 bool tap_cancel_;
290 bool begin_;
291 bool end_;
292 bool scroll_begin_;
293 bool scroll_update_;
294 bool scroll_end_;
295 bool pinch_begin_;
296 bool pinch_update_;
297 bool pinch_end_;
298 bool long_press_;
299 bool long_tap_;
300 bool fling_;
301 bool two_finger_tap_;
302 bool show_press_;
303 bool swipe_left_;
304 bool swipe_right_;
305 bool swipe_up_;
306 bool swipe_down_;
308 gfx::Point scroll_begin_position_;
309 gfx::Point tap_location_;
310 gfx::Point gesture_end_location_;
312 float scroll_x_;
313 float scroll_y_;
314 float scroll_velocity_x_;
315 float scroll_velocity_y_;
316 float velocity_x_;
317 float velocity_y_;
318 float scroll_x_hint_;
319 float scroll_y_hint_;
320 float scale_;
321 gfx::Rect bounding_box_;
322 int tap_count_;
323 int flags_;
325 ui::EventType wait_until_event_;
327 DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate);
330 class QueueTouchEventDelegate : public GestureEventConsumeDelegate {
331 public:
332 explicit QueueTouchEventDelegate(WindowEventDispatcher* dispatcher)
333 : window_(NULL),
334 dispatcher_(dispatcher),
335 synchronous_ack_for_next_event_(AckState::PENDING) {}
337 ~QueueTouchEventDelegate() override {}
339 void OnTouchEvent(ui::TouchEvent* event) override {
340 event->DisableSynchronousHandling();
341 if (synchronous_ack_for_next_event_ != AckState::PENDING) {
342 ui::GestureRecognizer::Get()->AckTouchEvent(
343 event->unique_event_id(),
344 synchronous_ack_for_next_event_ == AckState::CONSUMED
345 ? ui::ER_CONSUMED
346 : ui::ER_UNHANDLED,
347 window_);
348 synchronous_ack_for_next_event_ = AckState::PENDING;
349 } else {
350 sent_events_ids_.push_back(event->unique_event_id());
354 void ReceivedAck() {
355 ReceivedAckImpl(false);
358 void ReceivedAckPreventDefaulted() {
359 ReceivedAckImpl(true);
362 void set_window(Window* w) { window_ = w; }
363 void set_synchronous_ack_for_next_event(bool consumed) {
364 DCHECK(synchronous_ack_for_next_event_ == AckState::PENDING);
365 synchronous_ack_for_next_event_ =
366 consumed ? AckState::CONSUMED : AckState::UNCONSUMED;
369 private:
370 enum class AckState {
371 PENDING,
372 CONSUMED,
373 UNCONSUMED,
376 void ReceivedAckImpl(bool prevent_defaulted) {
377 DCHECK(!sent_events_ids_.empty());
378 if (sent_events_ids_.empty())
379 return;
380 uint32 sent_event_id = sent_events_ids_.front();
381 sent_events_ids_.pop_front();
382 dispatcher_->ProcessedTouchEvent(
383 sent_event_id, window_,
384 prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED);
387 Window* window_;
388 WindowEventDispatcher* dispatcher_;
389 AckState synchronous_ack_for_next_event_;
390 std::list<uint32> sent_events_ids_;
392 DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate);
395 // A delegate that ignores gesture events but keeps track of [synthetic] mouse
396 // events.
397 class GestureEventSynthDelegate : public TestWindowDelegate {
398 public:
399 GestureEventSynthDelegate()
400 : mouse_enter_(false),
401 mouse_exit_(false),
402 mouse_press_(false),
403 mouse_release_(false),
404 mouse_move_(false),
405 double_click_(false) {
408 void Reset() {
409 mouse_enter_ = false;
410 mouse_exit_ = false;
411 mouse_press_ = false;
412 mouse_release_ = false;
413 mouse_move_ = false;
414 double_click_ = false;
417 bool mouse_enter() const { return mouse_enter_; }
418 bool mouse_exit() const { return mouse_exit_; }
419 bool mouse_press() const { return mouse_press_; }
420 bool mouse_move() const { return mouse_move_; }
421 bool mouse_release() const { return mouse_release_; }
422 bool double_click() const { return double_click_; }
424 void OnMouseEvent(ui::MouseEvent* event) override {
425 switch (event->type()) {
426 case ui::ET_MOUSE_PRESSED:
427 double_click_ = event->flags() & ui::EF_IS_DOUBLE_CLICK;
428 mouse_press_ = true;
429 break;
430 case ui::ET_MOUSE_RELEASED:
431 mouse_release_ = true;
432 break;
433 case ui::ET_MOUSE_MOVED:
434 mouse_move_ = true;
435 break;
436 case ui::ET_MOUSE_ENTERED:
437 mouse_enter_ = true;
438 break;
439 case ui::ET_MOUSE_EXITED:
440 mouse_exit_ = true;
441 break;
442 default:
443 NOTREACHED();
445 event->SetHandled();
448 private:
449 bool mouse_enter_;
450 bool mouse_exit_;
451 bool mouse_press_;
452 bool mouse_release_;
453 bool mouse_move_;
454 bool double_click_;
456 DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate);
459 class ScopedGestureRecognizerSetter {
460 public:
461 // Takes ownership of |new_gr|.
462 explicit ScopedGestureRecognizerSetter(ui::GestureRecognizer* new_gr)
463 : new_gr_(new_gr) {
464 original_gr_ = ui::GestureRecognizer::Get();
465 ui::SetGestureRecognizerForTesting(new_gr_.get());
468 virtual ~ScopedGestureRecognizerSetter() {
469 ui::SetGestureRecognizerForTesting(original_gr_);
472 private:
473 ui::GestureRecognizer* original_gr_;
474 scoped_ptr<ui::GestureRecognizer> new_gr_;
476 DISALLOW_COPY_AND_ASSIGN(ScopedGestureRecognizerSetter);
479 class TimedEvents {
480 private:
481 int simulated_now_;
483 public:
484 // Use a non-zero start time to pass DCHECKs which ensure events have had a
485 // time assigned.
486 TimedEvents() : simulated_now_(1) {
489 base::TimeDelta Now() {
490 base::TimeDelta t = base::TimeDelta::FromMilliseconds(simulated_now_);
491 simulated_now_++;
492 return t;
495 base::TimeDelta LeapForward(int time_in_millis) {
496 simulated_now_ += time_in_millis;
497 return base::TimeDelta::FromMilliseconds(simulated_now_);
500 base::TimeDelta InFuture(int time_in_millis) {
501 return base::TimeDelta::FromMilliseconds(simulated_now_ + time_in_millis);
504 void SendScrollEvents(ui::EventProcessor* dispatcher,
505 float x_start,
506 float y_start,
507 int dx,
508 int dy,
509 int touch_id,
510 int time_step,
511 int num_steps,
512 GestureEventConsumeDelegate* delegate) {
513 float x = x_start;
514 float y = y_start;
516 for (int i = 0; i < num_steps; i++) {
517 x += dx;
518 y += dy;
519 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
520 touch_id,
521 base::TimeDelta::FromMilliseconds(simulated_now_));
522 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
523 ASSERT_FALSE(details.dispatcher_destroyed);
524 simulated_now_ += time_step;
528 void SendScrollEvent(ui::EventProcessor* dispatcher,
529 float x,
530 float y,
531 int touch_id,
532 GestureEventConsumeDelegate* delegate) {
533 delegate->Reset();
534 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
535 touch_id,
536 base::TimeDelta::FromMilliseconds(simulated_now_));
537 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
538 ASSERT_FALSE(details.dispatcher_destroyed);
539 simulated_now_++;
543 // An event handler to keep track of events.
544 class TestEventHandler : public ui::EventHandler {
545 public:
546 TestEventHandler()
547 : touch_released_count_(0),
548 touch_pressed_count_(0),
549 touch_moved_count_(0) {}
551 ~TestEventHandler() override {}
553 void OnTouchEvent(ui::TouchEvent* event) override {
554 switch (event->type()) {
555 case ui::ET_TOUCH_RELEASED:
556 touch_released_count_++;
557 break;
558 case ui::ET_TOUCH_PRESSED:
559 touch_pressed_count_++;
560 break;
561 case ui::ET_TOUCH_MOVED:
562 touch_moved_count_++;
563 break;
564 case ui::ET_TOUCH_CANCELLED:
565 cancelled_touch_points_.push_back(event->location());
566 break;
567 default:
568 break;
572 void Reset() {
573 touch_released_count_ = 0;
574 touch_pressed_count_ = 0;
575 touch_moved_count_ = 0;
576 cancelled_touch_points_.clear();
579 int touch_released_count() const { return touch_released_count_; }
580 int touch_pressed_count() const { return touch_pressed_count_; }
581 int touch_moved_count() const { return touch_moved_count_; }
582 int touch_cancelled_count() const {
583 return static_cast<int>(cancelled_touch_points_.size());
585 const std::vector<gfx::PointF>& cancelled_touch_points() const {
586 return cancelled_touch_points_;
589 private:
590 int touch_released_count_;
591 int touch_pressed_count_;
592 int touch_moved_count_;
593 std::vector<gfx::PointF> cancelled_touch_points_;
595 DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
598 // Removes the target window from its parent when it receives a touch-cancel
599 // event.
600 class RemoveOnTouchCancelHandler : public TestEventHandler {
601 public:
602 RemoveOnTouchCancelHandler() {}
603 ~RemoveOnTouchCancelHandler() override {}
605 private:
606 // ui::EventHandler:
607 void OnTouchEvent(ui::TouchEvent* event) override {
608 TestEventHandler::OnTouchEvent(event);
609 if (event->type() == ui::ET_TOUCH_CANCELLED) {
610 Window* target = static_cast<Window*>(event->target());
611 target->parent()->RemoveChild(target);
615 DISALLOW_COPY_AND_ASSIGN(RemoveOnTouchCancelHandler);
618 void DelayByLongPressTimeout() {
619 ui::GestureProvider::Config config;
620 base::RunLoop run_loop;
621 base::MessageLoop::current()->PostDelayedTask(
622 FROM_HERE,
623 run_loop.QuitClosure(),
624 config.gesture_detector_config.longpress_timeout * 2);
625 run_loop.Run();
628 void DelayByShowPressTimeout() {
629 ui::GestureProvider::Config config;
630 base::RunLoop run_loop;
631 base::MessageLoop::current()->PostDelayedTask(
632 FROM_HERE,
633 run_loop.QuitClosure(),
634 config.gesture_detector_config.showpress_timeout * 2);
635 run_loop.Run();
638 } // namespace
640 class GestureRecognizerTest : public AuraTestBase,
641 public ::testing::WithParamInterface<bool> {
642 public:
643 GestureRecognizerTest() {}
645 void SetUp() override {
646 AuraTestBase::SetUp();
647 ui::GestureConfiguration::GetInstance()->set_show_press_delay_in_ms(2);
648 ui::GestureConfiguration::GetInstance()->set_long_press_time_in_ms(3);
651 private:
652 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerTest);
655 class GestureRecognizerWithSwitchTest : public GestureRecognizerTest {
656 public:
657 GestureRecognizerWithSwitchTest() {}
659 void SetUp() override {
660 GestureRecognizerTest::SetUp();
661 base::CommandLine::ForCurrentProcess()->AppendSwitch(
662 switches::kCompensateForUnstablePinchZoom);
663 ui::GestureConfiguration::GetInstance()->set_min_pinch_update_span_delta(5);
666 private:
667 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerWithSwitchTest);
670 // Check that appropriate touch events generate tap gesture events.
671 TEST_F(GestureRecognizerTest, GestureEventTap) {
672 scoped_ptr<GestureEventConsumeDelegate> delegate(
673 new GestureEventConsumeDelegate());
674 TimedEvents tes;
675 const int kWindowWidth = 123;
676 const int kWindowHeight = 45;
677 const int kTouchId = 2;
678 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
679 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
680 delegate.get(), -1234, bounds, root_window()));
682 delegate->Reset();
683 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
684 kTouchId, tes.Now());
685 DispatchEventUsingWindowDispatcher(&press);
686 EXPECT_FALSE(delegate->tap());
687 EXPECT_FALSE(delegate->show_press());
688 EXPECT_TRUE(delegate->tap_down());
689 EXPECT_FALSE(delegate->tap_cancel());
690 EXPECT_TRUE(delegate->begin());
691 EXPECT_FALSE(delegate->scroll_begin());
692 EXPECT_FALSE(delegate->scroll_update());
693 EXPECT_FALSE(delegate->scroll_end());
694 EXPECT_FALSE(delegate->long_press());
696 delegate->Reset();
697 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
698 EXPECT_TRUE(delegate->show_press());
699 EXPECT_FALSE(delegate->tap_down());
701 // Make sure there is enough delay before the touch is released so that it is
702 // recognized as a tap.
703 delegate->Reset();
704 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
705 kTouchId, tes.LeapForward(50));
707 DispatchEventUsingWindowDispatcher(&release);
708 EXPECT_TRUE(delegate->tap());
709 EXPECT_FALSE(delegate->tap_down());
710 EXPECT_FALSE(delegate->tap_cancel());
711 EXPECT_FALSE(delegate->begin());
712 EXPECT_TRUE(delegate->end());
713 EXPECT_FALSE(delegate->scroll_begin());
714 EXPECT_FALSE(delegate->scroll_update());
715 EXPECT_FALSE(delegate->scroll_end());
717 EXPECT_EQ(1, delegate->tap_count());
720 // Check that appropriate touch events generate tap gesture events
721 // when information about the touch radii are provided.
722 TEST_F(GestureRecognizerTest, GestureEventTapRegion) {
723 scoped_ptr<GestureEventConsumeDelegate> delegate(
724 new GestureEventConsumeDelegate());
725 TimedEvents tes;
726 const int kWindowWidth = 800;
727 const int kWindowHeight = 600;
728 const int kTouchId = 2;
729 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
730 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
731 delegate.get(), -1234, bounds, root_window()));
733 // Test with no ET_TOUCH_MOVED events.
735 delegate->Reset();
736 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
737 kTouchId, tes.Now());
738 press.set_radius_x(5);
739 press.set_radius_y(12);
740 DispatchEventUsingWindowDispatcher(&press);
741 EXPECT_FALSE(delegate->tap());
742 EXPECT_TRUE(delegate->tap_down());
743 EXPECT_FALSE(delegate->tap_cancel());
744 EXPECT_TRUE(delegate->begin());
745 EXPECT_FALSE(delegate->scroll_begin());
746 EXPECT_FALSE(delegate->scroll_update());
747 EXPECT_FALSE(delegate->scroll_end());
748 EXPECT_FALSE(delegate->long_press());
750 // Make sure there is enough delay before the touch is released so that it
751 // is recognized as a tap.
752 delegate->Reset();
753 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
754 kTouchId, tes.LeapForward(50));
755 release.set_radius_x(5);
756 release.set_radius_y(12);
758 DispatchEventUsingWindowDispatcher(&release);
759 EXPECT_TRUE(delegate->tap());
760 EXPECT_FALSE(delegate->tap_down());
761 EXPECT_FALSE(delegate->tap_cancel());
762 EXPECT_FALSE(delegate->begin());
763 EXPECT_TRUE(delegate->end());
764 EXPECT_FALSE(delegate->scroll_begin());
765 EXPECT_FALSE(delegate->scroll_update());
766 EXPECT_FALSE(delegate->scroll_end());
768 EXPECT_EQ(1, delegate->tap_count());
769 gfx::Point actual_point(delegate->tap_location());
770 EXPECT_EQ(24, delegate->bounding_box().width());
771 EXPECT_EQ(24, delegate->bounding_box().height());
772 EXPECT_EQ(101, actual_point.x());
773 EXPECT_EQ(201, actual_point.y());
776 // Test with no ET_TOUCH_MOVED events but different touch points and radii.
778 delegate->Reset();
779 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(365, 290),
780 kTouchId, tes.Now());
781 press.set_radius_x(8);
782 press.set_radius_y(14);
783 DispatchEventUsingWindowDispatcher(&press);
784 EXPECT_FALSE(delegate->tap());
785 EXPECT_TRUE(delegate->tap_down());
786 EXPECT_FALSE(delegate->tap_cancel());
787 EXPECT_TRUE(delegate->begin());
788 EXPECT_FALSE(delegate->scroll_begin());
789 EXPECT_FALSE(delegate->scroll_update());
790 EXPECT_FALSE(delegate->scroll_end());
791 EXPECT_FALSE(delegate->long_press());
793 delegate->Reset();
794 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(367, 291),
795 kTouchId, tes.LeapForward(50));
796 release.set_radius_x(20);
797 release.set_radius_y(13);
799 DispatchEventUsingWindowDispatcher(&release);
800 EXPECT_TRUE(delegate->tap());
801 EXPECT_FALSE(delegate->tap_down());
802 EXPECT_FALSE(delegate->tap_cancel());
803 EXPECT_FALSE(delegate->begin());
804 EXPECT_TRUE(delegate->end());
805 EXPECT_FALSE(delegate->scroll_begin());
806 EXPECT_FALSE(delegate->scroll_update());
807 EXPECT_FALSE(delegate->scroll_end());
809 EXPECT_EQ(1, delegate->tap_count());
810 gfx::Point actual_point(delegate->tap_location());
811 EXPECT_EQ(40, delegate->bounding_box().width());
812 EXPECT_EQ(40, delegate->bounding_box().height());
813 EXPECT_EQ(367, actual_point.x());
814 EXPECT_EQ(291, actual_point.y());
817 // Test with a single ET_TOUCH_MOVED event.
819 delegate->Reset();
820 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(46, 205),
821 kTouchId, tes.Now());
822 press.set_radius_x(6);
823 press.set_radius_y(10);
824 DispatchEventUsingWindowDispatcher(&press);
825 EXPECT_FALSE(delegate->tap());
826 EXPECT_TRUE(delegate->tap_down());
827 EXPECT_FALSE(delegate->tap_cancel());
828 EXPECT_TRUE(delegate->begin());
829 EXPECT_FALSE(delegate->tap_cancel());
830 EXPECT_FALSE(delegate->scroll_begin());
831 EXPECT_FALSE(delegate->scroll_update());
832 EXPECT_FALSE(delegate->scroll_end());
833 EXPECT_FALSE(delegate->long_press());
835 delegate->Reset();
836 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(49, 204),
837 kTouchId, tes.LeapForward(50));
838 move.set_radius_x(8);
839 move.set_radius_y(12);
840 DispatchEventUsingWindowDispatcher(&move);
841 EXPECT_FALSE(delegate->tap());
842 EXPECT_FALSE(delegate->tap_down());
843 EXPECT_FALSE(delegate->tap_cancel());
844 EXPECT_FALSE(delegate->begin());
845 EXPECT_FALSE(delegate->scroll_begin());
846 EXPECT_FALSE(delegate->scroll_update());
847 EXPECT_FALSE(delegate->scroll_end());
848 EXPECT_FALSE(delegate->long_press());
850 delegate->Reset();
851 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(49, 204),
852 kTouchId, tes.LeapForward(50));
853 release.set_radius_x(4);
854 release.set_radius_y(8);
856 DispatchEventUsingWindowDispatcher(&release);
857 EXPECT_TRUE(delegate->tap());
858 EXPECT_FALSE(delegate->tap_down());
859 EXPECT_FALSE(delegate->tap_cancel());
860 EXPECT_FALSE(delegate->begin());
861 EXPECT_TRUE(delegate->end());
862 EXPECT_FALSE(delegate->scroll_begin());
863 EXPECT_FALSE(delegate->scroll_update());
864 EXPECT_FALSE(delegate->scroll_end());
866 EXPECT_EQ(1, delegate->tap_count());
867 gfx::Point actual_point(delegate->tap_location());
868 EXPECT_EQ(16, delegate->bounding_box().width());
869 EXPECT_EQ(16, delegate->bounding_box().height());
870 EXPECT_EQ(49, actual_point.x());
871 EXPECT_EQ(204, actual_point.y());
874 // Test with a few ET_TOUCH_MOVED events.
876 delegate->Reset();
877 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(400, 150),
878 kTouchId, tes.Now());
879 press.set_radius_x(7);
880 press.set_radius_y(10);
881 DispatchEventUsingWindowDispatcher(&press);
882 EXPECT_FALSE(delegate->tap());
883 EXPECT_TRUE(delegate->tap_down());
884 EXPECT_FALSE(delegate->tap_cancel());
885 EXPECT_TRUE(delegate->begin());
886 EXPECT_FALSE(delegate->scroll_begin());
887 EXPECT_FALSE(delegate->scroll_update());
888 EXPECT_FALSE(delegate->scroll_end());
889 EXPECT_FALSE(delegate->long_press());
891 delegate->Reset();
892 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(397, 151),
893 kTouchId, tes.LeapForward(50));
894 move.set_radius_x(13);
895 move.set_radius_y(12);
896 DispatchEventUsingWindowDispatcher(&move);
897 EXPECT_FALSE(delegate->tap());
898 EXPECT_FALSE(delegate->tap_down());
899 EXPECT_FALSE(delegate->tap_cancel());
900 EXPECT_FALSE(delegate->begin());
901 EXPECT_FALSE(delegate->scroll_begin());
902 EXPECT_FALSE(delegate->scroll_update());
903 EXPECT_FALSE(delegate->scroll_end());
904 EXPECT_FALSE(delegate->long_press());
906 delegate->Reset();
907 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(397, 149),
908 kTouchId, tes.LeapForward(50));
909 move1.set_radius_x(16);
910 move1.set_radius_y(16);
911 DispatchEventUsingWindowDispatcher(&move1);
912 EXPECT_FALSE(delegate->tap());
913 EXPECT_FALSE(delegate->tap_down());
914 EXPECT_FALSE(delegate->tap_cancel());
915 EXPECT_FALSE(delegate->begin());
916 EXPECT_FALSE(delegate->scroll_begin());
917 EXPECT_FALSE(delegate->scroll_update());
918 EXPECT_FALSE(delegate->scroll_end());
919 EXPECT_FALSE(delegate->long_press());
921 delegate->Reset();
922 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(400, 150),
923 kTouchId, tes.LeapForward(50));
924 move2.set_radius_x(14);
925 move2.set_radius_y(10);
926 DispatchEventUsingWindowDispatcher(&move2);
927 EXPECT_FALSE(delegate->tap());
928 EXPECT_FALSE(delegate->tap_down());
929 EXPECT_FALSE(delegate->tap_cancel());
930 EXPECT_FALSE(delegate->begin());
931 EXPECT_FALSE(delegate->scroll_begin());
932 EXPECT_FALSE(delegate->scroll_update());
933 EXPECT_FALSE(delegate->scroll_end());
934 EXPECT_FALSE(delegate->long_press());
936 delegate->Reset();
937 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(401, 149),
938 kTouchId, tes.LeapForward(50));
939 release.set_radius_x(8);
940 release.set_radius_y(9);
942 DispatchEventUsingWindowDispatcher(&release);
943 EXPECT_TRUE(delegate->tap());
944 EXPECT_FALSE(delegate->tap_down());
945 EXPECT_FALSE(delegate->tap_cancel());
946 EXPECT_FALSE(delegate->begin());
947 EXPECT_TRUE(delegate->end());
948 EXPECT_FALSE(delegate->scroll_begin());
949 EXPECT_FALSE(delegate->scroll_update());
950 EXPECT_FALSE(delegate->scroll_end());
952 EXPECT_EQ(1, delegate->tap_count());
953 gfx::Point actual_point(delegate->tap_location());
954 EXPECT_EQ(18, delegate->bounding_box().width());
955 EXPECT_EQ(18, delegate->bounding_box().height());
956 EXPECT_EQ(401, actual_point.x());
957 EXPECT_EQ(149, actual_point.y());
961 // Check that appropriate touch events generate scroll gesture events.
962 TEST_F(GestureRecognizerTest, GestureEventScroll) {
963 // We'll start by moving the touch point by (10.5, 10.5). We want 5 dips of
964 // that distance to be consumed by the slop, so we set the slop radius to
965 // sqrt(5 * 5 + 5 * 5).
966 ui::GestureConfiguration::GetInstance()
967 ->set_max_touch_move_in_pixels_for_click(sqrt(5.f * 5 + 5 * 5));
968 scoped_ptr<GestureEventConsumeDelegate> delegate(
969 new GestureEventConsumeDelegate());
970 TimedEvents tes;
971 const int kWindowWidth = 123;
972 const int kWindowHeight = 45;
973 const int kTouchId = 5;
974 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
975 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
976 delegate.get(), -1234, bounds, root_window()));
978 delegate->Reset();
979 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
980 kTouchId, tes.Now());
981 DispatchEventUsingWindowDispatcher(&press);
982 EXPECT_2_EVENTS(delegate->events(),
983 ui::ET_GESTURE_BEGIN,
984 ui::ET_GESTURE_TAP_DOWN);
986 // Move the touch-point enough so that it is considered as a scroll. This
987 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
988 // The first movement is diagonal, to ensure that we have a free scroll,
989 // and not a rail scroll.
990 tes.SendScrollEvent(event_processor(), 111.5, 211.5, kTouchId,
991 delegate.get());
992 EXPECT_3_EVENTS(delegate->events(),
993 ui::ET_GESTURE_TAP_CANCEL,
994 ui::ET_GESTURE_SCROLL_BEGIN,
995 ui::ET_GESTURE_SCROLL_UPDATE);
996 // The slop consumed 5 dips
997 EXPECT_FLOAT_EQ(5.5, delegate->scroll_x());
998 EXPECT_FLOAT_EQ(5.5, delegate->scroll_y());
999 EXPECT_EQ(gfx::Point(1, 1).ToString(),
1000 delegate->scroll_begin_position().ToString());
1002 // When scrolling with a single finger, the bounding box of the gesture should
1003 // be empty, since it's a single point and the radius for testing is zero.
1004 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1006 // Move some more to generate a few more scroll updates. Make sure that we get
1007 // out of the snap channel for the unified GR.
1008 tes.SendScrollEvent(event_processor(), 20, 120, kTouchId, delegate.get());
1009 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1010 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_x());
1011 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_y());
1012 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1014 tes.SendScrollEvent(event_processor(), 50, 124, kTouchId, delegate.get());
1015 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1016 EXPECT_EQ(30, delegate->scroll_x());
1017 EXPECT_EQ(4, delegate->scroll_y());
1018 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1020 // Release the touch. This should end the scroll.
1021 delegate->Reset();
1022 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1023 kTouchId,
1024 tes.LeapForward(50));
1025 DispatchEventUsingWindowDispatcher(&release);
1026 EXPECT_2_EVENTS(delegate->events(),
1027 ui::ET_SCROLL_FLING_START,
1028 ui::ET_GESTURE_END);
1029 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1032 // Check that predicted scroll update positions are correct.
1033 TEST_F(GestureRecognizerTest, GestureEventScrollPrediction) {
1034 // We'll start by moving the touch point by (5, 5). We want all of that
1035 // distance to be consumed by the slop, so we set the slop radius to
1036 // sqrt(5 * 5 + 5 * 5).
1037 ui::GestureConfiguration::GetInstance()
1038 ->set_max_touch_move_in_pixels_for_click(sqrt(5.f * 5 + 5 * 5));
1040 scoped_ptr<GestureEventConsumeDelegate> delegate(
1041 new GestureEventConsumeDelegate());
1042 TimedEvents tes;
1043 const int kWindowWidth = 123;
1044 const int kWindowHeight = 45;
1045 const int kTouchId = 5;
1046 gfx::Rect bounds(95, 195, kWindowWidth, kWindowHeight);
1047 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1048 delegate.get(), -1234, bounds, root_window()));
1050 delegate->Reset();
1051 // Tracks the total scroll since we want to verify that the correct position
1052 // will be scrolled to throughout the prediction.
1053 gfx::Vector2dF total_scroll;
1054 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(96, 196),
1055 kTouchId, tes.Now());
1056 DispatchEventUsingWindowDispatcher(&press);
1057 EXPECT_2_EVENTS(delegate->events(),
1058 ui::ET_GESTURE_BEGIN,
1059 ui::ET_GESTURE_TAP_DOWN);
1060 delegate->Reset();
1062 // Get rid of touch slop.
1063 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(111, 211),
1064 kTouchId, tes.Now());
1065 DispatchEventUsingWindowDispatcher(&move);
1066 EXPECT_3_EVENTS(delegate->events(),
1067 ui::ET_GESTURE_TAP_CANCEL,
1068 ui::ET_GESTURE_SCROLL_BEGIN,
1069 ui::ET_GESTURE_SCROLL_UPDATE);
1070 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1071 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1073 // Move the touch-point enough so that it is considered as a scroll. This
1074 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1075 // The first movement is diagonal, to ensure that we have a free scroll,
1076 // and not a rail scroll.
1077 tes.LeapForward(30);
1078 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1079 EXPECT_1_EVENT(delegate->events(),
1080 ui::ET_GESTURE_SCROLL_UPDATE);
1081 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1082 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1084 // Move some more to generate a few more scroll updates.
1085 tes.LeapForward(30);
1086 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
1087 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1088 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1089 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1091 tes.LeapForward(30);
1092 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
1093 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1094 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1095 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1097 // Release the touch. This should end the scroll.
1098 delegate->Reset();
1099 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1100 kTouchId,
1101 tes.LeapForward(50));
1102 DispatchEventUsingWindowDispatcher(&release);
1105 // Check that the bounding box during a scroll event is correct.
1106 TEST_F(GestureRecognizerTest, GestureEventScrollBoundingBox) {
1107 TimedEvents tes;
1108 for (float radius = 1; radius <= 10; ++radius) {
1109 ui::GestureConfiguration::GetInstance()->set_default_radius(radius);
1110 scoped_ptr<GestureEventConsumeDelegate> delegate(
1111 new GestureEventConsumeDelegate());
1112 const int kWindowWidth = 123;
1113 const int kWindowHeight = 45;
1114 const int kTouchId = 5;
1115 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1116 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1117 delegate.get(), -1234, bounds, root_window()));
1119 const float kPositionX = 101;
1120 const float kPositionY = 201;
1121 delegate->Reset();
1122 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
1123 gfx::PointF(kPositionX, kPositionY),
1124 kTouchId,
1125 tes.Now());
1126 DispatchEventUsingWindowDispatcher(&press);
1127 EXPECT_EQ(gfx::RectF(kPositionX - radius,
1128 kPositionY - radius,
1129 radius * 2,
1130 radius * 2),
1131 delegate->bounding_box());
1133 const int kScrollAmount = 50;
1134 tes.SendScrollEvents(event_processor(), kPositionX, kPositionY,
1135 1, 1, kTouchId, 1, kScrollAmount, delegate.get());
1136 EXPECT_EQ(gfx::Point(1, 1).ToString(),
1137 delegate->scroll_begin_position().ToString());
1138 EXPECT_EQ(gfx::RectF(kPositionX + kScrollAmount - radius,
1139 kPositionY + kScrollAmount - radius,
1140 radius * 2,
1141 radius * 2),
1142 delegate->bounding_box());
1144 // Release the touch. This should end the scroll.
1145 delegate->Reset();
1146 ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
1147 gfx::PointF(kPositionX + kScrollAmount,
1148 kPositionY + kScrollAmount),
1149 kTouchId, press.time_stamp() +
1150 base::TimeDelta::FromMilliseconds(50));
1151 DispatchEventUsingWindowDispatcher(&release);
1152 EXPECT_EQ(gfx::RectF(kPositionX + kScrollAmount - radius,
1153 kPositionY + kScrollAmount - radius,
1154 radius * 2,
1155 radius * 2),
1156 delegate->bounding_box());
1158 ui::GestureConfiguration::GetInstance()->set_default_radius(0);
1161 // Check Scroll End Events report correct velocities
1162 // if the user was on a horizontal rail
1163 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailFling) {
1164 scoped_ptr<GestureEventConsumeDelegate> delegate(
1165 new GestureEventConsumeDelegate());
1166 TimedEvents tes;
1167 const int kTouchId = 7;
1168 gfx::Rect bounds(0, 0, 1000, 1000);
1169 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1170 delegate.get(), -1234, bounds, root_window()));
1172 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1173 kTouchId, tes.Now());
1174 DispatchEventUsingWindowDispatcher(&press);
1176 // Get rid of touch slop.
1177 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 0),
1178 kTouchId, tes.Now());
1179 DispatchEventUsingWindowDispatcher(&move);
1180 delegate->Reset();
1183 // Move the touch-point horizontally enough that it is considered a
1184 // horizontal scroll.
1185 tes.SendScrollEvent(event_processor(), 30, 1, kTouchId, delegate.get());
1186 EXPECT_FLOAT_EQ(0, delegate->scroll_y());
1187 EXPECT_FLOAT_EQ(20, delegate->scroll_x());
1189 // Get a high x velocity, while still staying on the rail
1190 const int kScrollAmount = 8;
1191 tes.SendScrollEvents(event_processor(),
1194 100,
1196 kTouchId,
1198 kScrollAmount,
1199 delegate.get());
1201 delegate->Reset();
1202 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1203 kTouchId, tes.Now());
1204 DispatchEventUsingWindowDispatcher(&release);
1206 EXPECT_TRUE(delegate->fling());
1207 EXPECT_FALSE(delegate->scroll_end());
1208 EXPECT_GT(delegate->velocity_x(), 0);
1209 EXPECT_EQ(0, delegate->velocity_y());
1212 // Check Scroll End Events report correct velocities
1213 // if the user was on a vertical rail
1214 TEST_F(GestureRecognizerTest, GestureEventVerticalRailFling) {
1215 scoped_ptr<GestureEventConsumeDelegate> delegate(
1216 new GestureEventConsumeDelegate());
1217 TimedEvents tes;
1218 const int kTouchId = 7;
1219 gfx::Rect bounds(0, 0, 1000, 1000);
1220 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1221 delegate.get(), -1234, bounds, root_window()));
1223 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1224 kTouchId, tes.Now());
1225 DispatchEventUsingWindowDispatcher(&press);
1227 // Get rid of touch slop.
1228 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 10),
1229 kTouchId, tes.Now());
1230 DispatchEventUsingWindowDispatcher(&move);
1231 delegate->Reset();
1233 // Move the touch-point vertically enough that it is considered a
1234 // vertical scroll.
1235 tes.SendScrollEvent(event_processor(), 1, 30, kTouchId, delegate.get());
1236 EXPECT_EQ(20, delegate->scroll_y());
1237 EXPECT_EQ(0, delegate->scroll_x());
1238 EXPECT_EQ(0, delegate->scroll_velocity_x());
1240 // Get a high y velocity, while still staying on the rail
1241 const int kScrollAmount = 8;
1242 tes.SendScrollEvents(event_processor(),
1246 100,
1247 kTouchId,
1249 kScrollAmount,
1250 delegate.get());
1251 EXPECT_EQ(0, delegate->scroll_velocity_x());
1253 delegate->Reset();
1254 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 206),
1255 kTouchId, tes.Now());
1256 DispatchEventUsingWindowDispatcher(&release);
1258 EXPECT_TRUE(delegate->fling());
1259 EXPECT_FALSE(delegate->scroll_end());
1260 EXPECT_EQ(0, delegate->velocity_x());
1261 EXPECT_GT(delegate->velocity_y(), 0);
1264 // Check Scroll End Events report non-zero velocities if the user is not on a
1265 // rail
1266 TEST_F(GestureRecognizerTest, GestureEventNonRailFling) {
1267 ui::GestureConfiguration::GetInstance()
1268 ->set_max_touch_move_in_pixels_for_click(0);
1269 scoped_ptr<GestureEventConsumeDelegate> delegate(
1270 new GestureEventConsumeDelegate());
1271 TimedEvents tes;
1272 const int kTouchId = 7;
1273 gfx::Rect bounds(0, 0, 1000, 1000);
1274 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1275 delegate.get(), -1234, bounds, root_window()));
1277 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1278 kTouchId, tes.Now());
1279 DispatchEventUsingWindowDispatcher(&press);
1281 // Move the touch-point such that a non-rail scroll begins, and we're outside
1282 // the snap channel for the unified GR.
1283 tes.SendScrollEvent(event_processor(), 50, 50, kTouchId, delegate.get());
1284 EXPECT_EQ(50, delegate->scroll_y());
1285 EXPECT_EQ(50, delegate->scroll_x());
1287 const int kScrollAmount = 8;
1288 tes.SendScrollEvents(event_processor(),
1292 100,
1293 kTouchId,
1295 kScrollAmount,
1296 delegate.get());
1298 delegate->Reset();
1299 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1300 kTouchId, tes.Now());
1301 DispatchEventUsingWindowDispatcher(&release);
1303 EXPECT_TRUE(delegate->fling());
1304 EXPECT_FALSE(delegate->scroll_end());
1305 EXPECT_GT(delegate->velocity_x(), 0);
1306 EXPECT_GT(delegate->velocity_y(), 0);
1309 // Check that appropriate touch events generate long press events
1310 TEST_F(GestureRecognizerTest, GestureEventLongPress) {
1311 scoped_ptr<GestureEventConsumeDelegate> delegate(
1312 new GestureEventConsumeDelegate());
1313 const int kWindowWidth = 123;
1314 const int kWindowHeight = 45;
1315 const int kTouchId = 2;
1316 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1317 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1318 delegate.get(), -1234, bounds, root_window()));
1320 delegate->Reset();
1322 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1323 gfx::Point(101, 201),
1324 kTouchId,
1325 ui::EventTimeForNow());
1326 DispatchEventUsingWindowDispatcher(&press1);
1327 EXPECT_TRUE(delegate->tap_down());
1328 EXPECT_TRUE(delegate->begin());
1329 EXPECT_FALSE(delegate->tap_cancel());
1331 // We haven't pressed long enough for a long press to occur
1332 EXPECT_FALSE(delegate->long_press());
1334 // Wait until the timer runs out
1335 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1336 EXPECT_TRUE(delegate->long_press());
1337 EXPECT_FALSE(delegate->tap_cancel());
1339 delegate->Reset();
1340 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1341 gfx::Point(101, 201),
1342 kTouchId,
1343 ui::EventTimeForNow());
1344 DispatchEventUsingWindowDispatcher(&release1);
1345 EXPECT_FALSE(delegate->long_press());
1347 // Note the tap cancel isn't dispatched until the release
1348 EXPECT_TRUE(delegate->tap_cancel());
1349 EXPECT_FALSE(delegate->tap());
1352 // Check that scrolling prevents a long press.
1353 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledByScroll) {
1354 scoped_ptr<GestureEventConsumeDelegate> delegate(
1355 new GestureEventConsumeDelegate());
1356 TimedEvents tes;
1357 const int kWindowWidth = 123;
1358 const int kWindowHeight = 45;
1359 const int kTouchId = 6;
1360 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1361 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1362 delegate.get(), -1234, bounds, root_window()));
1364 delegate->Reset();
1366 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1367 kTouchId, tes.Now());
1368 DispatchEventUsingWindowDispatcher(&press1);
1369 EXPECT_TRUE(delegate->tap_down());
1371 // We haven't pressed long enough for a long press to occur
1372 EXPECT_FALSE(delegate->long_press());
1373 EXPECT_FALSE(delegate->tap_cancel());
1375 // Scroll around, to cancel the long press
1376 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1378 // Wait until a long press event would have fired, if it hadn't been
1379 // cancelled.
1380 DelayByLongPressTimeout();
1382 EXPECT_FALSE(delegate->long_press());
1383 EXPECT_TRUE(delegate->tap_cancel());
1385 delegate->Reset();
1386 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1387 kTouchId, tes.LeapForward(10));
1388 DispatchEventUsingWindowDispatcher(&release1);
1389 EXPECT_FALSE(delegate->long_press());
1390 EXPECT_FALSE(delegate->tap_cancel());
1393 // Check that appropriate touch events generate long tap events
1394 TEST_F(GestureRecognizerTest, GestureEventLongTap) {
1395 ui::GestureConfiguration::GetInstance()
1396 ->set_max_touch_down_duration_for_click_in_ms(3);
1397 scoped_ptr<GestureEventConsumeDelegate> delegate(
1398 new GestureEventConsumeDelegate());
1399 const int kWindowWidth = 123;
1400 const int kWindowHeight = 45;
1401 const int kTouchId = 2;
1402 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1403 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1404 delegate.get(), -1234, bounds, root_window()));
1406 delegate->Reset();
1408 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1409 gfx::Point(101, 201),
1410 kTouchId,
1411 ui::EventTimeForNow());
1412 DispatchEventUsingWindowDispatcher(&press1);
1413 EXPECT_TRUE(delegate->tap_down());
1414 EXPECT_TRUE(delegate->begin());
1415 EXPECT_FALSE(delegate->tap_cancel());
1417 // We haven't pressed long enough for a long press to occur
1418 EXPECT_FALSE(delegate->long_press());
1420 // Wait until the timer runs out
1421 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1422 EXPECT_TRUE(delegate->long_press());
1423 EXPECT_FALSE(delegate->tap_cancel());
1425 delegate->Reset();
1426 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1427 gfx::Point(101, 201),
1428 kTouchId,
1429 ui::EventTimeForNow());
1430 DispatchEventUsingWindowDispatcher(&release1);
1431 EXPECT_FALSE(delegate->long_press());
1432 EXPECT_TRUE(delegate->long_tap());
1434 // Note the tap cancel isn't dispatched until the release
1435 EXPECT_TRUE(delegate->tap_cancel());
1436 EXPECT_FALSE(delegate->tap());
1439 // Check that second tap cancels a long press
1440 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledBySecondTap) {
1441 scoped_ptr<GestureEventConsumeDelegate> delegate(
1442 new GestureEventConsumeDelegate());
1443 TimedEvents tes;
1444 const int kWindowWidth = 300;
1445 const int kWindowHeight = 400;
1446 const int kTouchId1 = 8;
1447 const int kTouchId2 = 2;
1448 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1449 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1450 delegate.get(), -1234, bounds, root_window()));
1452 delegate->Reset();
1453 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1454 kTouchId1, tes.Now());
1455 DispatchEventUsingWindowDispatcher(&press);
1456 EXPECT_TRUE(delegate->tap_down());
1457 EXPECT_TRUE(delegate->begin());
1459 // We haven't pressed long enough for a long press to occur
1460 EXPECT_FALSE(delegate->long_press());
1462 // Second tap, to cancel the long press
1463 delegate->Reset();
1464 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1465 kTouchId2, tes.Now());
1466 DispatchEventUsingWindowDispatcher(&press2);
1467 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
1468 EXPECT_TRUE(delegate->tap_cancel());
1469 EXPECT_TRUE(delegate->begin());
1471 // Wait until the timer runs out
1472 DelayByLongPressTimeout();
1474 // No long press occurred
1475 EXPECT_FALSE(delegate->long_press());
1477 delegate->Reset();
1478 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1479 kTouchId1, tes.Now());
1480 DispatchEventUsingWindowDispatcher(&release1);
1481 EXPECT_FALSE(delegate->long_press());
1482 EXPECT_TRUE(delegate->two_finger_tap());
1483 EXPECT_FALSE(delegate->tap_cancel());
1486 // Check that horizontal scroll gestures cause scrolls on horizontal rails.
1487 // Also tests that horizontal rails can be broken.
1488 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailScroll) {
1489 scoped_ptr<GestureEventConsumeDelegate> delegate(
1490 new GestureEventConsumeDelegate());
1491 TimedEvents tes;
1492 const int kTouchId = 7;
1493 gfx::Rect bounds(0, 0, 1000, 1000);
1494 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1495 delegate.get(), -1234, bounds, root_window()));
1497 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1498 kTouchId, tes.Now());
1499 DispatchEventUsingWindowDispatcher(&press);
1501 // Get rid of touch slop.
1502 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(5, 0),
1503 kTouchId, tes.Now());
1505 DispatchEventUsingWindowDispatcher(&move);
1506 delegate->Reset();
1508 // Move the touch-point horizontally enough that it is considered a
1509 // horizontal scroll.
1510 tes.SendScrollEvent(event_processor(), 25, 0, kTouchId, delegate.get());
1511 EXPECT_EQ(0, delegate->scroll_y());
1512 EXPECT_EQ(20, delegate->scroll_x());
1514 tes.SendScrollEvent(event_processor(), 30, 6, kTouchId, delegate.get());
1515 EXPECT_TRUE(delegate->scroll_update());
1516 EXPECT_EQ(5, delegate->scroll_x());
1517 // y shouldn't change, as we're on a horizontal rail.
1518 EXPECT_EQ(0, delegate->scroll_y());
1520 // Send enough information that a velocity can be calculated for the gesture,
1521 // and we can break the rail
1522 const int kScrollAmount = 8;
1523 tes.SendScrollEvents(event_processor(),
1527 100,
1528 kTouchId,
1530 kScrollAmount,
1531 delegate.get());
1533 tes.SendScrollEvent(event_processor(), 5, 0, kTouchId, delegate.get());
1534 tes.SendScrollEvent(event_processor(), 10, 5, kTouchId, delegate.get());
1536 // The rail should be broken
1537 EXPECT_TRUE(delegate->scroll_update());
1538 EXPECT_EQ(5, delegate->scroll_x());
1539 EXPECT_EQ(5, delegate->scroll_y());
1542 // Check that vertical scroll gestures cause scrolls on vertical rails.
1543 // Also tests that vertical rails can be broken.
1544 TEST_F(GestureRecognizerTest, GestureEventVerticalRailScroll) {
1545 scoped_ptr<GestureEventConsumeDelegate> delegate(
1546 new GestureEventConsumeDelegate());
1547 TimedEvents tes;
1548 const int kTouchId = 7;
1549 gfx::Rect bounds(0, 0, 1000, 1000);
1550 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1551 delegate.get(), -1234, bounds, root_window()));
1553 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1554 kTouchId, tes.Now());
1555 DispatchEventUsingWindowDispatcher(&press);
1557 // Get rid of touch slop.
1558 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 5),
1559 kTouchId, tes.Now());
1560 DispatchEventUsingWindowDispatcher(&move);
1561 delegate->Reset();
1563 // Move the touch-point vertically enough that it is considered a
1564 // vertical scroll.
1565 tes.SendScrollEvent(event_processor(), 0, 25, kTouchId, delegate.get());
1566 EXPECT_EQ(0, delegate->scroll_x());
1567 EXPECT_EQ(20, delegate->scroll_y());
1569 tes.SendScrollEvent(event_processor(), 6, 30, kTouchId, delegate.get());
1570 EXPECT_TRUE(delegate->scroll_update());
1571 EXPECT_EQ(5, delegate->scroll_y());
1572 // x shouldn't change, as we're on a vertical rail.
1573 EXPECT_EQ(0, delegate->scroll_x());
1574 EXPECT_EQ(0, delegate->scroll_velocity_x());
1576 // Send enough information that a velocity can be calculated for the gesture,
1577 // and we can break the rail
1578 const int kScrollAmount = 8;
1579 tes.SendScrollEvents(event_processor(),
1582 100,
1584 kTouchId,
1586 kScrollAmount,
1587 delegate.get());
1589 tes.SendScrollEvent(event_processor(), 0, 5, kTouchId, delegate.get());
1590 tes.SendScrollEvent(event_processor(), 5, 10, kTouchId, delegate.get());
1592 // The rail should be broken
1593 EXPECT_TRUE(delegate->scroll_update());
1594 EXPECT_EQ(5, delegate->scroll_x());
1595 EXPECT_EQ(5, delegate->scroll_y());
1598 TEST_F(GestureRecognizerTest, GestureTapFollowedByScroll) {
1599 // We'll start by moving the touch point by (5, 5). We want all of that
1600 // distance to be consumed by the slop, so we set the slop radius to
1601 // sqrt(5 * 5 + 5 * 5).
1602 ui::GestureConfiguration::GetInstance()
1603 ->set_max_touch_move_in_pixels_for_click(sqrt(5.f * 5 + 5 * 5));
1605 // First, tap. Then, do a scroll using the same touch-id.
1606 scoped_ptr<GestureEventConsumeDelegate> delegate(
1607 new GestureEventConsumeDelegate());
1608 TimedEvents tes;
1609 const int kWindowWidth = 123;
1610 const int kWindowHeight = 45;
1611 const int kTouchId = 3;
1612 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1613 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1614 delegate.get(), -1234, bounds, root_window()));
1616 delegate->Reset();
1617 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1618 kTouchId, tes.Now());
1619 DispatchEventUsingWindowDispatcher(&press);
1620 EXPECT_FALSE(delegate->tap());
1621 EXPECT_TRUE(delegate->tap_down());
1622 EXPECT_FALSE(delegate->tap_cancel());
1623 EXPECT_FALSE(delegate->scroll_begin());
1624 EXPECT_FALSE(delegate->scroll_update());
1625 EXPECT_FALSE(delegate->scroll_end());
1627 // Make sure there is enough delay before the touch is released so that it is
1628 // recognized as a tap.
1629 delegate->Reset();
1630 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1631 kTouchId, tes.LeapForward(50));
1632 DispatchEventUsingWindowDispatcher(&release);
1633 EXPECT_TRUE(delegate->tap());
1634 EXPECT_FALSE(delegate->tap_down());
1635 EXPECT_FALSE(delegate->tap_cancel());
1636 EXPECT_FALSE(delegate->scroll_begin());
1637 EXPECT_FALSE(delegate->scroll_update());
1638 EXPECT_FALSE(delegate->scroll_end());
1640 // Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger
1641 // a double-tap.
1642 delegate->Reset();
1643 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1644 kTouchId, tes.LeapForward(1000));
1645 DispatchEventUsingWindowDispatcher(&press1);
1646 EXPECT_FALSE(delegate->tap());
1647 EXPECT_TRUE(delegate->tap_down());
1648 EXPECT_FALSE(delegate->tap_cancel());
1649 EXPECT_FALSE(delegate->scroll_begin());
1650 EXPECT_FALSE(delegate->scroll_update());
1651 EXPECT_FALSE(delegate->scroll_end());
1653 // Get rid of touch slop.
1654 ui::TouchEvent move_remove_slop(ui::ET_TOUCH_MOVED, gfx::Point(116, 216),
1655 kTouchId, tes.Now());
1656 DispatchEventUsingWindowDispatcher(&move_remove_slop);
1657 EXPECT_TRUE(delegate->tap_cancel());
1658 EXPECT_TRUE(delegate->scroll_begin());
1659 EXPECT_TRUE(delegate->scroll_update());
1660 EXPECT_EQ(15, delegate->scroll_x_hint());
1661 EXPECT_EQ(15, delegate->scroll_y_hint());
1663 delegate->Reset();
1665 // Move the touch-point enough so that it is considered as a scroll. This
1666 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1667 // The first movement is diagonal, to ensure that we have a free scroll,
1668 // and not a rail scroll.
1669 delegate->Reset();
1670 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(135, 235),
1671 kTouchId, tes.Now());
1672 DispatchEventUsingWindowDispatcher(&move);
1673 EXPECT_FALSE(delegate->tap());
1674 EXPECT_FALSE(delegate->tap_down());
1675 EXPECT_FALSE(delegate->tap_cancel());
1676 EXPECT_FALSE(delegate->scroll_begin());
1677 EXPECT_TRUE(delegate->scroll_update());
1678 EXPECT_FALSE(delegate->scroll_end());
1679 EXPECT_EQ(19, delegate->scroll_x());
1680 EXPECT_EQ(19, delegate->scroll_y());
1682 // Move some more to generate a few more scroll updates.
1683 delegate->Reset();
1684 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(115, 216),
1685 kTouchId, tes.Now());
1686 DispatchEventUsingWindowDispatcher(&move1);
1687 EXPECT_FALSE(delegate->tap());
1688 EXPECT_FALSE(delegate->tap_down());
1689 EXPECT_FALSE(delegate->tap_cancel());
1690 EXPECT_FALSE(delegate->scroll_begin());
1691 EXPECT_TRUE(delegate->scroll_update());
1692 EXPECT_FALSE(delegate->scroll_end());
1693 EXPECT_EQ(-20, delegate->scroll_x());
1694 EXPECT_EQ(-19, delegate->scroll_y());
1695 EXPECT_EQ(0, delegate->scroll_x_hint());
1696 EXPECT_EQ(0, delegate->scroll_y_hint());
1698 delegate->Reset();
1699 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 220),
1700 kTouchId, tes.Now());
1701 DispatchEventUsingWindowDispatcher(&move2);
1702 EXPECT_FALSE(delegate->tap());
1703 EXPECT_FALSE(delegate->tap_down());
1704 EXPECT_FALSE(delegate->tap_cancel());
1705 EXPECT_FALSE(delegate->scroll_begin());
1706 EXPECT_TRUE(delegate->scroll_update());
1707 EXPECT_FALSE(delegate->scroll_end());
1708 EXPECT_EQ(30, delegate->scroll_x());
1709 EXPECT_EQ(4, delegate->scroll_y());
1711 // Release the touch. This should end the scroll.
1712 delegate->Reset();
1713 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1714 kTouchId, tes.Now());
1715 DispatchEventUsingWindowDispatcher(&release1);
1716 EXPECT_FALSE(delegate->tap());
1717 EXPECT_FALSE(delegate->tap_down());
1718 EXPECT_FALSE(delegate->tap_cancel());
1719 EXPECT_FALSE(delegate->scroll_begin());
1720 EXPECT_FALSE(delegate->scroll_update());
1721 EXPECT_FALSE(delegate->scroll_end());
1722 EXPECT_TRUE(delegate->fling());
1725 TEST_F(GestureRecognizerTest, AsynchronousGestureRecognition) {
1726 scoped_ptr<QueueTouchEventDelegate> queued_delegate(
1727 new QueueTouchEventDelegate(host()->dispatcher()));
1728 TimedEvents tes;
1729 const int kWindowWidth = 123;
1730 const int kWindowHeight = 45;
1731 const int kTouchId1 = 6;
1732 const int kTouchId2 = 4;
1733 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1734 scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate(
1735 queued_delegate.get(), -1234, bounds, root_window()));
1737 queued_delegate->set_window(queue.get());
1739 // Touch down on the window. This should not generate any gesture event.
1740 queued_delegate->Reset();
1741 ui::TouchEvent press(
1742 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
1743 DispatchEventUsingWindowDispatcher(&press);
1744 EXPECT_FALSE(queued_delegate->tap());
1745 EXPECT_FALSE(queued_delegate->tap_down());
1746 EXPECT_FALSE(queued_delegate->tap_cancel());
1747 EXPECT_FALSE(queued_delegate->begin());
1748 EXPECT_FALSE(queued_delegate->scroll_begin());
1749 EXPECT_FALSE(queued_delegate->scroll_update());
1750 EXPECT_FALSE(queued_delegate->scroll_end());
1752 // Introduce some delay before the touch is released so that it is recognized
1753 // as a tap. However, this still should not create any gesture events.
1754 queued_delegate->Reset();
1755 ui::TouchEvent release(
1756 ui::ET_TOUCH_RELEASED,
1757 gfx::Point(101, 201),
1758 kTouchId1,
1759 press.time_stamp() + base::TimeDelta::FromMilliseconds(50));
1760 DispatchEventUsingWindowDispatcher(&release);
1761 EXPECT_FALSE(queued_delegate->tap());
1762 EXPECT_FALSE(queued_delegate->tap_down());
1763 EXPECT_FALSE(queued_delegate->tap_cancel());
1764 EXPECT_FALSE(queued_delegate->begin());
1765 EXPECT_FALSE(queued_delegate->end());
1766 EXPECT_FALSE(queued_delegate->scroll_begin());
1767 EXPECT_FALSE(queued_delegate->scroll_update());
1768 EXPECT_FALSE(queued_delegate->scroll_end());
1770 // Create another window, and place a touch-down on it. This should create a
1771 // tap-down gesture.
1772 scoped_ptr<GestureEventConsumeDelegate> delegate(
1773 new GestureEventConsumeDelegate());
1774 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1775 delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), root_window()));
1776 delegate->Reset();
1777 ui::TouchEvent press2(
1778 ui::ET_TOUCH_PRESSED, gfx::Point(10, 20), kTouchId2, tes.Now());
1779 DispatchEventUsingWindowDispatcher(&press2);
1780 EXPECT_FALSE(delegate->tap());
1781 EXPECT_TRUE(delegate->tap_down());
1782 EXPECT_FALSE(delegate->tap_cancel());
1783 EXPECT_FALSE(queued_delegate->begin());
1784 EXPECT_FALSE(queued_delegate->end());
1785 EXPECT_FALSE(delegate->scroll_begin());
1786 EXPECT_FALSE(delegate->scroll_update());
1787 EXPECT_FALSE(delegate->scroll_end());
1789 ui::TouchEvent release2(
1790 ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), kTouchId2, tes.Now());
1791 DispatchEventUsingWindowDispatcher(&release2);
1793 // Process the first queued event.
1794 queued_delegate->Reset();
1795 queued_delegate->ReceivedAck();
1796 EXPECT_FALSE(queued_delegate->tap());
1797 EXPECT_TRUE(queued_delegate->tap_down());
1798 EXPECT_TRUE(queued_delegate->begin());
1799 EXPECT_FALSE(queued_delegate->tap_cancel());
1800 EXPECT_FALSE(queued_delegate->end());
1801 EXPECT_FALSE(queued_delegate->scroll_begin());
1802 EXPECT_FALSE(queued_delegate->scroll_update());
1803 EXPECT_FALSE(queued_delegate->scroll_end());
1805 // Now, process the second queued event.
1806 queued_delegate->Reset();
1807 queued_delegate->ReceivedAck();
1808 EXPECT_TRUE(queued_delegate->tap());
1809 EXPECT_FALSE(queued_delegate->tap_down());
1810 EXPECT_FALSE(queued_delegate->tap_cancel());
1811 EXPECT_FALSE(queued_delegate->begin());
1812 EXPECT_TRUE(queued_delegate->end());
1813 EXPECT_FALSE(queued_delegate->scroll_begin());
1814 EXPECT_FALSE(queued_delegate->scroll_update());
1815 EXPECT_FALSE(queued_delegate->scroll_end());
1817 // Start all over. Press on the first window, then press again on the second
1818 // window. The second press should still go to the first window.
1819 queued_delegate->Reset();
1820 ui::TouchEvent press3(
1821 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
1822 DispatchEventUsingWindowDispatcher(&press3);
1823 EXPECT_FALSE(queued_delegate->tap());
1824 EXPECT_FALSE(queued_delegate->tap_down());
1825 EXPECT_FALSE(queued_delegate->tap_cancel());
1826 EXPECT_FALSE(queued_delegate->begin());
1827 EXPECT_FALSE(queued_delegate->end());
1828 EXPECT_FALSE(queued_delegate->begin());
1829 EXPECT_FALSE(queued_delegate->end());
1830 EXPECT_FALSE(queued_delegate->scroll_begin());
1831 EXPECT_FALSE(queued_delegate->scroll_update());
1832 EXPECT_FALSE(queued_delegate->scroll_end());
1834 queued_delegate->Reset();
1835 delegate->Reset();
1836 ui::TouchEvent press4(
1837 ui::ET_TOUCH_PRESSED, gfx::Point(103, 203), kTouchId2, tes.Now());
1838 DispatchEventUsingWindowDispatcher(&press4);
1839 EXPECT_FALSE(delegate->tap());
1840 EXPECT_FALSE(delegate->tap_down());
1841 EXPECT_FALSE(delegate->tap_cancel());
1842 EXPECT_FALSE(delegate->begin());
1843 EXPECT_FALSE(delegate->end());
1844 EXPECT_FALSE(delegate->scroll_begin());
1845 EXPECT_FALSE(delegate->scroll_update());
1846 EXPECT_FALSE(delegate->scroll_end());
1847 EXPECT_FALSE(queued_delegate->tap());
1848 EXPECT_FALSE(queued_delegate->tap_down());
1849 EXPECT_FALSE(queued_delegate->tap_cancel());
1850 EXPECT_FALSE(queued_delegate->begin());
1851 EXPECT_FALSE(queued_delegate->end());
1852 EXPECT_FALSE(queued_delegate->scroll_begin());
1853 EXPECT_FALSE(queued_delegate->scroll_update());
1854 EXPECT_FALSE(queued_delegate->scroll_end());
1856 // Move the second touch-point enough so that it is considered a pinch. This
1857 // should generate both SCROLL_BEGIN and PINCH_BEGIN gestures.
1858 queued_delegate->Reset();
1859 delegate->Reset();
1860 ui::TouchEvent move(
1861 ui::ET_TOUCH_MOVED,
1862 gfx::PointF(203 +
1863 ui::GestureConfiguration::GetInstance()
1864 ->max_touch_move_in_pixels_for_click(),
1865 303),
1866 kTouchId2,
1867 tes.Now());
1868 DispatchEventUsingWindowDispatcher(&move);
1869 EXPECT_FALSE(delegate->tap());
1870 EXPECT_FALSE(delegate->tap_down());
1871 EXPECT_FALSE(delegate->tap_cancel());
1872 EXPECT_FALSE(delegate->begin());
1873 EXPECT_FALSE(delegate->scroll_begin());
1874 EXPECT_FALSE(delegate->scroll_update());
1875 EXPECT_FALSE(delegate->scroll_end());
1876 EXPECT_FALSE(queued_delegate->tap());
1877 EXPECT_FALSE(queued_delegate->tap_down());
1878 EXPECT_FALSE(queued_delegate->tap_cancel());
1879 EXPECT_FALSE(queued_delegate->begin());
1880 EXPECT_FALSE(queued_delegate->scroll_begin());
1881 EXPECT_FALSE(queued_delegate->scroll_update());
1882 EXPECT_FALSE(queued_delegate->scroll_end());
1884 queued_delegate->Reset();
1885 queued_delegate->ReceivedAck();
1886 EXPECT_FALSE(queued_delegate->tap());
1887 EXPECT_TRUE(queued_delegate->tap_down());
1888 EXPECT_TRUE(queued_delegate->begin());
1889 EXPECT_FALSE(queued_delegate->tap_cancel());
1890 EXPECT_FALSE(queued_delegate->end());
1891 EXPECT_FALSE(queued_delegate->scroll_begin());
1892 EXPECT_FALSE(queued_delegate->scroll_update());
1893 EXPECT_FALSE(queued_delegate->scroll_end());
1895 queued_delegate->Reset();
1896 queued_delegate->ReceivedAck();
1897 EXPECT_FALSE(queued_delegate->tap());
1898 EXPECT_FALSE(queued_delegate->tap_down()); // no touch down for second tap.
1899 EXPECT_TRUE(queued_delegate->tap_cancel());
1900 EXPECT_TRUE(queued_delegate->begin());
1901 EXPECT_FALSE(queued_delegate->end());
1902 EXPECT_FALSE(queued_delegate->scroll_begin());
1903 EXPECT_FALSE(queued_delegate->scroll_update());
1904 EXPECT_FALSE(queued_delegate->scroll_end());
1905 EXPECT_FALSE(queued_delegate->pinch_begin());
1906 EXPECT_FALSE(queued_delegate->pinch_update());
1907 EXPECT_FALSE(queued_delegate->pinch_end());
1909 queued_delegate->Reset();
1910 queued_delegate->ReceivedAck();
1911 EXPECT_FALSE(queued_delegate->tap());
1912 EXPECT_FALSE(queued_delegate->tap_down());
1913 EXPECT_FALSE(queued_delegate->tap_cancel());
1914 EXPECT_FALSE(queued_delegate->begin());
1915 EXPECT_FALSE(queued_delegate->end());
1916 EXPECT_TRUE(queued_delegate->scroll_begin());
1918 EXPECT_TRUE(queued_delegate->scroll_update());
1919 EXPECT_FALSE(queued_delegate->scroll_end());
1920 EXPECT_TRUE(queued_delegate->pinch_begin());
1921 EXPECT_FALSE(queued_delegate->pinch_update());
1922 EXPECT_FALSE(queued_delegate->pinch_end());
1925 // Check that appropriate touch events generate pinch gesture events.
1926 TEST_F(GestureRecognizerTest, GestureEventPinchFromScroll) {
1927 scoped_ptr<GestureEventConsumeDelegate> delegate(
1928 new GestureEventConsumeDelegate());
1929 TimedEvents tes;
1930 const int kWindowWidth = 300;
1931 const int kWindowHeight = 400;
1932 const int kTouchId1 = 5;
1933 const int kTouchId2 = 3;
1934 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1935 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1936 delegate.get(), -1234, bounds, root_window()));
1938 delegate->Reset();
1939 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1940 kTouchId1, tes.Now());
1941 DispatchEventUsingWindowDispatcher(&press);
1942 EXPECT_2_EVENTS(delegate->events(),
1943 ui::ET_GESTURE_BEGIN,
1944 ui::ET_GESTURE_TAP_DOWN);
1946 // Move the touch-point enough so that it is considered as a scroll. This
1947 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1948 delegate->Reset();
1949 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 301),
1950 kTouchId1, tes.Now());
1951 DispatchEventUsingWindowDispatcher(&move);
1952 EXPECT_3_EVENTS(delegate->events(),
1953 ui::ET_GESTURE_TAP_CANCEL,
1954 ui::ET_GESTURE_SCROLL_BEGIN,
1955 ui::ET_GESTURE_SCROLL_UPDATE);
1957 // Press the second finger. It should cause pinch-begin. Note that we will not
1958 // transition to two finger tap here because the touch points are far enough.
1959 delegate->Reset();
1960 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1961 kTouchId2, tes.Now());
1962 DispatchEventUsingWindowDispatcher(&press2);
1963 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_BEGIN);
1964 EXPECT_EQ(gfx::Rect(10, 10, 120, 291).ToString(),
1965 delegate->bounding_box().ToString());
1967 // Move the first finger.
1968 delegate->Reset();
1969 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(95, 201),
1970 kTouchId1, tes.Now());
1971 DispatchEventUsingWindowDispatcher(&move3);
1972 EXPECT_2_EVENTS(delegate->events(),
1973 ui::ET_GESTURE_SCROLL_UPDATE,
1974 ui::ET_GESTURE_PINCH_BEGIN);
1975 EXPECT_EQ(gfx::Rect(10, 10, 85, 191).ToString(),
1976 delegate->bounding_box().ToString());
1978 // Now move the second finger.
1979 delegate->Reset();
1980 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
1981 kTouchId2, tes.Now());
1982 DispatchEventUsingWindowDispatcher(&move4);
1983 EXPECT_2_EVENTS(delegate->events(),
1984 ui::ET_GESTURE_SCROLL_UPDATE,
1985 ui::ET_GESTURE_PINCH_UPDATE);
1986 EXPECT_EQ(gfx::Rect(55, 15, 40, 186).ToString(),
1987 delegate->bounding_box().ToString());
1989 // Release the first finger. This should end pinch.
1990 delegate->Reset();
1991 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1992 kTouchId1, tes.Now());
1993 DispatchEventUsingWindowDispatcher(&release);
1994 EXPECT_2_EVENTS(delegate->events(),
1995 ui::ET_GESTURE_PINCH_END,
1996 ui::ET_GESTURE_END);
1997 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
1998 delegate->bounding_box().ToString());
2000 // Move the second finger. This should still generate a scroll.
2001 delegate->Reset();
2002 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2003 kTouchId2, tes.Now());
2004 DispatchEventUsingWindowDispatcher(&move5);
2005 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2006 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2009 TEST_F(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) {
2010 scoped_ptr<GestureEventConsumeDelegate> delegate(
2011 new GestureEventConsumeDelegate());
2012 TimedEvents tes;
2013 const int kWindowWidth = 300;
2014 const int kWindowHeight = 400;
2015 const int kTouchId1 = 5;
2016 const int kTouchId2 = 3;
2017 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2018 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2019 delegate.get(), -1234, bounds, root_window()));
2021 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2022 kTouchId1, tes.Now());
2023 DispatchEventUsingWindowDispatcher(&press);
2024 delegate->Reset();
2025 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2026 kTouchId2, tes.Now());
2027 DispatchEventUsingWindowDispatcher(&press2);
2028 EXPECT_FALSE(delegate->pinch_begin());
2030 // Touch move triggers pinch begin.
2031 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2032 EXPECT_TRUE(delegate->pinch_begin());
2033 EXPECT_FALSE(delegate->pinch_update());
2035 // Touch move triggers pinch update.
2036 tes.SendScrollEvent(event_processor(), 160, 200, kTouchId1, delegate.get());
2037 EXPECT_FALSE(delegate->pinch_begin());
2038 EXPECT_TRUE(delegate->pinch_update());
2040 // Pinch has started, now release the second finger
2041 delegate->Reset();
2042 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2043 kTouchId1, tes.Now());
2044 DispatchEventUsingWindowDispatcher(&release);
2045 EXPECT_TRUE(delegate->pinch_end());
2047 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId2, delegate.get());
2048 EXPECT_TRUE(delegate->scroll_update());
2050 // Pinch again
2051 delegate->Reset();
2052 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2053 kTouchId1, tes.Now());
2054 DispatchEventUsingWindowDispatcher(&press3);
2055 // Now the touch points are close. So we will go into two finger tap.
2056 // Move the touch-point enough to break two-finger-tap and enter pinch.
2057 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 50),
2058 kTouchId1, tes.Now());
2059 DispatchEventUsingWindowDispatcher(&move2);
2060 EXPECT_TRUE(delegate->pinch_begin());
2062 tes.SendScrollEvent(event_processor(), 350, 350, kTouchId1, delegate.get());
2063 EXPECT_TRUE(delegate->pinch_update());
2066 TEST_F(GestureRecognizerTest, GestureEventPinchFromTap) {
2067 scoped_ptr<GestureEventConsumeDelegate> delegate(
2068 new GestureEventConsumeDelegate());
2069 TimedEvents tes;
2070 const int kWindowWidth = 300;
2071 const int kWindowHeight = 400;
2072 const int kTouchId1 = 3;
2073 const int kTouchId2 = 5;
2074 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2075 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2076 delegate.get(), -1234, bounds, root_window()));
2078 delegate->Reset();
2079 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2080 kTouchId1, tes.Now());
2081 DispatchEventUsingWindowDispatcher(&press);
2082 EXPECT_2_EVENTS(delegate->events(),
2083 ui::ET_GESTURE_BEGIN,
2084 ui::ET_GESTURE_TAP_DOWN);
2085 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2087 // Press the second finger far enough to break two finger tap.
2088 delegate->Reset();
2089 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2090 kTouchId2, tes.Now());
2091 DispatchEventUsingWindowDispatcher(&press2);
2092 EXPECT_2_EVENTS(delegate->events(),
2093 ui::ET_GESTURE_TAP_CANCEL,
2094 ui::ET_GESTURE_BEGIN);
2095 EXPECT_EQ(gfx::Rect(10, 10, 91, 291).ToString(),
2096 delegate->bounding_box().ToString());
2098 // Move the first finger.
2099 delegate->Reset();
2100 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
2101 kTouchId1, tes.Now());
2102 DispatchEventUsingWindowDispatcher(&move3);
2103 EXPECT_3_EVENTS(delegate->events(),
2104 ui::ET_GESTURE_SCROLL_BEGIN,
2105 ui::ET_GESTURE_SCROLL_UPDATE,
2106 ui::ET_GESTURE_PINCH_BEGIN);
2107 EXPECT_EQ(gfx::Rect(10, 10, 55, 191).ToString(),
2108 delegate->bounding_box().ToString());
2110 // Now move the second finger.
2111 delegate->Reset();
2112 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
2113 kTouchId2, tes.Now());
2114 DispatchEventUsingWindowDispatcher(&move4);
2115 EXPECT_2_EVENTS(delegate->events(),
2116 ui::ET_GESTURE_SCROLL_UPDATE,
2117 ui::ET_GESTURE_PINCH_UPDATE);
2118 EXPECT_EQ(gfx::Rect(55, 15, 10, 186).ToString(),
2119 delegate->bounding_box().ToString());
2121 // Release the first finger. This should end pinch.
2122 delegate->Reset();
2123 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2124 kTouchId1, tes.LeapForward(10));
2125 DispatchEventUsingWindowDispatcher(&release);
2126 EXPECT_2_EVENTS(delegate->events(),
2127 ui::ET_GESTURE_PINCH_END,
2128 ui::ET_GESTURE_END);
2129 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
2130 delegate->bounding_box().ToString());
2132 // Move the second finger. This should still generate a scroll.
2133 delegate->Reset();
2134 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2135 kTouchId2, tes.Now());
2136 DispatchEventUsingWindowDispatcher(&move5);
2137 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2138 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2141 TEST_F(GestureRecognizerTest, GestureEventIgnoresDisconnectedEvents) {
2142 scoped_ptr<GestureEventConsumeDelegate> delegate(
2143 new GestureEventConsumeDelegate());
2144 TimedEvents tes;
2146 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2147 6, tes.Now());
2148 DispatchEventUsingWindowDispatcher(&release1);
2149 EXPECT_FALSE(delegate->tap());
2150 EXPECT_FALSE(delegate->tap_down());
2153 // Check that a touch is locked to the window of the closest current touch
2154 // within max_separation_for_gesture_touches_in_pixels
2155 TEST_F(GestureRecognizerTest, GestureEventTouchLockSelectsCorrectWindow) {
2156 ui::GestureRecognizer* gesture_recognizer = new ui::GestureRecognizerImpl();
2157 TimedEvents tes;
2158 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2160 ui::GestureConsumer* target;
2161 const int kNumWindows = 4;
2163 scoped_ptr<GestureEventConsumeDelegate*[]> delegates(
2164 new GestureEventConsumeDelegate*[kNumWindows]);
2166 ui::GestureConfiguration::GetInstance()
2167 ->set_max_separation_for_gesture_touches_in_pixels(499);
2169 scoped_ptr<gfx::Rect[]> window_bounds(new gfx::Rect[kNumWindows]);
2170 window_bounds[0] = gfx::Rect(0, 0, 1, 1);
2171 window_bounds[1] = gfx::Rect(500, 0, 1, 1);
2172 window_bounds[2] = gfx::Rect(0, 500, 1, 1);
2173 window_bounds[3] = gfx::Rect(500, 500, 1, 1);
2175 scoped_ptr<aura::Window*[]> windows(new aura::Window*[kNumWindows]);
2177 // Instantiate windows with |window_bounds| and touch each window at
2178 // its origin.
2179 for (int i = 0; i < kNumWindows; ++i) {
2180 delegates[i] = new GestureEventConsumeDelegate();
2181 windows[i] = CreateTestWindowWithDelegate(
2182 delegates[i], i, window_bounds[i], root_window());
2183 windows[i]->set_id(i);
2184 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, window_bounds[i].origin(),
2185 i, tes.Now());
2186 DispatchEventUsingWindowDispatcher(&press);
2189 // Touches should now be associated with the closest touch within
2190 // ui::GestureConfiguration::max_separation_for_gesture_touches_in_pixels
2191 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 11), -1);
2192 EXPECT_EQ("0", WindowIDAsString(target));
2193 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 11), -1);
2194 EXPECT_EQ("1", WindowIDAsString(target));
2195 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 511), -1);
2196 EXPECT_EQ("2", WindowIDAsString(target));
2197 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 511), -1);
2198 EXPECT_EQ("3", WindowIDAsString(target));
2200 // Add a touch in the middle associated with windows[2]
2201 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 500),
2202 kNumWindows, tes.Now());
2203 DispatchEventUsingWindowDispatcher(&press);
2204 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(250, 250),
2205 kNumWindows, tes.Now());
2206 DispatchEventUsingWindowDispatcher(&move);
2208 target = gesture_recognizer->GetTargetForLocation(gfx::Point(250, 250), -1);
2209 EXPECT_EQ("2", WindowIDAsString(target));
2211 // Make sure that ties are broken by distance to a current touch
2212 // Closer to the point in the bottom right.
2213 target = gesture_recognizer->GetTargetForLocation(gfx::Point(380, 380), -1);
2214 EXPECT_EQ("3", WindowIDAsString(target));
2216 // This touch is closer to the point in the middle
2217 target = gesture_recognizer->GetTargetForLocation(gfx::Point(300, 300), -1);
2218 EXPECT_EQ("2", WindowIDAsString(target));
2220 // A touch too far from other touches won't be locked to anything
2221 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2222 EXPECT_TRUE(target == NULL);
2224 // Move a touch associated with windows[2] to 1000, 1000
2225 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(1000, 1000),
2226 kNumWindows, tes.Now());
2227 DispatchEventUsingWindowDispatcher(&move2);
2229 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2230 EXPECT_EQ("2", WindowIDAsString(target));
2232 for (int i = 0; i < kNumWindows; ++i) {
2233 // Delete windows before deleting delegates.
2234 delete windows[i];
2235 delete delegates[i];
2239 // Check that a touch's target will not be effected by a touch on a different
2240 // screen.
2241 TEST_F(GestureRecognizerTest, GestureEventTouchLockIgnoresOtherScreens) {
2242 scoped_ptr<GestureEventConsumeDelegate> delegate(
2243 new GestureEventConsumeDelegate());
2244 gfx::Rect bounds(0, 0, 10, 10);
2245 scoped_ptr<aura::Window> window(
2246 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2248 const int kTouchId1 = 8;
2249 const int kTouchId2 = 2;
2250 TimedEvents tes;
2252 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(5, 5),
2253 kTouchId1, tes.Now());
2254 ui::EventTestApi test_press1(&press1);
2255 test_press1.set_source_device_id(1);
2256 DispatchEventUsingWindowDispatcher(&press1);
2258 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2259 kTouchId2, tes.Now());
2260 ui::EventTestApi test_press2(&press2);
2261 test_press2.set_source_device_id(2);
2262 DispatchEventUsingWindowDispatcher(&press2);
2264 // The second press should not have been locked to the same target as the
2265 // first, as they occured on different displays.
2266 EXPECT_NE(
2267 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1),
2268 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2271 // Check that touch events outside the root window are still handled
2272 // by the root window's gesture sequence.
2273 TEST_F(GestureRecognizerTest, GestureEventOutsideRootWindowTap) {
2274 TimedEvents tes;
2275 scoped_ptr<aura::Window> window(CreateTestWindowWithBounds(
2276 gfx::Rect(-100, -100, 2000, 2000), root_window()));
2278 gfx::Point pos1(-10, -10);
2279 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, pos1, 0, tes.Now());
2280 DispatchEventUsingWindowDispatcher(&press1);
2282 gfx::Point pos2(1000, 1000);
2283 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, pos2, 1, tes.Now());
2284 DispatchEventUsingWindowDispatcher(&press2);
2286 // As these presses were outside the root window, they should be
2287 // associated with the root window.
2288 EXPECT_EQ(root_window(),
2289 static_cast<aura::Window*>(
2290 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1)));
2291 EXPECT_EQ(root_window(),
2292 static_cast<aura::Window*>(
2293 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2)));
2296 TEST_F(GestureRecognizerTest, NoTapWithPreventDefaultedRelease) {
2297 scoped_ptr<QueueTouchEventDelegate> delegate(
2298 new QueueTouchEventDelegate(host()->dispatcher()));
2299 TimedEvents tes;
2300 const int kTouchId = 2;
2301 gfx::Rect bounds(100, 200, 100, 100);
2302 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2303 delegate.get(), -1234, bounds, root_window()));
2304 delegate->set_window(window.get());
2306 delegate->Reset();
2307 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2308 kTouchId, tes.Now());
2309 DispatchEventUsingWindowDispatcher(&press);
2310 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2311 kTouchId, tes.LeapForward(50));
2312 DispatchEventUsingWindowDispatcher(&release);
2314 delegate->Reset();
2315 delegate->ReceivedAck();
2316 EXPECT_TRUE(delegate->tap_down());
2317 delegate->Reset();
2318 delegate->ReceivedAckPreventDefaulted();
2319 EXPECT_FALSE(delegate->tap());
2320 EXPECT_TRUE(delegate->tap_cancel());
2323 TEST_F(GestureRecognizerTest, PinchScrollWithPreventDefaultedRelease) {
2324 scoped_ptr<QueueTouchEventDelegate> delegate(
2325 new QueueTouchEventDelegate(host()->dispatcher()));
2326 TimedEvents tes;
2327 const int kTouchId1 = 7;
2328 const int kTouchId2 = 5;
2329 gfx::Rect bounds(10, 20, 100, 100);
2330 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2331 delegate.get(), -1234, bounds, root_window()));
2332 delegate->set_window(window.get());
2335 delegate->Reset();
2336 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2337 tes.Now());
2338 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2339 tes.LeapForward(200));
2340 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2341 tes.LeapForward(50));
2342 DispatchEventUsingWindowDispatcher(&press);
2343 DispatchEventUsingWindowDispatcher(&move);
2344 DispatchEventUsingWindowDispatcher(&release);
2345 delegate->Reset();
2347 // Ack the press event.
2348 delegate->ReceivedAck();
2349 EXPECT_2_EVENTS(
2350 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2351 delegate->Reset();
2353 // Ack the move event.
2354 delegate->ReceivedAck();
2355 EXPECT_3_EVENTS(delegate->events(),
2356 ui::ET_GESTURE_TAP_CANCEL,
2357 ui::ET_GESTURE_SCROLL_BEGIN,
2358 ui::ET_GESTURE_SCROLL_UPDATE);
2359 delegate->Reset();
2361 // Ack the release event. Although the release event has been processed, it
2362 // should still generate a scroll-end event.
2363 delegate->ReceivedAckPreventDefaulted();
2364 EXPECT_2_EVENTS(
2365 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2368 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2369 tes.Now());
2370 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2371 tes.LeapForward(200));
2372 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2373 tes.LeapForward(50));
2374 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 25), kTouchId2,
2375 tes.Now());
2376 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 85), kTouchId2,
2377 tes.LeapForward(1000));
2378 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(145, 85), kTouchId2,
2379 tes.LeapForward(14));
2381 // Do a pinch.
2382 DispatchEventUsingWindowDispatcher(&press);
2383 DispatchEventUsingWindowDispatcher(&move);
2384 DispatchEventUsingWindowDispatcher(&press2);
2385 DispatchEventUsingWindowDispatcher(&move2);
2386 DispatchEventUsingWindowDispatcher(&release);
2387 DispatchEventUsingWindowDispatcher(&release2);
2389 // Ack the press and move events.
2390 delegate->Reset();
2391 delegate->ReceivedAck();
2392 EXPECT_2_EVENTS(
2393 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2395 delegate->Reset();
2396 delegate->ReceivedAck();
2397 EXPECT_3_EVENTS(delegate->events(),
2398 ui::ET_GESTURE_TAP_CANCEL,
2399 ui::ET_GESTURE_SCROLL_BEGIN,
2400 ui::ET_GESTURE_SCROLL_UPDATE);
2402 delegate->Reset();
2403 delegate->ReceivedAck();
2404 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_BEGIN);
2406 delegate->Reset();
2407 delegate->ReceivedAck();
2408 EXPECT_2_EVENTS(delegate->events(),
2409 ui::ET_GESTURE_SCROLL_UPDATE,
2410 ui::ET_GESTURE_PINCH_BEGIN);
2412 // Ack the first release. Although the release is processed, it should still
2413 // generate a pinch-end event.
2414 delegate->Reset();
2415 delegate->ReceivedAckPreventDefaulted();
2416 EXPECT_2_EVENTS(
2417 delegate->events(), ui::ET_GESTURE_PINCH_END, ui::ET_GESTURE_END);
2419 delegate->Reset();
2420 delegate->ReceivedAckPreventDefaulted();
2421 EXPECT_2_EVENTS(
2422 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2425 TEST_F(GestureRecognizerTest, GestureEndLocation) {
2426 GestureEventConsumeDelegate delegate;
2427 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2428 &delegate, -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2429 ui::test::EventGenerator generator(root_window(), window.get());
2430 const gfx::Point begin(20, 20);
2431 const gfx::Point end(150, 150);
2432 const gfx::Vector2d window_offset =
2433 window->bounds().origin().OffsetFromOrigin();
2434 generator.GestureScrollSequence(begin, end,
2435 base::TimeDelta::FromMilliseconds(20),
2436 10);
2437 EXPECT_EQ((begin - window_offset).ToString(),
2438 delegate.scroll_begin_position().ToString());
2439 EXPECT_EQ((end - window_offset).ToString(),
2440 delegate.gesture_end_location().ToString());
2443 TEST_F(GestureRecognizerTest, CaptureSendsGestureEnd) {
2444 scoped_ptr<GestureEventConsumeDelegate> delegate(
2445 new GestureEventConsumeDelegate());
2446 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2447 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2448 ui::test::EventGenerator generator(root_window());
2450 generator.MoveMouseRelativeTo(window.get(), gfx::Point(10, 10));
2451 generator.PressTouch();
2452 RunAllPendingInMessageLoop();
2454 EXPECT_TRUE(delegate->tap_down());
2456 scoped_ptr<aura::Window> capture(CreateTestWindowWithBounds(
2457 gfx::Rect(10, 10, 200, 200), root_window()));
2458 capture->SetCapture();
2459 RunAllPendingInMessageLoop();
2461 EXPECT_TRUE(delegate->end());
2462 EXPECT_TRUE(delegate->tap_cancel());
2465 // Check that previous touch actions that are completely finished (either
2466 // released or cancelled), do not receive extra synthetic cancels upon change of
2467 // capture.
2468 TEST_F(GestureRecognizerTest, CaptureDoesNotCancelFinishedTouches) {
2469 scoped_ptr<GestureEventConsumeDelegate> delegate(
2470 new GestureEventConsumeDelegate());
2471 scoped_ptr<TestEventHandler> handler(new TestEventHandler);
2472 root_window()->AddPreTargetHandler(handler.get());
2474 // Create a window and set it as the capture window.
2475 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(delegate.get(),
2476 -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2477 window1->SetCapture();
2479 ui::test::EventGenerator generator(root_window());
2480 TimedEvents tes;
2482 // Generate two touch-press events on the window.
2483 scoped_ptr<ui::TouchEvent> touch0(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2484 gfx::Point(20, 20), 0,
2485 tes.Now()));
2486 scoped_ptr<ui::TouchEvent> touch1(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2487 gfx::Point(30, 30), 1,
2488 tes.Now()));
2489 generator.Dispatch(touch0.get());
2490 generator.Dispatch(touch1.get());
2491 RunAllPendingInMessageLoop();
2492 EXPECT_EQ(2, handler->touch_pressed_count());
2494 // Advance time.
2495 tes.LeapForward(1000);
2497 // End the two touches, one by a touch-release and one by a touch-cancel; to
2498 // cover both cases.
2499 touch0.reset(new ui::TouchEvent(ui::ET_TOUCH_RELEASED, gfx::Point(20, 20), 0,
2500 tes.Now()));
2501 touch1.reset(new ui::TouchEvent(ui::ET_TOUCH_CANCELLED, gfx::Point(30, 30), 1,
2502 tes.Now()));
2503 generator.Dispatch(touch0.get());
2504 generator.Dispatch(touch1.get());
2505 RunAllPendingInMessageLoop();
2506 EXPECT_EQ(1, handler->touch_released_count());
2507 EXPECT_EQ(1, handler->touch_cancelled_count());
2509 // Create a new window and set it as the new capture window.
2510 scoped_ptr<aura::Window> window2(CreateTestWindowWithBounds(
2511 gfx::Rect(100, 100, 300, 300), root_window()));
2512 window2->SetCapture();
2513 RunAllPendingInMessageLoop();
2514 // Check that setting capture does not generate any synthetic touch-cancels
2515 // for the two previously finished touch actions.
2516 EXPECT_EQ(1, handler->touch_cancelled_count());
2518 root_window()->RemovePreTargetHandler(handler.get());
2521 // Tests that a press with the same touch id as an existing touch is ignored.
2522 TEST_F(GestureRecognizerTest, PressDoesNotCrash) {
2523 scoped_ptr<GestureEventConsumeDelegate> delegate(
2524 new GestureEventConsumeDelegate());
2525 TimedEvents tes;
2527 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2528 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2530 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(45, 45), 7, tes.Now());
2531 press.set_radius_x(40);
2532 DispatchEventUsingWindowDispatcher(&press);
2533 EXPECT_TRUE(delegate->tap_down());
2534 EXPECT_EQ(gfx::Rect(5, 5, 80, 80).ToString(),
2535 delegate->bounding_box().ToString());
2536 delegate->Reset();
2538 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 45), 7, tes.Now());
2539 DispatchEventUsingWindowDispatcher(&press2);
2541 // FIXME(tdresser): this should not generate a tap down; however,
2542 // there is at least one case where we need to allow a touch press
2543 // from a currently used touch id. See crbug.com/373125 for details.
2544 EXPECT_TRUE(delegate->begin());
2545 EXPECT_TRUE(delegate->tap_down());
2546 EXPECT_TRUE(delegate->tap_cancel());
2547 EXPECT_FALSE(delegate->scroll_begin());
2550 TEST_F(GestureRecognizerTest, TwoFingerTap) {
2551 scoped_ptr<GestureEventConsumeDelegate> delegate(
2552 new GestureEventConsumeDelegate());
2553 const int kWindowWidth = 123;
2554 const int kWindowHeight = 45;
2555 const int kTouchId1 = 2;
2556 const int kTouchId2 = 3;
2557 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2558 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2559 delegate.get(), -1234, bounds, root_window()));
2560 TimedEvents tes;
2562 delegate->Reset();
2563 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2564 kTouchId1, tes.Now());
2565 DispatchEventUsingWindowDispatcher(&press1);
2566 EXPECT_2_EVENTS(
2567 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2569 delegate->Reset();
2570 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2571 kTouchId2, tes.Now());
2572 DispatchEventUsingWindowDispatcher(&press2);
2573 EXPECT_2_EVENTS(
2574 delegate->events(), ui::ET_GESTURE_TAP_CANCEL, ui::ET_GESTURE_BEGIN);
2576 // Little bit of touch move should not affect our state.
2577 delegate->Reset();
2578 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(102, 202),
2579 kTouchId1, tes.Now());
2580 DispatchEventUsingWindowDispatcher(&move1);
2581 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(131, 202),
2582 kTouchId2, tes.Now());
2583 DispatchEventUsingWindowDispatcher(&move2);
2584 EXPECT_3_EVENTS(delegate->events(), ui::ET_GESTURE_SCROLL_BEGIN,
2585 ui::ET_GESTURE_SCROLL_UPDATE, ui::ET_GESTURE_SCROLL_UPDATE);
2587 // Make sure there is enough delay before the touch is released so that it is
2588 // recognized as a tap.
2589 delegate->Reset();
2590 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2591 kTouchId1, tes.LeapForward(50));
2593 DispatchEventUsingWindowDispatcher(&release1);
2594 EXPECT_2_EVENTS(
2595 delegate->events(), ui::ET_GESTURE_TWO_FINGER_TAP, ui::ET_GESTURE_END);
2597 // Lift second finger.
2598 // Make sure there is enough delay before the touch is released so that it is
2599 // recognized as a tap.
2600 delegate->Reset();
2601 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2602 kTouchId2, tes.LeapForward(50));
2604 DispatchEventUsingWindowDispatcher(&release2);
2605 EXPECT_2_EVENTS(
2606 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2609 TEST_F(GestureRecognizerTest, TwoFingerTapExpired) {
2610 scoped_ptr<GestureEventConsumeDelegate> delegate(
2611 new GestureEventConsumeDelegate());
2612 const int kWindowWidth = 123;
2613 const int kWindowHeight = 45;
2614 const int kTouchId1 = 2;
2615 const int kTouchId2 = 3;
2616 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2617 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2618 delegate.get(), -1234, bounds, root_window()));
2619 TimedEvents tes;
2621 delegate->Reset();
2622 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2623 kTouchId1, tes.Now());
2624 DispatchEventUsingWindowDispatcher(&press1);
2626 delegate->Reset();
2627 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2628 kTouchId2, tes.Now());
2629 DispatchEventUsingWindowDispatcher(&press2);
2631 // Send release event after sufficient delay so that two finger time expires.
2632 delegate->Reset();
2633 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2634 kTouchId1, tes.LeapForward(1000));
2636 DispatchEventUsingWindowDispatcher(&release1);
2637 EXPECT_FALSE(delegate->two_finger_tap());
2639 // Lift second finger.
2640 // Make sure there is enough delay before the touch is released so that it is
2641 // recognized as a tap.
2642 delegate->Reset();
2643 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2644 kTouchId2, tes.LeapForward(50));
2646 DispatchEventUsingWindowDispatcher(&release2);
2647 EXPECT_FALSE(delegate->two_finger_tap());
2650 TEST_F(GestureRecognizerTest, TwoFingerTapChangesToPinch) {
2651 scoped_ptr<GestureEventConsumeDelegate> delegate(
2652 new GestureEventConsumeDelegate());
2653 const int kWindowWidth = 123;
2654 const int kWindowHeight = 45;
2655 const int kTouchId1 = 2;
2656 const int kTouchId2 = 3;
2657 TimedEvents tes;
2659 // Test moving first finger
2661 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2662 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2663 delegate.get(), -1234, bounds, root_window()));
2665 delegate->Reset();
2666 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2667 kTouchId1, tes.Now());
2668 DispatchEventUsingWindowDispatcher(&press1);
2670 delegate->Reset();
2671 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2672 kTouchId2, tes.Now());
2673 DispatchEventUsingWindowDispatcher(&press2);
2675 tes.SendScrollEvent(event_processor(), 230, 330, kTouchId1, delegate.get());
2676 EXPECT_FALSE(delegate->two_finger_tap());
2677 EXPECT_TRUE(delegate->pinch_begin());
2679 // Make sure there is enough delay before the touch is released so that it
2680 // is recognized as a tap.
2681 delegate->Reset();
2682 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2683 kTouchId2, tes.LeapForward(50));
2685 DispatchEventUsingWindowDispatcher(&release);
2686 EXPECT_FALSE(delegate->two_finger_tap());
2687 EXPECT_TRUE(delegate->pinch_end());
2690 // Test moving second finger
2692 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2693 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2694 delegate.get(), -1234, bounds, root_window()));
2696 delegate->Reset();
2697 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2698 kTouchId1, tes.Now());
2699 DispatchEventUsingWindowDispatcher(&press1);
2701 delegate->Reset();
2702 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2703 kTouchId2, tes.Now());
2704 DispatchEventUsingWindowDispatcher(&press2);
2706 tes.SendScrollEvent(event_processor(), 301, 230, kTouchId2, delegate.get());
2707 EXPECT_FALSE(delegate->two_finger_tap());
2708 EXPECT_TRUE(delegate->pinch_begin());
2710 // Make sure there is enough delay before the touch is released so that it
2711 // is recognized as a tap.
2712 delegate->Reset();
2713 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2714 kTouchId1, tes.LeapForward(50));
2716 DispatchEventUsingWindowDispatcher(&release);
2717 EXPECT_FALSE(delegate->two_finger_tap());
2718 EXPECT_TRUE(delegate->pinch_end());
2722 TEST_F(GestureRecognizerTest, NoTwoFingerTapWhenFirstFingerHasScrolled) {
2723 scoped_ptr<GestureEventConsumeDelegate> delegate(
2724 new GestureEventConsumeDelegate());
2725 const int kWindowWidth = 123;
2726 const int kWindowHeight = 45;
2727 const int kTouchId1 = 2;
2728 const int kTouchId2 = 3;
2729 TimedEvents tes;
2731 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2732 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2733 delegate.get(), -1234, bounds, root_window()));
2735 delegate->Reset();
2736 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2737 kTouchId1, tes.Now());
2738 DispatchEventUsingWindowDispatcher(&press1);
2739 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2741 delegate->Reset();
2742 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2743 kTouchId2, tes.Now());
2744 DispatchEventUsingWindowDispatcher(&press2);
2746 EXPECT_FALSE(delegate->pinch_begin());
2748 // Make sure there is enough delay before the touch is released so that it
2749 // is recognized as a tap.
2750 delegate->Reset();
2751 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2752 kTouchId2, tes.LeapForward(50));
2754 DispatchEventUsingWindowDispatcher(&release);
2755 EXPECT_FALSE(delegate->two_finger_tap());
2756 EXPECT_FALSE(delegate->pinch_end());
2759 TEST_F(GestureRecognizerTest, MultiFingerSwipe) {
2760 scoped_ptr<GestureEventConsumeDelegate> delegate(
2761 new GestureEventConsumeDelegate());
2762 const int kWindowWidth = 123;
2763 const int kWindowHeight = 45;
2765 gfx::Rect bounds(5, 10, kWindowWidth, kWindowHeight);
2766 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2767 delegate.get(), -1234, bounds, root_window()));
2769 const int kSteps = 15;
2770 const int kTouchPoints = 4;
2771 gfx::Point points[kTouchPoints] = {
2772 gfx::Point(10, 30),
2773 gfx::Point(30, 20),
2774 gfx::Point(50, 30),
2775 gfx::Point(80, 50)
2778 ui::test::EventGenerator generator(root_window(), window.get());
2780 // The unified gesture recognizer assumes a finger has stopped if it hasn't
2781 // moved for too long. See ui/events/gesture_detection/velocity_tracker.cc's
2782 // kAssumePointerStoppedTimeMs.
2783 for (int count = 2; count <= kTouchPoints; ++count) {
2784 generator.GestureMultiFingerScroll(
2785 count, points, 10, kSteps, 0, -11 * kSteps);
2786 EXPECT_TRUE(delegate->swipe_up());
2787 delegate->Reset();
2789 generator.GestureMultiFingerScroll(
2790 count, points, 10, kSteps, 0, 11 * kSteps);
2791 EXPECT_TRUE(delegate->swipe_down());
2792 delegate->Reset();
2794 generator.GestureMultiFingerScroll(
2795 count, points, 10, kSteps, -11 * kSteps, 0);
2796 EXPECT_TRUE(delegate->swipe_left());
2797 delegate->Reset();
2799 generator.GestureMultiFingerScroll(
2800 count, points, 10, kSteps, 11 * kSteps, 0);
2801 EXPECT_TRUE(delegate->swipe_right());
2802 delegate->Reset();
2804 generator.GestureMultiFingerScroll(
2805 count, points, 10, kSteps, 5 * kSteps, 12 * kSteps);
2806 EXPECT_FALSE(delegate->swipe_down());
2807 delegate->Reset();
2809 generator.GestureMultiFingerScroll(
2810 count, points, 10, kSteps, 4 * kSteps, 12 * kSteps);
2811 EXPECT_TRUE(delegate->swipe_down());
2812 delegate->Reset();
2814 generator.GestureMultiFingerScroll(
2815 count, points, 10, kSteps, 3 * kSteps, 12 * kSteps);
2816 EXPECT_TRUE(delegate->swipe_down());
2817 delegate->Reset();
2821 TEST_F(GestureRecognizerTest, TwoFingerTapCancelled) {
2822 scoped_ptr<GestureEventConsumeDelegate> delegate(
2823 new GestureEventConsumeDelegate());
2824 const int kWindowWidth = 123;
2825 const int kWindowHeight = 45;
2826 const int kTouchId1 = 2;
2827 const int kTouchId2 = 3;
2828 TimedEvents tes;
2830 // Test canceling first finger.
2832 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2833 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2834 delegate.get(), -1234, bounds, root_window()));
2836 delegate->Reset();
2837 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2838 kTouchId1, tes.Now());
2839 DispatchEventUsingWindowDispatcher(&press1);
2841 delegate->Reset();
2842 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2843 kTouchId2, tes.Now());
2844 DispatchEventUsingWindowDispatcher(&press2);
2846 delegate->Reset();
2847 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2848 kTouchId1, tes.Now());
2849 DispatchEventUsingWindowDispatcher(&cancel);
2850 EXPECT_FALSE(delegate->two_finger_tap());
2852 // Make sure there is enough delay before the touch is released so that it
2853 // is recognized as a tap.
2854 delegate->Reset();
2855 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2856 kTouchId2, tes.LeapForward(50));
2858 DispatchEventUsingWindowDispatcher(&release);
2859 EXPECT_FALSE(delegate->two_finger_tap());
2862 // Test canceling second finger
2864 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2865 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2866 delegate.get(), -1234, bounds, root_window()));
2868 delegate->Reset();
2869 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2870 kTouchId1, tes.Now());
2871 DispatchEventUsingWindowDispatcher(&press1);
2873 delegate->Reset();
2874 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2875 kTouchId2, tes.Now());
2876 DispatchEventUsingWindowDispatcher(&press2);
2878 delegate->Reset();
2879 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2880 kTouchId2, tes.Now());
2881 DispatchEventUsingWindowDispatcher(&cancel);
2882 EXPECT_FALSE(delegate->two_finger_tap());
2884 // Make sure there is enough delay before the touch is released so that it
2885 // is recognized as a tap.
2886 delegate->Reset();
2887 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2888 kTouchId1, tes.LeapForward(50));
2890 DispatchEventUsingWindowDispatcher(&release);
2891 EXPECT_FALSE(delegate->two_finger_tap());
2895 TEST_F(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) {
2896 scoped_ptr<GestureEventConsumeDelegate> delegate(
2897 new GestureEventConsumeDelegate());
2898 const int kWindowWidth = 523;
2899 const int kWindowHeight = 45;
2900 const int kTouchId1 = 2;
2901 const int kTouchId2 = 3;
2902 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2903 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2904 delegate.get(), -1234, bounds, root_window()));
2905 TimedEvents tes;
2907 delegate->Reset();
2908 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2909 kTouchId1, tes.Now());
2910 DispatchEventUsingWindowDispatcher(&press1);
2911 EXPECT_FALSE(delegate->tap());
2912 EXPECT_TRUE(delegate->tap_down());
2913 EXPECT_FALSE(delegate->tap_cancel());
2914 EXPECT_FALSE(delegate->scroll_begin());
2915 EXPECT_FALSE(delegate->scroll_update());
2916 EXPECT_FALSE(delegate->scroll_end());
2917 EXPECT_FALSE(delegate->long_press());
2918 EXPECT_FALSE(delegate->two_finger_tap());
2920 delegate->Reset();
2921 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201),
2922 kTouchId2, tes.Now());
2923 DispatchEventUsingWindowDispatcher(&press2);
2924 EXPECT_FALSE(delegate->tap());
2925 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
2926 EXPECT_TRUE(delegate->tap_cancel());
2927 EXPECT_FALSE(delegate->scroll_begin());
2928 EXPECT_FALSE(delegate->scroll_update());
2929 EXPECT_FALSE(delegate->scroll_end());
2930 EXPECT_FALSE(delegate->long_press());
2931 EXPECT_FALSE(delegate->two_finger_tap());
2932 EXPECT_FALSE(delegate->pinch_begin());
2934 delegate->Reset();
2935 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(530, 301),
2936 kTouchId2, tes.Now());
2937 DispatchEventUsingWindowDispatcher(&move2);
2938 EXPECT_FALSE(delegate->tap());
2939 EXPECT_FALSE(delegate->tap_down());
2940 EXPECT_FALSE(delegate->tap_cancel());
2941 // Pinch & Scroll only when there is enough movement.
2942 EXPECT_TRUE(delegate->scroll_begin());
2943 EXPECT_TRUE(delegate->scroll_update());
2944 EXPECT_FALSE(delegate->scroll_end());
2945 EXPECT_FALSE(delegate->long_press());
2946 EXPECT_FALSE(delegate->two_finger_tap());
2947 EXPECT_TRUE(delegate->pinch_begin());
2950 // Verifies if a window is the target of multiple touch-ids and we hide the
2951 // window everything is cleaned up correctly.
2952 TEST_F(GestureRecognizerTest, FlushAllOnHide) {
2953 scoped_ptr<GestureEventConsumeDelegate> delegate(
2954 new GestureEventConsumeDelegate());
2955 gfx::Rect bounds(0, 0, 200, 200);
2956 scoped_ptr<aura::Window> window(
2957 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2958 const int kTouchId1 = 8;
2959 const int kTouchId2 = 2;
2960 TimedEvents tes;
2962 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2963 kTouchId1, tes.Now());
2964 DispatchEventUsingWindowDispatcher(&press1);
2965 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2966 kTouchId2, tes.Now());
2967 DispatchEventUsingWindowDispatcher(&press2);
2968 window->Hide();
2969 EXPECT_EQ(NULL,
2970 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1));
2971 EXPECT_EQ(NULL,
2972 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2975 TEST_F(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) {
2976 scoped_ptr<QueueTouchEventDelegate> delegate(
2977 new QueueTouchEventDelegate(host()->dispatcher()));
2978 const int kTouchId = 2;
2979 gfx::Rect bounds(100, 200, 100, 100);
2980 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2981 delegate.get(), -1234, bounds, root_window()));
2982 delegate->set_window(window.get());
2983 TimedEvents tes;
2985 delegate->Reset();
2986 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2987 kTouchId, tes.Now());
2988 DispatchEventUsingWindowDispatcher(&press);
2989 // Scroll around, to cancel the long press
2990 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
2992 delegate->Reset();
2993 delegate->ReceivedAck();
2994 EXPECT_TRUE(delegate->tap_down());
2996 // Wait long enough that long press would have fired if the touchmove hadn't
2997 // prevented it.
2998 DelayByLongPressTimeout();
3000 delegate->Reset();
3001 delegate->ReceivedAckPreventDefaulted();
3002 EXPECT_FALSE(delegate->long_press());
3005 // Same as GestureEventConsumeDelegate, but consumes all the touch-move events.
3006 class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate {
3007 public:
3008 ConsumesTouchMovesDelegate() : consume_touch_move_(true) {}
3009 ~ConsumesTouchMovesDelegate() override {}
3011 void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; }
3013 private:
3014 void OnTouchEvent(ui::TouchEvent* touch) override {
3015 if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED)
3016 touch->SetHandled();
3017 else
3018 GestureEventConsumeDelegate::OnTouchEvent(touch);
3021 bool consume_touch_move_;
3023 DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate);
3026 // Same as GestureEventScroll, but tests that the behavior is the same
3027 // even if all the touch-move events are consumed.
3028 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) {
3029 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3030 new ConsumesTouchMovesDelegate());
3031 const int kWindowWidth = 123;
3032 const int kWindowHeight = 45;
3033 const int kTouchId = 5;
3034 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3035 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3036 delegate.get(), -1234, bounds, root_window()));
3037 TimedEvents tes;
3039 delegate->Reset();
3040 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3041 kTouchId, tes.Now());
3042 DispatchEventUsingWindowDispatcher(&press);
3043 EXPECT_FALSE(delegate->tap());
3044 EXPECT_TRUE(delegate->tap_down());
3045 EXPECT_FALSE(delegate->tap_cancel());
3046 EXPECT_TRUE(delegate->begin());
3047 EXPECT_FALSE(delegate->scroll_begin());
3048 EXPECT_FALSE(delegate->scroll_update());
3049 EXPECT_FALSE(delegate->scroll_end());
3051 // Move the touch-point enough so that it would normally be considered a
3052 // scroll. But since the touch-moves will be consumed, the scroll should not
3053 // start.
3054 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3055 EXPECT_FALSE(delegate->tap());
3056 EXPECT_FALSE(delegate->tap_down());
3057 EXPECT_TRUE(delegate->tap_cancel());
3058 EXPECT_FALSE(delegate->begin());
3059 EXPECT_FALSE(delegate->scroll_update());
3060 EXPECT_FALSE(delegate->scroll_end());
3062 EXPECT_TRUE(delegate->scroll_begin());
3064 // Release the touch back at the start point. This should end without causing
3065 // a tap.
3066 delegate->Reset();
3067 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230),
3068 kTouchId, tes.LeapForward(50));
3069 DispatchEventUsingWindowDispatcher(&release);
3070 EXPECT_FALSE(delegate->tap());
3071 EXPECT_FALSE(delegate->tap_down());
3072 EXPECT_FALSE(delegate->tap_cancel());
3073 EXPECT_FALSE(delegate->begin());
3074 EXPECT_TRUE(delegate->end());
3075 EXPECT_FALSE(delegate->scroll_begin());
3076 EXPECT_FALSE(delegate->scroll_update());
3078 EXPECT_TRUE(delegate->scroll_end());
3081 // Tests the behavior of 2F scroll when some of the touch-move events are
3082 // consumed.
3083 TEST_F(GestureRecognizerTest, GestureEventScrollTwoFingerTouchMoveConsumed) {
3084 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3085 new ConsumesTouchMovesDelegate());
3086 const int kWindowWidth = 123;
3087 const int kWindowHeight = 100;
3088 const int kTouchId1 = 2;
3089 const int kTouchId2 = 3;
3090 TimedEvents tes;
3092 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3093 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3094 delegate.get(), -1234, bounds, root_window()));
3096 delegate->Reset();
3097 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3098 kTouchId1, tes.Now());
3099 DispatchEventUsingWindowDispatcher(&press1);
3100 tes.SendScrollEvent(event_processor(), 131, 231, kTouchId1, delegate.get());
3102 EXPECT_2_EVENTS(delegate->events(),
3103 ui::ET_GESTURE_TAP_CANCEL,
3104 ui::ET_GESTURE_SCROLL_BEGIN);
3106 delegate->Reset();
3107 // Second finger touches down and moves.
3108 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
3109 kTouchId2, tes.LeapForward(50));
3110 DispatchEventUsingWindowDispatcher(&press2);
3111 tes.SendScrollEvent(event_processor(), 161, 231, kTouchId2, delegate.get());
3112 EXPECT_0_EVENTS(delegate->events());
3114 delegate->Reset();
3115 // Move first finger again, no PinchUpdate & ScrollUpdate.
3116 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId1, delegate.get());
3117 EXPECT_0_EVENTS(delegate->events());
3119 // Stops consuming touch-move.
3120 delegate->set_consume_touch_move(false);
3122 delegate->Reset();
3123 // Making a pinch gesture.
3124 tes.SendScrollEvent(event_processor(), 161, 260, kTouchId1, delegate.get());
3125 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
3127 delegate->Reset();
3128 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId2, delegate.get());
3129 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
3131 delegate->Reset();
3132 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3133 kTouchId1, tes.Now());
3134 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
3135 kTouchId2, tes.Now());
3136 DispatchEventUsingWindowDispatcher(&release1);
3137 DispatchEventUsingWindowDispatcher(&release2);
3139 EXPECT_3_EVENTS(delegate->events(),
3140 ui::ET_GESTURE_END,
3141 ui::ET_SCROLL_FLING_START,
3142 ui::ET_GESTURE_END);
3145 // Like as GestureEventTouchMoveConsumed but tests the different behavior
3146 // depending on whether the events were consumed before or after the scroll
3147 // started.
3148 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) {
3149 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3150 new ConsumesTouchMovesDelegate());
3151 const int kWindowWidth = 123;
3152 const int kWindowHeight = 45;
3153 const int kTouchId = 5;
3154 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3155 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3156 delegate.get(), -1234, bounds, root_window()));
3157 TimedEvents tes;
3159 delegate->Reset();
3160 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3161 kTouchId, tes.Now());
3162 DispatchEventUsingWindowDispatcher(&press);
3163 EXPECT_FALSE(delegate->tap());
3164 EXPECT_TRUE(delegate->tap_down());
3165 EXPECT_FALSE(delegate->tap_cancel());
3166 EXPECT_TRUE(delegate->begin());
3167 EXPECT_FALSE(delegate->scroll_begin());
3168 EXPECT_FALSE(delegate->scroll_update());
3169 EXPECT_FALSE(delegate->scroll_end());
3171 // Move the touch-point enough so that it would normally be considered a
3172 // scroll. But since the touch-moves will be consumed, the scroll should not
3173 // start.
3174 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3175 EXPECT_FALSE(delegate->tap());
3176 EXPECT_FALSE(delegate->tap_down());
3177 EXPECT_TRUE(delegate->tap_cancel());
3178 EXPECT_FALSE(delegate->begin());
3179 EXPECT_FALSE(delegate->scroll_update());
3180 EXPECT_FALSE(delegate->scroll_end());
3182 // Consuming the first touch move event won't prevent all future scrolling.
3183 EXPECT_TRUE(delegate->scroll_begin());
3185 // Now, stop consuming touch-move events, and move the touch-point again.
3186 delegate->set_consume_touch_move(false);
3187 tes.SendScrollEvent(event_processor(), 159, 259, kTouchId, delegate.get());
3188 EXPECT_FALSE(delegate->tap());
3189 EXPECT_FALSE(delegate->tap_down());
3190 EXPECT_FALSE(delegate->tap_cancel());
3191 EXPECT_FALSE(delegate->begin());
3192 EXPECT_FALSE(delegate->scroll_begin());
3193 EXPECT_FALSE(delegate->scroll_end());
3195 // Scroll not prevented by consumed first touch move.
3196 EXPECT_TRUE(delegate->scroll_update());
3197 EXPECT_EQ(29, delegate->scroll_x());
3198 EXPECT_EQ(29, delegate->scroll_y());
3199 EXPECT_EQ(gfx::Point(0, 0).ToString(),
3200 delegate->scroll_begin_position().ToString());
3202 // Start consuming touch-move events again.
3203 delegate->set_consume_touch_move(true);
3205 // Move some more to generate a few more scroll updates.
3206 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
3207 EXPECT_FALSE(delegate->tap());
3208 EXPECT_FALSE(delegate->tap_down());
3209 EXPECT_FALSE(delegate->tap_cancel());
3210 EXPECT_FALSE(delegate->begin());
3211 EXPECT_FALSE(delegate->scroll_begin());
3212 EXPECT_FALSE(delegate->scroll_update());
3213 EXPECT_FALSE(delegate->scroll_end());
3214 EXPECT_EQ(0, delegate->scroll_x());
3215 EXPECT_EQ(0, delegate->scroll_y());
3217 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
3218 EXPECT_FALSE(delegate->tap());
3219 EXPECT_FALSE(delegate->tap_down());
3220 EXPECT_FALSE(delegate->tap_cancel());
3221 EXPECT_FALSE(delegate->begin());
3222 EXPECT_FALSE(delegate->scroll_begin());
3223 EXPECT_FALSE(delegate->scroll_update());
3224 EXPECT_FALSE(delegate->scroll_end());
3225 EXPECT_EQ(0, delegate->scroll_x());
3226 EXPECT_EQ(0, delegate->scroll_y());
3228 // Release the touch.
3229 delegate->Reset();
3230 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3231 kTouchId, tes.LeapForward(50));
3232 DispatchEventUsingWindowDispatcher(&release);
3233 EXPECT_FALSE(delegate->tap());
3234 EXPECT_FALSE(delegate->tap_down());
3235 EXPECT_FALSE(delegate->tap_cancel());
3236 EXPECT_FALSE(delegate->begin());
3237 EXPECT_TRUE(delegate->end());
3238 EXPECT_FALSE(delegate->scroll_begin());
3239 EXPECT_FALSE(delegate->scroll_update());
3240 EXPECT_FALSE(delegate->fling());
3242 EXPECT_TRUE(delegate->scroll_end());
3245 // Check that appropriate touch events generate double tap gesture events.
3246 TEST_F(GestureRecognizerTest, GestureEventDoubleTap) {
3247 scoped_ptr<GestureEventConsumeDelegate> delegate(
3248 new GestureEventConsumeDelegate());
3249 const int kWindowWidth = 123;
3250 const int kWindowHeight = 45;
3251 const int kTouchId = 2;
3252 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3253 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3254 delegate.get(), -1234, bounds, root_window()));
3255 TimedEvents tes;
3257 // First tap (tested in GestureEventTap)
3258 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3259 kTouchId, tes.Now());
3260 DispatchEventUsingWindowDispatcher(&press1);
3261 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3262 kTouchId, tes.LeapForward(50));
3263 DispatchEventUsingWindowDispatcher(&release1);
3264 delegate->Reset();
3266 // Second tap
3267 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3268 kTouchId, tes.LeapForward(200));
3269 DispatchEventUsingWindowDispatcher(&press2);
3270 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3271 kTouchId, tes.LeapForward(50));
3272 DispatchEventUsingWindowDispatcher(&release2);
3274 EXPECT_TRUE(delegate->tap());
3275 EXPECT_TRUE(delegate->tap_down());
3276 EXPECT_FALSE(delegate->tap_cancel());
3277 EXPECT_TRUE(delegate->begin());
3278 EXPECT_TRUE(delegate->end());
3279 EXPECT_FALSE(delegate->scroll_begin());
3280 EXPECT_FALSE(delegate->scroll_update());
3281 EXPECT_FALSE(delegate->scroll_end());
3283 EXPECT_EQ(2, delegate->tap_count());
3286 // Check that appropriate touch events generate triple tap gesture events.
3287 TEST_F(GestureRecognizerTest, GestureEventTripleTap) {
3288 scoped_ptr<GestureEventConsumeDelegate> delegate(
3289 new GestureEventConsumeDelegate());
3290 const int kWindowWidth = 123;
3291 const int kWindowHeight = 45;
3292 const int kTouchId = 2;
3293 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3294 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3295 delegate.get(), -1234, bounds, root_window()));
3296 TimedEvents tes;
3298 // First tap (tested in GestureEventTap)
3299 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3300 kTouchId, tes.Now());
3301 DispatchEventUsingWindowDispatcher(&press1);
3302 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3303 kTouchId, tes.LeapForward(50));
3304 DispatchEventUsingWindowDispatcher(&release1);
3306 EXPECT_EQ(1, delegate->tap_count());
3307 delegate->Reset();
3309 // Second tap (tested in GestureEventDoubleTap)
3310 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3311 kTouchId, tes.LeapForward(200));
3312 DispatchEventUsingWindowDispatcher(&press2);
3313 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3314 kTouchId, tes.LeapForward(50));
3315 DispatchEventUsingWindowDispatcher(&release2);
3317 EXPECT_EQ(2, delegate->tap_count());
3318 delegate->Reset();
3320 // Third tap
3321 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(102, 206),
3322 kTouchId, tes.LeapForward(200));
3323 DispatchEventUsingWindowDispatcher(&press3);
3324 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3325 kTouchId, tes.LeapForward(50));
3326 DispatchEventUsingWindowDispatcher(&release3);
3328 // Third, Fourth and Fifth Taps. Taps after the third should have their
3329 // |tap_count| wrap around back to 1.
3330 for (int i = 3; i < 5; ++i) {
3331 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED,
3332 gfx::Point(102, 206),
3333 kTouchId,
3334 tes.LeapForward(200));
3335 DispatchEventUsingWindowDispatcher(&press3);
3336 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED,
3337 gfx::Point(102, 206),
3338 kTouchId,
3339 tes.LeapForward(50));
3340 DispatchEventUsingWindowDispatcher(&release3);
3342 EXPECT_TRUE(delegate->tap());
3343 EXPECT_TRUE(delegate->tap_down());
3344 EXPECT_FALSE(delegate->tap_cancel());
3345 EXPECT_TRUE(delegate->begin());
3346 EXPECT_TRUE(delegate->end());
3347 EXPECT_FALSE(delegate->scroll_begin());
3348 EXPECT_FALSE(delegate->scroll_update());
3349 EXPECT_FALSE(delegate->scroll_end());
3350 EXPECT_EQ(1 + (i % 3), delegate->tap_count());
3354 // Check that we don't get a double tap when the two taps are far apart.
3355 TEST_F(GestureRecognizerTest, TwoTapsFarApart) {
3356 scoped_ptr<GestureEventConsumeDelegate> delegate(
3357 new GestureEventConsumeDelegate());
3358 const int kWindowWidth = 123;
3359 const int kWindowHeight = 45;
3360 const int kTouchId = 2;
3361 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3362 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3363 delegate.get(), -1234, bounds, root_window()));
3364 TimedEvents tes;
3366 // First tap (tested in GestureEventTap)
3367 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3368 kTouchId, tes.Now());
3369 DispatchEventUsingWindowDispatcher(&press1);
3370 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3371 kTouchId, tes.LeapForward(50));
3372 DispatchEventUsingWindowDispatcher(&release1);
3373 delegate->Reset();
3375 // Second tap, close in time but far in distance
3376 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201),
3377 kTouchId, tes.LeapForward(200));
3378 DispatchEventUsingWindowDispatcher(&press2);
3379 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201),
3380 kTouchId, tes.LeapForward(50));
3381 DispatchEventUsingWindowDispatcher(&release2);
3383 EXPECT_TRUE(delegate->tap());
3384 EXPECT_TRUE(delegate->tap_down());
3385 EXPECT_FALSE(delegate->tap_cancel());
3386 EXPECT_TRUE(delegate->begin());
3387 EXPECT_TRUE(delegate->end());
3388 EXPECT_FALSE(delegate->scroll_begin());
3389 EXPECT_FALSE(delegate->scroll_update());
3390 EXPECT_FALSE(delegate->scroll_end());
3392 EXPECT_EQ(1, delegate->tap_count());
3395 // Check that we don't get a double tap when the two taps have a long enough
3396 // delay in between.
3397 TEST_F(GestureRecognizerTest, TwoTapsWithDelayBetween) {
3398 scoped_ptr<GestureEventConsumeDelegate> delegate(
3399 new GestureEventConsumeDelegate());
3400 const int kWindowWidth = 123;
3401 const int kWindowHeight = 45;
3402 const int kTouchId = 2;
3403 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3404 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3405 delegate.get(), -1234, bounds, root_window()));
3406 TimedEvents tes;
3408 // First tap (tested in GestureEventTap)
3409 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3410 kTouchId, tes.Now());
3411 DispatchEventUsingWindowDispatcher(&press1);
3412 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3413 kTouchId, tes.LeapForward(50));
3414 DispatchEventUsingWindowDispatcher(&release1);
3415 delegate->Reset();
3417 // Second tap, close in distance but after some delay
3418 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3419 kTouchId, tes.LeapForward(2000));
3420 DispatchEventUsingWindowDispatcher(&press2);
3421 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3422 kTouchId, tes.LeapForward(50));
3423 DispatchEventUsingWindowDispatcher(&release2);
3425 EXPECT_TRUE(delegate->tap());
3426 EXPECT_TRUE(delegate->tap_down());
3427 EXPECT_FALSE(delegate->tap_cancel());
3428 EXPECT_TRUE(delegate->begin());
3429 EXPECT_TRUE(delegate->end());
3430 EXPECT_FALSE(delegate->scroll_begin());
3431 EXPECT_FALSE(delegate->scroll_update());
3432 EXPECT_FALSE(delegate->scroll_end());
3434 EXPECT_EQ(1, delegate->tap_count());
3437 // Checks that if the bounding-box of a gesture changes because of change in
3438 // radius of a touch-point, and not because of change in position, then there
3439 // are not gesture events from that.
3440 TEST_F(GestureRecognizerTest, BoundingBoxRadiusChange) {
3441 scoped_ptr<GestureEventConsumeDelegate> delegate(
3442 new GestureEventConsumeDelegate());
3443 const int kWindowWidth = 234;
3444 const int kWindowHeight = 345;
3445 const int kTouchId = 5, kTouchId2 = 7;
3446 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3447 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3448 delegate.get(), -1234, bounds, root_window()));
3449 TimedEvents tes;
3451 ui::TouchEvent press1(
3452 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, tes.Now());
3453 DispatchEventUsingWindowDispatcher(&press1);
3454 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
3456 delegate->Reset();
3458 ui::TouchEvent press2(
3459 ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2,
3460 tes.LeapForward(400));
3461 press2.set_radius_x(5);
3462 DispatchEventUsingWindowDispatcher(&press2);
3463 EXPECT_FALSE(delegate->pinch_begin());
3464 EXPECT_EQ(gfx::Rect(101, 196, 105, 10).ToString(),
3465 delegate->bounding_box().ToString());
3467 delegate->Reset();
3469 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(50, 50), kTouchId,
3470 tes.LeapForward(40));
3471 DispatchEventUsingWindowDispatcher(&move1);
3472 EXPECT_TRUE(delegate->pinch_begin());
3473 EXPECT_EQ(gfx::Rect(50, 50, 156, 156).ToString(),
3474 delegate->bounding_box().ToString());
3476 delegate->Reset();
3478 // The position doesn't move, but the radius changes.
3479 ui::TouchEvent move2(
3480 ui::ET_TOUCH_MOVED, gfx::Point(50, 50), kTouchId, tes.LeapForward(40));
3481 move2.set_radius_x(50);
3482 move2.set_radius_y(60);
3483 DispatchEventUsingWindowDispatcher(&move2);
3484 EXPECT_FALSE(delegate->tap());
3485 EXPECT_FALSE(delegate->tap_cancel());
3486 EXPECT_FALSE(delegate->scroll_update());
3487 EXPECT_FALSE(delegate->pinch_update());
3489 delegate->Reset();
3492 // Checks that slow scrolls deliver the correct deltas.
3493 // In particular, fix for http;//crbug.com/150573.
3494 TEST_F(GestureRecognizerTest, NoDriftInScroll) {
3495 ui::GestureConfiguration::GetInstance()
3496 ->set_max_touch_move_in_pixels_for_click(3);
3497 scoped_ptr<GestureEventConsumeDelegate> delegate(
3498 new GestureEventConsumeDelegate());
3499 const int kWindowWidth = 234;
3500 const int kWindowHeight = 345;
3501 const int kTouchId = 5;
3502 TimedEvents tes;
3503 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3504 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3505 delegate.get(), -1234, bounds, root_window()));
3507 ui::TouchEvent press1(
3508 ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, tes.Now());
3509 DispatchEventUsingWindowDispatcher(&press1);
3510 EXPECT_TRUE(delegate->begin());
3512 delegate->Reset();
3514 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId,
3515 tes.LeapForward(40));
3516 DispatchEventUsingWindowDispatcher(&move1);
3517 EXPECT_FALSE(delegate->scroll_begin());
3519 delegate->Reset();
3521 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3522 tes.LeapForward(40));
3523 DispatchEventUsingWindowDispatcher(&move2);
3524 EXPECT_TRUE(delegate->tap_cancel());
3525 EXPECT_TRUE(delegate->scroll_begin());
3526 EXPECT_TRUE(delegate->scroll_update());
3527 // 3 px consumed by touch slop region.
3528 EXPECT_EQ(-1, delegate->scroll_y());
3529 EXPECT_EQ(-4, delegate->scroll_y_hint());
3531 delegate->Reset();
3533 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3534 tes.LeapForward(40));
3535 DispatchEventUsingWindowDispatcher(&move3);
3536 EXPECT_FALSE(delegate->scroll_update());
3538 delegate->Reset();
3540 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId,
3541 tes.LeapForward(40));
3542 DispatchEventUsingWindowDispatcher(&move4);
3543 EXPECT_TRUE(delegate->scroll_update());
3544 EXPECT_EQ(-1, delegate->scroll_y());
3546 delegate->Reset();
3549 // Ensure that move events which are preventDefaulted will cause a tap
3550 // cancel gesture event to be fired if the move would normally cause a
3551 // scroll. See bug http://crbug.com/146397.
3552 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveCanFireTapCancel) {
3553 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3554 new ConsumesTouchMovesDelegate());
3555 const int kTouchId = 5;
3556 gfx::Rect bounds(100, 200, 123, 45);
3557 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3558 delegate.get(), -1234, bounds, root_window()));
3559 TimedEvents tes;
3561 delegate->Reset();
3562 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3563 kTouchId, tes.Now());
3565 delegate->set_consume_touch_move(false);
3566 DispatchEventUsingWindowDispatcher(&press);
3567 delegate->set_consume_touch_move(true);
3568 delegate->Reset();
3569 // Move the touch-point enough so that it would normally be considered a
3570 // scroll. But since the touch-moves will be consumed, no scrolling should
3571 // occur.
3572 // With the unified gesture detector, we will receive a scroll begin gesture,
3573 // whereas with the aura gesture recognizer we won't.
3574 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3575 EXPECT_FALSE(delegate->tap());
3576 EXPECT_FALSE(delegate->tap_down());
3577 EXPECT_TRUE(delegate->tap_cancel());
3578 EXPECT_FALSE(delegate->begin());
3579 EXPECT_FALSE(delegate->scroll_update());
3580 EXPECT_FALSE(delegate->scroll_end());
3583 TEST_F(GestureRecognizerTest, CancelAllActiveTouches) {
3584 scoped_ptr<GestureEventConsumeDelegate> delegate(
3585 new GestureEventConsumeDelegate());
3586 TimedEvents tes;
3587 const int kWindowWidth = 800;
3588 const int kWindowHeight = 600;
3589 const int kTouchId1 = 1;
3590 const int kTouchId2 = 2;
3591 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3592 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3593 delegate.get(), -1234, bounds, root_window()));
3594 scoped_ptr<TestEventHandler> handler(new TestEventHandler());
3595 window->AddPreTargetHandler(handler.get());
3597 // Start a gesture sequence on |window|. Then cancel all touches.
3598 // Make sure |window| receives a touch-cancel event.
3599 delegate->Reset();
3600 ui::TouchEvent press(
3601 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
3602 DispatchEventUsingWindowDispatcher(&press);
3603 EXPECT_2_EVENTS(
3604 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
3605 delegate->Reset();
3606 ui::TouchEvent p2(
3607 ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), kTouchId2, tes.Now());
3608 DispatchEventUsingWindowDispatcher(&p2);
3609 EXPECT_2_EVENTS(
3610 delegate->events(), ui::ET_GESTURE_TAP_CANCEL, ui::ET_GESTURE_BEGIN);
3611 delegate->Reset();
3612 ui::TouchEvent move(
3613 ui::ET_TOUCH_MOVED, gfx::Point(350, 300), kTouchId2, tes.Now());
3614 DispatchEventUsingWindowDispatcher(&move);
3615 EXPECT_3_EVENTS(delegate->events(),
3616 ui::ET_GESTURE_SCROLL_BEGIN,
3617 ui::ET_GESTURE_SCROLL_UPDATE,
3618 ui::ET_GESTURE_PINCH_BEGIN);
3619 EXPECT_EQ(2, handler->touch_pressed_count());
3620 delegate->Reset();
3621 handler->Reset();
3623 ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get();
3624 EXPECT_EQ(window.get(),
3625 gesture_recognizer->GetTouchLockedTarget(press));
3627 ui::GestureRecognizer::Get()->CancelActiveTouchesExcept(nullptr);
3629 EXPECT_EQ(NULL, gesture_recognizer->GetTouchLockedTarget(press));
3630 EXPECT_4_EVENTS(delegate->events(),
3631 ui::ET_GESTURE_PINCH_END,
3632 ui::ET_GESTURE_SCROLL_END,
3633 ui::ET_GESTURE_END,
3634 ui::ET_GESTURE_END);
3635 const std::vector<gfx::PointF>& points = handler->cancelled_touch_points();
3636 EXPECT_EQ(2U, points.size());
3637 EXPECT_EQ(gfx::Point(101, 201), points[0]);
3638 EXPECT_EQ(gfx::Point(350, 300), points[1]);
3641 // Check that appropriate touch events generate show press events
3642 TEST_F(GestureRecognizerTest, GestureEventShowPress) {
3643 scoped_ptr<GestureEventConsumeDelegate> delegate(
3644 new GestureEventConsumeDelegate());
3645 TimedEvents tes;
3646 const int kWindowWidth = 123;
3647 const int kWindowHeight = 45;
3648 const int kTouchId = 2;
3649 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3650 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3651 delegate.get(), -1234, bounds, root_window()));
3653 delegate->Reset();
3655 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3656 kTouchId, tes.Now());
3657 DispatchEventUsingWindowDispatcher(&press1);
3658 EXPECT_TRUE(delegate->tap_down());
3659 EXPECT_TRUE(delegate->begin());
3660 EXPECT_FALSE(delegate->tap_cancel());
3662 // We haven't pressed long enough for a show press to occur
3663 EXPECT_FALSE(delegate->show_press());
3665 // Wait until the timer runs out
3666 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
3667 EXPECT_TRUE(delegate->show_press());
3668 EXPECT_FALSE(delegate->tap_cancel());
3670 delegate->Reset();
3671 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3672 kTouchId, tes.Now());
3673 DispatchEventUsingWindowDispatcher(&release1);
3674 EXPECT_FALSE(delegate->long_press());
3676 // Note the tap isn't dispatched until the release
3677 EXPECT_FALSE(delegate->tap_cancel());
3678 EXPECT_TRUE(delegate->tap());
3681 // Check that scrolling cancels a show press
3682 TEST_F(GestureRecognizerTest, GestureEventShowPressCancelledByScroll) {
3683 scoped_ptr<GestureEventConsumeDelegate> delegate(
3684 new GestureEventConsumeDelegate());
3685 TimedEvents tes;
3686 const int kWindowWidth = 123;
3687 const int kWindowHeight = 45;
3688 const int kTouchId = 6;
3689 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3690 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3691 delegate.get(), -1234, bounds, root_window()));
3693 delegate->Reset();
3695 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3696 kTouchId, tes.Now());
3697 DispatchEventUsingWindowDispatcher(&press1);
3698 EXPECT_TRUE(delegate->tap_down());
3700 // We haven't pressed long enough for a show press to occur
3701 EXPECT_FALSE(delegate->show_press());
3702 EXPECT_FALSE(delegate->tap_cancel());
3704 // Scroll around, to cancel the show press
3705 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3706 // Wait until the timer runs out
3707 DelayByShowPressTimeout();
3708 EXPECT_FALSE(delegate->show_press());
3709 EXPECT_TRUE(delegate->tap_cancel());
3711 delegate->Reset();
3712 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3713 kTouchId, tes.LeapForward(10));
3714 DispatchEventUsingWindowDispatcher(&release1);
3715 EXPECT_FALSE(delegate->show_press());
3716 EXPECT_FALSE(delegate->tap_cancel());
3719 // Test that show press events are sent immediately on tap
3720 TEST_F(GestureRecognizerTest, GestureEventShowPressSentOnTap) {
3721 scoped_ptr<GestureEventConsumeDelegate> delegate(
3722 new GestureEventConsumeDelegate());
3723 TimedEvents tes;
3724 const int kWindowWidth = 123;
3725 const int kWindowHeight = 45;
3726 const int kTouchId = 6;
3727 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3728 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3729 delegate.get(), -1234, bounds, root_window()));
3731 delegate->Reset();
3733 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3734 kTouchId, tes.Now());
3735 DispatchEventUsingWindowDispatcher(&press1);
3736 EXPECT_TRUE(delegate->tap_down());
3738 // We haven't pressed long enough for a show press to occur
3739 EXPECT_FALSE(delegate->show_press());
3740 EXPECT_FALSE(delegate->tap_cancel());
3742 delegate->Reset();
3743 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3744 kTouchId, tes.LeapForward(50));
3745 DispatchEventUsingWindowDispatcher(&release1);
3746 EXPECT_TRUE(delegate->show_press());
3747 EXPECT_FALSE(delegate->tap_cancel());
3748 EXPECT_TRUE(delegate->tap());
3751 // Test that consuming the first move touch event prevents a scroll.
3752 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveScrollTest) {
3753 scoped_ptr<QueueTouchEventDelegate> delegate(
3754 new QueueTouchEventDelegate(host()->dispatcher()));
3755 TimedEvents tes;
3756 const int kTouchId = 7;
3757 gfx::Rect bounds(0, 0, 1000, 1000);
3758 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3759 delegate.get(), -1234, bounds, root_window()));
3760 delegate->set_window(window.get());
3762 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3763 kTouchId, tes.Now());
3764 DispatchEventUsingWindowDispatcher(&press);
3765 delegate->ReceivedAck();
3767 // A touch move within the slop region is never consumed in web contents. The
3768 // unified GR won't prevent scroll if a touch move within the slop region is
3769 // consumed, so make sure this touch move exceeds the slop region.
3770 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(10, 10),
3771 kTouchId, tes.Now());
3772 DispatchEventUsingWindowDispatcher(&move1);
3773 delegate->ReceivedAckPreventDefaulted();
3775 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3776 kTouchId, tes.Now());
3777 DispatchEventUsingWindowDispatcher(&move2);
3778 delegate->ReceivedAck();
3780 // With the unified gesture detector, consuming the first touch move event
3781 // won't prevent all future scrolling.
3782 EXPECT_TRUE(delegate->scroll_begin());
3783 EXPECT_TRUE(delegate->scroll_update());
3786 // Test that consuming the first move touch doesn't prevent a tap.
3787 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveTapTest) {
3788 scoped_ptr<QueueTouchEventDelegate> delegate(
3789 new QueueTouchEventDelegate(host()->dispatcher()));
3790 TimedEvents tes;
3791 const int kTouchId = 7;
3792 gfx::Rect bounds(0, 0, 1000, 1000);
3793 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3794 delegate.get(), -1234, bounds, root_window()));
3795 delegate->set_window(window.get());
3797 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3798 kTouchId, tes.Now());
3799 DispatchEventUsingWindowDispatcher(&press);
3800 delegate->ReceivedAck();
3802 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
3803 kTouchId, tes.Now());
3804 DispatchEventUsingWindowDispatcher(&move);
3805 delegate->ReceivedAckPreventDefaulted();
3807 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(2, 2),
3808 kTouchId, tes.LeapForward(50));
3809 DispatchEventUsingWindowDispatcher(&release);
3810 delegate->ReceivedAck();
3812 EXPECT_TRUE(delegate->tap());
3815 // Test that consuming the first move touch doesn't prevent a long press.
3816 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveLongPressTest) {
3817 scoped_ptr<QueueTouchEventDelegate> delegate(
3818 new QueueTouchEventDelegate(host()->dispatcher()));
3819 TimedEvents tes;
3820 const int kWindowWidth = 123;
3821 const int kWindowHeight = 45;
3822 const int kTouchId = 2;
3823 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3824 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3825 delegate.get(), -1234, bounds, root_window()));
3826 delegate->set_window(window.get());
3828 delegate->Reset();
3830 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3831 kTouchId, tes.Now());
3832 DispatchEventUsingWindowDispatcher(&press1);
3833 delegate->ReceivedAck();
3835 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103, 203),
3836 kTouchId, tes.Now());
3837 DispatchEventUsingWindowDispatcher(&move);
3838 delegate->ReceivedAckPreventDefaulted();
3840 // Wait until the timer runs out
3841 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
3842 EXPECT_TRUE(delegate->long_press());
3845 // Tests that the deltas are correct when leaving the slop region very slowly.
3846 TEST_F(GestureRecognizerTest, TestExceedingSlopSlowly) {
3847 ui::GestureConfiguration::GetInstance()
3848 ->set_max_touch_move_in_pixels_for_click(3);
3849 scoped_ptr<GestureEventConsumeDelegate> delegate(
3850 new GestureEventConsumeDelegate());
3851 const int kWindowWidth = 234;
3852 const int kWindowHeight = 345;
3853 const int kTouchId = 5;
3854 TimedEvents tes;
3855 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3856 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3857 delegate.get(), -1234, bounds, root_window()));
3859 ui::TouchEvent press(
3860 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), kTouchId, tes.Now());
3861 DispatchEventUsingWindowDispatcher(&press);
3862 EXPECT_FALSE(delegate->scroll_begin());
3863 EXPECT_FALSE(delegate->scroll_update());
3864 delegate->Reset();
3866 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(11, 10), kTouchId,
3867 tes.LeapForward(40));
3868 DispatchEventUsingWindowDispatcher(&move1);
3869 EXPECT_FALSE(delegate->scroll_begin());
3870 EXPECT_FALSE(delegate->scroll_update());
3871 EXPECT_EQ(0, delegate->scroll_x());
3872 EXPECT_EQ(0, delegate->scroll_x_hint());
3873 delegate->Reset();
3875 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(12, 10), kTouchId,
3876 tes.LeapForward(40));
3877 DispatchEventUsingWindowDispatcher(&move2);
3878 EXPECT_FALSE(delegate->scroll_begin());
3879 EXPECT_FALSE(delegate->scroll_update());
3880 EXPECT_EQ(0, delegate->scroll_x());
3881 EXPECT_EQ(0, delegate->scroll_x_hint());
3882 delegate->Reset();
3885 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::PointF(13.1f, 10.f), kTouchId,
3886 tes.LeapForward(40));
3887 DispatchEventUsingWindowDispatcher(&move3);
3888 EXPECT_TRUE(delegate->scroll_begin());
3889 EXPECT_TRUE(delegate->scroll_update());
3890 EXPECT_NEAR(0.1, delegate->scroll_x(), 0.0001);
3891 EXPECT_FLOAT_EQ(3.1f, delegate->scroll_x_hint());
3892 delegate->Reset();
3894 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(14, 10), kTouchId,
3895 tes.LeapForward(40));
3896 DispatchEventUsingWindowDispatcher(&move4);
3897 EXPECT_FALSE(delegate->scroll_begin());
3898 EXPECT_TRUE(delegate->scroll_update());
3899 EXPECT_NEAR(0.9, delegate->scroll_x(), 0.0001);
3900 EXPECT_EQ(0.f, delegate->scroll_x_hint());
3901 delegate->Reset();
3904 TEST_F(GestureRecognizerTest, ScrollAlternatelyConsumedTest) {
3905 scoped_ptr<QueueTouchEventDelegate> delegate(
3906 new QueueTouchEventDelegate(host()->dispatcher()));
3907 TimedEvents tes;
3908 const int kWindowWidth = 3000;
3909 const int kWindowHeight = 3000;
3910 const int kTouchId = 2;
3911 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3912 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3913 delegate.get(), -1234, bounds, root_window()));
3914 delegate->set_window(window.get());
3916 delegate->Reset();
3918 int x = 0;
3919 int y = 0;
3921 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
3922 kTouchId, tes.Now());
3923 DispatchEventUsingWindowDispatcher(&press1);
3924 delegate->ReceivedAck();
3925 EXPECT_FALSE(delegate->scroll_begin());
3926 EXPECT_FALSE(delegate->scroll_update());
3927 delegate->Reset();
3929 x += 100;
3930 y += 100;
3931 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
3932 kTouchId, tes.Now());
3933 DispatchEventUsingWindowDispatcher(&move1);
3934 delegate->ReceivedAck();
3935 EXPECT_TRUE(delegate->scroll_begin());
3936 EXPECT_TRUE(delegate->scroll_update());
3937 delegate->Reset();
3939 for (int i = 0; i < 3; ++i) {
3940 x += 10;
3941 y += 10;
3942 ui::TouchEvent move2(
3943 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
3944 DispatchEventUsingWindowDispatcher(&move2);
3945 delegate->ReceivedAck();
3946 EXPECT_FALSE(delegate->scroll_begin());
3947 EXPECT_TRUE(delegate->scroll_update());
3948 EXPECT_EQ(10, delegate->scroll_x());
3949 EXPECT_EQ(10, delegate->scroll_y());
3950 delegate->Reset();
3952 x += 20;
3953 y += 20;
3954 ui::TouchEvent move3(
3955 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
3956 DispatchEventUsingWindowDispatcher(&move3);
3957 delegate->ReceivedAckPreventDefaulted();
3958 EXPECT_FALSE(delegate->scroll_begin());
3959 EXPECT_FALSE(delegate->scroll_update());
3960 delegate->Reset();
3964 TEST_F(GestureRecognizerTest, PinchAlternatelyConsumedTest) {
3965 scoped_ptr<QueueTouchEventDelegate> delegate(
3966 new QueueTouchEventDelegate(host()->dispatcher()));
3967 TimedEvents tes;
3968 const int kWindowWidth = 3000;
3969 const int kWindowHeight = 3000;
3970 const int kTouchId1 = 5;
3971 const int kTouchId2 = 7;
3972 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3973 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3974 delegate.get(), -1234, bounds, root_window()));
3975 delegate->set_window(window.get());
3976 delegate->Reset();
3978 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3979 kTouchId1, tes.Now());
3980 DispatchEventUsingWindowDispatcher(&press1);
3981 delegate->ReceivedAck();
3982 EXPECT_FALSE(delegate->scroll_begin());
3983 EXPECT_FALSE(delegate->scroll_update());
3984 delegate->Reset();
3986 int x = 0;
3987 int y = 0;
3989 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
3990 kTouchId2, tes.Now());
3991 DispatchEventUsingWindowDispatcher(&press2);
3992 delegate->ReceivedAck();
3993 EXPECT_FALSE(delegate->scroll_begin());
3994 EXPECT_FALSE(delegate->scroll_update());
3995 EXPECT_FALSE(delegate->pinch_begin());
3996 EXPECT_FALSE(delegate->pinch_update());
3998 delegate->Reset();
4000 x += 100;
4001 y += 100;
4002 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
4003 kTouchId2, tes.Now());
4004 DispatchEventUsingWindowDispatcher(&move1);
4005 delegate->ReceivedAck();
4006 EXPECT_TRUE(delegate->scroll_begin());
4007 EXPECT_TRUE(delegate->scroll_update());
4008 EXPECT_TRUE(delegate->pinch_begin());
4009 EXPECT_FALSE(delegate->pinch_update());
4010 delegate->Reset();
4012 const float expected_scales[] = {1.5f, 1.2f, 1.125f};
4014 for (int i = 0; i < 3; ++i) {
4015 x += 50;
4016 y += 50;
4017 ui::TouchEvent move2(
4018 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4019 DispatchEventUsingWindowDispatcher(&move2);
4020 delegate->ReceivedAck();
4021 EXPECT_FALSE(delegate->scroll_begin());
4022 EXPECT_TRUE(delegate->scroll_update());
4023 EXPECT_FALSE(delegate->scroll_end());
4024 EXPECT_FALSE(delegate->pinch_begin());
4025 EXPECT_TRUE(delegate->pinch_update());
4026 EXPECT_FALSE(delegate->pinch_end());
4027 EXPECT_EQ(25, delegate->scroll_x());
4028 EXPECT_EQ(25, delegate->scroll_y());
4029 EXPECT_FLOAT_EQ(expected_scales[i], delegate->scale());
4030 delegate->Reset();
4032 x += 100;
4033 y += 100;
4034 ui::TouchEvent move3(
4035 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4036 DispatchEventUsingWindowDispatcher(&move3);
4037 delegate->ReceivedAckPreventDefaulted();
4038 EXPECT_FALSE(delegate->scroll_begin());
4039 EXPECT_FALSE(delegate->scroll_update());
4040 EXPECT_FALSE(delegate->scroll_end());
4041 EXPECT_FALSE(delegate->pinch_begin());
4042 EXPECT_FALSE(delegate->pinch_update());
4043 EXPECT_FALSE(delegate->pinch_end());
4044 delegate->Reset();
4048 // Test that touch event flags are passed through to the gesture event.
4049 TEST_F(GestureRecognizerTest, GestureEventFlagsPassedFromTouchEvent) {
4050 scoped_ptr<GestureEventConsumeDelegate> delegate(
4051 new GestureEventConsumeDelegate());
4052 TimedEvents tes;
4053 const int kWindowWidth = 123;
4054 const int kWindowHeight = 45;
4055 const int kTouchId = 6;
4056 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4057 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4058 delegate.get(), -1234, bounds, root_window()));
4060 delegate->Reset();
4062 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4063 kTouchId, tes.Now());
4064 DispatchEventUsingWindowDispatcher(&press1);
4065 EXPECT_TRUE(delegate->tap_down());
4067 int default_flags = delegate->flags();
4069 ui::TouchEvent move1(
4070 ui::ET_TOUCH_MOVED, gfx::Point(397, 149), kTouchId, tes.LeapForward(50));
4071 move1.set_flags(992);
4073 DispatchEventUsingWindowDispatcher(&move1);
4074 EXPECT_NE(default_flags, delegate->flags());
4077 // A delegate that deletes a window on long press.
4078 class GestureEventDeleteWindowOnLongPress : public GestureEventConsumeDelegate {
4079 public:
4080 GestureEventDeleteWindowOnLongPress()
4081 : window_(NULL) {}
4083 void set_window(aura::Window** window) { window_ = window; }
4085 void OnGestureEvent(ui::GestureEvent* gesture) override {
4086 GestureEventConsumeDelegate::OnGestureEvent(gesture);
4087 if (gesture->type() != ui::ET_GESTURE_LONG_PRESS)
4088 return;
4089 ui::GestureRecognizer::Get()->CleanupStateForConsumer(*window_);
4090 delete *window_;
4091 *window_ = NULL;
4094 private:
4095 aura::Window** window_;
4096 DISALLOW_COPY_AND_ASSIGN(GestureEventDeleteWindowOnLongPress);
4099 // Check that deleting the window in response to a long press gesture doesn't
4100 // crash.
4101 TEST_F(GestureRecognizerTest, GestureEventLongPressDeletingWindow) {
4102 GestureEventDeleteWindowOnLongPress delegate;
4103 const int kWindowWidth = 123;
4104 const int kWindowHeight = 45;
4105 const int kTouchId = 2;
4106 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4107 aura::Window* window(CreateTestWindowWithDelegate(
4108 &delegate, -1234, bounds, root_window()));
4109 delegate.set_window(&window);
4111 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
4112 gfx::Point(101, 201),
4113 kTouchId,
4114 ui::EventTimeForNow());
4115 DispatchEventUsingWindowDispatcher(&press1);
4116 EXPECT_TRUE(window != NULL);
4118 // Wait until the timer runs out.
4119 delegate.WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4120 EXPECT_EQ(NULL, window);
4123 TEST_F(GestureRecognizerWithSwitchTest, GestureEventSmallPinchDisabled) {
4124 scoped_ptr<GestureEventConsumeDelegate> delegate(
4125 new GestureEventConsumeDelegate());
4126 TimedEvents tes;
4127 const int kWindowWidth = 300;
4128 const int kWindowHeight = 400;
4129 const int kTouchId1 = 3;
4130 const int kTouchId2 = 5;
4131 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4132 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4133 delegate.get(), -1234, bounds, root_window()));
4135 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4136 kTouchId1, tes.Now());
4137 DispatchEventUsingWindowDispatcher(&press1);
4138 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4139 kTouchId2, tes.Now());
4140 DispatchEventUsingWindowDispatcher(&press2);
4142 // Move the first finger.
4143 delegate->Reset();
4144 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4145 kTouchId1, tes.Now());
4146 DispatchEventUsingWindowDispatcher(&move1);
4148 EXPECT_3_EVENTS(delegate->events(),
4149 ui::ET_GESTURE_SCROLL_BEGIN,
4150 ui::ET_GESTURE_SCROLL_UPDATE,
4151 ui::ET_GESTURE_PINCH_BEGIN);
4153 // No pinch update occurs, as kCompensateForUnstablePinchZoom is on and
4154 // |min_pinch_update_span_delta| was nonzero, and this is a very small pinch.
4155 delegate->Reset();
4156 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4157 kTouchId1, tes.Now());
4158 DispatchEventUsingWindowDispatcher(&move2);
4159 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4162 TEST_F(GestureRecognizerTest, GestureEventSmallPinchEnabled) {
4163 scoped_ptr<GestureEventConsumeDelegate> delegate(
4164 new GestureEventConsumeDelegate());
4165 TimedEvents tes;
4166 const int kWindowWidth = 300;
4167 const int kWindowHeight = 400;
4168 const int kTouchId1 = 3;
4169 const int kTouchId2 = 5;
4170 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4171 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4172 delegate.get(), -1234, bounds, root_window()));
4174 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4175 kTouchId1, tes.Now());
4176 DispatchEventUsingWindowDispatcher(&press1);
4177 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4178 kTouchId2, tes.Now());
4179 DispatchEventUsingWindowDispatcher(&press2);
4181 // Move the first finger.
4182 delegate->Reset();
4183 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4184 kTouchId1, tes.Now());
4185 DispatchEventUsingWindowDispatcher(&move1);
4187 EXPECT_3_EVENTS(delegate->events(),
4188 ui::ET_GESTURE_SCROLL_BEGIN,
4189 ui::ET_GESTURE_SCROLL_UPDATE,
4190 ui::ET_GESTURE_PINCH_BEGIN);
4192 delegate->Reset();
4193 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4194 kTouchId1, tes.Now());
4195 DispatchEventUsingWindowDispatcher(&move2);
4196 EXPECT_2_EVENTS(delegate->events(),
4197 ui::ET_GESTURE_SCROLL_UPDATE,
4198 ui::ET_GESTURE_PINCH_UPDATE);
4201 // Tests that delaying the ack of a touch release doesn't trigger a long press
4202 // gesture.
4203 TEST_F(GestureRecognizerTest, EagerGestureDetection) {
4204 scoped_ptr<QueueTouchEventDelegate> delegate(
4205 new QueueTouchEventDelegate(host()->dispatcher()));
4206 TimedEvents tes;
4207 const int kTouchId = 2;
4208 gfx::Rect bounds(100, 200, 100, 100);
4209 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4210 delegate.get(), -1234, bounds, root_window()));
4211 delegate->set_window(window.get());
4213 delegate->Reset();
4214 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4215 kTouchId, tes.Now());
4216 DispatchEventUsingWindowDispatcher(&press);
4217 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
4218 kTouchId, tes.LeapForward(50));
4219 DispatchEventUsingWindowDispatcher(&release);
4221 delegate->Reset();
4222 // Ack the touch press.
4223 delegate->ReceivedAck();
4224 EXPECT_TRUE(delegate->tap_down());
4226 delegate->Reset();
4227 // Wait until the long press event would fire (if we weren't eager).
4228 DelayByLongPressTimeout();
4230 // Ack the touch release.
4231 delegate->ReceivedAck();
4232 EXPECT_TRUE(delegate->tap());
4233 EXPECT_FALSE(delegate->long_press());
4236 // This tests crbug.com/405519, in which touch events which the gesture detector
4237 // ignores interfere with gesture recognition.
4238 TEST_F(GestureRecognizerTest, IgnoredEventsDontBreakGestureRecognition) {
4239 scoped_ptr<QueueTouchEventDelegate> delegate(
4240 new QueueTouchEventDelegate(host()->dispatcher()));
4241 TimedEvents tes;
4242 const int kWindowWidth = 300;
4243 const int kWindowHeight = 400;
4244 const int kTouchId1 = 3;
4245 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4246 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4247 delegate.get(), -1234, bounds, root_window()));
4248 delegate->set_window(window.get());
4250 ui::TouchEvent press1(
4251 ui::ET_TOUCH_PRESSED, gfx::Point(101, 301), kTouchId1, tes.Now());
4252 DispatchEventUsingWindowDispatcher(&press1);
4253 delegate->ReceivedAck();
4255 EXPECT_2_EVENTS(
4256 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
4258 // Move the first finger.
4259 delegate->Reset();
4260 ui::TouchEvent move1(
4261 ui::ET_TOUCH_MOVED, gfx::Point(65, 201), kTouchId1, tes.Now());
4262 DispatchEventUsingWindowDispatcher(&move1);
4263 delegate->ReceivedAck();
4265 EXPECT_3_EVENTS(delegate->events(),
4266 ui::ET_GESTURE_TAP_CANCEL,
4267 ui::ET_GESTURE_SCROLL_BEGIN,
4268 ui::ET_GESTURE_SCROLL_UPDATE);
4270 delegate->Reset();
4272 // Send a valid event, but don't ack it.
4273 ui::TouchEvent move2(
4274 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4275 DispatchEventUsingWindowDispatcher(&move2);
4276 EXPECT_0_EVENTS(delegate->events());
4278 // Send a touchmove event at the same location as the previous touchmove
4279 // event. This shouldn't do anything.
4280 ui::TouchEvent move3(
4281 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4282 DispatchEventUsingWindowDispatcher(&move3);
4284 // Ack the previous valid event. The intermediary invalid event shouldn't
4285 // interfere.
4286 delegate->ReceivedAck();
4287 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4290 // Tests that an event stream can have a mix of sync and async acks.
4291 TEST_F(GestureRecognizerTest,
4292 MixedSyncAndAsyncAcksDontCauseOutOfOrderDispatch) {
4293 scoped_ptr<QueueTouchEventDelegate> delegate(
4294 new QueueTouchEventDelegate(host()->dispatcher()));
4295 TimedEvents tes;
4296 const int kWindowWidth = 300;
4297 const int kWindowHeight = 400;
4298 const int kTouchId1 = 3;
4299 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4300 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4301 delegate.get(), -1234, bounds, root_window()));
4302 delegate->set_window(window.get());
4304 // Start a scroll gesture.
4305 ui::TouchEvent press1(
4306 ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), kTouchId1, tes.Now());
4307 DispatchEventUsingWindowDispatcher(&press1);
4308 delegate->ReceivedAck();
4310 ui::TouchEvent move1(
4311 ui::ET_TOUCH_MOVED, gfx::Point(100, 100), kTouchId1, tes.Now());
4312 DispatchEventUsingWindowDispatcher(&move1);
4313 delegate->ReceivedAck();
4315 delegate->Reset();
4316 // Dispatch a synchronously consumed touch move, which should be ignored.
4317 delegate->set_synchronous_ack_for_next_event(true);
4318 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(200, 200), kTouchId1,
4319 tes.Now());
4320 DispatchEventUsingWindowDispatcher(&move2);
4321 EXPECT_0_EVENTS(delegate->events());
4323 // Dispatch a touch move, but don't ack it.
4324 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(300, 300), kTouchId1,
4325 tes.Now());
4326 DispatchEventUsingWindowDispatcher(&move3);
4328 // Dispatch two synchronously consumed touch moves, which should be ignored.
4329 delegate->set_synchronous_ack_for_next_event(true);
4330 ui::TouchEvent move4(
4331 ui::ET_TOUCH_MOVED, gfx::Point(400, 400), kTouchId1, tes.Now());
4332 DispatchEventUsingWindowDispatcher(&move4);
4334 delegate->set_synchronous_ack_for_next_event(true);
4335 ui::TouchEvent move5(
4336 ui::ET_TOUCH_MOVED, gfx::Point(500, 500), kTouchId1, tes.Now());
4337 DispatchEventUsingWindowDispatcher(&move5);
4339 EXPECT_0_EVENTS(delegate->events());
4340 EXPECT_EQ(100, delegate->bounding_box().x());
4341 // Ack the pending touch move, and ensure the most recent gesture event
4342 // used its co-ordinates.
4343 delegate->ReceivedAck();
4344 EXPECT_EQ(300, delegate->bounding_box().x());
4345 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4347 // Dispatch a touch move, but don't ack it.
4348 delegate->Reset();
4349 ui::TouchEvent move6(ui::ET_TOUCH_MOVED, gfx::Point(600, 600), kTouchId1,
4350 tes.Now());
4351 DispatchEventUsingWindowDispatcher(&move6);
4353 // Dispatch a synchronously unconsumed touch move.
4354 delegate->set_synchronous_ack_for_next_event(false);
4355 ui::TouchEvent move7(
4356 ui::ET_TOUCH_MOVED, gfx::Point(700, 700), kTouchId1, tes.Now());
4357 DispatchEventUsingWindowDispatcher(&move7);
4359 // The synchronous ack is stuck behind the pending touch move.
4360 EXPECT_0_EVENTS(delegate->events());
4362 delegate->ReceivedAck();
4363 EXPECT_2_EVENTS(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE,
4364 ui::ET_GESTURE_SCROLL_UPDATE);
4367 } // namespace test
4368 } // namespace aura