Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / aura / gestures / gesture_recognizer_unittest.cc
blob1392d6064bfbe659cd26d767cf55d7de91c55bd0
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/memory/scoped_vector.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/timer/timer.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/test/aura_test_base.h"
13 #include "ui/aura/test/test_window_delegate.h"
14 #include "ui/aura/test/test_windows.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/base/hit_test.h"
18 #include "ui/base/ui_base_switches.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_switches.h"
21 #include "ui/events/event_utils.h"
22 #include "ui/events/gesture_detection/gesture_configuration.h"
23 #include "ui/events/gestures/gesture_recognizer_impl.h"
24 #include "ui/events/gestures/gesture_types.h"
25 #include "ui/events/test/event_generator.h"
26 #include "ui/events/test/events_test_utils.h"
27 #include "ui/gfx/geometry/point.h"
28 #include "ui/gfx/geometry/rect.h"
30 #include <queue>
32 namespace aura {
33 namespace test {
35 namespace {
37 std::string WindowIDAsString(ui::GestureConsumer* consumer) {
38 return consumer ?
39 base::IntToString(static_cast<Window*>(consumer)->id()) : "?";
42 #define EXPECT_0_EVENTS(events) \
43 EXPECT_EQ(0u, events.size())
45 #define EXPECT_1_EVENT(events, e0) \
46 EXPECT_EQ(1u, events.size()); \
47 EXPECT_EQ(e0, events[0])
49 #define EXPECT_2_EVENTS(events, e0, e1) \
50 EXPECT_EQ(2u, events.size()); \
51 EXPECT_EQ(e0, events[0]); \
52 EXPECT_EQ(e1, events[1])
54 #define EXPECT_3_EVENTS(events, e0, e1, e2) \
55 EXPECT_EQ(3u, events.size()); \
56 EXPECT_EQ(e0, events[0]); \
57 EXPECT_EQ(e1, events[1]); \
58 EXPECT_EQ(e2, events[2])
60 #define EXPECT_4_EVENTS(events, e0, e1, e2, e3) \
61 EXPECT_EQ(4u, events.size()); \
62 EXPECT_EQ(e0, events[0]); \
63 EXPECT_EQ(e1, events[1]); \
64 EXPECT_EQ(e2, events[2]); \
65 EXPECT_EQ(e3, events[3])
67 // A delegate that keeps track of gesture events.
68 class GestureEventConsumeDelegate : public TestWindowDelegate {
69 public:
70 GestureEventConsumeDelegate()
71 : tap_(false),
72 tap_down_(false),
73 tap_cancel_(false),
74 begin_(false),
75 end_(false),
76 scroll_begin_(false),
77 scroll_update_(false),
78 scroll_end_(false),
79 pinch_begin_(false),
80 pinch_update_(false),
81 pinch_end_(false),
82 long_press_(false),
83 fling_(false),
84 two_finger_tap_(false),
85 show_press_(false),
86 swipe_left_(false),
87 swipe_right_(false),
88 swipe_up_(false),
89 swipe_down_(false),
90 scroll_x_(0),
91 scroll_y_(0),
92 scroll_velocity_x_(0),
93 scroll_velocity_y_(0),
94 velocity_x_(0),
95 velocity_y_(0),
96 scroll_x_hint_(0),
97 scroll_y_hint_(0),
98 tap_count_(0),
99 flags_(0),
100 wait_until_event_(ui::ET_UNKNOWN) {}
102 ~GestureEventConsumeDelegate() override {}
104 void Reset() {
105 events_.clear();
106 tap_ = false;
107 tap_down_ = false;
108 tap_cancel_ = false;
109 begin_ = false;
110 end_ = false;
111 scroll_begin_ = false;
112 scroll_update_ = false;
113 scroll_end_ = false;
114 pinch_begin_ = false;
115 pinch_update_ = false;
116 pinch_end_ = false;
117 long_press_ = false;
118 fling_ = false;
119 two_finger_tap_ = false;
120 show_press_ = false;
121 swipe_left_ = false;
122 swipe_right_ = false;
123 swipe_up_ = false;
124 swipe_down_ = false;
126 scroll_begin_position_.SetPoint(0, 0);
127 tap_location_.SetPoint(0, 0);
128 gesture_end_location_.SetPoint(0, 0);
130 scroll_x_ = 0;
131 scroll_y_ = 0;
132 scroll_velocity_x_ = 0;
133 scroll_velocity_y_ = 0;
134 velocity_x_ = 0;
135 velocity_y_ = 0;
136 scroll_x_hint_ = 0;
137 scroll_y_hint_ = 0;
138 tap_count_ = 0;
139 scale_ = 0;
140 flags_ = 0;
143 const std::vector<ui::EventType>& events() const { return events_; };
145 bool tap() const { return tap_; }
146 bool tap_down() const { return tap_down_; }
147 bool tap_cancel() const { return tap_cancel_; }
148 bool begin() const { return begin_; }
149 bool end() const { return end_; }
150 bool scroll_begin() const { return scroll_begin_; }
151 bool scroll_update() const { return scroll_update_; }
152 bool scroll_end() const { return scroll_end_; }
153 bool pinch_begin() const { return pinch_begin_; }
154 bool pinch_update() const { return pinch_update_; }
155 bool pinch_end() const { return pinch_end_; }
156 bool long_press() const { return long_press_; }
157 bool long_tap() const { return long_tap_; }
158 bool fling() const { return fling_; }
159 bool two_finger_tap() const { return two_finger_tap_; }
160 bool show_press() const { return show_press_; }
161 bool swipe_left() const { return swipe_left_; }
162 bool swipe_right() const { return swipe_right_; }
163 bool swipe_up() const { return swipe_up_; }
164 bool swipe_down() const { return swipe_down_; }
166 const gfx::Point& scroll_begin_position() const {
167 return scroll_begin_position_;
170 const gfx::Point& tap_location() const {
171 return tap_location_;
174 const gfx::Point& gesture_end_location() const {
175 return gesture_end_location_;
178 float scroll_x() const { return scroll_x_; }
179 float scroll_y() const { return scroll_y_; }
180 float scroll_velocity_x() const { return scroll_velocity_x_; }
181 float scroll_velocity_y() const { return scroll_velocity_y_; }
182 float velocity_x() const { return velocity_x_; }
183 float velocity_y() const { return velocity_y_; }
184 float scroll_x_hint() const { return scroll_x_hint_; }
185 float scroll_y_hint() const { return scroll_y_hint_; }
186 float scale() const { return scale_; }
187 const gfx::Rect& bounding_box() const { return bounding_box_; }
188 int tap_count() const { return tap_count_; }
189 int flags() const { return flags_; }
191 void WaitUntilReceivedGesture(ui::EventType type) {
192 wait_until_event_ = type;
193 run_loop_.reset(new base::RunLoop());
194 run_loop_->Run();
197 void OnGestureEvent(ui::GestureEvent* gesture) override {
198 events_.push_back(gesture->type());
199 bounding_box_ = gesture->details().bounding_box();
200 flags_ = gesture->flags();
201 switch (gesture->type()) {
202 case ui::ET_GESTURE_TAP:
203 tap_location_ = gesture->location();
204 tap_count_ = gesture->details().tap_count();
205 tap_ = true;
206 break;
207 case ui::ET_GESTURE_TAP_DOWN:
208 tap_down_ = true;
209 break;
210 case ui::ET_GESTURE_TAP_CANCEL:
211 tap_cancel_ = true;
212 break;
213 case ui::ET_GESTURE_BEGIN:
214 begin_ = true;
215 break;
216 case ui::ET_GESTURE_END:
217 end_ = true;
218 gesture_end_location_ = gesture->location();
219 break;
220 case ui::ET_GESTURE_SCROLL_BEGIN:
221 scroll_begin_ = true;
222 scroll_begin_position_ = gesture->location();
223 scroll_x_hint_ = gesture->details().scroll_x_hint();
224 scroll_y_hint_ = gesture->details().scroll_y_hint();
225 break;
226 case ui::ET_GESTURE_SCROLL_UPDATE:
227 scroll_update_ = true;
228 scroll_x_ += gesture->details().scroll_x();
229 scroll_y_ += gesture->details().scroll_y();
230 break;
231 case ui::ET_GESTURE_SCROLL_END:
232 EXPECT_TRUE(velocity_x_ == 0 && velocity_y_ == 0);
233 scroll_end_ = true;
234 break;
235 case ui::ET_GESTURE_PINCH_BEGIN:
236 pinch_begin_ = true;
237 break;
238 case ui::ET_GESTURE_PINCH_UPDATE:
239 pinch_update_ = true;
240 scale_ = gesture->details().scale();
241 break;
242 case ui::ET_GESTURE_PINCH_END:
243 pinch_end_ = true;
244 break;
245 case ui::ET_GESTURE_LONG_PRESS:
246 long_press_ = true;
247 break;
248 case ui::ET_GESTURE_LONG_TAP:
249 long_tap_ = true;
250 break;
251 case ui::ET_SCROLL_FLING_START:
252 EXPECT_TRUE(gesture->details().velocity_x() != 0 ||
253 gesture->details().velocity_y() != 0);
254 EXPECT_FALSE(scroll_end_);
255 fling_ = true;
256 velocity_x_ = gesture->details().velocity_x();
257 velocity_y_ = gesture->details().velocity_y();
258 break;
259 case ui::ET_GESTURE_TWO_FINGER_TAP:
260 two_finger_tap_ = true;
261 break;
262 case ui::ET_GESTURE_SHOW_PRESS:
263 show_press_ = true;
264 break;
265 case ui::ET_GESTURE_SWIPE:
266 swipe_left_ = gesture->details().swipe_left();
267 swipe_right_ = gesture->details().swipe_right();
268 swipe_up_ = gesture->details().swipe_up();
269 swipe_down_ = gesture->details().swipe_down();
270 break;
271 case ui::ET_SCROLL_FLING_CANCEL:
272 // Only used in unified gesture detection.
273 break;
274 default:
275 NOTREACHED();
277 if (wait_until_event_ == gesture->type() && run_loop_) {
278 run_loop_->Quit();
279 wait_until_event_ = ui::ET_UNKNOWN;
281 gesture->StopPropagation();
284 private:
285 scoped_ptr<base::RunLoop> run_loop_;
286 std::vector<ui::EventType> events_;
288 bool tap_;
289 bool tap_down_;
290 bool tap_cancel_;
291 bool begin_;
292 bool end_;
293 bool scroll_begin_;
294 bool scroll_update_;
295 bool scroll_end_;
296 bool pinch_begin_;
297 bool pinch_update_;
298 bool pinch_end_;
299 bool long_press_;
300 bool long_tap_;
301 bool fling_;
302 bool two_finger_tap_;
303 bool show_press_;
304 bool swipe_left_;
305 bool swipe_right_;
306 bool swipe_up_;
307 bool swipe_down_;
309 gfx::Point scroll_begin_position_;
310 gfx::Point tap_location_;
311 gfx::Point gesture_end_location_;
313 float scroll_x_;
314 float scroll_y_;
315 float scroll_velocity_x_;
316 float scroll_velocity_y_;
317 float velocity_x_;
318 float velocity_y_;
319 float scroll_x_hint_;
320 float scroll_y_hint_;
321 float scale_;
322 gfx::Rect bounding_box_;
323 int tap_count_;
324 int flags_;
326 ui::EventType wait_until_event_;
328 DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate);
331 class QueueTouchEventDelegate : public GestureEventConsumeDelegate {
332 public:
333 explicit QueueTouchEventDelegate(WindowEventDispatcher* dispatcher)
334 : window_(NULL),
335 dispatcher_(dispatcher),
336 synchronous_ack_for_next_event_(AckState::PENDING) {}
338 ~QueueTouchEventDelegate() override {}
340 void OnTouchEvent(ui::TouchEvent* event) override {
341 event->DisableSynchronousHandling();
342 if (synchronous_ack_for_next_event_ != AckState::PENDING) {
343 ui::GestureRecognizer::Get()->AckSyncTouchEvent(
344 event->unique_event_id(),
345 synchronous_ack_for_next_event_ == AckState::CONSUMED
346 ? ui::ER_CONSUMED
347 : ui::ER_UNHANDLED,
348 window_);
349 synchronous_ack_for_next_event_ = AckState::PENDING;
353 void ReceivedAck() {
354 ReceivedAckImpl(false);
357 void ReceivedAckPreventDefaulted() {
358 ReceivedAckImpl(true);
361 void set_window(Window* w) { window_ = w; }
362 void set_synchronous_ack_for_next_event(bool consumed) {
363 DCHECK(synchronous_ack_for_next_event_ == AckState::PENDING);
364 synchronous_ack_for_next_event_ =
365 consumed ? AckState::CONSUMED : AckState::UNCONSUMED;
368 private:
369 enum class AckState {
370 PENDING,
371 CONSUMED,
372 UNCONSUMED,
375 void ReceivedAckImpl(bool prevent_defaulted) {
376 dispatcher_->ProcessedTouchEvent(
377 window_, prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED);
380 Window* window_;
381 WindowEventDispatcher* dispatcher_;
382 AckState synchronous_ack_for_next_event_;
384 DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate);
387 // A delegate that ignores gesture events but keeps track of [synthetic] mouse
388 // events.
389 class GestureEventSynthDelegate : public TestWindowDelegate {
390 public:
391 GestureEventSynthDelegate()
392 : mouse_enter_(false),
393 mouse_exit_(false),
394 mouse_press_(false),
395 mouse_release_(false),
396 mouse_move_(false),
397 double_click_(false) {
400 void Reset() {
401 mouse_enter_ = false;
402 mouse_exit_ = false;
403 mouse_press_ = false;
404 mouse_release_ = false;
405 mouse_move_ = false;
406 double_click_ = false;
409 bool mouse_enter() const { return mouse_enter_; }
410 bool mouse_exit() const { return mouse_exit_; }
411 bool mouse_press() const { return mouse_press_; }
412 bool mouse_move() const { return mouse_move_; }
413 bool mouse_release() const { return mouse_release_; }
414 bool double_click() const { return double_click_; }
416 void OnMouseEvent(ui::MouseEvent* event) override {
417 switch (event->type()) {
418 case ui::ET_MOUSE_PRESSED:
419 double_click_ = event->flags() & ui::EF_IS_DOUBLE_CLICK;
420 mouse_press_ = true;
421 break;
422 case ui::ET_MOUSE_RELEASED:
423 mouse_release_ = true;
424 break;
425 case ui::ET_MOUSE_MOVED:
426 mouse_move_ = true;
427 break;
428 case ui::ET_MOUSE_ENTERED:
429 mouse_enter_ = true;
430 break;
431 case ui::ET_MOUSE_EXITED:
432 mouse_exit_ = true;
433 break;
434 default:
435 NOTREACHED();
437 event->SetHandled();
440 private:
441 bool mouse_enter_;
442 bool mouse_exit_;
443 bool mouse_press_;
444 bool mouse_release_;
445 bool mouse_move_;
446 bool double_click_;
448 DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate);
451 class ScopedGestureRecognizerSetter {
452 public:
453 // Takes ownership of |new_gr|.
454 explicit ScopedGestureRecognizerSetter(ui::GestureRecognizer* new_gr)
455 : new_gr_(new_gr) {
456 original_gr_ = ui::GestureRecognizer::Get();
457 ui::SetGestureRecognizerForTesting(new_gr_.get());
460 virtual ~ScopedGestureRecognizerSetter() {
461 ui::SetGestureRecognizerForTesting(original_gr_);
464 private:
465 ui::GestureRecognizer* original_gr_;
466 scoped_ptr<ui::GestureRecognizer> new_gr_;
468 DISALLOW_COPY_AND_ASSIGN(ScopedGestureRecognizerSetter);
471 class TimedEvents {
472 private:
473 int simulated_now_;
475 public:
476 // Use a non-zero start time to pass DCHECKs which ensure events have had a
477 // time assigned.
478 TimedEvents() : simulated_now_(1) {
481 base::TimeDelta Now() {
482 base::TimeDelta t = base::TimeDelta::FromMilliseconds(simulated_now_);
483 simulated_now_++;
484 return t;
487 base::TimeDelta LeapForward(int time_in_millis) {
488 simulated_now_ += time_in_millis;
489 return base::TimeDelta::FromMilliseconds(simulated_now_);
492 base::TimeDelta InFuture(int time_in_millis) {
493 return base::TimeDelta::FromMilliseconds(simulated_now_ + time_in_millis);
496 void SendScrollEvents(ui::EventProcessor* dispatcher,
497 float x_start,
498 float y_start,
499 int dx,
500 int dy,
501 int touch_id,
502 int time_step,
503 int num_steps,
504 GestureEventConsumeDelegate* delegate) {
505 float x = x_start;
506 float y = y_start;
508 for (int i = 0; i < num_steps; i++) {
509 x += dx;
510 y += dy;
511 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
512 touch_id,
513 base::TimeDelta::FromMilliseconds(simulated_now_));
514 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
515 ASSERT_FALSE(details.dispatcher_destroyed);
516 simulated_now_ += time_step;
520 void SendScrollEvent(ui::EventProcessor* dispatcher,
521 float x,
522 float y,
523 int touch_id,
524 GestureEventConsumeDelegate* delegate) {
525 delegate->Reset();
526 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
527 touch_id,
528 base::TimeDelta::FromMilliseconds(simulated_now_));
529 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
530 ASSERT_FALSE(details.dispatcher_destroyed);
531 simulated_now_++;
535 // An event handler to keep track of events.
536 class TestEventHandler : public ui::EventHandler {
537 public:
538 TestEventHandler()
539 : touch_released_count_(0),
540 touch_pressed_count_(0),
541 touch_moved_count_(0) {}
543 ~TestEventHandler() override {}
545 void OnTouchEvent(ui::TouchEvent* event) override {
546 switch (event->type()) {
547 case ui::ET_TOUCH_RELEASED:
548 touch_released_count_++;
549 break;
550 case ui::ET_TOUCH_PRESSED:
551 touch_pressed_count_++;
552 break;
553 case ui::ET_TOUCH_MOVED:
554 touch_moved_count_++;
555 break;
556 case ui::ET_TOUCH_CANCELLED:
557 cancelled_touch_points_.push_back(event->location());
558 break;
559 default:
560 break;
564 void Reset() {
565 touch_released_count_ = 0;
566 touch_pressed_count_ = 0;
567 touch_moved_count_ = 0;
568 cancelled_touch_points_.clear();
571 int touch_released_count() const { return touch_released_count_; }
572 int touch_pressed_count() const { return touch_pressed_count_; }
573 int touch_moved_count() const { return touch_moved_count_; }
574 int touch_cancelled_count() const {
575 return static_cast<int>(cancelled_touch_points_.size());
577 const std::vector<gfx::PointF>& cancelled_touch_points() const {
578 return cancelled_touch_points_;
581 private:
582 int touch_released_count_;
583 int touch_pressed_count_;
584 int touch_moved_count_;
585 std::vector<gfx::PointF> cancelled_touch_points_;
587 DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
590 // Removes the target window from its parent when it receives a touch-cancel
591 // event.
592 class RemoveOnTouchCancelHandler : public TestEventHandler {
593 public:
594 RemoveOnTouchCancelHandler() {}
595 ~RemoveOnTouchCancelHandler() override {}
597 private:
598 // ui::EventHandler:
599 void OnTouchEvent(ui::TouchEvent* event) override {
600 TestEventHandler::OnTouchEvent(event);
601 if (event->type() == ui::ET_TOUCH_CANCELLED) {
602 Window* target = static_cast<Window*>(event->target());
603 // This is tiptoeing around crbug.com/310172. If this event handler isn't
604 // removed, we enter an infinite loop.
605 target->RemovePreTargetHandler(this);
606 target->parent()->RemoveChild(target);
610 DISALLOW_COPY_AND_ASSIGN(RemoveOnTouchCancelHandler);
613 void DelayByLongPressTimeout() {
614 ui::GestureProvider::Config config;
615 base::RunLoop run_loop;
616 base::MessageLoop::current()->PostDelayedTask(
617 FROM_HERE,
618 run_loop.QuitClosure(),
619 config.gesture_detector_config.longpress_timeout * 2);
620 run_loop.Run();
623 void DelayByShowPressTimeout() {
624 ui::GestureProvider::Config config;
625 base::RunLoop run_loop;
626 base::MessageLoop::current()->PostDelayedTask(
627 FROM_HERE,
628 run_loop.QuitClosure(),
629 config.gesture_detector_config.showpress_timeout * 2);
630 run_loop.Run();
633 } // namespace
635 class GestureRecognizerTest : public AuraTestBase,
636 public ::testing::WithParamInterface<bool> {
637 public:
638 GestureRecognizerTest() {}
640 void SetUp() override {
641 AuraTestBase::SetUp();
642 ui::GestureConfiguration::GetInstance()->set_show_press_delay_in_ms(2);
643 ui::GestureConfiguration::GetInstance()->set_long_press_time_in_ms(3);
646 private:
647 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerTest);
650 class GestureRecognizerWithSwitchTest : public GestureRecognizerTest {
651 public:
652 GestureRecognizerWithSwitchTest() {}
654 void SetUp() override {
655 GestureRecognizerTest::SetUp();
656 base::CommandLine::ForCurrentProcess()->AppendSwitch(
657 switches::kCompensateForUnstablePinchZoom);
658 ui::GestureConfiguration::GetInstance()->set_min_pinch_update_span_delta(5);
661 private:
662 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerWithSwitchTest);
665 // Check that appropriate touch events generate tap gesture events.
666 TEST_F(GestureRecognizerTest, GestureEventTap) {
667 scoped_ptr<GestureEventConsumeDelegate> delegate(
668 new GestureEventConsumeDelegate());
669 TimedEvents tes;
670 const int kWindowWidth = 123;
671 const int kWindowHeight = 45;
672 const int kTouchId = 2;
673 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
674 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
675 delegate.get(), -1234, bounds, root_window()));
677 delegate->Reset();
678 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
679 kTouchId, tes.Now());
680 DispatchEventUsingWindowDispatcher(&press);
681 EXPECT_FALSE(delegate->tap());
682 EXPECT_FALSE(delegate->show_press());
683 EXPECT_TRUE(delegate->tap_down());
684 EXPECT_FALSE(delegate->tap_cancel());
685 EXPECT_TRUE(delegate->begin());
686 EXPECT_FALSE(delegate->scroll_begin());
687 EXPECT_FALSE(delegate->scroll_update());
688 EXPECT_FALSE(delegate->scroll_end());
689 EXPECT_FALSE(delegate->long_press());
691 delegate->Reset();
692 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
693 EXPECT_TRUE(delegate->show_press());
694 EXPECT_FALSE(delegate->tap_down());
696 // Make sure there is enough delay before the touch is released so that it is
697 // recognized as a tap.
698 delegate->Reset();
699 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
700 kTouchId, tes.LeapForward(50));
702 DispatchEventUsingWindowDispatcher(&release);
703 EXPECT_TRUE(delegate->tap());
704 EXPECT_FALSE(delegate->tap_down());
705 EXPECT_FALSE(delegate->tap_cancel());
706 EXPECT_FALSE(delegate->begin());
707 EXPECT_TRUE(delegate->end());
708 EXPECT_FALSE(delegate->scroll_begin());
709 EXPECT_FALSE(delegate->scroll_update());
710 EXPECT_FALSE(delegate->scroll_end());
712 EXPECT_EQ(1, delegate->tap_count());
715 // Check that appropriate touch events generate tap gesture events
716 // when information about the touch radii are provided.
717 TEST_F(GestureRecognizerTest, GestureEventTapRegion) {
718 scoped_ptr<GestureEventConsumeDelegate> delegate(
719 new GestureEventConsumeDelegate());
720 TimedEvents tes;
721 const int kWindowWidth = 800;
722 const int kWindowHeight = 600;
723 const int kTouchId = 2;
724 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
725 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
726 delegate.get(), -1234, bounds, root_window()));
728 // Test with no ET_TOUCH_MOVED events.
730 delegate->Reset();
731 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
732 kTouchId, tes.Now());
733 press.set_radius_x(5);
734 press.set_radius_y(12);
735 DispatchEventUsingWindowDispatcher(&press);
736 EXPECT_FALSE(delegate->tap());
737 EXPECT_TRUE(delegate->tap_down());
738 EXPECT_FALSE(delegate->tap_cancel());
739 EXPECT_TRUE(delegate->begin());
740 EXPECT_FALSE(delegate->scroll_begin());
741 EXPECT_FALSE(delegate->scroll_update());
742 EXPECT_FALSE(delegate->scroll_end());
743 EXPECT_FALSE(delegate->long_press());
745 // Make sure there is enough delay before the touch is released so that it
746 // is recognized as a tap.
747 delegate->Reset();
748 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
749 kTouchId, tes.LeapForward(50));
750 release.set_radius_x(5);
751 release.set_radius_y(12);
753 DispatchEventUsingWindowDispatcher(&release);
754 EXPECT_TRUE(delegate->tap());
755 EXPECT_FALSE(delegate->tap_down());
756 EXPECT_FALSE(delegate->tap_cancel());
757 EXPECT_FALSE(delegate->begin());
758 EXPECT_TRUE(delegate->end());
759 EXPECT_FALSE(delegate->scroll_begin());
760 EXPECT_FALSE(delegate->scroll_update());
761 EXPECT_FALSE(delegate->scroll_end());
763 EXPECT_EQ(1, delegate->tap_count());
764 gfx::Point actual_point(delegate->tap_location());
765 EXPECT_EQ(24, delegate->bounding_box().width());
766 EXPECT_EQ(24, delegate->bounding_box().height());
767 EXPECT_EQ(101, actual_point.x());
768 EXPECT_EQ(201, actual_point.y());
771 // Test with no ET_TOUCH_MOVED events but different touch points and radii.
773 delegate->Reset();
774 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(365, 290),
775 kTouchId, tes.Now());
776 press.set_radius_x(8);
777 press.set_radius_y(14);
778 DispatchEventUsingWindowDispatcher(&press);
779 EXPECT_FALSE(delegate->tap());
780 EXPECT_TRUE(delegate->tap_down());
781 EXPECT_FALSE(delegate->tap_cancel());
782 EXPECT_TRUE(delegate->begin());
783 EXPECT_FALSE(delegate->scroll_begin());
784 EXPECT_FALSE(delegate->scroll_update());
785 EXPECT_FALSE(delegate->scroll_end());
786 EXPECT_FALSE(delegate->long_press());
788 delegate->Reset();
789 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(367, 291),
790 kTouchId, tes.LeapForward(50));
791 release.set_radius_x(20);
792 release.set_radius_y(13);
794 DispatchEventUsingWindowDispatcher(&release);
795 EXPECT_TRUE(delegate->tap());
796 EXPECT_FALSE(delegate->tap_down());
797 EXPECT_FALSE(delegate->tap_cancel());
798 EXPECT_FALSE(delegate->begin());
799 EXPECT_TRUE(delegate->end());
800 EXPECT_FALSE(delegate->scroll_begin());
801 EXPECT_FALSE(delegate->scroll_update());
802 EXPECT_FALSE(delegate->scroll_end());
804 EXPECT_EQ(1, delegate->tap_count());
805 gfx::Point actual_point(delegate->tap_location());
806 EXPECT_EQ(40, delegate->bounding_box().width());
807 EXPECT_EQ(40, delegate->bounding_box().height());
808 EXPECT_EQ(367, actual_point.x());
809 EXPECT_EQ(291, actual_point.y());
812 // Test with a single ET_TOUCH_MOVED event.
814 delegate->Reset();
815 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(46, 205),
816 kTouchId, tes.Now());
817 press.set_radius_x(6);
818 press.set_radius_y(10);
819 DispatchEventUsingWindowDispatcher(&press);
820 EXPECT_FALSE(delegate->tap());
821 EXPECT_TRUE(delegate->tap_down());
822 EXPECT_FALSE(delegate->tap_cancel());
823 EXPECT_TRUE(delegate->begin());
824 EXPECT_FALSE(delegate->tap_cancel());
825 EXPECT_FALSE(delegate->scroll_begin());
826 EXPECT_FALSE(delegate->scroll_update());
827 EXPECT_FALSE(delegate->scroll_end());
828 EXPECT_FALSE(delegate->long_press());
830 delegate->Reset();
831 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(49, 204),
832 kTouchId, tes.LeapForward(50));
833 move.set_radius_x(8);
834 move.set_radius_y(12);
835 DispatchEventUsingWindowDispatcher(&move);
836 EXPECT_FALSE(delegate->tap());
837 EXPECT_FALSE(delegate->tap_down());
838 EXPECT_FALSE(delegate->tap_cancel());
839 EXPECT_FALSE(delegate->begin());
840 EXPECT_FALSE(delegate->scroll_begin());
841 EXPECT_FALSE(delegate->scroll_update());
842 EXPECT_FALSE(delegate->scroll_end());
843 EXPECT_FALSE(delegate->long_press());
845 delegate->Reset();
846 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(49, 204),
847 kTouchId, tes.LeapForward(50));
848 release.set_radius_x(4);
849 release.set_radius_y(8);
851 DispatchEventUsingWindowDispatcher(&release);
852 EXPECT_TRUE(delegate->tap());
853 EXPECT_FALSE(delegate->tap_down());
854 EXPECT_FALSE(delegate->tap_cancel());
855 EXPECT_FALSE(delegate->begin());
856 EXPECT_TRUE(delegate->end());
857 EXPECT_FALSE(delegate->scroll_begin());
858 EXPECT_FALSE(delegate->scroll_update());
859 EXPECT_FALSE(delegate->scroll_end());
861 EXPECT_EQ(1, delegate->tap_count());
862 gfx::Point actual_point(delegate->tap_location());
863 EXPECT_EQ(16, delegate->bounding_box().width());
864 EXPECT_EQ(16, delegate->bounding_box().height());
865 EXPECT_EQ(49, actual_point.x());
866 EXPECT_EQ(204, actual_point.y());
869 // Test with a few ET_TOUCH_MOVED events.
871 delegate->Reset();
872 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(400, 150),
873 kTouchId, tes.Now());
874 press.set_radius_x(7);
875 press.set_radius_y(10);
876 DispatchEventUsingWindowDispatcher(&press);
877 EXPECT_FALSE(delegate->tap());
878 EXPECT_TRUE(delegate->tap_down());
879 EXPECT_FALSE(delegate->tap_cancel());
880 EXPECT_TRUE(delegate->begin());
881 EXPECT_FALSE(delegate->scroll_begin());
882 EXPECT_FALSE(delegate->scroll_update());
883 EXPECT_FALSE(delegate->scroll_end());
884 EXPECT_FALSE(delegate->long_press());
886 delegate->Reset();
887 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(397, 151),
888 kTouchId, tes.LeapForward(50));
889 move.set_radius_x(13);
890 move.set_radius_y(12);
891 DispatchEventUsingWindowDispatcher(&move);
892 EXPECT_FALSE(delegate->tap());
893 EXPECT_FALSE(delegate->tap_down());
894 EXPECT_FALSE(delegate->tap_cancel());
895 EXPECT_FALSE(delegate->begin());
896 EXPECT_FALSE(delegate->scroll_begin());
897 EXPECT_FALSE(delegate->scroll_update());
898 EXPECT_FALSE(delegate->scroll_end());
899 EXPECT_FALSE(delegate->long_press());
901 delegate->Reset();
902 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(397, 149),
903 kTouchId, tes.LeapForward(50));
904 move1.set_radius_x(16);
905 move1.set_radius_y(16);
906 DispatchEventUsingWindowDispatcher(&move1);
907 EXPECT_FALSE(delegate->tap());
908 EXPECT_FALSE(delegate->tap_down());
909 EXPECT_FALSE(delegate->tap_cancel());
910 EXPECT_FALSE(delegate->begin());
911 EXPECT_FALSE(delegate->scroll_begin());
912 EXPECT_FALSE(delegate->scroll_update());
913 EXPECT_FALSE(delegate->scroll_end());
914 EXPECT_FALSE(delegate->long_press());
916 delegate->Reset();
917 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(400, 150),
918 kTouchId, tes.LeapForward(50));
919 move2.set_radius_x(14);
920 move2.set_radius_y(10);
921 DispatchEventUsingWindowDispatcher(&move2);
922 EXPECT_FALSE(delegate->tap());
923 EXPECT_FALSE(delegate->tap_down());
924 EXPECT_FALSE(delegate->tap_cancel());
925 EXPECT_FALSE(delegate->begin());
926 EXPECT_FALSE(delegate->scroll_begin());
927 EXPECT_FALSE(delegate->scroll_update());
928 EXPECT_FALSE(delegate->scroll_end());
929 EXPECT_FALSE(delegate->long_press());
931 delegate->Reset();
932 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(401, 149),
933 kTouchId, tes.LeapForward(50));
934 release.set_radius_x(8);
935 release.set_radius_y(9);
937 DispatchEventUsingWindowDispatcher(&release);
938 EXPECT_TRUE(delegate->tap());
939 EXPECT_FALSE(delegate->tap_down());
940 EXPECT_FALSE(delegate->tap_cancel());
941 EXPECT_FALSE(delegate->begin());
942 EXPECT_TRUE(delegate->end());
943 EXPECT_FALSE(delegate->scroll_begin());
944 EXPECT_FALSE(delegate->scroll_update());
945 EXPECT_FALSE(delegate->scroll_end());
947 EXPECT_EQ(1, delegate->tap_count());
948 gfx::Point actual_point(delegate->tap_location());
949 EXPECT_EQ(18, delegate->bounding_box().width());
950 EXPECT_EQ(18, delegate->bounding_box().height());
951 EXPECT_EQ(401, actual_point.x());
952 EXPECT_EQ(149, actual_point.y());
956 // Check that appropriate touch events generate scroll gesture events.
957 TEST_F(GestureRecognizerTest, GestureEventScroll) {
958 // We'll start by moving the touch point by (10.5, 10.5). We want 5 dips of
959 // that distance to be consumed by the slop, so we set the slop radius to
960 // sqrt(5 * 5 + 5 * 5).
961 ui::GestureConfiguration::GetInstance()
962 ->set_max_touch_move_in_pixels_for_click(sqrt(5.f * 5 + 5 * 5));
963 scoped_ptr<GestureEventConsumeDelegate> delegate(
964 new GestureEventConsumeDelegate());
965 TimedEvents tes;
966 const int kWindowWidth = 123;
967 const int kWindowHeight = 45;
968 const int kTouchId = 5;
969 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
970 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
971 delegate.get(), -1234, bounds, root_window()));
973 delegate->Reset();
974 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
975 kTouchId, tes.Now());
976 DispatchEventUsingWindowDispatcher(&press);
977 EXPECT_2_EVENTS(delegate->events(),
978 ui::ET_GESTURE_BEGIN,
979 ui::ET_GESTURE_TAP_DOWN);
981 // Move the touch-point enough so that it is considered as a scroll. This
982 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
983 // The first movement is diagonal, to ensure that we have a free scroll,
984 // and not a rail scroll.
985 tes.SendScrollEvent(event_processor(), 111.5, 211.5, kTouchId,
986 delegate.get());
987 EXPECT_3_EVENTS(delegate->events(),
988 ui::ET_GESTURE_TAP_CANCEL,
989 ui::ET_GESTURE_SCROLL_BEGIN,
990 ui::ET_GESTURE_SCROLL_UPDATE);
991 // The slop consumed 5 dips
992 EXPECT_FLOAT_EQ(5.5, delegate->scroll_x());
993 EXPECT_FLOAT_EQ(5.5, delegate->scroll_y());
994 EXPECT_EQ(gfx::Point(1, 1).ToString(),
995 delegate->scroll_begin_position().ToString());
997 // When scrolling with a single finger, the bounding box of the gesture should
998 // be empty, since it's a single point and the radius for testing is zero.
999 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1001 // Move some more to generate a few more scroll updates. Make sure that we get
1002 // out of the snap channel for the unified GR.
1003 tes.SendScrollEvent(event_processor(), 20, 120, kTouchId, delegate.get());
1004 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1005 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_x());
1006 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_y());
1007 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1009 tes.SendScrollEvent(event_processor(), 50, 124, kTouchId, delegate.get());
1010 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1011 EXPECT_EQ(30, delegate->scroll_x());
1012 EXPECT_EQ(4, delegate->scroll_y());
1013 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1015 // Release the touch. This should end the scroll.
1016 delegate->Reset();
1017 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1018 kTouchId,
1019 tes.LeapForward(50));
1020 DispatchEventUsingWindowDispatcher(&release);
1021 EXPECT_2_EVENTS(delegate->events(),
1022 ui::ET_SCROLL_FLING_START,
1023 ui::ET_GESTURE_END);
1024 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1027 // Check that predicted scroll update positions are correct.
1028 TEST_F(GestureRecognizerTest, GestureEventScrollPrediction) {
1029 // We'll start by moving the touch point by (5, 5). We want all of that
1030 // distance to be consumed by the slop, so we set the slop radius to
1031 // sqrt(5 * 5 + 5 * 5).
1032 ui::GestureConfiguration::GetInstance()
1033 ->set_max_touch_move_in_pixels_for_click(sqrt(5.f * 5 + 5 * 5));
1035 scoped_ptr<GestureEventConsumeDelegate> delegate(
1036 new GestureEventConsumeDelegate());
1037 TimedEvents tes;
1038 const int kWindowWidth = 123;
1039 const int kWindowHeight = 45;
1040 const int kTouchId = 5;
1041 gfx::Rect bounds(95, 195, kWindowWidth, kWindowHeight);
1042 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1043 delegate.get(), -1234, bounds, root_window()));
1045 delegate->Reset();
1046 // Tracks the total scroll since we want to verify that the correct position
1047 // will be scrolled to throughout the prediction.
1048 gfx::Vector2dF total_scroll;
1049 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(96, 196),
1050 kTouchId, tes.Now());
1051 DispatchEventUsingWindowDispatcher(&press);
1052 EXPECT_2_EVENTS(delegate->events(),
1053 ui::ET_GESTURE_BEGIN,
1054 ui::ET_GESTURE_TAP_DOWN);
1055 delegate->Reset();
1057 // Get rid of touch slop.
1058 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(111, 211),
1059 kTouchId, tes.Now());
1060 DispatchEventUsingWindowDispatcher(&move);
1061 EXPECT_3_EVENTS(delegate->events(),
1062 ui::ET_GESTURE_TAP_CANCEL,
1063 ui::ET_GESTURE_SCROLL_BEGIN,
1064 ui::ET_GESTURE_SCROLL_UPDATE);
1065 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1066 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1068 // Move the touch-point enough so that it is considered as a scroll. This
1069 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1070 // The first movement is diagonal, to ensure that we have a free scroll,
1071 // and not a rail scroll.
1072 tes.LeapForward(30);
1073 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1074 EXPECT_1_EVENT(delegate->events(),
1075 ui::ET_GESTURE_SCROLL_UPDATE);
1076 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1077 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1079 // Move some more to generate a few more scroll updates.
1080 tes.LeapForward(30);
1081 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
1082 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1083 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1084 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1086 tes.LeapForward(30);
1087 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
1088 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1089 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1090 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1092 // Release the touch. This should end the scroll.
1093 delegate->Reset();
1094 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1095 kTouchId,
1096 tes.LeapForward(50));
1097 DispatchEventUsingWindowDispatcher(&release);
1100 // Check that the bounding box during a scroll event is correct.
1101 TEST_F(GestureRecognizerTest, GestureEventScrollBoundingBox) {
1102 TimedEvents tes;
1103 for (float radius = 1; radius <= 10; ++radius) {
1104 ui::GestureConfiguration::GetInstance()->set_default_radius(radius);
1105 scoped_ptr<GestureEventConsumeDelegate> delegate(
1106 new GestureEventConsumeDelegate());
1107 const int kWindowWidth = 123;
1108 const int kWindowHeight = 45;
1109 const int kTouchId = 5;
1110 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1111 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1112 delegate.get(), -1234, bounds, root_window()));
1114 const float kPositionX = 101;
1115 const float kPositionY = 201;
1116 delegate->Reset();
1117 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
1118 gfx::PointF(kPositionX, kPositionY),
1119 kTouchId,
1120 tes.Now());
1121 DispatchEventUsingWindowDispatcher(&press);
1122 EXPECT_EQ(gfx::RectF(kPositionX - radius,
1123 kPositionY - radius,
1124 radius * 2,
1125 radius * 2),
1126 delegate->bounding_box());
1128 const int kScrollAmount = 50;
1129 tes.SendScrollEvents(event_processor(), kPositionX, kPositionY,
1130 1, 1, kTouchId, 1, kScrollAmount, delegate.get());
1131 EXPECT_EQ(gfx::Point(1, 1).ToString(),
1132 delegate->scroll_begin_position().ToString());
1133 EXPECT_EQ(gfx::RectF(kPositionX + kScrollAmount - radius,
1134 kPositionY + kScrollAmount - radius,
1135 radius * 2,
1136 radius * 2),
1137 delegate->bounding_box());
1139 // Release the touch. This should end the scroll.
1140 delegate->Reset();
1141 ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
1142 gfx::PointF(kPositionX + kScrollAmount,
1143 kPositionY + kScrollAmount),
1144 kTouchId, press.time_stamp() +
1145 base::TimeDelta::FromMilliseconds(50));
1146 DispatchEventUsingWindowDispatcher(&release);
1147 EXPECT_EQ(gfx::RectF(kPositionX + kScrollAmount - radius,
1148 kPositionY + kScrollAmount - radius,
1149 radius * 2,
1150 radius * 2),
1151 delegate->bounding_box());
1153 ui::GestureConfiguration::GetInstance()->set_default_radius(0);
1156 // Check Scroll End Events report correct velocities
1157 // if the user was on a horizontal rail
1158 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailFling) {
1159 scoped_ptr<GestureEventConsumeDelegate> delegate(
1160 new GestureEventConsumeDelegate());
1161 TimedEvents tes;
1162 const int kTouchId = 7;
1163 gfx::Rect bounds(0, 0, 1000, 1000);
1164 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1165 delegate.get(), -1234, bounds, root_window()));
1167 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1168 kTouchId, tes.Now());
1169 DispatchEventUsingWindowDispatcher(&press);
1171 // Get rid of touch slop.
1172 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 0),
1173 kTouchId, tes.Now());
1174 DispatchEventUsingWindowDispatcher(&move);
1175 delegate->Reset();
1178 // Move the touch-point horizontally enough that it is considered a
1179 // horizontal scroll.
1180 tes.SendScrollEvent(event_processor(), 30, 1, kTouchId, delegate.get());
1181 EXPECT_FLOAT_EQ(0, delegate->scroll_y());
1182 EXPECT_FLOAT_EQ(20, delegate->scroll_x());
1184 // Get a high x velocity, while still staying on the rail
1185 const int kScrollAmount = 8;
1186 tes.SendScrollEvents(event_processor(),
1189 100,
1191 kTouchId,
1193 kScrollAmount,
1194 delegate.get());
1196 delegate->Reset();
1197 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1198 kTouchId, tes.Now());
1199 DispatchEventUsingWindowDispatcher(&release);
1201 EXPECT_TRUE(delegate->fling());
1202 EXPECT_FALSE(delegate->scroll_end());
1203 EXPECT_GT(delegate->velocity_x(), 0);
1204 EXPECT_EQ(0, delegate->velocity_y());
1207 // Check Scroll End Events report correct velocities
1208 // if the user was on a vertical rail
1209 TEST_F(GestureRecognizerTest, GestureEventVerticalRailFling) {
1210 scoped_ptr<GestureEventConsumeDelegate> delegate(
1211 new GestureEventConsumeDelegate());
1212 TimedEvents tes;
1213 const int kTouchId = 7;
1214 gfx::Rect bounds(0, 0, 1000, 1000);
1215 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1216 delegate.get(), -1234, bounds, root_window()));
1218 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1219 kTouchId, tes.Now());
1220 DispatchEventUsingWindowDispatcher(&press);
1222 // Get rid of touch slop.
1223 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 10),
1224 kTouchId, tes.Now());
1225 DispatchEventUsingWindowDispatcher(&move);
1226 delegate->Reset();
1228 // Move the touch-point vertically enough that it is considered a
1229 // vertical scroll.
1230 tes.SendScrollEvent(event_processor(), 1, 30, kTouchId, delegate.get());
1231 EXPECT_EQ(20, delegate->scroll_y());
1232 EXPECT_EQ(0, delegate->scroll_x());
1233 EXPECT_EQ(0, delegate->scroll_velocity_x());
1235 // Get a high y velocity, while still staying on the rail
1236 const int kScrollAmount = 8;
1237 tes.SendScrollEvents(event_processor(),
1241 100,
1242 kTouchId,
1244 kScrollAmount,
1245 delegate.get());
1246 EXPECT_EQ(0, delegate->scroll_velocity_x());
1248 delegate->Reset();
1249 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 206),
1250 kTouchId, tes.Now());
1251 DispatchEventUsingWindowDispatcher(&release);
1253 EXPECT_TRUE(delegate->fling());
1254 EXPECT_FALSE(delegate->scroll_end());
1255 EXPECT_EQ(0, delegate->velocity_x());
1256 EXPECT_GT(delegate->velocity_y(), 0);
1259 // Check Scroll End Events report non-zero velocities if the user is not on a
1260 // rail
1261 TEST_F(GestureRecognizerTest, GestureEventNonRailFling) {
1262 ui::GestureConfiguration::GetInstance()
1263 ->set_max_touch_move_in_pixels_for_click(0);
1264 scoped_ptr<GestureEventConsumeDelegate> delegate(
1265 new GestureEventConsumeDelegate());
1266 TimedEvents tes;
1267 const int kTouchId = 7;
1268 gfx::Rect bounds(0, 0, 1000, 1000);
1269 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1270 delegate.get(), -1234, bounds, root_window()));
1272 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1273 kTouchId, tes.Now());
1274 DispatchEventUsingWindowDispatcher(&press);
1276 // Move the touch-point such that a non-rail scroll begins, and we're outside
1277 // the snap channel for the unified GR.
1278 tes.SendScrollEvent(event_processor(), 50, 50, kTouchId, delegate.get());
1279 EXPECT_EQ(50, delegate->scroll_y());
1280 EXPECT_EQ(50, delegate->scroll_x());
1282 const int kScrollAmount = 8;
1283 tes.SendScrollEvents(event_processor(),
1287 100,
1288 kTouchId,
1290 kScrollAmount,
1291 delegate.get());
1293 delegate->Reset();
1294 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1295 kTouchId, tes.Now());
1296 DispatchEventUsingWindowDispatcher(&release);
1298 EXPECT_TRUE(delegate->fling());
1299 EXPECT_FALSE(delegate->scroll_end());
1300 EXPECT_GT(delegate->velocity_x(), 0);
1301 EXPECT_GT(delegate->velocity_y(), 0);
1304 // Check that appropriate touch events generate long press events
1305 TEST_F(GestureRecognizerTest, GestureEventLongPress) {
1306 scoped_ptr<GestureEventConsumeDelegate> delegate(
1307 new GestureEventConsumeDelegate());
1308 const int kWindowWidth = 123;
1309 const int kWindowHeight = 45;
1310 const int kTouchId = 2;
1311 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1312 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1313 delegate.get(), -1234, bounds, root_window()));
1315 delegate->Reset();
1317 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1318 gfx::Point(101, 201),
1319 kTouchId,
1320 ui::EventTimeForNow());
1321 DispatchEventUsingWindowDispatcher(&press1);
1322 EXPECT_TRUE(delegate->tap_down());
1323 EXPECT_TRUE(delegate->begin());
1324 EXPECT_FALSE(delegate->tap_cancel());
1326 // We haven't pressed long enough for a long press to occur
1327 EXPECT_FALSE(delegate->long_press());
1329 // Wait until the timer runs out
1330 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1331 EXPECT_TRUE(delegate->long_press());
1332 EXPECT_FALSE(delegate->tap_cancel());
1334 delegate->Reset();
1335 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1336 gfx::Point(101, 201),
1337 kTouchId,
1338 ui::EventTimeForNow());
1339 DispatchEventUsingWindowDispatcher(&release1);
1340 EXPECT_FALSE(delegate->long_press());
1342 // Note the tap cancel isn't dispatched until the release
1343 EXPECT_TRUE(delegate->tap_cancel());
1344 EXPECT_FALSE(delegate->tap());
1347 // Check that scrolling prevents a long press.
1348 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledByScroll) {
1349 scoped_ptr<GestureEventConsumeDelegate> delegate(
1350 new GestureEventConsumeDelegate());
1351 TimedEvents tes;
1352 const int kWindowWidth = 123;
1353 const int kWindowHeight = 45;
1354 const int kTouchId = 6;
1355 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1356 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1357 delegate.get(), -1234, bounds, root_window()));
1359 delegate->Reset();
1361 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1362 kTouchId, tes.Now());
1363 DispatchEventUsingWindowDispatcher(&press1);
1364 EXPECT_TRUE(delegate->tap_down());
1366 // We haven't pressed long enough for a long press to occur
1367 EXPECT_FALSE(delegate->long_press());
1368 EXPECT_FALSE(delegate->tap_cancel());
1370 // Scroll around, to cancel the long press
1371 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1373 // Wait until a long press event would have fired, if it hadn't been
1374 // cancelled.
1375 DelayByLongPressTimeout();
1377 EXPECT_FALSE(delegate->long_press());
1378 EXPECT_TRUE(delegate->tap_cancel());
1380 delegate->Reset();
1381 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1382 kTouchId, tes.LeapForward(10));
1383 DispatchEventUsingWindowDispatcher(&release1);
1384 EXPECT_FALSE(delegate->long_press());
1385 EXPECT_FALSE(delegate->tap_cancel());
1388 // Check that appropriate touch events generate long tap events
1389 TEST_F(GestureRecognizerTest, GestureEventLongTap) {
1390 ui::GestureConfiguration::GetInstance()
1391 ->set_max_touch_down_duration_for_click_in_ms(3);
1392 scoped_ptr<GestureEventConsumeDelegate> delegate(
1393 new GestureEventConsumeDelegate());
1394 const int kWindowWidth = 123;
1395 const int kWindowHeight = 45;
1396 const int kTouchId = 2;
1397 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1398 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1399 delegate.get(), -1234, bounds, root_window()));
1401 delegate->Reset();
1403 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1404 gfx::Point(101, 201),
1405 kTouchId,
1406 ui::EventTimeForNow());
1407 DispatchEventUsingWindowDispatcher(&press1);
1408 EXPECT_TRUE(delegate->tap_down());
1409 EXPECT_TRUE(delegate->begin());
1410 EXPECT_FALSE(delegate->tap_cancel());
1412 // We haven't pressed long enough for a long press to occur
1413 EXPECT_FALSE(delegate->long_press());
1415 // Wait until the timer runs out
1416 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1417 EXPECT_TRUE(delegate->long_press());
1418 EXPECT_FALSE(delegate->tap_cancel());
1420 delegate->Reset();
1421 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1422 gfx::Point(101, 201),
1423 kTouchId,
1424 ui::EventTimeForNow());
1425 DispatchEventUsingWindowDispatcher(&release1);
1426 EXPECT_FALSE(delegate->long_press());
1427 EXPECT_TRUE(delegate->long_tap());
1429 // Note the tap cancel isn't dispatched until the release
1430 EXPECT_TRUE(delegate->tap_cancel());
1431 EXPECT_FALSE(delegate->tap());
1434 // Check that second tap cancels a long press
1435 TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledBySecondTap) {
1436 scoped_ptr<GestureEventConsumeDelegate> delegate(
1437 new GestureEventConsumeDelegate());
1438 TimedEvents tes;
1439 const int kWindowWidth = 300;
1440 const int kWindowHeight = 400;
1441 const int kTouchId1 = 8;
1442 const int kTouchId2 = 2;
1443 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1444 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1445 delegate.get(), -1234, bounds, root_window()));
1447 delegate->Reset();
1448 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1449 kTouchId1, tes.Now());
1450 DispatchEventUsingWindowDispatcher(&press);
1451 EXPECT_TRUE(delegate->tap_down());
1452 EXPECT_TRUE(delegate->begin());
1454 // We haven't pressed long enough for a long press to occur
1455 EXPECT_FALSE(delegate->long_press());
1457 // Second tap, to cancel the long press
1458 delegate->Reset();
1459 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1460 kTouchId2, tes.Now());
1461 DispatchEventUsingWindowDispatcher(&press2);
1462 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
1463 EXPECT_TRUE(delegate->tap_cancel());
1464 EXPECT_TRUE(delegate->begin());
1466 // Wait until the timer runs out
1467 DelayByLongPressTimeout();
1469 // No long press occurred
1470 EXPECT_FALSE(delegate->long_press());
1472 delegate->Reset();
1473 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1474 kTouchId1, tes.Now());
1475 DispatchEventUsingWindowDispatcher(&release1);
1476 EXPECT_FALSE(delegate->long_press());
1477 EXPECT_TRUE(delegate->two_finger_tap());
1478 EXPECT_FALSE(delegate->tap_cancel());
1481 // Check that horizontal scroll gestures cause scrolls on horizontal rails.
1482 // Also tests that horizontal rails can be broken.
1483 TEST_F(GestureRecognizerTest, GestureEventHorizontalRailScroll) {
1484 scoped_ptr<GestureEventConsumeDelegate> delegate(
1485 new GestureEventConsumeDelegate());
1486 TimedEvents tes;
1487 const int kTouchId = 7;
1488 gfx::Rect bounds(0, 0, 1000, 1000);
1489 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1490 delegate.get(), -1234, bounds, root_window()));
1492 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1493 kTouchId, tes.Now());
1494 DispatchEventUsingWindowDispatcher(&press);
1496 // Get rid of touch slop.
1497 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(5, 0),
1498 kTouchId, tes.Now());
1500 DispatchEventUsingWindowDispatcher(&move);
1501 delegate->Reset();
1503 // Move the touch-point horizontally enough that it is considered a
1504 // horizontal scroll.
1505 tes.SendScrollEvent(event_processor(), 25, 0, kTouchId, delegate.get());
1506 EXPECT_EQ(0, delegate->scroll_y());
1507 EXPECT_EQ(20, delegate->scroll_x());
1509 tes.SendScrollEvent(event_processor(), 30, 6, kTouchId, delegate.get());
1510 EXPECT_TRUE(delegate->scroll_update());
1511 EXPECT_EQ(5, delegate->scroll_x());
1512 // y shouldn't change, as we're on a horizontal rail.
1513 EXPECT_EQ(0, delegate->scroll_y());
1515 // Send enough information that a velocity can be calculated for the gesture,
1516 // and we can break the rail
1517 const int kScrollAmount = 8;
1518 tes.SendScrollEvents(event_processor(),
1522 100,
1523 kTouchId,
1525 kScrollAmount,
1526 delegate.get());
1528 tes.SendScrollEvent(event_processor(), 5, 0, kTouchId, delegate.get());
1529 tes.SendScrollEvent(event_processor(), 10, 5, kTouchId, delegate.get());
1531 // The rail should be broken
1532 EXPECT_TRUE(delegate->scroll_update());
1533 EXPECT_EQ(5, delegate->scroll_x());
1534 EXPECT_EQ(5, delegate->scroll_y());
1537 // Check that vertical scroll gestures cause scrolls on vertical rails.
1538 // Also tests that vertical rails can be broken.
1539 TEST_F(GestureRecognizerTest, GestureEventVerticalRailScroll) {
1540 scoped_ptr<GestureEventConsumeDelegate> delegate(
1541 new GestureEventConsumeDelegate());
1542 TimedEvents tes;
1543 const int kTouchId = 7;
1544 gfx::Rect bounds(0, 0, 1000, 1000);
1545 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1546 delegate.get(), -1234, bounds, root_window()));
1548 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1549 kTouchId, tes.Now());
1550 DispatchEventUsingWindowDispatcher(&press);
1552 // Get rid of touch slop.
1553 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 5),
1554 kTouchId, tes.Now());
1555 DispatchEventUsingWindowDispatcher(&move);
1556 delegate->Reset();
1558 // Move the touch-point vertically enough that it is considered a
1559 // vertical scroll.
1560 tes.SendScrollEvent(event_processor(), 0, 25, kTouchId, delegate.get());
1561 EXPECT_EQ(0, delegate->scroll_x());
1562 EXPECT_EQ(20, delegate->scroll_y());
1564 tes.SendScrollEvent(event_processor(), 6, 30, kTouchId, delegate.get());
1565 EXPECT_TRUE(delegate->scroll_update());
1566 EXPECT_EQ(5, delegate->scroll_y());
1567 // x shouldn't change, as we're on a vertical rail.
1568 EXPECT_EQ(0, delegate->scroll_x());
1569 EXPECT_EQ(0, delegate->scroll_velocity_x());
1571 // Send enough information that a velocity can be calculated for the gesture,
1572 // and we can break the rail
1573 const int kScrollAmount = 8;
1574 tes.SendScrollEvents(event_processor(),
1577 100,
1579 kTouchId,
1581 kScrollAmount,
1582 delegate.get());
1584 tes.SendScrollEvent(event_processor(), 0, 5, kTouchId, delegate.get());
1585 tes.SendScrollEvent(event_processor(), 5, 10, kTouchId, delegate.get());
1587 // The rail should be broken
1588 EXPECT_TRUE(delegate->scroll_update());
1589 EXPECT_EQ(5, delegate->scroll_x());
1590 EXPECT_EQ(5, delegate->scroll_y());
1593 TEST_F(GestureRecognizerTest, GestureTapFollowedByScroll) {
1594 // We'll start by moving the touch point by (5, 5). We want all of that
1595 // distance to be consumed by the slop, so we set the slop radius to
1596 // sqrt(5 * 5 + 5 * 5).
1597 ui::GestureConfiguration::GetInstance()
1598 ->set_max_touch_move_in_pixels_for_click(sqrt(5.f * 5 + 5 * 5));
1600 // First, tap. Then, do a scroll using the same touch-id.
1601 scoped_ptr<GestureEventConsumeDelegate> delegate(
1602 new GestureEventConsumeDelegate());
1603 TimedEvents tes;
1604 const int kWindowWidth = 123;
1605 const int kWindowHeight = 45;
1606 const int kTouchId = 3;
1607 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1608 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1609 delegate.get(), -1234, bounds, root_window()));
1611 delegate->Reset();
1612 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1613 kTouchId, tes.Now());
1614 DispatchEventUsingWindowDispatcher(&press);
1615 EXPECT_FALSE(delegate->tap());
1616 EXPECT_TRUE(delegate->tap_down());
1617 EXPECT_FALSE(delegate->tap_cancel());
1618 EXPECT_FALSE(delegate->scroll_begin());
1619 EXPECT_FALSE(delegate->scroll_update());
1620 EXPECT_FALSE(delegate->scroll_end());
1622 // Make sure there is enough delay before the touch is released so that it is
1623 // recognized as a tap.
1624 delegate->Reset();
1625 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1626 kTouchId, tes.LeapForward(50));
1627 DispatchEventUsingWindowDispatcher(&release);
1628 EXPECT_TRUE(delegate->tap());
1629 EXPECT_FALSE(delegate->tap_down());
1630 EXPECT_FALSE(delegate->tap_cancel());
1631 EXPECT_FALSE(delegate->scroll_begin());
1632 EXPECT_FALSE(delegate->scroll_update());
1633 EXPECT_FALSE(delegate->scroll_end());
1635 // Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger
1636 // a double-tap.
1637 delegate->Reset();
1638 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1639 kTouchId, tes.LeapForward(1000));
1640 DispatchEventUsingWindowDispatcher(&press1);
1641 EXPECT_FALSE(delegate->tap());
1642 EXPECT_TRUE(delegate->tap_down());
1643 EXPECT_FALSE(delegate->tap_cancel());
1644 EXPECT_FALSE(delegate->scroll_begin());
1645 EXPECT_FALSE(delegate->scroll_update());
1646 EXPECT_FALSE(delegate->scroll_end());
1648 // Get rid of touch slop.
1649 ui::TouchEvent move_remove_slop(ui::ET_TOUCH_MOVED, gfx::Point(116, 216),
1650 kTouchId, tes.Now());
1651 DispatchEventUsingWindowDispatcher(&move_remove_slop);
1652 EXPECT_TRUE(delegate->tap_cancel());
1653 EXPECT_TRUE(delegate->scroll_begin());
1654 EXPECT_TRUE(delegate->scroll_update());
1655 EXPECT_EQ(15, delegate->scroll_x_hint());
1656 EXPECT_EQ(15, delegate->scroll_y_hint());
1658 delegate->Reset();
1660 // Move the touch-point enough so that it is considered as a scroll. This
1661 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1662 // The first movement is diagonal, to ensure that we have a free scroll,
1663 // and not a rail scroll.
1664 delegate->Reset();
1665 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(135, 235),
1666 kTouchId, tes.Now());
1667 DispatchEventUsingWindowDispatcher(&move);
1668 EXPECT_FALSE(delegate->tap());
1669 EXPECT_FALSE(delegate->tap_down());
1670 EXPECT_FALSE(delegate->tap_cancel());
1671 EXPECT_FALSE(delegate->scroll_begin());
1672 EXPECT_TRUE(delegate->scroll_update());
1673 EXPECT_FALSE(delegate->scroll_end());
1674 EXPECT_EQ(19, delegate->scroll_x());
1675 EXPECT_EQ(19, delegate->scroll_y());
1677 // Move some more to generate a few more scroll updates.
1678 delegate->Reset();
1679 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(115, 216),
1680 kTouchId, tes.Now());
1681 DispatchEventUsingWindowDispatcher(&move1);
1682 EXPECT_FALSE(delegate->tap());
1683 EXPECT_FALSE(delegate->tap_down());
1684 EXPECT_FALSE(delegate->tap_cancel());
1685 EXPECT_FALSE(delegate->scroll_begin());
1686 EXPECT_TRUE(delegate->scroll_update());
1687 EXPECT_FALSE(delegate->scroll_end());
1688 EXPECT_EQ(-20, delegate->scroll_x());
1689 EXPECT_EQ(-19, delegate->scroll_y());
1690 EXPECT_EQ(0, delegate->scroll_x_hint());
1691 EXPECT_EQ(0, delegate->scroll_y_hint());
1693 delegate->Reset();
1694 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 220),
1695 kTouchId, tes.Now());
1696 DispatchEventUsingWindowDispatcher(&move2);
1697 EXPECT_FALSE(delegate->tap());
1698 EXPECT_FALSE(delegate->tap_down());
1699 EXPECT_FALSE(delegate->tap_cancel());
1700 EXPECT_FALSE(delegate->scroll_begin());
1701 EXPECT_TRUE(delegate->scroll_update());
1702 EXPECT_FALSE(delegate->scroll_end());
1703 EXPECT_EQ(30, delegate->scroll_x());
1704 EXPECT_EQ(4, delegate->scroll_y());
1706 // Release the touch. This should end the scroll.
1707 delegate->Reset();
1708 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1709 kTouchId, tes.Now());
1710 DispatchEventUsingWindowDispatcher(&release1);
1711 EXPECT_FALSE(delegate->tap());
1712 EXPECT_FALSE(delegate->tap_down());
1713 EXPECT_FALSE(delegate->tap_cancel());
1714 EXPECT_FALSE(delegate->scroll_begin());
1715 EXPECT_FALSE(delegate->scroll_update());
1716 EXPECT_FALSE(delegate->scroll_end());
1717 EXPECT_TRUE(delegate->fling());
1720 TEST_F(GestureRecognizerTest, AsynchronousGestureRecognition) {
1721 scoped_ptr<QueueTouchEventDelegate> queued_delegate(
1722 new QueueTouchEventDelegate(host()->dispatcher()));
1723 TimedEvents tes;
1724 const int kWindowWidth = 123;
1725 const int kWindowHeight = 45;
1726 const int kTouchId1 = 6;
1727 const int kTouchId2 = 4;
1728 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1729 scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate(
1730 queued_delegate.get(), -1234, bounds, root_window()));
1732 queued_delegate->set_window(queue.get());
1734 // Touch down on the window. This should not generate any gesture event.
1735 queued_delegate->Reset();
1736 ui::TouchEvent press(
1737 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
1738 DispatchEventUsingWindowDispatcher(&press);
1739 EXPECT_FALSE(queued_delegate->tap());
1740 EXPECT_FALSE(queued_delegate->tap_down());
1741 EXPECT_FALSE(queued_delegate->tap_cancel());
1742 EXPECT_FALSE(queued_delegate->begin());
1743 EXPECT_FALSE(queued_delegate->scroll_begin());
1744 EXPECT_FALSE(queued_delegate->scroll_update());
1745 EXPECT_FALSE(queued_delegate->scroll_end());
1747 // Introduce some delay before the touch is released so that it is recognized
1748 // as a tap. However, this still should not create any gesture events.
1749 queued_delegate->Reset();
1750 ui::TouchEvent release(
1751 ui::ET_TOUCH_RELEASED,
1752 gfx::Point(101, 201),
1753 kTouchId1,
1754 press.time_stamp() + base::TimeDelta::FromMilliseconds(50));
1755 DispatchEventUsingWindowDispatcher(&release);
1756 EXPECT_FALSE(queued_delegate->tap());
1757 EXPECT_FALSE(queued_delegate->tap_down());
1758 EXPECT_FALSE(queued_delegate->tap_cancel());
1759 EXPECT_FALSE(queued_delegate->begin());
1760 EXPECT_FALSE(queued_delegate->end());
1761 EXPECT_FALSE(queued_delegate->scroll_begin());
1762 EXPECT_FALSE(queued_delegate->scroll_update());
1763 EXPECT_FALSE(queued_delegate->scroll_end());
1765 // Create another window, and place a touch-down on it. This should create a
1766 // tap-down gesture.
1767 scoped_ptr<GestureEventConsumeDelegate> delegate(
1768 new GestureEventConsumeDelegate());
1769 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1770 delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), root_window()));
1771 delegate->Reset();
1772 ui::TouchEvent press2(
1773 ui::ET_TOUCH_PRESSED, gfx::Point(10, 20), kTouchId2, tes.Now());
1774 DispatchEventUsingWindowDispatcher(&press2);
1775 EXPECT_FALSE(delegate->tap());
1776 EXPECT_TRUE(delegate->tap_down());
1777 EXPECT_FALSE(delegate->tap_cancel());
1778 EXPECT_FALSE(queued_delegate->begin());
1779 EXPECT_FALSE(queued_delegate->end());
1780 EXPECT_FALSE(delegate->scroll_begin());
1781 EXPECT_FALSE(delegate->scroll_update());
1782 EXPECT_FALSE(delegate->scroll_end());
1784 ui::TouchEvent release2(
1785 ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), kTouchId2, tes.Now());
1786 DispatchEventUsingWindowDispatcher(&release2);
1788 // Process the first queued event.
1789 queued_delegate->Reset();
1790 queued_delegate->ReceivedAck();
1791 EXPECT_FALSE(queued_delegate->tap());
1792 EXPECT_TRUE(queued_delegate->tap_down());
1793 EXPECT_TRUE(queued_delegate->begin());
1794 EXPECT_FALSE(queued_delegate->tap_cancel());
1795 EXPECT_FALSE(queued_delegate->end());
1796 EXPECT_FALSE(queued_delegate->scroll_begin());
1797 EXPECT_FALSE(queued_delegate->scroll_update());
1798 EXPECT_FALSE(queued_delegate->scroll_end());
1800 // Now, process the second queued event.
1801 queued_delegate->Reset();
1802 queued_delegate->ReceivedAck();
1803 EXPECT_TRUE(queued_delegate->tap());
1804 EXPECT_FALSE(queued_delegate->tap_down());
1805 EXPECT_FALSE(queued_delegate->tap_cancel());
1806 EXPECT_FALSE(queued_delegate->begin());
1807 EXPECT_TRUE(queued_delegate->end());
1808 EXPECT_FALSE(queued_delegate->scroll_begin());
1809 EXPECT_FALSE(queued_delegate->scroll_update());
1810 EXPECT_FALSE(queued_delegate->scroll_end());
1812 // Start all over. Press on the first window, then press again on the second
1813 // window. The second press should still go to the first window.
1814 queued_delegate->Reset();
1815 ui::TouchEvent press3(
1816 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
1817 DispatchEventUsingWindowDispatcher(&press3);
1818 EXPECT_FALSE(queued_delegate->tap());
1819 EXPECT_FALSE(queued_delegate->tap_down());
1820 EXPECT_FALSE(queued_delegate->tap_cancel());
1821 EXPECT_FALSE(queued_delegate->begin());
1822 EXPECT_FALSE(queued_delegate->end());
1823 EXPECT_FALSE(queued_delegate->begin());
1824 EXPECT_FALSE(queued_delegate->end());
1825 EXPECT_FALSE(queued_delegate->scroll_begin());
1826 EXPECT_FALSE(queued_delegate->scroll_update());
1827 EXPECT_FALSE(queued_delegate->scroll_end());
1829 queued_delegate->Reset();
1830 delegate->Reset();
1831 ui::TouchEvent press4(
1832 ui::ET_TOUCH_PRESSED, gfx::Point(103, 203), kTouchId2, tes.Now());
1833 DispatchEventUsingWindowDispatcher(&press4);
1834 EXPECT_FALSE(delegate->tap());
1835 EXPECT_FALSE(delegate->tap_down());
1836 EXPECT_FALSE(delegate->tap_cancel());
1837 EXPECT_FALSE(delegate->begin());
1838 EXPECT_FALSE(delegate->end());
1839 EXPECT_FALSE(delegate->scroll_begin());
1840 EXPECT_FALSE(delegate->scroll_update());
1841 EXPECT_FALSE(delegate->scroll_end());
1842 EXPECT_FALSE(queued_delegate->tap());
1843 EXPECT_FALSE(queued_delegate->tap_down());
1844 EXPECT_FALSE(queued_delegate->tap_cancel());
1845 EXPECT_FALSE(queued_delegate->begin());
1846 EXPECT_FALSE(queued_delegate->end());
1847 EXPECT_FALSE(queued_delegate->scroll_begin());
1848 EXPECT_FALSE(queued_delegate->scroll_update());
1849 EXPECT_FALSE(queued_delegate->scroll_end());
1851 // Move the second touch-point enough so that it is considered a pinch. This
1852 // should generate both SCROLL_BEGIN and PINCH_BEGIN gestures.
1853 queued_delegate->Reset();
1854 delegate->Reset();
1855 ui::TouchEvent move(
1856 ui::ET_TOUCH_MOVED,
1857 gfx::PointF(203 +
1858 ui::GestureConfiguration::GetInstance()
1859 ->max_touch_move_in_pixels_for_click(),
1860 303),
1861 kTouchId2,
1862 tes.Now());
1863 DispatchEventUsingWindowDispatcher(&move);
1864 EXPECT_FALSE(delegate->tap());
1865 EXPECT_FALSE(delegate->tap_down());
1866 EXPECT_FALSE(delegate->tap_cancel());
1867 EXPECT_FALSE(delegate->begin());
1868 EXPECT_FALSE(delegate->scroll_begin());
1869 EXPECT_FALSE(delegate->scroll_update());
1870 EXPECT_FALSE(delegate->scroll_end());
1871 EXPECT_FALSE(queued_delegate->tap());
1872 EXPECT_FALSE(queued_delegate->tap_down());
1873 EXPECT_FALSE(queued_delegate->tap_cancel());
1874 EXPECT_FALSE(queued_delegate->begin());
1875 EXPECT_FALSE(queued_delegate->scroll_begin());
1876 EXPECT_FALSE(queued_delegate->scroll_update());
1877 EXPECT_FALSE(queued_delegate->scroll_end());
1879 queued_delegate->Reset();
1880 queued_delegate->ReceivedAck();
1881 EXPECT_FALSE(queued_delegate->tap());
1882 EXPECT_TRUE(queued_delegate->tap_down());
1883 EXPECT_TRUE(queued_delegate->begin());
1884 EXPECT_FALSE(queued_delegate->tap_cancel());
1885 EXPECT_FALSE(queued_delegate->end());
1886 EXPECT_FALSE(queued_delegate->scroll_begin());
1887 EXPECT_FALSE(queued_delegate->scroll_update());
1888 EXPECT_FALSE(queued_delegate->scroll_end());
1890 queued_delegate->Reset();
1891 queued_delegate->ReceivedAck();
1892 EXPECT_FALSE(queued_delegate->tap());
1893 EXPECT_FALSE(queued_delegate->tap_down()); // no touch down for second tap.
1894 EXPECT_TRUE(queued_delegate->tap_cancel());
1895 EXPECT_TRUE(queued_delegate->begin());
1896 EXPECT_FALSE(queued_delegate->end());
1897 EXPECT_FALSE(queued_delegate->scroll_begin());
1898 EXPECT_FALSE(queued_delegate->scroll_update());
1899 EXPECT_FALSE(queued_delegate->scroll_end());
1900 EXPECT_FALSE(queued_delegate->pinch_begin());
1901 EXPECT_FALSE(queued_delegate->pinch_update());
1902 EXPECT_FALSE(queued_delegate->pinch_end());
1904 queued_delegate->Reset();
1905 queued_delegate->ReceivedAck();
1906 EXPECT_FALSE(queued_delegate->tap());
1907 EXPECT_FALSE(queued_delegate->tap_down());
1908 EXPECT_FALSE(queued_delegate->tap_cancel());
1909 EXPECT_FALSE(queued_delegate->begin());
1910 EXPECT_FALSE(queued_delegate->end());
1911 EXPECT_TRUE(queued_delegate->scroll_begin());
1913 EXPECT_TRUE(queued_delegate->scroll_update());
1914 EXPECT_FALSE(queued_delegate->scroll_end());
1915 EXPECT_TRUE(queued_delegate->pinch_begin());
1916 EXPECT_FALSE(queued_delegate->pinch_update());
1917 EXPECT_FALSE(queued_delegate->pinch_end());
1920 // Check that appropriate touch events generate pinch gesture events.
1921 TEST_F(GestureRecognizerTest, GestureEventPinchFromScroll) {
1922 scoped_ptr<GestureEventConsumeDelegate> delegate(
1923 new GestureEventConsumeDelegate());
1924 TimedEvents tes;
1925 const int kWindowWidth = 300;
1926 const int kWindowHeight = 400;
1927 const int kTouchId1 = 5;
1928 const int kTouchId2 = 3;
1929 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1930 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1931 delegate.get(), -1234, bounds, root_window()));
1933 delegate->Reset();
1934 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1935 kTouchId1, tes.Now());
1936 DispatchEventUsingWindowDispatcher(&press);
1937 EXPECT_2_EVENTS(delegate->events(),
1938 ui::ET_GESTURE_BEGIN,
1939 ui::ET_GESTURE_TAP_DOWN);
1941 // Move the touch-point enough so that it is considered as a scroll. This
1942 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1943 delegate->Reset();
1944 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 301),
1945 kTouchId1, tes.Now());
1946 DispatchEventUsingWindowDispatcher(&move);
1947 EXPECT_3_EVENTS(delegate->events(),
1948 ui::ET_GESTURE_TAP_CANCEL,
1949 ui::ET_GESTURE_SCROLL_BEGIN,
1950 ui::ET_GESTURE_SCROLL_UPDATE);
1952 // Press the second finger. It should cause pinch-begin. Note that we will not
1953 // transition to two finger tap here because the touch points are far enough.
1954 delegate->Reset();
1955 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1956 kTouchId2, tes.Now());
1957 DispatchEventUsingWindowDispatcher(&press2);
1958 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_BEGIN);
1959 EXPECT_EQ(gfx::Rect(10, 10, 120, 291).ToString(),
1960 delegate->bounding_box().ToString());
1962 // Move the first finger.
1963 delegate->Reset();
1964 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(95, 201),
1965 kTouchId1, tes.Now());
1966 DispatchEventUsingWindowDispatcher(&move3);
1967 EXPECT_2_EVENTS(delegate->events(),
1968 ui::ET_GESTURE_SCROLL_UPDATE,
1969 ui::ET_GESTURE_PINCH_BEGIN);
1970 EXPECT_EQ(gfx::Rect(10, 10, 85, 191).ToString(),
1971 delegate->bounding_box().ToString());
1973 // Now move the second finger.
1974 delegate->Reset();
1975 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
1976 kTouchId2, tes.Now());
1977 DispatchEventUsingWindowDispatcher(&move4);
1978 EXPECT_2_EVENTS(delegate->events(),
1979 ui::ET_GESTURE_SCROLL_UPDATE,
1980 ui::ET_GESTURE_PINCH_UPDATE);
1981 EXPECT_EQ(gfx::Rect(55, 15, 40, 186).ToString(),
1982 delegate->bounding_box().ToString());
1984 // Release the first finger. This should end pinch.
1985 delegate->Reset();
1986 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1987 kTouchId1, tes.Now());
1988 DispatchEventUsingWindowDispatcher(&release);
1989 EXPECT_2_EVENTS(delegate->events(),
1990 ui::ET_GESTURE_PINCH_END,
1991 ui::ET_GESTURE_END);
1992 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
1993 delegate->bounding_box().ToString());
1995 // Move the second finger. This should still generate a scroll.
1996 delegate->Reset();
1997 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
1998 kTouchId2, tes.Now());
1999 DispatchEventUsingWindowDispatcher(&move5);
2000 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2001 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2004 TEST_F(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) {
2005 scoped_ptr<GestureEventConsumeDelegate> delegate(
2006 new GestureEventConsumeDelegate());
2007 TimedEvents tes;
2008 const int kWindowWidth = 300;
2009 const int kWindowHeight = 400;
2010 const int kTouchId1 = 5;
2011 const int kTouchId2 = 3;
2012 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2013 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2014 delegate.get(), -1234, bounds, root_window()));
2016 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2017 kTouchId1, tes.Now());
2018 DispatchEventUsingWindowDispatcher(&press);
2019 delegate->Reset();
2020 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2021 kTouchId2, tes.Now());
2022 DispatchEventUsingWindowDispatcher(&press2);
2023 EXPECT_FALSE(delegate->pinch_begin());
2025 // Touch move triggers pinch begin.
2026 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2027 EXPECT_TRUE(delegate->pinch_begin());
2028 EXPECT_FALSE(delegate->pinch_update());
2030 // Touch move triggers pinch update.
2031 tes.SendScrollEvent(event_processor(), 160, 200, kTouchId1, delegate.get());
2032 EXPECT_FALSE(delegate->pinch_begin());
2033 EXPECT_TRUE(delegate->pinch_update());
2035 // Pinch has started, now release the second finger
2036 delegate->Reset();
2037 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2038 kTouchId1, tes.Now());
2039 DispatchEventUsingWindowDispatcher(&release);
2040 EXPECT_TRUE(delegate->pinch_end());
2042 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId2, delegate.get());
2043 EXPECT_TRUE(delegate->scroll_update());
2045 // Pinch again
2046 delegate->Reset();
2047 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2048 kTouchId1, tes.Now());
2049 DispatchEventUsingWindowDispatcher(&press3);
2050 // Now the touch points are close. So we will go into two finger tap.
2051 // Move the touch-point enough to break two-finger-tap and enter pinch.
2052 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 50),
2053 kTouchId1, tes.Now());
2054 DispatchEventUsingWindowDispatcher(&move2);
2055 EXPECT_TRUE(delegate->pinch_begin());
2057 tes.SendScrollEvent(event_processor(), 350, 350, kTouchId1, delegate.get());
2058 EXPECT_TRUE(delegate->pinch_update());
2061 TEST_F(GestureRecognizerTest, GestureEventPinchFromTap) {
2062 scoped_ptr<GestureEventConsumeDelegate> delegate(
2063 new GestureEventConsumeDelegate());
2064 TimedEvents tes;
2065 const int kWindowWidth = 300;
2066 const int kWindowHeight = 400;
2067 const int kTouchId1 = 3;
2068 const int kTouchId2 = 5;
2069 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2070 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2071 delegate.get(), -1234, bounds, root_window()));
2073 delegate->Reset();
2074 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2075 kTouchId1, tes.Now());
2076 DispatchEventUsingWindowDispatcher(&press);
2077 EXPECT_2_EVENTS(delegate->events(),
2078 ui::ET_GESTURE_BEGIN,
2079 ui::ET_GESTURE_TAP_DOWN);
2080 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2082 // Press the second finger far enough to break two finger tap.
2083 delegate->Reset();
2084 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2085 kTouchId2, tes.Now());
2086 DispatchEventUsingWindowDispatcher(&press2);
2087 EXPECT_2_EVENTS(delegate->events(),
2088 ui::ET_GESTURE_TAP_CANCEL,
2089 ui::ET_GESTURE_BEGIN);
2090 EXPECT_EQ(gfx::Rect(10, 10, 91, 291).ToString(),
2091 delegate->bounding_box().ToString());
2093 // Move the first finger.
2094 delegate->Reset();
2095 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
2096 kTouchId1, tes.Now());
2097 DispatchEventUsingWindowDispatcher(&move3);
2098 EXPECT_3_EVENTS(delegate->events(),
2099 ui::ET_GESTURE_SCROLL_BEGIN,
2100 ui::ET_GESTURE_SCROLL_UPDATE,
2101 ui::ET_GESTURE_PINCH_BEGIN);
2102 EXPECT_EQ(gfx::Rect(10, 10, 55, 191).ToString(),
2103 delegate->bounding_box().ToString());
2105 // Now move the second finger.
2106 delegate->Reset();
2107 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
2108 kTouchId2, tes.Now());
2109 DispatchEventUsingWindowDispatcher(&move4);
2110 EXPECT_2_EVENTS(delegate->events(),
2111 ui::ET_GESTURE_SCROLL_UPDATE,
2112 ui::ET_GESTURE_PINCH_UPDATE);
2113 EXPECT_EQ(gfx::Rect(55, 15, 10, 186).ToString(),
2114 delegate->bounding_box().ToString());
2116 // Release the first finger. This should end pinch.
2117 delegate->Reset();
2118 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2119 kTouchId1, tes.LeapForward(10));
2120 DispatchEventUsingWindowDispatcher(&release);
2121 EXPECT_2_EVENTS(delegate->events(),
2122 ui::ET_GESTURE_PINCH_END,
2123 ui::ET_GESTURE_END);
2124 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
2125 delegate->bounding_box().ToString());
2127 // Move the second finger. This should still generate a scroll.
2128 delegate->Reset();
2129 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2130 kTouchId2, tes.Now());
2131 DispatchEventUsingWindowDispatcher(&move5);
2132 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2133 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2136 TEST_F(GestureRecognizerTest, GestureEventIgnoresDisconnectedEvents) {
2137 scoped_ptr<GestureEventConsumeDelegate> delegate(
2138 new GestureEventConsumeDelegate());
2139 TimedEvents tes;
2141 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2142 6, tes.Now());
2143 DispatchEventUsingWindowDispatcher(&release1);
2144 EXPECT_FALSE(delegate->tap());
2145 EXPECT_FALSE(delegate->tap_down());
2148 // Check that a touch is locked to the window of the closest current touch
2149 // within max_separation_for_gesture_touches_in_pixels
2150 TEST_F(GestureRecognizerTest, GestureEventTouchLockSelectsCorrectWindow) {
2151 ui::GestureRecognizer* gesture_recognizer = new ui::GestureRecognizerImpl();
2152 TimedEvents tes;
2153 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2155 ui::GestureConsumer* target;
2156 const int kNumWindows = 4;
2158 scoped_ptr<GestureEventConsumeDelegate*[]> delegates(
2159 new GestureEventConsumeDelegate*[kNumWindows]);
2161 ui::GestureConfiguration::GetInstance()
2162 ->set_max_separation_for_gesture_touches_in_pixels(499);
2164 scoped_ptr<gfx::Rect[]> window_bounds(new gfx::Rect[kNumWindows]);
2165 window_bounds[0] = gfx::Rect(0, 0, 1, 1);
2166 window_bounds[1] = gfx::Rect(500, 0, 1, 1);
2167 window_bounds[2] = gfx::Rect(0, 500, 1, 1);
2168 window_bounds[3] = gfx::Rect(500, 500, 1, 1);
2170 scoped_ptr<aura::Window*[]> windows(new aura::Window*[kNumWindows]);
2172 // Instantiate windows with |window_bounds| and touch each window at
2173 // its origin.
2174 for (int i = 0; i < kNumWindows; ++i) {
2175 delegates[i] = new GestureEventConsumeDelegate();
2176 windows[i] = CreateTestWindowWithDelegate(
2177 delegates[i], i, window_bounds[i], root_window());
2178 windows[i]->set_id(i);
2179 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, window_bounds[i].origin(),
2180 i, tes.Now());
2181 DispatchEventUsingWindowDispatcher(&press);
2184 // Touches should now be associated with the closest touch within
2185 // ui::GestureConfiguration::max_separation_for_gesture_touches_in_pixels
2186 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 11), -1);
2187 EXPECT_EQ("0", WindowIDAsString(target));
2188 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 11), -1);
2189 EXPECT_EQ("1", WindowIDAsString(target));
2190 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 511), -1);
2191 EXPECT_EQ("2", WindowIDAsString(target));
2192 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 511), -1);
2193 EXPECT_EQ("3", WindowIDAsString(target));
2195 // Add a touch in the middle associated with windows[2]
2196 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 500),
2197 kNumWindows, tes.Now());
2198 DispatchEventUsingWindowDispatcher(&press);
2199 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(250, 250),
2200 kNumWindows, tes.Now());
2201 DispatchEventUsingWindowDispatcher(&move);
2203 target = gesture_recognizer->GetTargetForLocation(gfx::Point(250, 250), -1);
2204 EXPECT_EQ("2", WindowIDAsString(target));
2206 // Make sure that ties are broken by distance to a current touch
2207 // Closer to the point in the bottom right.
2208 target = gesture_recognizer->GetTargetForLocation(gfx::Point(380, 380), -1);
2209 EXPECT_EQ("3", WindowIDAsString(target));
2211 // This touch is closer to the point in the middle
2212 target = gesture_recognizer->GetTargetForLocation(gfx::Point(300, 300), -1);
2213 EXPECT_EQ("2", WindowIDAsString(target));
2215 // A touch too far from other touches won't be locked to anything
2216 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2217 EXPECT_TRUE(target == NULL);
2219 // Move a touch associated with windows[2] to 1000, 1000
2220 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(1000, 1000),
2221 kNumWindows, tes.Now());
2222 DispatchEventUsingWindowDispatcher(&move2);
2224 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2225 EXPECT_EQ("2", WindowIDAsString(target));
2227 for (int i = 0; i < kNumWindows; ++i) {
2228 // Delete windows before deleting delegates.
2229 delete windows[i];
2230 delete delegates[i];
2234 // Check that a touch's target will not be effected by a touch on a different
2235 // screen.
2236 TEST_F(GestureRecognizerTest, GestureEventTouchLockIgnoresOtherScreens) {
2237 scoped_ptr<GestureEventConsumeDelegate> delegate(
2238 new GestureEventConsumeDelegate());
2239 gfx::Rect bounds(0, 0, 10, 10);
2240 scoped_ptr<aura::Window> window(
2241 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2243 const int kTouchId1 = 8;
2244 const int kTouchId2 = 2;
2245 TimedEvents tes;
2247 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(5, 5),
2248 kTouchId1, tes.Now());
2249 ui::EventTestApi test_press1(&press1);
2250 test_press1.set_source_device_id(1);
2251 DispatchEventUsingWindowDispatcher(&press1);
2253 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2254 kTouchId2, tes.Now());
2255 ui::EventTestApi test_press2(&press2);
2256 test_press2.set_source_device_id(2);
2257 DispatchEventUsingWindowDispatcher(&press2);
2259 // The second press should not have been locked to the same target as the
2260 // first, as they occured on different displays.
2261 EXPECT_NE(
2262 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1),
2263 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2266 // Check that touch events outside the root window are still handled
2267 // by the root window's gesture sequence.
2268 TEST_F(GestureRecognizerTest, GestureEventOutsideRootWindowTap) {
2269 TimedEvents tes;
2270 scoped_ptr<aura::Window> window(CreateTestWindowWithBounds(
2271 gfx::Rect(-100, -100, 2000, 2000), root_window()));
2273 gfx::Point pos1(-10, -10);
2274 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, pos1, 0, tes.Now());
2275 DispatchEventUsingWindowDispatcher(&press1);
2277 gfx::Point pos2(1000, 1000);
2278 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, pos2, 1, tes.Now());
2279 DispatchEventUsingWindowDispatcher(&press2);
2281 // As these presses were outside the root window, they should be
2282 // associated with the root window.
2283 EXPECT_EQ(root_window(),
2284 static_cast<aura::Window*>(
2285 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1)));
2286 EXPECT_EQ(root_window(),
2287 static_cast<aura::Window*>(
2288 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2)));
2291 TEST_F(GestureRecognizerTest, NoTapWithPreventDefaultedRelease) {
2292 scoped_ptr<QueueTouchEventDelegate> delegate(
2293 new QueueTouchEventDelegate(host()->dispatcher()));
2294 TimedEvents tes;
2295 const int kTouchId = 2;
2296 gfx::Rect bounds(100, 200, 100, 100);
2297 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2298 delegate.get(), -1234, bounds, root_window()));
2299 delegate->set_window(window.get());
2301 delegate->Reset();
2302 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2303 kTouchId, tes.Now());
2304 DispatchEventUsingWindowDispatcher(&press);
2305 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2306 kTouchId, tes.LeapForward(50));
2307 DispatchEventUsingWindowDispatcher(&release);
2309 delegate->Reset();
2310 delegate->ReceivedAck();
2311 EXPECT_TRUE(delegate->tap_down());
2312 delegate->Reset();
2313 delegate->ReceivedAckPreventDefaulted();
2314 EXPECT_FALSE(delegate->tap());
2315 EXPECT_TRUE(delegate->tap_cancel());
2318 TEST_F(GestureRecognizerTest, PinchScrollWithPreventDefaultedRelease) {
2319 scoped_ptr<QueueTouchEventDelegate> delegate(
2320 new QueueTouchEventDelegate(host()->dispatcher()));
2321 TimedEvents tes;
2322 const int kTouchId1 = 7;
2323 const int kTouchId2 = 5;
2324 gfx::Rect bounds(10, 20, 100, 100);
2325 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2326 delegate.get(), -1234, bounds, root_window()));
2327 delegate->set_window(window.get());
2330 delegate->Reset();
2331 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2332 tes.Now());
2333 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2334 tes.LeapForward(200));
2335 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2336 tes.LeapForward(50));
2337 DispatchEventUsingWindowDispatcher(&press);
2338 DispatchEventUsingWindowDispatcher(&move);
2339 DispatchEventUsingWindowDispatcher(&release);
2340 delegate->Reset();
2342 // Ack the press event.
2343 delegate->ReceivedAck();
2344 EXPECT_2_EVENTS(
2345 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2346 delegate->Reset();
2348 // Ack the move event.
2349 delegate->ReceivedAck();
2350 EXPECT_3_EVENTS(delegate->events(),
2351 ui::ET_GESTURE_TAP_CANCEL,
2352 ui::ET_GESTURE_SCROLL_BEGIN,
2353 ui::ET_GESTURE_SCROLL_UPDATE);
2354 delegate->Reset();
2356 // Ack the release event. Although the release event has been processed, it
2357 // should still generate a scroll-end event.
2358 delegate->ReceivedAckPreventDefaulted();
2359 EXPECT_2_EVENTS(
2360 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2363 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2364 tes.Now());
2365 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2366 tes.LeapForward(200));
2367 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2368 tes.LeapForward(50));
2369 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 25), kTouchId2,
2370 tes.Now());
2371 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 85), kTouchId2,
2372 tes.LeapForward(1000));
2373 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(145, 85), kTouchId2,
2374 tes.LeapForward(14));
2376 // Do a pinch.
2377 DispatchEventUsingWindowDispatcher(&press);
2378 DispatchEventUsingWindowDispatcher(&move);
2379 DispatchEventUsingWindowDispatcher(&press2);
2380 DispatchEventUsingWindowDispatcher(&move2);
2381 DispatchEventUsingWindowDispatcher(&release);
2382 DispatchEventUsingWindowDispatcher(&release2);
2384 // Ack the press and move events.
2385 delegate->Reset();
2386 delegate->ReceivedAck();
2387 EXPECT_2_EVENTS(
2388 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2390 delegate->Reset();
2391 delegate->ReceivedAck();
2392 EXPECT_3_EVENTS(delegate->events(),
2393 ui::ET_GESTURE_TAP_CANCEL,
2394 ui::ET_GESTURE_SCROLL_BEGIN,
2395 ui::ET_GESTURE_SCROLL_UPDATE);
2397 delegate->Reset();
2398 delegate->ReceivedAck();
2399 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_BEGIN);
2401 delegate->Reset();
2402 delegate->ReceivedAck();
2403 EXPECT_2_EVENTS(delegate->events(),
2404 ui::ET_GESTURE_SCROLL_UPDATE,
2405 ui::ET_GESTURE_PINCH_BEGIN);
2407 // Ack the first release. Although the release is processed, it should still
2408 // generate a pinch-end event.
2409 delegate->Reset();
2410 delegate->ReceivedAckPreventDefaulted();
2411 EXPECT_2_EVENTS(
2412 delegate->events(), ui::ET_GESTURE_PINCH_END, ui::ET_GESTURE_END);
2414 delegate->Reset();
2415 delegate->ReceivedAckPreventDefaulted();
2416 EXPECT_2_EVENTS(
2417 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2420 TEST_F(GestureRecognizerTest, GestureEndLocation) {
2421 GestureEventConsumeDelegate delegate;
2422 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2423 &delegate, -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2424 ui::test::EventGenerator generator(root_window(), window.get());
2425 const gfx::Point begin(20, 20);
2426 const gfx::Point end(150, 150);
2427 const gfx::Vector2d window_offset =
2428 window->bounds().origin().OffsetFromOrigin();
2429 generator.GestureScrollSequence(begin, end,
2430 base::TimeDelta::FromMilliseconds(20),
2431 10);
2432 EXPECT_EQ((begin - window_offset).ToString(),
2433 delegate.scroll_begin_position().ToString());
2434 EXPECT_EQ((end - window_offset).ToString(),
2435 delegate.gesture_end_location().ToString());
2438 TEST_F(GestureRecognizerTest, CaptureSendsGestureEnd) {
2439 scoped_ptr<GestureEventConsumeDelegate> delegate(
2440 new GestureEventConsumeDelegate());
2441 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2442 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2443 ui::test::EventGenerator generator(root_window());
2445 generator.MoveMouseRelativeTo(window.get(), gfx::Point(10, 10));
2446 generator.PressTouch();
2447 RunAllPendingInMessageLoop();
2449 EXPECT_TRUE(delegate->tap_down());
2451 scoped_ptr<aura::Window> capture(CreateTestWindowWithBounds(
2452 gfx::Rect(10, 10, 200, 200), root_window()));
2453 capture->SetCapture();
2454 RunAllPendingInMessageLoop();
2456 EXPECT_TRUE(delegate->end());
2457 EXPECT_TRUE(delegate->tap_cancel());
2460 // Check that previous touch actions that are completely finished (either
2461 // released or cancelled), do not receive extra synthetic cancels upon change of
2462 // capture.
2463 TEST_F(GestureRecognizerTest, CaptureDoesNotCancelFinishedTouches) {
2464 scoped_ptr<GestureEventConsumeDelegate> delegate(
2465 new GestureEventConsumeDelegate());
2466 scoped_ptr<TestEventHandler> handler(new TestEventHandler);
2467 root_window()->AddPreTargetHandler(handler.get());
2469 // Create a window and set it as the capture window.
2470 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(delegate.get(),
2471 -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2472 window1->SetCapture();
2474 ui::test::EventGenerator generator(root_window());
2475 TimedEvents tes;
2477 // Generate two touch-press events on the window.
2478 scoped_ptr<ui::TouchEvent> touch0(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2479 gfx::Point(20, 20), 0,
2480 tes.Now()));
2481 scoped_ptr<ui::TouchEvent> touch1(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2482 gfx::Point(30, 30), 1,
2483 tes.Now()));
2484 generator.Dispatch(touch0.get());
2485 generator.Dispatch(touch1.get());
2486 RunAllPendingInMessageLoop();
2487 EXPECT_EQ(2, handler->touch_pressed_count());
2489 // Advance time.
2490 tes.LeapForward(1000);
2492 // End the two touches, one by a touch-release and one by a touch-cancel; to
2493 // cover both cases.
2494 touch0.reset(new ui::TouchEvent(ui::ET_TOUCH_RELEASED, gfx::Point(20, 20), 0,
2495 tes.Now()));
2496 touch1.reset(new ui::TouchEvent(ui::ET_TOUCH_CANCELLED, gfx::Point(30, 30), 1,
2497 tes.Now()));
2498 generator.Dispatch(touch0.get());
2499 generator.Dispatch(touch1.get());
2500 RunAllPendingInMessageLoop();
2501 EXPECT_EQ(1, handler->touch_released_count());
2502 EXPECT_EQ(1, handler->touch_cancelled_count());
2504 // Create a new window and set it as the new capture window.
2505 scoped_ptr<aura::Window> window2(CreateTestWindowWithBounds(
2506 gfx::Rect(100, 100, 300, 300), root_window()));
2507 window2->SetCapture();
2508 RunAllPendingInMessageLoop();
2509 // Check that setting capture does not generate any synthetic touch-cancels
2510 // for the two previously finished touch actions.
2511 EXPECT_EQ(1, handler->touch_cancelled_count());
2513 root_window()->RemovePreTargetHandler(handler.get());
2516 // Tests that a press with the same touch id as an existing touch is ignored.
2517 TEST_F(GestureRecognizerTest, PressDoesNotCrash) {
2518 scoped_ptr<GestureEventConsumeDelegate> delegate(
2519 new GestureEventConsumeDelegate());
2520 TimedEvents tes;
2522 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2523 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2525 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(45, 45), 7, tes.Now());
2526 press.set_radius_x(40);
2527 DispatchEventUsingWindowDispatcher(&press);
2528 EXPECT_TRUE(delegate->tap_down());
2529 EXPECT_EQ(gfx::Rect(5, 5, 80, 80).ToString(),
2530 delegate->bounding_box().ToString());
2531 delegate->Reset();
2533 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 45), 7, tes.Now());
2534 DispatchEventUsingWindowDispatcher(&press2);
2536 // FIXME(tdresser): this should not generate a tap down; however,
2537 // there is at least one case where we need to allow a touch press
2538 // from a currently used touch id. See crbug.com/373125 for details.
2539 EXPECT_TRUE(delegate->begin());
2540 EXPECT_TRUE(delegate->tap_down());
2541 EXPECT_TRUE(delegate->tap_cancel());
2542 EXPECT_FALSE(delegate->scroll_begin());
2545 TEST_F(GestureRecognizerTest, TwoFingerTap) {
2546 scoped_ptr<GestureEventConsumeDelegate> delegate(
2547 new GestureEventConsumeDelegate());
2548 const int kWindowWidth = 123;
2549 const int kWindowHeight = 45;
2550 const int kTouchId1 = 2;
2551 const int kTouchId2 = 3;
2552 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2553 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2554 delegate.get(), -1234, bounds, root_window()));
2555 TimedEvents tes;
2557 delegate->Reset();
2558 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2559 kTouchId1, tes.Now());
2560 DispatchEventUsingWindowDispatcher(&press1);
2561 EXPECT_2_EVENTS(
2562 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
2564 delegate->Reset();
2565 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2566 kTouchId2, tes.Now());
2567 DispatchEventUsingWindowDispatcher(&press2);
2568 EXPECT_2_EVENTS(
2569 delegate->events(), ui::ET_GESTURE_TAP_CANCEL, ui::ET_GESTURE_BEGIN);
2571 // Little bit of touch move should not affect our state.
2572 delegate->Reset();
2573 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(102, 202),
2574 kTouchId1, tes.Now());
2575 DispatchEventUsingWindowDispatcher(&move1);
2576 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(131, 202),
2577 kTouchId2, tes.Now());
2578 DispatchEventUsingWindowDispatcher(&move2);
2579 EXPECT_2_EVENTS(delegate->events(),
2580 ui::ET_GESTURE_SCROLL_BEGIN,
2581 ui::ET_GESTURE_SCROLL_UPDATE);
2583 // Make sure there is enough delay before the touch is released so that it is
2584 // recognized as a tap.
2585 delegate->Reset();
2586 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2587 kTouchId1, tes.LeapForward(50));
2589 DispatchEventUsingWindowDispatcher(&release1);
2590 EXPECT_2_EVENTS(
2591 delegate->events(), ui::ET_GESTURE_TWO_FINGER_TAP, ui::ET_GESTURE_END);
2593 // Lift second finger.
2594 // Make sure there is enough delay before the touch is released so that it is
2595 // recognized as a tap.
2596 delegate->Reset();
2597 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2598 kTouchId2, tes.LeapForward(50));
2600 DispatchEventUsingWindowDispatcher(&release2);
2601 EXPECT_2_EVENTS(
2602 delegate->events(), ui::ET_GESTURE_SCROLL_END, ui::ET_GESTURE_END);
2605 TEST_F(GestureRecognizerTest, TwoFingerTapExpired) {
2606 scoped_ptr<GestureEventConsumeDelegate> delegate(
2607 new GestureEventConsumeDelegate());
2608 const int kWindowWidth = 123;
2609 const int kWindowHeight = 45;
2610 const int kTouchId1 = 2;
2611 const int kTouchId2 = 3;
2612 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2613 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2614 delegate.get(), -1234, bounds, root_window()));
2615 TimedEvents tes;
2617 delegate->Reset();
2618 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2619 kTouchId1, tes.Now());
2620 DispatchEventUsingWindowDispatcher(&press1);
2622 delegate->Reset();
2623 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2624 kTouchId2, tes.Now());
2625 DispatchEventUsingWindowDispatcher(&press2);
2627 // Send release event after sufficient delay so that two finger time expires.
2628 delegate->Reset();
2629 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2630 kTouchId1, tes.LeapForward(1000));
2632 DispatchEventUsingWindowDispatcher(&release1);
2633 EXPECT_FALSE(delegate->two_finger_tap());
2635 // Lift second finger.
2636 // Make sure there is enough delay before the touch is released so that it is
2637 // recognized as a tap.
2638 delegate->Reset();
2639 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2640 kTouchId2, tes.LeapForward(50));
2642 DispatchEventUsingWindowDispatcher(&release2);
2643 EXPECT_FALSE(delegate->two_finger_tap());
2646 TEST_F(GestureRecognizerTest, TwoFingerTapChangesToPinch) {
2647 scoped_ptr<GestureEventConsumeDelegate> delegate(
2648 new GestureEventConsumeDelegate());
2649 const int kWindowWidth = 123;
2650 const int kWindowHeight = 45;
2651 const int kTouchId1 = 2;
2652 const int kTouchId2 = 3;
2653 TimedEvents tes;
2655 // Test moving first finger
2657 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2658 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2659 delegate.get(), -1234, bounds, root_window()));
2661 delegate->Reset();
2662 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2663 kTouchId1, tes.Now());
2664 DispatchEventUsingWindowDispatcher(&press1);
2666 delegate->Reset();
2667 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2668 kTouchId2, tes.Now());
2669 DispatchEventUsingWindowDispatcher(&press2);
2671 tes.SendScrollEvent(event_processor(), 230, 330, kTouchId1, delegate.get());
2672 EXPECT_FALSE(delegate->two_finger_tap());
2673 EXPECT_TRUE(delegate->pinch_begin());
2675 // Make sure there is enough delay before the touch is released so that it
2676 // is recognized as a tap.
2677 delegate->Reset();
2678 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2679 kTouchId2, tes.LeapForward(50));
2681 DispatchEventUsingWindowDispatcher(&release);
2682 EXPECT_FALSE(delegate->two_finger_tap());
2683 EXPECT_TRUE(delegate->pinch_end());
2686 // Test moving second finger
2688 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2689 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2690 delegate.get(), -1234, bounds, root_window()));
2692 delegate->Reset();
2693 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2694 kTouchId1, tes.Now());
2695 DispatchEventUsingWindowDispatcher(&press1);
2697 delegate->Reset();
2698 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2699 kTouchId2, tes.Now());
2700 DispatchEventUsingWindowDispatcher(&press2);
2702 tes.SendScrollEvent(event_processor(), 301, 230, kTouchId2, delegate.get());
2703 EXPECT_FALSE(delegate->two_finger_tap());
2704 EXPECT_TRUE(delegate->pinch_begin());
2706 // Make sure there is enough delay before the touch is released so that it
2707 // is recognized as a tap.
2708 delegate->Reset();
2709 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2710 kTouchId1, tes.LeapForward(50));
2712 DispatchEventUsingWindowDispatcher(&release);
2713 EXPECT_FALSE(delegate->two_finger_tap());
2714 EXPECT_TRUE(delegate->pinch_end());
2718 TEST_F(GestureRecognizerTest, NoTwoFingerTapWhenFirstFingerHasScrolled) {
2719 scoped_ptr<GestureEventConsumeDelegate> delegate(
2720 new GestureEventConsumeDelegate());
2721 const int kWindowWidth = 123;
2722 const int kWindowHeight = 45;
2723 const int kTouchId1 = 2;
2724 const int kTouchId2 = 3;
2725 TimedEvents tes;
2727 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2728 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2729 delegate.get(), -1234, bounds, root_window()));
2731 delegate->Reset();
2732 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2733 kTouchId1, tes.Now());
2734 DispatchEventUsingWindowDispatcher(&press1);
2735 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2737 delegate->Reset();
2738 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2739 kTouchId2, tes.Now());
2740 DispatchEventUsingWindowDispatcher(&press2);
2742 EXPECT_FALSE(delegate->pinch_begin());
2744 // Make sure there is enough delay before the touch is released so that it
2745 // is recognized as a tap.
2746 delegate->Reset();
2747 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2748 kTouchId2, tes.LeapForward(50));
2750 DispatchEventUsingWindowDispatcher(&release);
2751 EXPECT_FALSE(delegate->two_finger_tap());
2752 EXPECT_FALSE(delegate->pinch_end());
2755 TEST_F(GestureRecognizerTest, MultiFingerSwipe) {
2756 scoped_ptr<GestureEventConsumeDelegate> delegate(
2757 new GestureEventConsumeDelegate());
2758 const int kWindowWidth = 123;
2759 const int kWindowHeight = 45;
2761 gfx::Rect bounds(5, 10, kWindowWidth, kWindowHeight);
2762 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2763 delegate.get(), -1234, bounds, root_window()));
2765 const int kSteps = 15;
2766 const int kTouchPoints = 4;
2767 gfx::Point points[kTouchPoints] = {
2768 gfx::Point(10, 30),
2769 gfx::Point(30, 20),
2770 gfx::Point(50, 30),
2771 gfx::Point(80, 50)
2774 ui::test::EventGenerator generator(root_window(), window.get());
2776 // The unified gesture recognizer assumes a finger has stopped if it hasn't
2777 // moved for too long. See ui/events/gesture_detection/velocity_tracker.cc's
2778 // kAssumePointerStoppedTimeMs.
2779 for (int count = 2; count <= kTouchPoints; ++count) {
2780 generator.GestureMultiFingerScroll(
2781 count, points, 10, kSteps, 0, -11 * kSteps);
2782 EXPECT_TRUE(delegate->swipe_up());
2783 delegate->Reset();
2785 generator.GestureMultiFingerScroll(
2786 count, points, 10, kSteps, 0, 11 * kSteps);
2787 EXPECT_TRUE(delegate->swipe_down());
2788 delegate->Reset();
2790 generator.GestureMultiFingerScroll(
2791 count, points, 10, kSteps, -11 * kSteps, 0);
2792 EXPECT_TRUE(delegate->swipe_left());
2793 delegate->Reset();
2795 generator.GestureMultiFingerScroll(
2796 count, points, 10, kSteps, 11 * kSteps, 0);
2797 EXPECT_TRUE(delegate->swipe_right());
2798 delegate->Reset();
2800 generator.GestureMultiFingerScroll(
2801 count, points, 10, kSteps, 5 * kSteps, 12 * kSteps);
2802 EXPECT_FALSE(delegate->swipe_down());
2803 delegate->Reset();
2805 generator.GestureMultiFingerScroll(
2806 count, points, 10, kSteps, 4 * kSteps, 12 * kSteps);
2807 EXPECT_TRUE(delegate->swipe_down());
2808 delegate->Reset();
2810 generator.GestureMultiFingerScroll(
2811 count, points, 10, kSteps, 3 * kSteps, 12 * kSteps);
2812 EXPECT_TRUE(delegate->swipe_down());
2813 delegate->Reset();
2817 TEST_F(GestureRecognizerTest, TwoFingerTapCancelled) {
2818 scoped_ptr<GestureEventConsumeDelegate> delegate(
2819 new GestureEventConsumeDelegate());
2820 const int kWindowWidth = 123;
2821 const int kWindowHeight = 45;
2822 const int kTouchId1 = 2;
2823 const int kTouchId2 = 3;
2824 TimedEvents tes;
2826 // Test canceling first finger.
2828 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2829 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2830 delegate.get(), -1234, bounds, root_window()));
2832 delegate->Reset();
2833 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2834 kTouchId1, tes.Now());
2835 DispatchEventUsingWindowDispatcher(&press1);
2837 delegate->Reset();
2838 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2839 kTouchId2, tes.Now());
2840 DispatchEventUsingWindowDispatcher(&press2);
2842 delegate->Reset();
2843 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2844 kTouchId1, tes.Now());
2845 DispatchEventUsingWindowDispatcher(&cancel);
2846 EXPECT_FALSE(delegate->two_finger_tap());
2848 // Make sure there is enough delay before the touch is released so that it
2849 // is recognized as a tap.
2850 delegate->Reset();
2851 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2852 kTouchId2, tes.LeapForward(50));
2854 DispatchEventUsingWindowDispatcher(&release);
2855 EXPECT_FALSE(delegate->two_finger_tap());
2858 // Test canceling second finger
2860 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2861 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2862 delegate.get(), -1234, bounds, root_window()));
2864 delegate->Reset();
2865 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2866 kTouchId1, tes.Now());
2867 DispatchEventUsingWindowDispatcher(&press1);
2869 delegate->Reset();
2870 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2871 kTouchId2, tes.Now());
2872 DispatchEventUsingWindowDispatcher(&press2);
2874 delegate->Reset();
2875 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2876 kTouchId2, tes.Now());
2877 DispatchEventUsingWindowDispatcher(&cancel);
2878 EXPECT_FALSE(delegate->two_finger_tap());
2880 // Make sure there is enough delay before the touch is released so that it
2881 // is recognized as a tap.
2882 delegate->Reset();
2883 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2884 kTouchId1, tes.LeapForward(50));
2886 DispatchEventUsingWindowDispatcher(&release);
2887 EXPECT_FALSE(delegate->two_finger_tap());
2891 TEST_F(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) {
2892 scoped_ptr<GestureEventConsumeDelegate> delegate(
2893 new GestureEventConsumeDelegate());
2894 const int kWindowWidth = 523;
2895 const int kWindowHeight = 45;
2896 const int kTouchId1 = 2;
2897 const int kTouchId2 = 3;
2898 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2899 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2900 delegate.get(), -1234, bounds, root_window()));
2901 TimedEvents tes;
2903 delegate->Reset();
2904 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2905 kTouchId1, tes.Now());
2906 DispatchEventUsingWindowDispatcher(&press1);
2907 EXPECT_FALSE(delegate->tap());
2908 EXPECT_TRUE(delegate->tap_down());
2909 EXPECT_FALSE(delegate->tap_cancel());
2910 EXPECT_FALSE(delegate->scroll_begin());
2911 EXPECT_FALSE(delegate->scroll_update());
2912 EXPECT_FALSE(delegate->scroll_end());
2913 EXPECT_FALSE(delegate->long_press());
2914 EXPECT_FALSE(delegate->two_finger_tap());
2916 delegate->Reset();
2917 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201),
2918 kTouchId2, tes.Now());
2919 DispatchEventUsingWindowDispatcher(&press2);
2920 EXPECT_FALSE(delegate->tap());
2921 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
2922 EXPECT_TRUE(delegate->tap_cancel());
2923 EXPECT_FALSE(delegate->scroll_begin());
2924 EXPECT_FALSE(delegate->scroll_update());
2925 EXPECT_FALSE(delegate->scroll_end());
2926 EXPECT_FALSE(delegate->long_press());
2927 EXPECT_FALSE(delegate->two_finger_tap());
2928 EXPECT_FALSE(delegate->pinch_begin());
2930 delegate->Reset();
2931 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(530, 301),
2932 kTouchId2, tes.Now());
2933 DispatchEventUsingWindowDispatcher(&move2);
2934 EXPECT_FALSE(delegate->tap());
2935 EXPECT_FALSE(delegate->tap_down());
2936 EXPECT_FALSE(delegate->tap_cancel());
2937 // Pinch & Scroll only when there is enough movement.
2938 EXPECT_TRUE(delegate->scroll_begin());
2939 EXPECT_TRUE(delegate->scroll_update());
2940 EXPECT_FALSE(delegate->scroll_end());
2941 EXPECT_FALSE(delegate->long_press());
2942 EXPECT_FALSE(delegate->two_finger_tap());
2943 EXPECT_TRUE(delegate->pinch_begin());
2946 // Verifies if a window is the target of multiple touch-ids and we hide the
2947 // window everything is cleaned up correctly.
2948 TEST_F(GestureRecognizerTest, FlushAllOnHide) {
2949 scoped_ptr<GestureEventConsumeDelegate> delegate(
2950 new GestureEventConsumeDelegate());
2951 gfx::Rect bounds(0, 0, 200, 200);
2952 scoped_ptr<aura::Window> window(
2953 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2954 const int kTouchId1 = 8;
2955 const int kTouchId2 = 2;
2956 TimedEvents tes;
2958 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2959 kTouchId1, tes.Now());
2960 DispatchEventUsingWindowDispatcher(&press1);
2961 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2962 kTouchId2, tes.Now());
2963 DispatchEventUsingWindowDispatcher(&press2);
2964 window->Hide();
2965 EXPECT_EQ(NULL,
2966 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1));
2967 EXPECT_EQ(NULL,
2968 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2971 TEST_F(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) {
2972 scoped_ptr<QueueTouchEventDelegate> delegate(
2973 new QueueTouchEventDelegate(host()->dispatcher()));
2974 const int kTouchId = 2;
2975 gfx::Rect bounds(100, 200, 100, 100);
2976 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2977 delegate.get(), -1234, bounds, root_window()));
2978 delegate->set_window(window.get());
2979 TimedEvents tes;
2981 delegate->Reset();
2982 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2983 kTouchId, tes.Now());
2984 DispatchEventUsingWindowDispatcher(&press);
2985 // Scroll around, to cancel the long press
2986 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
2988 delegate->Reset();
2989 delegate->ReceivedAck();
2990 EXPECT_TRUE(delegate->tap_down());
2992 // Wait long enough that long press would have fired if the touchmove hadn't
2993 // prevented it.
2994 DelayByLongPressTimeout();
2996 delegate->Reset();
2997 delegate->ReceivedAckPreventDefaulted();
2998 EXPECT_FALSE(delegate->long_press());
3001 // Same as GestureEventConsumeDelegate, but consumes all the touch-move events.
3002 class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate {
3003 public:
3004 ConsumesTouchMovesDelegate() : consume_touch_move_(true) {}
3005 ~ConsumesTouchMovesDelegate() override {}
3007 void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; }
3009 private:
3010 void OnTouchEvent(ui::TouchEvent* touch) override {
3011 if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED)
3012 touch->SetHandled();
3013 else
3014 GestureEventConsumeDelegate::OnTouchEvent(touch);
3017 bool consume_touch_move_;
3019 DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate);
3022 // Same as GestureEventScroll, but tests that the behavior is the same
3023 // even if all the touch-move events are consumed.
3024 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) {
3025 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3026 new ConsumesTouchMovesDelegate());
3027 const int kWindowWidth = 123;
3028 const int kWindowHeight = 45;
3029 const int kTouchId = 5;
3030 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3031 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3032 delegate.get(), -1234, bounds, root_window()));
3033 TimedEvents tes;
3035 delegate->Reset();
3036 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3037 kTouchId, tes.Now());
3038 DispatchEventUsingWindowDispatcher(&press);
3039 EXPECT_FALSE(delegate->tap());
3040 EXPECT_TRUE(delegate->tap_down());
3041 EXPECT_FALSE(delegate->tap_cancel());
3042 EXPECT_TRUE(delegate->begin());
3043 EXPECT_FALSE(delegate->scroll_begin());
3044 EXPECT_FALSE(delegate->scroll_update());
3045 EXPECT_FALSE(delegate->scroll_end());
3047 // Move the touch-point enough so that it would normally be considered a
3048 // scroll. But since the touch-moves will be consumed, the scroll should not
3049 // start.
3050 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3051 EXPECT_FALSE(delegate->tap());
3052 EXPECT_FALSE(delegate->tap_down());
3053 EXPECT_TRUE(delegate->tap_cancel());
3054 EXPECT_FALSE(delegate->begin());
3055 EXPECT_FALSE(delegate->scroll_update());
3056 EXPECT_FALSE(delegate->scroll_end());
3058 EXPECT_TRUE(delegate->scroll_begin());
3060 // Release the touch back at the start point. This should end without causing
3061 // a tap.
3062 delegate->Reset();
3063 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230),
3064 kTouchId, tes.LeapForward(50));
3065 DispatchEventUsingWindowDispatcher(&release);
3066 EXPECT_FALSE(delegate->tap());
3067 EXPECT_FALSE(delegate->tap_down());
3068 EXPECT_FALSE(delegate->tap_cancel());
3069 EXPECT_FALSE(delegate->begin());
3070 EXPECT_TRUE(delegate->end());
3071 EXPECT_FALSE(delegate->scroll_begin());
3072 EXPECT_FALSE(delegate->scroll_update());
3074 EXPECT_TRUE(delegate->scroll_end());
3077 // Tests the behavior of 2F scroll when some of the touch-move events are
3078 // consumed.
3079 TEST_F(GestureRecognizerTest, GestureEventScrollTwoFingerTouchMoveConsumed) {
3080 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3081 new ConsumesTouchMovesDelegate());
3082 const int kWindowWidth = 123;
3083 const int kWindowHeight = 100;
3084 const int kTouchId1 = 2;
3085 const int kTouchId2 = 3;
3086 TimedEvents tes;
3088 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3089 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3090 delegate.get(), -1234, bounds, root_window()));
3092 delegate->Reset();
3093 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3094 kTouchId1, tes.Now());
3095 DispatchEventUsingWindowDispatcher(&press1);
3096 tes.SendScrollEvent(event_processor(), 131, 231, kTouchId1, delegate.get());
3098 EXPECT_2_EVENTS(delegate->events(),
3099 ui::ET_GESTURE_TAP_CANCEL,
3100 ui::ET_GESTURE_SCROLL_BEGIN);
3102 delegate->Reset();
3103 // Second finger touches down and moves.
3104 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
3105 kTouchId2, tes.LeapForward(50));
3106 DispatchEventUsingWindowDispatcher(&press2);
3107 tes.SendScrollEvent(event_processor(), 161, 231, kTouchId2, delegate.get());
3108 EXPECT_0_EVENTS(delegate->events());
3110 delegate->Reset();
3111 // Move first finger again, no PinchUpdate & ScrollUpdate.
3112 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId1, delegate.get());
3113 EXPECT_0_EVENTS(delegate->events());
3115 // Stops consuming touch-move.
3116 delegate->set_consume_touch_move(false);
3118 delegate->Reset();
3119 // Making a pinch gesture.
3120 tes.SendScrollEvent(event_processor(), 161, 260, kTouchId1, delegate.get());
3121 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
3123 delegate->Reset();
3124 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId2, delegate.get());
3125 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
3127 delegate->Reset();
3128 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3129 kTouchId1, tes.Now());
3130 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
3131 kTouchId2, tes.Now());
3132 DispatchEventUsingWindowDispatcher(&release1);
3133 DispatchEventUsingWindowDispatcher(&release2);
3135 EXPECT_3_EVENTS(delegate->events(),
3136 ui::ET_GESTURE_END,
3137 ui::ET_SCROLL_FLING_START,
3138 ui::ET_GESTURE_END);
3141 // Like as GestureEventTouchMoveConsumed but tests the different behavior
3142 // depending on whether the events were consumed before or after the scroll
3143 // started.
3144 TEST_F(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) {
3145 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3146 new ConsumesTouchMovesDelegate());
3147 const int kWindowWidth = 123;
3148 const int kWindowHeight = 45;
3149 const int kTouchId = 5;
3150 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3151 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3152 delegate.get(), -1234, bounds, root_window()));
3153 TimedEvents tes;
3155 delegate->Reset();
3156 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3157 kTouchId, tes.Now());
3158 DispatchEventUsingWindowDispatcher(&press);
3159 EXPECT_FALSE(delegate->tap());
3160 EXPECT_TRUE(delegate->tap_down());
3161 EXPECT_FALSE(delegate->tap_cancel());
3162 EXPECT_TRUE(delegate->begin());
3163 EXPECT_FALSE(delegate->scroll_begin());
3164 EXPECT_FALSE(delegate->scroll_update());
3165 EXPECT_FALSE(delegate->scroll_end());
3167 // Move the touch-point enough so that it would normally be considered a
3168 // scroll. But since the touch-moves will be consumed, the scroll should not
3169 // start.
3170 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3171 EXPECT_FALSE(delegate->tap());
3172 EXPECT_FALSE(delegate->tap_down());
3173 EXPECT_TRUE(delegate->tap_cancel());
3174 EXPECT_FALSE(delegate->begin());
3175 EXPECT_FALSE(delegate->scroll_update());
3176 EXPECT_FALSE(delegate->scroll_end());
3178 // Consuming the first touch move event won't prevent all future scrolling.
3179 EXPECT_TRUE(delegate->scroll_begin());
3181 // Now, stop consuming touch-move events, and move the touch-point again.
3182 delegate->set_consume_touch_move(false);
3183 tes.SendScrollEvent(event_processor(), 159, 259, kTouchId, delegate.get());
3184 EXPECT_FALSE(delegate->tap());
3185 EXPECT_FALSE(delegate->tap_down());
3186 EXPECT_FALSE(delegate->tap_cancel());
3187 EXPECT_FALSE(delegate->begin());
3188 EXPECT_FALSE(delegate->scroll_begin());
3189 EXPECT_FALSE(delegate->scroll_end());
3191 // Scroll not prevented by consumed first touch move.
3192 EXPECT_TRUE(delegate->scroll_update());
3193 EXPECT_EQ(29, delegate->scroll_x());
3194 EXPECT_EQ(29, delegate->scroll_y());
3195 EXPECT_EQ(gfx::Point(0, 0).ToString(),
3196 delegate->scroll_begin_position().ToString());
3198 // Start consuming touch-move events again.
3199 delegate->set_consume_touch_move(true);
3201 // Move some more to generate a few more scroll updates.
3202 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
3203 EXPECT_FALSE(delegate->tap());
3204 EXPECT_FALSE(delegate->tap_down());
3205 EXPECT_FALSE(delegate->tap_cancel());
3206 EXPECT_FALSE(delegate->begin());
3207 EXPECT_FALSE(delegate->scroll_begin());
3208 EXPECT_FALSE(delegate->scroll_update());
3209 EXPECT_FALSE(delegate->scroll_end());
3210 EXPECT_EQ(0, delegate->scroll_x());
3211 EXPECT_EQ(0, delegate->scroll_y());
3213 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
3214 EXPECT_FALSE(delegate->tap());
3215 EXPECT_FALSE(delegate->tap_down());
3216 EXPECT_FALSE(delegate->tap_cancel());
3217 EXPECT_FALSE(delegate->begin());
3218 EXPECT_FALSE(delegate->scroll_begin());
3219 EXPECT_FALSE(delegate->scroll_update());
3220 EXPECT_FALSE(delegate->scroll_end());
3221 EXPECT_EQ(0, delegate->scroll_x());
3222 EXPECT_EQ(0, delegate->scroll_y());
3224 // Release the touch.
3225 delegate->Reset();
3226 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3227 kTouchId, tes.LeapForward(50));
3228 DispatchEventUsingWindowDispatcher(&release);
3229 EXPECT_FALSE(delegate->tap());
3230 EXPECT_FALSE(delegate->tap_down());
3231 EXPECT_FALSE(delegate->tap_cancel());
3232 EXPECT_FALSE(delegate->begin());
3233 EXPECT_TRUE(delegate->end());
3234 EXPECT_FALSE(delegate->scroll_begin());
3235 EXPECT_FALSE(delegate->scroll_update());
3236 EXPECT_FALSE(delegate->fling());
3238 EXPECT_TRUE(delegate->scroll_end());
3241 // Check that appropriate touch events generate double tap gesture events.
3242 TEST_F(GestureRecognizerTest, GestureEventDoubleTap) {
3243 scoped_ptr<GestureEventConsumeDelegate> delegate(
3244 new GestureEventConsumeDelegate());
3245 const int kWindowWidth = 123;
3246 const int kWindowHeight = 45;
3247 const int kTouchId = 2;
3248 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3249 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3250 delegate.get(), -1234, bounds, root_window()));
3251 TimedEvents tes;
3253 // First tap (tested in GestureEventTap)
3254 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3255 kTouchId, tes.Now());
3256 DispatchEventUsingWindowDispatcher(&press1);
3257 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3258 kTouchId, tes.LeapForward(50));
3259 DispatchEventUsingWindowDispatcher(&release1);
3260 delegate->Reset();
3262 // Second tap
3263 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3264 kTouchId, tes.LeapForward(200));
3265 DispatchEventUsingWindowDispatcher(&press2);
3266 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3267 kTouchId, tes.LeapForward(50));
3268 DispatchEventUsingWindowDispatcher(&release2);
3270 EXPECT_TRUE(delegate->tap());
3271 EXPECT_TRUE(delegate->tap_down());
3272 EXPECT_FALSE(delegate->tap_cancel());
3273 EXPECT_TRUE(delegate->begin());
3274 EXPECT_TRUE(delegate->end());
3275 EXPECT_FALSE(delegate->scroll_begin());
3276 EXPECT_FALSE(delegate->scroll_update());
3277 EXPECT_FALSE(delegate->scroll_end());
3279 EXPECT_EQ(2, delegate->tap_count());
3282 // Check that appropriate touch events generate triple tap gesture events.
3283 TEST_F(GestureRecognizerTest, GestureEventTripleTap) {
3284 scoped_ptr<GestureEventConsumeDelegate> delegate(
3285 new GestureEventConsumeDelegate());
3286 const int kWindowWidth = 123;
3287 const int kWindowHeight = 45;
3288 const int kTouchId = 2;
3289 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3290 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3291 delegate.get(), -1234, bounds, root_window()));
3292 TimedEvents tes;
3294 // First tap (tested in GestureEventTap)
3295 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3296 kTouchId, tes.Now());
3297 DispatchEventUsingWindowDispatcher(&press1);
3298 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3299 kTouchId, tes.LeapForward(50));
3300 DispatchEventUsingWindowDispatcher(&release1);
3302 EXPECT_EQ(1, delegate->tap_count());
3303 delegate->Reset();
3305 // Second tap (tested in GestureEventDoubleTap)
3306 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3307 kTouchId, tes.LeapForward(200));
3308 DispatchEventUsingWindowDispatcher(&press2);
3309 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3310 kTouchId, tes.LeapForward(50));
3311 DispatchEventUsingWindowDispatcher(&release2);
3313 EXPECT_EQ(2, delegate->tap_count());
3314 delegate->Reset();
3316 // Third tap
3317 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(102, 206),
3318 kTouchId, tes.LeapForward(200));
3319 DispatchEventUsingWindowDispatcher(&press3);
3320 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3321 kTouchId, tes.LeapForward(50));
3322 DispatchEventUsingWindowDispatcher(&release3);
3324 // Third, Fourth and Fifth Taps. Taps after the third should have their
3325 // |tap_count| wrap around back to 1.
3326 for (int i = 3; i < 5; ++i) {
3327 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED,
3328 gfx::Point(102, 206),
3329 kTouchId,
3330 tes.LeapForward(200));
3331 DispatchEventUsingWindowDispatcher(&press3);
3332 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED,
3333 gfx::Point(102, 206),
3334 kTouchId,
3335 tes.LeapForward(50));
3336 DispatchEventUsingWindowDispatcher(&release3);
3338 EXPECT_TRUE(delegate->tap());
3339 EXPECT_TRUE(delegate->tap_down());
3340 EXPECT_FALSE(delegate->tap_cancel());
3341 EXPECT_TRUE(delegate->begin());
3342 EXPECT_TRUE(delegate->end());
3343 EXPECT_FALSE(delegate->scroll_begin());
3344 EXPECT_FALSE(delegate->scroll_update());
3345 EXPECT_FALSE(delegate->scroll_end());
3346 EXPECT_EQ(1 + (i % 3), delegate->tap_count());
3350 // Check that we don't get a double tap when the two taps are far apart.
3351 TEST_F(GestureRecognizerTest, TwoTapsFarApart) {
3352 scoped_ptr<GestureEventConsumeDelegate> delegate(
3353 new GestureEventConsumeDelegate());
3354 const int kWindowWidth = 123;
3355 const int kWindowHeight = 45;
3356 const int kTouchId = 2;
3357 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3358 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3359 delegate.get(), -1234, bounds, root_window()));
3360 TimedEvents tes;
3362 // First tap (tested in GestureEventTap)
3363 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3364 kTouchId, tes.Now());
3365 DispatchEventUsingWindowDispatcher(&press1);
3366 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3367 kTouchId, tes.LeapForward(50));
3368 DispatchEventUsingWindowDispatcher(&release1);
3369 delegate->Reset();
3371 // Second tap, close in time but far in distance
3372 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201),
3373 kTouchId, tes.LeapForward(200));
3374 DispatchEventUsingWindowDispatcher(&press2);
3375 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201),
3376 kTouchId, tes.LeapForward(50));
3377 DispatchEventUsingWindowDispatcher(&release2);
3379 EXPECT_TRUE(delegate->tap());
3380 EXPECT_TRUE(delegate->tap_down());
3381 EXPECT_FALSE(delegate->tap_cancel());
3382 EXPECT_TRUE(delegate->begin());
3383 EXPECT_TRUE(delegate->end());
3384 EXPECT_FALSE(delegate->scroll_begin());
3385 EXPECT_FALSE(delegate->scroll_update());
3386 EXPECT_FALSE(delegate->scroll_end());
3388 EXPECT_EQ(1, delegate->tap_count());
3391 // Check that we don't get a double tap when the two taps have a long enough
3392 // delay in between.
3393 TEST_F(GestureRecognizerTest, TwoTapsWithDelayBetween) {
3394 scoped_ptr<GestureEventConsumeDelegate> delegate(
3395 new GestureEventConsumeDelegate());
3396 const int kWindowWidth = 123;
3397 const int kWindowHeight = 45;
3398 const int kTouchId = 2;
3399 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3400 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3401 delegate.get(), -1234, bounds, root_window()));
3402 TimedEvents tes;
3404 // First tap (tested in GestureEventTap)
3405 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3406 kTouchId, tes.Now());
3407 DispatchEventUsingWindowDispatcher(&press1);
3408 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3409 kTouchId, tes.LeapForward(50));
3410 DispatchEventUsingWindowDispatcher(&release1);
3411 delegate->Reset();
3413 // Second tap, close in distance but after some delay
3414 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3415 kTouchId, tes.LeapForward(2000));
3416 DispatchEventUsingWindowDispatcher(&press2);
3417 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3418 kTouchId, tes.LeapForward(50));
3419 DispatchEventUsingWindowDispatcher(&release2);
3421 EXPECT_TRUE(delegate->tap());
3422 EXPECT_TRUE(delegate->tap_down());
3423 EXPECT_FALSE(delegate->tap_cancel());
3424 EXPECT_TRUE(delegate->begin());
3425 EXPECT_TRUE(delegate->end());
3426 EXPECT_FALSE(delegate->scroll_begin());
3427 EXPECT_FALSE(delegate->scroll_update());
3428 EXPECT_FALSE(delegate->scroll_end());
3430 EXPECT_EQ(1, delegate->tap_count());
3433 // Checks that if the bounding-box of a gesture changes because of change in
3434 // radius of a touch-point, and not because of change in position, then there
3435 // are not gesture events from that.
3436 TEST_F(GestureRecognizerTest, BoundingBoxRadiusChange) {
3437 scoped_ptr<GestureEventConsumeDelegate> delegate(
3438 new GestureEventConsumeDelegate());
3439 const int kWindowWidth = 234;
3440 const int kWindowHeight = 345;
3441 const int kTouchId = 5, kTouchId2 = 7;
3442 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3443 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3444 delegate.get(), -1234, bounds, root_window()));
3445 TimedEvents tes;
3447 ui::TouchEvent press1(
3448 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, tes.Now());
3449 DispatchEventUsingWindowDispatcher(&press1);
3450 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
3452 delegate->Reset();
3454 ui::TouchEvent press2(
3455 ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2,
3456 tes.LeapForward(400));
3457 press2.set_radius_x(5);
3458 DispatchEventUsingWindowDispatcher(&press2);
3459 EXPECT_FALSE(delegate->pinch_begin());
3460 EXPECT_EQ(gfx::Rect(101, 196, 105, 10).ToString(),
3461 delegate->bounding_box().ToString());
3463 delegate->Reset();
3465 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(50, 50), kTouchId,
3466 tes.LeapForward(40));
3467 DispatchEventUsingWindowDispatcher(&move1);
3468 EXPECT_TRUE(delegate->pinch_begin());
3469 EXPECT_EQ(gfx::Rect(50, 50, 156, 156).ToString(),
3470 delegate->bounding_box().ToString());
3472 delegate->Reset();
3474 // The position doesn't move, but the radius changes.
3475 ui::TouchEvent move2(
3476 ui::ET_TOUCH_MOVED, gfx::Point(50, 50), kTouchId, tes.LeapForward(40));
3477 move2.set_radius_x(50);
3478 move2.set_radius_y(60);
3479 DispatchEventUsingWindowDispatcher(&move2);
3480 EXPECT_FALSE(delegate->tap());
3481 EXPECT_FALSE(delegate->tap_cancel());
3482 EXPECT_FALSE(delegate->scroll_update());
3483 EXPECT_FALSE(delegate->pinch_update());
3485 delegate->Reset();
3488 // Checks that slow scrolls deliver the correct deltas.
3489 // In particular, fix for http;//crbug.com/150573.
3490 TEST_F(GestureRecognizerTest, NoDriftInScroll) {
3491 ui::GestureConfiguration::GetInstance()
3492 ->set_max_touch_move_in_pixels_for_click(3);
3493 scoped_ptr<GestureEventConsumeDelegate> delegate(
3494 new GestureEventConsumeDelegate());
3495 const int kWindowWidth = 234;
3496 const int kWindowHeight = 345;
3497 const int kTouchId = 5;
3498 TimedEvents tes;
3499 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3500 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3501 delegate.get(), -1234, bounds, root_window()));
3503 ui::TouchEvent press1(
3504 ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, tes.Now());
3505 DispatchEventUsingWindowDispatcher(&press1);
3506 EXPECT_TRUE(delegate->begin());
3508 delegate->Reset();
3510 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId,
3511 tes.LeapForward(40));
3512 DispatchEventUsingWindowDispatcher(&move1);
3513 EXPECT_FALSE(delegate->scroll_begin());
3515 delegate->Reset();
3517 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3518 tes.LeapForward(40));
3519 DispatchEventUsingWindowDispatcher(&move2);
3520 EXPECT_TRUE(delegate->tap_cancel());
3521 EXPECT_TRUE(delegate->scroll_begin());
3522 EXPECT_TRUE(delegate->scroll_update());
3523 // 3 px consumed by touch slop region.
3524 EXPECT_EQ(-1, delegate->scroll_y());
3525 EXPECT_EQ(-4, delegate->scroll_y_hint());
3527 delegate->Reset();
3529 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3530 tes.LeapForward(40));
3531 DispatchEventUsingWindowDispatcher(&move3);
3532 EXPECT_FALSE(delegate->scroll_update());
3534 delegate->Reset();
3536 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId,
3537 tes.LeapForward(40));
3538 DispatchEventUsingWindowDispatcher(&move4);
3539 EXPECT_TRUE(delegate->scroll_update());
3540 EXPECT_EQ(-1, delegate->scroll_y());
3542 delegate->Reset();
3545 // Ensure that move events which are preventDefaulted will cause a tap
3546 // cancel gesture event to be fired if the move would normally cause a
3547 // scroll. See bug http://crbug.com/146397.
3548 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveCanFireTapCancel) {
3549 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3550 new ConsumesTouchMovesDelegate());
3551 const int kTouchId = 5;
3552 gfx::Rect bounds(100, 200, 123, 45);
3553 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3554 delegate.get(), -1234, bounds, root_window()));
3555 TimedEvents tes;
3557 delegate->Reset();
3558 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3559 kTouchId, tes.Now());
3561 delegate->set_consume_touch_move(false);
3562 DispatchEventUsingWindowDispatcher(&press);
3563 delegate->set_consume_touch_move(true);
3564 delegate->Reset();
3565 // Move the touch-point enough so that it would normally be considered a
3566 // scroll. But since the touch-moves will be consumed, no scrolling should
3567 // occur.
3568 // With the unified gesture detector, we will receive a scroll begin gesture,
3569 // whereas with the aura gesture recognizer we won't.
3570 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3571 EXPECT_FALSE(delegate->tap());
3572 EXPECT_FALSE(delegate->tap_down());
3573 EXPECT_TRUE(delegate->tap_cancel());
3574 EXPECT_FALSE(delegate->begin());
3575 EXPECT_FALSE(delegate->scroll_update());
3576 EXPECT_FALSE(delegate->scroll_end());
3579 TEST_F(GestureRecognizerTest, CancelAllActiveTouches) {
3580 scoped_ptr<GestureEventConsumeDelegate> delegate(
3581 new GestureEventConsumeDelegate());
3582 TimedEvents tes;
3583 const int kWindowWidth = 800;
3584 const int kWindowHeight = 600;
3585 const int kTouchId1 = 1;
3586 const int kTouchId2 = 2;
3587 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3588 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3589 delegate.get(), -1234, bounds, root_window()));
3590 scoped_ptr<TestEventHandler> handler(new TestEventHandler());
3591 window->AddPreTargetHandler(handler.get());
3593 // Start a gesture sequence on |window|. Then cancel all touches.
3594 // Make sure |window| receives a touch-cancel event.
3595 delegate->Reset();
3596 ui::TouchEvent press(
3597 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId1, tes.Now());
3598 DispatchEventUsingWindowDispatcher(&press);
3599 EXPECT_2_EVENTS(
3600 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
3601 delegate->Reset();
3602 ui::TouchEvent p2(
3603 ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), kTouchId2, tes.Now());
3604 DispatchEventUsingWindowDispatcher(&p2);
3605 EXPECT_2_EVENTS(
3606 delegate->events(), ui::ET_GESTURE_TAP_CANCEL, ui::ET_GESTURE_BEGIN);
3607 delegate->Reset();
3608 ui::TouchEvent move(
3609 ui::ET_TOUCH_MOVED, gfx::Point(350, 300), kTouchId2, tes.Now());
3610 DispatchEventUsingWindowDispatcher(&move);
3611 EXPECT_3_EVENTS(delegate->events(),
3612 ui::ET_GESTURE_SCROLL_BEGIN,
3613 ui::ET_GESTURE_SCROLL_UPDATE,
3614 ui::ET_GESTURE_PINCH_BEGIN);
3615 EXPECT_EQ(2, handler->touch_pressed_count());
3616 delegate->Reset();
3617 handler->Reset();
3619 ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get();
3620 EXPECT_EQ(window.get(),
3621 gesture_recognizer->GetTouchLockedTarget(press));
3623 ui::GestureRecognizer::Get()->CancelActiveTouchesExcept(nullptr);
3625 EXPECT_EQ(NULL, gesture_recognizer->GetTouchLockedTarget(press));
3626 EXPECT_4_EVENTS(delegate->events(),
3627 ui::ET_GESTURE_PINCH_END,
3628 ui::ET_GESTURE_SCROLL_END,
3629 ui::ET_GESTURE_END,
3630 ui::ET_GESTURE_END);
3631 const std::vector<gfx::PointF>& points = handler->cancelled_touch_points();
3632 EXPECT_EQ(2U, points.size());
3633 EXPECT_EQ(gfx::Point(101, 201), points[0]);
3634 EXPECT_EQ(gfx::Point(350, 300), points[1]);
3637 // Check that appropriate touch events generate show press events
3638 TEST_F(GestureRecognizerTest, GestureEventShowPress) {
3639 scoped_ptr<GestureEventConsumeDelegate> delegate(
3640 new GestureEventConsumeDelegate());
3641 TimedEvents tes;
3642 const int kWindowWidth = 123;
3643 const int kWindowHeight = 45;
3644 const int kTouchId = 2;
3645 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3646 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3647 delegate.get(), -1234, bounds, root_window()));
3649 delegate->Reset();
3651 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3652 kTouchId, tes.Now());
3653 DispatchEventUsingWindowDispatcher(&press1);
3654 EXPECT_TRUE(delegate->tap_down());
3655 EXPECT_TRUE(delegate->begin());
3656 EXPECT_FALSE(delegate->tap_cancel());
3658 // We haven't pressed long enough for a show press to occur
3659 EXPECT_FALSE(delegate->show_press());
3661 // Wait until the timer runs out
3662 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
3663 EXPECT_TRUE(delegate->show_press());
3664 EXPECT_FALSE(delegate->tap_cancel());
3666 delegate->Reset();
3667 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3668 kTouchId, tes.Now());
3669 DispatchEventUsingWindowDispatcher(&release1);
3670 EXPECT_FALSE(delegate->long_press());
3672 // Note the tap isn't dispatched until the release
3673 EXPECT_FALSE(delegate->tap_cancel());
3674 EXPECT_TRUE(delegate->tap());
3677 // Check that scrolling cancels a show press
3678 TEST_F(GestureRecognizerTest, GestureEventShowPressCancelledByScroll) {
3679 scoped_ptr<GestureEventConsumeDelegate> delegate(
3680 new GestureEventConsumeDelegate());
3681 TimedEvents tes;
3682 const int kWindowWidth = 123;
3683 const int kWindowHeight = 45;
3684 const int kTouchId = 6;
3685 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3686 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3687 delegate.get(), -1234, bounds, root_window()));
3689 delegate->Reset();
3691 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3692 kTouchId, tes.Now());
3693 DispatchEventUsingWindowDispatcher(&press1);
3694 EXPECT_TRUE(delegate->tap_down());
3696 // We haven't pressed long enough for a show press to occur
3697 EXPECT_FALSE(delegate->show_press());
3698 EXPECT_FALSE(delegate->tap_cancel());
3700 // Scroll around, to cancel the show press
3701 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3702 // Wait until the timer runs out
3703 DelayByShowPressTimeout();
3704 EXPECT_FALSE(delegate->show_press());
3705 EXPECT_TRUE(delegate->tap_cancel());
3707 delegate->Reset();
3708 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3709 kTouchId, tes.LeapForward(10));
3710 DispatchEventUsingWindowDispatcher(&release1);
3711 EXPECT_FALSE(delegate->show_press());
3712 EXPECT_FALSE(delegate->tap_cancel());
3715 // Test that show press events are sent immediately on tap
3716 TEST_F(GestureRecognizerTest, GestureEventShowPressSentOnTap) {
3717 scoped_ptr<GestureEventConsumeDelegate> delegate(
3718 new GestureEventConsumeDelegate());
3719 TimedEvents tes;
3720 const int kWindowWidth = 123;
3721 const int kWindowHeight = 45;
3722 const int kTouchId = 6;
3723 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3724 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3725 delegate.get(), -1234, bounds, root_window()));
3727 delegate->Reset();
3729 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3730 kTouchId, tes.Now());
3731 DispatchEventUsingWindowDispatcher(&press1);
3732 EXPECT_TRUE(delegate->tap_down());
3734 // We haven't pressed long enough for a show press to occur
3735 EXPECT_FALSE(delegate->show_press());
3736 EXPECT_FALSE(delegate->tap_cancel());
3738 delegate->Reset();
3739 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3740 kTouchId, tes.LeapForward(50));
3741 DispatchEventUsingWindowDispatcher(&release1);
3742 EXPECT_TRUE(delegate->show_press());
3743 EXPECT_FALSE(delegate->tap_cancel());
3744 EXPECT_TRUE(delegate->tap());
3747 // Test that consuming the first move touch event prevents a scroll.
3748 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveScrollTest) {
3749 scoped_ptr<QueueTouchEventDelegate> delegate(
3750 new QueueTouchEventDelegate(host()->dispatcher()));
3751 TimedEvents tes;
3752 const int kTouchId = 7;
3753 gfx::Rect bounds(0, 0, 1000, 1000);
3754 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3755 delegate.get(), -1234, bounds, root_window()));
3756 delegate->set_window(window.get());
3758 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3759 kTouchId, tes.Now());
3760 DispatchEventUsingWindowDispatcher(&press);
3761 delegate->ReceivedAck();
3763 // A touch move within the slop region is never consumed in web contents. The
3764 // unified GR won't prevent scroll if a touch move within the slop region is
3765 // consumed, so make sure this touch move exceeds the slop region.
3766 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(10, 10),
3767 kTouchId, tes.Now());
3768 DispatchEventUsingWindowDispatcher(&move1);
3769 delegate->ReceivedAckPreventDefaulted();
3771 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3772 kTouchId, tes.Now());
3773 DispatchEventUsingWindowDispatcher(&move2);
3774 delegate->ReceivedAck();
3776 // With the unified gesture detector, consuming the first touch move event
3777 // won't prevent all future scrolling.
3778 EXPECT_TRUE(delegate->scroll_begin());
3779 EXPECT_TRUE(delegate->scroll_update());
3782 // Test that consuming the first move touch doesn't prevent a tap.
3783 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveTapTest) {
3784 scoped_ptr<QueueTouchEventDelegate> delegate(
3785 new QueueTouchEventDelegate(host()->dispatcher()));
3786 TimedEvents tes;
3787 const int kTouchId = 7;
3788 gfx::Rect bounds(0, 0, 1000, 1000);
3789 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3790 delegate.get(), -1234, bounds, root_window()));
3791 delegate->set_window(window.get());
3793 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3794 kTouchId, tes.Now());
3795 DispatchEventUsingWindowDispatcher(&press);
3796 delegate->ReceivedAck();
3798 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
3799 kTouchId, tes.Now());
3800 DispatchEventUsingWindowDispatcher(&move);
3801 delegate->ReceivedAckPreventDefaulted();
3803 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(2, 2),
3804 kTouchId, tes.LeapForward(50));
3805 DispatchEventUsingWindowDispatcher(&release);
3806 delegate->ReceivedAck();
3808 EXPECT_TRUE(delegate->tap());
3811 // Test that consuming the first move touch doesn't prevent a long press.
3812 TEST_F(GestureRecognizerTest, GestureEventConsumedTouchMoveLongPressTest) {
3813 scoped_ptr<QueueTouchEventDelegate> delegate(
3814 new QueueTouchEventDelegate(host()->dispatcher()));
3815 TimedEvents tes;
3816 const int kWindowWidth = 123;
3817 const int kWindowHeight = 45;
3818 const int kTouchId = 2;
3819 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3820 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3821 delegate.get(), -1234, bounds, root_window()));
3822 delegate->set_window(window.get());
3824 delegate->Reset();
3826 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3827 kTouchId, tes.Now());
3828 DispatchEventUsingWindowDispatcher(&press1);
3829 delegate->ReceivedAck();
3831 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103, 203),
3832 kTouchId, tes.Now());
3833 DispatchEventUsingWindowDispatcher(&move);
3834 delegate->ReceivedAckPreventDefaulted();
3836 // Wait until the timer runs out
3837 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
3838 EXPECT_TRUE(delegate->long_press());
3841 // Tests that the deltas are correct when leaving the slop region very slowly.
3842 TEST_F(GestureRecognizerTest, TestExceedingSlopSlowly) {
3843 ui::GestureConfiguration::GetInstance()
3844 ->set_max_touch_move_in_pixels_for_click(3);
3845 scoped_ptr<GestureEventConsumeDelegate> delegate(
3846 new GestureEventConsumeDelegate());
3847 const int kWindowWidth = 234;
3848 const int kWindowHeight = 345;
3849 const int kTouchId = 5;
3850 TimedEvents tes;
3851 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3852 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3853 delegate.get(), -1234, bounds, root_window()));
3855 ui::TouchEvent press(
3856 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), kTouchId, tes.Now());
3857 DispatchEventUsingWindowDispatcher(&press);
3858 EXPECT_FALSE(delegate->scroll_begin());
3859 EXPECT_FALSE(delegate->scroll_update());
3860 delegate->Reset();
3862 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(11, 10), kTouchId,
3863 tes.LeapForward(40));
3864 DispatchEventUsingWindowDispatcher(&move1);
3865 EXPECT_FALSE(delegate->scroll_begin());
3866 EXPECT_FALSE(delegate->scroll_update());
3867 EXPECT_EQ(0, delegate->scroll_x());
3868 EXPECT_EQ(0, delegate->scroll_x_hint());
3869 delegate->Reset();
3871 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(12, 10), kTouchId,
3872 tes.LeapForward(40));
3873 DispatchEventUsingWindowDispatcher(&move2);
3874 EXPECT_FALSE(delegate->scroll_begin());
3875 EXPECT_FALSE(delegate->scroll_update());
3876 EXPECT_EQ(0, delegate->scroll_x());
3877 EXPECT_EQ(0, delegate->scroll_x_hint());
3878 delegate->Reset();
3881 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::PointF(13.1f, 10.f), kTouchId,
3882 tes.LeapForward(40));
3883 DispatchEventUsingWindowDispatcher(&move3);
3884 EXPECT_TRUE(delegate->scroll_begin());
3885 EXPECT_TRUE(delegate->scroll_update());
3886 EXPECT_NEAR(0.1, delegate->scroll_x(), 0.0001);
3887 EXPECT_FLOAT_EQ(3.1f, delegate->scroll_x_hint());
3888 delegate->Reset();
3890 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(14, 10), kTouchId,
3891 tes.LeapForward(40));
3892 DispatchEventUsingWindowDispatcher(&move4);
3893 EXPECT_FALSE(delegate->scroll_begin());
3894 EXPECT_TRUE(delegate->scroll_update());
3895 EXPECT_NEAR(0.9, delegate->scroll_x(), 0.0001);
3896 EXPECT_EQ(0.f, delegate->scroll_x_hint());
3897 delegate->Reset();
3900 TEST_F(GestureRecognizerTest, ScrollAlternatelyConsumedTest) {
3901 scoped_ptr<QueueTouchEventDelegate> delegate(
3902 new QueueTouchEventDelegate(host()->dispatcher()));
3903 TimedEvents tes;
3904 const int kWindowWidth = 3000;
3905 const int kWindowHeight = 3000;
3906 const int kTouchId = 2;
3907 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3908 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3909 delegate.get(), -1234, bounds, root_window()));
3910 delegate->set_window(window.get());
3912 delegate->Reset();
3914 int x = 0;
3915 int y = 0;
3917 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
3918 kTouchId, tes.Now());
3919 DispatchEventUsingWindowDispatcher(&press1);
3920 delegate->ReceivedAck();
3921 EXPECT_FALSE(delegate->scroll_begin());
3922 EXPECT_FALSE(delegate->scroll_update());
3923 delegate->Reset();
3925 x += 100;
3926 y += 100;
3927 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
3928 kTouchId, tes.Now());
3929 DispatchEventUsingWindowDispatcher(&move1);
3930 delegate->ReceivedAck();
3931 EXPECT_TRUE(delegate->scroll_begin());
3932 EXPECT_TRUE(delegate->scroll_update());
3933 delegate->Reset();
3935 for (int i = 0; i < 3; ++i) {
3936 x += 10;
3937 y += 10;
3938 ui::TouchEvent move2(
3939 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
3940 DispatchEventUsingWindowDispatcher(&move2);
3941 delegate->ReceivedAck();
3942 EXPECT_FALSE(delegate->scroll_begin());
3943 EXPECT_TRUE(delegate->scroll_update());
3944 EXPECT_EQ(10, delegate->scroll_x());
3945 EXPECT_EQ(10, delegate->scroll_y());
3946 delegate->Reset();
3948 x += 20;
3949 y += 20;
3950 ui::TouchEvent move3(
3951 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
3952 DispatchEventUsingWindowDispatcher(&move3);
3953 delegate->ReceivedAckPreventDefaulted();
3954 EXPECT_FALSE(delegate->scroll_begin());
3955 EXPECT_FALSE(delegate->scroll_update());
3956 delegate->Reset();
3960 TEST_F(GestureRecognizerTest, PinchAlternatelyConsumedTest) {
3961 scoped_ptr<QueueTouchEventDelegate> delegate(
3962 new QueueTouchEventDelegate(host()->dispatcher()));
3963 TimedEvents tes;
3964 const int kWindowWidth = 3000;
3965 const int kWindowHeight = 3000;
3966 const int kTouchId1 = 5;
3967 const int kTouchId2 = 7;
3968 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3969 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3970 delegate.get(), -1234, bounds, root_window()));
3971 delegate->set_window(window.get());
3972 delegate->Reset();
3974 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3975 kTouchId1, tes.Now());
3976 DispatchEventUsingWindowDispatcher(&press1);
3977 delegate->ReceivedAck();
3978 EXPECT_FALSE(delegate->scroll_begin());
3979 EXPECT_FALSE(delegate->scroll_update());
3980 delegate->Reset();
3982 int x = 0;
3983 int y = 0;
3985 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
3986 kTouchId2, tes.Now());
3987 DispatchEventUsingWindowDispatcher(&press2);
3988 delegate->ReceivedAck();
3989 EXPECT_FALSE(delegate->scroll_begin());
3990 EXPECT_FALSE(delegate->scroll_update());
3991 EXPECT_FALSE(delegate->pinch_begin());
3992 EXPECT_FALSE(delegate->pinch_update());
3994 delegate->Reset();
3996 x += 100;
3997 y += 100;
3998 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
3999 kTouchId2, tes.Now());
4000 DispatchEventUsingWindowDispatcher(&move1);
4001 delegate->ReceivedAck();
4002 EXPECT_TRUE(delegate->scroll_begin());
4003 EXPECT_TRUE(delegate->scroll_update());
4004 EXPECT_TRUE(delegate->pinch_begin());
4005 EXPECT_FALSE(delegate->pinch_update());
4006 delegate->Reset();
4008 const float expected_scales[] = {1.5f, 1.2f, 1.125f};
4010 for (int i = 0; i < 3; ++i) {
4011 x += 50;
4012 y += 50;
4013 ui::TouchEvent move2(
4014 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4015 DispatchEventUsingWindowDispatcher(&move2);
4016 delegate->ReceivedAck();
4017 EXPECT_FALSE(delegate->scroll_begin());
4018 EXPECT_TRUE(delegate->scroll_update());
4019 EXPECT_FALSE(delegate->scroll_end());
4020 EXPECT_FALSE(delegate->pinch_begin());
4021 EXPECT_TRUE(delegate->pinch_update());
4022 EXPECT_FALSE(delegate->pinch_end());
4023 EXPECT_EQ(25, delegate->scroll_x());
4024 EXPECT_EQ(25, delegate->scroll_y());
4025 EXPECT_FLOAT_EQ(expected_scales[i], delegate->scale());
4026 delegate->Reset();
4028 x += 100;
4029 y += 100;
4030 ui::TouchEvent move3(
4031 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4032 DispatchEventUsingWindowDispatcher(&move3);
4033 delegate->ReceivedAckPreventDefaulted();
4034 EXPECT_FALSE(delegate->scroll_begin());
4035 EXPECT_FALSE(delegate->scroll_update());
4036 EXPECT_FALSE(delegate->scroll_end());
4037 EXPECT_FALSE(delegate->pinch_begin());
4038 EXPECT_FALSE(delegate->pinch_update());
4039 EXPECT_FALSE(delegate->pinch_end());
4040 delegate->Reset();
4044 // Test that touch event flags are passed through to the gesture event.
4045 TEST_F(GestureRecognizerTest, GestureEventFlagsPassedFromTouchEvent) {
4046 scoped_ptr<GestureEventConsumeDelegate> delegate(
4047 new GestureEventConsumeDelegate());
4048 TimedEvents tes;
4049 const int kWindowWidth = 123;
4050 const int kWindowHeight = 45;
4051 const int kTouchId = 6;
4052 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4053 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4054 delegate.get(), -1234, bounds, root_window()));
4056 delegate->Reset();
4058 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4059 kTouchId, tes.Now());
4060 DispatchEventUsingWindowDispatcher(&press1);
4061 EXPECT_TRUE(delegate->tap_down());
4063 int default_flags = delegate->flags();
4065 ui::TouchEvent move1(
4066 ui::ET_TOUCH_MOVED, gfx::Point(397, 149), kTouchId, tes.LeapForward(50));
4067 move1.set_flags(992);
4069 DispatchEventUsingWindowDispatcher(&move1);
4070 EXPECT_NE(default_flags, delegate->flags());
4073 // A delegate that deletes a window on long press.
4074 class GestureEventDeleteWindowOnLongPress : public GestureEventConsumeDelegate {
4075 public:
4076 GestureEventDeleteWindowOnLongPress()
4077 : window_(NULL) {}
4079 void set_window(aura::Window** window) { window_ = window; }
4081 void OnGestureEvent(ui::GestureEvent* gesture) override {
4082 GestureEventConsumeDelegate::OnGestureEvent(gesture);
4083 if (gesture->type() != ui::ET_GESTURE_LONG_PRESS)
4084 return;
4085 ui::GestureRecognizer::Get()->CleanupStateForConsumer(*window_);
4086 delete *window_;
4087 *window_ = NULL;
4090 private:
4091 aura::Window** window_;
4092 DISALLOW_COPY_AND_ASSIGN(GestureEventDeleteWindowOnLongPress);
4095 // Check that deleting the window in response to a long press gesture doesn't
4096 // crash.
4097 TEST_F(GestureRecognizerTest, GestureEventLongPressDeletingWindow) {
4098 GestureEventDeleteWindowOnLongPress delegate;
4099 const int kWindowWidth = 123;
4100 const int kWindowHeight = 45;
4101 const int kTouchId = 2;
4102 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4103 aura::Window* window(CreateTestWindowWithDelegate(
4104 &delegate, -1234, bounds, root_window()));
4105 delegate.set_window(&window);
4107 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
4108 gfx::Point(101, 201),
4109 kTouchId,
4110 ui::EventTimeForNow());
4111 DispatchEventUsingWindowDispatcher(&press1);
4112 EXPECT_TRUE(window != NULL);
4114 // Wait until the timer runs out.
4115 delegate.WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4116 EXPECT_EQ(NULL, window);
4119 TEST_F(GestureRecognizerWithSwitchTest, GestureEventSmallPinchDisabled) {
4120 scoped_ptr<GestureEventConsumeDelegate> delegate(
4121 new GestureEventConsumeDelegate());
4122 TimedEvents tes;
4123 const int kWindowWidth = 300;
4124 const int kWindowHeight = 400;
4125 const int kTouchId1 = 3;
4126 const int kTouchId2 = 5;
4127 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4128 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4129 delegate.get(), -1234, bounds, root_window()));
4131 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4132 kTouchId1, tes.Now());
4133 DispatchEventUsingWindowDispatcher(&press1);
4134 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4135 kTouchId2, tes.Now());
4136 DispatchEventUsingWindowDispatcher(&press2);
4138 // Move the first finger.
4139 delegate->Reset();
4140 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4141 kTouchId1, tes.Now());
4142 DispatchEventUsingWindowDispatcher(&move1);
4144 EXPECT_3_EVENTS(delegate->events(),
4145 ui::ET_GESTURE_SCROLL_BEGIN,
4146 ui::ET_GESTURE_SCROLL_UPDATE,
4147 ui::ET_GESTURE_PINCH_BEGIN);
4149 // No pinch update occurs, as kCompensateForUnstablePinchZoom is on and
4150 // |min_pinch_update_span_delta| was nonzero, and this is a very small pinch.
4151 delegate->Reset();
4152 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4153 kTouchId1, tes.Now());
4154 DispatchEventUsingWindowDispatcher(&move2);
4155 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4158 TEST_F(GestureRecognizerTest, GestureEventSmallPinchEnabled) {
4159 scoped_ptr<GestureEventConsumeDelegate> delegate(
4160 new GestureEventConsumeDelegate());
4161 TimedEvents tes;
4162 const int kWindowWidth = 300;
4163 const int kWindowHeight = 400;
4164 const int kTouchId1 = 3;
4165 const int kTouchId2 = 5;
4166 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4167 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4168 delegate.get(), -1234, bounds, root_window()));
4170 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4171 kTouchId1, tes.Now());
4172 DispatchEventUsingWindowDispatcher(&press1);
4173 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4174 kTouchId2, tes.Now());
4175 DispatchEventUsingWindowDispatcher(&press2);
4177 // Move the first finger.
4178 delegate->Reset();
4179 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4180 kTouchId1, tes.Now());
4181 DispatchEventUsingWindowDispatcher(&move1);
4183 EXPECT_3_EVENTS(delegate->events(),
4184 ui::ET_GESTURE_SCROLL_BEGIN,
4185 ui::ET_GESTURE_SCROLL_UPDATE,
4186 ui::ET_GESTURE_PINCH_BEGIN);
4188 delegate->Reset();
4189 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4190 kTouchId1, tes.Now());
4191 DispatchEventUsingWindowDispatcher(&move2);
4192 EXPECT_2_EVENTS(delegate->events(),
4193 ui::ET_GESTURE_SCROLL_UPDATE,
4194 ui::ET_GESTURE_PINCH_UPDATE);
4197 // Tests that delaying the ack of a touch release doesn't trigger a long press
4198 // gesture.
4199 TEST_F(GestureRecognizerTest, EagerGestureDetection) {
4200 scoped_ptr<QueueTouchEventDelegate> delegate(
4201 new QueueTouchEventDelegate(host()->dispatcher()));
4202 TimedEvents tes;
4203 const int kTouchId = 2;
4204 gfx::Rect bounds(100, 200, 100, 100);
4205 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4206 delegate.get(), -1234, bounds, root_window()));
4207 delegate->set_window(window.get());
4209 delegate->Reset();
4210 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4211 kTouchId, tes.Now());
4212 DispatchEventUsingWindowDispatcher(&press);
4213 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
4214 kTouchId, tes.LeapForward(50));
4215 DispatchEventUsingWindowDispatcher(&release);
4217 delegate->Reset();
4218 // Ack the touch press.
4219 delegate->ReceivedAck();
4220 EXPECT_TRUE(delegate->tap_down());
4222 delegate->Reset();
4223 // Wait until the long press event would fire (if we weren't eager).
4224 DelayByLongPressTimeout();
4226 // Ack the touch release.
4227 delegate->ReceivedAck();
4228 EXPECT_TRUE(delegate->tap());
4229 EXPECT_FALSE(delegate->long_press());
4232 // This tests crbug.com/405519, in which touch events which the gesture detector
4233 // ignores interfere with gesture recognition.
4234 TEST_F(GestureRecognizerTest, IgnoredEventsDontBreakGestureRecognition) {
4235 scoped_ptr<QueueTouchEventDelegate> delegate(
4236 new QueueTouchEventDelegate(host()->dispatcher()));
4237 TimedEvents tes;
4238 const int kWindowWidth = 300;
4239 const int kWindowHeight = 400;
4240 const int kTouchId1 = 3;
4241 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4242 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4243 delegate.get(), -1234, bounds, root_window()));
4244 delegate->set_window(window.get());
4246 ui::TouchEvent press1(
4247 ui::ET_TOUCH_PRESSED, gfx::Point(101, 301), kTouchId1, tes.Now());
4248 DispatchEventUsingWindowDispatcher(&press1);
4249 delegate->ReceivedAck();
4251 EXPECT_2_EVENTS(
4252 delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
4254 // Move the first finger.
4255 delegate->Reset();
4256 ui::TouchEvent move1(
4257 ui::ET_TOUCH_MOVED, gfx::Point(65, 201), kTouchId1, tes.Now());
4258 DispatchEventUsingWindowDispatcher(&move1);
4259 delegate->ReceivedAck();
4261 EXPECT_3_EVENTS(delegate->events(),
4262 ui::ET_GESTURE_TAP_CANCEL,
4263 ui::ET_GESTURE_SCROLL_BEGIN,
4264 ui::ET_GESTURE_SCROLL_UPDATE);
4266 delegate->Reset();
4268 // Send a valid event, but don't ack it.
4269 ui::TouchEvent move2(
4270 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4271 DispatchEventUsingWindowDispatcher(&move2);
4272 EXPECT_0_EVENTS(delegate->events());
4274 // Send a touchmove event at the same location as the previous touchmove
4275 // event. This shouldn't do anything.
4276 ui::TouchEvent move3(
4277 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4278 DispatchEventUsingWindowDispatcher(&move3);
4280 // Ack the previous valid event. The intermediary invalid event shouldn't
4281 // interfere.
4282 delegate->ReceivedAck();
4283 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4286 // Tests that an event stream can have a mix of sync and async acks.
4287 TEST_F(GestureRecognizerTest,
4288 MixedSyncAndAsyncAcksDontCauseOutOfOrderDispatch) {
4289 scoped_ptr<QueueTouchEventDelegate> delegate(
4290 new QueueTouchEventDelegate(host()->dispatcher()));
4291 TimedEvents tes;
4292 const int kWindowWidth = 300;
4293 const int kWindowHeight = 400;
4294 const int kTouchId1 = 3;
4295 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4296 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4297 delegate.get(), -1234, bounds, root_window()));
4298 delegate->set_window(window.get());
4300 // Start a scroll gesture.
4301 ui::TouchEvent press1(
4302 ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), kTouchId1, tes.Now());
4303 DispatchEventUsingWindowDispatcher(&press1);
4304 delegate->ReceivedAck();
4306 ui::TouchEvent move1(
4307 ui::ET_TOUCH_MOVED, gfx::Point(100, 100), kTouchId1, tes.Now());
4308 DispatchEventUsingWindowDispatcher(&move1);
4309 delegate->ReceivedAck();
4311 delegate->Reset();
4312 // Dispatch a synchronously consumed touch move, which should be ignored.
4313 delegate->set_synchronous_ack_for_next_event(true);
4314 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(200, 200), kTouchId1,
4315 tes.Now());
4316 DispatchEventUsingWindowDispatcher(&move2);
4317 EXPECT_0_EVENTS(delegate->events());
4319 // Dispatch a touch move, but don't ack it.
4320 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(300, 300), kTouchId1,
4321 tes.Now());
4322 DispatchEventUsingWindowDispatcher(&move3);
4324 // Dispatch two synchronously consumed touch moves, which should be ignored.
4325 delegate->set_synchronous_ack_for_next_event(true);
4326 ui::TouchEvent move4(
4327 ui::ET_TOUCH_MOVED, gfx::Point(400, 400), kTouchId1, tes.Now());
4328 DispatchEventUsingWindowDispatcher(&move4);
4330 delegate->set_synchronous_ack_for_next_event(true);
4331 ui::TouchEvent move5(
4332 ui::ET_TOUCH_MOVED, gfx::Point(500, 500), kTouchId1, tes.Now());
4333 DispatchEventUsingWindowDispatcher(&move5);
4335 EXPECT_0_EVENTS(delegate->events());
4336 EXPECT_EQ(100, delegate->bounding_box().x());
4337 // Ack the pending touch move, and ensure the most recent gesture event
4338 // used its co-ordinates.
4339 delegate->ReceivedAck();
4340 EXPECT_EQ(300, delegate->bounding_box().x());
4341 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4343 // Dispatch a touch move, but don't ack it.
4344 delegate->Reset();
4345 ui::TouchEvent move6(ui::ET_TOUCH_MOVED, gfx::Point(600, 600), kTouchId1,
4346 tes.Now());
4347 DispatchEventUsingWindowDispatcher(&move6);
4349 // Dispatch a synchronously unconsumed touch move.
4350 delegate->set_synchronous_ack_for_next_event(false);
4351 ui::TouchEvent move7(
4352 ui::ET_TOUCH_MOVED, gfx::Point(700, 700), kTouchId1, tes.Now());
4353 DispatchEventUsingWindowDispatcher(&move7);
4355 // The synchronous ack is stuck behind the pending touch move.
4356 EXPECT_0_EVENTS(delegate->events());
4358 delegate->ReceivedAck();
4359 EXPECT_2_EVENTS(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE,
4360 ui::ET_GESTURE_SCROLL_UPDATE);
4363 } // namespace test
4364 } // namespace aura