Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / aura / gestures / gesture_recognizer_unittest.cc
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.
4
5 #include "base/command_line.h"
6 #include "base/memory/scoped_vector.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/timer/timer.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/test/aura_test_base.h"
13 #include "ui/aura/test/test_window_delegate.h"
14 #include "ui/aura/test/test_windows.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/base/hit_test.h"
18 #include "ui/base/ui_base_switches.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_switches.h"
21 #include "ui/events/event_utils.h"
22 #include "ui/events/gestures/gesture_configuration.h"
23 #include "ui/events/gestures/gesture_recognizer_impl.h"
24 #include "ui/events/gestures/gesture_sequence.h"
25 #include "ui/events/gestures/gesture_types.h"
26 #include "ui/events/test/event_generator.h"
27 #include "ui/events/test/events_test_utils.h"
28 #include "ui/gfx/point.h"
29 #include "ui/gfx/rect.h"
30
31 #include <queue>
32
33 namespace aura {
34 namespace test {
35
36 namespace {
37
38 std::string WindowIDAsString(ui::GestureConsumer* consumer) {
39   return consumer ?
40       base::IntToString(static_cast<Window*>(consumer)->id()) : "?";
41 }
42
43 #define EXPECT_0_EVENTS(events) \
44     EXPECT_EQ(0u, events.size())
45
46 #define EXPECT_1_EVENT(events, e0) \
47     EXPECT_EQ(1u, events.size()); \
48     EXPECT_EQ(e0, events[0])
49
50 #define EXPECT_2_EVENTS(events, e0, e1) \
51     EXPECT_EQ(2u, events.size()); \
52     EXPECT_EQ(e0, events[0]); \
53     EXPECT_EQ(e1, events[1])
54
55 #define EXPECT_3_EVENTS(events, e0, e1, e2) \
56     EXPECT_EQ(3u, events.size()); \
57     EXPECT_EQ(e0, events[0]); \
58     EXPECT_EQ(e1, events[1]); \
59     EXPECT_EQ(e2, events[2])
60
61 #define EXPECT_4_EVENTS(events, e0, e1, e2, e3) \
62     EXPECT_EQ(4u, events.size()); \
63     EXPECT_EQ(e0, events[0]); \
64     EXPECT_EQ(e1, events[1]); \
65     EXPECT_EQ(e2, events[2]); \
66     EXPECT_EQ(e3, events[3])
67
68 // A delegate that keeps track of gesture events.
69 class GestureEventConsumeDelegate : public TestWindowDelegate {
70  public:
71   GestureEventConsumeDelegate()
72       : tap_(false),
73         tap_down_(false),
74         tap_cancel_(false),
75         begin_(false),
76         end_(false),
77         scroll_begin_(false),
78         scroll_update_(false),
79         scroll_end_(false),
80         pinch_begin_(false),
81         pinch_update_(false),
82         pinch_end_(false),
83         long_press_(false),
84         fling_(false),
85         two_finger_tap_(false),
86         show_press_(false),
87         swipe_left_(false),
88         swipe_right_(false),
89         swipe_up_(false),
90         swipe_down_(false),
91         scroll_x_(0),
92         scroll_y_(0),
93         scroll_velocity_x_(0),
94         scroll_velocity_y_(0),
95         velocity_x_(0),
96         velocity_y_(0),
97         scroll_x_hint_(0),
98         scroll_y_hint_(0),
99         tap_count_(0),
100         flags_(0),
101         wait_until_event_(ui::ET_UNKNOWN) {}
102
103   virtual ~GestureEventConsumeDelegate() {}
104
105   void Reset() {
106     events_.clear();
107     tap_ = false;
108     tap_down_ = false;
109     tap_cancel_ = false;
110     begin_ = false;
111     end_ = false;
112     scroll_begin_ = false;
113     scroll_update_ = false;
114     scroll_end_ = false;
115     pinch_begin_ = false;
116     pinch_update_ = false;
117     pinch_end_ = false;
118     long_press_ = false;
119     fling_ = false;
120     two_finger_tap_ = false;
121     show_press_ = false;
122     swipe_left_ = false;
123     swipe_right_ = false;
124     swipe_up_ = false;
125     swipe_down_ = false;
126
127     scroll_begin_position_.SetPoint(0, 0);
128     tap_location_.SetPoint(0, 0);
129     gesture_end_location_.SetPoint(0, 0);
130
131     scroll_x_ = 0;
132     scroll_y_ = 0;
133     scroll_velocity_x_ = 0;
134     scroll_velocity_y_ = 0;
135     velocity_x_ = 0;
136     velocity_y_ = 0;
137     scroll_x_hint_ = 0;
138     scroll_y_hint_ = 0;
139     tap_count_ = 0;
140     scale_ = 0;
141     flags_ = 0;
142     latency_info_.Clear();
143   }
144
145   const std::vector<ui::EventType>& events() const { return events_; };
146
147   bool tap() const { return tap_; }
148   bool tap_down() const { return tap_down_; }
149   bool tap_cancel() const { return tap_cancel_; }
150   bool begin() const { return begin_; }
151   bool end() const { return end_; }
152   bool scroll_begin() const { return scroll_begin_; }
153   bool scroll_update() const { return scroll_update_; }
154   bool scroll_end() const { return scroll_end_; }
155   bool pinch_begin() const { return pinch_begin_; }
156   bool pinch_update() const { return pinch_update_; }
157   bool pinch_end() const { return pinch_end_; }
158   bool long_press() const { return long_press_; }
159   bool long_tap() const { return long_tap_; }
160   bool fling() const { return fling_; }
161   bool two_finger_tap() const { return two_finger_tap_; }
162   bool show_press() const { return show_press_; }
163   bool swipe_left() const { return swipe_left_; }
164   bool swipe_right() const { return swipe_right_; }
165   bool swipe_up() const { return swipe_up_; }
166   bool swipe_down() const { return swipe_down_; }
167
168   const gfx::Point& scroll_begin_position() const {
169     return scroll_begin_position_;
170   }
171
172   const gfx::Point& tap_location() const {
173     return tap_location_;
174   }
175
176   const gfx::Point& gesture_end_location() const {
177     return gesture_end_location_;
178   }
179
180   float scroll_x() const { return scroll_x_; }
181   float scroll_y() const { return scroll_y_; }
182   float scroll_velocity_x() const { return scroll_velocity_x_; }
183   float scroll_velocity_y() const { return scroll_velocity_y_; }
184   float velocity_x() const { return velocity_x_; }
185   float velocity_y() const { return velocity_y_; }
186   float scroll_x_hint() const { return scroll_x_hint_; }
187   float scroll_y_hint() const { return scroll_y_hint_; }
188   float scale() const { return scale_; }
189   const gfx::Rect& bounding_box() const { return bounding_box_; }
190   int tap_count() const { return tap_count_; }
191   int flags() const { return flags_; }
192   const ui::LatencyInfo& latency_info() const { return latency_info_; }
193
194   void WaitUntilReceivedGesture(ui::EventType type) {
195     wait_until_event_ = type;
196     run_loop_.reset(new base::RunLoop());
197     run_loop_->Run();
198   }
199
200   virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
201     events_.push_back(gesture->type());
202     bounding_box_ = gesture->details().bounding_box();
203     flags_ = gesture->flags();
204     latency_info_ = *gesture->latency();
205     switch (gesture->type()) {
206       case ui::ET_GESTURE_TAP:
207         tap_location_ = gesture->location();
208         tap_count_ = gesture->details().tap_count();
209         tap_ = true;
210         break;
211       case ui::ET_GESTURE_TAP_DOWN:
212         tap_down_ = true;
213         break;
214       case ui::ET_GESTURE_TAP_CANCEL:
215         tap_cancel_ = true;
216         break;
217       case ui::ET_GESTURE_BEGIN:
218         begin_ = true;
219         break;
220       case ui::ET_GESTURE_END:
221         end_ = true;
222         gesture_end_location_ = gesture->location();
223         break;
224       case ui::ET_GESTURE_SCROLL_BEGIN:
225         scroll_begin_ = true;
226         scroll_begin_position_ = gesture->location();
227         scroll_x_hint_ = gesture->details().scroll_x_hint();
228         scroll_y_hint_ = gesture->details().scroll_y_hint();
229         break;
230       case ui::ET_GESTURE_SCROLL_UPDATE:
231         scroll_update_ = true;
232         scroll_x_ += gesture->details().scroll_x();
233         scroll_y_ += gesture->details().scroll_y();
234         break;
235       case ui::ET_GESTURE_SCROLL_END:
236         EXPECT_TRUE(velocity_x_ == 0 && velocity_y_ == 0);
237         scroll_end_ = true;
238         break;
239       case ui::ET_GESTURE_PINCH_BEGIN:
240         pinch_begin_ = true;
241         break;
242       case ui::ET_GESTURE_PINCH_UPDATE:
243         pinch_update_ = true;
244         scale_ = gesture->details().scale();
245         break;
246       case ui::ET_GESTURE_PINCH_END:
247         pinch_end_ = true;
248         break;
249       case ui::ET_GESTURE_LONG_PRESS:
250         long_press_ = true;
251         break;
252       case ui::ET_GESTURE_LONG_TAP:
253         long_tap_ = true;
254         break;
255       case ui::ET_SCROLL_FLING_START:
256         EXPECT_TRUE(gesture->details().velocity_x() != 0 ||
257                     gesture->details().velocity_y() != 0);
258         EXPECT_FALSE(scroll_end_);
259         fling_ = true;
260         velocity_x_ = gesture->details().velocity_x();
261         velocity_y_ = gesture->details().velocity_y();
262         break;
263       case ui::ET_GESTURE_TWO_FINGER_TAP:
264         two_finger_tap_ = true;
265         break;
266       case ui::ET_GESTURE_SHOW_PRESS:
267         show_press_ = true;
268         break;
269       case ui::ET_GESTURE_SWIPE:
270         swipe_left_ = gesture->details().swipe_left();
271         swipe_right_ = gesture->details().swipe_right();
272         swipe_up_ = gesture->details().swipe_up();
273         swipe_down_ = gesture->details().swipe_down();
274         break;
275       case ui::ET_SCROLL_FLING_CANCEL:
276         // Only used in unified gesture detection.
277         break;
278       default:
279         NOTREACHED();
280     }
281     if (wait_until_event_ == gesture->type() && run_loop_) {
282       run_loop_->Quit();
283       wait_until_event_ = ui::ET_UNKNOWN;
284     }
285     gesture->StopPropagation();
286   }
287
288  private:
289   scoped_ptr<base::RunLoop> run_loop_;
290   std::vector<ui::EventType> events_;
291
292   bool tap_;
293   bool tap_down_;
294   bool tap_cancel_;
295   bool begin_;
296   bool end_;
297   bool scroll_begin_;
298   bool scroll_update_;
299   bool scroll_end_;
300   bool pinch_begin_;
301   bool pinch_update_;
302   bool pinch_end_;
303   bool long_press_;
304   bool long_tap_;
305   bool fling_;
306   bool two_finger_tap_;
307   bool show_press_;
308   bool swipe_left_;
309   bool swipe_right_;
310   bool swipe_up_;
311   bool swipe_down_;
312
313   gfx::Point scroll_begin_position_;
314   gfx::Point tap_location_;
315   gfx::Point gesture_end_location_;
316
317   float scroll_x_;
318   float scroll_y_;
319   float scroll_velocity_x_;
320   float scroll_velocity_y_;
321   float velocity_x_;
322   float velocity_y_;
323   float scroll_x_hint_;
324   float scroll_y_hint_;
325   float scale_;
326   gfx::Rect bounding_box_;
327   int tap_count_;
328   int flags_;
329   ui::LatencyInfo latency_info_;
330
331   ui::EventType wait_until_event_;
332
333   DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate);
334 };
335
336 class QueueTouchEventDelegate : public GestureEventConsumeDelegate {
337  public:
338   explicit QueueTouchEventDelegate(WindowEventDispatcher* dispatcher)
339       : window_(NULL),
340         dispatcher_(dispatcher),
341         queue_events_(true) {
342   }
343   virtual ~QueueTouchEventDelegate() {
344     while(!queue_.empty()) {
345       delete queue_.front();
346       queue_.pop();
347     }
348   }
349
350   virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
351     if (queue_events_) {
352       queue_.push(new ui::TouchEvent(*event, window_, window_));
353       event->StopPropagation();
354     }
355   }
356
357   void ReceivedAck() {
358     ReceivedAckImpl(false);
359   }
360
361   void ReceivedAckPreventDefaulted() {
362     ReceivedAckImpl(true);
363   }
364
365   void set_window(Window* w) { window_ = w; }
366   void set_queue_events(bool queue) { queue_events_ = queue; }
367
368  private:
369   void ReceivedAckImpl(bool prevent_defaulted) {
370     scoped_ptr<ui::TouchEvent> event(queue_.front());
371     dispatcher_->ProcessedTouchEvent(event.get(), window_,
372         prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED);
373     queue_.pop();
374   }
375
376   std::queue<ui::TouchEvent*> queue_;
377   Window* window_;
378   WindowEventDispatcher* dispatcher_;
379   bool queue_events_;
380
381   DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate);
382 };
383
384 // A delegate that ignores gesture events but keeps track of [synthetic] mouse
385 // events.
386 class GestureEventSynthDelegate : public TestWindowDelegate {
387  public:
388   GestureEventSynthDelegate()
389       : mouse_enter_(false),
390         mouse_exit_(false),
391         mouse_press_(false),
392         mouse_release_(false),
393         mouse_move_(false),
394         double_click_(false) {
395   }
396
397   void Reset() {
398     mouse_enter_ = false;
399     mouse_exit_ = false;
400     mouse_press_ = false;
401     mouse_release_ = false;
402     mouse_move_ = false;
403     double_click_ = false;
404   }
405
406   bool mouse_enter() const { return mouse_enter_; }
407   bool mouse_exit() const { return mouse_exit_; }
408   bool mouse_press() const { return mouse_press_; }
409   bool mouse_move() const { return mouse_move_; }
410   bool mouse_release() const { return mouse_release_; }
411   bool double_click() const { return double_click_; }
412
413   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
414     switch (event->type()) {
415       case ui::ET_MOUSE_PRESSED:
416         double_click_ = event->flags() & ui::EF_IS_DOUBLE_CLICK;
417         mouse_press_ = true;
418         break;
419       case ui::ET_MOUSE_RELEASED:
420         mouse_release_ = true;
421         break;
422       case ui::ET_MOUSE_MOVED:
423         mouse_move_ = true;
424         break;
425       case ui::ET_MOUSE_ENTERED:
426         mouse_enter_ = true;
427         break;
428       case ui::ET_MOUSE_EXITED:
429         mouse_exit_ = true;
430         break;
431       default:
432         NOTREACHED();
433     }
434     event->SetHandled();
435   }
436
437  private:
438   bool mouse_enter_;
439   bool mouse_exit_;
440   bool mouse_press_;
441   bool mouse_release_;
442   bool mouse_move_;
443   bool double_click_;
444
445   DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate);
446 };
447
448 class TestOneShotGestureSequenceTimer
449     : public base::OneShotTimer<ui::GestureSequence> {
450  public:
451   TestOneShotGestureSequenceTimer() {}
452
453   void ForceTimeout() {
454     if (IsRunning()) {
455       user_task().Run();
456       Stop();
457     }
458   }
459
460  private:
461   DISALLOW_COPY_AND_ASSIGN(TestOneShotGestureSequenceTimer);
462 };
463
464 class TimerTestGestureSequence : public ui::GestureSequence {
465  public:
466   explicit TimerTestGestureSequence(ui::GestureSequenceDelegate* delegate)
467       : ui::GestureSequence(delegate) {
468   }
469
470   void ForceTimeout() {
471     static_cast<TestOneShotGestureSequenceTimer*>(
472         GetLongPressTimer())->ForceTimeout();
473   }
474
475   bool IsTimerRunning() {
476     return GetLongPressTimer()->IsRunning();
477   }
478
479   virtual base::OneShotTimer<ui::GestureSequence>* CreateTimer() OVERRIDE {
480     return new TestOneShotGestureSequenceTimer();
481   }
482
483  private:
484   DISALLOW_COPY_AND_ASSIGN(TimerTestGestureSequence);
485 };
486
487 class TestGestureRecognizer : public ui::GestureRecognizerImpl {
488  public:
489   TestGestureRecognizer() : GestureRecognizerImpl() {
490   }
491
492   ui::GestureSequence* GetGestureSequenceForTesting(Window* window) {
493     return GetGestureSequenceForConsumer(window);
494   }
495
496  private:
497   DISALLOW_COPY_AND_ASSIGN(TestGestureRecognizer);
498 };
499
500 class TimerTestGestureRecognizer : public TestGestureRecognizer {
501  public:
502   TimerTestGestureRecognizer() : TestGestureRecognizer() {
503   }
504
505   virtual ui::GestureSequence* CreateSequence(
506       ui::GestureSequenceDelegate* delegate) OVERRIDE {
507     return new TimerTestGestureSequence(delegate);
508   }
509
510  private:
511   DISALLOW_COPY_AND_ASSIGN(TimerTestGestureRecognizer);
512 };
513
514 base::TimeDelta GetTime() {
515   return ui::EventTimeForNow();
516 }
517
518 class ScopedGestureRecognizerSetter {
519  public:
520   // Takes ownership of |new_gr|.
521   explicit ScopedGestureRecognizerSetter(ui::GestureRecognizer* new_gr)
522       : new_gr_(new_gr) {
523     original_gr_ = ui::GestureRecognizer::Get();
524     ui::SetGestureRecognizerForTesting(new_gr_.get());
525   }
526
527   virtual ~ScopedGestureRecognizerSetter() {
528     ui::SetGestureRecognizerForTesting(original_gr_);
529   }
530
531  private:
532   ui::GestureRecognizer* original_gr_;
533   scoped_ptr<ui::GestureRecognizer> new_gr_;
534
535   DISALLOW_COPY_AND_ASSIGN(ScopedGestureRecognizerSetter);
536 };
537
538 class TimedEvents {
539  private:
540   int simulated_now_;
541
542  public:
543   // Use a non-zero start time to pass DCHECKs which ensure events have had a
544   // time assigned.
545   TimedEvents() : simulated_now_(1) {
546   }
547
548   base::TimeDelta Now() {
549     base::TimeDelta t = base::TimeDelta::FromMilliseconds(simulated_now_);
550     simulated_now_++;
551     return t;
552   }
553
554   base::TimeDelta LeapForward(int time_in_millis) {
555     simulated_now_ += time_in_millis;
556     return base::TimeDelta::FromMilliseconds(simulated_now_);
557   }
558
559   base::TimeDelta InFuture(int time_in_millis) {
560     return base::TimeDelta::FromMilliseconds(simulated_now_ + time_in_millis);
561   }
562
563   void SendScrollEvents(ui::EventProcessor* dispatcher,
564                         float x_start,
565                         float y_start,
566                         int dx,
567                         int dy,
568                         int touch_id,
569                         int time_step,
570                         int num_steps,
571                         GestureEventConsumeDelegate* delegate) {
572     int x = x_start;
573     int y = y_start;
574
575     for (int i = 0; i < num_steps; i++) {
576       x += dx;
577       y += dy;
578       ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
579                           touch_id,
580                           base::TimeDelta::FromMilliseconds(simulated_now_));
581       ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
582       ASSERT_FALSE(details.dispatcher_destroyed);
583       simulated_now_ += time_step;
584     }
585   }
586
587   void SendScrollEvent(ui::EventProcessor* dispatcher,
588                        float x,
589                        float y,
590                        int touch_id,
591                        GestureEventConsumeDelegate* delegate) {
592     delegate->Reset();
593     ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::PointF(x, y),
594                         touch_id,
595                         base::TimeDelta::FromMilliseconds(simulated_now_));
596     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move);
597     ASSERT_FALSE(details.dispatcher_destroyed);
598     simulated_now_++;
599   }
600 };
601
602 // An event handler to keep track of events.
603 class TestEventHandler : public ui::EventHandler {
604  public:
605   TestEventHandler() : touch_released_count_(0),
606                        touch_pressed_count_(0),
607                        touch_moved_count_(0),
608                        touch_cancelled_count_(0) {
609   }
610
611   virtual ~TestEventHandler() {}
612
613   virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
614     switch (event->type()) {
615       case ui::ET_TOUCH_RELEASED:
616         touch_released_count_++;
617         break;
618       case ui::ET_TOUCH_PRESSED:
619         touch_pressed_count_++;
620         break;
621       case ui::ET_TOUCH_MOVED:
622         touch_moved_count_++;
623         break;
624       case ui::ET_TOUCH_CANCELLED:
625         touch_cancelled_count_++;
626         break;
627       default:
628         break;
629     }
630   }
631
632   void Reset() {
633     touch_released_count_ = 0;
634     touch_pressed_count_ = 0;
635     touch_moved_count_ = 0;
636     touch_cancelled_count_ = 0;
637   }
638
639   int touch_released_count() const { return touch_released_count_; }
640   int touch_pressed_count() const { return touch_pressed_count_; }
641   int touch_moved_count() const { return touch_moved_count_; }
642   int touch_cancelled_count() const { return touch_cancelled_count_; }
643
644  private:
645   int touch_released_count_;
646   int touch_pressed_count_;
647   int touch_moved_count_;
648   int touch_cancelled_count_;
649
650   DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
651 };
652
653 // Removes the target window from its parent when it receives a touch-cancel
654 // event.
655 class RemoveOnTouchCancelHandler : public TestEventHandler {
656  public:
657   RemoveOnTouchCancelHandler() {}
658   virtual ~RemoveOnTouchCancelHandler() {}
659
660  private:
661   // ui::EventHandler:
662   virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
663     TestEventHandler::OnTouchEvent(event);
664     if (event->type() == ui::ET_TOUCH_CANCELLED) {
665       Window* target = static_cast<Window*>(event->target());
666       // This is tiptoeing around crbug.com/310172. If this event handler isn't
667       // removed, we enter an infinite loop.
668       target->RemovePreTargetHandler(this);
669       target->parent()->RemoveChild(target);
670     }
671   }
672
673   DISALLOW_COPY_AND_ASSIGN(RemoveOnTouchCancelHandler);
674 };
675
676 }  // namespace
677
678 class GestureRecognizerTest : public AuraTestBase,
679                               public ::testing::WithParamInterface<bool> {
680  public:
681   GestureRecognizerTest() {}
682
683   bool UsingUnifiedGR() {
684     return GetParam();
685   }
686
687   virtual void SetUp() OVERRIDE {
688     // TODO(tdresser): Once unified GR has landed, only run these tests once.
689     CommandLine::ForCurrentProcess()->AppendSwitchASCII(
690         switches::kUnifiedGestureDetector,
691         UsingUnifiedGR() ? switches::kUnifiedGestureDetectorEnabled
692                          : switches::kUnifiedGestureDetectorDisabled);
693
694     AuraTestBase::SetUp();
695     ui::GestureConfiguration::set_min_touch_down_duration_in_seconds_for_click(
696         0.001);
697     ui::GestureConfiguration::set_show_press_delay_in_ms(2);
698     ui::GestureConfiguration::set_long_press_time_in_seconds(0.003);
699   }
700
701   DISALLOW_COPY_AND_ASSIGN(GestureRecognizerTest);
702 };
703
704 // Check that appropriate touch events generate tap gesture events.
705 TEST_P(GestureRecognizerTest, GestureEventTap) {
706   scoped_ptr<GestureEventConsumeDelegate> delegate(
707       new GestureEventConsumeDelegate());
708   TimedEvents tes;
709   const int kWindowWidth = 123;
710   const int kWindowHeight = 45;
711   const int kTouchId = 2;
712   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
713   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
714       delegate.get(), -1234, bounds, root_window()));
715
716   delegate->Reset();
717   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
718                        kTouchId, tes.Now());
719   DispatchEventUsingWindowDispatcher(&press);
720   EXPECT_FALSE(delegate->tap());
721   EXPECT_FALSE(delegate->show_press());
722   EXPECT_TRUE(delegate->tap_down());
723   EXPECT_FALSE(delegate->tap_cancel());
724   EXPECT_TRUE(delegate->begin());
725   EXPECT_FALSE(delegate->scroll_begin());
726   EXPECT_FALSE(delegate->scroll_update());
727   EXPECT_FALSE(delegate->scroll_end());
728   EXPECT_FALSE(delegate->long_press());
729
730   delegate->Reset();
731   delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
732   EXPECT_TRUE(delegate->show_press());
733   EXPECT_FALSE(delegate->tap_down());
734
735   // Make sure there is enough delay before the touch is released so that it is
736   // recognized as a tap.
737   delegate->Reset();
738   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
739                          kTouchId, tes.LeapForward(50));
740
741   DispatchEventUsingWindowDispatcher(&release);
742   EXPECT_TRUE(delegate->tap());
743   EXPECT_FALSE(delegate->tap_down());
744   EXPECT_FALSE(delegate->tap_cancel());
745   EXPECT_FALSE(delegate->begin());
746   EXPECT_TRUE(delegate->end());
747   EXPECT_FALSE(delegate->scroll_begin());
748   EXPECT_FALSE(delegate->scroll_update());
749   EXPECT_FALSE(delegate->scroll_end());
750
751   EXPECT_EQ(1, delegate->tap_count());
752 }
753
754 // Check that appropriate touch events generate tap gesture events
755 // when information about the touch radii are provided.
756 TEST_P(GestureRecognizerTest, GestureEventTapRegion) {
757   // TODO(tdresser): enable this test with unified GR once we resolve the
758   // bounding box differences. See crbug.com/366641.
759   if (UsingUnifiedGR())
760     return;
761
762   scoped_ptr<GestureEventConsumeDelegate> delegate(
763       new GestureEventConsumeDelegate());
764   TimedEvents tes;
765   const int kWindowWidth = 800;
766   const int kWindowHeight = 600;
767   const int kTouchId = 2;
768   gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
769   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
770       delegate.get(), -1234, bounds, root_window()));
771
772   // Test with no ET_TOUCH_MOVED events.
773   {
774      delegate->Reset();
775      ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
776                           kTouchId, tes.Now());
777      press.set_radius_x(5);
778      press.set_radius_y(12);
779      DispatchEventUsingWindowDispatcher(&press);
780      EXPECT_FALSE(delegate->tap());
781      EXPECT_TRUE(delegate->tap_down());
782      EXPECT_FALSE(delegate->tap_cancel());
783      EXPECT_TRUE(delegate->begin());
784      EXPECT_FALSE(delegate->scroll_begin());
785      EXPECT_FALSE(delegate->scroll_update());
786      EXPECT_FALSE(delegate->scroll_end());
787      EXPECT_FALSE(delegate->long_press());
788
789      // Make sure there is enough delay before the touch is released so that it
790      // is recognized as a tap.
791      delegate->Reset();
792      ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
793                             kTouchId, tes.LeapForward(50));
794      release.set_radius_x(5);
795      release.set_radius_y(12);
796
797      DispatchEventUsingWindowDispatcher(&release);
798      EXPECT_TRUE(delegate->tap());
799      EXPECT_FALSE(delegate->tap_down());
800      EXPECT_FALSE(delegate->tap_cancel());
801      EXPECT_FALSE(delegate->begin());
802      EXPECT_TRUE(delegate->end());
803      EXPECT_FALSE(delegate->scroll_begin());
804      EXPECT_FALSE(delegate->scroll_update());
805      EXPECT_FALSE(delegate->scroll_end());
806
807      EXPECT_EQ(1, delegate->tap_count());
808      gfx::Point actual_point(delegate->tap_location());
809      EXPECT_EQ(24, delegate->bounding_box().width());
810      EXPECT_EQ(24, delegate->bounding_box().height());
811      EXPECT_EQ(101, actual_point.x());
812      EXPECT_EQ(201, actual_point.y());
813   }
814
815   // Test with no ET_TOUCH_MOVED events but different touch points and radii.
816   {
817      delegate->Reset();
818      ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(365, 290),
819                           kTouchId, tes.Now());
820      press.set_radius_x(8);
821      press.set_radius_y(14);
822      DispatchEventUsingWindowDispatcher(&press);
823      EXPECT_FALSE(delegate->tap());
824      EXPECT_TRUE(delegate->tap_down());
825      EXPECT_FALSE(delegate->tap_cancel());
826      EXPECT_TRUE(delegate->begin());
827      EXPECT_FALSE(delegate->scroll_begin());
828      EXPECT_FALSE(delegate->scroll_update());
829      EXPECT_FALSE(delegate->scroll_end());
830      EXPECT_FALSE(delegate->long_press());
831
832      delegate->Reset();
833      ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(367, 291),
834                             kTouchId, tes.LeapForward(50));
835      release.set_radius_x(20);
836      release.set_radius_y(13);
837
838      DispatchEventUsingWindowDispatcher(&release);
839      EXPECT_TRUE(delegate->tap());
840      EXPECT_FALSE(delegate->tap_down());
841      EXPECT_FALSE(delegate->tap_cancel());
842      EXPECT_FALSE(delegate->begin());
843      EXPECT_TRUE(delegate->end());
844      EXPECT_FALSE(delegate->scroll_begin());
845      EXPECT_FALSE(delegate->scroll_update());
846      EXPECT_FALSE(delegate->scroll_end());
847
848      EXPECT_EQ(1, delegate->tap_count());
849      gfx::Point actual_point(delegate->tap_location());
850      EXPECT_EQ(40, delegate->bounding_box().width());
851      EXPECT_EQ(40, delegate->bounding_box().height());
852      EXPECT_EQ(367, actual_point.x());
853      EXPECT_EQ(291, actual_point.y());
854   }
855
856   // Test with a single ET_TOUCH_MOVED event.
857   {
858      delegate->Reset();
859      ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(46, 205),
860                           kTouchId, tes.Now());
861      press.set_radius_x(6);
862      press.set_radius_y(10);
863      DispatchEventUsingWindowDispatcher(&press);
864      EXPECT_FALSE(delegate->tap());
865      EXPECT_TRUE(delegate->tap_down());
866      EXPECT_FALSE(delegate->tap_cancel());
867      EXPECT_TRUE(delegate->begin());
868      EXPECT_FALSE(delegate->tap_cancel());
869      EXPECT_FALSE(delegate->scroll_begin());
870      EXPECT_FALSE(delegate->scroll_update());
871      EXPECT_FALSE(delegate->scroll_end());
872      EXPECT_FALSE(delegate->long_press());
873
874      delegate->Reset();
875      ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(49, 204),
876                          kTouchId, tes.LeapForward(50));
877      move.set_radius_x(8);
878      move.set_radius_y(12);
879      DispatchEventUsingWindowDispatcher(&move);
880      EXPECT_FALSE(delegate->tap());
881      EXPECT_FALSE(delegate->tap_down());
882      EXPECT_FALSE(delegate->tap_cancel());
883      EXPECT_FALSE(delegate->begin());
884      EXPECT_FALSE(delegate->scroll_begin());
885      EXPECT_FALSE(delegate->scroll_update());
886      EXPECT_FALSE(delegate->scroll_end());
887      EXPECT_FALSE(delegate->long_press());
888
889      delegate->Reset();
890      ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(49, 204),
891                             kTouchId, tes.LeapForward(50));
892      release.set_radius_x(4);
893      release.set_radius_y(8);
894
895      DispatchEventUsingWindowDispatcher(&release);
896      EXPECT_TRUE(delegate->tap());
897      EXPECT_FALSE(delegate->tap_down());
898      EXPECT_FALSE(delegate->tap_cancel());
899      EXPECT_FALSE(delegate->begin());
900      EXPECT_TRUE(delegate->end());
901      EXPECT_FALSE(delegate->scroll_begin());
902      EXPECT_FALSE(delegate->scroll_update());
903      EXPECT_FALSE(delegate->scroll_end());
904
905      EXPECT_EQ(1, delegate->tap_count());
906      gfx::Point actual_point(delegate->tap_location());
907      EXPECT_EQ(25, delegate->bounding_box().width());
908      EXPECT_EQ(24, delegate->bounding_box().height());
909      EXPECT_EQ(48, actual_point.x());
910      EXPECT_EQ(204, actual_point.y());
911   }
912
913   // Test with a few ET_TOUCH_MOVED events.
914   {
915      delegate->Reset();
916      ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(400, 150),
917                           kTouchId, tes.Now());
918      press.set_radius_x(7);
919      press.set_radius_y(10);
920      DispatchEventUsingWindowDispatcher(&press);
921      EXPECT_FALSE(delegate->tap());
922      EXPECT_TRUE(delegate->tap_down());
923      EXPECT_FALSE(delegate->tap_cancel());
924      EXPECT_TRUE(delegate->begin());
925      EXPECT_FALSE(delegate->scroll_begin());
926      EXPECT_FALSE(delegate->scroll_update());
927      EXPECT_FALSE(delegate->scroll_end());
928      EXPECT_FALSE(delegate->long_press());
929
930      delegate->Reset();
931      ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(397, 151),
932                          kTouchId, tes.LeapForward(50));
933      move.set_radius_x(13);
934      move.set_radius_y(12);
935      DispatchEventUsingWindowDispatcher(&move);
936      EXPECT_FALSE(delegate->tap());
937      EXPECT_FALSE(delegate->tap_down());
938      EXPECT_FALSE(delegate->tap_cancel());
939      EXPECT_FALSE(delegate->begin());
940      EXPECT_FALSE(delegate->scroll_begin());
941      EXPECT_FALSE(delegate->scroll_update());
942      EXPECT_FALSE(delegate->scroll_end());
943      EXPECT_FALSE(delegate->long_press());
944
945      delegate->Reset();
946      ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(397, 149),
947                           kTouchId, tes.LeapForward(50));
948      move1.set_radius_x(16);
949      move1.set_radius_y(16);
950      DispatchEventUsingWindowDispatcher(&move1);
951      EXPECT_FALSE(delegate->tap());
952      EXPECT_FALSE(delegate->tap_down());
953      EXPECT_FALSE(delegate->tap_cancel());
954      EXPECT_FALSE(delegate->begin());
955      EXPECT_FALSE(delegate->scroll_begin());
956      EXPECT_FALSE(delegate->scroll_update());
957      EXPECT_FALSE(delegate->scroll_end());
958      EXPECT_FALSE(delegate->long_press());
959
960      delegate->Reset();
961      ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(400, 150),
962                           kTouchId, tes.LeapForward(50));
963      move2.set_radius_x(14);
964      move2.set_radius_y(10);
965      DispatchEventUsingWindowDispatcher(&move2);
966      EXPECT_FALSE(delegate->tap());
967      EXPECT_FALSE(delegate->tap_down());
968      EXPECT_FALSE(delegate->tap_cancel());
969      EXPECT_FALSE(delegate->begin());
970      EXPECT_FALSE(delegate->scroll_begin());
971      EXPECT_FALSE(delegate->scroll_update());
972      EXPECT_FALSE(delegate->scroll_end());
973      EXPECT_FALSE(delegate->long_press());
974
975      delegate->Reset();
976      ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(401, 149),
977                             kTouchId, tes.LeapForward(50));
978      release.set_radius_x(8);
979      release.set_radius_y(9);
980
981      DispatchEventUsingWindowDispatcher(&release);
982      EXPECT_TRUE(delegate->tap());
983      EXPECT_FALSE(delegate->tap_down());
984      EXPECT_FALSE(delegate->tap_cancel());
985      EXPECT_FALSE(delegate->begin());
986      EXPECT_TRUE(delegate->end());
987      EXPECT_FALSE(delegate->scroll_begin());
988      EXPECT_FALSE(delegate->scroll_update());
989      EXPECT_FALSE(delegate->scroll_end());
990
991      EXPECT_EQ(1, delegate->tap_count());
992      gfx::Point actual_point(delegate->tap_location());
993      EXPECT_EQ(33, delegate->bounding_box().width());
994      EXPECT_EQ(32, delegate->bounding_box().height());
995      EXPECT_EQ(397, actual_point.x());
996      EXPECT_EQ(149, actual_point.y());
997   }
998 }
999
1000 // Check that appropriate touch events generate scroll gesture events.
1001 TEST_P(GestureRecognizerTest, GestureEventScroll) {
1002   // We'll start by moving the touch point by (10.5, 10.5). We want 5 dips of
1003   // that distance to be consumed by the slop, so we set the slop radius to
1004   // sqrt(5 * 5 + 5 * 5).
1005   ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(
1006       sqrt(static_cast<double>(5 * 5 + 5 * 5)));
1007   scoped_ptr<GestureEventConsumeDelegate> delegate(
1008       new GestureEventConsumeDelegate());
1009   TimedEvents tes;
1010   const int kWindowWidth = 123;
1011   const int kWindowHeight = 45;
1012   const int kTouchId = 5;
1013   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1014   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1015       delegate.get(), -1234, bounds, root_window()));
1016
1017   delegate->Reset();
1018   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1019                        kTouchId, tes.Now());
1020   DispatchEventUsingWindowDispatcher(&press);
1021   EXPECT_2_EVENTS(delegate->events(),
1022                   ui::ET_GESTURE_BEGIN,
1023                   ui::ET_GESTURE_TAP_DOWN);
1024
1025   // Move the touch-point enough so that it is considered as a scroll. This
1026   // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1027   // The first movement is diagonal, to ensure that we have a free scroll,
1028   // and not a rail scroll.
1029   tes.SendScrollEvent(event_processor(), 111.5, 211.5, kTouchId,
1030                       delegate.get());
1031   EXPECT_3_EVENTS(delegate->events(),
1032                   ui::ET_GESTURE_TAP_CANCEL,
1033                   ui::ET_GESTURE_SCROLL_BEGIN,
1034                   ui::ET_GESTURE_SCROLL_UPDATE);
1035   // The slop consumed 5 dips
1036   EXPECT_FLOAT_EQ(5.5, delegate->scroll_x());
1037   EXPECT_FLOAT_EQ(5.5, delegate->scroll_y());
1038   EXPECT_EQ(gfx::Point(1, 1).ToString(),
1039             delegate->scroll_begin_position().ToString());
1040
1041   // When scrolling with a single finger, the bounding box of the gesture should
1042   // be empty, since it's a single point and the radius for testing is zero.
1043   EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1044
1045   // Move some more to generate a few more scroll updates. Make sure that we get
1046   // out of the snap channel for the unified GR.
1047   tes.SendScrollEvent(event_processor(), 20, 120, kTouchId, delegate.get());
1048   EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1049   EXPECT_FLOAT_EQ(-91.5, delegate->scroll_x());
1050   EXPECT_FLOAT_EQ(-91.5, delegate->scroll_y());
1051   EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1052
1053   tes.SendScrollEvent(event_processor(), 50, 124, kTouchId, delegate.get());
1054   EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1055   EXPECT_EQ(30, delegate->scroll_x());
1056   EXPECT_EQ(4, delegate->scroll_y());
1057   EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1058
1059   // Release the touch. This should end the scroll.
1060   delegate->Reset();
1061   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1062                          kTouchId,
1063                          tes.LeapForward(50));
1064   DispatchEventUsingWindowDispatcher(&release);
1065   EXPECT_2_EVENTS(delegate->events(),
1066                   ui::ET_SCROLL_FLING_START,
1067                   ui::ET_GESTURE_END);
1068   EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1069 }
1070
1071 // Check that predicted scroll update positions are correct.
1072 TEST_P(GestureRecognizerTest, GestureEventScrollPrediction) {
1073   const double prediction_interval = 0.03;
1074   ui::GestureConfiguration::set_scroll_prediction_seconds(prediction_interval);
1075    // We'll start by moving the touch point by (5, 5). We want all of that
1076   // distance to be consumed by the slop, so we set the slop radius to
1077   // sqrt(5 * 5 + 5 * 5).
1078   ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(
1079       sqrt(static_cast<double>(5 * 5 + 5 * 5)));
1080
1081   scoped_ptr<GestureEventConsumeDelegate> delegate(
1082       new GestureEventConsumeDelegate());
1083   TimedEvents tes;
1084   const int kWindowWidth = 123;
1085   const int kWindowHeight = 45;
1086   const int kTouchId = 5;
1087   gfx::Rect bounds(95, 195, kWindowWidth, kWindowHeight);
1088   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1089       delegate.get(), -1234, bounds, root_window()));
1090
1091   delegate->Reset();
1092   // Tracks the total scroll since we want to verify that the correct position
1093   // will be scrolled to throughout the prediction.
1094   gfx::Vector2dF total_scroll;
1095   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(96, 196),
1096                        kTouchId, tes.Now());
1097   DispatchEventUsingWindowDispatcher(&press);
1098   EXPECT_2_EVENTS(delegate->events(),
1099                   ui::ET_GESTURE_BEGIN,
1100                   ui::ET_GESTURE_TAP_DOWN);
1101   delegate->Reset();
1102
1103   // Get rid of touch slop.
1104   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(111, 211),
1105                       kTouchId, tes.Now());
1106   DispatchEventUsingWindowDispatcher(&move);
1107   EXPECT_3_EVENTS(delegate->events(),
1108                   ui::ET_GESTURE_TAP_CANCEL,
1109                   ui::ET_GESTURE_SCROLL_BEGIN,
1110                   ui::ET_GESTURE_SCROLL_UPDATE);
1111   total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1112   total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1113
1114   // Move the touch-point enough so that it is considered as a scroll. This
1115   // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1116   // The first movement is diagonal, to ensure that we have a free scroll,
1117   // and not a rail scroll.
1118   tes.LeapForward(30);
1119   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1120   EXPECT_1_EVENT(delegate->events(),
1121                  ui::ET_GESTURE_SCROLL_UPDATE);
1122   total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1123   total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1124
1125   // Move some more to generate a few more scroll updates.
1126   tes.LeapForward(30);
1127   tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
1128   EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1129   total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1130   total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1131
1132   tes.LeapForward(30);
1133   tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
1134   EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1135   total_scroll.set_x(total_scroll.x() + delegate->scroll_x());
1136   total_scroll.set_y(total_scroll.y() + delegate->scroll_y());
1137
1138   // Release the touch. This should end the scroll.
1139   delegate->Reset();
1140   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1141                          kTouchId,
1142                          tes.LeapForward(50));
1143   DispatchEventUsingWindowDispatcher(&release);
1144 }
1145
1146 // Check that the bounding box during a scroll event is correct.
1147 TEST_P(GestureRecognizerTest, GestureEventScrollBoundingBox) {
1148   TimedEvents tes;
1149   for (int radius = 1; radius <= 10; ++radius) {
1150     ui::GestureConfiguration::set_default_radius(radius);
1151     scoped_ptr<GestureEventConsumeDelegate> delegate(
1152         new GestureEventConsumeDelegate());
1153     const int kWindowWidth = 123;
1154     const int kWindowHeight = 45;
1155     const int kTouchId = 5;
1156     gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1157     scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1158         delegate.get(), -1234, bounds, root_window()));
1159
1160     const int kPositionX = 101;
1161     const int kPositionY = 201;
1162     delegate->Reset();
1163     ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
1164                          gfx::Point(kPositionX, kPositionY),
1165                          kTouchId,
1166                          tes.Now());
1167     DispatchEventUsingWindowDispatcher(&press);
1168     EXPECT_EQ(gfx::Rect(kPositionX - radius,
1169                         kPositionY - radius,
1170                         radius * 2,
1171                         radius * 2).ToString(),
1172               delegate->bounding_box().ToString());
1173
1174     const int kScrollAmount = 50;
1175     tes.SendScrollEvents(event_processor(), kPositionX, kPositionY,
1176         1, 1, kTouchId, 1, kScrollAmount, delegate.get());
1177     EXPECT_EQ(gfx::Point(1, 1).ToString(),
1178               delegate->scroll_begin_position().ToString());
1179     EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius,
1180                         kPositionY + kScrollAmount - radius,
1181                         radius * 2,
1182                         radius * 2).ToString(),
1183               delegate->bounding_box().ToString());
1184
1185     // Release the touch. This should end the scroll.
1186     delegate->Reset();
1187     ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
1188                                gfx::Point(kPositionX + kScrollAmount,
1189                                           kPositionY + kScrollAmount),
1190                                kTouchId, press.time_stamp() +
1191                                base::TimeDelta::FromMilliseconds(50));
1192     DispatchEventUsingWindowDispatcher(&release);
1193     EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius,
1194                         kPositionY + kScrollAmount - radius,
1195                         radius * 2,
1196                         radius * 2).ToString(),
1197               delegate->bounding_box().ToString());
1198   }
1199   ui::GestureConfiguration::set_default_radius(0);
1200 }
1201
1202 // Check Scroll End Events report correct velocities
1203 // if the user was on a horizontal rail
1204 TEST_P(GestureRecognizerTest, GestureEventHorizontalRailFling) {
1205   scoped_ptr<GestureEventConsumeDelegate> delegate(
1206       new GestureEventConsumeDelegate());
1207   TimedEvents tes;
1208   const int kTouchId = 7;
1209   gfx::Rect bounds(0, 0, 1000, 1000);
1210   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1211       delegate.get(), -1234, bounds, root_window()));
1212
1213   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1214                        kTouchId, tes.Now());
1215   DispatchEventUsingWindowDispatcher(&press);
1216
1217   // Get rid of touch slop.
1218   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 0),
1219                        kTouchId, tes.Now());
1220   DispatchEventUsingWindowDispatcher(&move);
1221   delegate->Reset();
1222
1223
1224   // Move the touch-point horizontally enough that it is considered a
1225   // horizontal scroll.
1226   tes.SendScrollEvent(event_processor(), 30, 1, kTouchId, delegate.get());
1227   EXPECT_FLOAT_EQ(0, delegate->scroll_y());
1228   EXPECT_FLOAT_EQ(20, delegate->scroll_x());
1229
1230   // Get a high x velocity, while still staying on the rail
1231   tes.SendScrollEvents(event_processor(), 1, 1,
1232                    100, 10, kTouchId, 1,
1233                    ui::GestureConfiguration::points_buffered_for_velocity(),
1234                    delegate.get());
1235
1236   delegate->Reset();
1237   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1238                          kTouchId, tes.Now());
1239   DispatchEventUsingWindowDispatcher(&release);
1240
1241   EXPECT_TRUE(delegate->fling());
1242   EXPECT_FALSE(delegate->scroll_end());
1243   EXPECT_GT(delegate->velocity_x(), 0);
1244   EXPECT_EQ(0, delegate->velocity_y());
1245 }
1246
1247 // Check Scroll End Events report correct velocities
1248 // if the user was on a vertical rail
1249 TEST_P(GestureRecognizerTest, GestureEventVerticalRailFling) {
1250   scoped_ptr<GestureEventConsumeDelegate> delegate(
1251       new GestureEventConsumeDelegate());
1252   TimedEvents tes;
1253   const int kTouchId = 7;
1254   gfx::Rect bounds(0, 0, 1000, 1000);
1255   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1256       delegate.get(), -1234, bounds, root_window()));
1257
1258   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1259                        kTouchId, tes.Now());
1260   DispatchEventUsingWindowDispatcher(&press);
1261
1262   // Get rid of touch slop.
1263   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 10),
1264                        kTouchId, tes.Now());
1265   DispatchEventUsingWindowDispatcher(&move);
1266   delegate->Reset();
1267
1268   // Move the touch-point vertically enough that it is considered a
1269   // vertical scroll.
1270   tes.SendScrollEvent(event_processor(), 1, 30, kTouchId, delegate.get());
1271   EXPECT_EQ(20, delegate->scroll_y());
1272   EXPECT_EQ(0, delegate->scroll_x());
1273   EXPECT_EQ(0, delegate->scroll_velocity_x());
1274
1275   // Get a high y velocity, while still staying on the rail
1276   tes.SendScrollEvents(event_processor(), 1, 6,
1277                        10, 100, kTouchId, 1,
1278                        ui::GestureConfiguration::points_buffered_for_velocity(),
1279                        delegate.get());
1280   EXPECT_EQ(0, delegate->scroll_velocity_x());
1281
1282   delegate->Reset();
1283   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 206),
1284                          kTouchId, tes.Now());
1285   DispatchEventUsingWindowDispatcher(&release);
1286
1287   EXPECT_TRUE(delegate->fling());
1288   EXPECT_FALSE(delegate->scroll_end());
1289   EXPECT_EQ(0, delegate->velocity_x());
1290   EXPECT_GT(delegate->velocity_y(), 0);
1291 }
1292
1293 // Check Scroll End Events report non-zero velocities if the user is not on a
1294 // rail
1295 TEST_P(GestureRecognizerTest, GestureEventNonRailFling) {
1296   ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(0);
1297   scoped_ptr<GestureEventConsumeDelegate> delegate(
1298       new GestureEventConsumeDelegate());
1299   TimedEvents tes;
1300   const int kTouchId = 7;
1301   gfx::Rect bounds(0, 0, 1000, 1000);
1302   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1303       delegate.get(), -1234, bounds, root_window()));
1304
1305   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1306                        kTouchId, tes.Now());
1307   DispatchEventUsingWindowDispatcher(&press);
1308
1309   // Move the touch-point such that a non-rail scroll begins, and we're outside
1310   // the snap channel for the unified GR.
1311   tes.SendScrollEvent(event_processor(), 50, 50, kTouchId, delegate.get());
1312   EXPECT_EQ(50, delegate->scroll_y());
1313   EXPECT_EQ(50, delegate->scroll_x());
1314
1315   tes.SendScrollEvents(event_processor(), 1, 1,
1316                        10, 100, kTouchId, 1,
1317                        ui::GestureConfiguration::points_buffered_for_velocity(),
1318                        delegate.get());
1319
1320   delegate->Reset();
1321   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1322                          kTouchId, tes.Now());
1323   DispatchEventUsingWindowDispatcher(&release);
1324
1325   EXPECT_TRUE(delegate->fling());
1326   EXPECT_FALSE(delegate->scroll_end());
1327   EXPECT_GT(delegate->velocity_x(), 0);
1328   EXPECT_GT(delegate->velocity_y(), 0);
1329 }
1330
1331 // Check that appropriate touch events generate long press events
1332 TEST_P(GestureRecognizerTest, GestureEventLongPress) {
1333   ui::GestureConfiguration::set_max_touch_down_duration_in_seconds_for_click(
1334       0.0025);
1335
1336   scoped_ptr<GestureEventConsumeDelegate> delegate(
1337       new GestureEventConsumeDelegate());
1338   const int kWindowWidth = 123;
1339   const int kWindowHeight = 45;
1340   const int kTouchId = 2;
1341   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1342   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1343       delegate.get(), -1234, bounds, root_window()));
1344
1345   delegate->Reset();
1346
1347   TimerTestGestureRecognizer* gesture_recognizer =
1348       new TimerTestGestureRecognizer();
1349
1350   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
1351
1352   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1353                         gfx::Point(101, 201),
1354                         kTouchId,
1355                         ui::EventTimeForNow());
1356   DispatchEventUsingWindowDispatcher(&press1);
1357   EXPECT_TRUE(delegate->tap_down());
1358   EXPECT_TRUE(delegate->begin());
1359   EXPECT_FALSE(delegate->tap_cancel());
1360
1361   // We haven't pressed long enough for a long press to occur
1362   EXPECT_FALSE(delegate->long_press());
1363
1364   // Wait until the timer runs out
1365   delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1366   EXPECT_TRUE(delegate->long_press());
1367   EXPECT_FALSE(delegate->tap_cancel());
1368
1369   delegate->Reset();
1370   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1371                           gfx::Point(101, 201),
1372                           kTouchId,
1373                           ui::EventTimeForNow());
1374   DispatchEventUsingWindowDispatcher(&release1);
1375   EXPECT_FALSE(delegate->long_press());
1376
1377   // Note the tap cancel isn't dispatched until the release
1378   EXPECT_TRUE(delegate->tap_cancel());
1379   EXPECT_FALSE(delegate->tap());
1380 }
1381
1382 // Check that scrolling cancels a long press
1383 TEST_P(GestureRecognizerTest, GestureEventLongPressCancelledByScroll) {
1384   scoped_ptr<GestureEventConsumeDelegate> delegate(
1385       new GestureEventConsumeDelegate());
1386   TimedEvents tes;
1387   const int kWindowWidth = 123;
1388   const int kWindowHeight = 45;
1389   const int kTouchId = 6;
1390   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1391   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1392       delegate.get(), -1234, bounds, root_window()));
1393
1394   delegate->Reset();
1395
1396   TimerTestGestureRecognizer* gesture_recognizer =
1397       new TimerTestGestureRecognizer();
1398   TimerTestGestureSequence* gesture_sequence =
1399       static_cast<TimerTestGestureSequence*>(
1400           gesture_recognizer->GetGestureSequenceForTesting(window.get()));
1401
1402   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
1403
1404   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1405                         kTouchId, tes.Now());
1406   DispatchEventUsingWindowDispatcher(&press1);
1407   EXPECT_TRUE(delegate->tap_down());
1408
1409   // We haven't pressed long enough for a long press to occur
1410   EXPECT_FALSE(delegate->long_press());
1411   EXPECT_FALSE(delegate->tap_cancel());
1412
1413   // Scroll around, to cancel the long press
1414   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
1415   // Wait until the timer runs out
1416   gesture_sequence->ForceTimeout();
1417   EXPECT_FALSE(delegate->long_press());
1418   EXPECT_TRUE(delegate->tap_cancel());
1419
1420   delegate->Reset();
1421   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1422                           kTouchId, tes.LeapForward(10));
1423   DispatchEventUsingWindowDispatcher(&release1);
1424   EXPECT_FALSE(delegate->long_press());
1425   EXPECT_FALSE(delegate->tap_cancel());
1426 }
1427
1428 // Check that appropriate touch events generate long tap events
1429 TEST_P(GestureRecognizerTest, GestureEventLongTap) {
1430   ui::GestureConfiguration::set_max_touch_down_duration_in_seconds_for_click(
1431       0.0025);
1432   scoped_ptr<GestureEventConsumeDelegate> delegate(
1433       new GestureEventConsumeDelegate());
1434   const int kWindowWidth = 123;
1435   const int kWindowHeight = 45;
1436   const int kTouchId = 2;
1437   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1438   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1439       delegate.get(), -1234, bounds, root_window()));
1440
1441   delegate->Reset();
1442
1443   TimerTestGestureRecognizer* gesture_recognizer =
1444       new TimerTestGestureRecognizer();
1445
1446   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
1447
1448   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
1449                         gfx::Point(101, 201),
1450                         kTouchId,
1451                         ui::EventTimeForNow());
1452   DispatchEventUsingWindowDispatcher(&press1);
1453   EXPECT_TRUE(delegate->tap_down());
1454   EXPECT_TRUE(delegate->begin());
1455   EXPECT_FALSE(delegate->tap_cancel());
1456
1457   // We haven't pressed long enough for a long press to occur
1458   EXPECT_FALSE(delegate->long_press());
1459
1460   // Wait until the timer runs out
1461   delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1462   EXPECT_TRUE(delegate->long_press());
1463   EXPECT_FALSE(delegate->tap_cancel());
1464
1465   delegate->Reset();
1466   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED,
1467                           gfx::Point(101, 201),
1468                           kTouchId,
1469                           ui::EventTimeForNow());
1470   DispatchEventUsingWindowDispatcher(&release1);
1471   EXPECT_FALSE(delegate->long_press());
1472   EXPECT_TRUE(delegate->long_tap());
1473
1474   // Note the tap cancel isn't dispatched until the release
1475   EXPECT_TRUE(delegate->tap_cancel());
1476   EXPECT_FALSE(delegate->tap());
1477 }
1478
1479 // Check that second tap cancels a long press
1480 TEST_P(GestureRecognizerTest, GestureEventLongPressCancelledBySecondTap) {
1481   scoped_ptr<GestureEventConsumeDelegate> delegate(
1482       new GestureEventConsumeDelegate());
1483   TimedEvents tes;
1484   const int kWindowWidth = 300;
1485   const int kWindowHeight = 400;
1486   const int kTouchId1 = 8;
1487   const int kTouchId2 = 2;
1488   gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1489   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1490       delegate.get(), -1234, bounds, root_window()));
1491
1492   TimerTestGestureRecognizer* gesture_recognizer =
1493       new TimerTestGestureRecognizer();
1494   TimerTestGestureSequence* gesture_sequence =
1495       static_cast<TimerTestGestureSequence*>(
1496           gesture_recognizer->GetGestureSequenceForTesting(window.get()));
1497
1498   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
1499
1500   delegate->Reset();
1501   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1502                        kTouchId1, tes.Now());
1503   DispatchEventUsingWindowDispatcher(&press);
1504   EXPECT_TRUE(delegate->tap_down());
1505   EXPECT_TRUE(delegate->begin());
1506
1507   // We haven't pressed long enough for a long press to occur
1508   EXPECT_FALSE(delegate->long_press());
1509
1510   // Second tap, to cancel the long press
1511   delegate->Reset();
1512   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1513                         kTouchId2, tes.Now());
1514   DispatchEventUsingWindowDispatcher(&press2);
1515   EXPECT_FALSE(delegate->tap_down());  // no touch down for second tap.
1516   EXPECT_TRUE(delegate->tap_cancel());
1517   EXPECT_TRUE(delegate->begin());
1518
1519   // Wait until the timer runs out
1520   gesture_sequence->ForceTimeout();
1521
1522   // No long press occurred
1523   EXPECT_FALSE(delegate->long_press());
1524
1525   delegate->Reset();
1526   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1527                           kTouchId1, tes.Now());
1528   DispatchEventUsingWindowDispatcher(&release1);
1529   EXPECT_FALSE(delegate->long_press());
1530   EXPECT_TRUE(delegate->two_finger_tap());
1531   EXPECT_FALSE(delegate->tap_cancel());
1532 }
1533
1534 // Check that horizontal scroll gestures cause scrolls on horizontal rails.
1535 // Also tests that horizontal rails can be broken.
1536 TEST_P(GestureRecognizerTest, GestureEventHorizontalRailScroll) {
1537   scoped_ptr<GestureEventConsumeDelegate> delegate(
1538       new GestureEventConsumeDelegate());
1539   TimedEvents tes;
1540   const int kTouchId = 7;
1541   gfx::Rect bounds(0, 0, 1000, 1000);
1542   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1543       delegate.get(), -1234, bounds, root_window()));
1544
1545   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1546                        kTouchId, tes.Now());
1547   DispatchEventUsingWindowDispatcher(&press);
1548
1549   // Get rid of touch slop.
1550   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(5, 0),
1551                       kTouchId, tes.Now());
1552
1553   DispatchEventUsingWindowDispatcher(&move);
1554   delegate->Reset();
1555
1556   // Move the touch-point horizontally enough that it is considered a
1557   // horizontal scroll.
1558   tes.SendScrollEvent(event_processor(), 25, 0, kTouchId, delegate.get());
1559   EXPECT_EQ(0, delegate->scroll_y());
1560   EXPECT_EQ(20, delegate->scroll_x());
1561
1562   tes.SendScrollEvent(event_processor(), 30, 6, kTouchId, delegate.get());
1563   EXPECT_TRUE(delegate->scroll_update());
1564   EXPECT_EQ(5, delegate->scroll_x());
1565   // y shouldn't change, as we're on a horizontal rail.
1566   EXPECT_EQ(0, delegate->scroll_y());
1567
1568   // Send enough information that a velocity can be calculated for the gesture,
1569   // and we can break the rail
1570   tes.SendScrollEvents(event_processor(), 1, 1,
1571                        6, 100, kTouchId, 1,
1572                        ui::GestureConfiguration::points_buffered_for_velocity(),
1573                        delegate.get());
1574
1575   tes.SendScrollEvent(event_processor(), 5, 0, kTouchId, delegate.get());
1576   tes.SendScrollEvent(event_processor(), 10, 5, kTouchId, delegate.get());
1577
1578   // The rail should be broken
1579   EXPECT_TRUE(delegate->scroll_update());
1580   EXPECT_EQ(5, delegate->scroll_x());
1581   EXPECT_EQ(5, delegate->scroll_y());
1582 }
1583
1584 // Check that vertical scroll gestures cause scrolls on vertical rails.
1585 // Also tests that vertical rails can be broken.
1586 TEST_P(GestureRecognizerTest, GestureEventVerticalRailScroll) {
1587   scoped_ptr<GestureEventConsumeDelegate> delegate(
1588       new GestureEventConsumeDelegate());
1589   TimedEvents tes;
1590   const int kTouchId = 7;
1591   gfx::Rect bounds(0, 0, 1000, 1000);
1592   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1593       delegate.get(), -1234, bounds, root_window()));
1594
1595   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1596                        kTouchId, tes.Now());
1597   DispatchEventUsingWindowDispatcher(&press);
1598
1599   // Get rid of touch slop.
1600   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(0, 5),
1601                        kTouchId, tes.Now());
1602   DispatchEventUsingWindowDispatcher(&move);
1603   delegate->Reset();
1604
1605   // Move the touch-point vertically enough that it is considered a
1606   // vertical scroll.
1607   tes.SendScrollEvent(event_processor(), 0, 25, kTouchId, delegate.get());
1608   EXPECT_EQ(0, delegate->scroll_x());
1609   EXPECT_EQ(20, delegate->scroll_y());
1610
1611   tes.SendScrollEvent(event_processor(), 6, 30, kTouchId, delegate.get());
1612   EXPECT_TRUE(delegate->scroll_update());
1613   EXPECT_EQ(5, delegate->scroll_y());
1614   // x shouldn't change, as we're on a vertical rail.
1615   EXPECT_EQ(0, delegate->scroll_x());
1616   EXPECT_EQ(0, delegate->scroll_velocity_x());
1617
1618   // Send enough information that a velocity can be calculated for the gesture,
1619   // and we can break the rail
1620   tes.SendScrollEvents(event_processor(), 1, 6,
1621                        100, 1, kTouchId, 1,
1622                        ui::GestureConfiguration::points_buffered_for_velocity(),
1623                        delegate.get());
1624
1625   tes.SendScrollEvent(event_processor(), 0, 5, kTouchId, delegate.get());
1626   tes.SendScrollEvent(event_processor(), 5, 10, kTouchId, delegate.get());
1627
1628   // The rail should be broken
1629   EXPECT_TRUE(delegate->scroll_update());
1630   EXPECT_EQ(5, delegate->scroll_x());
1631   EXPECT_EQ(5, delegate->scroll_y());
1632 }
1633
1634 TEST_P(GestureRecognizerTest, GestureTapFollowedByScroll) {
1635   // We'll start by moving the touch point by (5, 5). We want all of that
1636   // distance to be consumed by the slop, so we set the slop radius to
1637   // sqrt(5 * 5 + 5 * 5).
1638   ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(
1639       sqrt(static_cast<double>(5 * 5 + 5 * 5)));
1640
1641   // First, tap. Then, do a scroll using the same touch-id.
1642   scoped_ptr<GestureEventConsumeDelegate> delegate(
1643       new GestureEventConsumeDelegate());
1644   TimedEvents tes;
1645   const int kWindowWidth = 123;
1646   const int kWindowHeight = 45;
1647   const int kTouchId = 3;
1648   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1649   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1650       delegate.get(), -1234, bounds, root_window()));
1651
1652   delegate->Reset();
1653   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1654                        kTouchId, tes.Now());
1655   DispatchEventUsingWindowDispatcher(&press);
1656   EXPECT_FALSE(delegate->tap());
1657   EXPECT_TRUE(delegate->tap_down());
1658   EXPECT_FALSE(delegate->tap_cancel());
1659   EXPECT_FALSE(delegate->scroll_begin());
1660   EXPECT_FALSE(delegate->scroll_update());
1661   EXPECT_FALSE(delegate->scroll_end());
1662
1663   // Make sure there is enough delay before the touch is released so that it is
1664   // recognized as a tap.
1665   delegate->Reset();
1666   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1667                          kTouchId, tes.LeapForward(50));
1668   DispatchEventUsingWindowDispatcher(&release);
1669   EXPECT_TRUE(delegate->tap());
1670   EXPECT_FALSE(delegate->tap_down());
1671   EXPECT_FALSE(delegate->tap_cancel());
1672   EXPECT_FALSE(delegate->scroll_begin());
1673   EXPECT_FALSE(delegate->scroll_update());
1674   EXPECT_FALSE(delegate->scroll_end());
1675
1676   // Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger
1677   // a double-tap.
1678   delegate->Reset();
1679   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1680                         kTouchId, tes.LeapForward(1000));
1681   DispatchEventUsingWindowDispatcher(&press1);
1682   EXPECT_FALSE(delegate->tap());
1683   EXPECT_TRUE(delegate->tap_down());
1684   EXPECT_FALSE(delegate->tap_cancel());
1685   EXPECT_FALSE(delegate->scroll_begin());
1686   EXPECT_FALSE(delegate->scroll_update());
1687   EXPECT_FALSE(delegate->scroll_end());
1688
1689   // Get rid of touch slop.
1690   ui::TouchEvent move_remove_slop(ui::ET_TOUCH_MOVED, gfx::Point(116, 216),
1691                                   kTouchId, tes.Now());
1692   DispatchEventUsingWindowDispatcher(&move_remove_slop);
1693   EXPECT_TRUE(delegate->tap_cancel());
1694   EXPECT_TRUE(delegate->scroll_begin());
1695   EXPECT_TRUE(delegate->scroll_update());
1696   EXPECT_EQ(15, delegate->scroll_x_hint());
1697   EXPECT_EQ(15, delegate->scroll_y_hint());
1698
1699   delegate->Reset();
1700
1701   // Move the touch-point enough so that it is considered as a scroll. This
1702   // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1703   // The first movement is diagonal, to ensure that we have a free scroll,
1704   // and not a rail scroll.
1705   delegate->Reset();
1706   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(135, 235),
1707                       kTouchId, tes.Now());
1708   DispatchEventUsingWindowDispatcher(&move);
1709   EXPECT_FALSE(delegate->tap());
1710   EXPECT_FALSE(delegate->tap_down());
1711   EXPECT_FALSE(delegate->tap_cancel());
1712   EXPECT_FALSE(delegate->scroll_begin());
1713   EXPECT_TRUE(delegate->scroll_update());
1714   EXPECT_FALSE(delegate->scroll_end());
1715   EXPECT_EQ(19, delegate->scroll_x());
1716   EXPECT_EQ(19, delegate->scroll_y());
1717
1718   // Move some more to generate a few more scroll updates.
1719   delegate->Reset();
1720   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(115, 216),
1721                        kTouchId, tes.Now());
1722   DispatchEventUsingWindowDispatcher(&move1);
1723   EXPECT_FALSE(delegate->tap());
1724   EXPECT_FALSE(delegate->tap_down());
1725   EXPECT_FALSE(delegate->tap_cancel());
1726   EXPECT_FALSE(delegate->scroll_begin());
1727   EXPECT_TRUE(delegate->scroll_update());
1728   EXPECT_FALSE(delegate->scroll_end());
1729   EXPECT_EQ(-20, delegate->scroll_x());
1730   EXPECT_EQ(-19, delegate->scroll_y());
1731   EXPECT_EQ(0, delegate->scroll_x_hint());
1732   EXPECT_EQ(0, delegate->scroll_y_hint());
1733
1734   delegate->Reset();
1735   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(145, 220),
1736                        kTouchId, tes.Now());
1737   DispatchEventUsingWindowDispatcher(&move2);
1738   EXPECT_FALSE(delegate->tap());
1739   EXPECT_FALSE(delegate->tap_down());
1740   EXPECT_FALSE(delegate->tap_cancel());
1741   EXPECT_FALSE(delegate->scroll_begin());
1742   EXPECT_TRUE(delegate->scroll_update());
1743   EXPECT_FALSE(delegate->scroll_end());
1744   EXPECT_EQ(30, delegate->scroll_x());
1745   EXPECT_EQ(4, delegate->scroll_y());
1746
1747   // Release the touch. This should end the scroll.
1748   delegate->Reset();
1749   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1750                           kTouchId, tes.Now());
1751   DispatchEventUsingWindowDispatcher(&release1);
1752   EXPECT_FALSE(delegate->tap());
1753   EXPECT_FALSE(delegate->tap_down());
1754   EXPECT_FALSE(delegate->tap_cancel());
1755   EXPECT_FALSE(delegate->scroll_begin());
1756   EXPECT_FALSE(delegate->scroll_update());
1757   EXPECT_FALSE(delegate->scroll_end());
1758   EXPECT_TRUE(delegate->fling());
1759 }
1760
1761 TEST_P(GestureRecognizerTest, AsynchronousGestureRecognition) {
1762   scoped_ptr<QueueTouchEventDelegate> queued_delegate(
1763       new QueueTouchEventDelegate(host()->dispatcher()));
1764   const int kWindowWidth = 123;
1765   const int kWindowHeight = 45;
1766   const int kTouchId1 = 6;
1767   const int kTouchId2 = 4;
1768   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1769   scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate(
1770       queued_delegate.get(), -1234, bounds, root_window()));
1771
1772   queued_delegate->set_window(queue.get());
1773
1774   // Touch down on the window. This should not generate any gesture event.
1775   queued_delegate->Reset();
1776   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1777                            kTouchId1, GetTime());
1778   DispatchEventUsingWindowDispatcher(&press);
1779   EXPECT_FALSE(queued_delegate->tap());
1780   EXPECT_FALSE(queued_delegate->tap_down());
1781   EXPECT_FALSE(queued_delegate->tap_cancel());
1782   EXPECT_FALSE(queued_delegate->begin());
1783   EXPECT_FALSE(queued_delegate->scroll_begin());
1784   EXPECT_FALSE(queued_delegate->scroll_update());
1785   EXPECT_FALSE(queued_delegate->scroll_end());
1786
1787   // Introduce some delay before the touch is released so that it is recognized
1788   // as a tap. However, this still should not create any gesture events.
1789   queued_delegate->Reset();
1790   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1791                              kTouchId1, press.time_stamp() +
1792                              base::TimeDelta::FromMilliseconds(50));
1793   DispatchEventUsingWindowDispatcher(&release);
1794   EXPECT_FALSE(queued_delegate->tap());
1795   EXPECT_FALSE(queued_delegate->tap_down());
1796   EXPECT_FALSE(queued_delegate->tap_cancel());
1797   EXPECT_FALSE(queued_delegate->begin());
1798   EXPECT_FALSE(queued_delegate->end());
1799   EXPECT_FALSE(queued_delegate->scroll_begin());
1800   EXPECT_FALSE(queued_delegate->scroll_update());
1801   EXPECT_FALSE(queued_delegate->scroll_end());
1802
1803   // Create another window, and place a touch-down on it. This should create a
1804   // tap-down gesture.
1805   scoped_ptr<GestureEventConsumeDelegate> delegate(
1806       new GestureEventConsumeDelegate());
1807   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1808       delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), root_window()));
1809   delegate->Reset();
1810   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 20),
1811                             kTouchId2, GetTime());
1812   DispatchEventUsingWindowDispatcher(&press2);
1813   EXPECT_FALSE(delegate->tap());
1814   EXPECT_TRUE(delegate->tap_down());
1815   EXPECT_FALSE(delegate->tap_cancel());
1816   EXPECT_FALSE(queued_delegate->begin());
1817   EXPECT_FALSE(queued_delegate->end());
1818   EXPECT_FALSE(delegate->scroll_begin());
1819   EXPECT_FALSE(delegate->scroll_update());
1820   EXPECT_FALSE(delegate->scroll_end());
1821
1822   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(10, 20),
1823                               kTouchId2, GetTime());
1824   DispatchEventUsingWindowDispatcher(&release2);
1825
1826   // Process the first queued event.
1827   queued_delegate->Reset();
1828   queued_delegate->ReceivedAck();
1829   EXPECT_FALSE(queued_delegate->tap());
1830   EXPECT_TRUE(queued_delegate->tap_down());
1831   EXPECT_TRUE(queued_delegate->begin());
1832   EXPECT_FALSE(queued_delegate->tap_cancel());
1833   EXPECT_FALSE(queued_delegate->end());
1834   EXPECT_FALSE(queued_delegate->scroll_begin());
1835   EXPECT_FALSE(queued_delegate->scroll_update());
1836   EXPECT_FALSE(queued_delegate->scroll_end());
1837
1838   // Now, process the second queued event.
1839   queued_delegate->Reset();
1840   queued_delegate->ReceivedAck();
1841   EXPECT_TRUE(queued_delegate->tap());
1842   EXPECT_FALSE(queued_delegate->tap_down());
1843   EXPECT_FALSE(queued_delegate->tap_cancel());
1844   EXPECT_FALSE(queued_delegate->begin());
1845   EXPECT_TRUE(queued_delegate->end());
1846   EXPECT_FALSE(queued_delegate->scroll_begin());
1847   EXPECT_FALSE(queued_delegate->scroll_update());
1848   EXPECT_FALSE(queued_delegate->scroll_end());
1849
1850   // Start all over. Press on the first window, then press again on the second
1851   // window. The second press should still go to the first window.
1852   queued_delegate->Reset();
1853   ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1854                             kTouchId1, GetTime());
1855   DispatchEventUsingWindowDispatcher(&press3);
1856   EXPECT_FALSE(queued_delegate->tap());
1857   EXPECT_FALSE(queued_delegate->tap_down());
1858   EXPECT_FALSE(queued_delegate->tap_cancel());
1859   EXPECT_FALSE(queued_delegate->begin());
1860   EXPECT_FALSE(queued_delegate->end());
1861   EXPECT_FALSE(queued_delegate->begin());
1862   EXPECT_FALSE(queued_delegate->end());
1863   EXPECT_FALSE(queued_delegate->scroll_begin());
1864   EXPECT_FALSE(queued_delegate->scroll_update());
1865   EXPECT_FALSE(queued_delegate->scroll_end());
1866
1867   queued_delegate->Reset();
1868   delegate->Reset();
1869   ui::TouchEvent press4(ui::ET_TOUCH_PRESSED, gfx::Point(103, 203),
1870                             kTouchId2, GetTime());
1871   DispatchEventUsingWindowDispatcher(&press4);
1872   EXPECT_FALSE(delegate->tap());
1873   EXPECT_FALSE(delegate->tap_down());
1874   EXPECT_FALSE(delegate->tap_cancel());
1875   EXPECT_FALSE(delegate->begin());
1876   EXPECT_FALSE(delegate->end());
1877   EXPECT_FALSE(delegate->scroll_begin());
1878   EXPECT_FALSE(delegate->scroll_update());
1879   EXPECT_FALSE(delegate->scroll_end());
1880   EXPECT_FALSE(queued_delegate->tap());
1881   EXPECT_FALSE(queued_delegate->tap_down());
1882   EXPECT_FALSE(queued_delegate->tap_cancel());
1883   EXPECT_FALSE(queued_delegate->begin());
1884   EXPECT_FALSE(queued_delegate->end());
1885   EXPECT_FALSE(queued_delegate->scroll_begin());
1886   EXPECT_FALSE(queued_delegate->scroll_update());
1887   EXPECT_FALSE(queued_delegate->scroll_end());
1888
1889   // Move the second touch-point enough so that it is considered a pinch. This
1890   // should generate both SCROLL_BEGIN and PINCH_BEGIN gestures.
1891   queued_delegate->Reset();
1892   delegate->Reset();
1893   int x_move = ui::GestureConfiguration::max_touch_move_in_pixels_for_click();
1894   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(203 + x_move, 303),
1895                           kTouchId2, GetTime());
1896   DispatchEventUsingWindowDispatcher(&move);
1897   EXPECT_FALSE(delegate->tap());
1898   EXPECT_FALSE(delegate->tap_down());
1899   EXPECT_FALSE(delegate->tap_cancel());
1900   EXPECT_FALSE(delegate->begin());
1901   EXPECT_FALSE(delegate->scroll_begin());
1902   EXPECT_FALSE(delegate->scroll_update());
1903   EXPECT_FALSE(delegate->scroll_end());
1904   EXPECT_FALSE(queued_delegate->tap());
1905   EXPECT_FALSE(queued_delegate->tap_down());
1906   EXPECT_FALSE(queued_delegate->tap_cancel());
1907   EXPECT_FALSE(queued_delegate->begin());
1908   EXPECT_FALSE(queued_delegate->scroll_begin());
1909   EXPECT_FALSE(queued_delegate->scroll_update());
1910   EXPECT_FALSE(queued_delegate->scroll_end());
1911
1912   queued_delegate->Reset();
1913   queued_delegate->ReceivedAck();
1914   EXPECT_FALSE(queued_delegate->tap());
1915   EXPECT_TRUE(queued_delegate->tap_down());
1916   EXPECT_TRUE(queued_delegate->begin());
1917   EXPECT_FALSE(queued_delegate->tap_cancel());
1918   EXPECT_FALSE(queued_delegate->end());
1919   EXPECT_FALSE(queued_delegate->scroll_begin());
1920   EXPECT_FALSE(queued_delegate->scroll_update());
1921   EXPECT_FALSE(queued_delegate->scroll_end());
1922
1923   queued_delegate->Reset();
1924   queued_delegate->ReceivedAck();
1925   EXPECT_FALSE(queued_delegate->tap());
1926   EXPECT_FALSE(queued_delegate->tap_down());  // no touch down for second tap.
1927   EXPECT_TRUE(queued_delegate->tap_cancel());
1928   EXPECT_TRUE(queued_delegate->begin());
1929   EXPECT_FALSE(queued_delegate->end());
1930   EXPECT_FALSE(queued_delegate->scroll_begin());
1931   EXPECT_FALSE(queued_delegate->scroll_update());
1932   EXPECT_FALSE(queued_delegate->scroll_end());
1933   EXPECT_FALSE(queued_delegate->pinch_begin());
1934   EXPECT_FALSE(queued_delegate->pinch_update());
1935   EXPECT_FALSE(queued_delegate->pinch_end());
1936
1937   queued_delegate->Reset();
1938   queued_delegate->ReceivedAck();
1939   EXPECT_FALSE(queued_delegate->tap());
1940   EXPECT_FALSE(queued_delegate->tap_down());
1941   EXPECT_FALSE(queued_delegate->tap_cancel());
1942   EXPECT_FALSE(queued_delegate->begin());
1943   EXPECT_FALSE(queued_delegate->end());
1944   EXPECT_TRUE(queued_delegate->scroll_begin());
1945   // TODO(tdresser): uncomment once we've switched to the unified GR.
1946   //  EXPECT_TRUE(queued_delegate->scroll_update());
1947   EXPECT_FALSE(queued_delegate->scroll_end());
1948   EXPECT_TRUE(queued_delegate->pinch_begin());
1949   EXPECT_FALSE(queued_delegate->pinch_update());
1950   EXPECT_FALSE(queued_delegate->pinch_end());
1951 }
1952
1953 // Check that appropriate touch events generate pinch gesture events.
1954 TEST_P(GestureRecognizerTest, GestureEventPinchFromScroll) {
1955   // Disabled for unified GR due to differences in when pinch begin is sent. The
1956   // Aura GR sends it earlier than is necessary.
1957   if (UsingUnifiedGR())
1958     return;
1959
1960   scoped_ptr<GestureEventConsumeDelegate> delegate(
1961       new GestureEventConsumeDelegate());
1962   TimedEvents tes;
1963   const int kWindowWidth = 300;
1964   const int kWindowHeight = 400;
1965   const int kTouchId1 = 5;
1966   const int kTouchId2 = 3;
1967   gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1968   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1969       delegate.get(), -1234, bounds, root_window()));
1970
1971   delegate->Reset();
1972   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1973                        kTouchId1, tes.Now());
1974   DispatchEventUsingWindowDispatcher(&press);
1975   EXPECT_2_EVENTS(delegate->events(),
1976                   ui::ET_GESTURE_BEGIN,
1977                   ui::ET_GESTURE_TAP_DOWN);
1978
1979   // Move the touch-point enough so that it is considered as a scroll. This
1980   // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1981   delegate->Reset();
1982   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 301),
1983                       kTouchId1, tes.Now());
1984   DispatchEventUsingWindowDispatcher(&move);
1985   EXPECT_3_EVENTS(delegate->events(),
1986                   ui::ET_GESTURE_TAP_CANCEL,
1987                   ui::ET_GESTURE_SCROLL_BEGIN,
1988                   ui::ET_GESTURE_SCROLL_UPDATE);
1989
1990   // Press the second finger. It should cause pinch-begin. Note that we will not
1991   // transition to two finger tap here because the touch points are far enough.
1992   delegate->Reset();
1993   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1994                         kTouchId2, tes.Now());
1995   DispatchEventUsingWindowDispatcher(&press2);
1996   EXPECT_2_EVENTS(delegate->events(),
1997                  ui::ET_GESTURE_BEGIN,
1998                  ui::ET_GESTURE_PINCH_BEGIN);
1999   EXPECT_EQ(gfx::Rect(10, 10, 120, 291).ToString(),
2000             delegate->bounding_box().ToString());
2001
2002   // Move the first finger.
2003   delegate->Reset();
2004   ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(95, 201),
2005                        kTouchId1, tes.Now());
2006   DispatchEventUsingWindowDispatcher(&move3);
2007   EXPECT_2_EVENTS(delegate->events(),
2008                   ui::ET_GESTURE_PINCH_UPDATE,
2009                   ui::ET_GESTURE_SCROLL_UPDATE);
2010   EXPECT_EQ(gfx::Rect(10, 10, 85, 191).ToString(),
2011             delegate->bounding_box().ToString());
2012
2013   // Now move the second finger.
2014   delegate->Reset();
2015   ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
2016                        kTouchId2, tes.Now());
2017   DispatchEventUsingWindowDispatcher(&move4);
2018   EXPECT_2_EVENTS(delegate->events(),
2019                   ui::ET_GESTURE_PINCH_UPDATE,
2020                   ui::ET_GESTURE_SCROLL_UPDATE);
2021   EXPECT_EQ(gfx::Rect(55, 15, 40, 186).ToString(),
2022             delegate->bounding_box().ToString());
2023
2024   // Release the first finger. This should end pinch.
2025   delegate->Reset();
2026   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2027                          kTouchId1, tes.Now());
2028   DispatchEventUsingWindowDispatcher(&release);
2029   EXPECT_2_EVENTS(delegate->events(),
2030                  ui::ET_GESTURE_PINCH_END,
2031                  ui::ET_GESTURE_END);
2032   EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
2033             delegate->bounding_box().ToString());
2034
2035   // Move the second finger. This should still generate a scroll.
2036   delegate->Reset();
2037   ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2038                        kTouchId2, tes.Now());
2039   DispatchEventUsingWindowDispatcher(&move5);
2040   EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2041   EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2042 }
2043
2044 TEST_P(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) {
2045 scoped_ptr<GestureEventConsumeDelegate> delegate(
2046       new GestureEventConsumeDelegate());
2047   TimedEvents tes;
2048   const int kWindowWidth = 300;
2049   const int kWindowHeight = 400;
2050   const int kTouchId1 = 5;
2051   const int kTouchId2 = 3;
2052   gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2053   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2054       delegate.get(), -1234, bounds, root_window()));
2055
2056   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2057                        kTouchId1, tes.Now());
2058   DispatchEventUsingWindowDispatcher(&press);
2059   delegate->Reset();
2060   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2061                         kTouchId2, tes.Now());
2062   DispatchEventUsingWindowDispatcher(&press2);
2063   EXPECT_FALSE(delegate->pinch_begin());
2064
2065   // Touch move triggers pinch begin.
2066   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2067   EXPECT_TRUE(delegate->pinch_begin());
2068   EXPECT_FALSE(delegate->pinch_update());
2069
2070   // Touch move triggers pinch update.
2071   tes.SendScrollEvent(event_processor(), 160, 200, kTouchId1, delegate.get());
2072   EXPECT_FALSE(delegate->pinch_begin());
2073   EXPECT_TRUE(delegate->pinch_update());
2074
2075   // Pinch has started, now release the second finger
2076   delegate->Reset();
2077   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2078                          kTouchId1, tes.Now());
2079   DispatchEventUsingWindowDispatcher(&release);
2080   EXPECT_TRUE(delegate->pinch_end());
2081
2082   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId2, delegate.get());
2083   EXPECT_TRUE(delegate->scroll_update());
2084
2085   // Pinch again
2086   delegate->Reset();
2087   ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2088                         kTouchId1, tes.Now());
2089   DispatchEventUsingWindowDispatcher(&press3);
2090   // Now the touch points are close. So we will go into two finger tap.
2091   // Move the touch-point enough to break two-finger-tap and enter pinch.
2092   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 50),
2093                        kTouchId1, tes.Now());
2094   DispatchEventUsingWindowDispatcher(&move2);
2095   EXPECT_TRUE(delegate->pinch_begin());
2096
2097   tes.SendScrollEvent(event_processor(), 350, 350, kTouchId1, delegate.get());
2098   EXPECT_TRUE(delegate->pinch_update());
2099 }
2100
2101 TEST_P(GestureRecognizerTest, GestureEventPinchFromTap) {
2102   // Disabled under unified gesture recognizer due to behavior differences in
2103   // scroll and bounding box behavior.
2104   if (UsingUnifiedGR())
2105     return;
2106
2107   scoped_ptr<GestureEventConsumeDelegate> delegate(
2108       new GestureEventConsumeDelegate());
2109   TimedEvents tes;
2110   const int kWindowWidth = 300;
2111   const int kWindowHeight = 400;
2112   const int kTouchId1 = 3;
2113   const int kTouchId2 = 5;
2114   gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
2115   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2116       delegate.get(), -1234, bounds, root_window()));
2117
2118   delegate->Reset();
2119   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
2120                        kTouchId1, tes.Now());
2121   DispatchEventUsingWindowDispatcher(&press);
2122   EXPECT_2_EVENTS(delegate->events(),
2123                   ui::ET_GESTURE_BEGIN,
2124                   ui::ET_GESTURE_TAP_DOWN);
2125   EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2126
2127   // Press the second finger far enough to break two finger tap.
2128   delegate->Reset();
2129   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2130                         kTouchId2, tes.Now());
2131   DispatchEventUsingWindowDispatcher(&press2);
2132   EXPECT_2_EVENTS(delegate->events(),
2133                   ui::ET_GESTURE_TAP_CANCEL,
2134                   ui::ET_GESTURE_BEGIN);
2135   EXPECT_EQ(gfx::Rect(10, 10, 91, 291).ToString(),
2136             delegate->bounding_box().ToString());
2137
2138   // Move the first finger.
2139   delegate->Reset();
2140   ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
2141                        kTouchId1, tes.Now());
2142   DispatchEventUsingWindowDispatcher(&move3);
2143   EXPECT_2_EVENTS(delegate->events(),
2144                   ui::ET_GESTURE_SCROLL_BEGIN,
2145                   ui::ET_GESTURE_PINCH_BEGIN);
2146   EXPECT_EQ(gfx::Rect(10, 10, 55, 191).ToString(),
2147             delegate->bounding_box().ToString());
2148
2149   // Now move the second finger.
2150   delegate->Reset();
2151   ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
2152                        kTouchId2, tes.Now());
2153   DispatchEventUsingWindowDispatcher(&move4);
2154   EXPECT_2_EVENTS(delegate->events(),
2155                   ui::ET_GESTURE_PINCH_UPDATE,
2156                   ui::ET_GESTURE_SCROLL_UPDATE);
2157   EXPECT_EQ(gfx::Rect(55, 15, 10, 186).ToString(),
2158             delegate->bounding_box().ToString());
2159
2160   // Release the first finger. This should end pinch.
2161   delegate->Reset();
2162   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2163                          kTouchId1, tes.LeapForward(10));
2164   DispatchEventUsingWindowDispatcher(&release);
2165   EXPECT_2_EVENTS(delegate->events(),
2166                   ui::ET_GESTURE_PINCH_END,
2167                   ui::ET_GESTURE_END);
2168   EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
2169             delegate->bounding_box().ToString());
2170
2171   // Move the second finger. This should still generate a scroll.
2172   delegate->Reset();
2173   ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
2174                        kTouchId2, tes.Now());
2175   DispatchEventUsingWindowDispatcher(&move5);
2176   EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
2177   EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2178 }
2179
2180 TEST_P(GestureRecognizerTest, GestureEventIgnoresDisconnectedEvents) {
2181   scoped_ptr<GestureEventConsumeDelegate> delegate(
2182       new GestureEventConsumeDelegate());
2183   TimedEvents tes;
2184
2185   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2186                           6, tes.Now());
2187   DispatchEventUsingWindowDispatcher(&release1);
2188   EXPECT_FALSE(delegate->tap());
2189   EXPECT_FALSE(delegate->tap_down());
2190 }
2191
2192 // Check that a touch is locked to the window of the closest current touch
2193 // within max_separation_for_gesture_touches_in_pixels
2194 TEST_P(GestureRecognizerTest, GestureEventTouchLockSelectsCorrectWindow) {
2195   ui::GestureRecognizer* gesture_recognizer = new ui::GestureRecognizerImpl();
2196   TimedEvents tes;
2197   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2198
2199   ui::GestureConsumer* target;
2200   const int kNumWindows = 4;
2201
2202   scoped_ptr<GestureEventConsumeDelegate*[]> delegates(
2203       new GestureEventConsumeDelegate*[kNumWindows]);
2204
2205   ui::GestureConfiguration::
2206       set_max_separation_for_gesture_touches_in_pixels(499);
2207
2208   scoped_ptr<gfx::Rect[]> window_bounds(new gfx::Rect[kNumWindows]);
2209   window_bounds[0] = gfx::Rect(0, 0, 1, 1);
2210   window_bounds[1] = gfx::Rect(500, 0, 1, 1);
2211   window_bounds[2] = gfx::Rect(0, 500, 1, 1);
2212   window_bounds[3] = gfx::Rect(500, 500, 1, 1);
2213
2214   scoped_ptr<aura::Window*[]> windows(new aura::Window*[kNumWindows]);
2215
2216   // Instantiate windows with |window_bounds| and touch each window at
2217   // its origin.
2218   for (int i = 0; i < kNumWindows; ++i) {
2219     delegates[i] = new GestureEventConsumeDelegate();
2220     windows[i] = CreateTestWindowWithDelegate(
2221         delegates[i], i, window_bounds[i], root_window());
2222     windows[i]->set_id(i);
2223     ui::TouchEvent press(ui::ET_TOUCH_PRESSED, window_bounds[i].origin(),
2224                          i, tes.Now());
2225     DispatchEventUsingWindowDispatcher(&press);
2226   }
2227
2228   // Touches should now be associated with the closest touch within
2229   // ui::GestureConfiguration::max_separation_for_gesture_touches_in_pixels
2230   target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 11), -1);
2231   EXPECT_EQ("0", WindowIDAsString(target));
2232   target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 11), -1);
2233   EXPECT_EQ("1", WindowIDAsString(target));
2234   target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 511), -1);
2235   EXPECT_EQ("2", WindowIDAsString(target));
2236   target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 511), -1);
2237   EXPECT_EQ("3", WindowIDAsString(target));
2238
2239   // Add a touch in the middle associated with windows[2]
2240   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 500),
2241                        kNumWindows, tes.Now());
2242   DispatchEventUsingWindowDispatcher(&press);
2243   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(250, 250),
2244                       kNumWindows, tes.Now());
2245   DispatchEventUsingWindowDispatcher(&move);
2246
2247   target = gesture_recognizer->GetTargetForLocation(gfx::Point(250, 250), -1);
2248   EXPECT_EQ("2", WindowIDAsString(target));
2249
2250   // Make sure that ties are broken by distance to a current touch
2251   // Closer to the point in the bottom right.
2252   target = gesture_recognizer->GetTargetForLocation(gfx::Point(380, 380), -1);
2253   EXPECT_EQ("3", WindowIDAsString(target));
2254
2255   // This touch is closer to the point in the middle
2256   target = gesture_recognizer->GetTargetForLocation(gfx::Point(300, 300), -1);
2257   EXPECT_EQ("2", WindowIDAsString(target));
2258
2259   // A touch too far from other touches won't be locked to anything
2260   target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2261   EXPECT_TRUE(target == NULL);
2262
2263   // Move a touch associated with windows[2] to 1000, 1000
2264   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(1000, 1000),
2265                        kNumWindows, tes.Now());
2266   DispatchEventUsingWindowDispatcher(&move2);
2267
2268   target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000), -1);
2269   EXPECT_EQ("2", WindowIDAsString(target));
2270
2271   for (int i = 0; i < kNumWindows; ++i) {
2272     // Delete windows before deleting delegates.
2273     delete windows[i];
2274     delete delegates[i];
2275   }
2276 }
2277
2278 // Check that a touch's target will not be effected by a touch on a different
2279 // screen.
2280 TEST_P(GestureRecognizerTest, GestureEventTouchLockIgnoresOtherScreens) {
2281   scoped_ptr<GestureEventConsumeDelegate> delegate(
2282       new GestureEventConsumeDelegate());
2283   gfx::Rect bounds(0, 0, 10, 10);
2284   scoped_ptr<aura::Window> window(
2285       CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
2286
2287   const int kTouchId1 = 8;
2288   const int kTouchId2 = 2;
2289   TimedEvents tes;
2290
2291   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(5, 5),
2292                         kTouchId1, tes.Now());
2293   ui::EventTestApi test_press1(&press1);
2294   test_press1.set_source_device_id(1);
2295   DispatchEventUsingWindowDispatcher(&press1);
2296
2297   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2298                         kTouchId2, tes.Now());
2299   ui::EventTestApi test_press2(&press2);
2300   test_press2.set_source_device_id(2);
2301   DispatchEventUsingWindowDispatcher(&press2);
2302
2303   // The second press should not have been locked to the same target as the
2304   // first, as they occured on different displays.
2305   EXPECT_NE(
2306       ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1),
2307       ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
2308 }
2309
2310 // Check that touch events outside the root window are still handled
2311 // by the root window's gesture sequence.
2312 TEST_P(GestureRecognizerTest, GestureEventOutsideRootWindowTap) {
2313   // TODO(tdresser): write a version of this test for the unified GR.
2314   if (UsingUnifiedGR())
2315     return;
2316
2317   TestGestureRecognizer* gesture_recognizer =
2318       new TestGestureRecognizer();
2319   TimedEvents tes;
2320   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2321
2322   scoped_ptr<aura::Window> window(CreateTestWindowWithBounds(
2323       gfx::Rect(-100, -100, 2000, 2000), root_window()));
2324
2325   ui::GestureSequence* window_gesture_sequence =
2326       gesture_recognizer->GetGestureSequenceForTesting(window.get());
2327
2328   ui::GestureSequence* root_window_gesture_sequence =
2329       gesture_recognizer->GetGestureSequenceForTesting(root_window());
2330
2331   gfx::Point pos1(-10, -10);
2332   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, pos1, 0, tes.Now());
2333   DispatchEventUsingWindowDispatcher(&press1);
2334
2335   gfx::Point pos2(1000, 1000);
2336   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, pos2, 1, tes.Now());
2337   DispatchEventUsingWindowDispatcher(&press2);
2338
2339   // As these presses were outside the root window, they should be
2340   // associated with the root window.
2341   EXPECT_EQ(0, window_gesture_sequence->point_count());
2342   EXPECT_EQ(2, root_window_gesture_sequence->point_count());
2343 }
2344
2345 TEST_P(GestureRecognizerTest, NoTapWithPreventDefaultedRelease) {
2346   scoped_ptr<QueueTouchEventDelegate> delegate(
2347       new QueueTouchEventDelegate(host()->dispatcher()));
2348   TimedEvents tes;
2349   const int kTouchId = 2;
2350   gfx::Rect bounds(100, 200, 100, 100);
2351   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2352       delegate.get(), -1234, bounds, root_window()));
2353   delegate->set_window(window.get());
2354
2355   delegate->Reset();
2356   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2357                        kTouchId, tes.Now());
2358   DispatchEventUsingWindowDispatcher(&press);
2359   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2360                          kTouchId, tes.LeapForward(50));
2361   DispatchEventUsingWindowDispatcher(&release);
2362
2363   delegate->Reset();
2364   delegate->ReceivedAck();
2365   EXPECT_TRUE(delegate->tap_down());
2366   delegate->Reset();
2367   delegate->ReceivedAckPreventDefaulted();
2368   EXPECT_FALSE(delegate->tap());
2369   EXPECT_TRUE(delegate->tap_cancel());
2370 }
2371
2372 TEST_P(GestureRecognizerTest, PinchScrollWithPreventDefaultedRelease) {
2373   // Disabled for unified GR due to differences in when pinch begin is sent. The
2374   // Aura GR sends it earlier than is necessary.
2375   if (UsingUnifiedGR())
2376     return;
2377
2378   scoped_ptr<QueueTouchEventDelegate> delegate(
2379       new QueueTouchEventDelegate(host()->dispatcher()));
2380   TimedEvents tes;
2381   const int kTouchId1 = 7;
2382   const int kTouchId2 = 5;
2383   gfx::Rect bounds(10, 20, 100, 100);
2384   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2385       delegate.get(), -1234, bounds, root_window()));
2386   delegate->set_window(window.get());
2387
2388   {
2389     delegate->Reset();
2390     ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2391                          tes.Now());
2392     ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2393                         tes.LeapForward(200));
2394     ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2395                            tes.LeapForward(50));
2396     DispatchEventUsingWindowDispatcher(&press);
2397     DispatchEventUsingWindowDispatcher(&move);
2398     DispatchEventUsingWindowDispatcher(&release);
2399     delegate->Reset();
2400
2401     // Ack the press event.
2402     delegate->ReceivedAck();
2403     EXPECT_TRUE(delegate->tap_down());
2404     delegate->Reset();
2405
2406     // Ack the move event.
2407     delegate->ReceivedAck();
2408     EXPECT_TRUE(delegate->tap_cancel());
2409     EXPECT_TRUE(delegate->scroll_begin());
2410     delegate->Reset();
2411
2412     // Ack the release event. Although the release event has been processed, it
2413     // should still generate a scroll-end event.
2414     delegate->ReceivedAckPreventDefaulted();
2415     EXPECT_TRUE(delegate->scroll_end());
2416   }
2417
2418   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2419                        tes.Now());
2420   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2421                       tes.LeapForward(200));
2422   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2423                          tes.LeapForward(50));
2424   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 25), kTouchId2,
2425                         tes.Now());
2426   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(45, 85), kTouchId2,
2427                        tes.LeapForward(1000));
2428   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(45, 85), kTouchId2,
2429                           tes.LeapForward(14));
2430
2431   // Do a pinch.
2432   DispatchEventUsingWindowDispatcher(&press);
2433   DispatchEventUsingWindowDispatcher(&move);
2434   DispatchEventUsingWindowDispatcher(&press2);
2435   DispatchEventUsingWindowDispatcher(&move2);
2436   DispatchEventUsingWindowDispatcher(&release);
2437   DispatchEventUsingWindowDispatcher(&release2);
2438
2439   // Ack the press and move events.
2440   delegate->Reset();
2441   delegate->ReceivedAck();
2442   EXPECT_TRUE(delegate->begin());
2443   EXPECT_TRUE(delegate->tap_down());
2444
2445   delegate->Reset();
2446   delegate->ReceivedAck();
2447   EXPECT_TRUE(delegate->scroll_begin());
2448
2449   delegate->Reset();
2450   delegate->ReceivedAck();
2451   EXPECT_TRUE(delegate->begin());
2452   EXPECT_TRUE(delegate->pinch_begin());
2453
2454   delegate->Reset();
2455   delegate->ReceivedAck();
2456   EXPECT_TRUE(delegate->pinch_update());
2457
2458   // Ack the first release. Although the release is processed, it should still
2459   // generate a pinch-end event.
2460   delegate->Reset();
2461   delegate->ReceivedAckPreventDefaulted();
2462   EXPECT_TRUE(delegate->pinch_end());
2463   EXPECT_TRUE(delegate->end());
2464
2465   delegate->Reset();
2466   delegate->ReceivedAckPreventDefaulted();
2467   EXPECT_TRUE(delegate->scroll_end());
2468   EXPECT_TRUE(delegate->end());
2469 }
2470
2471 TEST_P(GestureRecognizerTest, GestureEndLocation) {
2472   GestureEventConsumeDelegate delegate;
2473   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2474       &delegate, -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2475   ui::test::EventGenerator generator(root_window(), window.get());
2476   const gfx::Point begin(20, 20);
2477   const gfx::Point end(150, 150);
2478   const gfx::Vector2d window_offset =
2479       window->bounds().origin().OffsetFromOrigin();
2480   generator.GestureScrollSequence(begin, end,
2481                                   base::TimeDelta::FromMilliseconds(20),
2482                                   10);
2483   EXPECT_EQ((begin - window_offset).ToString(),
2484             delegate.scroll_begin_position().ToString());
2485   EXPECT_EQ((end - window_offset).ToString(),
2486             delegate.gesture_end_location().ToString());
2487 }
2488
2489 TEST_P(GestureRecognizerTest, CaptureSendsGestureEnd) {
2490   scoped_ptr<GestureEventConsumeDelegate> delegate(
2491       new GestureEventConsumeDelegate());
2492   TestGestureRecognizer* gesture_recognizer =
2493       new TestGestureRecognizer();
2494   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2495
2496   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2497       delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2498   ui::test::EventGenerator generator(root_window());
2499
2500   generator.MoveMouseRelativeTo(window.get(), gfx::Point(10, 10));
2501   generator.PressTouch();
2502   RunAllPendingInMessageLoop();
2503
2504   EXPECT_TRUE(delegate->tap_down());
2505
2506   scoped_ptr<aura::Window> capture(CreateTestWindowWithBounds(
2507       gfx::Rect(10, 10, 200, 200), root_window()));
2508   capture->SetCapture();
2509   RunAllPendingInMessageLoop();
2510
2511   EXPECT_TRUE(delegate->end());
2512   EXPECT_TRUE(delegate->tap_cancel());
2513 }
2514
2515 // Check that previous touch actions that are completely finished (either
2516 // released or cancelled), do not receive extra synthetic cancels upon change of
2517 // capture.
2518 TEST_P(GestureRecognizerTest, CaptureDoesNotCancelFinishedTouches) {
2519   scoped_ptr<GestureEventConsumeDelegate> delegate(
2520       new GestureEventConsumeDelegate());
2521   scoped_ptr<TestEventHandler> handler(new TestEventHandler);
2522   root_window()->AddPreTargetHandler(handler.get());
2523
2524   // Create a window and set it as the capture window.
2525   scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(delegate.get(),
2526       -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2527   window1->SetCapture();
2528
2529   ui::test::EventGenerator generator(root_window());
2530   TimedEvents tes;
2531
2532   // Generate two touch-press events on the window.
2533   scoped_ptr<ui::TouchEvent> touch0(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2534                                                        gfx::Point(20, 20), 0,
2535                                                        tes.Now()));
2536   scoped_ptr<ui::TouchEvent> touch1(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
2537                                                        gfx::Point(30, 30), 1,
2538                                                        tes.Now()));
2539   generator.Dispatch(touch0.get());
2540   generator.Dispatch(touch1.get());
2541   RunAllPendingInMessageLoop();
2542   EXPECT_EQ(2, handler->touch_pressed_count());
2543
2544   // Advance time.
2545   tes.LeapForward(1000);
2546
2547   // End the two touches, one by a touch-release and one by a touch-cancel; to
2548   // cover both cases.
2549   touch0.reset(new ui::TouchEvent(ui::ET_TOUCH_RELEASED, gfx::Point(20, 20), 0,
2550                                   tes.Now()));
2551   touch1.reset(new ui::TouchEvent(ui::ET_TOUCH_CANCELLED, gfx::Point(30, 30), 1,
2552                                   tes.Now()));
2553   generator.Dispatch(touch0.get());
2554   generator.Dispatch(touch1.get());
2555   RunAllPendingInMessageLoop();
2556   EXPECT_EQ(1, handler->touch_released_count());
2557   EXPECT_EQ(1, handler->touch_cancelled_count());
2558
2559   // Create a new window and set it as the new capture window.
2560   scoped_ptr<aura::Window> window2(CreateTestWindowWithBounds(
2561       gfx::Rect(100, 100, 300, 300), root_window()));
2562   window2->SetCapture();
2563   RunAllPendingInMessageLoop();
2564   // Check that setting capture does not generate any synthetic touch-cancels
2565   // for the two previously finished touch actions.
2566   EXPECT_EQ(1, handler->touch_cancelled_count());
2567
2568   root_window()->RemovePreTargetHandler(handler.get());
2569 }
2570
2571 // Tests that a press with the same touch id as an existing touch is ignored.
2572 TEST_P(GestureRecognizerTest, PressDoesNotCrash) {
2573   scoped_ptr<GestureEventConsumeDelegate> delegate(
2574       new GestureEventConsumeDelegate());
2575   TestGestureRecognizer* gesture_recognizer =
2576       new TestGestureRecognizer();
2577   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
2578   TimedEvents tes;
2579
2580   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2581       delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), root_window()));
2582
2583   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(45, 45), 7, tes.Now());
2584   press.set_radius_x(40);
2585   DispatchEventUsingWindowDispatcher(&press);
2586   EXPECT_TRUE(delegate->tap_down());
2587   EXPECT_EQ(gfx::Rect(5, 5, 80, 80).ToString(),
2588             delegate->bounding_box().ToString());
2589   delegate->Reset();
2590
2591   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 45), 7, tes.Now());
2592   DispatchEventUsingWindowDispatcher(&press2);
2593
2594   // This new press should not generate a tap-down.
2595   EXPECT_FALSE(delegate->begin());
2596   EXPECT_FALSE(delegate->tap_down());
2597   EXPECT_FALSE(delegate->tap_cancel());
2598   EXPECT_FALSE(delegate->scroll_begin());
2599 }
2600
2601 TEST_P(GestureRecognizerTest, TwoFingerTap) {
2602   // TODO(tdresser): enable this test with unified GR once two finger tap is
2603   // supported. See crbug.com/354396.
2604   if (UsingUnifiedGR())
2605     return;
2606
2607   scoped_ptr<GestureEventConsumeDelegate> delegate(
2608       new GestureEventConsumeDelegate());
2609   const int kWindowWidth = 123;
2610   const int kWindowHeight = 45;
2611   const int kTouchId1 = 2;
2612   const int kTouchId2 = 3;
2613   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2614   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2615       delegate.get(), -1234, bounds, root_window()));
2616   TimedEvents tes;
2617
2618   delegate->Reset();
2619   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2620                         kTouchId1, tes.Now());
2621   DispatchEventUsingWindowDispatcher(&press1);
2622   EXPECT_FALSE(delegate->tap());
2623   EXPECT_TRUE(delegate->tap_down());
2624   EXPECT_FALSE(delegate->tap_cancel());
2625   EXPECT_FALSE(delegate->scroll_begin());
2626   EXPECT_FALSE(delegate->scroll_update());
2627   EXPECT_FALSE(delegate->scroll_end());
2628   EXPECT_FALSE(delegate->long_press());
2629   EXPECT_FALSE(delegate->two_finger_tap());
2630
2631   delegate->Reset();
2632   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2633                         kTouchId2, tes.Now());
2634   DispatchEventUsingWindowDispatcher(&press2);
2635   EXPECT_FALSE(delegate->tap());
2636   EXPECT_FALSE(delegate->tap_down());  // no touch down for second tap.
2637   EXPECT_TRUE(delegate->tap_cancel());
2638   EXPECT_FALSE(delegate->scroll_begin());
2639   EXPECT_FALSE(delegate->scroll_update());
2640   EXPECT_FALSE(delegate->scroll_end());
2641   EXPECT_FALSE(delegate->long_press());
2642   EXPECT_FALSE(delegate->two_finger_tap());
2643
2644   // Little bit of touch move should not affect our state.
2645   delegate->Reset();
2646   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(102, 202),
2647                        kTouchId1, tes.Now());
2648   DispatchEventUsingWindowDispatcher(&move1);
2649   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(131, 202),
2650                        kTouchId2, tes.Now());
2651   DispatchEventUsingWindowDispatcher(&move2);
2652   EXPECT_FALSE(delegate->tap());
2653   EXPECT_FALSE(delegate->tap_down());
2654   EXPECT_FALSE(delegate->tap_cancel());
2655   EXPECT_FALSE(delegate->scroll_begin());
2656   EXPECT_FALSE(delegate->scroll_update());
2657   EXPECT_FALSE(delegate->scroll_end());
2658   EXPECT_FALSE(delegate->long_press());
2659   EXPECT_FALSE(delegate->two_finger_tap());
2660
2661   // Make sure there is enough delay before the touch is released so that it is
2662   // recognized as a tap.
2663   delegate->Reset();
2664   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2665                           kTouchId1, tes.LeapForward(50));
2666
2667   DispatchEventUsingWindowDispatcher(&release1);
2668   EXPECT_FALSE(delegate->tap());
2669   EXPECT_FALSE(delegate->tap_down());
2670   EXPECT_FALSE(delegate->tap_cancel());
2671   EXPECT_TRUE(delegate->scroll_begin());
2672   EXPECT_FALSE(delegate->scroll_update());
2673   EXPECT_FALSE(delegate->scroll_end());
2674   EXPECT_TRUE(delegate->two_finger_tap());
2675
2676   // Lift second finger.
2677   // Make sure there is enough delay before the touch is released so that it is
2678   // recognized as a tap.
2679   delegate->Reset();
2680   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2681                           kTouchId2, tes.LeapForward(50));
2682
2683   DispatchEventUsingWindowDispatcher(&release2);
2684   EXPECT_FALSE(delegate->tap());
2685   EXPECT_FALSE(delegate->tap_down());
2686   EXPECT_FALSE(delegate->tap_cancel());
2687   EXPECT_FALSE(delegate->scroll_begin());
2688   EXPECT_FALSE(delegate->scroll_update());
2689   EXPECT_FALSE(delegate->scroll_end());
2690   EXPECT_TRUE(delegate->fling());
2691   EXPECT_FALSE(delegate->two_finger_tap());
2692 }
2693
2694 TEST_P(GestureRecognizerTest, TwoFingerTapExpired) {
2695   scoped_ptr<GestureEventConsumeDelegate> delegate(
2696       new GestureEventConsumeDelegate());
2697   const int kWindowWidth = 123;
2698   const int kWindowHeight = 45;
2699   const int kTouchId1 = 2;
2700   const int kTouchId2 = 3;
2701   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2702   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2703       delegate.get(), -1234, bounds, root_window()));
2704   TimedEvents tes;
2705
2706   delegate->Reset();
2707   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2708                         kTouchId1, tes.Now());
2709   DispatchEventUsingWindowDispatcher(&press1);
2710
2711   delegate->Reset();
2712   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2713                         kTouchId2, tes.Now());
2714   DispatchEventUsingWindowDispatcher(&press2);
2715
2716   // Send release event after sufficient delay so that two finger time expires.
2717   delegate->Reset();
2718   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2719                           kTouchId1, tes.LeapForward(1000));
2720
2721   DispatchEventUsingWindowDispatcher(&release1);
2722   EXPECT_FALSE(delegate->two_finger_tap());
2723
2724   // Lift second finger.
2725   // Make sure there is enough delay before the touch is released so that it is
2726   // recognized as a tap.
2727   delegate->Reset();
2728   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2729                           kTouchId2, tes.LeapForward(50));
2730
2731   DispatchEventUsingWindowDispatcher(&release2);
2732   EXPECT_FALSE(delegate->two_finger_tap());
2733 }
2734
2735 TEST_P(GestureRecognizerTest, TwoFingerTapChangesToPinch) {
2736   scoped_ptr<GestureEventConsumeDelegate> delegate(
2737       new GestureEventConsumeDelegate());
2738   const int kWindowWidth = 123;
2739   const int kWindowHeight = 45;
2740   const int kTouchId1 = 2;
2741   const int kTouchId2 = 3;
2742   TimedEvents tes;
2743
2744   // Test moving first finger
2745   {
2746     gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2747     scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2748         delegate.get(), -1234, bounds, root_window()));
2749
2750     delegate->Reset();
2751     ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2752                           kTouchId1, tes.Now());
2753     DispatchEventUsingWindowDispatcher(&press1);
2754
2755     delegate->Reset();
2756     ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2757                           kTouchId2, tes.Now());
2758     DispatchEventUsingWindowDispatcher(&press2);
2759
2760     tes.SendScrollEvent(event_processor(), 230, 330, kTouchId1, delegate.get());
2761     EXPECT_FALSE(delegate->two_finger_tap());
2762     EXPECT_TRUE(delegate->pinch_begin());
2763
2764     // Make sure there is enough delay before the touch is released so that it
2765     // is recognized as a tap.
2766     delegate->Reset();
2767     ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2768                            kTouchId2, tes.LeapForward(50));
2769
2770     DispatchEventUsingWindowDispatcher(&release);
2771     EXPECT_FALSE(delegate->two_finger_tap());
2772     EXPECT_TRUE(delegate->pinch_end());
2773   }
2774
2775   // Test moving second finger
2776   {
2777     gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2778     scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2779         delegate.get(), -1234, bounds, root_window()));
2780
2781     delegate->Reset();
2782     ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2783                           kTouchId1, tes.Now());
2784     DispatchEventUsingWindowDispatcher(&press1);
2785
2786     delegate->Reset();
2787     ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2788                           kTouchId2, tes.Now());
2789     DispatchEventUsingWindowDispatcher(&press2);
2790
2791     tes.SendScrollEvent(event_processor(), 301, 230, kTouchId2, delegate.get());
2792     EXPECT_FALSE(delegate->two_finger_tap());
2793     EXPECT_TRUE(delegate->pinch_begin());
2794
2795     // Make sure there is enough delay before the touch is released so that it
2796     // is recognized as a tap.
2797     delegate->Reset();
2798     ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2799                            kTouchId1, tes.LeapForward(50));
2800
2801     DispatchEventUsingWindowDispatcher(&release);
2802     EXPECT_FALSE(delegate->two_finger_tap());
2803     EXPECT_TRUE(delegate->pinch_end());
2804   }
2805 }
2806
2807 TEST_P(GestureRecognizerTest, NoTwoFingerTapWhenFirstFingerHasScrolled) {
2808   // Disabled for unified GR due to differences in when pinch begin is sent. The
2809   // Aura GR sends it earlier than is necessary.
2810   if (UsingUnifiedGR())
2811     return;
2812
2813   scoped_ptr<GestureEventConsumeDelegate> delegate(
2814       new GestureEventConsumeDelegate());
2815   const int kWindowWidth = 123;
2816   const int kWindowHeight = 45;
2817   const int kTouchId1 = 2;
2818   const int kTouchId2 = 3;
2819   TimedEvents tes;
2820
2821   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2822   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2823       delegate.get(), -1234, bounds, root_window()));
2824
2825   delegate->Reset();
2826   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2827                         kTouchId1, tes.Now());
2828   DispatchEventUsingWindowDispatcher(&press1);
2829   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId1, delegate.get());
2830
2831   delegate->Reset();
2832   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2833                         kTouchId2, tes.Now());
2834   DispatchEventUsingWindowDispatcher(&press2);
2835
2836   EXPECT_TRUE(delegate->pinch_begin());
2837
2838   // Make sure there is enough delay before the touch is released so that it
2839   // is recognized as a tap.
2840   delegate->Reset();
2841   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2842                          kTouchId2, tes.LeapForward(50));
2843
2844   DispatchEventUsingWindowDispatcher(&release);
2845   EXPECT_FALSE(delegate->two_finger_tap());
2846   EXPECT_TRUE(delegate->pinch_end());
2847 }
2848
2849 TEST_P(GestureRecognizerTest, MultiFingerSwipe) {
2850   scoped_ptr<GestureEventConsumeDelegate> delegate(
2851       new GestureEventConsumeDelegate());
2852   const int kWindowWidth = 123;
2853   const int kWindowHeight = 45;
2854
2855   gfx::Rect bounds(5, 10, kWindowWidth, kWindowHeight);
2856   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2857       delegate.get(), -1234, bounds, root_window()));
2858
2859   const int kSteps = 15;
2860   const int kTouchPoints = 4;
2861   gfx::Point points[kTouchPoints] = {
2862     gfx::Point(10, 30),
2863     gfx::Point(30, 20),
2864     gfx::Point(50, 30),
2865     gfx::Point(80, 50)
2866   };
2867
2868   ui::test::EventGenerator generator(root_window(), window.get());
2869
2870   // The unified gesture recognizer assumes a finger has stopped if it hasn't
2871   // moved for too long. See ui/events/gesture_detection/velocity_tracker.cc's
2872   // kAssumePointerStoppedTimeMs.
2873   for (int count = 2; count <= kTouchPoints; ++count) {
2874     generator.GestureMultiFingerScroll(
2875         count, points, 10, kSteps, 0, -11 * kSteps);
2876     EXPECT_TRUE(delegate->swipe_up());
2877     delegate->Reset();
2878
2879     generator.GestureMultiFingerScroll(
2880         count, points, 10, kSteps, 0, 11 * kSteps);
2881     EXPECT_TRUE(delegate->swipe_down());
2882     delegate->Reset();
2883
2884     generator.GestureMultiFingerScroll(
2885         count, points, 10, kSteps, -11 * kSteps, 0);
2886     EXPECT_TRUE(delegate->swipe_left());
2887     delegate->Reset();
2888
2889     generator.GestureMultiFingerScroll(
2890         count, points, 10, kSteps, 11 * kSteps, 0);
2891     EXPECT_TRUE(delegate->swipe_right());
2892     delegate->Reset();
2893
2894     generator.GestureMultiFingerScroll(
2895         count, points, 10, kSteps, 5 * kSteps, 12 * kSteps);
2896     EXPECT_FALSE(delegate->swipe_down());
2897     delegate->Reset();
2898
2899     generator.GestureMultiFingerScroll(
2900         count, points, 10, kSteps, 4 * kSteps, 12 * kSteps);
2901     EXPECT_TRUE(delegate->swipe_down());
2902     delegate->Reset();
2903
2904     generator.GestureMultiFingerScroll(
2905         count, points, 10, kSteps, 3 * kSteps, 12 * kSteps);
2906     EXPECT_TRUE(delegate->swipe_down());
2907     delegate->Reset();
2908   }
2909 }
2910
2911 TEST_P(GestureRecognizerTest, TwoFingerTapCancelled) {
2912   scoped_ptr<GestureEventConsumeDelegate> delegate(
2913       new GestureEventConsumeDelegate());
2914   const int kWindowWidth = 123;
2915   const int kWindowHeight = 45;
2916   const int kTouchId1 = 2;
2917   const int kTouchId2 = 3;
2918   TimedEvents tes;
2919
2920   // Test canceling first finger.
2921   {
2922     gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2923     scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2924         delegate.get(), -1234, bounds, root_window()));
2925
2926     delegate->Reset();
2927     ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2928                           kTouchId1, tes.Now());
2929     DispatchEventUsingWindowDispatcher(&press1);
2930
2931     delegate->Reset();
2932     ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2933                           kTouchId2, tes.Now());
2934     DispatchEventUsingWindowDispatcher(&press2);
2935
2936     delegate->Reset();
2937     ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2938                           kTouchId1, tes.Now());
2939     DispatchEventUsingWindowDispatcher(&cancel);
2940     EXPECT_FALSE(delegate->two_finger_tap());
2941
2942     // Make sure there is enough delay before the touch is released so that it
2943     // is recognized as a tap.
2944     delegate->Reset();
2945     ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2946                            kTouchId2, tes.LeapForward(50));
2947
2948     DispatchEventUsingWindowDispatcher(&release);
2949     EXPECT_FALSE(delegate->two_finger_tap());
2950   }
2951
2952   // Test canceling second finger
2953   {
2954     gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2955     scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2956         delegate.get(), -1234, bounds, root_window()));
2957
2958     delegate->Reset();
2959     ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2960                           kTouchId1, tes.Now());
2961     DispatchEventUsingWindowDispatcher(&press1);
2962
2963     delegate->Reset();
2964     ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2965                           kTouchId2, tes.Now());
2966     DispatchEventUsingWindowDispatcher(&press2);
2967
2968     delegate->Reset();
2969     ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2970                           kTouchId2, tes.Now());
2971     DispatchEventUsingWindowDispatcher(&cancel);
2972     EXPECT_FALSE(delegate->two_finger_tap());
2973
2974     // Make sure there is enough delay before the touch is released so that it
2975     // is recognized as a tap.
2976     delegate->Reset();
2977     ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2978                            kTouchId1, tes.LeapForward(50));
2979
2980     DispatchEventUsingWindowDispatcher(&release);
2981     EXPECT_FALSE(delegate->two_finger_tap());
2982   }
2983 }
2984
2985 TEST_P(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) {
2986   // Disabled for unified GR due to differences in when scroll update is
2987   // sent. The Aura GR will never send a ScrollUpdate with a ScrollBegin, but
2988   // the unified GR will.
2989   if (UsingUnifiedGR())
2990     return;
2991
2992   scoped_ptr<GestureEventConsumeDelegate> delegate(
2993       new GestureEventConsumeDelegate());
2994   const int kWindowWidth = 523;
2995   const int kWindowHeight = 45;
2996   const int kTouchId1 = 2;
2997   const int kTouchId2 = 3;
2998   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2999   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3000       delegate.get(), -1234, bounds, root_window()));
3001   TimedEvents tes;
3002
3003   delegate->Reset();
3004   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3005                         kTouchId1, tes.Now());
3006   DispatchEventUsingWindowDispatcher(&press1);
3007   EXPECT_FALSE(delegate->tap());
3008   EXPECT_TRUE(delegate->tap_down());
3009   EXPECT_FALSE(delegate->tap_cancel());
3010   EXPECT_FALSE(delegate->scroll_begin());
3011   EXPECT_FALSE(delegate->scroll_update());
3012   EXPECT_FALSE(delegate->scroll_end());
3013   EXPECT_FALSE(delegate->long_press());
3014   EXPECT_FALSE(delegate->two_finger_tap());
3015
3016   delegate->Reset();
3017   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201),
3018                         kTouchId2, tes.Now());
3019   DispatchEventUsingWindowDispatcher(&press2);
3020   EXPECT_FALSE(delegate->tap());
3021   EXPECT_FALSE(delegate->tap_down());  // no touch down for second tap.
3022   EXPECT_TRUE(delegate->tap_cancel());
3023   EXPECT_FALSE(delegate->scroll_begin());
3024   EXPECT_FALSE(delegate->scroll_update());
3025   EXPECT_FALSE(delegate->scroll_end());
3026   EXPECT_FALSE(delegate->long_press());
3027   EXPECT_FALSE(delegate->two_finger_tap());
3028   EXPECT_FALSE(delegate->pinch_begin());
3029
3030   delegate->Reset();
3031   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(530, 301),
3032                        kTouchId2, tes.Now());
3033   DispatchEventUsingWindowDispatcher(&move2);
3034   EXPECT_FALSE(delegate->tap());
3035   EXPECT_FALSE(delegate->tap_down());
3036   EXPECT_FALSE(delegate->tap_cancel());
3037   // Pinch & Scroll only when there is enough movement.
3038   EXPECT_TRUE(delegate->scroll_begin());
3039   EXPECT_FALSE(delegate->scroll_update());
3040   EXPECT_FALSE(delegate->scroll_end());
3041   EXPECT_FALSE(delegate->long_press());
3042   EXPECT_FALSE(delegate->two_finger_tap());
3043   EXPECT_TRUE(delegate->pinch_begin());
3044 }
3045
3046 // Verifies if a window is the target of multiple touch-ids and we hide the
3047 // window everything is cleaned up correctly.
3048 TEST_P(GestureRecognizerTest, FlushAllOnHide) {
3049   scoped_ptr<GestureEventConsumeDelegate> delegate(
3050       new GestureEventConsumeDelegate());
3051   gfx::Rect bounds(0, 0, 200, 200);
3052   scoped_ptr<aura::Window> window(
3053       CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
3054   const int kTouchId1 = 8;
3055   const int kTouchId2 = 2;
3056   TimedEvents tes;
3057
3058   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
3059                         kTouchId1, tes.Now());
3060   DispatchEventUsingWindowDispatcher(&press1);
3061   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
3062                         kTouchId2, tes.Now());
3063   DispatchEventUsingWindowDispatcher(&press2);
3064   window->Hide();
3065   EXPECT_EQ(NULL,
3066       ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1));
3067   EXPECT_EQ(NULL,
3068       ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
3069 }
3070
3071 TEST_P(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) {
3072   // TODO(tdresser): write a version of this test for the unified GR.
3073   if (UsingUnifiedGR())
3074     return;
3075
3076   scoped_ptr<QueueTouchEventDelegate> delegate(
3077       new QueueTouchEventDelegate(host()->dispatcher()));
3078   const int kTouchId = 2;
3079   gfx::Rect bounds(100, 200, 100, 100);
3080   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3081       delegate.get(), -1234, bounds, root_window()));
3082   delegate->set_window(window.get());
3083   TimedEvents tes;
3084
3085   TimerTestGestureRecognizer* gesture_recognizer =
3086       new TimerTestGestureRecognizer();
3087   TimerTestGestureSequence* gesture_sequence =
3088       static_cast<TimerTestGestureSequence*>(
3089           gesture_recognizer->GetGestureSequenceForTesting(window.get()));
3090
3091   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
3092
3093   delegate->Reset();
3094   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3095                        kTouchId, tes.Now());
3096   DispatchEventUsingWindowDispatcher(&press);
3097   // Scroll around, to cancel the long press
3098   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3099
3100   delegate->Reset();
3101   delegate->ReceivedAck();
3102   EXPECT_TRUE(delegate->tap_down());
3103   EXPECT_TRUE(gesture_sequence->IsTimerRunning());
3104
3105   delegate->Reset();
3106   delegate->ReceivedAckPreventDefaulted();
3107   EXPECT_FALSE(gesture_sequence->IsTimerRunning());
3108   gesture_sequence->ForceTimeout();
3109   EXPECT_FALSE(delegate->long_press());
3110 }
3111
3112 // Same as GestureEventConsumeDelegate, but consumes all the touch-move events.
3113 class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate {
3114  public:
3115   ConsumesTouchMovesDelegate() : consume_touch_move_(true) {}
3116   virtual ~ConsumesTouchMovesDelegate() {}
3117
3118   void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; }
3119
3120  private:
3121   virtual void OnTouchEvent(ui::TouchEvent* touch) OVERRIDE {
3122     if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED)
3123       touch->SetHandled();
3124     else
3125       GestureEventConsumeDelegate::OnTouchEvent(touch);
3126   }
3127
3128   bool consume_touch_move_;
3129
3130   DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate);
3131 };
3132
3133 // Same as GestureEventScroll, but tests that the behavior is the same
3134 // even if all the touch-move events are consumed.
3135 TEST_P(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) {
3136   scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3137       new ConsumesTouchMovesDelegate());
3138   const int kWindowWidth = 123;
3139   const int kWindowHeight = 45;
3140   const int kTouchId = 5;
3141   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3142   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3143       delegate.get(), -1234, bounds, root_window()));
3144   TimedEvents tes;
3145
3146   delegate->Reset();
3147   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3148                        kTouchId, tes.Now());
3149   DispatchEventUsingWindowDispatcher(&press);
3150   EXPECT_FALSE(delegate->tap());
3151   EXPECT_TRUE(delegate->tap_down());
3152   EXPECT_FALSE(delegate->tap_cancel());
3153   EXPECT_TRUE(delegate->begin());
3154   EXPECT_FALSE(delegate->scroll_begin());
3155   EXPECT_FALSE(delegate->scroll_update());
3156   EXPECT_FALSE(delegate->scroll_end());
3157
3158   // Move the touch-point enough so that it would normally be considered a
3159   // scroll. But since the touch-moves will be consumed, the scroll should not
3160   // start.
3161   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3162   EXPECT_FALSE(delegate->tap());
3163   EXPECT_FALSE(delegate->tap_down());
3164   EXPECT_TRUE(delegate->tap_cancel());
3165   EXPECT_FALSE(delegate->begin());
3166   EXPECT_FALSE(delegate->scroll_update());
3167   EXPECT_FALSE(delegate->scroll_end());
3168
3169   // With the unified gesture detector, consuming the first touch move event
3170   // won't prevent all future scrolling.
3171   if (UsingUnifiedGR())
3172     EXPECT_TRUE(delegate->scroll_begin());
3173   else
3174     EXPECT_FALSE(delegate->scroll_begin());
3175
3176   // Release the touch back at the start point. This should end without causing
3177   // a tap.
3178   delegate->Reset();
3179   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230),
3180                          kTouchId, tes.LeapForward(50));
3181   DispatchEventUsingWindowDispatcher(&release);
3182   EXPECT_FALSE(delegate->tap());
3183   EXPECT_FALSE(delegate->tap_down());
3184   EXPECT_FALSE(delegate->tap_cancel());
3185   EXPECT_FALSE(delegate->begin());
3186   EXPECT_TRUE(delegate->end());
3187   EXPECT_FALSE(delegate->scroll_begin());
3188   EXPECT_FALSE(delegate->scroll_update());
3189
3190   if (UsingUnifiedGR())
3191     EXPECT_TRUE(delegate->scroll_end());
3192   else
3193     EXPECT_FALSE(delegate->scroll_end());
3194 }
3195
3196 // Tests the behavior of 2F scroll when all the touch-move events are consumed.
3197 TEST_P(GestureRecognizerTest, GestureEventScrollTwoFingerTouchMoveConsumed) {
3198   // TODO(tdresser): enable this test with unified GR once two finger tap is
3199   // supported. See crbug.com/354396.
3200   if (UsingUnifiedGR())
3201     return;
3202
3203   scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3204       new ConsumesTouchMovesDelegate());
3205   const int kWindowWidth = 123;
3206   const int kWindowHeight = 100;
3207   const int kTouchId1 = 2;
3208   const int kTouchId2 = 3;
3209   TimedEvents tes;
3210
3211   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3212   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3213       delegate.get(), -1234, bounds, root_window()));
3214
3215   delegate->Reset();
3216   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3217                         kTouchId1, tes.Now());
3218   DispatchEventUsingWindowDispatcher(&press1);
3219   tes.SendScrollEvent(event_processor(), 131, 231, kTouchId1, delegate.get());
3220
3221   // First finger touches down and moves.
3222   EXPECT_FALSE(delegate->tap());
3223   EXPECT_FALSE(delegate->scroll_begin());
3224   EXPECT_FALSE(delegate->scroll_update());
3225   EXPECT_FALSE(delegate->scroll_end());
3226
3227   delegate->Reset();
3228   // Second finger touches down and moves.
3229   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
3230                         kTouchId2, tes.LeapForward(50));
3231   DispatchEventUsingWindowDispatcher(&press2);
3232   tes.SendScrollEvent(event_processor(), 161, 231, kTouchId2, delegate.get());
3233
3234   // PinchBegin & ScrollBegin were not sent if the touch-move events were
3235   // consumed.
3236   EXPECT_FALSE(delegate->pinch_begin());
3237   EXPECT_FALSE(delegate->scroll_begin());
3238
3239   EXPECT_FALSE(delegate->tap());
3240   EXPECT_FALSE(delegate->two_finger_tap());
3241
3242   // Should be no PinchUpdate & ScrollUpdate.
3243   EXPECT_FALSE(delegate->pinch_update());
3244   EXPECT_FALSE(delegate->pinch_end());
3245   EXPECT_FALSE(delegate->scroll_update());
3246   EXPECT_FALSE(delegate->scroll_end());
3247
3248   delegate->Reset();
3249   // Moves First finger again, no PinchUpdate & ScrollUpdate.
3250   tes.SendScrollEvent(event_processor(), 161, 261, kTouchId1, delegate.get());
3251
3252   EXPECT_FALSE(delegate->pinch_update());
3253   EXPECT_FALSE(delegate->pinch_end());
3254   EXPECT_FALSE(delegate->scroll_update());
3255   EXPECT_FALSE(delegate->scroll_end());
3256
3257   // Stops consuming touch-move.
3258   delegate->set_consume_touch_move(false);
3259
3260   delegate->Reset();
3261   // Making a pinch gesture.
3262   tes.SendScrollEvent(event_processor(), 161, 251, kTouchId1, delegate.get());
3263   // If touch moves are ever consumed, we should not see PinchBegin/Update
3264   // even touch moves become not consumed.
3265   EXPECT_FALSE(delegate->scroll_begin());
3266   EXPECT_FALSE(delegate->scroll_update());
3267   EXPECT_FALSE(delegate->scroll_end());
3268
3269   EXPECT_FALSE(delegate->pinch_begin());
3270   EXPECT_FALSE(delegate->pinch_update());
3271   EXPECT_FALSE(delegate->pinch_end());
3272
3273   delegate->Reset();
3274   tes.SendScrollEvent(event_processor(), 161, 241, kTouchId2, delegate.get());
3275   EXPECT_FALSE(delegate->scroll_begin());
3276   EXPECT_FALSE(delegate->scroll_update());
3277   EXPECT_FALSE(delegate->scroll_end());
3278
3279   EXPECT_FALSE(delegate->pinch_begin());
3280   EXPECT_FALSE(delegate->pinch_update());
3281   EXPECT_FALSE(delegate->pinch_end());
3282
3283   delegate->Reset();
3284   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3285                           kTouchId1, tes.Now());
3286   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
3287                           kTouchId2, tes.Now());
3288   DispatchEventUsingWindowDispatcher(&release1);
3289   DispatchEventUsingWindowDispatcher(&release2);
3290
3291   EXPECT_FALSE(delegate->tap());
3292   // Touch release is not consumed, so we still see two finger tap.
3293   EXPECT_TRUE(delegate->two_finger_tap());
3294
3295   // Should not see PinchEnd.
3296   EXPECT_TRUE(delegate->scroll_begin());
3297   EXPECT_FALSE(delegate->scroll_update());
3298   EXPECT_FALSE(delegate->scroll_end());
3299   EXPECT_TRUE(delegate->fling());
3300
3301   EXPECT_FALSE(delegate->pinch_begin());
3302   EXPECT_FALSE(delegate->pinch_update());
3303   EXPECT_FALSE(delegate->pinch_end());
3304 }
3305
3306 // Like as GestureEventTouchMoveConsumed but tests the different behavior
3307 // depending on whether the events were consumed before or after the scroll
3308 // started.
3309 TEST_P(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) {
3310   scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3311       new ConsumesTouchMovesDelegate());
3312   const int kWindowWidth = 123;
3313   const int kWindowHeight = 45;
3314   const int kTouchId = 5;
3315   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3316   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3317       delegate.get(), -1234, bounds, root_window()));
3318   TimedEvents tes;
3319
3320   delegate->Reset();
3321   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3322                        kTouchId, tes.Now());
3323   DispatchEventUsingWindowDispatcher(&press);
3324   EXPECT_FALSE(delegate->tap());
3325   EXPECT_TRUE(delegate->tap_down());
3326   EXPECT_FALSE(delegate->tap_cancel());
3327   EXPECT_TRUE(delegate->begin());
3328   EXPECT_FALSE(delegate->scroll_begin());
3329   EXPECT_FALSE(delegate->scroll_update());
3330   EXPECT_FALSE(delegate->scroll_end());
3331
3332   // Move the touch-point enough so that it would normally be considered a
3333   // scroll. But since the touch-moves will be consumed, the scroll should not
3334   // start.
3335   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3336   EXPECT_FALSE(delegate->tap());
3337   EXPECT_FALSE(delegate->tap_down());
3338   EXPECT_TRUE(delegate->tap_cancel());
3339   EXPECT_FALSE(delegate->begin());
3340   EXPECT_FALSE(delegate->scroll_update());
3341   EXPECT_FALSE(delegate->scroll_end());
3342
3343   // With the unified gesture detector, consuming the first touch move event
3344   // won't prevent all future scrolling.
3345   if (UsingUnifiedGR())
3346     EXPECT_TRUE(delegate->scroll_begin());
3347   else
3348     EXPECT_FALSE(delegate->scroll_begin());
3349
3350   // Now, stop consuming touch-move events, and move the touch-point again.
3351   delegate->set_consume_touch_move(false);
3352   tes.SendScrollEvent(event_processor(), 159, 259, kTouchId, delegate.get());
3353   EXPECT_FALSE(delegate->tap());
3354   EXPECT_FALSE(delegate->tap_down());
3355   EXPECT_FALSE(delegate->tap_cancel());
3356   EXPECT_FALSE(delegate->begin());
3357   EXPECT_FALSE(delegate->scroll_begin());
3358   EXPECT_FALSE(delegate->scroll_end());
3359
3360   if (UsingUnifiedGR()) {
3361     // Scroll not prevented by consumed first touch move.
3362     EXPECT_TRUE(delegate->scroll_update());
3363     EXPECT_EQ(29, delegate->scroll_x());
3364     EXPECT_EQ(29, delegate->scroll_y());
3365     EXPECT_EQ(gfx::Point(0, 0).ToString(),
3366               delegate->scroll_begin_position().ToString());
3367   } else {
3368     EXPECT_FALSE(delegate->scroll_update());
3369     // No scroll has occurred, because an early touch move was consumed.
3370     EXPECT_EQ(0, delegate->scroll_x());
3371     EXPECT_EQ(0, delegate->scroll_y());
3372     EXPECT_EQ(gfx::Point(0, 0).ToString(),
3373               delegate->scroll_begin_position().ToString());
3374   }
3375
3376   // Start consuming touch-move events again.
3377   delegate->set_consume_touch_move(true);
3378
3379   // Move some more to generate a few more scroll updates.
3380   tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
3381   EXPECT_FALSE(delegate->tap());
3382   EXPECT_FALSE(delegate->tap_down());
3383   EXPECT_FALSE(delegate->tap_cancel());
3384   EXPECT_FALSE(delegate->begin());
3385   EXPECT_FALSE(delegate->scroll_begin());
3386   EXPECT_FALSE(delegate->scroll_update());
3387   EXPECT_FALSE(delegate->scroll_end());
3388   EXPECT_EQ(0, delegate->scroll_x());
3389   EXPECT_EQ(0, delegate->scroll_y());
3390
3391   tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
3392   EXPECT_FALSE(delegate->tap());
3393   EXPECT_FALSE(delegate->tap_down());
3394   EXPECT_FALSE(delegate->tap_cancel());
3395   EXPECT_FALSE(delegate->begin());
3396   EXPECT_FALSE(delegate->scroll_begin());
3397   EXPECT_FALSE(delegate->scroll_update());
3398   EXPECT_FALSE(delegate->scroll_end());
3399   EXPECT_EQ(0, delegate->scroll_x());
3400   EXPECT_EQ(0, delegate->scroll_y());
3401
3402   // Release the touch.
3403   delegate->Reset();
3404   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3405                          kTouchId, tes.LeapForward(50));
3406   DispatchEventUsingWindowDispatcher(&release);
3407   EXPECT_FALSE(delegate->tap());
3408   EXPECT_FALSE(delegate->tap_down());
3409   EXPECT_FALSE(delegate->tap_cancel());
3410   EXPECT_FALSE(delegate->begin());
3411   EXPECT_TRUE(delegate->end());
3412   EXPECT_FALSE(delegate->scroll_begin());
3413   EXPECT_FALSE(delegate->scroll_update());
3414   EXPECT_FALSE(delegate->fling());
3415
3416   if (UsingUnifiedGR())
3417     EXPECT_TRUE(delegate->scroll_end());
3418   else
3419     EXPECT_FALSE(delegate->scroll_end());
3420 }
3421
3422 // Check that appropriate touch events generate double tap gesture events.
3423 TEST_P(GestureRecognizerTest, GestureEventDoubleTap) {
3424   scoped_ptr<GestureEventConsumeDelegate> delegate(
3425       new GestureEventConsumeDelegate());
3426   const int kWindowWidth = 123;
3427   const int kWindowHeight = 45;
3428   const int kTouchId = 2;
3429   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3430   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3431       delegate.get(), -1234, bounds, root_window()));
3432   TimedEvents tes;
3433
3434   // First tap (tested in GestureEventTap)
3435   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3436                         kTouchId, tes.Now());
3437   DispatchEventUsingWindowDispatcher(&press1);
3438   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3439                           kTouchId, tes.LeapForward(50));
3440   DispatchEventUsingWindowDispatcher(&release1);
3441   delegate->Reset();
3442
3443   // Second tap
3444   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3445                         kTouchId, tes.LeapForward(200));
3446   DispatchEventUsingWindowDispatcher(&press2);
3447   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3448                           kTouchId, tes.LeapForward(50));
3449   DispatchEventUsingWindowDispatcher(&release2);
3450
3451   EXPECT_TRUE(delegate->tap());
3452   EXPECT_TRUE(delegate->tap_down());
3453   EXPECT_FALSE(delegate->tap_cancel());
3454   EXPECT_TRUE(delegate->begin());
3455   EXPECT_TRUE(delegate->end());
3456   EXPECT_FALSE(delegate->scroll_begin());
3457   EXPECT_FALSE(delegate->scroll_update());
3458   EXPECT_FALSE(delegate->scroll_end());
3459
3460   EXPECT_EQ(2, delegate->tap_count());
3461 }
3462
3463 // Check that appropriate touch events generate triple tap gesture events.
3464 TEST_P(GestureRecognizerTest, GestureEventTripleTap) {
3465   scoped_ptr<GestureEventConsumeDelegate> delegate(
3466       new GestureEventConsumeDelegate());
3467   const int kWindowWidth = 123;
3468   const int kWindowHeight = 45;
3469   const int kTouchId = 2;
3470   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3471   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3472       delegate.get(), -1234, bounds, root_window()));
3473   TimedEvents tes;
3474
3475   // First tap (tested in GestureEventTap)
3476   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3477                         kTouchId, tes.Now());
3478   DispatchEventUsingWindowDispatcher(&press1);
3479   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3480                           kTouchId, tes.LeapForward(50));
3481   DispatchEventUsingWindowDispatcher(&release1);
3482
3483   EXPECT_EQ(1, delegate->tap_count());
3484   delegate->Reset();
3485
3486   // Second tap (tested in GestureEventDoubleTap)
3487   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3488                         kTouchId, tes.LeapForward(200));
3489   DispatchEventUsingWindowDispatcher(&press2);
3490   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3491                           kTouchId, tes.LeapForward(50));
3492   DispatchEventUsingWindowDispatcher(&release2);
3493
3494   EXPECT_EQ(2, delegate->tap_count());
3495   delegate->Reset();
3496
3497   // Third tap
3498   ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(102, 206),
3499                         kTouchId, tes.LeapForward(200));
3500   DispatchEventUsingWindowDispatcher(&press3);
3501   ui::TouchEvent release3(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3502                           kTouchId, tes.LeapForward(50));
3503   DispatchEventUsingWindowDispatcher(&release3);
3504
3505   // Third, Fourth and Fifth Taps. Taps after the third should have their
3506   // |tap_count| wrap around back to 1.
3507   for (int i = 3; i < 5; ++i) {
3508     ui::TouchEvent press3(ui::ET_TOUCH_PRESSED,
3509                           gfx::Point(102, 206),
3510                           kTouchId,
3511                           tes.LeapForward(200));
3512     DispatchEventUsingWindowDispatcher(&press3);
3513     ui::TouchEvent release3(ui::ET_TOUCH_RELEASED,
3514                             gfx::Point(102, 206),
3515                             kTouchId,
3516                             tes.LeapForward(50));
3517     DispatchEventUsingWindowDispatcher(&release3);
3518
3519     EXPECT_TRUE(delegate->tap());
3520     EXPECT_TRUE(delegate->tap_down());
3521     EXPECT_FALSE(delegate->tap_cancel());
3522     EXPECT_TRUE(delegate->begin());
3523     EXPECT_TRUE(delegate->end());
3524     EXPECT_FALSE(delegate->scroll_begin());
3525     EXPECT_FALSE(delegate->scroll_update());
3526     EXPECT_FALSE(delegate->scroll_end());
3527
3528     // The behavior for the Aura GR is incorrect.
3529     if (UsingUnifiedGR())
3530       EXPECT_EQ(1 + (i % 3), delegate->tap_count());
3531     else
3532       EXPECT_EQ(3, delegate->tap_count());
3533   }
3534 }
3535
3536 // Check that we don't get a double tap when the two taps are far apart.
3537 TEST_P(GestureRecognizerTest, TwoTapsFarApart) {
3538   scoped_ptr<GestureEventConsumeDelegate> delegate(
3539       new GestureEventConsumeDelegate());
3540   const int kWindowWidth = 123;
3541   const int kWindowHeight = 45;
3542   const int kTouchId = 2;
3543   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3544   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3545       delegate.get(), -1234, bounds, root_window()));
3546   TimedEvents tes;
3547
3548   // First tap (tested in GestureEventTap)
3549   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3550                         kTouchId, tes.Now());
3551   DispatchEventUsingWindowDispatcher(&press1);
3552   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3553                           kTouchId, tes.LeapForward(50));
3554   DispatchEventUsingWindowDispatcher(&release1);
3555   delegate->Reset();
3556
3557   // Second tap, close in time but far in distance
3558   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201),
3559                         kTouchId, tes.LeapForward(200));
3560   DispatchEventUsingWindowDispatcher(&press2);
3561   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201),
3562                           kTouchId, tes.LeapForward(50));
3563   DispatchEventUsingWindowDispatcher(&release2);
3564
3565   EXPECT_TRUE(delegate->tap());
3566   EXPECT_TRUE(delegate->tap_down());
3567   EXPECT_FALSE(delegate->tap_cancel());
3568   EXPECT_TRUE(delegate->begin());
3569   EXPECT_TRUE(delegate->end());
3570   EXPECT_FALSE(delegate->scroll_begin());
3571   EXPECT_FALSE(delegate->scroll_update());
3572   EXPECT_FALSE(delegate->scroll_end());
3573
3574   EXPECT_EQ(1, delegate->tap_count());
3575 }
3576
3577 // Check that we don't get a double tap when the two taps have a long enough
3578 // delay in between.
3579 TEST_P(GestureRecognizerTest, TwoTapsWithDelayBetween) {
3580   scoped_ptr<GestureEventConsumeDelegate> delegate(
3581       new GestureEventConsumeDelegate());
3582   const int kWindowWidth = 123;
3583   const int kWindowHeight = 45;
3584   const int kTouchId = 2;
3585   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3586   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3587       delegate.get(), -1234, bounds, root_window()));
3588   TimedEvents tes;
3589
3590   // First tap (tested in GestureEventTap)
3591   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3592                         kTouchId, tes.Now());
3593   DispatchEventUsingWindowDispatcher(&press1);
3594   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3595                           kTouchId, tes.LeapForward(50));
3596   DispatchEventUsingWindowDispatcher(&release1);
3597   delegate->Reset();
3598
3599   // Second tap, close in distance but after some delay
3600   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3601                         kTouchId, tes.LeapForward(2000));
3602   DispatchEventUsingWindowDispatcher(&press2);
3603   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3604                           kTouchId, tes.LeapForward(50));
3605   DispatchEventUsingWindowDispatcher(&release2);
3606
3607   EXPECT_TRUE(delegate->tap());
3608   EXPECT_TRUE(delegate->tap_down());
3609   EXPECT_FALSE(delegate->tap_cancel());
3610   EXPECT_TRUE(delegate->begin());
3611   EXPECT_TRUE(delegate->end());
3612   EXPECT_FALSE(delegate->scroll_begin());
3613   EXPECT_FALSE(delegate->scroll_update());
3614   EXPECT_FALSE(delegate->scroll_end());
3615
3616   EXPECT_EQ(1, delegate->tap_count());
3617 }
3618
3619 // Checks that if the bounding-box of a gesture changes because of change in
3620 // radius of a touch-point, and not because of change in position, then there
3621 // are not gesture events from that.
3622 TEST_P(GestureRecognizerTest, BoundingBoxRadiusChange) {
3623   // TODO(tdresser): enable this test with unified GR when (if?) bounding box
3624   // behavior is unified.
3625   if (UsingUnifiedGR())
3626     return;
3627
3628   scoped_ptr<GestureEventConsumeDelegate> delegate(
3629       new GestureEventConsumeDelegate());
3630   const int kWindowWidth = 234;
3631   const int kWindowHeight = 345;
3632   const int kTouchId = 5, kTouchId2 = 7;
3633   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3634   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3635       delegate.get(), -1234, bounds, root_window()));
3636   TimedEvents tes;
3637
3638   ui::TouchEvent press1(
3639       ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, tes.Now());
3640   DispatchEventUsingWindowDispatcher(&press1);
3641   EXPECT_TRUE(delegate->bounding_box().IsEmpty());
3642
3643   delegate->Reset();
3644
3645   ui::TouchEvent press2(
3646       ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2,
3647       tes.LeapForward(400));
3648   press2.set_radius_x(5);
3649   DispatchEventUsingWindowDispatcher(&press2);
3650   EXPECT_FALSE(delegate->pinch_begin());
3651   EXPECT_EQ(gfx::Rect(101, 201, 100, 0).ToString(),
3652             delegate->bounding_box().ToString());
3653
3654   delegate->Reset();
3655
3656   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(141, 201), kTouchId,
3657                        tes.LeapForward(40));
3658   DispatchEventUsingWindowDispatcher(&move1);
3659   EXPECT_TRUE(delegate->pinch_begin());
3660   EXPECT_EQ(gfx::Rect(141, 201, 60, 0).ToString(),
3661             delegate->bounding_box().ToString());
3662
3663   delegate->Reset();
3664
3665   // The position doesn't move, but the radius changes.
3666   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 201), kTouchId,
3667                        tes.LeapForward(40));
3668   move2.set_radius_x(50);
3669   move2.set_radius_y(60);
3670   DispatchEventUsingWindowDispatcher(&move2);
3671   EXPECT_FALSE(delegate->tap());
3672   EXPECT_FALSE(delegate->tap_cancel());
3673   EXPECT_FALSE(delegate->scroll_update());
3674   EXPECT_FALSE(delegate->pinch_update());
3675
3676   delegate->Reset();
3677 }
3678
3679 // Checks that slow scrolls deliver the correct deltas.
3680 // In particular, fix for http;//crbug.com/150573.
3681 TEST_P(GestureRecognizerTest, NoDriftInScroll) {
3682   ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
3683   ui::GestureConfiguration::set_min_scroll_delta_squared(9);
3684   scoped_ptr<GestureEventConsumeDelegate> delegate(
3685       new GestureEventConsumeDelegate());
3686   const int kWindowWidth = 234;
3687   const int kWindowHeight = 345;
3688   const int kTouchId = 5;
3689   TimedEvents tes;
3690   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3691   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3692       delegate.get(), -1234, bounds, root_window()));
3693
3694   ui::TouchEvent press1(
3695       ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, tes.Now());
3696   DispatchEventUsingWindowDispatcher(&press1);
3697   EXPECT_TRUE(delegate->begin());
3698
3699   delegate->Reset();
3700
3701   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId,
3702                        tes.LeapForward(40));
3703   DispatchEventUsingWindowDispatcher(&move1);
3704   EXPECT_FALSE(delegate->scroll_begin());
3705
3706   delegate->Reset();
3707
3708   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3709                        tes.LeapForward(40));
3710   DispatchEventUsingWindowDispatcher(&move2);
3711   EXPECT_TRUE(delegate->tap_cancel());
3712   EXPECT_TRUE(delegate->scroll_begin());
3713   EXPECT_TRUE(delegate->scroll_update());
3714   // 3 px consumed by touch slop region.
3715   EXPECT_EQ(-1, delegate->scroll_y());
3716   EXPECT_EQ(-4, delegate->scroll_y_hint());
3717
3718   delegate->Reset();
3719
3720   ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3721                        tes.LeapForward(40));
3722   DispatchEventUsingWindowDispatcher(&move3);
3723   EXPECT_FALSE(delegate->scroll_update());
3724
3725   delegate->Reset();
3726
3727   ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId,
3728                        tes.LeapForward(40));
3729   DispatchEventUsingWindowDispatcher(&move4);
3730   EXPECT_TRUE(delegate->scroll_update());
3731   EXPECT_EQ(-1, delegate->scroll_y());
3732
3733   delegate->Reset();
3734 }
3735
3736 // Ensure that move events which are preventDefaulted will cause a tap
3737 // cancel gesture event to be fired if the move would normally cause a
3738 // scroll. See bug http://crbug.com/146397.
3739 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveCanFireTapCancel) {
3740   scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3741       new ConsumesTouchMovesDelegate());
3742   const int kTouchId = 5;
3743   gfx::Rect bounds(100, 200, 123, 45);
3744   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3745       delegate.get(), -1234, bounds, root_window()));
3746   TimedEvents tes;
3747
3748   delegate->Reset();
3749   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3750                        kTouchId, tes.Now());
3751
3752   delegate->set_consume_touch_move(false);
3753   DispatchEventUsingWindowDispatcher(&press);
3754   delegate->set_consume_touch_move(true);
3755   delegate->Reset();
3756   // Move the touch-point enough so that it would normally be considered a
3757   // scroll. But since the touch-moves will be consumed, no scrolling should
3758   // occur.
3759   // With the unified gesture detector, we will receive a scroll begin gesture,
3760   // whereas with the aura gesture recognizer we won't.
3761   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3762   EXPECT_FALSE(delegate->tap());
3763   EXPECT_FALSE(delegate->tap_down());
3764   EXPECT_TRUE(delegate->tap_cancel());
3765   EXPECT_FALSE(delegate->begin());
3766   EXPECT_FALSE(delegate->scroll_update());
3767   EXPECT_FALSE(delegate->scroll_end());
3768 }
3769
3770 TEST_P(GestureRecognizerTest,
3771        TransferEventDispatchesTouchCancel) {
3772   scoped_ptr<GestureEventConsumeDelegate> delegate(
3773       new GestureEventConsumeDelegate());
3774   TimedEvents tes;
3775   const int kWindowWidth = 800;
3776   const int kWindowHeight = 600;
3777   const int kTouchId = 2;
3778   gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3779   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3780       delegate.get(), -1234, bounds, root_window()));
3781   scoped_ptr<RemoveOnTouchCancelHandler>
3782       handler(new RemoveOnTouchCancelHandler());
3783   window->AddPreTargetHandler(handler.get());
3784
3785   // Start a gesture sequence on |window|. Then transfer the events to NULL.
3786   // Make sure |window| receives a touch-cancel event.
3787   delegate->Reset();
3788   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3789                        kTouchId, tes.Now());
3790   ui::TouchEvent p2(ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), 1, tes.Now());
3791   DispatchEventUsingWindowDispatcher(&press);
3792   DispatchEventUsingWindowDispatcher(&p2);
3793   EXPECT_FALSE(delegate->tap());
3794   EXPECT_TRUE(delegate->tap_down());
3795   EXPECT_TRUE(delegate->tap_cancel());
3796   EXPECT_TRUE(delegate->begin());
3797   EXPECT_EQ(2, handler->touch_pressed_count());
3798   delegate->Reset();
3799   handler->Reset();
3800
3801   ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get();
3802   EXPECT_EQ(window.get(),
3803             gesture_recognizer->GetTouchLockedTarget(press));
3804   gesture_recognizer->TransferEventsTo(window.get(), NULL);
3805   EXPECT_EQ(NULL,
3806             gesture_recognizer->GetTouchLockedTarget(press));
3807   // The event-handler removes |window| from its parent on the first
3808   // touch-cancel event, so it won't receive the second touch-cancel event.
3809   EXPECT_EQ(1, handler->touch_cancelled_count());
3810 }
3811
3812 // Check that appropriate touch events generate show press events
3813 TEST_P(GestureRecognizerTest, GestureEventShowPress) {
3814   scoped_ptr<GestureEventConsumeDelegate> delegate(
3815       new GestureEventConsumeDelegate());
3816   TimedEvents tes;
3817   const int kWindowWidth = 123;
3818   const int kWindowHeight = 45;
3819   const int kTouchId = 2;
3820   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3821   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3822       delegate.get(), -1234, bounds, root_window()));
3823
3824   delegate->Reset();
3825
3826   TimerTestGestureRecognizer* gesture_recognizer =
3827       new TimerTestGestureRecognizer();
3828
3829   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
3830
3831   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3832                         kTouchId, tes.Now());
3833   DispatchEventUsingWindowDispatcher(&press1);
3834   EXPECT_TRUE(delegate->tap_down());
3835   EXPECT_TRUE(delegate->begin());
3836   EXPECT_FALSE(delegate->tap_cancel());
3837
3838   // We haven't pressed long enough for a show press to occur
3839   EXPECT_FALSE(delegate->show_press());
3840
3841   // Wait until the timer runs out
3842   delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
3843   EXPECT_TRUE(delegate->show_press());
3844   EXPECT_FALSE(delegate->tap_cancel());
3845
3846   delegate->Reset();
3847   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3848                           kTouchId, tes.Now());
3849   DispatchEventUsingWindowDispatcher(&release1);
3850   EXPECT_FALSE(delegate->long_press());
3851
3852   // Note the tap isn't dispatched until the release
3853   EXPECT_FALSE(delegate->tap_cancel());
3854   EXPECT_TRUE(delegate->tap());
3855 }
3856
3857 // Check that scrolling cancels a show press
3858 TEST_P(GestureRecognizerTest, GestureEventShowPressCancelledByScroll) {
3859   scoped_ptr<GestureEventConsumeDelegate> delegate(
3860       new GestureEventConsumeDelegate());
3861   TimedEvents tes;
3862   const int kWindowWidth = 123;
3863   const int kWindowHeight = 45;
3864   const int kTouchId = 6;
3865   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3866   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3867       delegate.get(), -1234, bounds, root_window()));
3868
3869   delegate->Reset();
3870
3871   TimerTestGestureRecognizer* gesture_recognizer =
3872       new TimerTestGestureRecognizer();
3873
3874   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
3875
3876   TimerTestGestureSequence* gesture_sequence =
3877       static_cast<TimerTestGestureSequence*>(
3878           gesture_recognizer->GetGestureSequenceForTesting(window.get()));
3879
3880   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3881                         kTouchId, tes.Now());
3882   DispatchEventUsingWindowDispatcher(&press1);
3883   EXPECT_TRUE(delegate->tap_down());
3884
3885   // We haven't pressed long enough for a show press to occur
3886   EXPECT_FALSE(delegate->show_press());
3887   EXPECT_FALSE(delegate->tap_cancel());
3888
3889   // Scroll around, to cancel the show press
3890   tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3891   // Wait until the timer runs out
3892   gesture_sequence->ForceTimeout();
3893   EXPECT_FALSE(delegate->show_press());
3894   EXPECT_TRUE(delegate->tap_cancel());
3895
3896   delegate->Reset();
3897   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3898                           kTouchId, tes.LeapForward(10));
3899   DispatchEventUsingWindowDispatcher(&release1);
3900   EXPECT_FALSE(delegate->show_press());
3901   EXPECT_FALSE(delegate->tap_cancel());
3902 }
3903
3904 // Test that show press events are sent immediately on tap
3905 TEST_P(GestureRecognizerTest, GestureEventShowPressSentOnTap) {
3906   scoped_ptr<GestureEventConsumeDelegate> delegate(
3907       new GestureEventConsumeDelegate());
3908   TimedEvents tes;
3909   const int kWindowWidth = 123;
3910   const int kWindowHeight = 45;
3911   const int kTouchId = 6;
3912   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3913   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3914       delegate.get(), -1234, bounds, root_window()));
3915
3916   delegate->Reset();
3917
3918   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3919                         kTouchId, tes.Now());
3920   DispatchEventUsingWindowDispatcher(&press1);
3921   EXPECT_TRUE(delegate->tap_down());
3922
3923   // We haven't pressed long enough for a show press to occur
3924   EXPECT_FALSE(delegate->show_press());
3925   EXPECT_FALSE(delegate->tap_cancel());
3926
3927   delegate->Reset();
3928   ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3929                           kTouchId, tes.LeapForward(50));
3930   DispatchEventUsingWindowDispatcher(&release1);
3931   EXPECT_TRUE(delegate->show_press());
3932   EXPECT_FALSE(delegate->tap_cancel());
3933   EXPECT_TRUE(delegate->tap());
3934 }
3935
3936 // Test that consuming the first move touch event prevents a scroll.
3937 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveScrollTest) {
3938   scoped_ptr<QueueTouchEventDelegate> delegate(
3939       new QueueTouchEventDelegate(host()->dispatcher()));
3940   TimedEvents tes;
3941   const int kTouchId = 7;
3942   gfx::Rect bounds(0, 0, 1000, 1000);
3943   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3944       delegate.get(), -1234, bounds, root_window()));
3945   delegate->set_window(window.get());
3946
3947   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3948                        kTouchId, tes.Now());
3949   DispatchEventUsingWindowDispatcher(&press);
3950   delegate->ReceivedAck();
3951
3952   // A touch move within the slop region is never consumed in web contents. The
3953   // unified GR won't prevent scroll if a touch move within the slop region is
3954   // consumed, so make sure this touch move exceeds the slop region.
3955   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(10, 10),
3956                        kTouchId, tes.Now());
3957   DispatchEventUsingWindowDispatcher(&move1);
3958   delegate->ReceivedAckPreventDefaulted();
3959
3960   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3961                        kTouchId, tes.Now());
3962   DispatchEventUsingWindowDispatcher(&move2);
3963   delegate->ReceivedAck();
3964
3965   if (UsingUnifiedGR()) {
3966     // With the unified gesture detector, consuming the first touch move event
3967     // won't prevent all future scrolling.
3968     EXPECT_TRUE(delegate->scroll_begin());
3969     EXPECT_TRUE(delegate->scroll_update());
3970   } else {
3971     EXPECT_FALSE(delegate->scroll_begin());
3972     EXPECT_FALSE(delegate->scroll_update());
3973   }
3974 }
3975
3976 // Test that consuming the first touch move event of a touch point doesn't
3977 // prevent pinching once an additional touch has been pressed.
3978 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMovePinchTest) {
3979   // Consuming moves within the touch slop and the the disposition handling of
3980   // pinch events behave differently between the Unified GR and the Aura GR.
3981   if (UsingUnifiedGR())
3982     return;
3983
3984   scoped_ptr<QueueTouchEventDelegate> delegate(
3985       new QueueTouchEventDelegate(host()->dispatcher()));
3986   TimedEvents tes;
3987   const int kTouchId1 = 7;
3988   const int kTouchId2 = 4;
3989   gfx::Rect bounds(0, 0, 1000, 1000);
3990   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3991       delegate.get(), -1234, bounds, root_window()));
3992
3993   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3994                        kTouchId1, tes.Now());
3995   DispatchEventUsingWindowDispatcher(&press1);
3996   delegate->ReceivedAck();
3997
3998   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
3999                        kTouchId1, tes.Now());
4000   DispatchEventUsingWindowDispatcher(&move1);
4001   delegate->ReceivedAckPreventDefaulted();
4002
4003   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
4004                        kTouchId1, tes.Now());
4005   DispatchEventUsingWindowDispatcher(&move2);
4006   delegate->ReceivedAck();
4007
4008   // We can't scroll, because a move has been consumed.
4009   EXPECT_FALSE(delegate->scroll_begin());
4010   EXPECT_FALSE(delegate->scroll_update());
4011   EXPECT_FALSE(delegate->pinch_begin());
4012
4013   // An additional press will allow us to pinch.
4014   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
4015                        kTouchId2, tes.Now());
4016   DispatchEventUsingWindowDispatcher(&press2);
4017   delegate->ReceivedAck();
4018
4019   ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
4020                        kTouchId2, tes.Now());
4021   DispatchEventUsingWindowDispatcher(&move3);
4022   delegate->ReceivedAck();
4023
4024   EXPECT_TRUE(delegate->pinch_begin());
4025   EXPECT_FALSE(delegate->pinch_update());
4026
4027   delegate->Reset();
4028
4029   ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(40, 40),
4030                        kTouchId2, tes.Now());
4031   DispatchEventUsingWindowDispatcher(&move4);
4032   delegate->ReceivedAck();
4033
4034   EXPECT_TRUE(delegate->pinch_update());
4035   EXPECT_EQ(10, delegate->scroll_x());
4036   EXPECT_EQ(10, delegate->scroll_y());
4037 }
4038
4039 // Test that consuming the first move touch doesn't prevent a tap.
4040 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveTapTest) {
4041   scoped_ptr<QueueTouchEventDelegate> delegate(
4042       new QueueTouchEventDelegate(host()->dispatcher()));
4043   TimedEvents tes;
4044   const int kTouchId = 7;
4045   gfx::Rect bounds(0, 0, 1000, 1000);
4046   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4047       delegate.get(), -1234, bounds, root_window()));
4048   delegate->set_window(window.get());
4049
4050   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
4051                        kTouchId, tes.Now());
4052   DispatchEventUsingWindowDispatcher(&press);
4053   delegate->ReceivedAck();
4054
4055   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
4056                        kTouchId, tes.Now());
4057   DispatchEventUsingWindowDispatcher(&move);
4058   delegate->ReceivedAckPreventDefaulted();
4059
4060   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(2, 2),
4061                          kTouchId, tes.LeapForward(50));
4062   DispatchEventUsingWindowDispatcher(&release);
4063   delegate->ReceivedAck();
4064
4065   EXPECT_TRUE(delegate->tap());
4066 }
4067
4068 // Test that consuming the first move touch doesn't prevent a long press.
4069 TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveLongPressTest) {
4070   scoped_ptr<QueueTouchEventDelegate> delegate(
4071       new QueueTouchEventDelegate(host()->dispatcher()));
4072   TimedEvents tes;
4073   const int kWindowWidth = 123;
4074   const int kWindowHeight = 45;
4075   const int kTouchId = 2;
4076   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4077   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4078       delegate.get(), -1234, bounds, root_window()));
4079   delegate->set_window(window.get());
4080
4081   delegate->Reset();
4082
4083   TimerTestGestureRecognizer* gesture_recognizer =
4084       new TimerTestGestureRecognizer();
4085
4086   ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
4087
4088   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4089                         kTouchId, tes.Now());
4090   DispatchEventUsingWindowDispatcher(&press1);
4091   delegate->ReceivedAck();
4092
4093   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103, 203),
4094                        kTouchId, tes.Now());
4095   DispatchEventUsingWindowDispatcher(&move);
4096   delegate->ReceivedAckPreventDefaulted();
4097
4098   // Wait until the timer runs out
4099   delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4100   EXPECT_TRUE(delegate->long_press());
4101 }
4102
4103 // Tests that the deltas are correct when leaving the slop region very slowly.
4104 TEST_P(GestureRecognizerTest, TestExceedingSlopSlowly) {
4105   // Disabled for unified GR due to subtle differences in touch slop handling.
4106   if (UsingUnifiedGR())
4107     return;
4108
4109   ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
4110   scoped_ptr<GestureEventConsumeDelegate> delegate(
4111       new GestureEventConsumeDelegate());
4112   const int kWindowWidth = 234;
4113   const int kWindowHeight = 345;
4114   const int kTouchId = 5;
4115   TimedEvents tes;
4116   gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4117   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4118       delegate.get(), -1234, bounds, root_window()));
4119
4120   ui::TouchEvent press(
4121       ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), kTouchId, tes.Now());
4122   DispatchEventUsingWindowDispatcher(&press);
4123   EXPECT_FALSE(delegate->scroll_begin());
4124   EXPECT_FALSE(delegate->scroll_update());
4125   delegate->Reset();
4126
4127   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(11, 10), kTouchId,
4128                        tes.LeapForward(40));
4129   DispatchEventUsingWindowDispatcher(&move1);
4130   EXPECT_FALSE(delegate->scroll_begin());
4131   EXPECT_FALSE(delegate->scroll_update());
4132   EXPECT_EQ(0, delegate->scroll_x());
4133   EXPECT_EQ(0, delegate->scroll_x_hint());
4134   delegate->Reset();
4135
4136   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(12, 10), kTouchId,
4137                        tes.LeapForward(40));
4138   DispatchEventUsingWindowDispatcher(&move2);
4139   EXPECT_FALSE(delegate->scroll_begin());
4140   EXPECT_FALSE(delegate->scroll_update());
4141   EXPECT_EQ(0, delegate->scroll_x());
4142   EXPECT_EQ(0, delegate->scroll_x_hint());
4143   delegate->Reset();
4144
4145
4146   ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(13, 10), kTouchId,
4147                        tes.LeapForward(40));
4148   DispatchEventUsingWindowDispatcher(&move3);
4149   EXPECT_TRUE(delegate->scroll_begin());
4150   EXPECT_FALSE(delegate->scroll_update());
4151   EXPECT_EQ(0, delegate->scroll_x());
4152   EXPECT_EQ(3, delegate->scroll_x_hint());
4153   delegate->Reset();
4154
4155
4156   ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(14, 10), kTouchId,
4157                        tes.LeapForward(40));
4158   DispatchEventUsingWindowDispatcher(&move4);
4159   EXPECT_FALSE(delegate->scroll_begin());
4160   EXPECT_TRUE(delegate->scroll_update());
4161   EXPECT_EQ(1, delegate->scroll_x());
4162   EXPECT_EQ(0, delegate->scroll_x_hint());
4163   delegate->Reset();
4164 }
4165
4166 TEST_P(GestureRecognizerTest, ScrollAlternatelyConsumedTest) {
4167   scoped_ptr<QueueTouchEventDelegate> delegate(
4168       new QueueTouchEventDelegate(host()->dispatcher()));
4169   TimedEvents tes;
4170   const int kWindowWidth = 3000;
4171   const int kWindowHeight = 3000;
4172   const int kTouchId = 2;
4173   gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4174   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4175       delegate.get(), -1234, bounds, root_window()));
4176   delegate->set_window(window.get());
4177
4178   delegate->Reset();
4179
4180   int x = 0;
4181   int y = 0;
4182
4183   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
4184                         kTouchId, tes.Now());
4185   DispatchEventUsingWindowDispatcher(&press1);
4186   delegate->ReceivedAck();
4187   EXPECT_FALSE(delegate->scroll_begin());
4188   EXPECT_FALSE(delegate->scroll_update());
4189   delegate->Reset();
4190
4191   x += 100;
4192   y += 100;
4193   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
4194                        kTouchId, tes.Now());
4195   DispatchEventUsingWindowDispatcher(&move1);
4196   delegate->ReceivedAck();
4197   EXPECT_TRUE(delegate->scroll_begin());
4198   EXPECT_TRUE(delegate->scroll_update());
4199   delegate->Reset();
4200
4201   for (int i = 0; i < 3; ++i) {
4202     x += 10;
4203     y += 10;
4204     ui::TouchEvent move2(
4205         ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
4206     DispatchEventUsingWindowDispatcher(&move2);
4207     delegate->ReceivedAck();
4208     EXPECT_FALSE(delegate->scroll_begin());
4209     EXPECT_TRUE(delegate->scroll_update());
4210     EXPECT_EQ(10, delegate->scroll_x());
4211     EXPECT_EQ(10, delegate->scroll_y());
4212     delegate->Reset();
4213
4214     x += 20;
4215     y += 20;
4216     ui::TouchEvent move3(
4217         ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
4218     DispatchEventUsingWindowDispatcher(&move3);
4219     delegate->ReceivedAckPreventDefaulted();
4220     EXPECT_FALSE(delegate->scroll_begin());
4221     EXPECT_FALSE(delegate->scroll_update());
4222     delegate->Reset();
4223   }
4224 }
4225
4226 TEST_P(GestureRecognizerTest, PinchAlternatelyConsumedTest) {
4227   // Disabled for unified GR due to differences in when scroll update is
4228   // sent. The Aura GR will never send a ScrollUpdate with a ScrollBegin, but
4229   // the unified GR will.
4230   if (UsingUnifiedGR())
4231     return;
4232
4233   scoped_ptr<QueueTouchEventDelegate> delegate(
4234       new QueueTouchEventDelegate(host()->dispatcher()));
4235   TimedEvents tes;
4236   const int kWindowWidth = 3000;
4237   const int kWindowHeight = 3000;
4238   const int kTouchId1 = 5;
4239   const int kTouchId2 = 7;
4240   gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4241   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4242       delegate.get(), -1234, bounds, root_window()));
4243
4244   delegate->Reset();
4245
4246   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
4247                         kTouchId1, tes.Now());
4248   DispatchEventUsingWindowDispatcher(&press1);
4249   delegate->ReceivedAck();
4250   EXPECT_FALSE(delegate->scroll_begin());
4251   EXPECT_FALSE(delegate->scroll_update());
4252   delegate->Reset();
4253
4254   int x = 0;
4255   int y = 0;
4256
4257   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
4258                         kTouchId2, tes.Now());
4259   DispatchEventUsingWindowDispatcher(&press2);
4260   delegate->ReceivedAck();
4261   EXPECT_FALSE(delegate->scroll_begin());
4262   EXPECT_FALSE(delegate->scroll_update());
4263   EXPECT_FALSE(delegate->pinch_begin());
4264   EXPECT_FALSE(delegate->pinch_update());
4265
4266   delegate->Reset();
4267
4268   x += 100;
4269   y += 100;
4270   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
4271                        kTouchId2, tes.Now());
4272   DispatchEventUsingWindowDispatcher(&move1);
4273   delegate->ReceivedAck();
4274   EXPECT_TRUE(delegate->scroll_begin());
4275   EXPECT_FALSE(delegate->scroll_update());
4276   EXPECT_TRUE(delegate->pinch_begin());
4277   EXPECT_FALSE(delegate->pinch_update());
4278   delegate->Reset();
4279
4280   const float expected_scales[] = {1.5f, 1.2f, 1.125f};
4281
4282   for (int i = 0; i < 3; ++i) {
4283     x += 50;
4284     y += 50;
4285     ui::TouchEvent move2(
4286         ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4287     DispatchEventUsingWindowDispatcher(&move2);
4288     delegate->ReceivedAck();
4289     EXPECT_FALSE(delegate->scroll_begin());
4290     EXPECT_TRUE(delegate->scroll_update());
4291     EXPECT_FALSE(delegate->scroll_end());
4292     EXPECT_FALSE(delegate->pinch_begin());
4293     EXPECT_TRUE(delegate->pinch_update());
4294     EXPECT_FALSE(delegate->pinch_end());
4295     EXPECT_EQ(25, delegate->scroll_x());
4296     EXPECT_EQ(25, delegate->scroll_y());
4297     EXPECT_FLOAT_EQ(expected_scales[i], delegate->scale());
4298     delegate->Reset();
4299
4300     x += 100;
4301     y += 100;
4302     ui::TouchEvent move3(
4303         ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4304     DispatchEventUsingWindowDispatcher(&move3);
4305     delegate->ReceivedAckPreventDefaulted();
4306     EXPECT_FALSE(delegate->scroll_begin());
4307     EXPECT_FALSE(delegate->scroll_update());
4308     EXPECT_FALSE(delegate->scroll_end());
4309     EXPECT_FALSE(delegate->pinch_begin());
4310     EXPECT_FALSE(delegate->pinch_update());
4311     EXPECT_FALSE(delegate->pinch_end());
4312     delegate->Reset();
4313   }
4314 }
4315
4316 // Test that touch event flags are passed through to the gesture event.
4317 TEST_P(GestureRecognizerTest, GestureEventFlagsPassedFromTouchEvent) {
4318   scoped_ptr<GestureEventConsumeDelegate> delegate(
4319       new GestureEventConsumeDelegate());
4320   TimedEvents tes;
4321   const int kWindowWidth = 123;
4322   const int kWindowHeight = 45;
4323   const int kTouchId = 6;
4324   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4325   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4326       delegate.get(), -1234, bounds, root_window()));
4327
4328   delegate->Reset();
4329
4330   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4331                         kTouchId, tes.Now());
4332   DispatchEventUsingWindowDispatcher(&press1);
4333   EXPECT_TRUE(delegate->tap_down());
4334
4335   int default_flags = delegate->flags();
4336
4337   ui::TouchEvent move1(
4338       ui::ET_TOUCH_MOVED, gfx::Point(397, 149), kTouchId, tes.LeapForward(50));
4339   move1.set_flags(992);
4340
4341   DispatchEventUsingWindowDispatcher(&move1);
4342   EXPECT_NE(default_flags, delegate->flags());
4343 }
4344
4345 // Test that latency info is passed through to the gesture event.
4346 TEST_P(GestureRecognizerTest, LatencyPassedFromTouchEvent) {
4347   scoped_ptr<GestureEventConsumeDelegate> delegate(
4348       new GestureEventConsumeDelegate());
4349   TimedEvents tes;
4350   const int kWindowWidth = 123;
4351   const int kWindowHeight = 45;
4352   const int kTouchId = 6;
4353
4354   const base::TimeTicks time_original = base::TimeTicks::FromInternalValue(100);
4355   const base::TimeTicks time_ui = base::TimeTicks::FromInternalValue(200);
4356   const base::TimeTicks time_acked = base::TimeTicks::FromInternalValue(300);
4357
4358   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4359   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4360       delegate.get(), -1234, bounds, root_window()));
4361
4362   delegate->Reset();
4363
4364   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4365                         kTouchId, tes.Now());
4366
4367   // Ensure the only components around are the ones we add.
4368   press1.latency()->Clear();
4369
4370   press1.latency()->AddLatencyNumberWithTimestamp(
4371       ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, 0, time_original, 1);
4372
4373   press1.latency()->AddLatencyNumberWithTimestamp(
4374       ui::INPUT_EVENT_LATENCY_UI_COMPONENT, 0, 0, time_ui, 1);
4375
4376   press1.latency()->AddLatencyNumberWithTimestamp(
4377       ui::INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT, 0, 0, time_acked, 1);
4378
4379   DispatchEventUsingWindowDispatcher(&press1);
4380   EXPECT_TRUE(delegate->tap_down());
4381
4382   ui::LatencyInfo::LatencyComponent component;
4383
4384   EXPECT_EQ(3U, delegate->latency_info().latency_components.size());
4385   ASSERT_TRUE(delegate->latency_info().FindLatency(
4386       ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, &component));
4387   EXPECT_EQ(time_original, component.event_time);
4388
4389   ASSERT_TRUE(delegate->latency_info().FindLatency(
4390       ui::INPUT_EVENT_LATENCY_UI_COMPONENT, 0, &component));
4391   EXPECT_EQ(time_ui, component.event_time);
4392
4393   ASSERT_TRUE(delegate->latency_info().FindLatency(
4394       ui::INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT, 0, &component));
4395   EXPECT_EQ(time_acked, component.event_time);
4396
4397   delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
4398   EXPECT_TRUE(delegate->show_press());
4399   EXPECT_EQ(0U, delegate->latency_info().latency_components.size());
4400 }
4401
4402 // A delegate that deletes a window on long press.
4403 class GestureEventDeleteWindowOnLongPress : public GestureEventConsumeDelegate {
4404  public:
4405   GestureEventDeleteWindowOnLongPress()
4406       : window_(NULL) {}
4407
4408   void set_window(aura::Window** window) { window_ = window; }
4409
4410   virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
4411     GestureEventConsumeDelegate::OnGestureEvent(gesture);
4412     if (gesture->type() != ui::ET_GESTURE_LONG_PRESS)
4413       return;
4414     ui::GestureRecognizer::Get()->CleanupStateForConsumer(*window_);
4415     delete *window_;
4416     *window_ = NULL;
4417   }
4418
4419  private:
4420   aura::Window** window_;
4421   DISALLOW_COPY_AND_ASSIGN(GestureEventDeleteWindowOnLongPress);
4422 };
4423
4424 // Check that deleting the window in response to a long press gesture doesn't
4425 // crash.
4426 TEST_P(GestureRecognizerTest, GestureEventLongPressDeletingWindow) {
4427   GestureEventDeleteWindowOnLongPress delegate;
4428   const int kWindowWidth = 123;
4429   const int kWindowHeight = 45;
4430   const int kTouchId = 2;
4431   gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4432   aura::Window* window(CreateTestWindowWithDelegate(
4433       &delegate, -1234, bounds, root_window()));
4434   delegate.set_window(&window);
4435
4436   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
4437                         gfx::Point(101, 201),
4438                         kTouchId,
4439                         ui::EventTimeForNow());
4440   DispatchEventUsingWindowDispatcher(&press1);
4441   EXPECT_TRUE(window != NULL);
4442
4443   // Wait until the timer runs out.
4444   delegate.WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4445   EXPECT_EQ(NULL, window);
4446 }
4447
4448 TEST_P(GestureRecognizerTest, GestureEventSmallPinchDisabled) {
4449   if (!UsingUnifiedGR())
4450     return;
4451   CommandLine::ForCurrentProcess()->AppendSwitch(
4452       switches::kCompensateForUnstablePinchZoom);
4453
4454   scoped_ptr<GestureEventConsumeDelegate> delegate(
4455       new GestureEventConsumeDelegate());
4456   TimedEvents tes;
4457   const int kWindowWidth = 300;
4458   const int kWindowHeight = 400;
4459   const int kTouchId1 = 3;
4460   const int kTouchId2 = 5;
4461   gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4462   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4463       delegate.get(), -1234, bounds, root_window()));
4464
4465   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4466                        kTouchId1, tes.Now());
4467   DispatchEventUsingWindowDispatcher(&press1);
4468   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4469                         kTouchId2, tes.Now());
4470   DispatchEventUsingWindowDispatcher(&press2);
4471
4472   // Move the first finger.
4473   delegate->Reset();
4474   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4475                        kTouchId1, tes.Now());
4476   DispatchEventUsingWindowDispatcher(&move1);
4477
4478   EXPECT_3_EVENTS(delegate->events(),
4479                   ui::ET_GESTURE_SCROLL_BEGIN,
4480                   ui::ET_GESTURE_SCROLL_UPDATE,
4481                   ui::ET_GESTURE_PINCH_BEGIN);
4482
4483   // No pinch update occurs, as kCompensateForUnstablePinchZoom is on, and this
4484   // is a very small pinch.
4485   delegate->Reset();
4486   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4487                        kTouchId1, tes.Now());
4488   DispatchEventUsingWindowDispatcher(&move2);
4489   EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4490 }
4491
4492 TEST_P(GestureRecognizerTest, GestureEventSmallPinchEnabled) {
4493   if (!UsingUnifiedGR())
4494     return;
4495
4496   scoped_ptr<GestureEventConsumeDelegate> delegate(
4497       new GestureEventConsumeDelegate());
4498   TimedEvents tes;
4499   const int kWindowWidth = 300;
4500   const int kWindowHeight = 400;
4501   const int kTouchId1 = 3;
4502   const int kTouchId2 = 5;
4503   gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4504   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4505       delegate.get(), -1234, bounds, root_window()));
4506
4507   ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
4508                        kTouchId1, tes.Now());
4509   DispatchEventUsingWindowDispatcher(&press1);
4510   ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
4511                         kTouchId2, tes.Now());
4512   DispatchEventUsingWindowDispatcher(&press2);
4513
4514   // Move the first finger.
4515   delegate->Reset();
4516   ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
4517                        kTouchId1, tes.Now());
4518   DispatchEventUsingWindowDispatcher(&move1);
4519
4520   EXPECT_3_EVENTS(delegate->events(),
4521                   ui::ET_GESTURE_SCROLL_BEGIN,
4522                   ui::ET_GESTURE_SCROLL_UPDATE,
4523                   ui::ET_GESTURE_PINCH_BEGIN);
4524
4525   delegate->Reset();
4526   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202),
4527                        kTouchId1, tes.Now());
4528   DispatchEventUsingWindowDispatcher(&move2);
4529   EXPECT_2_EVENTS(delegate->events(),
4530                   ui::ET_GESTURE_SCROLL_UPDATE,
4531                   ui::ET_GESTURE_PINCH_UPDATE);
4532 }
4533
4534 // Tests that delaying the ack of a touch release doesn't trigger a long press
4535 // gesture.
4536 TEST_P(GestureRecognizerTest, EagerGestureDetection) {
4537   if (!UsingUnifiedGR())
4538     return;
4539
4540   scoped_ptr<QueueTouchEventDelegate> delegate(
4541       new QueueTouchEventDelegate(host()->dispatcher()));
4542   TimedEvents tes;
4543   const int kTouchId = 2;
4544   gfx::Rect bounds(100, 200, 100, 100);
4545   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4546       delegate.get(), -1234, bounds, root_window()));
4547   delegate->set_window(window.get());
4548
4549   delegate->Reset();
4550   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4551                        kTouchId, tes.Now());
4552   DispatchEventUsingWindowDispatcher(&press);
4553   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
4554                          kTouchId, tes.LeapForward(50));
4555   DispatchEventUsingWindowDispatcher(&release);
4556
4557   delegate->Reset();
4558   // Ack the touch press.
4559   delegate->ReceivedAck();
4560   EXPECT_TRUE(delegate->tap_down());
4561
4562   delegate->Reset();
4563   // Wait until the long press event would fire (if we weren't eager).
4564   base::MessageLoop::current()->PostDelayedTask(
4565       FROM_HERE,
4566       base::MessageLoop::QuitClosure(),
4567       base::TimeDelta::FromSecondsD(
4568           ui::GestureConfiguration::long_press_time_in_seconds() * 1.1));
4569   base::MessageLoop::current()->Run();
4570
4571   // Ack the touch release.
4572   delegate->ReceivedAck();
4573   EXPECT_TRUE(delegate->tap());
4574   EXPECT_FALSE(delegate->long_press());
4575 }
4576
4577 // This tests crbug.com/405519, in which events which the gesture detector
4578 // ignores cause future events to also be thrown away.
4579 TEST_P(GestureRecognizerTest, IgnoredEventsDontPreventFutureEvents) {
4580   if (!UsingUnifiedGR())
4581     return;
4582
4583   scoped_ptr<QueueTouchEventDelegate> delegate(
4584       new QueueTouchEventDelegate(host()->dispatcher()));
4585   TimedEvents tes;
4586   const int kWindowWidth = 300;
4587   const int kWindowHeight = 400;
4588   const int kTouchId1 = 3;
4589   gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
4590   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4591       delegate.get(), -1234, bounds, root_window()));
4592   delegate->set_window(window.get());
4593
4594   ui::TouchEvent press1(
4595       ui::ET_TOUCH_PRESSED, gfx::Point(101, 301), kTouchId1, tes.Now());
4596   DispatchEventUsingWindowDispatcher(&press1);
4597   delegate->ReceivedAck();
4598
4599   EXPECT_2_EVENTS(
4600       delegate->events(), ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_TAP_DOWN);
4601
4602   // Move the first finger.
4603   delegate->Reset();
4604   ui::TouchEvent move1(
4605       ui::ET_TOUCH_MOVED, gfx::Point(65, 201), kTouchId1, tes.Now());
4606   DispatchEventUsingWindowDispatcher(&move1);
4607   delegate->ReceivedAck();
4608
4609   EXPECT_3_EVENTS(delegate->events(),
4610                   ui::ET_GESTURE_TAP_CANCEL,
4611                   ui::ET_GESTURE_SCROLL_BEGIN,
4612                   ui::ET_GESTURE_SCROLL_UPDATE);
4613
4614   delegate->Reset();
4615   ui::TouchEvent move2(
4616       ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4617   DispatchEventUsingWindowDispatcher(&move2);
4618
4619   // Send a touchmove event at the same location as the previous touchmove
4620   // event. This shouldn't do anything.
4621   ui::TouchEvent move3(
4622       ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now());
4623   DispatchEventUsingWindowDispatcher(&move3);
4624
4625   delegate->ReceivedAck();
4626   EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
4627 }
4628
4629 INSTANTIATE_TEST_CASE_P(GestureRecognizer,
4630                         GestureRecognizerTest,
4631                         ::testing::Bool());
4632
4633 }  // namespace test
4634 }  // namespace aura