gesture_recognizer_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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/event_generator.h"
14#include "ui/aura/test/test_window_delegate.h"
15#include "ui/aura/test/test_windows.h"
16#include "ui/aura/window.h"
17#include "ui/aura/window_event_dispatcher.h"
18#include "ui/base/hit_test.h"
19#include "ui/base/ui_base_switches.h"
20#include "ui/events/event.h"
21#include "ui/events/event_switches.h"
22#include "ui/events/event_utils.h"
23#include "ui/events/gestures/gesture_configuration.h"
24#include "ui/events/gestures/gesture_recognizer_impl.h"
25#include "ui/events/gestures/gesture_sequence.h"
26#include "ui/events/gestures/gesture_types.h"
27#include "ui/events/test/events_test_utils.h"
28#include "ui/gfx/point.h"
29#include "ui/gfx/rect.h"
30
31#include <queue>
32
33namespace aura {
34namespace test {
35
36namespace {
37
38std::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.
69class 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
336class 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.
386class 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
448class 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
464class 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
487class 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
500class 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
514base::TimeDelta GetTime() {
515  return ui::EventTimeForNow();
516}
517
518class 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
538class 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.
603class 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.
655class 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
678class 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.
705TEST_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.
756TEST_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.
1001TEST_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.
1072TEST_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.
1147TEST_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
1204TEST_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
1249TEST_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
1295TEST_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
1332TEST_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
1383TEST_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
1429TEST_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
1480TEST_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.
1536TEST_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.
1586TEST_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
1634TEST_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
1761TEST_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.
1954TEST_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
2044TEST_P(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) {
2045scoped_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
2101TEST_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
2180TEST_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
2194TEST_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.
2280TEST_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.
2312TEST_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
2345TEST_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
2372TEST_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
2471TEST_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  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
2489TEST_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  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.
2518TEST_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  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.
2572TEST_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
2601TEST_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
2694TEST_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
2735TEST_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
2807TEST_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
2849TEST_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  aura::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}
2895
2896TEST_P(GestureRecognizerTest, TwoFingerTapCancelled) {
2897  scoped_ptr<GestureEventConsumeDelegate> delegate(
2898      new GestureEventConsumeDelegate());
2899  const int kWindowWidth = 123;
2900  const int kWindowHeight = 45;
2901  const int kTouchId1 = 2;
2902  const int kTouchId2 = 3;
2903  TimedEvents tes;
2904
2905  // Test canceling first finger.
2906  {
2907    gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2908    scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2909        delegate.get(), -1234, bounds, root_window()));
2910
2911    delegate->Reset();
2912    ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2913                          kTouchId1, tes.Now());
2914    DispatchEventUsingWindowDispatcher(&press1);
2915
2916    delegate->Reset();
2917    ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2918                          kTouchId2, tes.Now());
2919    DispatchEventUsingWindowDispatcher(&press2);
2920
2921    delegate->Reset();
2922    ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2923                          kTouchId1, tes.Now());
2924    DispatchEventUsingWindowDispatcher(&cancel);
2925    EXPECT_FALSE(delegate->two_finger_tap());
2926
2927    // Make sure there is enough delay before the touch is released so that it
2928    // is recognized as a tap.
2929    delegate->Reset();
2930    ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2931                           kTouchId2, tes.LeapForward(50));
2932
2933    DispatchEventUsingWindowDispatcher(&release);
2934    EXPECT_FALSE(delegate->two_finger_tap());
2935  }
2936
2937  // Test canceling second finger
2938  {
2939    gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2940    scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2941        delegate.get(), -1234, bounds, root_window()));
2942
2943    delegate->Reset();
2944    ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2945                          kTouchId1, tes.Now());
2946    DispatchEventUsingWindowDispatcher(&press1);
2947
2948    delegate->Reset();
2949    ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2950                          kTouchId2, tes.Now());
2951    DispatchEventUsingWindowDispatcher(&press2);
2952
2953    delegate->Reset();
2954    ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2955                          kTouchId2, tes.Now());
2956    DispatchEventUsingWindowDispatcher(&cancel);
2957    EXPECT_FALSE(delegate->two_finger_tap());
2958
2959    // Make sure there is enough delay before the touch is released so that it
2960    // is recognized as a tap.
2961    delegate->Reset();
2962    ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2963                           kTouchId1, tes.LeapForward(50));
2964
2965    DispatchEventUsingWindowDispatcher(&release);
2966    EXPECT_FALSE(delegate->two_finger_tap());
2967  }
2968}
2969
2970TEST_P(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) {
2971  // Disabled for unified GR due to differences in when scroll update is
2972  // sent. The Aura GR will never send a ScrollUpdate with a ScrollBegin, but
2973  // the unified GR will.
2974  if (UsingUnifiedGR())
2975    return;
2976
2977  scoped_ptr<GestureEventConsumeDelegate> delegate(
2978      new GestureEventConsumeDelegate());
2979  const int kWindowWidth = 523;
2980  const int kWindowHeight = 45;
2981  const int kTouchId1 = 2;
2982  const int kTouchId2 = 3;
2983  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2984  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2985      delegate.get(), -1234, bounds, root_window()));
2986  TimedEvents tes;
2987
2988  delegate->Reset();
2989  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2990                        kTouchId1, tes.Now());
2991  DispatchEventUsingWindowDispatcher(&press1);
2992  EXPECT_FALSE(delegate->tap());
2993  EXPECT_TRUE(delegate->tap_down());
2994  EXPECT_FALSE(delegate->tap_cancel());
2995  EXPECT_FALSE(delegate->scroll_begin());
2996  EXPECT_FALSE(delegate->scroll_update());
2997  EXPECT_FALSE(delegate->scroll_end());
2998  EXPECT_FALSE(delegate->long_press());
2999  EXPECT_FALSE(delegate->two_finger_tap());
3000
3001  delegate->Reset();
3002  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201),
3003                        kTouchId2, tes.Now());
3004  DispatchEventUsingWindowDispatcher(&press2);
3005  EXPECT_FALSE(delegate->tap());
3006  EXPECT_FALSE(delegate->tap_down());  // no touch down for second tap.
3007  EXPECT_TRUE(delegate->tap_cancel());
3008  EXPECT_FALSE(delegate->scroll_begin());
3009  EXPECT_FALSE(delegate->scroll_update());
3010  EXPECT_FALSE(delegate->scroll_end());
3011  EXPECT_FALSE(delegate->long_press());
3012  EXPECT_FALSE(delegate->two_finger_tap());
3013  EXPECT_FALSE(delegate->pinch_begin());
3014
3015  delegate->Reset();
3016  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(530, 301),
3017                       kTouchId2, tes.Now());
3018  DispatchEventUsingWindowDispatcher(&move2);
3019  EXPECT_FALSE(delegate->tap());
3020  EXPECT_FALSE(delegate->tap_down());
3021  EXPECT_FALSE(delegate->tap_cancel());
3022  // Pinch & Scroll only when there is enough movement.
3023  EXPECT_TRUE(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_TRUE(delegate->pinch_begin());
3029}
3030
3031// Verifies if a window is the target of multiple touch-ids and we hide the
3032// window everything is cleaned up correctly.
3033TEST_P(GestureRecognizerTest, FlushAllOnHide) {
3034  scoped_ptr<GestureEventConsumeDelegate> delegate(
3035      new GestureEventConsumeDelegate());
3036  gfx::Rect bounds(0, 0, 200, 200);
3037  scoped_ptr<aura::Window> window(
3038      CreateTestWindowWithDelegate(delegate.get(), 0, bounds, root_window()));
3039  const int kTouchId1 = 8;
3040  const int kTouchId2 = 2;
3041  TimedEvents tes;
3042
3043  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
3044                        kTouchId1, tes.Now());
3045  DispatchEventUsingWindowDispatcher(&press1);
3046  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
3047                        kTouchId2, tes.Now());
3048  DispatchEventUsingWindowDispatcher(&press2);
3049  window->Hide();
3050  EXPECT_EQ(NULL,
3051      ui::GestureRecognizer::Get()->GetTouchLockedTarget(press1));
3052  EXPECT_EQ(NULL,
3053      ui::GestureRecognizer::Get()->GetTouchLockedTarget(press2));
3054}
3055
3056TEST_P(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) {
3057  // TODO(tdresser): write a version of this test for the unified GR.
3058  if (UsingUnifiedGR())
3059    return;
3060
3061  scoped_ptr<QueueTouchEventDelegate> delegate(
3062      new QueueTouchEventDelegate(host()->dispatcher()));
3063  const int kTouchId = 2;
3064  gfx::Rect bounds(100, 200, 100, 100);
3065  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3066      delegate.get(), -1234, bounds, root_window()));
3067  delegate->set_window(window.get());
3068  TimedEvents tes;
3069
3070  TimerTestGestureRecognizer* gesture_recognizer =
3071      new TimerTestGestureRecognizer();
3072  TimerTestGestureSequence* gesture_sequence =
3073      static_cast<TimerTestGestureSequence*>(
3074          gesture_recognizer->GetGestureSequenceForTesting(window.get()));
3075
3076  ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
3077
3078  delegate->Reset();
3079  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3080                       kTouchId, tes.Now());
3081  DispatchEventUsingWindowDispatcher(&press);
3082  // Scroll around, to cancel the long press
3083  tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3084
3085  delegate->Reset();
3086  delegate->ReceivedAck();
3087  EXPECT_TRUE(delegate->tap_down());
3088  EXPECT_TRUE(gesture_sequence->IsTimerRunning());
3089
3090  delegate->Reset();
3091  delegate->ReceivedAckPreventDefaulted();
3092  EXPECT_FALSE(gesture_sequence->IsTimerRunning());
3093  gesture_sequence->ForceTimeout();
3094  EXPECT_FALSE(delegate->long_press());
3095}
3096
3097// Same as GestureEventConsumeDelegate, but consumes all the touch-move events.
3098class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate {
3099 public:
3100  ConsumesTouchMovesDelegate() : consume_touch_move_(true) {}
3101  virtual ~ConsumesTouchMovesDelegate() {}
3102
3103  void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; }
3104
3105 private:
3106  virtual void OnTouchEvent(ui::TouchEvent* touch) OVERRIDE {
3107    if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED)
3108      touch->SetHandled();
3109    else
3110      GestureEventConsumeDelegate::OnTouchEvent(touch);
3111  }
3112
3113  bool consume_touch_move_;
3114
3115  DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate);
3116};
3117
3118// Same as GestureEventScroll, but tests that the behavior is the same
3119// even if all the touch-move events are consumed.
3120TEST_P(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) {
3121  scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3122      new ConsumesTouchMovesDelegate());
3123  const int kWindowWidth = 123;
3124  const int kWindowHeight = 45;
3125  const int kTouchId = 5;
3126  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3127  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3128      delegate.get(), -1234, bounds, root_window()));
3129  TimedEvents tes;
3130
3131  delegate->Reset();
3132  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3133                       kTouchId, tes.Now());
3134  DispatchEventUsingWindowDispatcher(&press);
3135  EXPECT_FALSE(delegate->tap());
3136  EXPECT_TRUE(delegate->tap_down());
3137  EXPECT_FALSE(delegate->tap_cancel());
3138  EXPECT_TRUE(delegate->begin());
3139  EXPECT_FALSE(delegate->scroll_begin());
3140  EXPECT_FALSE(delegate->scroll_update());
3141  EXPECT_FALSE(delegate->scroll_end());
3142
3143  // Move the touch-point enough so that it would normally be considered a
3144  // scroll. But since the touch-moves will be consumed, the scroll should not
3145  // start.
3146  tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3147  EXPECT_FALSE(delegate->tap());
3148  EXPECT_FALSE(delegate->tap_down());
3149  EXPECT_TRUE(delegate->tap_cancel());
3150  EXPECT_FALSE(delegate->begin());
3151  EXPECT_FALSE(delegate->scroll_update());
3152  EXPECT_FALSE(delegate->scroll_end());
3153
3154  // With the unified gesture detector, consuming the first touch move event
3155  // won't prevent all future scrolling.
3156  if (UsingUnifiedGR())
3157    EXPECT_TRUE(delegate->scroll_begin());
3158  else
3159    EXPECT_FALSE(delegate->scroll_begin());
3160
3161  // Release the touch back at the start point. This should end without causing
3162  // a tap.
3163  delegate->Reset();
3164  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230),
3165                         kTouchId, tes.LeapForward(50));
3166  DispatchEventUsingWindowDispatcher(&release);
3167  EXPECT_FALSE(delegate->tap());
3168  EXPECT_FALSE(delegate->tap_down());
3169  EXPECT_FALSE(delegate->tap_cancel());
3170  EXPECT_FALSE(delegate->begin());
3171  EXPECT_TRUE(delegate->end());
3172  EXPECT_FALSE(delegate->scroll_begin());
3173  EXPECT_FALSE(delegate->scroll_update());
3174
3175  if (UsingUnifiedGR())
3176    EXPECT_TRUE(delegate->scroll_end());
3177  else
3178    EXPECT_FALSE(delegate->scroll_end());
3179}
3180
3181// Tests the behavior of 2F scroll when all the touch-move events are consumed.
3182TEST_P(GestureRecognizerTest, GestureEventScrollTwoFingerTouchMoveConsumed) {
3183  // TODO(tdresser): enable this test with unified GR once two finger tap is
3184  // supported. See crbug.com/354396.
3185  if (UsingUnifiedGR())
3186    return;
3187
3188  scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3189      new ConsumesTouchMovesDelegate());
3190  const int kWindowWidth = 123;
3191  const int kWindowHeight = 100;
3192  const int kTouchId1 = 2;
3193  const int kTouchId2 = 3;
3194  TimedEvents tes;
3195
3196  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3197  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3198      delegate.get(), -1234, bounds, root_window()));
3199
3200  delegate->Reset();
3201  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3202                        kTouchId1, tes.Now());
3203  DispatchEventUsingWindowDispatcher(&press1);
3204  tes.SendScrollEvent(event_processor(), 131, 231, kTouchId1, delegate.get());
3205
3206  // First finger touches down and moves.
3207  EXPECT_FALSE(delegate->tap());
3208  EXPECT_FALSE(delegate->scroll_begin());
3209  EXPECT_FALSE(delegate->scroll_update());
3210  EXPECT_FALSE(delegate->scroll_end());
3211
3212  delegate->Reset();
3213  // Second finger touches down and moves.
3214  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
3215                        kTouchId2, tes.LeapForward(50));
3216  DispatchEventUsingWindowDispatcher(&press2);
3217  tes.SendScrollEvent(event_processor(), 161, 231, kTouchId2, delegate.get());
3218
3219  // PinchBegin & ScrollBegin were not sent if the touch-move events were
3220  // consumed.
3221  EXPECT_FALSE(delegate->pinch_begin());
3222  EXPECT_FALSE(delegate->scroll_begin());
3223
3224  EXPECT_FALSE(delegate->tap());
3225  EXPECT_FALSE(delegate->two_finger_tap());
3226
3227  // Should be no PinchUpdate & ScrollUpdate.
3228  EXPECT_FALSE(delegate->pinch_update());
3229  EXPECT_FALSE(delegate->pinch_end());
3230  EXPECT_FALSE(delegate->scroll_update());
3231  EXPECT_FALSE(delegate->scroll_end());
3232
3233  delegate->Reset();
3234  // Moves First finger again, no PinchUpdate & ScrollUpdate.
3235  tes.SendScrollEvent(event_processor(), 161, 261, kTouchId1, delegate.get());
3236
3237  EXPECT_FALSE(delegate->pinch_update());
3238  EXPECT_FALSE(delegate->pinch_end());
3239  EXPECT_FALSE(delegate->scroll_update());
3240  EXPECT_FALSE(delegate->scroll_end());
3241
3242  // Stops consuming touch-move.
3243  delegate->set_consume_touch_move(false);
3244
3245  delegate->Reset();
3246  // Making a pinch gesture.
3247  tes.SendScrollEvent(event_processor(), 161, 251, kTouchId1, delegate.get());
3248  // If touch moves are ever consumed, we should not see PinchBegin/Update
3249  // even touch moves become not consumed.
3250  EXPECT_FALSE(delegate->scroll_begin());
3251  EXPECT_FALSE(delegate->scroll_update());
3252  EXPECT_FALSE(delegate->scroll_end());
3253
3254  EXPECT_FALSE(delegate->pinch_begin());
3255  EXPECT_FALSE(delegate->pinch_update());
3256  EXPECT_FALSE(delegate->pinch_end());
3257
3258  delegate->Reset();
3259  tes.SendScrollEvent(event_processor(), 161, 241, kTouchId2, delegate.get());
3260  EXPECT_FALSE(delegate->scroll_begin());
3261  EXPECT_FALSE(delegate->scroll_update());
3262  EXPECT_FALSE(delegate->scroll_end());
3263
3264  EXPECT_FALSE(delegate->pinch_begin());
3265  EXPECT_FALSE(delegate->pinch_update());
3266  EXPECT_FALSE(delegate->pinch_end());
3267
3268  delegate->Reset();
3269  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3270                          kTouchId1, tes.Now());
3271  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
3272                          kTouchId2, tes.Now());
3273  DispatchEventUsingWindowDispatcher(&release1);
3274  DispatchEventUsingWindowDispatcher(&release2);
3275
3276  EXPECT_FALSE(delegate->tap());
3277  // Touch release is not consumed, so we still see two finger tap.
3278  EXPECT_TRUE(delegate->two_finger_tap());
3279
3280  // Should not see PinchEnd.
3281  EXPECT_TRUE(delegate->scroll_begin());
3282  EXPECT_FALSE(delegate->scroll_update());
3283  EXPECT_FALSE(delegate->scroll_end());
3284  EXPECT_TRUE(delegate->fling());
3285
3286  EXPECT_FALSE(delegate->pinch_begin());
3287  EXPECT_FALSE(delegate->pinch_update());
3288  EXPECT_FALSE(delegate->pinch_end());
3289}
3290
3291// Like as GestureEventTouchMoveConsumed but tests the different behavior
3292// depending on whether the events were consumed before or after the scroll
3293// started.
3294TEST_P(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) {
3295  scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3296      new ConsumesTouchMovesDelegate());
3297  const int kWindowWidth = 123;
3298  const int kWindowHeight = 45;
3299  const int kTouchId = 5;
3300  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3301  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3302      delegate.get(), -1234, bounds, root_window()));
3303  TimedEvents tes;
3304
3305  delegate->Reset();
3306  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3307                       kTouchId, tes.Now());
3308  DispatchEventUsingWindowDispatcher(&press);
3309  EXPECT_FALSE(delegate->tap());
3310  EXPECT_TRUE(delegate->tap_down());
3311  EXPECT_FALSE(delegate->tap_cancel());
3312  EXPECT_TRUE(delegate->begin());
3313  EXPECT_FALSE(delegate->scroll_begin());
3314  EXPECT_FALSE(delegate->scroll_update());
3315  EXPECT_FALSE(delegate->scroll_end());
3316
3317  // Move the touch-point enough so that it would normally be considered a
3318  // scroll. But since the touch-moves will be consumed, the scroll should not
3319  // start.
3320  tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3321  EXPECT_FALSE(delegate->tap());
3322  EXPECT_FALSE(delegate->tap_down());
3323  EXPECT_TRUE(delegate->tap_cancel());
3324  EXPECT_FALSE(delegate->begin());
3325  EXPECT_FALSE(delegate->scroll_update());
3326  EXPECT_FALSE(delegate->scroll_end());
3327
3328  // With the unified gesture detector, consuming the first touch move event
3329  // won't prevent all future scrolling.
3330  if (UsingUnifiedGR())
3331    EXPECT_TRUE(delegate->scroll_begin());
3332  else
3333    EXPECT_FALSE(delegate->scroll_begin());
3334
3335  // Now, stop consuming touch-move events, and move the touch-point again.
3336  delegate->set_consume_touch_move(false);
3337  tes.SendScrollEvent(event_processor(), 159, 259, kTouchId, delegate.get());
3338  EXPECT_FALSE(delegate->tap());
3339  EXPECT_FALSE(delegate->tap_down());
3340  EXPECT_FALSE(delegate->tap_cancel());
3341  EXPECT_FALSE(delegate->begin());
3342  EXPECT_FALSE(delegate->scroll_begin());
3343  EXPECT_FALSE(delegate->scroll_end());
3344
3345  if (UsingUnifiedGR()) {
3346    // Scroll not prevented by consumed first touch move.
3347    EXPECT_TRUE(delegate->scroll_update());
3348    EXPECT_EQ(29, delegate->scroll_x());
3349    EXPECT_EQ(29, delegate->scroll_y());
3350    EXPECT_EQ(gfx::Point(0, 0).ToString(),
3351              delegate->scroll_begin_position().ToString());
3352  } else {
3353    EXPECT_FALSE(delegate->scroll_update());
3354    // No scroll has occurred, because an early touch move was consumed.
3355    EXPECT_EQ(0, delegate->scroll_x());
3356    EXPECT_EQ(0, delegate->scroll_y());
3357    EXPECT_EQ(gfx::Point(0, 0).ToString(),
3358              delegate->scroll_begin_position().ToString());
3359  }
3360
3361  // Start consuming touch-move events again.
3362  delegate->set_consume_touch_move(true);
3363
3364  // Move some more to generate a few more scroll updates.
3365  tes.SendScrollEvent(event_processor(), 110, 211, kTouchId, delegate.get());
3366  EXPECT_FALSE(delegate->tap());
3367  EXPECT_FALSE(delegate->tap_down());
3368  EXPECT_FALSE(delegate->tap_cancel());
3369  EXPECT_FALSE(delegate->begin());
3370  EXPECT_FALSE(delegate->scroll_begin());
3371  EXPECT_FALSE(delegate->scroll_update());
3372  EXPECT_FALSE(delegate->scroll_end());
3373  EXPECT_EQ(0, delegate->scroll_x());
3374  EXPECT_EQ(0, delegate->scroll_y());
3375
3376  tes.SendScrollEvent(event_processor(), 140, 215, kTouchId, delegate.get());
3377  EXPECT_FALSE(delegate->tap());
3378  EXPECT_FALSE(delegate->tap_down());
3379  EXPECT_FALSE(delegate->tap_cancel());
3380  EXPECT_FALSE(delegate->begin());
3381  EXPECT_FALSE(delegate->scroll_begin());
3382  EXPECT_FALSE(delegate->scroll_update());
3383  EXPECT_FALSE(delegate->scroll_end());
3384  EXPECT_EQ(0, delegate->scroll_x());
3385  EXPECT_EQ(0, delegate->scroll_y());
3386
3387  // Release the touch.
3388  delegate->Reset();
3389  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3390                         kTouchId, tes.LeapForward(50));
3391  DispatchEventUsingWindowDispatcher(&release);
3392  EXPECT_FALSE(delegate->tap());
3393  EXPECT_FALSE(delegate->tap_down());
3394  EXPECT_FALSE(delegate->tap_cancel());
3395  EXPECT_FALSE(delegate->begin());
3396  EXPECT_TRUE(delegate->end());
3397  EXPECT_FALSE(delegate->scroll_begin());
3398  EXPECT_FALSE(delegate->scroll_update());
3399  EXPECT_FALSE(delegate->fling());
3400
3401  if (UsingUnifiedGR())
3402    EXPECT_TRUE(delegate->scroll_end());
3403  else
3404    EXPECT_FALSE(delegate->scroll_end());
3405}
3406
3407// Check that appropriate touch events generate double tap gesture events.
3408TEST_P(GestureRecognizerTest, GestureEventDoubleTap) {
3409  scoped_ptr<GestureEventConsumeDelegate> delegate(
3410      new GestureEventConsumeDelegate());
3411  const int kWindowWidth = 123;
3412  const int kWindowHeight = 45;
3413  const int kTouchId = 2;
3414  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3415  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3416      delegate.get(), -1234, bounds, root_window()));
3417  TimedEvents tes;
3418
3419  // First tap (tested in GestureEventTap)
3420  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3421                        kTouchId, tes.Now());
3422  DispatchEventUsingWindowDispatcher(&press1);
3423  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3424                          kTouchId, tes.LeapForward(50));
3425  DispatchEventUsingWindowDispatcher(&release1);
3426  delegate->Reset();
3427
3428  // Second tap
3429  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3430                        kTouchId, tes.LeapForward(200));
3431  DispatchEventUsingWindowDispatcher(&press2);
3432  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3433                          kTouchId, tes.LeapForward(50));
3434  DispatchEventUsingWindowDispatcher(&release2);
3435
3436  EXPECT_TRUE(delegate->tap());
3437  EXPECT_TRUE(delegate->tap_down());
3438  EXPECT_FALSE(delegate->tap_cancel());
3439  EXPECT_TRUE(delegate->begin());
3440  EXPECT_TRUE(delegate->end());
3441  EXPECT_FALSE(delegate->scroll_begin());
3442  EXPECT_FALSE(delegate->scroll_update());
3443  EXPECT_FALSE(delegate->scroll_end());
3444
3445  EXPECT_EQ(2, delegate->tap_count());
3446}
3447
3448// Check that appropriate touch events generate triple tap gesture events.
3449TEST_P(GestureRecognizerTest, GestureEventTripleTap) {
3450  scoped_ptr<GestureEventConsumeDelegate> delegate(
3451      new GestureEventConsumeDelegate());
3452  const int kWindowWidth = 123;
3453  const int kWindowHeight = 45;
3454  const int kTouchId = 2;
3455  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3456  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3457      delegate.get(), -1234, bounds, root_window()));
3458  TimedEvents tes;
3459
3460  // First tap (tested in GestureEventTap)
3461  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
3462                        kTouchId, tes.Now());
3463  DispatchEventUsingWindowDispatcher(&press1);
3464  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
3465                          kTouchId, tes.LeapForward(50));
3466  DispatchEventUsingWindowDispatcher(&release1);
3467
3468  EXPECT_EQ(1, delegate->tap_count());
3469  delegate->Reset();
3470
3471  // Second tap (tested in GestureEventDoubleTap)
3472  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
3473                        kTouchId, tes.LeapForward(200));
3474  DispatchEventUsingWindowDispatcher(&press2);
3475  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3476                          kTouchId, tes.LeapForward(50));
3477  DispatchEventUsingWindowDispatcher(&release2);
3478
3479  EXPECT_EQ(2, delegate->tap_count());
3480  delegate->Reset();
3481
3482  // Third tap
3483  ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(102, 206),
3484                        kTouchId, tes.LeapForward(200));
3485  DispatchEventUsingWindowDispatcher(&press3);
3486  ui::TouchEvent release3(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
3487                          kTouchId, tes.LeapForward(50));
3488  DispatchEventUsingWindowDispatcher(&release3);
3489
3490  // Third, Fourth and Fifth Taps. Taps after the third should have their
3491  // |tap_count| wrap around back to 1.
3492  for (int i = 3; i < 5; ++i) {
3493    ui::TouchEvent press3(ui::ET_TOUCH_PRESSED,
3494                          gfx::Point(102, 206),
3495                          kTouchId,
3496                          tes.LeapForward(200));
3497    DispatchEventUsingWindowDispatcher(&press3);
3498    ui::TouchEvent release3(ui::ET_TOUCH_RELEASED,
3499                            gfx::Point(102, 206),
3500                            kTouchId,
3501                            tes.LeapForward(50));
3502    DispatchEventUsingWindowDispatcher(&release3);
3503
3504    EXPECT_TRUE(delegate->tap());
3505    EXPECT_TRUE(delegate->tap_down());
3506    EXPECT_FALSE(delegate->tap_cancel());
3507    EXPECT_TRUE(delegate->begin());
3508    EXPECT_TRUE(delegate->end());
3509    EXPECT_FALSE(delegate->scroll_begin());
3510    EXPECT_FALSE(delegate->scroll_update());
3511    EXPECT_FALSE(delegate->scroll_end());
3512
3513    // The behavior for the Aura GR is incorrect.
3514    if (UsingUnifiedGR())
3515      EXPECT_EQ(1 + (i % 3), delegate->tap_count());
3516    else
3517      EXPECT_EQ(3, delegate->tap_count());
3518  }
3519}
3520
3521// Check that we don't get a double tap when the two taps are far apart.
3522TEST_P(GestureRecognizerTest, TwoTapsFarApart) {
3523  scoped_ptr<GestureEventConsumeDelegate> delegate(
3524      new GestureEventConsumeDelegate());
3525  const int kWindowWidth = 123;
3526  const int kWindowHeight = 45;
3527  const int kTouchId = 2;
3528  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3529  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3530      delegate.get(), -1234, bounds, root_window()));
3531  TimedEvents tes;
3532
3533  // First tap (tested in GestureEventTap)
3534  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3535                        kTouchId, tes.Now());
3536  DispatchEventUsingWindowDispatcher(&press1);
3537  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3538                          kTouchId, tes.LeapForward(50));
3539  DispatchEventUsingWindowDispatcher(&release1);
3540  delegate->Reset();
3541
3542  // Second tap, close in time but far in distance
3543  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201),
3544                        kTouchId, tes.LeapForward(200));
3545  DispatchEventUsingWindowDispatcher(&press2);
3546  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201),
3547                          kTouchId, tes.LeapForward(50));
3548  DispatchEventUsingWindowDispatcher(&release2);
3549
3550  EXPECT_TRUE(delegate->tap());
3551  EXPECT_TRUE(delegate->tap_down());
3552  EXPECT_FALSE(delegate->tap_cancel());
3553  EXPECT_TRUE(delegate->begin());
3554  EXPECT_TRUE(delegate->end());
3555  EXPECT_FALSE(delegate->scroll_begin());
3556  EXPECT_FALSE(delegate->scroll_update());
3557  EXPECT_FALSE(delegate->scroll_end());
3558
3559  EXPECT_EQ(1, delegate->tap_count());
3560}
3561
3562// Check that we don't get a double tap when the two taps have a long enough
3563// delay in between.
3564TEST_P(GestureRecognizerTest, TwoTapsWithDelayBetween) {
3565  scoped_ptr<GestureEventConsumeDelegate> delegate(
3566      new GestureEventConsumeDelegate());
3567  const int kWindowWidth = 123;
3568  const int kWindowHeight = 45;
3569  const int kTouchId = 2;
3570  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3571  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3572      delegate.get(), -1234, bounds, root_window()));
3573  TimedEvents tes;
3574
3575  // First tap (tested in GestureEventTap)
3576  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3577                        kTouchId, tes.Now());
3578  DispatchEventUsingWindowDispatcher(&press1);
3579  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3580                          kTouchId, tes.LeapForward(50));
3581  DispatchEventUsingWindowDispatcher(&release1);
3582  delegate->Reset();
3583
3584  // Second tap, close in distance but after some delay
3585  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3586                        kTouchId, tes.LeapForward(2000));
3587  DispatchEventUsingWindowDispatcher(&press2);
3588  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3589                          kTouchId, tes.LeapForward(50));
3590  DispatchEventUsingWindowDispatcher(&release2);
3591
3592  EXPECT_TRUE(delegate->tap());
3593  EXPECT_TRUE(delegate->tap_down());
3594  EXPECT_FALSE(delegate->tap_cancel());
3595  EXPECT_TRUE(delegate->begin());
3596  EXPECT_TRUE(delegate->end());
3597  EXPECT_FALSE(delegate->scroll_begin());
3598  EXPECT_FALSE(delegate->scroll_update());
3599  EXPECT_FALSE(delegate->scroll_end());
3600
3601  EXPECT_EQ(1, delegate->tap_count());
3602}
3603
3604// Checks that if the bounding-box of a gesture changes because of change in
3605// radius of a touch-point, and not because of change in position, then there
3606// are not gesture events from that.
3607TEST_P(GestureRecognizerTest, BoundingBoxRadiusChange) {
3608  // TODO(tdresser): enable this test with unified GR when (if?) bounding box
3609  // behavior is unified.
3610  if (UsingUnifiedGR())
3611    return;
3612
3613  scoped_ptr<GestureEventConsumeDelegate> delegate(
3614      new GestureEventConsumeDelegate());
3615  const int kWindowWidth = 234;
3616  const int kWindowHeight = 345;
3617  const int kTouchId = 5, kTouchId2 = 7;
3618  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3619  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3620      delegate.get(), -1234, bounds, root_window()));
3621  TimedEvents tes;
3622
3623  ui::TouchEvent press1(
3624      ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, tes.Now());
3625  DispatchEventUsingWindowDispatcher(&press1);
3626  EXPECT_TRUE(delegate->bounding_box().IsEmpty());
3627
3628  delegate->Reset();
3629
3630  ui::TouchEvent press2(
3631      ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2,
3632      tes.LeapForward(400));
3633  press2.set_radius_x(5);
3634  DispatchEventUsingWindowDispatcher(&press2);
3635  EXPECT_FALSE(delegate->pinch_begin());
3636  EXPECT_EQ(gfx::Rect(101, 201, 100, 0).ToString(),
3637            delegate->bounding_box().ToString());
3638
3639  delegate->Reset();
3640
3641  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(141, 201), kTouchId,
3642                       tes.LeapForward(40));
3643  DispatchEventUsingWindowDispatcher(&move1);
3644  EXPECT_TRUE(delegate->pinch_begin());
3645  EXPECT_EQ(gfx::Rect(141, 201, 60, 0).ToString(),
3646            delegate->bounding_box().ToString());
3647
3648  delegate->Reset();
3649
3650  // The position doesn't move, but the radius changes.
3651  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 201), kTouchId,
3652                       tes.LeapForward(40));
3653  move2.set_radius_x(50);
3654  move2.set_radius_y(60);
3655  DispatchEventUsingWindowDispatcher(&move2);
3656  EXPECT_FALSE(delegate->tap());
3657  EXPECT_FALSE(delegate->tap_cancel());
3658  EXPECT_FALSE(delegate->scroll_update());
3659  EXPECT_FALSE(delegate->pinch_update());
3660
3661  delegate->Reset();
3662}
3663
3664// Checks that slow scrolls deliver the correct deltas.
3665// In particular, fix for http;//crbug.com/150573.
3666TEST_P(GestureRecognizerTest, NoDriftInScroll) {
3667  ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
3668  ui::GestureConfiguration::set_min_scroll_delta_squared(9);
3669  scoped_ptr<GestureEventConsumeDelegate> delegate(
3670      new GestureEventConsumeDelegate());
3671  const int kWindowWidth = 234;
3672  const int kWindowHeight = 345;
3673  const int kTouchId = 5;
3674  TimedEvents tes;
3675  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3676  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3677      delegate.get(), -1234, bounds, root_window()));
3678
3679  ui::TouchEvent press1(
3680      ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, tes.Now());
3681  DispatchEventUsingWindowDispatcher(&press1);
3682  EXPECT_TRUE(delegate->begin());
3683
3684  delegate->Reset();
3685
3686  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId,
3687                       tes.LeapForward(40));
3688  DispatchEventUsingWindowDispatcher(&move1);
3689  EXPECT_FALSE(delegate->scroll_begin());
3690
3691  delegate->Reset();
3692
3693  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3694                       tes.LeapForward(40));
3695  DispatchEventUsingWindowDispatcher(&move2);
3696  EXPECT_TRUE(delegate->tap_cancel());
3697  EXPECT_TRUE(delegate->scroll_begin());
3698  EXPECT_TRUE(delegate->scroll_update());
3699  // 3 px consumed by touch slop region.
3700  EXPECT_EQ(-1, delegate->scroll_y());
3701  EXPECT_EQ(-4, delegate->scroll_y_hint());
3702
3703  delegate->Reset();
3704
3705  ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3706                       tes.LeapForward(40));
3707  DispatchEventUsingWindowDispatcher(&move3);
3708  EXPECT_FALSE(delegate->scroll_update());
3709
3710  delegate->Reset();
3711
3712  ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId,
3713                       tes.LeapForward(40));
3714  DispatchEventUsingWindowDispatcher(&move4);
3715  EXPECT_TRUE(delegate->scroll_update());
3716  EXPECT_EQ(-1, delegate->scroll_y());
3717
3718  delegate->Reset();
3719}
3720
3721// Ensure that move events which are preventDefaulted will cause a tap
3722// cancel gesture event to be fired if the move would normally cause a
3723// scroll. See bug http://crbug.com/146397.
3724TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveCanFireTapCancel) {
3725  scoped_ptr<ConsumesTouchMovesDelegate> delegate(
3726      new ConsumesTouchMovesDelegate());
3727  const int kTouchId = 5;
3728  gfx::Rect bounds(100, 200, 123, 45);
3729  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3730      delegate.get(), -1234, bounds, root_window()));
3731  TimedEvents tes;
3732
3733  delegate->Reset();
3734  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3735                       kTouchId, tes.Now());
3736
3737  delegate->set_consume_touch_move(false);
3738  DispatchEventUsingWindowDispatcher(&press);
3739  delegate->set_consume_touch_move(true);
3740  delegate->Reset();
3741  // Move the touch-point enough so that it would normally be considered a
3742  // scroll. But since the touch-moves will be consumed, no scrolling should
3743  // occur.
3744  // With the unified gesture detector, we will receive a scroll begin gesture,
3745  // whereas with the aura gesture recognizer we won't.
3746  tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3747  EXPECT_FALSE(delegate->tap());
3748  EXPECT_FALSE(delegate->tap_down());
3749  EXPECT_TRUE(delegate->tap_cancel());
3750  EXPECT_FALSE(delegate->begin());
3751  EXPECT_FALSE(delegate->scroll_update());
3752  EXPECT_FALSE(delegate->scroll_end());
3753}
3754
3755TEST_P(GestureRecognizerTest,
3756       TransferEventDispatchesTouchCancel) {
3757  scoped_ptr<GestureEventConsumeDelegate> delegate(
3758      new GestureEventConsumeDelegate());
3759  TimedEvents tes;
3760  const int kWindowWidth = 800;
3761  const int kWindowHeight = 600;
3762  const int kTouchId = 2;
3763  gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
3764  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3765      delegate.get(), -1234, bounds, root_window()));
3766  scoped_ptr<RemoveOnTouchCancelHandler>
3767      handler(new RemoveOnTouchCancelHandler());
3768  window->AddPreTargetHandler(handler.get());
3769
3770  // Start a gesture sequence on |window|. Then transfer the events to NULL.
3771  // Make sure |window| receives a touch-cancel event.
3772  delegate->Reset();
3773  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3774                       kTouchId, tes.Now());
3775  ui::TouchEvent p2(ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), 1, tes.Now());
3776  DispatchEventUsingWindowDispatcher(&press);
3777  DispatchEventUsingWindowDispatcher(&p2);
3778  EXPECT_FALSE(delegate->tap());
3779  EXPECT_TRUE(delegate->tap_down());
3780  EXPECT_TRUE(delegate->tap_cancel());
3781  EXPECT_TRUE(delegate->begin());
3782  EXPECT_EQ(2, handler->touch_pressed_count());
3783  delegate->Reset();
3784  handler->Reset();
3785
3786  ui::GestureRecognizer* gesture_recognizer = ui::GestureRecognizer::Get();
3787  EXPECT_EQ(window.get(),
3788            gesture_recognizer->GetTouchLockedTarget(press));
3789  gesture_recognizer->TransferEventsTo(window.get(), NULL);
3790  EXPECT_EQ(NULL,
3791            gesture_recognizer->GetTouchLockedTarget(press));
3792  // The event-handler removes |window| from its parent on the first
3793  // touch-cancel event, so it won't receive the second touch-cancel event.
3794  EXPECT_EQ(1, handler->touch_cancelled_count());
3795}
3796
3797// Check that appropriate touch events generate show press events
3798TEST_P(GestureRecognizerTest, GestureEventShowPress) {
3799  scoped_ptr<GestureEventConsumeDelegate> delegate(
3800      new GestureEventConsumeDelegate());
3801  TimedEvents tes;
3802  const int kWindowWidth = 123;
3803  const int kWindowHeight = 45;
3804  const int kTouchId = 2;
3805  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3806  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3807      delegate.get(), -1234, bounds, root_window()));
3808
3809  delegate->Reset();
3810
3811  TimerTestGestureRecognizer* gesture_recognizer =
3812      new TimerTestGestureRecognizer();
3813
3814  ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
3815
3816  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3817                        kTouchId, tes.Now());
3818  DispatchEventUsingWindowDispatcher(&press1);
3819  EXPECT_TRUE(delegate->tap_down());
3820  EXPECT_TRUE(delegate->begin());
3821  EXPECT_FALSE(delegate->tap_cancel());
3822
3823  // We haven't pressed long enough for a show press to occur
3824  EXPECT_FALSE(delegate->show_press());
3825
3826  // Wait until the timer runs out
3827  delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
3828  EXPECT_TRUE(delegate->show_press());
3829  EXPECT_FALSE(delegate->tap_cancel());
3830
3831  delegate->Reset();
3832  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3833                          kTouchId, tes.Now());
3834  DispatchEventUsingWindowDispatcher(&release1);
3835  EXPECT_FALSE(delegate->long_press());
3836
3837  // Note the tap isn't dispatched until the release
3838  EXPECT_FALSE(delegate->tap_cancel());
3839  EXPECT_TRUE(delegate->tap());
3840}
3841
3842// Check that scrolling cancels a show press
3843TEST_P(GestureRecognizerTest, GestureEventShowPressCancelledByScroll) {
3844  scoped_ptr<GestureEventConsumeDelegate> delegate(
3845      new GestureEventConsumeDelegate());
3846  TimedEvents tes;
3847  const int kWindowWidth = 123;
3848  const int kWindowHeight = 45;
3849  const int kTouchId = 6;
3850  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3851  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3852      delegate.get(), -1234, bounds, root_window()));
3853
3854  delegate->Reset();
3855
3856  TimerTestGestureRecognizer* gesture_recognizer =
3857      new TimerTestGestureRecognizer();
3858
3859  ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
3860
3861  TimerTestGestureSequence* gesture_sequence =
3862      static_cast<TimerTestGestureSequence*>(
3863          gesture_recognizer->GetGestureSequenceForTesting(window.get()));
3864
3865  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3866                        kTouchId, tes.Now());
3867  DispatchEventUsingWindowDispatcher(&press1);
3868  EXPECT_TRUE(delegate->tap_down());
3869
3870  // We haven't pressed long enough for a show press to occur
3871  EXPECT_FALSE(delegate->show_press());
3872  EXPECT_FALSE(delegate->tap_cancel());
3873
3874  // Scroll around, to cancel the show press
3875  tes.SendScrollEvent(event_processor(), 130, 230, kTouchId, delegate.get());
3876  // Wait until the timer runs out
3877  gesture_sequence->ForceTimeout();
3878  EXPECT_FALSE(delegate->show_press());
3879  EXPECT_TRUE(delegate->tap_cancel());
3880
3881  delegate->Reset();
3882  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3883                          kTouchId, tes.LeapForward(10));
3884  DispatchEventUsingWindowDispatcher(&release1);
3885  EXPECT_FALSE(delegate->show_press());
3886  EXPECT_FALSE(delegate->tap_cancel());
3887}
3888
3889// Test that show press events are sent immediately on tap
3890TEST_P(GestureRecognizerTest, GestureEventShowPressSentOnTap) {
3891  scoped_ptr<GestureEventConsumeDelegate> delegate(
3892      new GestureEventConsumeDelegate());
3893  TimedEvents tes;
3894  const int kWindowWidth = 123;
3895  const int kWindowHeight = 45;
3896  const int kTouchId = 6;
3897  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
3898  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3899      delegate.get(), -1234, bounds, root_window()));
3900
3901  delegate->Reset();
3902
3903  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
3904                        kTouchId, tes.Now());
3905  DispatchEventUsingWindowDispatcher(&press1);
3906  EXPECT_TRUE(delegate->tap_down());
3907
3908  // We haven't pressed long enough for a show press to occur
3909  EXPECT_FALSE(delegate->show_press());
3910  EXPECT_FALSE(delegate->tap_cancel());
3911
3912  delegate->Reset();
3913  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
3914                          kTouchId, tes.LeapForward(50));
3915  DispatchEventUsingWindowDispatcher(&release1);
3916  EXPECT_TRUE(delegate->show_press());
3917  EXPECT_FALSE(delegate->tap_cancel());
3918  EXPECT_TRUE(delegate->tap());
3919}
3920
3921// Test that consuming the first move touch event prevents a scroll.
3922TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveScrollTest) {
3923  scoped_ptr<QueueTouchEventDelegate> delegate(
3924      new QueueTouchEventDelegate(host()->dispatcher()));
3925  TimedEvents tes;
3926  const int kTouchId = 7;
3927  gfx::Rect bounds(0, 0, 1000, 1000);
3928  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3929      delegate.get(), -1234, bounds, root_window()));
3930
3931  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3932                       kTouchId, tes.Now());
3933  DispatchEventUsingWindowDispatcher(&press);
3934  delegate->ReceivedAck();
3935
3936  // A touch move within the slop region is never consumed in web contents. The
3937  // unified GR won't prevent scroll if a touch move within the slop region is
3938  // consumed, so make sure this touch move exceeds the slop region.
3939  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(10, 10),
3940                       kTouchId, tes.Now());
3941  DispatchEventUsingWindowDispatcher(&move1);
3942  delegate->ReceivedAckPreventDefaulted();
3943
3944  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3945                       kTouchId, tes.Now());
3946  DispatchEventUsingWindowDispatcher(&move2);
3947  delegate->ReceivedAck();
3948
3949  if (UsingUnifiedGR()) {
3950    // With the unified gesture detector, consuming the first touch move event
3951    // won't prevent all future scrolling.
3952    EXPECT_TRUE(delegate->scroll_begin());
3953    EXPECT_TRUE(delegate->scroll_update());
3954  } else {
3955    EXPECT_FALSE(delegate->scroll_begin());
3956    EXPECT_FALSE(delegate->scroll_update());
3957  }
3958}
3959
3960// Test that consuming the first touch move event of a touch point doesn't
3961// prevent pinching once an additional touch has been pressed.
3962TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMovePinchTest) {
3963  // Consuming moves within the touch slop and the the disposition handling of
3964  // pinch events behave differently between the Unified GR and the Aura GR.
3965  if (UsingUnifiedGR())
3966    return;
3967
3968  scoped_ptr<QueueTouchEventDelegate> delegate(
3969      new QueueTouchEventDelegate(host()->dispatcher()));
3970  TimedEvents tes;
3971  const int kTouchId1 = 7;
3972  const int kTouchId2 = 4;
3973  gfx::Rect bounds(0, 0, 1000, 1000);
3974  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
3975      delegate.get(), -1234, bounds, root_window()));
3976
3977  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3978                       kTouchId1, tes.Now());
3979  DispatchEventUsingWindowDispatcher(&press1);
3980  delegate->ReceivedAck();
3981
3982  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
3983                       kTouchId1, tes.Now());
3984  DispatchEventUsingWindowDispatcher(&move1);
3985  delegate->ReceivedAckPreventDefaulted();
3986
3987  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
3988                       kTouchId1, tes.Now());
3989  DispatchEventUsingWindowDispatcher(&move2);
3990  delegate->ReceivedAck();
3991
3992  // We can't scroll, because a move has been consumed.
3993  EXPECT_FALSE(delegate->scroll_begin());
3994  EXPECT_FALSE(delegate->scroll_update());
3995  EXPECT_FALSE(delegate->pinch_begin());
3996
3997  // An additional press will allow us to pinch.
3998  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
3999                       kTouchId2, tes.Now());
4000  DispatchEventUsingWindowDispatcher(&press2);
4001  delegate->ReceivedAck();
4002
4003  ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(20, 20),
4004                       kTouchId2, tes.Now());
4005  DispatchEventUsingWindowDispatcher(&move3);
4006  delegate->ReceivedAck();
4007
4008  EXPECT_TRUE(delegate->pinch_begin());
4009  EXPECT_FALSE(delegate->pinch_update());
4010
4011  delegate->Reset();
4012
4013  ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(40, 40),
4014                       kTouchId2, tes.Now());
4015  DispatchEventUsingWindowDispatcher(&move4);
4016  delegate->ReceivedAck();
4017
4018  EXPECT_TRUE(delegate->pinch_update());
4019  EXPECT_EQ(10, delegate->scroll_x());
4020  EXPECT_EQ(10, delegate->scroll_y());
4021}
4022
4023// Test that consuming the first move touch doesn't prevent a tap.
4024TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveTapTest) {
4025  scoped_ptr<QueueTouchEventDelegate> delegate(
4026      new QueueTouchEventDelegate(host()->dispatcher()));
4027  TimedEvents tes;
4028  const int kTouchId = 7;
4029  gfx::Rect bounds(0, 0, 1000, 1000);
4030  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4031      delegate.get(), -1234, bounds, root_window()));
4032
4033  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
4034                       kTouchId, tes.Now());
4035  DispatchEventUsingWindowDispatcher(&press);
4036  delegate->ReceivedAck();
4037
4038  ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(2, 2),
4039                       kTouchId, tes.Now());
4040  DispatchEventUsingWindowDispatcher(&move);
4041  delegate->ReceivedAckPreventDefaulted();
4042
4043  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(2, 2),
4044                         kTouchId, tes.LeapForward(50));
4045  DispatchEventUsingWindowDispatcher(&release);
4046  delegate->ReceivedAck();
4047
4048  EXPECT_TRUE(delegate->tap());
4049}
4050
4051// Test that consuming the first move touch doesn't prevent a long press.
4052TEST_P(GestureRecognizerTest, GestureEventConsumedTouchMoveLongPressTest) {
4053  scoped_ptr<QueueTouchEventDelegate> delegate(
4054      new QueueTouchEventDelegate(host()->dispatcher()));
4055  TimedEvents tes;
4056  const int kWindowWidth = 123;
4057  const int kWindowHeight = 45;
4058  const int kTouchId = 2;
4059  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4060  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4061      delegate.get(), -1234, bounds, root_window()));
4062
4063  delegate->Reset();
4064
4065  TimerTestGestureRecognizer* gesture_recognizer =
4066      new TimerTestGestureRecognizer();
4067
4068  ScopedGestureRecognizerSetter gr_setter(gesture_recognizer);
4069
4070  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4071                        kTouchId, tes.Now());
4072  DispatchEventUsingWindowDispatcher(&press1);
4073  delegate->ReceivedAck();
4074
4075  ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103, 203),
4076                       kTouchId, tes.Now());
4077  DispatchEventUsingWindowDispatcher(&move);
4078  delegate->ReceivedAckPreventDefaulted();
4079
4080  // Wait until the timer runs out
4081  delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4082  EXPECT_TRUE(delegate->long_press());
4083}
4084
4085// Tests that the deltas are correct when leaving the slop region very slowly.
4086TEST_P(GestureRecognizerTest, TestExceedingSlopSlowly) {
4087  // Disabled for unified GR due to subtle differences in touch slop handling.
4088  if (UsingUnifiedGR())
4089    return;
4090
4091  ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
4092  scoped_ptr<GestureEventConsumeDelegate> delegate(
4093      new GestureEventConsumeDelegate());
4094  const int kWindowWidth = 234;
4095  const int kWindowHeight = 345;
4096  const int kTouchId = 5;
4097  TimedEvents tes;
4098  gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4099  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4100      delegate.get(), -1234, bounds, root_window()));
4101
4102  ui::TouchEvent press(
4103      ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), kTouchId, tes.Now());
4104  DispatchEventUsingWindowDispatcher(&press);
4105  EXPECT_FALSE(delegate->scroll_begin());
4106  EXPECT_FALSE(delegate->scroll_update());
4107  delegate->Reset();
4108
4109  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(11, 10), kTouchId,
4110                       tes.LeapForward(40));
4111  DispatchEventUsingWindowDispatcher(&move1);
4112  EXPECT_FALSE(delegate->scroll_begin());
4113  EXPECT_FALSE(delegate->scroll_update());
4114  EXPECT_EQ(0, delegate->scroll_x());
4115  EXPECT_EQ(0, delegate->scroll_x_hint());
4116  delegate->Reset();
4117
4118  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(12, 10), kTouchId,
4119                       tes.LeapForward(40));
4120  DispatchEventUsingWindowDispatcher(&move2);
4121  EXPECT_FALSE(delegate->scroll_begin());
4122  EXPECT_FALSE(delegate->scroll_update());
4123  EXPECT_EQ(0, delegate->scroll_x());
4124  EXPECT_EQ(0, delegate->scroll_x_hint());
4125  delegate->Reset();
4126
4127
4128  ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(13, 10), kTouchId,
4129                       tes.LeapForward(40));
4130  DispatchEventUsingWindowDispatcher(&move3);
4131  EXPECT_TRUE(delegate->scroll_begin());
4132  EXPECT_FALSE(delegate->scroll_update());
4133  EXPECT_EQ(0, delegate->scroll_x());
4134  EXPECT_EQ(3, delegate->scroll_x_hint());
4135  delegate->Reset();
4136
4137
4138  ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(14, 10), kTouchId,
4139                       tes.LeapForward(40));
4140  DispatchEventUsingWindowDispatcher(&move4);
4141  EXPECT_FALSE(delegate->scroll_begin());
4142  EXPECT_TRUE(delegate->scroll_update());
4143  EXPECT_EQ(1, delegate->scroll_x());
4144  EXPECT_EQ(0, delegate->scroll_x_hint());
4145  delegate->Reset();
4146}
4147
4148TEST_P(GestureRecognizerTest, ScrollAlternatelyConsumedTest) {
4149  scoped_ptr<QueueTouchEventDelegate> delegate(
4150      new QueueTouchEventDelegate(host()->dispatcher()));
4151  TimedEvents tes;
4152  const int kWindowWidth = 3000;
4153  const int kWindowHeight = 3000;
4154  const int kTouchId = 2;
4155  gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4156  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4157      delegate.get(), -1234, bounds, root_window()));
4158
4159  delegate->Reset();
4160
4161  int x = 0;
4162  int y = 0;
4163
4164  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
4165                        kTouchId, tes.Now());
4166  DispatchEventUsingWindowDispatcher(&press1);
4167  delegate->ReceivedAck();
4168  EXPECT_FALSE(delegate->scroll_begin());
4169  EXPECT_FALSE(delegate->scroll_update());
4170  delegate->Reset();
4171
4172  x += 100;
4173  y += 100;
4174  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
4175                       kTouchId, tes.Now());
4176  DispatchEventUsingWindowDispatcher(&move1);
4177  delegate->ReceivedAck();
4178  EXPECT_TRUE(delegate->scroll_begin());
4179  EXPECT_TRUE(delegate->scroll_update());
4180  delegate->Reset();
4181
4182  for (int i = 0; i < 3; ++i) {
4183    x += 10;
4184    y += 10;
4185    ui::TouchEvent move2(
4186        ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
4187    DispatchEventUsingWindowDispatcher(&move2);
4188    delegate->ReceivedAck();
4189    EXPECT_FALSE(delegate->scroll_begin());
4190    EXPECT_TRUE(delegate->scroll_update());
4191    EXPECT_EQ(10, delegate->scroll_x());
4192    EXPECT_EQ(10, delegate->scroll_y());
4193    delegate->Reset();
4194
4195    x += 20;
4196    y += 20;
4197    ui::TouchEvent move3(
4198        ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId, tes.Now());
4199    DispatchEventUsingWindowDispatcher(&move3);
4200    delegate->ReceivedAckPreventDefaulted();
4201    EXPECT_FALSE(delegate->scroll_begin());
4202    EXPECT_FALSE(delegate->scroll_update());
4203    delegate->Reset();
4204  }
4205}
4206
4207TEST_P(GestureRecognizerTest, PinchAlternatelyConsumedTest) {
4208  // Disabled for unified GR due to differences in when scroll update is
4209  // sent. The Aura GR will never send a ScrollUpdate with a ScrollBegin, but
4210  // the unified GR will.
4211  if (UsingUnifiedGR())
4212    return;
4213
4214  scoped_ptr<QueueTouchEventDelegate> delegate(
4215      new QueueTouchEventDelegate(host()->dispatcher()));
4216  TimedEvents tes;
4217  const int kWindowWidth = 3000;
4218  const int kWindowHeight = 3000;
4219  const int kTouchId1 = 5;
4220  const int kTouchId2 = 7;
4221  gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
4222  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4223      delegate.get(), -1234, bounds, root_window()));
4224
4225  delegate->Reset();
4226
4227  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
4228                        kTouchId1, tes.Now());
4229  DispatchEventUsingWindowDispatcher(&press1);
4230  delegate->ReceivedAck();
4231  EXPECT_FALSE(delegate->scroll_begin());
4232  EXPECT_FALSE(delegate->scroll_update());
4233  delegate->Reset();
4234
4235  int x = 0;
4236  int y = 0;
4237
4238  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(x, y),
4239                        kTouchId2, tes.Now());
4240  DispatchEventUsingWindowDispatcher(&press2);
4241  delegate->ReceivedAck();
4242  EXPECT_FALSE(delegate->scroll_begin());
4243  EXPECT_FALSE(delegate->scroll_update());
4244  EXPECT_FALSE(delegate->pinch_begin());
4245  EXPECT_FALSE(delegate->pinch_update());
4246
4247  delegate->Reset();
4248
4249  x += 100;
4250  y += 100;
4251  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
4252                       kTouchId2, tes.Now());
4253  DispatchEventUsingWindowDispatcher(&move1);
4254  delegate->ReceivedAck();
4255  EXPECT_TRUE(delegate->scroll_begin());
4256  EXPECT_FALSE(delegate->scroll_update());
4257  EXPECT_TRUE(delegate->pinch_begin());
4258  EXPECT_FALSE(delegate->pinch_update());
4259  delegate->Reset();
4260
4261  const float expected_scales[] = {1.5f, 1.2f, 1.125f};
4262
4263  for (int i = 0; i < 3; ++i) {
4264    x += 50;
4265    y += 50;
4266    ui::TouchEvent move2(
4267        ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4268    DispatchEventUsingWindowDispatcher(&move2);
4269    delegate->ReceivedAck();
4270    EXPECT_FALSE(delegate->scroll_begin());
4271    EXPECT_TRUE(delegate->scroll_update());
4272    EXPECT_FALSE(delegate->scroll_end());
4273    EXPECT_FALSE(delegate->pinch_begin());
4274    EXPECT_TRUE(delegate->pinch_update());
4275    EXPECT_FALSE(delegate->pinch_end());
4276    EXPECT_EQ(25, delegate->scroll_x());
4277    EXPECT_EQ(25, delegate->scroll_y());
4278    EXPECT_FLOAT_EQ(expected_scales[i], delegate->scale());
4279    delegate->Reset();
4280
4281    x += 100;
4282    y += 100;
4283    ui::TouchEvent move3(
4284        ui::ET_TOUCH_MOVED, gfx::Point(x, y), kTouchId2, tes.Now());
4285    DispatchEventUsingWindowDispatcher(&move3);
4286    delegate->ReceivedAckPreventDefaulted();
4287    EXPECT_FALSE(delegate->scroll_begin());
4288    EXPECT_FALSE(delegate->scroll_update());
4289    EXPECT_FALSE(delegate->scroll_end());
4290    EXPECT_FALSE(delegate->pinch_begin());
4291    EXPECT_FALSE(delegate->pinch_update());
4292    EXPECT_FALSE(delegate->pinch_end());
4293    delegate->Reset();
4294  }
4295}
4296
4297// Test that touch event flags are passed through to the gesture event.
4298TEST_P(GestureRecognizerTest, GestureEventFlagsPassedFromTouchEvent) {
4299  scoped_ptr<GestureEventConsumeDelegate> delegate(
4300      new GestureEventConsumeDelegate());
4301  TimedEvents tes;
4302  const int kWindowWidth = 123;
4303  const int kWindowHeight = 45;
4304  const int kTouchId = 6;
4305  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4306  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4307      delegate.get(), -1234, bounds, root_window()));
4308
4309  delegate->Reset();
4310
4311  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4312                        kTouchId, tes.Now());
4313  DispatchEventUsingWindowDispatcher(&press1);
4314  EXPECT_TRUE(delegate->tap_down());
4315
4316  int default_flags = delegate->flags();
4317
4318  ui::TouchEvent move1(
4319      ui::ET_TOUCH_MOVED, gfx::Point(397, 149), kTouchId, tes.LeapForward(50));
4320  move1.set_flags(992);
4321
4322  DispatchEventUsingWindowDispatcher(&move1);
4323  EXPECT_NE(default_flags, delegate->flags());
4324}
4325
4326// Test that latency info is passed through to the gesture event.
4327TEST_P(GestureRecognizerTest, LatencyPassedFromTouchEvent) {
4328  scoped_ptr<GestureEventConsumeDelegate> delegate(
4329      new GestureEventConsumeDelegate());
4330  TimedEvents tes;
4331  const int kWindowWidth = 123;
4332  const int kWindowHeight = 45;
4333  const int kTouchId = 6;
4334
4335  const base::TimeTicks time_original = base::TimeTicks::FromInternalValue(100);
4336  const base::TimeTicks time_ui = base::TimeTicks::FromInternalValue(200);
4337  const base::TimeTicks time_acked = base::TimeTicks::FromInternalValue(300);
4338
4339  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4340  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
4341      delegate.get(), -1234, bounds, root_window()));
4342
4343  delegate->Reset();
4344
4345  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
4346                        kTouchId, tes.Now());
4347
4348  // Ensure the only components around are the ones we add.
4349  press1.latency()->Clear();
4350
4351  press1.latency()->AddLatencyNumberWithTimestamp(
4352      ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, 0, time_original, 1);
4353
4354  press1.latency()->AddLatencyNumberWithTimestamp(
4355      ui::INPUT_EVENT_LATENCY_UI_COMPONENT, 0, 0, time_ui, 1);
4356
4357  press1.latency()->AddLatencyNumberWithTimestamp(
4358      ui::INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT, 0, 0, time_acked, 1);
4359
4360  DispatchEventUsingWindowDispatcher(&press1);
4361  EXPECT_TRUE(delegate->tap_down());
4362
4363  ui::LatencyInfo::LatencyComponent component;
4364
4365  EXPECT_EQ(3U, delegate->latency_info().latency_components.size());
4366  ASSERT_TRUE(delegate->latency_info().FindLatency(
4367      ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, &component));
4368  EXPECT_EQ(time_original, component.event_time);
4369
4370  ASSERT_TRUE(delegate->latency_info().FindLatency(
4371      ui::INPUT_EVENT_LATENCY_UI_COMPONENT, 0, &component));
4372  EXPECT_EQ(time_ui, component.event_time);
4373
4374  ASSERT_TRUE(delegate->latency_info().FindLatency(
4375      ui::INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT, 0, &component));
4376  EXPECT_EQ(time_acked, component.event_time);
4377
4378  delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_SHOW_PRESS);
4379  EXPECT_TRUE(delegate->show_press());
4380  EXPECT_EQ(0U, delegate->latency_info().latency_components.size());
4381}
4382
4383// A delegate that deletes a window on long press.
4384class GestureEventDeleteWindowOnLongPress : public GestureEventConsumeDelegate {
4385 public:
4386  GestureEventDeleteWindowOnLongPress()
4387      : window_(NULL) {}
4388
4389  void set_window(aura::Window** window) { window_ = window; }
4390
4391  virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
4392    GestureEventConsumeDelegate::OnGestureEvent(gesture);
4393    if (gesture->type() != ui::ET_GESTURE_LONG_PRESS)
4394      return;
4395    ui::GestureRecognizer::Get()->CleanupStateForConsumer(*window_);
4396    delete *window_;
4397    *window_ = NULL;
4398  }
4399
4400 private:
4401  aura::Window** window_;
4402  DISALLOW_COPY_AND_ASSIGN(GestureEventDeleteWindowOnLongPress);
4403};
4404
4405// Check that deleting the window in response to a long press gesture doesn't
4406// crash.
4407TEST_P(GestureRecognizerTest, GestureEventLongPressDeletingWindow) {
4408  GestureEventDeleteWindowOnLongPress delegate;
4409  const int kWindowWidth = 123;
4410  const int kWindowHeight = 45;
4411  const int kTouchId = 2;
4412  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
4413  aura::Window* window(CreateTestWindowWithDelegate(
4414      &delegate, -1234, bounds, root_window()));
4415  delegate.set_window(&window);
4416
4417  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED,
4418                        gfx::Point(101, 201),
4419                        kTouchId,
4420                        ui::EventTimeForNow());
4421  DispatchEventUsingWindowDispatcher(&press1);
4422  EXPECT_TRUE(window != NULL);
4423
4424  // Wait until the timer runs out.
4425  delegate.WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
4426  EXPECT_EQ(NULL, window);
4427}
4428
4429INSTANTIATE_TEST_CASE_P(GestureRecognizer,
4430                        GestureRecognizerTest,
4431                        ::testing::Bool());
4432
4433}  // namespace test
4434}  // namespace aura
4435