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