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/event_generator.h"
14 #include "ui/aura/test/test_window_delegate.h"
15 #include "ui/aura/test/test_windows.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/base/hit_test.h"
19 #include "ui/base/ui_base_switches.h"
20 #include "ui/events/event.h"
21 #include "ui/events/event_switches.h"
22 #include "ui/events/event_utils.h"
23 #include "ui/events/gestures/gesture_configuration.h"
24 #include "ui/events/gestures/gesture_recognizer_impl.h"
25 #include "ui/events/gestures/gesture_sequence.h"
26 #include "ui/events/gestures/gesture_types.h"
27 #include "ui/gfx/point.h"
28 #include "ui/gfx/rect.h"
37 std::string WindowIDAsString(ui::GestureConsumer* 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 {
70 GestureEventConsumeDelegate()
77 scroll_update_(false),
84 two_finger_tap_(false),
92 scroll_velocity_x_(0),
93 scroll_velocity_y_(0),
100 wait_until_event_(ui::ET_UNKNOWN) {}
102 virtual ~GestureEventConsumeDelegate() {}
111 scroll_begin_ = false;
112 scroll_update_ = false;
114 pinch_begin_ = false;
115 pinch_update_ = false;
119 two_finger_tap_ = false;
122 swipe_right_ = false;
126 scroll_begin_position_.SetPoint(0, 0);
127 tap_location_.SetPoint(0, 0);
128 gesture_end_location_.SetPoint(0, 0);
132 scroll_velocity_x_ = 0;
133 scroll_velocity_y_ = 0;
141 latency_info_.Clear();
144 const std::vector<ui::EventType>& events() const { return events_; };
146 bool tap() const { return tap_; }
147 bool tap_down() const { return tap_down_; }
148 bool tap_cancel() const { return tap_cancel_; }
149 bool begin() const { return begin_; }
150 bool end() const { return end_; }
151 bool scroll_begin() const { return scroll_begin_; }
152 bool scroll_update() const { return scroll_update_; }
153 bool scroll_end() const { return scroll_end_; }
154 bool pinch_begin() const { return pinch_begin_; }
155 bool pinch_update() const { return pinch_update_; }
156 bool pinch_end() const { return pinch_end_; }
157 bool long_press() const { return long_press_; }
158 bool long_tap() const { return long_tap_; }
159 bool fling() const { return fling_; }
160 bool two_finger_tap() const { return two_finger_tap_; }
161 bool show_press() const { return show_press_; }
162 bool swipe_left() const { return swipe_left_; }
163 bool swipe_right() const { return swipe_right_; }
164 bool swipe_up() const { return swipe_up_; }
165 bool swipe_down() const { return swipe_down_; }
167 const gfx::Point& scroll_begin_position() const {
168 return scroll_begin_position_;
171 const gfx::Point& tap_location() const {
172 return tap_location_;
175 const gfx::Point& gesture_end_location() const {
176 return gesture_end_location_;
179 float scroll_x() const { return scroll_x_; }
180 float scroll_y() const { return scroll_y_; }
181 float scroll_velocity_x() const { return scroll_velocity_x_; }
182 float scroll_velocity_y() const { return scroll_velocity_y_; }
183 float velocity_x() const { return velocity_x_; }
184 float velocity_y() const { return velocity_y_; }
185 float scroll_x_hint() const { return scroll_x_hint_; }
186 float scroll_y_hint() const { return scroll_y_hint_; }
187 float scale() const { return scale_; }
188 const gfx::Rect& bounding_box() const { return bounding_box_; }
189 int tap_count() const { return tap_count_; }
190 int flags() const { return flags_; }
191 const ui::LatencyInfo& latency_info() const { return latency_info_; }
193 void WaitUntilReceivedGesture(ui::EventType type) {
194 wait_until_event_ = type;
195 run_loop_.reset(new base::RunLoop());
199 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
200 events_.push_back(gesture->type());
201 bounding_box_ = gesture->details().bounding_box();
202 flags_ = gesture->flags();
203 latency_info_ = *gesture->latency();
204 switch (gesture->type()) {
205 case ui::ET_GESTURE_TAP:
206 tap_location_ = gesture->location();
207 tap_count_ = gesture->details().tap_count();
210 case ui::ET_GESTURE_TAP_DOWN:
213 case ui::ET_GESTURE_TAP_CANCEL:
216 case ui::ET_GESTURE_BEGIN:
219 case ui::ET_GESTURE_END:
221 gesture_end_location_ = gesture->location();
223 case ui::ET_GESTURE_SCROLL_BEGIN:
224 scroll_begin_ = true;
225 scroll_begin_position_ = gesture->location();
226 scroll_x_hint_ = gesture->details().scroll_x_hint();
227 scroll_y_hint_ = gesture->details().scroll_y_hint();
229 case ui::ET_GESTURE_SCROLL_UPDATE:
230 scroll_update_ = true;
231 scroll_x_ += gesture->details().scroll_x();
232 scroll_y_ += gesture->details().scroll_y();
234 case ui::ET_GESTURE_SCROLL_END:
235 EXPECT_TRUE(velocity_x_ == 0 && velocity_y_ == 0);
238 case ui::ET_GESTURE_PINCH_BEGIN:
241 case ui::ET_GESTURE_PINCH_UPDATE:
242 pinch_update_ = true;
243 scale_ = gesture->details().scale();
245 case ui::ET_GESTURE_PINCH_END:
248 case ui::ET_GESTURE_LONG_PRESS:
251 case ui::ET_GESTURE_LONG_TAP:
254 case ui::ET_SCROLL_FLING_START:
255 EXPECT_TRUE(gesture->details().velocity_x() != 0 ||
256 gesture->details().velocity_y() != 0);
257 EXPECT_FALSE(scroll_end_);
259 velocity_x_ = gesture->details().velocity_x();
260 velocity_y_ = gesture->details().velocity_y();
262 case ui::ET_GESTURE_TWO_FINGER_TAP:
263 two_finger_tap_ = true;
265 case ui::ET_GESTURE_SHOW_PRESS:
268 case ui::ET_GESTURE_SWIPE:
269 swipe_left_ = gesture->details().swipe_left();
270 swipe_right_ = gesture->details().swipe_right();
271 swipe_up_ = gesture->details().swipe_up();
272 swipe_down_ = gesture->details().swipe_down();
274 case ui::ET_SCROLL_FLING_CANCEL:
275 // Only used in unified gesture detection.
280 if (wait_until_event_ == gesture->type() && run_loop_) {
282 wait_until_event_ = ui::ET_UNKNOWN;
284 gesture->StopPropagation();
288 scoped_ptr<base::RunLoop> run_loop_;
289 std::vector<ui::EventType> events_;
305 bool two_finger_tap_;
312 gfx::Point scroll_begin_position_;
313 gfx::Point tap_location_;
314 gfx::Point gesture_end_location_;
318 float scroll_velocity_x_;
319 float scroll_velocity_y_;
322 float scroll_x_hint_;
323 float scroll_y_hint_;
325 gfx::Rect bounding_box_;
328 ui::LatencyInfo latency_info_;
330 ui::EventType wait_until_event_;
332 DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate);
335 class QueueTouchEventDelegate : public GestureEventConsumeDelegate {
337 explicit QueueTouchEventDelegate(WindowEventDispatcher* dispatcher)
339 dispatcher_(dispatcher),
340 queue_events_(true) {
342 virtual ~QueueTouchEventDelegate() {
343 while(!queue_.empty()) {
344 delete queue_.front();
349 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
351 queue_.push(new ui::TouchEvent(*event, window_, window_));
352 event->StopPropagation();
357 ReceivedAckImpl(false);
360 void ReceivedAckPreventDefaulted() {
361 ReceivedAckImpl(true);
364 void set_window(Window* w) { window_ = w; }
365 void set_queue_events(bool queue) { queue_events_ = queue; }
368 void ReceivedAckImpl(bool prevent_defaulted) {
369 scoped_ptr<ui::TouchEvent> event(queue_.front());
370 dispatcher_->ProcessedTouchEvent(event.get(), window_,
371 prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED);
375 std::queue<ui::TouchEvent*> queue_;
377 WindowEventDispatcher* dispatcher_;
380 DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate);
383 // A delegate that ignores gesture events but keeps track of [synthetic] mouse
385 class GestureEventSynthDelegate : public TestWindowDelegate {
387 GestureEventSynthDelegate()
388 : mouse_enter_(false),
391 mouse_release_(false),
393 double_click_(false) {
397 mouse_enter_ = false;
399 mouse_press_ = false;
400 mouse_release_ = false;
402 double_click_ = false;
405 bool mouse_enter() const { return mouse_enter_; }
406 bool mouse_exit() const { return mouse_exit_; }
407 bool mouse_press() const { return mouse_press_; }
408 bool mouse_move() const { return mouse_move_; }
409 bool mouse_release() const { return mouse_release_; }
410 bool double_click() const { return double_click_; }
412 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
413 switch (event->type()) {
414 case ui::ET_MOUSE_PRESSED:
415 double_click_ = event->flags() & ui::EF_IS_DOUBLE_CLICK;
418 case ui::ET_MOUSE_RELEASED:
419 mouse_release_ = true;
421 case ui::ET_MOUSE_MOVED:
424 case ui::ET_MOUSE_ENTERED:
427 case ui::ET_MOUSE_EXITED:
444 DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate);
447 class TestOneShotGestureSequenceTimer
448 : public base::OneShotTimer<ui::GestureSequence> {
450 TestOneShotGestureSequenceTimer() {}
452 void ForceTimeout() {
460 DISALLOW_COPY_AND_ASSIGN(TestOneShotGestureSequenceTimer);
463 class TimerTestGestureSequence : public ui::GestureSequence {
465 explicit TimerTestGestureSequence(ui::GestureSequenceDelegate* delegate)
466 : ui::GestureSequence(delegate) {
469 void ForceTimeout() {
470 static_cast<TestOneShotGestureSequenceTimer*>(
471 GetLongPressTimer())->ForceTimeout();
474 bool IsTimerRunning() {
475 return GetLongPressTimer()->IsRunning();
478 virtual base::OneShotTimer<ui::GestureSequence>* CreateTimer() OVERRIDE {
479 return new TestOneShotGestureSequenceTimer();
483 DISALLOW_COPY_AND_ASSIGN(TimerTestGestureSequence);
486 class TestGestureRecognizer : public ui::GestureRecognizerImpl {
488 TestGestureRecognizer() : GestureRecognizerImpl() {
491 ui::GestureSequence* GetGestureSequenceForTesting(Window* window) {
492 return GetGestureSequenceForConsumer(window);
496 DISALLOW_COPY_AND_ASSIGN(TestGestureRecognizer);
499 class TimerTestGestureRecognizer : public TestGestureRecognizer {
501 TimerTestGestureRecognizer() : TestGestureRecognizer() {
504 virtual ui::GestureSequence* CreateSequence(
505 ui::GestureSequenceDelegate* delegate) OVERRIDE {
506 return new TimerTestGestureSequence(delegate);
510 DISALLOW_COPY_AND_ASSIGN(TimerTestGestureRecognizer);
513 base::TimeDelta GetTime() {
514 return ui::EventTimeForNow();
517 class ScopedGestureRecognizerSetter {
519 // Takes ownership of |new_gr|.
520 explicit ScopedGestureRecognizerSetter(ui::GestureRecognizer* new_gr)
522 original_gr_ = ui::GestureRecognizer::Get();
523 ui::SetGestureRecognizerForTesting(new_gr_.get());
526 virtual ~ScopedGestureRecognizerSetter() {
527 ui::SetGestureRecognizerForTesting(original_gr_);
531 ui::GestureRecognizer* original_gr_;
532 scoped_ptr<ui::GestureRecognizer> new_gr_;
534 DISALLOW_COPY_AND_ASSIGN(ScopedGestureRecognizerSetter);
542 // Use a non-zero start time to pass DCHECKs which ensure events have had a
544 TimedEvents() : simulated_now_(1) {
547 base::TimeDelta Now() {
548 base::TimeDelta t = base::TimeDelta::FromMilliseconds(simulated_now_);
553 base::TimeDelta LeapForward(int time_in_millis) {
554 simulated_now_ += time_in_millis;
555 return base::TimeDelta::FromMilliseconds(simulated_now_);
558 base::TimeDelta InFuture(int time_in_millis) {
559 return base::TimeDelta::FromMilliseconds(simulated_now_ + time_in_millis);
562 void SendScrollEvents(ui::EventProcessor* dispatcher,
570 GestureEventConsumeDelegate* delegate) {
574 for (int i = 0; i < num_steps; i++) {
577 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
579 base::TimeDelta::FromMilliseconds(simulated_now_));
580 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
581 ASSERT_FALSE(details.dispatcher_destroyed);
582 simulated_now_ += time_step;
586 void SendScrollEvent(ui::EventProcessor* dispatcher,
590 GestureEventConsumeDelegate* delegate) {
592 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
594 base::TimeDelta::FromMilliseconds(simulated_now_));
595 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
596 ASSERT_FALSE(details.dispatcher_destroyed);
601 // An event handler to keep track of events.
602 class TestEventHandler : public ui::EventHandler {
604 TestEventHandler() : touch_released_count_(0),
605 touch_pressed_count_(0),
606 touch_moved_count_(0),
607 touch_cancelled_count_(0) {
610 virtual ~TestEventHandler() {}
612 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
613 switch (event->type()) {
614 case ui::ET_TOUCH_RELEASED:
615 touch_released_count_++;
617 case ui::ET_TOUCH_PRESSED:
618 touch_pressed_count_++;
620 case ui::ET_TOUCH_MOVED:
621 touch_moved_count_++;
623 case ui::ET_TOUCH_CANCELLED:
624 touch_cancelled_count_++;
632 touch_released_count_ = 0;
633 touch_pressed_count_ = 0;
634 touch_moved_count_ = 0;
635 touch_cancelled_count_ = 0;
638 int touch_released_count() const { return touch_released_count_; }
639 int touch_pressed_count() const { return touch_pressed_count_; }
640 int touch_moved_count() const { return touch_moved_count_; }
641 int touch_cancelled_count() const { return touch_cancelled_count_; }
644 int touch_released_count_;
645 int touch_pressed_count_;
646 int touch_moved_count_;
647 int touch_cancelled_count_;
649 DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
652 // Removes the target window from its parent when it receives a touch-cancel
654 class RemoveOnTouchCancelHandler : public TestEventHandler {
656 RemoveOnTouchCancelHandler() {}
657 virtual ~RemoveOnTouchCancelHandler() {}
661 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
662 TestEventHandler::OnTouchEvent(event);
663 if (event->type() == ui::ET_TOUCH_CANCELLED) {
664 Window* target = static_cast<Window*>(event->target());
665 // This is tiptoeing around crbug.com/310172. If this event handler isn't
666 // removed, we enter an infinite loop.
667 target->RemovePreTargetHandler(this);
668 target->parent()->RemoveChild(target);
672 DISALLOW_COPY_AND_ASSIGN(RemoveOnTouchCancelHandler);
677 class GestureRecognizerTest : public AuraTestBase,
678 public ::testing::WithParamInterface<bool> {
680 GestureRecognizerTest() {}
682 bool UsingUnifiedGR() {
686 virtual void SetUp() OVERRIDE {
687 // TODO(tdresser): Once unified GR has landed, only run these tests once.
688 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
689 switches::kUnifiedGestureDetector,
690 UsingUnifiedGR() ? switches::kUnifiedGestureDetectorEnabled
691 : switches::kUnifiedGestureDetectorDisabled);
693 AuraTestBase::SetUp();
694 ui::GestureConfiguration::set_min_touch_down_duration_in_seconds_for_click(
696 ui::GestureConfiguration::set_show_press_delay_in_ms(2);
697 ui::GestureConfiguration::set_long_press_time_in_seconds(0.003);
700 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerTest);
703 // Check that appropriate touch events generate tap gesture events.
704 TEST_P(GestureRecognizerTest, GestureEventTap) {
705 scoped_ptr<GestureEventConsumeDelegate> delegate(
706 new GestureEventConsumeDelegate());
708 const int kWindowWidth = 123;
709 const int kWindowHeight = 45;
710 const int kTouchId = 2;
711 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
712 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
713 delegate.get(), -1234, bounds, root_window()));
716 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
717 kTouchId, tes.Now());
718 DispatchEventUsingWindowDispatcher(&press);
719 EXPECT_FALSE(delegate->tap());
720 EXPECT_FALSE(delegate->show_press());
721 EXPECT_TRUE(delegate->tap_down());
722 EXPECT_FALSE(delegate->tap_cancel());
723 EXPECT_TRUE(delegate->begin());
724 EXPECT_FALSE(delegate->scroll_begin());
725 EXPECT_FALSE(delegate->scroll_update());
726 EXPECT_FALSE(delegate->scroll_end());
727 EXPECT_FALSE(delegate->long_press());
730 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
731 EXPECT_TRUE(delegate->show_press());
732 EXPECT_FALSE(delegate->tap_down());
734 // Make sure there is enough delay before the touch is released so that it is
735 // recognized as a tap.
737 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
738 kTouchId, tes.LeapForward(50));
740 DispatchEventUsingWindowDispatcher(&release);
741 EXPECT_TRUE(delegate->tap());
742 EXPECT_FALSE(delegate->tap_down());
743 EXPECT_FALSE(delegate->tap_cancel());
744 EXPECT_FALSE(delegate->begin());
745 EXPECT_TRUE(delegate->end());
746 EXPECT_FALSE(delegate->scroll_begin());
747 EXPECT_FALSE(delegate->scroll_update());
748 EXPECT_FALSE(delegate->scroll_end());
750 EXPECT_EQ(1, delegate->tap_count());
753 // Check that appropriate touch events generate tap gesture events
754 // when information about the touch radii are provided.
755 TEST_P(GestureRecognizerTest, GestureEventTapRegion) {
756 // TODO(tdresser): enable this test with unified GR once we resolve the
757 // bounding box differences. See crbug.com/366641.
758 if (UsingUnifiedGR())
761 scoped_ptr<GestureEventConsumeDelegate> delegate(
762 new GestureEventConsumeDelegate());
764 const int kWindowWidth = 800;
765 const int kWindowHeight = 600;
766 const int kTouchId = 2;
767 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
768 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
769 delegate.get(), -1234, bounds, root_window()));
771 // Test with no ET_TOUCH_MOVED events.
774 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
775 kTouchId, tes.Now());
776 press.set_radius_x(5);
777 press.set_radius_y(12);
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 // Make sure there is enough delay before the touch is released so that it
789 // is recognized as a tap.
791 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
792 kTouchId, tes.LeapForward(50));
793 release.set_radius_x(5);
794 release.set_radius_y(12);
796 DispatchEventUsingWindowDispatcher(&release);
797 EXPECT_TRUE(delegate->tap());
798 EXPECT_FALSE(delegate->tap_down());
799 EXPECT_FALSE(delegate->tap_cancel());
800 EXPECT_FALSE(delegate->begin());
801 EXPECT_TRUE(delegate->end());
802 EXPECT_FALSE(delegate->scroll_begin());
803 EXPECT_FALSE(delegate->scroll_update());
804 EXPECT_FALSE(delegate->scroll_end());
806 EXPECT_EQ(1, delegate->tap_count());
807 gfx::Point actual_point(delegate->tap_location());
808 EXPECT_EQ(24, delegate->bounding_box().width());
809 EXPECT_EQ(24, delegate->bounding_box().height());
810 EXPECT_EQ(101, actual_point.x());
811 EXPECT_EQ(201, actual_point.y());
814 // Test with no ET_TOUCH_MOVED events but different touch points and radii.
817 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(365, 290),
818 kTouchId, tes.Now());
819 press.set_radius_x(8);
820 press.set_radius_y(14);
821 DispatchEventUsingWindowDispatcher(&press);
822 EXPECT_FALSE(delegate->tap());
823 EXPECT_TRUE(delegate->tap_down());
824 EXPECT_FALSE(delegate->tap_cancel());
825 EXPECT_TRUE(delegate->begin());
826 EXPECT_FALSE(delegate->scroll_begin());
827 EXPECT_FALSE(delegate->scroll_update());
828 EXPECT_FALSE(delegate->scroll_end());
829 EXPECT_FALSE(delegate->long_press());
832 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(367, 291),
833 kTouchId, tes.LeapForward(50));
834 release.set_radius_x(20);
835 release.set_radius_y(13);
837 DispatchEventUsingWindowDispatcher(&release);
838 EXPECT_TRUE(delegate->tap());
839 EXPECT_FALSE(delegate->tap_down());
840 EXPECT_FALSE(delegate->tap_cancel());
841 EXPECT_FALSE(delegate->begin());
842 EXPECT_TRUE(delegate->end());
843 EXPECT_FALSE(delegate->scroll_begin());
844 EXPECT_FALSE(delegate->scroll_update());
845 EXPECT_FALSE(delegate->scroll_end());
847 EXPECT_EQ(1, delegate->tap_count());
848 gfx::Point actual_point(delegate->tap_location());
849 EXPECT_EQ(40, delegate->bounding_box().width());
850 EXPECT_EQ(40, delegate->bounding_box().height());
851 EXPECT_EQ(367, actual_point.x());
852 EXPECT_EQ(291, actual_point.y());
855 // Test with a single ET_TOUCH_MOVED event.
858 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(46, 205),
859 kTouchId, tes.Now());
860 press.set_radius_x(6);
861 press.set_radius_y(10);
862 DispatchEventUsingWindowDispatcher(&press);
863 EXPECT_FALSE(delegate->tap());
864 EXPECT_TRUE(delegate->tap_down());
865 EXPECT_FALSE(delegate->tap_cancel());
866 EXPECT_TRUE(delegate->begin());
867 EXPECT_FALSE(delegate->tap_cancel());
868 EXPECT_FALSE(delegate->scroll_begin());
869 EXPECT_FALSE(delegate->scroll_update());
870 EXPECT_FALSE(delegate->scroll_end());
871 EXPECT_FALSE(delegate->long_press());
874 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(49, 204),
875 kTouchId, tes.LeapForward(50));
876 move.set_radius_x(8);
877 move.set_radius_y(12);
878 DispatchEventUsingWindowDispatcher(&move);
879 EXPECT_FALSE(delegate->tap());
880 EXPECT_FALSE(delegate->tap_down());
881 EXPECT_FALSE(delegate->tap_cancel());
882 EXPECT_FALSE(delegate->begin());
883 EXPECT_FALSE(delegate->scroll_begin());
884 EXPECT_FALSE(delegate->scroll_update());
885 EXPECT_FALSE(delegate->scroll_end());
886 EXPECT_FALSE(delegate->long_press());
889 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(49, 204),
890 kTouchId, tes.LeapForward(50));
891 release.set_radius_x(4);
892 release.set_radius_y(8);
894 DispatchEventUsingWindowDispatcher(&release);
895 EXPECT_TRUE(delegate->tap());
896 EXPECT_FALSE(delegate->tap_down());
897 EXPECT_FALSE(delegate->tap_cancel());
898 EXPECT_FALSE(delegate->begin());
899 EXPECT_TRUE(delegate->end());
900 EXPECT_FALSE(delegate->scroll_begin());
901 EXPECT_FALSE(delegate->scroll_update());
902 EXPECT_FALSE(delegate->scroll_end());
904 EXPECT_EQ(1, delegate->tap_count());
905 gfx::Point actual_point(delegate->tap_location());
906 EXPECT_EQ(25, delegate->bounding_box().width());
907 EXPECT_EQ(24, delegate->bounding_box().height());
908 EXPECT_EQ(48, actual_point.x());
909 EXPECT_EQ(204, actual_point.y());
912 // Test with a few ET_TOUCH_MOVED events.
915 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(400, 150),
916 kTouchId, tes.Now());
917 press.set_radius_x(7);
918 press.set_radius_y(10);
919 DispatchEventUsingWindowDispatcher(&press);
920 EXPECT_FALSE(delegate->tap());
921 EXPECT_TRUE(delegate->tap_down());
922 EXPECT_FALSE(delegate->tap_cancel());
923 EXPECT_TRUE(delegate->begin());
924 EXPECT_FALSE(delegate->scroll_begin());
925 EXPECT_FALSE(delegate->scroll_update());
926 EXPECT_FALSE(delegate->scroll_end());
927 EXPECT_FALSE(delegate->long_press());
930 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(397, 151),
931 kTouchId, tes.LeapForward(50));
932 move.set_radius_x(13);
933 move.set_radius_y(12);
934 DispatchEventUsingWindowDispatcher(&move);
935 EXPECT_FALSE(delegate->tap());
936 EXPECT_FALSE(delegate->tap_down());
937 EXPECT_FALSE(delegate->tap_cancel());
938 EXPECT_FALSE(delegate->begin());
939 EXPECT_FALSE(delegate->scroll_begin());
940 EXPECT_FALSE(delegate->scroll_update());
941 EXPECT_FALSE(delegate->scroll_end());
942 EXPECT_FALSE(delegate->long_press());
945 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(397, 149),
946 kTouchId, tes.LeapForward(50));
947 move1.set_radius_x(16);
948 move1.set_radius_y(16);
949 DispatchEventUsingWindowDispatcher(&move1);
950 EXPECT_FALSE(delegate->tap());
951 EXPECT_FALSE(delegate->tap_down());
952 EXPECT_FALSE(delegate->tap_cancel());
953 EXPECT_FALSE(delegate->begin());
954 EXPECT_FALSE(delegate->scroll_begin());
955 EXPECT_FALSE(delegate->scroll_update());
956 EXPECT_FALSE(delegate->scroll_end());
957 EXPECT_FALSE(delegate->long_press());
960 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(400, 150),
961 kTouchId, tes.LeapForward(50));
962 move2.set_radius_x(14);
963 move2.set_radius_y(10);
964 DispatchEventUsingWindowDispatcher(&move2);
965 EXPECT_FALSE(delegate->tap());
966 EXPECT_FALSE(delegate->tap_down());
967 EXPECT_FALSE(delegate->tap_cancel());
968 EXPECT_FALSE(delegate->begin());
969 EXPECT_FALSE(delegate->scroll_begin());
970 EXPECT_FALSE(delegate->scroll_update());
971 EXPECT_FALSE(delegate->scroll_end());
972 EXPECT_FALSE(delegate->long_press());
975 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(401, 149),
976 kTouchId, tes.LeapForward(50));
977 release.set_radius_x(8);
978 release.set_radius_y(9);
980 DispatchEventUsingWindowDispatcher(&release);
981 EXPECT_TRUE(delegate->tap());
982 EXPECT_FALSE(delegate->tap_down());
983 EXPECT_FALSE(delegate->tap_cancel());
984 EXPECT_FALSE(delegate->begin());
985 EXPECT_TRUE(delegate->end());
986 EXPECT_FALSE(delegate->scroll_begin());
987 EXPECT_FALSE(delegate->scroll_update());
988 EXPECT_FALSE(delegate->scroll_end());
990 EXPECT_EQ(1, delegate->tap_count());
991 gfx::Point actual_point(delegate->tap_location());
992 EXPECT_EQ(33, delegate->bounding_box().width());
993 EXPECT_EQ(32, delegate->bounding_box().height());
994 EXPECT_EQ(397, actual_point.x());
995 EXPECT_EQ(149, actual_point.y());
999 // Check that appropriate touch events generate scroll gesture events.
1000 TEST_P(GestureRecognizerTest, GestureEventScroll) {
1001 // We'll start by moving the touch point by (10.5, 10.5). We want 5 dips of
1002 // that distance to be consumed by the slop, so we set the slop radius to
1003 // sqrt(5 * 5 + 5 * 5).
1004 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(
1005 sqrt(static_cast<double>(5 * 5 + 5 * 5)));
1006 scoped_ptr<GestureEventConsumeDelegate> delegate(
1007 new GestureEventConsumeDelegate());
1009 const int kWindowWidth = 123;
1010 const int kWindowHeight = 45;
1011 const int kTouchId = 5;
1012 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1013 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1014 delegate.get(), -1234, bounds, root_window()));
1017 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1018 kTouchId, tes.Now());
1019 DispatchEventUsingWindowDispatcher(&press);
1020 EXPECT_2_EVENTS(delegate->events(),
1021 ui::ET_GESTURE_BEGIN,
1022 ui::ET_GESTURE_TAP_DOWN);
1024 // Move the touch-point enough so that it is considered as a scroll. This
1025 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1026 // The first movement is diagonal, to ensure that we have a free scroll,
1027 // and not a rail scroll.
1028 tes.SendScrollEvent(event_processor(), 111.5, 211.5, kTouchId,
1030 EXPECT_3_EVENTS(delegate->events(),
1031 ui::ET_GESTURE_TAP_CANCEL,
1032 ui::ET_GESTURE_SCROLL_BEGIN,
1033 ui::ET_GESTURE_SCROLL_UPDATE);
1034 // The slop consumed 5 dips
1035 EXPECT_FLOAT_EQ(5.5, delegate->scroll_x());
1036 EXPECT_FLOAT_EQ(5.5, delegate->scroll_y());
1037 EXPECT_EQ(gfx::Point(1, 1).ToString(),
1038 delegate->scroll_begin_position().ToString());
1040 // When scrolling with a single finger, the bounding box of the gesture should
1041 // be empty, since it's a single point and the radius for testing is zero.
1042 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1044 // Move some more to generate a few more scroll updates. Make sure that we get
1045 // out of the snap channel for the unified GR.
1046 tes.SendScrollEvent(event_processor(), 20, 120, kTouchId, delegate.get());
1047 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1048 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_x());
1049 EXPECT_FLOAT_EQ(-91.5, delegate->scroll_y());
1050 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1052 tes.SendScrollEvent(event_processor(), 50, 124, kTouchId, delegate.get());
1053 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1054 EXPECT_EQ(30, delegate->scroll_x());
1055 EXPECT_EQ(4, delegate->scroll_y());
1056 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1058 // Release the touch. This should end the scroll.
1060 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1062 tes.LeapForward(50));
1063 DispatchEventUsingWindowDispatcher(&release);
1064 EXPECT_2_EVENTS(delegate->events(),
1065 ui::ET_SCROLL_FLING_START,
1066 ui::ET_GESTURE_END);
1067 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1070 // Check that predicted scroll update positions are correct.
1071 TEST_P(GestureRecognizerTest, GestureEventScrollPrediction) {
1072 const double prediction_interval = 0.03;
1073 ui::GestureConfiguration::set_scroll_prediction_seconds(prediction_interval);
1074 // We'll start by moving the touch point by (5, 5). We want all of that
1075 // distance to be consumed by the slop, so we set the slop radius to
1076 // sqrt(5 * 5 + 5 * 5).
1077 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(
1078 sqrt(static_cast<double>(5 * 5 + 5 * 5)));
1080 scoped_ptr<GestureEventConsumeDelegate> delegate(
1081 new GestureEventConsumeDelegate());
1083 const int kWindowWidth = 123;
1084 const int kWindowHeight = 45;
1085 const int kTouchId = 5;
1086 gfx::Rect bounds(95, 195, kWindowWidth, kWindowHeight);
1087 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1088 delegate.get(), -1234, bounds, root_window()));
1091 // Tracks the total scroll since we want to verify that the correct position
1092 // will be scrolled to throughout the prediction.
1093 gfx::Vector2dF total_scroll;
1094 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(96, 196),
1095 kTouchId, tes.Now());
1096 DispatchEventUsingWindowDispatcher(&press);
1097 EXPECT_2_EVENTS(delegate->events(),
1098 ui::ET_GESTURE_BEGIN,
1099 ui::ET_GESTURE_TAP_DOWN);
1102 // Get rid of touch slop.
1103 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(111, 211),
1104 kTouchId, tes.Now());
1105 DispatchEventUsingWindowDispatcher(&move);
1106 EXPECT_3_EVENTS(delegate->events(),
1107 ui::ET_GESTURE_TAP_CANCEL,
1108 ui::ET_GESTURE_SCROLL_BEGIN,
1109 ui::ET_GESTURE_SCROLL_UPDATE);
1110 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1111 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1113 // Move the touch-point enough so that it is considered as a scroll. This
1114 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1115 // The first movement is diagonal, to ensure that we have a free scroll,
1116 // and not a rail scroll.
1117 tes.LeapForward(30);
1118 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1119 EXPECT_1_EVENT(delegate->events(),
1120 ui::ET_GESTURE_SCROLL_UPDATE);
1121 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1122 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1124 // Move some more to generate a few more scroll updates.
1125 tes.LeapForward(30);
1126 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
1127 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1128 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1129 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1131 tes.LeapForward(30);
1132 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
1133 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1134 total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1135 total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1137 // Release the touch. This should end the scroll.
1139 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1141 tes.LeapForward(50));
1142 DispatchEventUsingWindowDispatcher(&release);
1145 // Check that the bounding box during a scroll event is correct.
1146 TEST_P(GestureRecognizerTest, GestureEventScrollBoundingBox) {
1148 for (int radius = 1; radius <= 10; ++radius) {
1149 ui::GestureConfiguration::set_default_radius(radius);
1150 scoped_ptr<GestureEventConsumeDelegate> delegate(
1151 new GestureEventConsumeDelegate());
1152 const int kWindowWidth = 123;
1153 const int kWindowHeight = 45;
1154 const int kTouchId = 5;
1155 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1156 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1157 delegate.get(), -1234, bounds, root_window()));
1159 const int kPositionX = 101;
1160 const int kPositionY = 201;
1162 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
1163 gfx::Point(kPositionX, kPositionY),
1166 DispatchEventUsingWindowDispatcher(&press);
1167 EXPECT_EQ(gfx::Rect(kPositionX - radius,
1168 kPositionY - radius,
1170 radius * 2).ToString(),
1171 delegate->bounding_box().ToString());
1173 const int kScrollAmount = 50;
1174 tes.SendScrollEvents(event_processor(), kPositionX, kPositionY,
1175 1, 1, kTouchId, 1, kScrollAmount, delegate.get());
1176 EXPECT_EQ(gfx::Point(1, 1).ToString(),
1177 delegate->scroll_begin_position().ToString());
1178 EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius,
1179 kPositionY + kScrollAmount - radius,
1181 radius * 2).ToString(),
1182 delegate->bounding_box().ToString());
1184 // Release the touch. This should end the scroll.
1186 ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
1187 gfx::Point(kPositionX + kScrollAmount,
1188 kPositionY + kScrollAmount),
1189 kTouchId, press.time_stamp() +
1190 base::TimeDelta::FromMilliseconds(50));
1191 DispatchEventUsingWindowDispatcher(&release);
1192 EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius,
1193 kPositionY + kScrollAmount - radius,
1195 radius * 2).ToString(),
1196 delegate->bounding_box().ToString());
1198 ui::GestureConfiguration::set_default_radius(0);
1201 // Check Scroll End Events report correct velocities
1202 // if the user was on a horizontal rail
1203 TEST_P(GestureRecognizerTest, GestureEventHorizontalRailFling) {
1204 scoped_ptr<GestureEventConsumeDelegate> delegate(
1205 new GestureEventConsumeDelegate());
1207 const int kTouchId = 7;
1208 gfx::Rect bounds(0, 0, 1000, 1000);
1209 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1210 delegate.get(), -1234, bounds, root_window()));
1212 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1213 kTouchId, tes.Now());
1214 DispatchEventUsingWindowDispatcher(&press);
1216 // Get rid of touch slop.
1217 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 0),
1218 kTouchId, tes.Now());
1219 DispatchEventUsingWindowDispatcher(&move);
1223 // Move the touch-point horizontally enough that it is considered a
1224 // horizontal scroll.
1225 tes.SendScrollEvent(event_processor(), 30, 1, kTouchId, delegate.get());
1226 EXPECT_FLOAT_EQ(0, delegate->scroll_y());
1227 EXPECT_FLOAT_EQ(20, delegate->scroll_x());
1229 // Get a high x velocity, while still staying on the rail
1230 tes.SendScrollEvents(event_processor(), 1, 1,
1231 100, 10, kTouchId, 1,
1232 ui::GestureConfiguration::points_buffered_for_velocity(),
1236 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1237 kTouchId, tes.Now());
1238 DispatchEventUsingWindowDispatcher(&release);
1240 EXPECT_TRUE(delegate->fling());
1241 EXPECT_FALSE(delegate->scroll_end());
1242 EXPECT_GT(delegate->velocity_x(), 0);
1243 EXPECT_EQ(0, delegate->velocity_y());
1246 // Check Scroll End Events report correct velocities
1247 // if the user was on a vertical rail
1248 TEST_P(GestureRecognizerTest, GestureEventVerticalRailFling) {
1249 scoped_ptr<GestureEventConsumeDelegate> delegate(
1250 new GestureEventConsumeDelegate());
1252 const int kTouchId = 7;
1253 gfx::Rect bounds(0, 0, 1000, 1000);
1254 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1255 delegate.get(), -1234, bounds, root_window()));
1257 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1258 kTouchId, tes.Now());
1259 DispatchEventUsingWindowDispatcher(&press);
1261 // Get rid of touch slop.
1262 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 10),
1263 kTouchId, tes.Now());
1264 DispatchEventUsingWindowDispatcher(&move);
1267 // Move the touch-point vertically enough that it is considered a
1269 tes.SendScrollEvent(event_processor(), 1, 30, kTouchId, delegate.get());
1270 EXPECT_EQ(20, delegate->scroll_y());
1271 EXPECT_EQ(0, delegate->scroll_x());
1272 EXPECT_EQ(0, delegate->scroll_velocity_x());
1274 // Get a high y velocity, while still staying on the rail
1275 tes.SendScrollEvents(event_processor(), 1, 6,
1276 10, 100, kTouchId, 1,
1277 ui::GestureConfiguration::points_buffered_for_velocity(),
1279 EXPECT_EQ(0, delegate->scroll_velocity_x());
1282 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 206),
1283 kTouchId, tes.Now());
1284 DispatchEventUsingWindowDispatcher(&release);
1286 EXPECT_TRUE(delegate->fling());
1287 EXPECT_FALSE(delegate->scroll_end());
1288 EXPECT_EQ(0, delegate->velocity_x());
1289 EXPECT_GT(delegate->velocity_y(), 0);
1292 // Check Scroll End Events report non-zero velocities if the user is not on a
1294 TEST_P(GestureRecognizerTest, GestureEventNonRailFling) {
1295 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(0);
1296 scoped_ptr<GestureEventConsumeDelegate> delegate(
1297 new GestureEventConsumeDelegate());
1299 const int kTouchId = 7;
1300 gfx::Rect bounds(0, 0, 1000, 1000);
1301 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1302 delegate.get(), -1234, bounds, root_window()));
1304 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1305 kTouchId, tes.Now());
1306 DispatchEventUsingWindowDispatcher(&press);
1308 // Move the touch-point such that a non-rail scroll begins, and we're outside
1309 // the snap channel for the unified GR.
1310 tes.SendScrollEvent(event_processor(), 50, 50, kTouchId, delegate.get());
1311 EXPECT_EQ(50, delegate->scroll_y());
1312 EXPECT_EQ(50, delegate->scroll_x());
1314 tes.SendScrollEvents(event_processor(), 1, 1,
1315 10, 100, kTouchId, 1,
1316 ui::GestureConfiguration::points_buffered_for_velocity(),
1320 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1321 kTouchId, tes.Now());
1322 DispatchEventUsingWindowDispatcher(&release);
1324 EXPECT_TRUE(delegate->fling());
1325 EXPECT_FALSE(delegate->scroll_end());
1326 EXPECT_GT(delegate->velocity_x(), 0);
1327 EXPECT_GT(delegate->velocity_y(), 0);
1330 // Check that appropriate touch events generate long press events
1331 TEST_P(GestureRecognizerTest, GestureEventLongPress) {
1332 ui::GestureConfiguration::set_max_touch_down_duration_in_seconds_for_click(
1335 scoped_ptr<GestureEventConsumeDelegate> delegate(
1336 new GestureEventConsumeDelegate());
1337 const int kWindowWidth = 123;
1338 const int kWindowHeight = 45;
1339 const int kTouchId = 2;
1340 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1341 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1342 delegate.get(), -1234, bounds, root_window()));
1346 TimerTestGestureRecognizer* gesture_recognizer =
1347 new TimerTestGestureRecognizer();
1349 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
1351 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1352 gfx::Point(101, 201),
1354 ui::EventTimeForNow());
1355 DispatchEventUsingWindowDispatcher(&press1);
1356 EXPECT_TRUE(delegate->tap_down());
1357 EXPECT_TRUE(delegate->begin());
1358 EXPECT_FALSE(delegate->tap_cancel());
1360 // We haven't pressed long enough for a long press to occur
1361 EXPECT_FALSE(delegate->long_press());
1363 // Wait until the timer runs out
1364 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1365 EXPECT_TRUE(delegate->long_press());
1366 EXPECT_FALSE(delegate->tap_cancel());
1369 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1370 gfx::Point(101, 201),
1372 ui::EventTimeForNow());
1373 DispatchEventUsingWindowDispatcher(&release1);
1374 EXPECT_FALSE(delegate->long_press());
1376 // Note the tap cancel isn't dispatched until the release
1377 EXPECT_TRUE(delegate->tap_cancel());
1378 EXPECT_FALSE(delegate->tap());
1381 // Check that scrolling cancels a long press
1382 TEST_P(GestureRecognizerTest, GestureEventLongPressCancelledByScroll) {
1383 scoped_ptr<GestureEventConsumeDelegate> delegate(
1384 new GestureEventConsumeDelegate());
1386 const int kWindowWidth = 123;
1387 const int kWindowHeight = 45;
1388 const int kTouchId = 6;
1389 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1390 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1391 delegate.get(), -1234, bounds, root_window()));
1395 TimerTestGestureRecognizer* gesture_recognizer =
1396 new TimerTestGestureRecognizer();
1397 TimerTestGestureSequence* gesture_sequence =
1398 static_cast<TimerTestGestureSequence*>(
1399 gesture_recognizer->GetGestureSequenceForTesting(window.get()));
1401 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
1403 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1404 kTouchId, tes.Now());
1405 DispatchEventUsingWindowDispatcher(&press1);
1406 EXPECT_TRUE(delegate->tap_down());
1408 // We haven't pressed long enough for a long press to occur
1409 EXPECT_FALSE(delegate->long_press());
1410 EXPECT_FALSE(delegate->tap_cancel());
1412 // Scroll around, to cancel the long press
1413 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1414 // Wait until the timer runs out
1415 gesture_sequence->ForceTimeout();
1416 EXPECT_FALSE(delegate->long_press());
1417 EXPECT_TRUE(delegate->tap_cancel());
1420 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1421 kTouchId, tes.LeapForward(10));
1422 DispatchEventUsingWindowDispatcher(&release1);
1423 EXPECT_FALSE(delegate->long_press());
1424 EXPECT_FALSE(delegate->tap_cancel());
1427 // Check that appropriate touch events generate long tap events
1428 TEST_P(GestureRecognizerTest, GestureEventLongTap) {
1429 ui::GestureConfiguration::set_max_touch_down_duration_in_seconds_for_click(
1431 scoped_ptr<GestureEventConsumeDelegate> delegate(
1432 new GestureEventConsumeDelegate());
1433 const int kWindowWidth = 123;
1434 const int kWindowHeight = 45;
1435 const int kTouchId = 2;
1436 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1437 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1438 delegate.get(), -1234, bounds, root_window()));
1442 TimerTestGestureRecognizer* gesture_recognizer =
1443 new TimerTestGestureRecognizer();
1445 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
1447 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1448 gfx::Point(101, 201),
1450 ui::EventTimeForNow());
1451 DispatchEventUsingWindowDispatcher(&press1);
1452 EXPECT_TRUE(delegate->tap_down());
1453 EXPECT_TRUE(delegate->begin());
1454 EXPECT_FALSE(delegate->tap_cancel());
1456 // We haven't pressed long enough for a long press to occur
1457 EXPECT_FALSE(delegate->long_press());
1459 // Wait until the timer runs out
1460 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1461 EXPECT_TRUE(delegate->long_press());
1462 EXPECT_FALSE(delegate->tap_cancel());
1465 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1466 gfx::Point(101, 201),
1468 ui::EventTimeForNow());
1469 DispatchEventUsingWindowDispatcher(&release1);
1470 EXPECT_FALSE(delegate->long_press());
1471 EXPECT_TRUE(delegate->long_tap());
1473 // Note the tap cancel isn't dispatched until the release
1474 EXPECT_TRUE(delegate->tap_cancel());
1475 EXPECT_FALSE(delegate->tap());
1478 // Check that second tap cancels a long press
1479 TEST_P(GestureRecognizerTest, GestureEventLongPressCancelledBySecondTap) {
1480 scoped_ptr<GestureEventConsumeDelegate> delegate(
1481 new GestureEventConsumeDelegate());
1483 const int kWindowWidth = 300;
1484 const int kWindowHeight = 400;
1485 const int kTouchId1 = 8;
1486 const int kTouchId2 = 2;
1487 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1488 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1489 delegate.get(), -1234, bounds, root_window()));
1491 TimerTestGestureRecognizer* gesture_recognizer =
1492 new TimerTestGestureRecognizer();
1493 TimerTestGestureSequence* gesture_sequence =
1494 static_cast<TimerTestGestureSequence*>(
1495 gesture_recognizer->GetGestureSequenceForTesting(window.get()));
1497 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
1500 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1501 kTouchId1, tes.Now());
1502 DispatchEventUsingWindowDispatcher(&press);
1503 EXPECT_TRUE(delegate->tap_down());
1504 EXPECT_TRUE(delegate->begin());
1506 // We haven't pressed long enough for a long press to occur
1507 EXPECT_FALSE(delegate->long_press());
1509 // Second tap, to cancel the long press
1511 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1512 kTouchId2, tes.Now());
1513 DispatchEventUsingWindowDispatcher(&press2);
1514 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
1515 EXPECT_TRUE(delegate->tap_cancel());
1516 EXPECT_TRUE(delegate->begin());
1518 // Wait until the timer runs out
1519 gesture_sequence->ForceTimeout();
1521 // No long press occurred
1522 EXPECT_FALSE(delegate->long_press());
1525 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1526 kTouchId1, tes.Now());
1527 DispatchEventUsingWindowDispatcher(&release1);
1528 EXPECT_FALSE(delegate->long_press());
1529 EXPECT_TRUE(delegate->two_finger_tap());
1530 EXPECT_FALSE(delegate->tap_cancel());
1533 // Check that horizontal scroll gestures cause scrolls on horizontal rails.
1534 // Also tests that horizontal rails can be broken.
1535 TEST_P(GestureRecognizerTest, GestureEventHorizontalRailScroll) {
1536 scoped_ptr<GestureEventConsumeDelegate> delegate(
1537 new GestureEventConsumeDelegate());
1539 const int kTouchId = 7;
1540 gfx::Rect bounds(0, 0, 1000, 1000);
1541 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1542 delegate.get(), -1234, bounds, root_window()));
1544 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1545 kTouchId, tes.Now());
1546 DispatchEventUsingWindowDispatcher(&press);
1548 // Get rid of touch slop.
1549 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(5, 0),
1550 kTouchId, tes.Now());
1552 DispatchEventUsingWindowDispatcher(&move);
1555 // Move the touch-point horizontally enough that it is considered a
1556 // horizontal scroll.
1557 tes.SendScrollEvent(event_processor(), 25, 0, kTouchId, delegate.get());
1558 EXPECT_EQ(0, delegate->scroll_y());
1559 EXPECT_EQ(20, delegate->scroll_x());
1561 tes.SendScrollEvent(event_processor(), 30, 6, kTouchId, delegate.get());
1562 EXPECT_TRUE(delegate->scroll_update());
1563 EXPECT_EQ(5, delegate->scroll_x());
1564 // y shouldn't change, as we're on a horizontal rail.
1565 EXPECT_EQ(0, delegate->scroll_y());
1567 // Send enough information that a velocity can be calculated for the gesture,
1568 // and we can break the rail
1569 tes.SendScrollEvents(event_processor(), 1, 1,
1570 6, 100, kTouchId, 1,
1571 ui::GestureConfiguration::points_buffered_for_velocity(),
1574 tes.SendScrollEvent(event_processor(), 5, 0, kTouchId, delegate.get());
1575 tes.SendScrollEvent(event_processor(), 10, 5, kTouchId, delegate.get());
1577 // The rail should be broken
1578 EXPECT_TRUE(delegate->scroll_update());
1579 EXPECT_EQ(5, delegate->scroll_x());
1580 EXPECT_EQ(5, delegate->scroll_y());
1583 // Check that vertical scroll gestures cause scrolls on vertical rails.
1584 // Also tests that vertical rails can be broken.
1585 TEST_P(GestureRecognizerTest, GestureEventVerticalRailScroll) {
1586 scoped_ptr<GestureEventConsumeDelegate> delegate(
1587 new GestureEventConsumeDelegate());
1589 const int kTouchId = 7;
1590 gfx::Rect bounds(0, 0, 1000, 1000);
1591 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1592 delegate.get(), -1234, bounds, root_window()));
1594 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1595 kTouchId, tes.Now());
1596 DispatchEventUsingWindowDispatcher(&press);
1598 // Get rid of touch slop.
1599 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 5),
1600 kTouchId, tes.Now());
1601 DispatchEventUsingWindowDispatcher(&move);
1604 // Move the touch-point vertically enough that it is considered a
1606 tes.SendScrollEvent(event_processor(), 0, 25, kTouchId, delegate.get());
1607 EXPECT_EQ(0, delegate->scroll_x());
1608 EXPECT_EQ(20, delegate->scroll_y());
1610 tes.SendScrollEvent(event_processor(), 6, 30, kTouchId, delegate.get());
1611 EXPECT_TRUE(delegate->scroll_update());
1612 EXPECT_EQ(5, delegate->scroll_y());
1613 // x shouldn't change, as we're on a vertical rail.
1614 EXPECT_EQ(0, delegate->scroll_x());
1615 EXPECT_EQ(0, delegate->scroll_velocity_x());
1617 // Send enough information that a velocity can be calculated for the gesture,
1618 // and we can break the rail
1619 tes.SendScrollEvents(event_processor(), 1, 6,
1620 100, 1, kTouchId, 1,
1621 ui::GestureConfiguration::points_buffered_for_velocity(),
1624 tes.SendScrollEvent(event_processor(), 0, 5, kTouchId, delegate.get());
1625 tes.SendScrollEvent(event_processor(), 5, 10, kTouchId, delegate.get());
1627 // The rail should be broken
1628 EXPECT_TRUE(delegate->scroll_update());
1629 EXPECT_EQ(5, delegate->scroll_x());
1630 EXPECT_EQ(5, delegate->scroll_y());
1633 TEST_P(GestureRecognizerTest, GestureTapFollowedByScroll) {
1634 // We'll start by moving the touch point by (5, 5). We want all of that
1635 // distance to be consumed by the slop, so we set the slop radius to
1636 // sqrt(5 * 5 + 5 * 5).
1637 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(
1638 sqrt(static_cast<double>(5 * 5 + 5 * 5)));
1640 // First, tap. Then, do a scroll using the same touch-id.
1641 scoped_ptr<GestureEventConsumeDelegate> delegate(
1642 new GestureEventConsumeDelegate());
1644 const int kWindowWidth = 123;
1645 const int kWindowHeight = 45;
1646 const int kTouchId = 3;
1647 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1648 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1649 delegate.get(), -1234, bounds, root_window()));
1652 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1653 kTouchId, tes.Now());
1654 DispatchEventUsingWindowDispatcher(&press);
1655 EXPECT_FALSE(delegate->tap());
1656 EXPECT_TRUE(delegate->tap_down());
1657 EXPECT_FALSE(delegate->tap_cancel());
1658 EXPECT_FALSE(delegate->scroll_begin());
1659 EXPECT_FALSE(delegate->scroll_update());
1660 EXPECT_FALSE(delegate->scroll_end());
1662 // Make sure there is enough delay before the touch is released so that it is
1663 // recognized as a tap.
1665 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1666 kTouchId, tes.LeapForward(50));
1667 DispatchEventUsingWindowDispatcher(&release);
1668 EXPECT_TRUE(delegate->tap());
1669 EXPECT_FALSE(delegate->tap_down());
1670 EXPECT_FALSE(delegate->tap_cancel());
1671 EXPECT_FALSE(delegate->scroll_begin());
1672 EXPECT_FALSE(delegate->scroll_update());
1673 EXPECT_FALSE(delegate->scroll_end());
1675 // Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger
1678 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1679 kTouchId, tes.LeapForward(1000));
1680 DispatchEventUsingWindowDispatcher(&press1);
1681 EXPECT_FALSE(delegate->tap());
1682 EXPECT_TRUE(delegate->tap_down());
1683 EXPECT_FALSE(delegate->tap_cancel());
1684 EXPECT_FALSE(delegate->scroll_begin());
1685 EXPECT_FALSE(delegate->scroll_update());
1686 EXPECT_FALSE(delegate->scroll_end());
1688 // Get rid of touch slop.
1689 ui::TouchEvent move_remove_slop(ui::ET_TOUCH_MOVED, gfx::Point(116, 216),
1690 kTouchId, tes.Now());
1691 DispatchEventUsingWindowDispatcher(&move_remove_slop);
1692 EXPECT_TRUE(delegate->tap_cancel());
1693 EXPECT_TRUE(delegate->scroll_begin());
1694 EXPECT_TRUE(delegate->scroll_update());
1695 EXPECT_EQ(15, delegate->scroll_x_hint());
1696 EXPECT_EQ(15, delegate->scroll_y_hint());
1700 // Move the touch-point enough so that it is considered as a scroll. This
1701 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1702 // The first movement is diagonal, to ensure that we have a free scroll,
1703 // and not a rail scroll.
1705 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(135, 235),
1706 kTouchId, tes.Now());
1707 DispatchEventUsingWindowDispatcher(&move);
1708 EXPECT_FALSE(delegate->tap());
1709 EXPECT_FALSE(delegate->tap_down());
1710 EXPECT_FALSE(delegate->tap_cancel());
1711 EXPECT_FALSE(delegate->scroll_begin());
1712 EXPECT_TRUE(delegate->scroll_update());
1713 EXPECT_FALSE(delegate->scroll_end());
1714 EXPECT_EQ(19, delegate->scroll_x());
1715 EXPECT_EQ(19, delegate->scroll_y());
1717 // Move some more to generate a few more scroll updates.
1719 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(115, 216),
1720 kTouchId, tes.Now());
1721 DispatchEventUsingWindowDispatcher(&move1);
1722 EXPECT_FALSE(delegate->tap());
1723 EXPECT_FALSE(delegate->tap_down());
1724 EXPECT_FALSE(delegate->tap_cancel());
1725 EXPECT_FALSE(delegate->scroll_begin());
1726 EXPECT_TRUE(delegate->scroll_update());
1727 EXPECT_FALSE(delegate->scroll_end());
1728 EXPECT_EQ(-20, delegate->scroll_x());
1729 EXPECT_EQ(-19, delegate->scroll_y());
1730 EXPECT_EQ(0, delegate->scroll_x_hint());
1731 EXPECT_EQ(0, delegate->scroll_y_hint());
1734 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 220),
1735 kTouchId, tes.Now());
1736 DispatchEventUsingWindowDispatcher(&move2);
1737 EXPECT_FALSE(delegate->tap());
1738 EXPECT_FALSE(delegate->tap_down());
1739 EXPECT_FALSE(delegate->tap_cancel());
1740 EXPECT_FALSE(delegate->scroll_begin());
1741 EXPECT_TRUE(delegate->scroll_update());
1742 EXPECT_FALSE(delegate->scroll_end());
1743 EXPECT_EQ(30, delegate->scroll_x());
1744 EXPECT_EQ(4, delegate->scroll_y());
1746 // Release the touch. This should end the scroll.
1748 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1749 kTouchId, tes.Now());
1750 DispatchEventUsingWindowDispatcher(&release1);
1751 EXPECT_FALSE(delegate->tap());
1752 EXPECT_FALSE(delegate->tap_down());
1753 EXPECT_FALSE(delegate->tap_cancel());
1754 EXPECT_FALSE(delegate->scroll_begin());
1755 EXPECT_FALSE(delegate->scroll_update());
1756 EXPECT_FALSE(delegate->scroll_end());
1757 EXPECT_TRUE(delegate->fling());
1760 TEST_P(GestureRecognizerTest, AsynchronousGestureRecognition) {
1761 scoped_ptr<QueueTouchEventDelegate> queued_delegate(
1762 new QueueTouchEventDelegate(host()->dispatcher()));
1763 const int kWindowWidth = 123;
1764 const int kWindowHeight = 45;
1765 const int kTouchId1 = 6;
1766 const int kTouchId2 = 4;
1767 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1768 scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate(
1769 queued_delegate.get(), -1234, bounds, root_window()));
1771 queued_delegate->set_window(queue.get());
1773 // Touch down on the window. This should not generate any gesture event.
1774 queued_delegate->Reset();
1775 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1776 kTouchId1, GetTime());
1777 DispatchEventUsingWindowDispatcher(&press);
1778 EXPECT_FALSE(queued_delegate->tap());
1779 EXPECT_FALSE(queued_delegate->tap_down());
1780 EXPECT_FALSE(queued_delegate->tap_cancel());
1781 EXPECT_FALSE(queued_delegate->begin());
1782 EXPECT_FALSE(queued_delegate->scroll_begin());
1783 EXPECT_FALSE(queued_delegate->scroll_update());
1784 EXPECT_FALSE(queued_delegate->scroll_end());
1786 // Introduce some delay before the touch is released so that it is recognized
1787 // as a tap. However, this still should not create any gesture events.
1788 queued_delegate->Reset();
1789 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1790 kTouchId1, press.time_stamp() +
1791 base::TimeDelta::FromMilliseconds(50));
1792 DispatchEventUsingWindowDispatcher(&release);
1793 EXPECT_FALSE(queued_delegate->tap());
1794 EXPECT_FALSE(queued_delegate->tap_down());
1795 EXPECT_FALSE(queued_delegate->tap_cancel());
1796 EXPECT_FALSE(queued_delegate->begin());
1797 EXPECT_FALSE(queued_delegate->end());
1798 EXPECT_FALSE(queued_delegate->scroll_begin());
1799 EXPECT_FALSE(queued_delegate->scroll_update());
1800 EXPECT_FALSE(queued_delegate->scroll_end());
1802 // Create another window, and place a touch-down on it. This should create a
1803 // tap-down gesture.
1804 scoped_ptr<GestureEventConsumeDelegate> delegate(
1805 new GestureEventConsumeDelegate());
1806 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1807 delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), root_window()));
1809 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 20),
1810 kTouchId2, GetTime());
1811 DispatchEventUsingWindowDispatcher(&press2);
1812 EXPECT_FALSE(delegate->tap());
1813 EXPECT_TRUE(delegate->tap_down());
1814 EXPECT_FALSE(delegate->tap_cancel());
1815 EXPECT_FALSE(queued_delegate->begin());
1816 EXPECT_FALSE(queued_delegate->end());
1817 EXPECT_FALSE(delegate->scroll_begin());
1818 EXPECT_FALSE(delegate->scroll_update());
1819 EXPECT_FALSE(delegate->scroll_end());
1821 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(10, 20),
1822 kTouchId2, GetTime());
1823 DispatchEventUsingWindowDispatcher(&release2);
1825 // Process the first queued event.
1826 queued_delegate->Reset();
1827 queued_delegate->ReceivedAck();
1828 EXPECT_FALSE(queued_delegate->tap());
1829 EXPECT_TRUE(queued_delegate->tap_down());
1830 EXPECT_TRUE(queued_delegate->begin());
1831 EXPECT_FALSE(queued_delegate->tap_cancel());
1832 EXPECT_FALSE(queued_delegate->end());
1833 EXPECT_FALSE(queued_delegate->scroll_begin());
1834 EXPECT_FALSE(queued_delegate->scroll_update());
1835 EXPECT_FALSE(queued_delegate->scroll_end());
1837 // Now, process the second queued event.
1838 queued_delegate->Reset();
1839 queued_delegate->ReceivedAck();
1840 EXPECT_TRUE(queued_delegate->tap());
1841 EXPECT_FALSE(queued_delegate->tap_down());
1842 EXPECT_FALSE(queued_delegate->tap_cancel());
1843 EXPECT_FALSE(queued_delegate->begin());
1844 EXPECT_TRUE(queued_delegate->end());
1845 EXPECT_FALSE(queued_delegate->scroll_begin());
1846 EXPECT_FALSE(queued_delegate->scroll_update());
1847 EXPECT_FALSE(queued_delegate->scroll_end());
1849 // Start all over. Press on the first window, then press again on the second
1850 // window. The second press should still go to the first window.
1851 queued_delegate->Reset();
1852 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1853 kTouchId1, GetTime());
1854 DispatchEventUsingWindowDispatcher(&press3);
1855 EXPECT_FALSE(queued_delegate->tap());
1856 EXPECT_FALSE(queued_delegate->tap_down());
1857 EXPECT_FALSE(queued_delegate->tap_cancel());
1858 EXPECT_FALSE(queued_delegate->begin());
1859 EXPECT_FALSE(queued_delegate->end());
1860 EXPECT_FALSE(queued_delegate->begin());
1861 EXPECT_FALSE(queued_delegate->end());
1862 EXPECT_FALSE(queued_delegate->scroll_begin());
1863 EXPECT_FALSE(queued_delegate->scroll_update());
1864 EXPECT_FALSE(queued_delegate->scroll_end());
1866 queued_delegate->Reset();
1868 ui::TouchEvent press4(ui::ET_TOUCH_PRESSED, gfx::Point(103, 203),
1869 kTouchId2, GetTime());
1870 DispatchEventUsingWindowDispatcher(&press4);
1871 EXPECT_FALSE(delegate->tap());
1872 EXPECT_FALSE(delegate->tap_down());
1873 EXPECT_FALSE(delegate->tap_cancel());
1874 EXPECT_FALSE(delegate->begin());
1875 EXPECT_FALSE(delegate->end());
1876 EXPECT_FALSE(delegate->scroll_begin());
1877 EXPECT_FALSE(delegate->scroll_update());
1878 EXPECT_FALSE(delegate->scroll_end());
1879 EXPECT_FALSE(queued_delegate->tap());
1880 EXPECT_FALSE(queued_delegate->tap_down());
1881 EXPECT_FALSE(queued_delegate->tap_cancel());
1882 EXPECT_FALSE(queued_delegate->begin());
1883 EXPECT_FALSE(queued_delegate->end());
1884 EXPECT_FALSE(queued_delegate->scroll_begin());
1885 EXPECT_FALSE(queued_delegate->scroll_update());
1886 EXPECT_FALSE(queued_delegate->scroll_end());
1888 // Move the second touch-point enough so that it is considered a pinch. This
1889 // should generate both SCROLL_BEGIN and PINCH_BEGIN gestures.
1890 queued_delegate->Reset();
1892 int x_move = ui::GestureConfiguration::max_touch_move_in_pixels_for_click();
1893 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(203 + x_move, 303),
1894 kTouchId2, GetTime());
1895 DispatchEventUsingWindowDispatcher(&move);
1896 EXPECT_FALSE(delegate->tap());
1897 EXPECT_FALSE(delegate->tap_down());
1898 EXPECT_FALSE(delegate->tap_cancel());
1899 EXPECT_FALSE(delegate->begin());
1900 EXPECT_FALSE(delegate->scroll_begin());
1901 EXPECT_FALSE(delegate->scroll_update());
1902 EXPECT_FALSE(delegate->scroll_end());
1903 EXPECT_FALSE(queued_delegate->tap());
1904 EXPECT_FALSE(queued_delegate->tap_down());
1905 EXPECT_FALSE(queued_delegate->tap_cancel());
1906 EXPECT_FALSE(queued_delegate->begin());
1907 EXPECT_FALSE(queued_delegate->scroll_begin());
1908 EXPECT_FALSE(queued_delegate->scroll_update());
1909 EXPECT_FALSE(queued_delegate->scroll_end());
1911 queued_delegate->Reset();
1912 queued_delegate->ReceivedAck();
1913 EXPECT_FALSE(queued_delegate->tap());
1914 EXPECT_TRUE(queued_delegate->tap_down());
1915 EXPECT_TRUE(queued_delegate->begin());
1916 EXPECT_FALSE(queued_delegate->tap_cancel());
1917 EXPECT_FALSE(queued_delegate->end());
1918 EXPECT_FALSE(queued_delegate->scroll_begin());
1919 EXPECT_FALSE(queued_delegate->scroll_update());
1920 EXPECT_FALSE(queued_delegate->scroll_end());
1922 queued_delegate->Reset();
1923 queued_delegate->ReceivedAck();
1924 EXPECT_FALSE(queued_delegate->tap());
1925 EXPECT_FALSE(queued_delegate->tap_down()); // no touch down for second tap.
1926 EXPECT_TRUE(queued_delegate->tap_cancel());
1927 EXPECT_TRUE(queued_delegate->begin());
1928 EXPECT_FALSE(queued_delegate->end());
1929 EXPECT_FALSE(queued_delegate->scroll_begin());
1930 EXPECT_FALSE(queued_delegate->scroll_update());
1931 EXPECT_FALSE(queued_delegate->scroll_end());
1932 EXPECT_FALSE(queued_delegate->pinch_begin());
1933 EXPECT_FALSE(queued_delegate->pinch_update());
1934 EXPECT_FALSE(queued_delegate->pinch_end());
1936 queued_delegate->Reset();
1937 queued_delegate->ReceivedAck();
1938 EXPECT_FALSE(queued_delegate->tap());
1939 EXPECT_FALSE(queued_delegate->tap_down());
1940 EXPECT_FALSE(queued_delegate->tap_cancel());
1941 EXPECT_FALSE(queued_delegate->begin());
1942 EXPECT_FALSE(queued_delegate->end());
1943 EXPECT_TRUE(queued_delegate->scroll_begin());
1944 // TODO(tdresser): uncomment once we've switched to the unified GR.
1945 // EXPECT_TRUE(queued_delegate->scroll_update());
1946 EXPECT_FALSE(queued_delegate->scroll_end());
1947 EXPECT_TRUE(queued_delegate->pinch_begin());
1948 EXPECT_FALSE(queued_delegate->pinch_update());
1949 EXPECT_FALSE(queued_delegate->pinch_end());
1952 // Check that appropriate touch events generate pinch gesture events.
1953 TEST_P(GestureRecognizerTest, GestureEventPinchFromScroll) {
1954 // Disabled for unified GR due to differences in when pinch begin is sent. The
1955 // Aura GR sends it earlier than is necessary.
1956 if (UsingUnifiedGR())
1959 scoped_ptr<GestureEventConsumeDelegate> delegate(
1960 new GestureEventConsumeDelegate());
1962 const int kWindowWidth = 300;
1963 const int kWindowHeight = 400;
1964 const int kTouchId1 = 5;
1965 const int kTouchId2 = 3;
1966 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1967 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1968 delegate.get(), -1234, bounds, root_window()));
1971 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1972 kTouchId1, tes.Now());
1973 DispatchEventUsingWindowDispatcher(&press);
1974 EXPECT_2_EVENTS(delegate->events(),
1975 ui::ET_GESTURE_BEGIN,
1976 ui::ET_GESTURE_TAP_DOWN);
1978 // Move the touch-point enough so that it is considered as a scroll. This
1979 // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1981 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 301),
1982 kTouchId1, tes.Now());
1983 DispatchEventUsingWindowDispatcher(&move);
1984 EXPECT_3_EVENTS(delegate->events(),
1985 ui::ET_GESTURE_TAP_CANCEL,
1986 ui::ET_GESTURE_SCROLL_BEGIN,
1987 ui::ET_GESTURE_SCROLL_UPDATE);
1989 // Press the second finger. It should cause pinch-begin. Note that we will not
1990 // transition to two finger tap here because the touch points are far enough.
1992 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1993 kTouchId2, tes.Now());
1994 DispatchEventUsingWindowDispatcher(&press2);
1995 EXPECT_2_EVENTS(delegate->events(),
1996 ui::ET_GESTURE_BEGIN,
1997 ui::ET_GESTURE_PINCH_BEGIN);
1998 EXPECT_EQ(gfx::Rect(10, 10, 120, 291).ToString(),
1999 delegate->bounding_box().ToString());
2001 // Move the first finger.
2003 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(95, 201),
2004 kTouchId1, tes.Now());
2005 DispatchEventUsingWindowDispatcher(&move3);
2006 EXPECT_2_EVENTS(delegate->events(),
2007 ui::ET_GESTURE_PINCH_UPDATE,
2008 ui::ET_GESTURE_SCROLL_UPDATE);
2009 EXPECT_EQ(gfx::Rect(10, 10, 85, 191).ToString(),
2010 delegate->bounding_box().ToString());
2012 // Now move the second finger.
2014 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
2015 kTouchId2, tes.Now());
2016 DispatchEventUsingWindowDispatcher(&move4);
2017 EXPECT_2_EVENTS(delegate->events(),
2018 ui::ET_GESTURE_PINCH_UPDATE,
2019 ui::ET_GESTURE_SCROLL_UPDATE);
2020 EXPECT_EQ(gfx::Rect(55, 15, 40, 186).ToString(),
2021 delegate->bounding_box().ToString());
2023 // Release the first finger. This should end pinch.
2025 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2026 kTouchId1, tes.Now());
2027 DispatchEventUsingWindowDispatcher(&release);
2028 EXPECT_2_EVENTS(delegate->events(),
2029 ui::ET_GESTURE_PINCH_END,
2030 ui::ET_GESTURE_END);
2031 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
2032 delegate->bounding_box().ToString());
2034 // Move the second finger. This should still generate a scroll.
2036 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2037 kTouchId2, tes.Now());
2038 DispatchEventUsingWindowDispatcher(&move5);
2039 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2040 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2043 TEST_P(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) {
2044 scoped_ptr<GestureEventConsumeDelegate> delegate(
2045 new GestureEventConsumeDelegate());
2047 const int kWindowWidth = 300;
2048 const int kWindowHeight = 400;
2049 const int kTouchId1 = 5;
2050 const int kTouchId2 = 3;
2051 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2052 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2053 delegate.get(), -1234, bounds, root_window()));
2055 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2056 kTouchId1, tes.Now());
2057 DispatchEventUsingWindowDispatcher(&press);
2059 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2060 kTouchId2, tes.Now());
2061 DispatchEventUsingWindowDispatcher(&press2);
2062 EXPECT_FALSE(delegate->pinch_begin());
2064 // Touch move triggers pinch begin.
2065 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2066 EXPECT_TRUE(delegate->pinch_begin());
2067 EXPECT_FALSE(delegate->pinch_update());
2069 // Touch move triggers pinch update.
2070 tes.SendScrollEvent(event_processor(), 160, 200, kTouchId1, delegate.get());
2071 EXPECT_FALSE(delegate->pinch_begin());
2072 EXPECT_TRUE(delegate->pinch_update());
2074 // Pinch has started, now release the second finger
2076 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2077 kTouchId1, tes.Now());
2078 DispatchEventUsingWindowDispatcher(&release);
2079 EXPECT_TRUE(delegate->pinch_end());
2081 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId2, delegate.get());
2082 EXPECT_TRUE(delegate->scroll_update());
2086 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2087 kTouchId1, tes.Now());
2088 DispatchEventUsingWindowDispatcher(&press3);
2089 // Now the touch points are close. So we will go into two finger tap.
2090 // Move the touch-point enough to break two-finger-tap and enter pinch.
2091 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 50),
2092 kTouchId1, tes.Now());
2093 DispatchEventUsingWindowDispatcher(&move2);
2094 EXPECT_TRUE(delegate->pinch_begin());
2096 tes.SendScrollEvent(event_processor(), 350, 350, kTouchId1, delegate.get());
2097 EXPECT_TRUE(delegate->pinch_update());
2100 TEST_P(GestureRecognizerTest, GestureEventPinchFromTap) {
2101 // TODO(tdresser): enable this test with unified GR once two finger tap.
2102 if (UsingUnifiedGR())
2105 scoped_ptr<GestureEventConsumeDelegate> delegate(
2106 new GestureEventConsumeDelegate());
2108 const int kWindowWidth = 300;
2109 const int kWindowHeight = 400;
2110 const int kTouchId1 = 3;
2111 const int kTouchId2 = 5;
2112 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2113 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2114 delegate.get(), -1234, bounds, root_window()));
2117 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2118 kTouchId1, tes.Now());
2119 DispatchEventUsingWindowDispatcher(&press);
2120 EXPECT_2_EVENTS(delegate->events(),
2121 ui::ET_GESTURE_BEGIN,
2122 ui::ET_GESTURE_TAP_DOWN);
2123 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2125 // Press the second finger far enough to break two finger tap.
2127 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2128 kTouchId2, tes.Now());
2129 DispatchEventUsingWindowDispatcher(&press2);
2130 EXPECT_2_EVENTS(delegate->events(),
2131 ui::ET_GESTURE_TAP_CANCEL,
2132 ui::ET_GESTURE_BEGIN);
2133 EXPECT_EQ(gfx::Rect(10, 10, 91, 291).ToString(),
2134 delegate->bounding_box().ToString());
2136 // Move the first finger.
2138 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
2139 kTouchId1, tes.Now());
2140 DispatchEventUsingWindowDispatcher(&move3);
2141 EXPECT_2_EVENTS(delegate->events(),
2142 ui::ET_GESTURE_SCROLL_BEGIN,
2143 ui::ET_GESTURE_PINCH_BEGIN);
2144 EXPECT_EQ(gfx::Rect(10, 10, 55, 191).ToString(),
2145 delegate->bounding_box().ToString());
2147 // Now move the second finger.
2149 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
2150 kTouchId2, tes.Now());
2151 DispatchEventUsingWindowDispatcher(&move4);
2152 EXPECT_2_EVENTS(delegate->events(),
2153 ui::ET_GESTURE_PINCH_UPDATE,
2154 ui::ET_GESTURE_SCROLL_UPDATE);
2155 EXPECT_EQ(gfx::Rect(55, 15, 10, 186).ToString(),
2156 delegate->bounding_box().ToString());
2158 // Release the first finger. This should end pinch.
2160 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2161 kTouchId1, tes.LeapForward(10));
2162 DispatchEventUsingWindowDispatcher(&release);
2163 EXPECT_2_EVENTS(delegate->events(),
2164 ui::ET_GESTURE_PINCH_END,
2165 ui::ET_GESTURE_END);
2166 EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
2167 delegate->bounding_box().ToString());
2169 // Move the second finger. This should still generate a scroll.
2171 ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2172 kTouchId2, tes.Now());
2173 DispatchEventUsingWindowDispatcher(&move5);
2174 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2175 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2178 TEST_P(GestureRecognizerTest, GestureEventIgnoresDisconnectedEvents) {
2179 scoped_ptr<GestureEventConsumeDelegate> delegate(
2180 new GestureEventConsumeDelegate());
2183 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2185 DispatchEventUsingWindowDispatcher(&release1);
2186 EXPECT_FALSE(delegate->tap());
2187 EXPECT_FALSE(delegate->tap_down());
2190 // Check that a touch is locked to the window of the closest current touch
2191 // within max_separation_for_gesture_touches_in_pixels
2192 TEST_P(GestureRecognizerTest, GestureEventTouchLockSelectsCorrectWindow) {
2193 ui::GestureRecognizer* gesture_recognizer = new ui::GestureRecognizerImpl();
2195 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2197 ui::GestureConsumer* target;
2198 const int kNumWindows = 4;
2200 scoped_ptr<GestureEventConsumeDelegate*[]> delegates(
2201 new GestureEventConsumeDelegate*[kNumWindows]);
2203 ui::GestureConfiguration::
2204 set_max_separation_for_gesture_touches_in_pixels(499);
2206 scoped_ptr<gfx::Rect[]> window_bounds(new gfx::Rect[kNumWindows]);
2207 window_bounds[0] = gfx::Rect(0, 0, 1, 1);
2208 window_bounds[1] = gfx::Rect(500, 0, 1, 1);
2209 window_bounds[2] = gfx::Rect(0, 500, 1, 1);
2210 window_bounds[3] = gfx::Rect(500, 500, 1, 1);
2212 scoped_ptr<aura::Window*[]> windows(new aura::Window*[kNumWindows]);
2214 // Instantiate windows with |window_bounds| and touch each window at
2216 for (int i = 0; i < kNumWindows; ++i) {
2217 delegates[i] = new GestureEventConsumeDelegate();
2218 windows[i] = CreateTestWindowWithDelegate(
2219 delegates[i], i, window_bounds[i], root_window());
2220 windows[i]->set_id(i);
2221 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, window_bounds[i].origin(),
2223 DispatchEventUsingWindowDispatcher(&press);
2226 // Touches should now be associated with the closest touch within
2227 // ui::GestureConfiguration::max_separation_for_gesture_touches_in_pixels
2228 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 11), -1);
2229 EXPECT_EQ("0", WindowIDAsString(target));
2230 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 11), -1);
2231 EXPECT_EQ("1", WindowIDAsString(target));
2232 target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 511), -1);
2233 EXPECT_EQ("2", WindowIDAsString(target));
2234 target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 511), -1);
2235 EXPECT_EQ("3", WindowIDAsString(target));
2237 // Add a touch in the middle associated with windows[2]
2238 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 500),
2239 kNumWindows, tes.Now());
2240 DispatchEventUsingWindowDispatcher(&press);
2241 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(250, 250),
2242 kNumWindows, tes.Now());
2243 DispatchEventUsingWindowDispatcher(&move);
2245 target = gesture_recognizer->GetTargetForLocation(gfx::Point(250, 250), -1);
2246 EXPECT_EQ("2", WindowIDAsString(target));
2248 // Make sure that ties are broken by distance to a current touch
2249 // Closer to the point in the bottom right.
2250 target = gesture_recognizer->GetTargetForLocation(gfx::Point(380, 380), -1);
2251 EXPECT_EQ("3", WindowIDAsString(target));
2253 // This touch is closer to the point in the middle
2254 target = gesture_recognizer->GetTargetForLocation(gfx::Point(300, 300), -1);
2255 EXPECT_EQ("2", WindowIDAsString(target));
2257 // A touch too far from other touches won't be locked to anything
2258 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2259 EXPECT_TRUE(target == NULL);
2261 // Move a touch associated with windows[2] to 1000, 1000
2262 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(1000, 1000),
2263 kNumWindows, tes.Now());
2264 DispatchEventUsingWindowDispatcher(&move2);
2266 target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2267 EXPECT_EQ("2", WindowIDAsString(target));
2269 for (int i = 0; i < kNumWindows; ++i) {
2270 // Delete windows before deleting delegates.
2272 delete delegates[i];
2276 // Check that a touch's target will not be effected by a touch on a different
2278 TEST_P(GestureRecognizerTest, GestureEventTouchLockIgnoresOtherScreens) {
2279 scoped_ptr<GestureEventConsumeDelegate> delegate(
2280 new GestureEventConsumeDelegate());
2281 gfx::Rect bounds(0, 0, 10, 10);
2282 scoped_ptr<aura::Window> window(
2283 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2285 const int kTouchId1 = 8;
2286 const int kTouchId2 = 2;
2289 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(5, 5),
2290 kTouchId1, tes.Now());
2291 press1.set_source_device_id(1);
2292 DispatchEventUsingWindowDispatcher(&press1);
2294 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2295 kTouchId2, tes.Now());
2296 press2.set_source_device_id(2);
2297 DispatchEventUsingWindowDispatcher(&press2);
2299 // The second press should not have been locked to the same target as the
2300 // first, as they occured on different displays.
2302 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1),
2303 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2306 // Check that touch events outside the root window are still handled
2307 // by the root window's gesture sequence.
2308 TEST_P(GestureRecognizerTest, GestureEventOutsideRootWindowTap) {
2309 // TODO(tdresser): write a version of this test for the unified GR.
2310 if (UsingUnifiedGR())
2313 TestGestureRecognizer* gesture_recognizer =
2314 new TestGestureRecognizer();
2316 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2318 scoped_ptr<aura::Window> window(CreateTestWindowWithBounds(
2319 gfx::Rect(-100, -100, 2000, 2000), root_window()));
2321 ui::GestureSequence* window_gesture_sequence =
2322 gesture_recognizer->GetGestureSequenceForTesting(window.get());
2324 ui::GestureSequence* root_window_gesture_sequence =
2325 gesture_recognizer->GetGestureSequenceForTesting(root_window());
2327 gfx::Point pos1(-10, -10);
2328 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, pos1, 0, tes.Now());
2329 DispatchEventUsingWindowDispatcher(&press1);
2331 gfx::Point pos2(1000, 1000);
2332 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, pos2, 1, tes.Now());
2333 DispatchEventUsingWindowDispatcher(&press2);
2335 // As these presses were outside the root window, they should be
2336 // associated with the root window.
2337 EXPECT_EQ(0, window_gesture_sequence->point_count());
2338 EXPECT_EQ(2, root_window_gesture_sequence->point_count());
2341 TEST_P(GestureRecognizerTest, NoTapWithPreventDefaultedRelease) {
2342 scoped_ptr<QueueTouchEventDelegate> delegate(
2343 new QueueTouchEventDelegate(host()->dispatcher()));
2345 const int kTouchId = 2;
2346 gfx::Rect bounds(100, 200, 100, 100);
2347 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2348 delegate.get(), -1234, bounds, root_window()));
2349 delegate->set_window(window.get());
2352 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2353 kTouchId, tes.Now());
2354 DispatchEventUsingWindowDispatcher(&press);
2355 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2356 kTouchId, tes.LeapForward(50));
2357 DispatchEventUsingWindowDispatcher(&release);
2360 delegate->ReceivedAck();
2361 EXPECT_TRUE(delegate->tap_down());
2363 delegate->ReceivedAckPreventDefaulted();
2364 EXPECT_FALSE(delegate->tap());
2365 EXPECT_TRUE(delegate->tap_cancel());
2368 TEST_P(GestureRecognizerTest, PinchScrollWithPreventDefaultedRelease) {
2369 // Disabled for unified GR due to differences in when pinch begin is sent. The
2370 // Aura GR sends it earlier than is necessary.
2371 if (UsingUnifiedGR())
2374 scoped_ptr<QueueTouchEventDelegate> delegate(
2375 new QueueTouchEventDelegate(host()->dispatcher()));
2377 const int kTouchId1 = 7;
2378 const int kTouchId2 = 5;
2379 gfx::Rect bounds(10, 20, 100, 100);
2380 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2381 delegate.get(), -1234, bounds, root_window()));
2382 delegate->set_window(window.get());
2386 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2388 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2389 tes.LeapForward(200));
2390 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2391 tes.LeapForward(50));
2392 DispatchEventUsingWindowDispatcher(&press);
2393 DispatchEventUsingWindowDispatcher(&move);
2394 DispatchEventUsingWindowDispatcher(&release);
2397 // Ack the press event.
2398 delegate->ReceivedAck();
2399 EXPECT_TRUE(delegate->tap_down());
2402 // Ack the move event.
2403 delegate->ReceivedAck();
2404 EXPECT_TRUE(delegate->tap_cancel());
2405 EXPECT_TRUE(delegate->scroll_begin());
2408 // Ack the release event. Although the release event has been processed, it
2409 // should still generate a scroll-end event.
2410 delegate->ReceivedAckPreventDefaulted();
2411 EXPECT_TRUE(delegate->scroll_end());
2414 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2416 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2417 tes.LeapForward(200));
2418 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2419 tes.LeapForward(50));
2420 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 25), kTouchId2,
2422 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(45, 85), kTouchId2,
2423 tes.LeapForward(1000));
2424 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(45, 85), kTouchId2,
2425 tes.LeapForward(14));
2428 DispatchEventUsingWindowDispatcher(&press);
2429 DispatchEventUsingWindowDispatcher(&move);
2430 DispatchEventUsingWindowDispatcher(&press2);
2431 DispatchEventUsingWindowDispatcher(&move2);
2432 DispatchEventUsingWindowDispatcher(&release);
2433 DispatchEventUsingWindowDispatcher(&release2);
2435 // Ack the press and move events.
2437 delegate->ReceivedAck();
2438 EXPECT_TRUE(delegate->begin());
2439 EXPECT_TRUE(delegate->tap_down());
2442 delegate->ReceivedAck();
2443 EXPECT_TRUE(delegate->scroll_begin());
2446 delegate->ReceivedAck();
2447 EXPECT_TRUE(delegate->begin());
2448 EXPECT_TRUE(delegate->pinch_begin());
2451 delegate->ReceivedAck();
2452 EXPECT_TRUE(delegate->pinch_update());
2454 // Ack the first release. Although the release is processed, it should still
2455 // generate a pinch-end event.
2457 delegate->ReceivedAckPreventDefaulted();
2458 EXPECT_TRUE(delegate->pinch_end());
2459 EXPECT_TRUE(delegate->end());
2462 delegate->ReceivedAckPreventDefaulted();
2463 EXPECT_TRUE(delegate->scroll_end());
2464 EXPECT_TRUE(delegate->end());
2467 TEST_P(GestureRecognizerTest, GestureEndLocation) {
2468 GestureEventConsumeDelegate delegate;
2469 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2470 &delegate, -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2471 EventGenerator generator(root_window(), window.get());
2472 const gfx::Point begin(20, 20);
2473 const gfx::Point end(150, 150);
2474 const gfx::Vector2d window_offset =
2475 window->bounds().origin().OffsetFromOrigin();
2476 generator.GestureScrollSequence(begin, end,
2477 base::TimeDelta::FromMilliseconds(20),
2479 EXPECT_EQ((begin - window_offset).ToString(),
2480 delegate.scroll_begin_position().ToString());
2481 EXPECT_EQ((end - window_offset).ToString(),
2482 delegate.gesture_end_location().ToString());
2485 TEST_P(GestureRecognizerTest, CaptureSendsGestureEnd) {
2486 scoped_ptr<GestureEventConsumeDelegate> delegate(
2487 new GestureEventConsumeDelegate());
2488 TestGestureRecognizer* gesture_recognizer =
2489 new TestGestureRecognizer();
2490 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2492 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2493 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2494 EventGenerator generator(root_window());
2496 generator.MoveMouseRelativeTo(window.get(), gfx::Point(10, 10));
2497 generator.PressTouch();
2498 RunAllPendingInMessageLoop();
2500 EXPECT_TRUE(delegate->tap_down());
2502 scoped_ptr<aura::Window> capture(CreateTestWindowWithBounds(
2503 gfx::Rect(10, 10, 200, 200), root_window()));
2504 capture->SetCapture();
2505 RunAllPendingInMessageLoop();
2507 EXPECT_TRUE(delegate->end());
2508 EXPECT_TRUE(delegate->tap_cancel());
2511 // Check that previous touch actions that are completely finished (either
2512 // released or cancelled), do not receive extra synthetic cancels upon change of
2514 TEST_P(GestureRecognizerTest, CaptureDoesNotCancelFinishedTouches) {
2515 scoped_ptr<GestureEventConsumeDelegate> delegate(
2516 new GestureEventConsumeDelegate());
2517 scoped_ptr<TestEventHandler> handler(new TestEventHandler);
2518 root_window()->AddPreTargetHandler(handler.get());
2520 // Create a window and set it as the capture window.
2521 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(delegate.get(),
2522 -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2523 window1->SetCapture();
2525 EventGenerator generator(root_window());
2528 // Generate two touch-press events on the window.
2529 scoped_ptr<ui::TouchEvent> touch0(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2530 gfx::Point(20, 20), 0,
2532 scoped_ptr<ui::TouchEvent> touch1(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2533 gfx::Point(30, 30), 1,
2535 generator.Dispatch(touch0.get());
2536 generator.Dispatch(touch1.get());
2537 RunAllPendingInMessageLoop();
2538 EXPECT_EQ(2, handler->touch_pressed_count());
2541 tes.LeapForward(1000);
2543 // End the two touches, one by a touch-release and one by a touch-cancel; to
2544 // cover both cases.
2545 touch0.reset(new ui::TouchEvent(ui::ET_TOUCH_RELEASED, gfx::Point(20, 20), 0,
2547 touch1.reset(new ui::TouchEvent(ui::ET_TOUCH_CANCELLED, gfx::Point(30, 30), 1,
2549 generator.Dispatch(touch0.get());
2550 generator.Dispatch(touch1.get());
2551 RunAllPendingInMessageLoop();
2552 EXPECT_EQ(1, handler->touch_released_count());
2553 EXPECT_EQ(1, handler->touch_cancelled_count());
2555 // Create a new window and set it as the new capture window.
2556 scoped_ptr<aura::Window> window2(CreateTestWindowWithBounds(
2557 gfx::Rect(100, 100, 300, 300), root_window()));
2558 window2->SetCapture();
2559 RunAllPendingInMessageLoop();
2560 // Check that setting capture does not generate any synthetic touch-cancels
2561 // for the two previously finished touch actions.
2562 EXPECT_EQ(1, handler->touch_cancelled_count());
2564 root_window()->RemovePreTargetHandler(handler.get());
2567 // Tests that a press with the same touch id as an existing touch is ignored.
2568 TEST_P(GestureRecognizerTest, PressDoesNotCrash) {
2569 scoped_ptr<GestureEventConsumeDelegate> delegate(
2570 new GestureEventConsumeDelegate());
2571 TestGestureRecognizer* gesture_recognizer =
2572 new TestGestureRecognizer();
2573 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2576 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2577 delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2579 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(45, 45), 7, tes.Now());
2580 press.set_radius_x(40);
2581 DispatchEventUsingWindowDispatcher(&press);
2582 EXPECT_TRUE(delegate->tap_down());
2583 EXPECT_EQ(gfx::Rect(5, 5, 80, 80).ToString(),
2584 delegate->bounding_box().ToString());
2587 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 45), 7, tes.Now());
2588 DispatchEventUsingWindowDispatcher(&press2);
2590 // This new press should not generate a tap-down.
2591 EXPECT_FALSE(delegate->begin());
2592 EXPECT_FALSE(delegate->tap_down());
2593 EXPECT_FALSE(delegate->tap_cancel());
2594 EXPECT_FALSE(delegate->scroll_begin());
2597 TEST_P(GestureRecognizerTest, TwoFingerTap) {
2598 // TODO(tdresser): enable this test with unified GR once two finger tap is
2599 // supported. See crbug.com/354396.
2600 if (UsingUnifiedGR())
2603 scoped_ptr<GestureEventConsumeDelegate> delegate(
2604 new GestureEventConsumeDelegate());
2605 const int kWindowWidth = 123;
2606 const int kWindowHeight = 45;
2607 const int kTouchId1 = 2;
2608 const int kTouchId2 = 3;
2609 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2610 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2611 delegate.get(), -1234, bounds, root_window()));
2615 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2616 kTouchId1, tes.Now());
2617 DispatchEventUsingWindowDispatcher(&press1);
2618 EXPECT_FALSE(delegate->tap());
2619 EXPECT_TRUE(delegate->tap_down());
2620 EXPECT_FALSE(delegate->tap_cancel());
2621 EXPECT_FALSE(delegate->scroll_begin());
2622 EXPECT_FALSE(delegate->scroll_update());
2623 EXPECT_FALSE(delegate->scroll_end());
2624 EXPECT_FALSE(delegate->long_press());
2625 EXPECT_FALSE(delegate->two_finger_tap());
2628 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2629 kTouchId2, tes.Now());
2630 DispatchEventUsingWindowDispatcher(&press2);
2631 EXPECT_FALSE(delegate->tap());
2632 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
2633 EXPECT_TRUE(delegate->tap_cancel());
2634 EXPECT_FALSE(delegate->scroll_begin());
2635 EXPECT_FALSE(delegate->scroll_update());
2636 EXPECT_FALSE(delegate->scroll_end());
2637 EXPECT_FALSE(delegate->long_press());
2638 EXPECT_FALSE(delegate->two_finger_tap());
2640 // Little bit of touch move should not affect our state.
2642 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(102, 202),
2643 kTouchId1, tes.Now());
2644 DispatchEventUsingWindowDispatcher(&move1);
2645 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(131, 202),
2646 kTouchId2, tes.Now());
2647 DispatchEventUsingWindowDispatcher(&move2);
2648 EXPECT_FALSE(delegate->tap());
2649 EXPECT_FALSE(delegate->tap_down());
2650 EXPECT_FALSE(delegate->tap_cancel());
2651 EXPECT_FALSE(delegate->scroll_begin());
2652 EXPECT_FALSE(delegate->scroll_update());
2653 EXPECT_FALSE(delegate->scroll_end());
2654 EXPECT_FALSE(delegate->long_press());
2655 EXPECT_FALSE(delegate->two_finger_tap());
2657 // Make sure there is enough delay before the touch is released so that it is
2658 // recognized as a tap.
2660 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2661 kTouchId1, tes.LeapForward(50));
2663 DispatchEventUsingWindowDispatcher(&release1);
2664 EXPECT_FALSE(delegate->tap());
2665 EXPECT_FALSE(delegate->tap_down());
2666 EXPECT_FALSE(delegate->tap_cancel());
2667 EXPECT_TRUE(delegate->scroll_begin());
2668 EXPECT_FALSE(delegate->scroll_update());
2669 EXPECT_FALSE(delegate->scroll_end());
2670 EXPECT_TRUE(delegate->two_finger_tap());
2672 // Lift second finger.
2673 // Make sure there is enough delay before the touch is released so that it is
2674 // recognized as a tap.
2676 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2677 kTouchId2, tes.LeapForward(50));
2679 DispatchEventUsingWindowDispatcher(&release2);
2680 EXPECT_FALSE(delegate->tap());
2681 EXPECT_FALSE(delegate->tap_down());
2682 EXPECT_FALSE(delegate->tap_cancel());
2683 EXPECT_FALSE(delegate->scroll_begin());
2684 EXPECT_FALSE(delegate->scroll_update());
2685 EXPECT_FALSE(delegate->scroll_end());
2686 EXPECT_TRUE(delegate->fling());
2687 EXPECT_FALSE(delegate->two_finger_tap());
2690 TEST_P(GestureRecognizerTest, TwoFingerTapExpired) {
2691 scoped_ptr<GestureEventConsumeDelegate> delegate(
2692 new GestureEventConsumeDelegate());
2693 const int kWindowWidth = 123;
2694 const int kWindowHeight = 45;
2695 const int kTouchId1 = 2;
2696 const int kTouchId2 = 3;
2697 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2698 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2699 delegate.get(), -1234, bounds, root_window()));
2703 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2704 kTouchId1, tes.Now());
2705 DispatchEventUsingWindowDispatcher(&press1);
2708 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2709 kTouchId2, tes.Now());
2710 DispatchEventUsingWindowDispatcher(&press2);
2712 // Send release event after sufficient delay so that two finger time expires.
2714 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2715 kTouchId1, tes.LeapForward(1000));
2717 DispatchEventUsingWindowDispatcher(&release1);
2718 EXPECT_FALSE(delegate->two_finger_tap());
2720 // Lift second finger.
2721 // Make sure there is enough delay before the touch is released so that it is
2722 // recognized as a tap.
2724 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2725 kTouchId2, tes.LeapForward(50));
2727 DispatchEventUsingWindowDispatcher(&release2);
2728 EXPECT_FALSE(delegate->two_finger_tap());
2731 TEST_P(GestureRecognizerTest, TwoFingerTapChangesToPinch) {
2732 scoped_ptr<GestureEventConsumeDelegate> delegate(
2733 new GestureEventConsumeDelegate());
2734 const int kWindowWidth = 123;
2735 const int kWindowHeight = 45;
2736 const int kTouchId1 = 2;
2737 const int kTouchId2 = 3;
2740 // Test moving first finger
2742 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2743 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2744 delegate.get(), -1234, bounds, root_window()));
2747 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2748 kTouchId1, tes.Now());
2749 DispatchEventUsingWindowDispatcher(&press1);
2752 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2753 kTouchId2, tes.Now());
2754 DispatchEventUsingWindowDispatcher(&press2);
2756 tes.SendScrollEvent(event_processor(), 230, 330, kTouchId1, delegate.get());
2757 EXPECT_FALSE(delegate->two_finger_tap());
2758 EXPECT_TRUE(delegate->pinch_begin());
2760 // Make sure there is enough delay before the touch is released so that it
2761 // is recognized as a tap.
2763 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2764 kTouchId2, tes.LeapForward(50));
2766 DispatchEventUsingWindowDispatcher(&release);
2767 EXPECT_FALSE(delegate->two_finger_tap());
2768 EXPECT_TRUE(delegate->pinch_end());
2771 // Test moving second finger
2773 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2774 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2775 delegate.get(), -1234, bounds, root_window()));
2778 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2779 kTouchId1, tes.Now());
2780 DispatchEventUsingWindowDispatcher(&press1);
2783 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2784 kTouchId2, tes.Now());
2785 DispatchEventUsingWindowDispatcher(&press2);
2787 tes.SendScrollEvent(event_processor(), 301, 230, kTouchId2, delegate.get());
2788 EXPECT_FALSE(delegate->two_finger_tap());
2789 EXPECT_TRUE(delegate->pinch_begin());
2791 // Make sure there is enough delay before the touch is released so that it
2792 // is recognized as a tap.
2794 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2795 kTouchId1, tes.LeapForward(50));
2797 DispatchEventUsingWindowDispatcher(&release);
2798 EXPECT_FALSE(delegate->two_finger_tap());
2799 EXPECT_TRUE(delegate->pinch_end());
2803 TEST_P(GestureRecognizerTest, NoTwoFingerTapWhenFirstFingerHasScrolled) {
2804 // Disabled for unified GR due to differences in when pinch begin is sent. The
2805 // Aura GR sends it earlier than is necessary.
2806 if (UsingUnifiedGR())
2809 scoped_ptr<GestureEventConsumeDelegate> delegate(
2810 new GestureEventConsumeDelegate());
2811 const int kWindowWidth = 123;
2812 const int kWindowHeight = 45;
2813 const int kTouchId1 = 2;
2814 const int kTouchId2 = 3;
2817 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2818 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2819 delegate.get(), -1234, bounds, root_window()));
2822 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2823 kTouchId1, tes.Now());
2824 DispatchEventUsingWindowDispatcher(&press1);
2825 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2828 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2829 kTouchId2, tes.Now());
2830 DispatchEventUsingWindowDispatcher(&press2);
2832 EXPECT_TRUE(delegate->pinch_begin());
2834 // Make sure there is enough delay before the touch is released so that it
2835 // is recognized as a tap.
2837 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2838 kTouchId2, tes.LeapForward(50));
2840 DispatchEventUsingWindowDispatcher(&release);
2841 EXPECT_FALSE(delegate->two_finger_tap());
2842 EXPECT_TRUE(delegate->pinch_end());
2845 TEST_P(GestureRecognizerTest, MultiFingerSwipe) {
2846 scoped_ptr<GestureEventConsumeDelegate> delegate(
2847 new GestureEventConsumeDelegate());
2848 const int kWindowWidth = 123;
2849 const int kWindowHeight = 45;
2851 gfx::Rect bounds(5, 10, kWindowWidth, kWindowHeight);
2852 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2853 delegate.get(), -1234, bounds, root_window()));
2855 const int kSteps = 15;
2856 const int kTouchPoints = 4;
2857 gfx::Point points[kTouchPoints] = {
2864 aura::test::EventGenerator generator(root_window(), window.get());
2866 // The unified gesture recognizer assumes a finger has stopped if it hasn't
2867 // moved for too long. See ui/events/gesture_detection/velocity_tracker.cc's
2868 // kAssumePointerStoppedTimeMs.
2869 for (int count = 2; count <= kTouchPoints; ++count) {
2870 generator.GestureMultiFingerScroll(
2871 count, points, 10, kSteps, 0, -11 * kSteps);
2872 EXPECT_TRUE(delegate->swipe_up());
2875 generator.GestureMultiFingerScroll(
2876 count, points, 10, kSteps, 0, 11 * kSteps);
2877 EXPECT_TRUE(delegate->swipe_down());
2880 generator.GestureMultiFingerScroll(
2881 count, points, 10, kSteps, -11 * kSteps, 0);
2882 EXPECT_TRUE(delegate->swipe_left());
2885 generator.GestureMultiFingerScroll(
2886 count, points, 10, kSteps, 11 * kSteps, 0);
2887 EXPECT_TRUE(delegate->swipe_right());
2892 TEST_P(GestureRecognizerTest, TwoFingerTapCancelled) {
2893 scoped_ptr<GestureEventConsumeDelegate> delegate(
2894 new GestureEventConsumeDelegate());
2895 const int kWindowWidth = 123;
2896 const int kWindowHeight = 45;
2897 const int kTouchId1 = 2;
2898 const int kTouchId2 = 3;
2901 // Test canceling first finger.
2903 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2904 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2905 delegate.get(), -1234, bounds, root_window()));
2908 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2909 kTouchId1, tes.Now());
2910 DispatchEventUsingWindowDispatcher(&press1);
2913 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2914 kTouchId2, tes.Now());
2915 DispatchEventUsingWindowDispatcher(&press2);
2918 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2919 kTouchId1, tes.Now());
2920 DispatchEventUsingWindowDispatcher(&cancel);
2921 EXPECT_FALSE(delegate->two_finger_tap());
2923 // Make sure there is enough delay before the touch is released so that it
2924 // is recognized as a tap.
2926 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2927 kTouchId2, tes.LeapForward(50));
2929 DispatchEventUsingWindowDispatcher(&release);
2930 EXPECT_FALSE(delegate->two_finger_tap());
2933 // Test canceling second finger
2935 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2936 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2937 delegate.get(), -1234, bounds, root_window()));
2940 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2941 kTouchId1, tes.Now());
2942 DispatchEventUsingWindowDispatcher(&press1);
2945 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2946 kTouchId2, tes.Now());
2947 DispatchEventUsingWindowDispatcher(&press2);
2950 ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2951 kTouchId2, tes.Now());
2952 DispatchEventUsingWindowDispatcher(&cancel);
2953 EXPECT_FALSE(delegate->two_finger_tap());
2955 // Make sure there is enough delay before the touch is released so that it
2956 // is recognized as a tap.
2958 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2959 kTouchId1, tes.LeapForward(50));
2961 DispatchEventUsingWindowDispatcher(&release);
2962 EXPECT_FALSE(delegate->two_finger_tap());
2966 TEST_P(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) {
2967 // Disabled for unified GR due to differences in when scroll update is
2968 // sent. The Aura GR will never send a ScrollUpdate with a ScrollBegin, but
2969 // the unified GR will.
2970 if (UsingUnifiedGR())
2973 scoped_ptr<GestureEventConsumeDelegate> delegate(
2974 new GestureEventConsumeDelegate());
2975 const int kWindowWidth = 523;
2976 const int kWindowHeight = 45;
2977 const int kTouchId1 = 2;
2978 const int kTouchId2 = 3;
2979 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2980 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2981 delegate.get(), -1234, bounds, root_window()));
2985 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2986 kTouchId1, tes.Now());
2987 DispatchEventUsingWindowDispatcher(&press1);
2988 EXPECT_FALSE(delegate->tap());
2989 EXPECT_TRUE(delegate->tap_down());
2990 EXPECT_FALSE(delegate->tap_cancel());
2991 EXPECT_FALSE(delegate->scroll_begin());
2992 EXPECT_FALSE(delegate->scroll_update());
2993 EXPECT_FALSE(delegate->scroll_end());
2994 EXPECT_FALSE(delegate->long_press());
2995 EXPECT_FALSE(delegate->two_finger_tap());
2998 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201),
2999 kTouchId2, tes.Now());
3000 DispatchEventUsingWindowDispatcher(&press2);
3001 EXPECT_FALSE(delegate->tap());
3002 EXPECT_FALSE(delegate->tap_down()); // no touch down for second tap.
3003 EXPECT_TRUE(delegate->tap_cancel());
3004 EXPECT_FALSE(delegate->scroll_begin());
3005 EXPECT_FALSE(delegate->scroll_update());
3006 EXPECT_FALSE(delegate->scroll_end());
3007 EXPECT_FALSE(delegate->long_press());
3008 EXPECT_FALSE(delegate->two_finger_tap());
3009 EXPECT_FALSE(delegate->pinch_begin());
3012 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(530, 301),
3013 kTouchId2, tes.Now());
3014 DispatchEventUsingWindowDispatcher(&move2);
3015 EXPECT_FALSE(delegate->tap());
3016 EXPECT_FALSE(delegate->tap_down());
3017 EXPECT_FALSE(delegate->tap_cancel());
3018 // Pinch & Scroll only when there is enough movement.
3019 EXPECT_TRUE(delegate->scroll_begin());
3020 EXPECT_FALSE(delegate->scroll_update());
3021 EXPECT_FALSE(delegate->scroll_end());
3022 EXPECT_FALSE(delegate->long_press());
3023 EXPECT_FALSE(delegate->two_finger_tap());
3024 EXPECT_TRUE(delegate->pinch_begin());
3027 // Verifies if a window is the target of multiple touch-ids and we hide the
3028 // window everything is cleaned up correctly.
3029 TEST_P(GestureRecognizerTest, FlushAllOnHide) {
3030 scoped_ptr<GestureEventConsumeDelegate> delegate(
3031 new GestureEventConsumeDelegate());
3032 gfx::Rect bounds(0, 0, 200, 200);
3033 scoped_ptr<aura::Window> window(
3034 CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
3035 const int kTouchId1 = 8;
3036 const int kTouchId2 = 2;
3039 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
3040 kTouchId1, tes.Now());
3041 DispatchEventUsingWindowDispatcher(&press1);
3042 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
3043 kTouchId2, tes.Now());
3044 DispatchEventUsingWindowDispatcher(&press2);
3047 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1));
3049 ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
3052 TEST_P(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) {
3053 // TODO(tdresser): write a version of this test for the unified GR.
3054 if (UsingUnifiedGR())
3057 scoped_ptr<QueueTouchEventDelegate> delegate(
3058 new QueueTouchEventDelegate(host()->dispatcher()));
3059 const int kTouchId = 2;
3060 gfx::Rect bounds(100, 200, 100, 100);
3061 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3062 delegate.get(), -1234, bounds, root_window()));
3063 delegate->set_window(window.get());
3066 TimerTestGestureRecognizer* gesture_recognizer =
3067 new TimerTestGestureRecognizer();
3068 TimerTestGestureSequence* gesture_sequence =
3069 static_cast<TimerTestGestureSequence*>(
3070 gesture_recognizer->GetGestureSequenceForTesting(window.get()));
3072 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
3075 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3076 kTouchId, tes.Now());
3077 DispatchEventUsingWindowDispatcher(&press);
3078 // Scroll around, to cancel the long press
3079 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3082 delegate->ReceivedAck();
3083 EXPECT_TRUE(delegate->tap_down());
3084 EXPECT_TRUE(gesture_sequence->IsTimerRunning());
3087 delegate->ReceivedAckPreventDefaulted();
3088 EXPECT_FALSE(gesture_sequence->IsTimerRunning());
3089 gesture_sequence->ForceTimeout();
3090 EXPECT_FALSE(delegate->long_press());
3093 // Same as GestureEventConsumeDelegate, but consumes all the touch-move events.
3094 class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate {
3096 ConsumesTouchMovesDelegate() : consume_touch_move_(true) {}
3097 virtual ~ConsumesTouchMovesDelegate() {}
3099 void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; }
3102 virtual void OnTouchEvent(ui::TouchEvent* touch) OVERRIDE {
3103 if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED)
3104 touch->SetHandled();
3106 GestureEventConsumeDelegate::OnTouchEvent(touch);
3109 bool consume_touch_move_;
3111 DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate);
3114 // Same as GestureEventScroll, but tests that the behavior is the same
3115 // even if all the touch-move events are consumed.
3116 TEST_P(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) {
3117 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3118 new ConsumesTouchMovesDelegate());
3119 const int kWindowWidth = 123;
3120 const int kWindowHeight = 45;
3121 const int kTouchId = 5;
3122 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3123 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3124 delegate.get(), -1234, bounds, root_window()));
3128 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3129 kTouchId, tes.Now());
3130 DispatchEventUsingWindowDispatcher(&press);
3131 EXPECT_FALSE(delegate->tap());
3132 EXPECT_TRUE(delegate->tap_down());
3133 EXPECT_FALSE(delegate->tap_cancel());
3134 EXPECT_TRUE(delegate->begin());
3135 EXPECT_FALSE(delegate->scroll_begin());
3136 EXPECT_FALSE(delegate->scroll_update());
3137 EXPECT_FALSE(delegate->scroll_end());
3139 // Move the touch-point enough so that it would normally be considered a
3140 // scroll. But since the touch-moves will be consumed, the scroll should not
3142 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3143 EXPECT_FALSE(delegate->tap());
3144 EXPECT_FALSE(delegate->tap_down());
3145 EXPECT_TRUE(delegate->tap_cancel());
3146 EXPECT_FALSE(delegate->begin());
3147 EXPECT_FALSE(delegate->scroll_begin());
3148 EXPECT_FALSE(delegate->scroll_update());
3149 EXPECT_FALSE(delegate->scroll_end());
3151 // Release the touch back at the start point. This should end without causing
3154 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230),
3155 kTouchId, tes.LeapForward(50));
3156 DispatchEventUsingWindowDispatcher(&release);
3157 EXPECT_FALSE(delegate->tap());
3158 EXPECT_FALSE(delegate->tap_down());
3159 EXPECT_FALSE(delegate->tap_cancel());
3160 EXPECT_FALSE(delegate->begin());
3161 EXPECT_TRUE(delegate->end());
3162 EXPECT_FALSE(delegate->scroll_begin());
3163 EXPECT_FALSE(delegate->scroll_update());
3164 EXPECT_FALSE(delegate->scroll_end());
3167 // Tests the behavior of 2F scroll when all the touch-move events are consumed.
3168 TEST_P(GestureRecognizerTest, GestureEventScrollTwoFingerTouchMoveConsumed) {
3169 // TODO(tdresser): enable this test with unified GR once two finger tap is
3170 // supported. See crbug.com/354396.
3171 if (UsingUnifiedGR())
3174 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3175 new ConsumesTouchMovesDelegate());
3176 const int kWindowWidth = 123;
3177 const int kWindowHeight = 100;
3178 const int kTouchId1 = 2;
3179 const int kTouchId2 = 3;
3182 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3183 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3184 delegate.get(), -1234, bounds, root_window()));
3187 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3188 kTouchId1, tes.Now());
3189 DispatchEventUsingWindowDispatcher(&press1);
3190 tes.SendScrollEvent(event_processor(), 131, 231, kTouchId1, delegate.get());
3192 // First finger touches down and moves.
3193 EXPECT_FALSE(delegate->tap());
3194 EXPECT_FALSE(delegate->scroll_begin());
3195 EXPECT_FALSE(delegate->scroll_update());
3196 EXPECT_FALSE(delegate->scroll_end());
3199 // Second finger touches down and moves.
3200 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
3201 kTouchId2, tes.LeapForward(50));
3202 DispatchEventUsingWindowDispatcher(&press2);
3203 tes.SendScrollEvent(event_processor(), 161, 231, kTouchId2, delegate.get());
3205 // PinchBegin & ScrollBegin were not sent if the touch-move events were
3207 EXPECT_FALSE(delegate->pinch_begin());
3208 EXPECT_FALSE(delegate->scroll_begin());
3210 EXPECT_FALSE(delegate->tap());
3211 EXPECT_FALSE(delegate->two_finger_tap());
3213 // Should be no PinchUpdate & ScrollUpdate.
3214 EXPECT_FALSE(delegate->pinch_update());
3215 EXPECT_FALSE(delegate->pinch_end());
3216 EXPECT_FALSE(delegate->scroll_update());
3217 EXPECT_FALSE(delegate->scroll_end());
3220 // Moves First finger again, no PinchUpdate & ScrollUpdate.
3221 tes.SendScrollEvent(event_processor(), 161, 261, kTouchId1, delegate.get());
3223 EXPECT_FALSE(delegate->pinch_update());
3224 EXPECT_FALSE(delegate->pinch_end());
3225 EXPECT_FALSE(delegate->scroll_update());
3226 EXPECT_FALSE(delegate->scroll_end());
3228 // Stops consuming touch-move.
3229 delegate->set_consume_touch_move(false);
3232 // Making a pinch gesture.
3233 tes.SendScrollEvent(event_processor(), 161, 251, kTouchId1, delegate.get());
3234 // If touch moves are ever consumed, we should not see PinchBegin/Update
3235 // even touch moves become not consumed.
3236 EXPECT_FALSE(delegate->scroll_begin());
3237 EXPECT_FALSE(delegate->scroll_update());
3238 EXPECT_FALSE(delegate->scroll_end());
3240 EXPECT_FALSE(delegate->pinch_begin());
3241 EXPECT_FALSE(delegate->pinch_update());
3242 EXPECT_FALSE(delegate->pinch_end());
3245 tes.SendScrollEvent(event_processor(), 161, 241, kTouchId2, delegate.get());
3246 EXPECT_FALSE(delegate->scroll_begin());
3247 EXPECT_FALSE(delegate->scroll_update());
3248 EXPECT_FALSE(delegate->scroll_end());
3250 EXPECT_FALSE(delegate->pinch_begin());
3251 EXPECT_FALSE(delegate->pinch_update());
3252 EXPECT_FALSE(delegate->pinch_end());
3255 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3256 kTouchId1, tes.Now());
3257 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
3258 kTouchId2, tes.Now());
3259 DispatchEventUsingWindowDispatcher(&release1);
3260 DispatchEventUsingWindowDispatcher(&release2);
3262 EXPECT_FALSE(delegate->tap());
3263 // Touch release is not consumed, so we still see two finger tap.
3264 EXPECT_TRUE(delegate->two_finger_tap());
3266 // Should not see PinchEnd.
3267 EXPECT_TRUE(delegate->scroll_begin());
3268 EXPECT_FALSE(delegate->scroll_update());
3269 EXPECT_FALSE(delegate->scroll_end());
3270 EXPECT_TRUE(delegate->fling());
3272 EXPECT_FALSE(delegate->pinch_begin());
3273 EXPECT_FALSE(delegate->pinch_update());
3274 EXPECT_FALSE(delegate->pinch_end());
3277 // Like as GestureEventTouchMoveConsumed but tests the different behavior
3278 // depending on whether the events were consumed before or after the scroll
3280 TEST_P(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) {
3281 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3282 new ConsumesTouchMovesDelegate());
3283 const int kWindowWidth = 123;
3284 const int kWindowHeight = 45;
3285 const int kTouchId = 5;
3286 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3287 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3288 delegate.get(), -1234, bounds, root_window()));
3292 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3293 kTouchId, tes.Now());
3294 DispatchEventUsingWindowDispatcher(&press);
3295 EXPECT_FALSE(delegate->tap());
3296 EXPECT_TRUE(delegate->tap_down());
3297 EXPECT_FALSE(delegate->tap_cancel());
3298 EXPECT_TRUE(delegate->begin());
3299 EXPECT_FALSE(delegate->scroll_begin());
3300 EXPECT_FALSE(delegate->scroll_update());
3301 EXPECT_FALSE(delegate->scroll_end());
3303 // Move the touch-point enough so that it would normally be considered a
3304 // scroll. But since the touch-moves will be consumed, the scroll should not
3306 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3307 EXPECT_FALSE(delegate->tap());
3308 EXPECT_FALSE(delegate->tap_down());
3309 EXPECT_TRUE(delegate->tap_cancel());
3310 EXPECT_FALSE(delegate->begin());
3311 EXPECT_FALSE(delegate->scroll_begin());
3312 EXPECT_FALSE(delegate->scroll_update());
3313 EXPECT_FALSE(delegate->scroll_end());
3315 // Now, stop consuming touch-move events, and move the touch-point again.
3316 delegate->set_consume_touch_move(false);
3317 tes.SendScrollEvent(event_processor(), 159, 259, kTouchId, delegate.get());
3318 EXPECT_FALSE(delegate->tap());
3319 EXPECT_FALSE(delegate->tap_down());
3320 EXPECT_FALSE(delegate->tap_cancel());
3321 EXPECT_FALSE(delegate->begin());
3322 EXPECT_FALSE(delegate->scroll_begin());
3323 EXPECT_FALSE(delegate->scroll_update());
3324 EXPECT_FALSE(delegate->scroll_end());
3325 // No scroll has occurred, because an early touch move was consumed.
3326 EXPECT_EQ(0, delegate->scroll_x());
3327 EXPECT_EQ(0, delegate->scroll_y());
3328 EXPECT_EQ(gfx::Point(0, 0).ToString(),
3329 delegate->scroll_begin_position().ToString());
3331 // Start consuming touch-move events again.
3332 delegate->set_consume_touch_move(true);
3334 // Move some more to generate a few more scroll updates.
3335 tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
3336 EXPECT_FALSE(delegate->tap());
3337 EXPECT_FALSE(delegate->tap_down());
3338 EXPECT_FALSE(delegate->tap_cancel());
3339 EXPECT_FALSE(delegate->begin());
3340 EXPECT_FALSE(delegate->scroll_begin());
3341 EXPECT_FALSE(delegate->scroll_update());
3342 EXPECT_FALSE(delegate->scroll_end());
3343 EXPECT_EQ(0, delegate->scroll_x());
3344 EXPECT_EQ(0, delegate->scroll_y());
3346 tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
3347 EXPECT_FALSE(delegate->tap());
3348 EXPECT_FALSE(delegate->tap_down());
3349 EXPECT_FALSE(delegate->tap_cancel());
3350 EXPECT_FALSE(delegate->begin());
3351 EXPECT_FALSE(delegate->scroll_begin());
3352 EXPECT_FALSE(delegate->scroll_update());
3353 EXPECT_FALSE(delegate->scroll_end());
3354 EXPECT_EQ(0, delegate->scroll_x());
3355 EXPECT_EQ(0, delegate->scroll_y());
3357 // Release the touch.
3359 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3360 kTouchId, tes.LeapForward(50));
3361 DispatchEventUsingWindowDispatcher(&release);
3362 EXPECT_FALSE(delegate->tap());
3363 EXPECT_FALSE(delegate->tap_down());
3364 EXPECT_FALSE(delegate->tap_cancel());
3365 EXPECT_FALSE(delegate->begin());
3366 EXPECT_TRUE(delegate->end());
3367 EXPECT_FALSE(delegate->scroll_begin());
3368 EXPECT_FALSE(delegate->scroll_update());
3369 EXPECT_FALSE(delegate->scroll_end());
3370 EXPECT_FALSE(delegate->fling());
3373 // Check that appropriate touch events generate double tap gesture events.
3374 TEST_P(GestureRecognizerTest, GestureEventDoubleTap) {
3375 scoped_ptr<GestureEventConsumeDelegate> delegate(
3376 new GestureEventConsumeDelegate());
3377 const int kWindowWidth = 123;
3378 const int kWindowHeight = 45;
3379 const int kTouchId = 2;
3380 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3381 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3382 delegate.get(), -1234, bounds, root_window()));
3385 // First tap (tested in GestureEventTap)
3386 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3387 kTouchId, tes.Now());
3388 DispatchEventUsingWindowDispatcher(&press1);
3389 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3390 kTouchId, tes.LeapForward(50));
3391 DispatchEventUsingWindowDispatcher(&release1);
3395 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3396 kTouchId, tes.LeapForward(200));
3397 DispatchEventUsingWindowDispatcher(&press2);
3398 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3399 kTouchId, tes.LeapForward(50));
3400 DispatchEventUsingWindowDispatcher(&release2);
3402 EXPECT_TRUE(delegate->tap());
3403 EXPECT_TRUE(delegate->tap_down());
3404 EXPECT_FALSE(delegate->tap_cancel());
3405 EXPECT_TRUE(delegate->begin());
3406 EXPECT_TRUE(delegate->end());
3407 EXPECT_FALSE(delegate->scroll_begin());
3408 EXPECT_FALSE(delegate->scroll_update());
3409 EXPECT_FALSE(delegate->scroll_end());
3411 EXPECT_EQ(2, delegate->tap_count());
3414 // Check that appropriate touch events generate triple tap gesture events.
3415 TEST_P(GestureRecognizerTest, GestureEventTripleTap) {
3416 scoped_ptr<GestureEventConsumeDelegate> delegate(
3417 new GestureEventConsumeDelegate());
3418 const int kWindowWidth = 123;
3419 const int kWindowHeight = 45;
3420 const int kTouchId = 2;
3421 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3422 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3423 delegate.get(), -1234, bounds, root_window()));
3426 // First tap (tested in GestureEventTap)
3427 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3428 kTouchId, tes.Now());
3429 DispatchEventUsingWindowDispatcher(&press1);
3430 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3431 kTouchId, tes.LeapForward(50));
3432 DispatchEventUsingWindowDispatcher(&release1);
3434 EXPECT_EQ(1, delegate->tap_count());
3437 // Second tap (tested in GestureEventDoubleTap)
3438 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3439 kTouchId, tes.LeapForward(200));
3440 DispatchEventUsingWindowDispatcher(&press2);
3441 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3442 kTouchId, tes.LeapForward(50));
3443 DispatchEventUsingWindowDispatcher(&release2);
3445 EXPECT_EQ(2, delegate->tap_count());
3449 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(102, 206),
3450 kTouchId, tes.LeapForward(200));
3451 DispatchEventUsingWindowDispatcher(&press3);
3452 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3453 kTouchId, tes.LeapForward(50));
3454 DispatchEventUsingWindowDispatcher(&release3);
3456 // Third, Fourth and Fifth Taps. Taps after the third should have their
3457 // |tap_count| wrap around back to 1.
3458 for (int i = 3; i < 5; ++i) {
3459 ui::TouchEvent press3(ui::ET_TOUCH_PRESSED,
3460 gfx::Point(102, 206),
3462 tes.LeapForward(200));
3463 DispatchEventUsingWindowDispatcher(&press3);
3464 ui::TouchEvent release3(ui::ET_TOUCH_RELEASED,
3465 gfx::Point(102, 206),
3467 tes.LeapForward(50));
3468 DispatchEventUsingWindowDispatcher(&release3);
3470 EXPECT_TRUE(delegate->tap());
3471 EXPECT_TRUE(delegate->tap_down());
3472 EXPECT_FALSE(delegate->tap_cancel());
3473 EXPECT_TRUE(delegate->begin());
3474 EXPECT_TRUE(delegate->end());
3475 EXPECT_FALSE(delegate->scroll_begin());
3476 EXPECT_FALSE(delegate->scroll_update());
3477 EXPECT_FALSE(delegate->scroll_end());
3479 // The behavior for the Aura GR is incorrect.
3480 if (UsingUnifiedGR())
3481 EXPECT_EQ(1 + (i % 3), delegate->tap_count());
3483 EXPECT_EQ(3, delegate->tap_count());
3487 // Check that we don't get a double tap when the two taps are far apart.
3488 TEST_P(GestureRecognizerTest, TwoTapsFarApart) {
3489 scoped_ptr<GestureEventConsumeDelegate> delegate(
3490 new GestureEventConsumeDelegate());
3491 const int kWindowWidth = 123;
3492 const int kWindowHeight = 45;
3493 const int kTouchId = 2;
3494 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3495 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3496 delegate.get(), -1234, bounds, root_window()));
3499 // First tap (tested in GestureEventTap)
3500 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3501 kTouchId, tes.Now());
3502 DispatchEventUsingWindowDispatcher(&press1);
3503 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3504 kTouchId, tes.LeapForward(50));
3505 DispatchEventUsingWindowDispatcher(&release1);
3508 // Second tap, close in time but far in distance
3509 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201),
3510 kTouchId, tes.LeapForward(200));
3511 DispatchEventUsingWindowDispatcher(&press2);
3512 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201),
3513 kTouchId, tes.LeapForward(50));
3514 DispatchEventUsingWindowDispatcher(&release2);
3516 EXPECT_TRUE(delegate->tap());
3517 EXPECT_TRUE(delegate->tap_down());
3518 EXPECT_FALSE(delegate->tap_cancel());
3519 EXPECT_TRUE(delegate->begin());
3520 EXPECT_TRUE(delegate->end());
3521 EXPECT_FALSE(delegate->scroll_begin());
3522 EXPECT_FALSE(delegate->scroll_update());
3523 EXPECT_FALSE(delegate->scroll_end());
3525 EXPECT_EQ(1, delegate->tap_count());
3528 // Check that we don't get a double tap when the two taps have a long enough
3529 // delay in between.
3530 TEST_P(GestureRecognizerTest, TwoTapsWithDelayBetween) {
3531 scoped_ptr<GestureEventConsumeDelegate> delegate(
3532 new GestureEventConsumeDelegate());
3533 const int kWindowWidth = 123;
3534 const int kWindowHeight = 45;
3535 const int kTouchId = 2;
3536 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3537 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3538 delegate.get(), -1234, bounds, root_window()));
3541 // First tap (tested in GestureEventTap)
3542 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3543 kTouchId, tes.Now());
3544 DispatchEventUsingWindowDispatcher(&press1);
3545 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3546 kTouchId, tes.LeapForward(50));
3547 DispatchEventUsingWindowDispatcher(&release1);
3550 // Second tap, close in distance but after some delay
3551 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3552 kTouchId, tes.LeapForward(2000));
3553 DispatchEventUsingWindowDispatcher(&press2);
3554 ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3555 kTouchId, tes.LeapForward(50));
3556 DispatchEventUsingWindowDispatcher(&release2);
3558 EXPECT_TRUE(delegate->tap());
3559 EXPECT_TRUE(delegate->tap_down());
3560 EXPECT_FALSE(delegate->tap_cancel());
3561 EXPECT_TRUE(delegate->begin());
3562 EXPECT_TRUE(delegate->end());
3563 EXPECT_FALSE(delegate->scroll_begin());
3564 EXPECT_FALSE(delegate->scroll_update());
3565 EXPECT_FALSE(delegate->scroll_end());
3567 EXPECT_EQ(1, delegate->tap_count());
3570 // Checks that if the bounding-box of a gesture changes because of change in
3571 // radius of a touch-point, and not because of change in position, then there
3572 // are not gesture events from that.
3573 TEST_P(GestureRecognizerTest, BoundingBoxRadiusChange) {
3574 // TODO(tdresser): enable this test with unified GR when (if?) bounding box
3575 // behavior is unified.
3576 if (UsingUnifiedGR())
3579 scoped_ptr<GestureEventConsumeDelegate> delegate(
3580 new GestureEventConsumeDelegate());
3581 const int kWindowWidth = 234;
3582 const int kWindowHeight = 345;
3583 const int kTouchId = 5, kTouchId2 = 7;
3584 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3585 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3586 delegate.get(), -1234, bounds, root_window()));
3589 ui::TouchEvent press1(
3590 ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, tes.Now());
3591 DispatchEventUsingWindowDispatcher(&press1);
3592 EXPECT_TRUE(delegate->bounding_box().IsEmpty());
3596 ui::TouchEvent press2(
3597 ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2,
3598 tes.LeapForward(400));
3599 press2.set_radius_x(5);
3600 DispatchEventUsingWindowDispatcher(&press2);
3601 EXPECT_FALSE(delegate->pinch_begin());
3602 EXPECT_EQ(gfx::Rect(101, 201, 100, 0).ToString(),
3603 delegate->bounding_box().ToString());
3607 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(141, 201), kTouchId,
3608 tes.LeapForward(40));
3609 DispatchEventUsingWindowDispatcher(&move1);
3610 EXPECT_TRUE(delegate->pinch_begin());
3611 EXPECT_EQ(gfx::Rect(141, 201, 60, 0).ToString(),
3612 delegate->bounding_box().ToString());
3616 // The position doesn't move, but the radius changes.
3617 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 201), kTouchId,
3618 tes.LeapForward(40));
3619 move2.set_radius_x(50);
3620 move2.set_radius_y(60);
3621 DispatchEventUsingWindowDispatcher(&move2);
3622 EXPECT_FALSE(delegate->tap());
3623 EXPECT_FALSE(delegate->tap_cancel());
3624 EXPECT_FALSE(delegate->scroll_update());
3625 EXPECT_FALSE(delegate->pinch_update());
3630 // Checks that slow scrolls deliver the correct deltas.
3631 // In particular, fix for http;//crbug.com/150573.
3632 TEST_P(GestureRecognizerTest, NoDriftInScroll) {
3633 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
3634 ui::GestureConfiguration::set_min_scroll_delta_squared(9);
3635 scoped_ptr<GestureEventConsumeDelegate> delegate(
3636 new GestureEventConsumeDelegate());
3637 const int kWindowWidth = 234;
3638 const int kWindowHeight = 345;
3639 const int kTouchId = 5;
3641 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3642 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3643 delegate.get(), -1234, bounds, root_window()));
3645 ui::TouchEvent press1(
3646 ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, tes.Now());
3647 DispatchEventUsingWindowDispatcher(&press1);
3648 EXPECT_TRUE(delegate->begin());
3652 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId,
3653 tes.LeapForward(40));
3654 DispatchEventUsingWindowDispatcher(&move1);
3655 EXPECT_FALSE(delegate->scroll_begin());
3659 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3660 tes.LeapForward(40));
3661 DispatchEventUsingWindowDispatcher(&move2);
3662 EXPECT_TRUE(delegate->tap_cancel());
3663 EXPECT_TRUE(delegate->scroll_begin());
3664 EXPECT_TRUE(delegate->scroll_update());
3665 // 3 px consumed by touch slop region.
3666 EXPECT_EQ(-1, delegate->scroll_y());
3667 EXPECT_EQ(-4, delegate->scroll_y_hint());
3671 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3672 tes.LeapForward(40));
3673 DispatchEventUsingWindowDispatcher(&move3);
3674 EXPECT_FALSE(delegate->scroll_update());
3678 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId,
3679 tes.LeapForward(40));
3680 DispatchEventUsingWindowDispatcher(&move4);
3681 EXPECT_TRUE(delegate->scroll_update());
3682 EXPECT_EQ(-1, delegate->scroll_y());
3687 // Ensure that move events which are preventDefaulted will cause a tap
3688 // cancel gesture event to be fired if the move would normally cause a
3689 // scroll. See bug http://crbug.com/146397.
3690 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveCanFireTapCancel) {
3691 scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3692 new ConsumesTouchMovesDelegate());
3693 const int kTouchId = 5;
3694 gfx::Rect bounds(100, 200, 123, 45);
3695 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3696 delegate.get(), -1234, bounds, root_window()));
3700 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3701 kTouchId, tes.Now());
3703 delegate->set_consume_touch_move(false);
3704 DispatchEventUsingWindowDispatcher(&press);
3705 delegate->set_consume_touch_move(true);
3707 // Move the touch-point enough so that it would normally be considered a
3708 // scroll. But since the touch-moves will be consumed, the scroll should not
3710 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3711 EXPECT_FALSE(delegate->tap());
3712 EXPECT_FALSE(delegate->tap_down());
3713 EXPECT_TRUE(delegate->tap_cancel());
3714 EXPECT_FALSE(delegate->begin());
3715 EXPECT_FALSE(delegate->scroll_begin());
3716 EXPECT_FALSE(delegate->scroll_update());
3717 EXPECT_FALSE(delegate->scroll_end());
3720 TEST_P(GestureRecognizerTest,
3721 TransferEventDispatchesTouchCancel) {
3722 scoped_ptr<GestureEventConsumeDelegate> delegate(
3723 new GestureEventConsumeDelegate());
3725 const int kWindowWidth = 800;
3726 const int kWindowHeight = 600;
3727 const int kTouchId = 2;
3728 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3729 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3730 delegate.get(), -1234, bounds, root_window()));
3731 scoped_ptr<RemoveOnTouchCancelHandler>
3732 handler(new RemoveOnTouchCancelHandler());
3733 window->AddPreTargetHandler(handler.get());
3735 // Start a gesture sequence on |window|. Then transfer the events to NULL.
3736 // Make sure |window| receives a touch-cancel event.
3738 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3739 kTouchId, tes.Now());
3740 ui::TouchEvent p2(ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), 1, tes.Now());
3741 DispatchEventUsingWindowDispatcher(&press);
3742 DispatchEventUsingWindowDispatcher(&p2);
3743 EXPECT_FALSE(delegate->tap());
3744 EXPECT_TRUE(delegate->tap_down());
3745 EXPECT_TRUE(delegate->tap_cancel());
3746 EXPECT_TRUE(delegate->begin());
3747 EXPECT_EQ(2, handler->touch_pressed_count());
3751 ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get();
3752 EXPECT_EQ(window.get(),
3753 gesture_recognizer->GetTouchLockedTarget(press));
3754 gesture_recognizer->TransferEventsTo(window.get(), NULL);
3756 gesture_recognizer->GetTouchLockedTarget(press));
3757 // The event-handler removes |window| from its parent on the first
3758 // touch-cancel event, so it won't receive the second touch-cancel event.
3759 EXPECT_EQ(1, handler->touch_cancelled_count());
3762 // Check that appropriate touch events generate show press events
3763 TEST_P(GestureRecognizerTest, GestureEventShowPress) {
3764 scoped_ptr<GestureEventConsumeDelegate> delegate(
3765 new GestureEventConsumeDelegate());
3767 const int kWindowWidth = 123;
3768 const int kWindowHeight = 45;
3769 const int kTouchId = 2;
3770 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3771 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3772 delegate.get(), -1234, bounds, root_window()));
3776 TimerTestGestureRecognizer* gesture_recognizer =
3777 new TimerTestGestureRecognizer();
3779 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
3781 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3782 kTouchId, tes.Now());
3783 DispatchEventUsingWindowDispatcher(&press1);
3784 EXPECT_TRUE(delegate->tap_down());
3785 EXPECT_TRUE(delegate->begin());
3786 EXPECT_FALSE(delegate->tap_cancel());
3788 // We haven't pressed long enough for a show press to occur
3789 EXPECT_FALSE(delegate->show_press());
3791 // Wait until the timer runs out
3792 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
3793 EXPECT_TRUE(delegate->show_press());
3794 EXPECT_FALSE(delegate->tap_cancel());
3797 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3798 kTouchId, tes.Now());
3799 DispatchEventUsingWindowDispatcher(&release1);
3800 EXPECT_FALSE(delegate->long_press());
3802 // Note the tap isn't dispatched until the release
3803 EXPECT_FALSE(delegate->tap_cancel());
3804 EXPECT_TRUE(delegate->tap());
3807 // Check that scrolling cancels a show press
3808 TEST_P(GestureRecognizerTest, GestureEventShowPressCancelledByScroll) {
3809 scoped_ptr<GestureEventConsumeDelegate> delegate(
3810 new GestureEventConsumeDelegate());
3812 const int kWindowWidth = 123;
3813 const int kWindowHeight = 45;
3814 const int kTouchId = 6;
3815 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3816 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3817 delegate.get(), -1234, bounds, root_window()));
3821 TimerTestGestureRecognizer* gesture_recognizer =
3822 new TimerTestGestureRecognizer();
3824 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
3826 TimerTestGestureSequence* gesture_sequence =
3827 static_cast<TimerTestGestureSequence*>(
3828 gesture_recognizer->GetGestureSequenceForTesting(window.get()));
3830 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3831 kTouchId, tes.Now());
3832 DispatchEventUsingWindowDispatcher(&press1);
3833 EXPECT_TRUE(delegate->tap_down());
3835 // We haven't pressed long enough for a show press to occur
3836 EXPECT_FALSE(delegate->show_press());
3837 EXPECT_FALSE(delegate->tap_cancel());
3839 // Scroll around, to cancel the show press
3840 tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3841 // Wait until the timer runs out
3842 gesture_sequence->ForceTimeout();
3843 EXPECT_FALSE(delegate->show_press());
3844 EXPECT_TRUE(delegate->tap_cancel());
3847 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3848 kTouchId, tes.LeapForward(10));
3849 DispatchEventUsingWindowDispatcher(&release1);
3850 EXPECT_FALSE(delegate->show_press());
3851 EXPECT_FALSE(delegate->tap_cancel());
3854 // Test that show press events are sent immediately on tap
3855 TEST_P(GestureRecognizerTest, GestureEventShowPressSentOnTap) {
3856 scoped_ptr<GestureEventConsumeDelegate> delegate(
3857 new GestureEventConsumeDelegate());
3859 const int kWindowWidth = 123;
3860 const int kWindowHeight = 45;
3861 const int kTouchId = 6;
3862 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3863 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3864 delegate.get(), -1234, bounds, root_window()));
3868 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3869 kTouchId, tes.Now());
3870 DispatchEventUsingWindowDispatcher(&press1);
3871 EXPECT_TRUE(delegate->tap_down());
3873 // We haven't pressed long enough for a show press to occur
3874 EXPECT_FALSE(delegate->show_press());
3875 EXPECT_FALSE(delegate->tap_cancel());
3878 ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3879 kTouchId, tes.LeapForward(50));
3880 DispatchEventUsingWindowDispatcher(&release1);
3881 EXPECT_TRUE(delegate->show_press());
3882 EXPECT_FALSE(delegate->tap_cancel());
3883 EXPECT_TRUE(delegate->tap());
3886 // Test that consuming the first move touch event prevents a scroll.
3887 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveScrollTest) {
3888 scoped_ptr<QueueTouchEventDelegate> delegate(
3889 new QueueTouchEventDelegate(host()->dispatcher()));
3891 const int kTouchId = 7;
3892 gfx::Rect bounds(0, 0, 1000, 1000);
3893 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3894 delegate.get(), -1234, bounds, root_window()));
3896 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3897 kTouchId, tes.Now());
3898 DispatchEventUsingWindowDispatcher(&press);
3899 delegate->ReceivedAck();
3901 // A touch move within the slop region is never consumed in web contents. The
3902 // unified GR won't prevent scroll if a touch move within the slop region is
3903 // consumed, so make sure this touch move exceeds the slop region.
3904 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(10, 10),
3905 kTouchId, tes.Now());
3906 DispatchEventUsingWindowDispatcher(&move1);
3907 delegate->ReceivedAckPreventDefaulted();
3909 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3910 kTouchId, tes.Now());
3911 DispatchEventUsingWindowDispatcher(&move2);
3912 delegate->ReceivedAck();
3914 EXPECT_FALSE(delegate->scroll_begin());
3915 EXPECT_FALSE(delegate->scroll_update());
3918 // Test that consuming the first touch move event of a touch point doesn't
3919 // prevent pinching once an additional touch has been pressed.
3920 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMovePinchTest) {
3921 // Consuming moves within the touch slop and the the disposition handling of
3922 // pinch events behave differently between the Unified GR and the Aura GR.
3923 if (UsingUnifiedGR())
3926 scoped_ptr<QueueTouchEventDelegate> delegate(
3927 new QueueTouchEventDelegate(host()->dispatcher()));
3929 const int kTouchId1 = 7;
3930 const int kTouchId2 = 4;
3931 gfx::Rect bounds(0, 0, 1000, 1000);
3932 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3933 delegate.get(), -1234, bounds, root_window()));
3935 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3936 kTouchId1, tes.Now());
3937 DispatchEventUsingWindowDispatcher(&press1);
3938 delegate->ReceivedAck();
3940 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
3941 kTouchId1, tes.Now());
3942 DispatchEventUsingWindowDispatcher(&move1);
3943 delegate->ReceivedAckPreventDefaulted();
3945 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3946 kTouchId1, tes.Now());
3947 DispatchEventUsingWindowDispatcher(&move2);
3948 delegate->ReceivedAck();
3950 // We can't scroll, because a move has been consumed.
3951 EXPECT_FALSE(delegate->scroll_begin());
3952 EXPECT_FALSE(delegate->scroll_update());
3953 EXPECT_FALSE(delegate->pinch_begin());
3955 // An additional press will allow us to pinch.
3956 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3957 kTouchId2, tes.Now());
3958 DispatchEventUsingWindowDispatcher(&press2);
3959 delegate->ReceivedAck();
3961 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3962 kTouchId2, tes.Now());
3963 DispatchEventUsingWindowDispatcher(&move3);
3964 delegate->ReceivedAck();
3966 EXPECT_TRUE(delegate->pinch_begin());
3967 EXPECT_FALSE(delegate->pinch_update());
3971 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(40, 40),
3972 kTouchId2, tes.Now());
3973 DispatchEventUsingWindowDispatcher(&move4);
3974 delegate->ReceivedAck();
3976 EXPECT_TRUE(delegate->pinch_update());
3977 EXPECT_EQ(10, delegate->scroll_x());
3978 EXPECT_EQ(10, delegate->scroll_y());
3981 // Test that consuming the first move touch doesn't prevent a tap.
3982 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveTapTest) {
3983 scoped_ptr<QueueTouchEventDelegate> delegate(
3984 new QueueTouchEventDelegate(host()->dispatcher()));
3986 const int kTouchId = 7;
3987 gfx::Rect bounds(0, 0, 1000, 1000);
3988 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3989 delegate.get(), -1234, bounds, root_window()));
3991 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3992 kTouchId, tes.Now());
3993 DispatchEventUsingWindowDispatcher(&press);
3994 delegate->ReceivedAck();
3996 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
3997 kTouchId, tes.Now());
3998 DispatchEventUsingWindowDispatcher(&move);
3999 delegate->ReceivedAckPreventDefaulted();
4001 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(2, 2),
4002 kTouchId, tes.LeapForward(50));
4003 DispatchEventUsingWindowDispatcher(&release);
4004 delegate->ReceivedAck();
4006 EXPECT_TRUE(delegate->tap());
4009 // Test that consuming the first move touch doesn't prevent a long press.
4010 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveLongPressTest) {
4011 scoped_ptr<QueueTouchEventDelegate> delegate(
4012 new QueueTouchEventDelegate(host()->dispatcher()));
4014 const int kWindowWidth = 123;
4015 const int kWindowHeight = 45;
4016 const int kTouchId = 2;
4017 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4018 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4019 delegate.get(), -1234, bounds, root_window()));
4023 TimerTestGestureRecognizer* gesture_recognizer =
4024 new TimerTestGestureRecognizer();
4026 ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
4028 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4029 kTouchId, tes.Now());
4030 DispatchEventUsingWindowDispatcher(&press1);
4031 delegate->ReceivedAck();
4033 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103, 203),
4034 kTouchId, tes.Now());
4035 DispatchEventUsingWindowDispatcher(&move);
4036 delegate->ReceivedAckPreventDefaulted();
4038 // Wait until the timer runs out
4039 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4040 EXPECT_TRUE(delegate->long_press());
4043 // Tests that the deltas are correct when leaving the slop region very slowly.
4044 TEST_P(GestureRecognizerTest, TestExceedingSlopSlowly) {
4045 // Disabled for unified GR due to subtle differences in touch slop handling.
4046 if (UsingUnifiedGR())
4049 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
4050 scoped_ptr<GestureEventConsumeDelegate> delegate(
4051 new GestureEventConsumeDelegate());
4052 const int kWindowWidth = 234;
4053 const int kWindowHeight = 345;
4054 const int kTouchId = 5;
4056 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4057 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4058 delegate.get(), -1234, bounds, root_window()));
4060 ui::TouchEvent press(
4061 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), kTouchId, tes.Now());
4062 DispatchEventUsingWindowDispatcher(&press);
4063 EXPECT_FALSE(delegate->scroll_begin());
4064 EXPECT_FALSE(delegate->scroll_update());
4067 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(11, 10), kTouchId,
4068 tes.LeapForward(40));
4069 DispatchEventUsingWindowDispatcher(&move1);
4070 EXPECT_FALSE(delegate->scroll_begin());
4071 EXPECT_FALSE(delegate->scroll_update());
4072 EXPECT_EQ(0, delegate->scroll_x());
4073 EXPECT_EQ(0, delegate->scroll_x_hint());
4076 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(12, 10), kTouchId,
4077 tes.LeapForward(40));
4078 DispatchEventUsingWindowDispatcher(&move2);
4079 EXPECT_FALSE(delegate->scroll_begin());
4080 EXPECT_FALSE(delegate->scroll_update());
4081 EXPECT_EQ(0, delegate->scroll_x());
4082 EXPECT_EQ(0, delegate->scroll_x_hint());
4086 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(13, 10), kTouchId,
4087 tes.LeapForward(40));
4088 DispatchEventUsingWindowDispatcher(&move3);
4089 EXPECT_TRUE(delegate->scroll_begin());
4090 EXPECT_FALSE(delegate->scroll_update());
4091 EXPECT_EQ(0, delegate->scroll_x());
4092 EXPECT_EQ(3, delegate->scroll_x_hint());
4096 ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(14, 10), kTouchId,
4097 tes.LeapForward(40));
4098 DispatchEventUsingWindowDispatcher(&move4);
4099 EXPECT_FALSE(delegate->scroll_begin());
4100 EXPECT_TRUE(delegate->scroll_update());
4101 EXPECT_EQ(1, delegate->scroll_x());
4102 EXPECT_EQ(0, delegate->scroll_x_hint());
4106 TEST_P(GestureRecognizerTest, ScrollAlternatelyConsumedTest) {
4107 scoped_ptr<QueueTouchEventDelegate> delegate(
4108 new QueueTouchEventDelegate(host()->dispatcher()));
4110 const int kWindowWidth = 3000;
4111 const int kWindowHeight = 3000;
4112 const int kTouchId = 2;
4113 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4114 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4115 delegate.get(), -1234, bounds, root_window()));
4122 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
4123 kTouchId, tes.Now());
4124 DispatchEventUsingWindowDispatcher(&press1);
4125 delegate->ReceivedAck();
4126 EXPECT_FALSE(delegate->scroll_begin());
4127 EXPECT_FALSE(delegate->scroll_update());
4132 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
4133 kTouchId, tes.Now());
4134 DispatchEventUsingWindowDispatcher(&move1);
4135 delegate->ReceivedAck();
4136 EXPECT_TRUE(delegate->scroll_begin());
4137 EXPECT_TRUE(delegate->scroll_update());
4140 for (int i = 0; i < 3; ++i) {
4143 ui::TouchEvent move2(
4144 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
4145 DispatchEventUsingWindowDispatcher(&move2);
4146 delegate->ReceivedAck();
4147 EXPECT_FALSE(delegate->scroll_begin());
4148 EXPECT_TRUE(delegate->scroll_update());
4149 EXPECT_EQ(10, delegate->scroll_x());
4150 EXPECT_EQ(10, delegate->scroll_y());
4155 ui::TouchEvent move3(
4156 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
4157 DispatchEventUsingWindowDispatcher(&move3);
4158 delegate->ReceivedAckPreventDefaulted();
4159 EXPECT_FALSE(delegate->scroll_begin());
4160 EXPECT_FALSE(delegate->scroll_update());
4165 TEST_P(GestureRecognizerTest, PinchAlternatelyConsumedTest) {
4166 // Disabled for unified GR due to differences in when scroll update is
4167 // sent. The Aura GR will never send a ScrollUpdate with a ScrollBegin, but
4168 // the unified GR will.
4169 if (UsingUnifiedGR())
4172 scoped_ptr<QueueTouchEventDelegate> delegate(
4173 new QueueTouchEventDelegate(host()->dispatcher()));
4175 const int kWindowWidth = 3000;
4176 const int kWindowHeight = 3000;
4177 const int kTouchId1 = 5;
4178 const int kTouchId2 = 7;
4179 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4180 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4181 delegate.get(), -1234, bounds, root_window()));
4185 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
4186 kTouchId1, tes.Now());
4187 DispatchEventUsingWindowDispatcher(&press1);
4188 delegate->ReceivedAck();
4189 EXPECT_FALSE(delegate->scroll_begin());
4190 EXPECT_FALSE(delegate->scroll_update());
4196 ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
4197 kTouchId2, tes.Now());
4198 DispatchEventUsingWindowDispatcher(&press2);
4199 delegate->ReceivedAck();
4200 EXPECT_FALSE(delegate->scroll_begin());
4201 EXPECT_FALSE(delegate->scroll_update());
4202 EXPECT_FALSE(delegate->pinch_begin());
4203 EXPECT_FALSE(delegate->pinch_update());
4209 ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
4210 kTouchId2, tes.Now());
4211 DispatchEventUsingWindowDispatcher(&move1);
4212 delegate->ReceivedAck();
4213 EXPECT_TRUE(delegate->scroll_begin());
4214 EXPECT_FALSE(delegate->scroll_update());
4215 EXPECT_TRUE(delegate->pinch_begin());
4216 EXPECT_FALSE(delegate->pinch_update());
4219 const float expected_scales[] = {1.5f, 1.2f, 1.125f};
4221 for (int i = 0; i < 3; ++i) {
4224 ui::TouchEvent move2(
4225 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4226 DispatchEventUsingWindowDispatcher(&move2);
4227 delegate->ReceivedAck();
4228 EXPECT_FALSE(delegate->scroll_begin());
4229 EXPECT_TRUE(delegate->scroll_update());
4230 EXPECT_FALSE(delegate->scroll_end());
4231 EXPECT_FALSE(delegate->pinch_begin());
4232 EXPECT_TRUE(delegate->pinch_update());
4233 EXPECT_FALSE(delegate->pinch_end());
4234 EXPECT_EQ(25, delegate->scroll_x());
4235 EXPECT_EQ(25, delegate->scroll_y());
4236 EXPECT_FLOAT_EQ(expected_scales[i], delegate->scale());
4241 ui::TouchEvent move3(
4242 ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4243 DispatchEventUsingWindowDispatcher(&move3);
4244 delegate->ReceivedAckPreventDefaulted();
4245 EXPECT_FALSE(delegate->scroll_begin());
4246 EXPECT_FALSE(delegate->scroll_update());
4247 EXPECT_FALSE(delegate->scroll_end());
4248 EXPECT_FALSE(delegate->pinch_begin());
4249 EXPECT_FALSE(delegate->pinch_update());
4250 EXPECT_FALSE(delegate->pinch_end());
4255 // Test that touch event flags are passed through to the gesture event.
4256 TEST_P(GestureRecognizerTest, GestureEventFlagsPassedFromTouchEvent) {
4257 scoped_ptr<GestureEventConsumeDelegate> delegate(
4258 new GestureEventConsumeDelegate());
4260 const int kWindowWidth = 123;
4261 const int kWindowHeight = 45;
4262 const int kTouchId = 6;
4263 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4264 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4265 delegate.get(), -1234, bounds, root_window()));
4269 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4270 kTouchId, tes.Now());
4271 DispatchEventUsingWindowDispatcher(&press1);
4272 EXPECT_TRUE(delegate->tap_down());
4274 int default_flags = delegate->flags();
4276 ui::TouchEvent move1(
4277 ui::ET_TOUCH_MOVED, gfx::Point(397, 149), kTouchId, tes.LeapForward(50));
4278 move1.set_flags(992);
4280 DispatchEventUsingWindowDispatcher(&move1);
4281 EXPECT_NE(default_flags, delegate->flags());
4284 // Test that latency info is passed through to the gesture event.
4285 TEST_P(GestureRecognizerTest, LatencyPassedFromTouchEvent) {
4286 scoped_ptr<GestureEventConsumeDelegate> delegate(
4287 new GestureEventConsumeDelegate());
4289 const int kWindowWidth = 123;
4290 const int kWindowHeight = 45;
4291 const int kTouchId = 6;
4293 const base::TimeTicks time_original = base::TimeTicks::FromInternalValue(100);
4294 const base::TimeTicks time_ui = base::TimeTicks::FromInternalValue(200);
4295 const base::TimeTicks time_acked = base::TimeTicks::FromInternalValue(300);
4297 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4298 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4299 delegate.get(), -1234, bounds, root_window()));
4303 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4304 kTouchId, tes.Now());
4306 // Ensure the only components around are the ones we add.
4307 press1.latency()->Clear();
4309 press1.latency()->AddLatencyNumberWithTimestamp(
4310 ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, 0, time_original, 1);
4312 press1.latency()->AddLatencyNumberWithTimestamp(
4313 ui::INPUT_EVENT_LATENCY_UI_COMPONENT, 0, 0, time_ui, 1);
4315 press1.latency()->AddLatencyNumberWithTimestamp(
4316 ui::INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT, 0, 0, time_acked, 1);
4318 DispatchEventUsingWindowDispatcher(&press1);
4319 EXPECT_TRUE(delegate->tap_down());
4321 ui::LatencyInfo::LatencyComponent component;
4323 EXPECT_EQ(3U, delegate->latency_info().latency_components.size());
4324 ASSERT_TRUE(delegate->latency_info().FindLatency(
4325 ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, &component));
4326 EXPECT_EQ(time_original, component.event_time);
4328 ASSERT_TRUE(delegate->latency_info().FindLatency(
4329 ui::INPUT_EVENT_LATENCY_UI_COMPONENT, 0, &component));
4330 EXPECT_EQ(time_ui, component.event_time);
4332 ASSERT_TRUE(delegate->latency_info().FindLatency(
4333 ui::INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT, 0, &component));
4334 EXPECT_EQ(time_acked, component.event_time);
4336 delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
4337 EXPECT_TRUE(delegate->show_press());
4338 EXPECT_EQ(0U, delegate->latency_info().latency_components.size());
4341 // A delegate that deletes a window on long press.
4342 class GestureEventDeleteWindowOnLongPress : public GestureEventConsumeDelegate {
4344 GestureEventDeleteWindowOnLongPress()
4347 void set_window(aura::Window** window) { window_ = window; }
4349 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
4350 GestureEventConsumeDelegate::OnGestureEvent(gesture);
4351 if (gesture->type() != ui::ET_GESTURE_LONG_PRESS)
4353 ui::GestureRecognizer::Get()->CleanupStateForConsumer(*window_);
4359 aura::Window** window_;
4360 DISALLOW_COPY_AND_ASSIGN(GestureEventDeleteWindowOnLongPress);
4363 // Check that deleting the window in response to a long press gesture doesn't
4365 TEST_P(GestureRecognizerTest, GestureEventLongPressDeletingWindow) {
4366 GestureEventDeleteWindowOnLongPress delegate;
4367 const int kWindowWidth = 123;
4368 const int kWindowHeight = 45;
4369 const int kTouchId = 2;
4370 gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4371 aura::Window* window(CreateTestWindowWithDelegate(
4372 &delegate, -1234, bounds, root_window()));
4373 delegate.set_window(&window);
4375 ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
4376 gfx::Point(101, 201),
4378 ui::EventTimeForNow());
4379 DispatchEventUsingWindowDispatcher(&press1);
4380 EXPECT_TRUE(window != NULL);
4382 // Wait until the timer runs out.
4383 delegate.WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4384 EXPECT_EQ(NULL, window);
4387 INSTANTIATE_TEST_CASE_P(GestureRecognizer,
4388 GestureRecognizerTest,