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