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