gesture_recognizer_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/memory/scoped_vector.h"
6#include "base/run_loop.h"
7#include "base/string_number_conversions.h"
8#include "base/timer.h"
9#include "testing/gtest/include/gtest/gtest.h"
10#include "ui/aura/env.h"
11#include "ui/aura/root_window.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/base/events/event.h"
17#include "ui/base/gestures/gesture_configuration.h"
18#include "ui/base/gestures/gesture_recognizer_impl.h"
19#include "ui/base/gestures/gesture_sequence.h"
20#include "ui/base/gestures/gesture_types.h"
21#include "ui/base/hit_test.h"
22#include "ui/gfx/point.h"
23#include "ui/gfx/rect.h"
24
25#include <queue>
26
27namespace aura {
28namespace test {
29
30namespace {
31
32std::string WindowIDAsString(ui::GestureConsumer* consumer) {
33  return consumer && !consumer->ignores_events() ?
34      base::IntToString(static_cast<Window*>(consumer)->id()) : "?";
35}
36
37#define EXPECT_0_EVENTS(events) \
38    EXPECT_EQ(0u, events.size())
39
40#define EXPECT_1_EVENT(events, e0) \
41    EXPECT_EQ(1u, events.size()); \
42    EXPECT_EQ(e0, events[0])
43
44#define EXPECT_2_EVENTS(events, e0, e1) \
45    EXPECT_EQ(2u, events.size()); \
46    EXPECT_EQ(e0, events[0]); \
47    EXPECT_EQ(e1, events[1])
48
49#define EXPECT_3_EVENTS(events, e0, e1, e2) \
50    EXPECT_EQ(3u, events.size()); \
51    EXPECT_EQ(e0, events[0]); \
52    EXPECT_EQ(e1, events[1]); \
53    EXPECT_EQ(e2, events[2])
54
55#define EXPECT_4_EVENTS(events, e0, e1, e2, e3) \
56    EXPECT_EQ(4u, events.size()); \
57    EXPECT_EQ(e0, events[0]); \
58    EXPECT_EQ(e1, events[1]); \
59    EXPECT_EQ(e2, events[2]); \
60    EXPECT_EQ(e3, events[3])
61
62// A delegate that keeps track of gesture events.
63class GestureEventConsumeDelegate : public TestWindowDelegate {
64 public:
65  GestureEventConsumeDelegate()
66      : tap_(false),
67        tap_down_(false),
68        tap_cancel_(false),
69        begin_(false),
70        end_(false),
71        double_tap_(false),
72        scroll_begin_(false),
73        scroll_update_(false),
74        scroll_end_(false),
75        pinch_begin_(false),
76        pinch_update_(false),
77        pinch_end_(false),
78        long_press_(false),
79        fling_(false),
80        two_finger_tap_(false),
81        swipe_left_(false),
82        swipe_right_(false),
83        swipe_up_(false),
84        swipe_down_(false),
85        scroll_x_(0),
86        scroll_y_(0),
87        scroll_velocity_x_(0),
88        scroll_velocity_y_(0),
89        velocity_x_(0),
90        velocity_y_(0),
91        tap_count_(0),
92        wait_until_event_(ui::ET_UNKNOWN) {
93  }
94
95  virtual ~GestureEventConsumeDelegate() {}
96
97  void Reset() {
98    events_.clear();
99    tap_ = false;
100    tap_down_ = false;
101    tap_cancel_ = false;
102    begin_ = false;
103    end_ = false;
104    double_tap_ = false;
105    scroll_begin_ = false;
106    scroll_update_ = false;
107    scroll_end_ = false;
108    pinch_begin_ = false;
109    pinch_update_ = false;
110    pinch_end_ = false;
111    long_press_ = false;
112    fling_ = false;
113    two_finger_tap_ = false;
114    swipe_left_ = false;
115    swipe_right_ = false;
116    swipe_up_ = false;
117    swipe_down_ = false;
118
119    scroll_begin_position_.SetPoint(0, 0);
120    tap_location_.SetPoint(0, 0);
121
122    scroll_x_ = 0;
123    scroll_y_ = 0;
124    scroll_velocity_x_ = 0;
125    scroll_velocity_y_ = 0;
126    velocity_x_ = 0;
127    velocity_y_ = 0;
128    tap_count_ = 0;
129  }
130
131  const std::vector<ui::EventType>& events() const { return events_; };
132
133  bool tap() const { return tap_; }
134  bool tap_down() const { return tap_down_; }
135  bool tap_cancel() const { return tap_cancel_; }
136  bool begin() const { return begin_; }
137  bool end() const { return end_; }
138  bool double_tap() const { return double_tap_; }
139  bool scroll_begin() const { return scroll_begin_; }
140  bool scroll_update() const { return scroll_update_; }
141  bool scroll_end() const { return scroll_end_; }
142  bool pinch_begin() const { return pinch_begin_; }
143  bool pinch_update() const { return pinch_update_; }
144  bool pinch_end() const { return pinch_end_; }
145  bool long_press() const { return long_press_; }
146  bool long_tap() const { return long_tap_; }
147  bool fling() const { return fling_; }
148  bool two_finger_tap() const { return two_finger_tap_; }
149  bool swipe_left() const { return swipe_left_; }
150  bool swipe_right() const { return swipe_right_; }
151  bool swipe_up() const { return swipe_up_; }
152  bool swipe_down() const { return swipe_down_; }
153
154  const gfx::Point scroll_begin_position() const {
155    return scroll_begin_position_;
156  }
157
158  const gfx::Point tap_location() const {
159    return tap_location_;
160  }
161
162  float scroll_x() const { return scroll_x_; }
163  float scroll_y() const { return scroll_y_; }
164  float scroll_velocity_x() const { return scroll_velocity_x_; }
165  float scroll_velocity_y() const { return scroll_velocity_y_; }
166  int touch_id() const { return touch_id_; }
167  float velocity_x() const { return velocity_x_; }
168  float velocity_y() const { return velocity_y_; }
169  const gfx::Rect& bounding_box() const { return bounding_box_; }
170  int tap_count() const { return tap_count_; }
171
172  void WaitUntilReceivedGesture(ui::EventType type) {
173    wait_until_event_ = type;
174    run_loop_.reset(new base::RunLoop(
175        Env::GetInstance()->GetDispatcher()));
176    run_loop_->Run();
177  }
178
179  virtual ui::EventResult OnGestureEvent(
180      ui::GestureEvent* gesture) OVERRIDE {
181    events_.push_back(gesture->type());
182    bounding_box_ = gesture->details().bounding_box();
183    switch (gesture->type()) {
184      case ui::ET_GESTURE_TAP:
185        tap_location_ = gesture->location();
186        tap_count_ = gesture->details().tap_count();
187        tap_ = true;
188        break;
189      case ui::ET_GESTURE_TAP_DOWN:
190        tap_down_ = true;
191        break;
192      case ui::ET_GESTURE_TAP_CANCEL:
193        tap_cancel_ = true;
194        break;
195      case ui::ET_GESTURE_BEGIN:
196        begin_ = true;
197        break;
198      case ui::ET_GESTURE_END:
199        end_ = true;
200        break;
201      case ui::ET_GESTURE_DOUBLE_TAP:
202        double_tap_ = true;
203        break;
204      case ui::ET_GESTURE_SCROLL_BEGIN:
205        scroll_begin_ = true;
206        scroll_begin_position_ = gesture->location();
207        break;
208      case ui::ET_GESTURE_SCROLL_UPDATE:
209        scroll_update_ = true;
210        scroll_x_ += gesture->details().scroll_x();
211        scroll_y_ += gesture->details().scroll_y();
212        scroll_velocity_x_ = gesture->details().velocity_x();
213        scroll_velocity_y_ = gesture->details().velocity_y();
214        break;
215      case ui::ET_GESTURE_SCROLL_END:
216        EXPECT_TRUE(velocity_x_ == 0 && velocity_y_ == 0);
217        scroll_end_ = true;
218        break;
219      case ui::ET_GESTURE_PINCH_BEGIN:
220        pinch_begin_ = true;
221        break;
222      case ui::ET_GESTURE_PINCH_UPDATE:
223        pinch_update_ = true;
224        break;
225      case ui::ET_GESTURE_PINCH_END:
226        pinch_end_ = true;
227        break;
228      case ui::ET_GESTURE_LONG_PRESS:
229        long_press_ = true;
230        touch_id_ = gesture->details().touch_id();
231        break;
232      case ui::ET_GESTURE_LONG_TAP:
233        long_tap_ = true;
234        break;
235      case ui::ET_SCROLL_FLING_START:
236        EXPECT_TRUE(gesture->details().velocity_x() != 0 ||
237                    gesture->details().velocity_y() != 0);
238        EXPECT_FALSE(scroll_end_);
239        fling_ = true;
240        velocity_x_ = gesture->details().velocity_x();
241        velocity_y_ = gesture->details().velocity_y();
242        break;
243      case ui::ET_GESTURE_TWO_FINGER_TAP:
244        two_finger_tap_ = true;
245        break;
246      case ui::ET_GESTURE_MULTIFINGER_SWIPE:
247        swipe_left_ = gesture->details().swipe_left();
248        swipe_right_ = gesture->details().swipe_right();
249        swipe_up_ = gesture->details().swipe_up();
250        swipe_down_ = gesture->details().swipe_down();
251        break;
252      default:
253        NOTREACHED();
254    }
255    if (wait_until_event_ == gesture->type() && run_loop_.get()) {
256      run_loop_->Quit();
257      wait_until_event_ = ui::ET_UNKNOWN;
258    }
259    return ui::ER_CONSUMED;
260  }
261
262 private:
263  scoped_ptr<base::RunLoop> run_loop_;
264  std::vector<ui::EventType> events_;
265
266  bool tap_;
267  bool tap_down_;
268  bool tap_cancel_;
269  bool begin_;
270  bool end_;
271  bool double_tap_;
272  bool scroll_begin_;
273  bool scroll_update_;
274  bool scroll_end_;
275  bool pinch_begin_;
276  bool pinch_update_;
277  bool pinch_end_;
278  bool long_press_;
279  bool long_tap_;
280  bool fling_;
281  bool two_finger_tap_;
282  bool swipe_left_;
283  bool swipe_right_;
284  bool swipe_up_;
285  bool swipe_down_;
286
287  gfx::Point scroll_begin_position_;
288  gfx::Point tap_location_;
289
290  float scroll_x_;
291  float scroll_y_;
292  float scroll_velocity_x_;
293  float scroll_velocity_y_;
294  float velocity_x_;
295  float velocity_y_;
296  int touch_id_;
297  gfx::Rect bounding_box_;
298  int tap_count_;
299
300  ui::EventType wait_until_event_;
301
302  DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate);
303};
304
305class QueueTouchEventDelegate : public GestureEventConsumeDelegate {
306 public:
307  explicit QueueTouchEventDelegate(RootWindow* root_window)
308      : window_(NULL),
309        root_window_(root_window),
310        queue_events_(true) {
311  }
312  virtual ~QueueTouchEventDelegate() {}
313
314  virtual ui::EventResult OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
315    if (queue_events_) {
316      queue_.push(new ui::TouchEvent(*event, window_, window_));
317      return ui::ER_CONSUMED;
318    }
319    return ui::ER_UNHANDLED;
320  }
321
322  void ReceivedAck() {
323    ReceivedAckImpl(false);
324  }
325
326  void ReceivedAckPreventDefaulted() {
327    ReceivedAckImpl(true);
328  }
329
330  void set_window(Window* w) { window_ = w; }
331  void set_queue_events(bool queue) { queue_events_ = queue; }
332
333 private:
334  void ReceivedAckImpl(bool prevent_defaulted) {
335    scoped_ptr<ui::TouchEvent> event(queue_.front());
336    root_window_->ProcessedTouchEvent(event.get(), window_,
337        prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED);
338    queue_.pop();
339  }
340
341  std::queue<ui::TouchEvent*> queue_;
342  Window* window_;
343  RootWindow* root_window_;
344  bool queue_events_;
345
346  DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate);
347};
348
349// A delegate that ignores gesture events but keeps track of [synthetic] mouse
350// events.
351class GestureEventSynthDelegate : public TestWindowDelegate {
352 public:
353  GestureEventSynthDelegate()
354      : mouse_enter_(false),
355        mouse_exit_(false),
356        mouse_press_(false),
357        mouse_release_(false),
358        mouse_move_(false),
359        double_click_(false) {
360  }
361
362  void Reset() {
363    mouse_enter_ = false;
364    mouse_exit_ = false;
365    mouse_press_ = false;
366    mouse_release_ = false;
367    mouse_move_ = false;
368    double_click_ = false;
369  }
370
371  bool mouse_enter() const { return mouse_enter_; }
372  bool mouse_exit() const { return mouse_exit_; }
373  bool mouse_press() const { return mouse_press_; }
374  bool mouse_move() const { return mouse_move_; }
375  bool mouse_release() const { return mouse_release_; }
376  bool double_click() const { return double_click_; }
377
378  virtual ui::EventResult OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
379    switch (event->type()) {
380      case ui::ET_MOUSE_PRESSED:
381        double_click_ = event->flags() & ui::EF_IS_DOUBLE_CLICK;
382        mouse_press_ = true;
383        break;
384      case ui::ET_MOUSE_RELEASED:
385        mouse_release_ = true;
386        break;
387      case ui::ET_MOUSE_MOVED:
388        mouse_move_ = true;
389        break;
390      case ui::ET_MOUSE_ENTERED:
391        mouse_enter_ = true;
392        break;
393      case ui::ET_MOUSE_EXITED:
394        mouse_exit_ = true;
395        break;
396      default:
397        NOTREACHED();
398    }
399    return ui::ER_HANDLED;
400  }
401
402 private:
403  bool mouse_enter_;
404  bool mouse_exit_;
405  bool mouse_press_;
406  bool mouse_release_;
407  bool mouse_move_;
408  bool double_click_;
409
410  DISALLOW_COPY_AND_ASSIGN(GestureEventSynthDelegate);
411};
412
413class TestOneShotGestureSequenceTimer
414    : public base::OneShotTimer<ui::GestureSequence> {
415 public:
416  TestOneShotGestureSequenceTimer() {}
417
418  void ForceTimeout() {
419    if (IsRunning()) {
420      user_task().Run();
421      Stop();
422    }
423  }
424
425 private:
426  DISALLOW_COPY_AND_ASSIGN(TestOneShotGestureSequenceTimer);
427};
428
429class TimerTestGestureSequence : public ui::GestureSequence {
430 public:
431  explicit TimerTestGestureSequence(ui::GestureEventHelper* helper)
432      : ui::GestureSequence(helper) {
433  }
434
435  void ForceTimeout() {
436    static_cast<TestOneShotGestureSequenceTimer*>(
437        GetLongPressTimer())->ForceTimeout();
438  }
439
440  bool IsTimerRunning() {
441    return GetLongPressTimer()->IsRunning();
442  }
443
444  virtual base::OneShotTimer<ui::GestureSequence>* CreateTimer() OVERRIDE {
445    return new TestOneShotGestureSequenceTimer();
446  }
447
448 private:
449  DISALLOW_COPY_AND_ASSIGN(TimerTestGestureSequence);
450};
451
452class TestGestureRecognizer : public ui::GestureRecognizerImpl {
453 public:
454  explicit TestGestureRecognizer(RootWindow* root_window)
455      : GestureRecognizerImpl(root_window) {
456  }
457
458  ui::GestureSequence* GetGestureSequenceForTesting(Window* window) {
459    return GetGestureSequenceForConsumer(window);
460  }
461
462 private:
463  DISALLOW_COPY_AND_ASSIGN(TestGestureRecognizer);
464};
465
466class TimerTestGestureRecognizer : public TestGestureRecognizer {
467 public:
468  explicit TimerTestGestureRecognizer(RootWindow* root_window)
469      : TestGestureRecognizer(root_window) {
470  }
471
472  virtual ui::GestureSequence* CreateSequence(
473      ui::GestureEventHelper* helper) OVERRIDE {
474    return new TimerTestGestureSequence(helper);
475  }
476
477 private:
478  DISALLOW_COPY_AND_ASSIGN(TimerTestGestureRecognizer);
479};
480
481base::TimeDelta GetTime() {
482  return base::Time::NowFromSystemTime() - base::Time();
483}
484
485void SendScrollEvents(RootWindow* root_window,
486                      int x_start,
487                      int y_start,
488                      base::TimeDelta time_start,
489                      int dx,
490                      int dy,
491                      int touch_id,
492                      int time_step,
493                      int num_steps,
494                      GestureEventConsumeDelegate* delegate) {
495  int x = x_start;
496  int y = y_start;
497  base::TimeDelta time = time_start;
498
499  for (int i = 0; i < num_steps; i++) {
500    x += dx;
501    y += dy;
502    time = time + base::TimeDelta::FromMilliseconds(time_step);
503    ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
504                            touch_id, time);
505    root_window->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
506  }
507}
508
509void SendScrollEvent(RootWindow* root_window,
510                     int x,
511                     int y,
512                     int touch_id,
513                     GestureEventConsumeDelegate* delegate) {
514  delegate->Reset();
515  ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(x, y),
516                          touch_id, GetTime());
517  root_window->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
518}
519
520}  // namespace
521
522typedef AuraTestBase GestureRecognizerTest;
523
524// Check that appropriate touch events generate tap gesture events.
525TEST_F(GestureRecognizerTest, GestureEventTap) {
526  scoped_ptr<GestureEventConsumeDelegate> delegate(
527      new GestureEventConsumeDelegate());
528  const int kWindowWidth = 123;
529  const int kWindowHeight = 45;
530  const int kTouchId = 2;
531  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
532  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
533      delegate.get(), -1234, bounds, NULL));
534
535  delegate->Reset();
536  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
537                           kTouchId, GetTime());
538  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
539  EXPECT_FALSE(delegate->tap());
540  EXPECT_TRUE(delegate->tap_down());
541  EXPECT_FALSE(delegate->tap_cancel());
542  EXPECT_TRUE(delegate->begin());
543  EXPECT_FALSE(delegate->double_tap());
544  EXPECT_FALSE(delegate->scroll_begin());
545  EXPECT_FALSE(delegate->scroll_update());
546  EXPECT_FALSE(delegate->scroll_end());
547  EXPECT_FALSE(delegate->long_press());
548
549  // Make sure there is enough delay before the touch is released so that it is
550  // recognized as a tap.
551  delegate->Reset();
552  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
553                             kTouchId, press.time_stamp() +
554                             base::TimeDelta::FromMilliseconds(50));
555
556  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
557  EXPECT_TRUE(delegate->tap());
558  EXPECT_FALSE(delegate->tap_down());
559  EXPECT_FALSE(delegate->tap_cancel());
560  EXPECT_FALSE(delegate->begin());
561  EXPECT_TRUE(delegate->end());
562  EXPECT_FALSE(delegate->double_tap());
563  EXPECT_FALSE(delegate->scroll_begin());
564  EXPECT_FALSE(delegate->scroll_update());
565  EXPECT_FALSE(delegate->scroll_end());
566
567  EXPECT_EQ(1, delegate->tap_count());
568}
569
570// Check that appropriate touch events generate tap gesture events
571// when information about the touch radii are provided.
572TEST_F(GestureRecognizerTest, GestureEventTapRegion) {
573  scoped_ptr<GestureEventConsumeDelegate> delegate(
574      new GestureEventConsumeDelegate());
575  const int kWindowWidth = 800;
576  const int kWindowHeight = 600;
577  const int kTouchId = 2;
578  gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight);
579  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
580      delegate.get(), -1234, bounds, NULL));
581
582  // Test with no ET_TOUCH_MOVED events.
583  {
584     delegate->Reset();
585     ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
586                              kTouchId, GetTime());
587     press.set_radius_x(5);
588     press.set_radius_y(12);
589     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
590     EXPECT_FALSE(delegate->tap());
591     EXPECT_TRUE(delegate->tap_down());
592     EXPECT_FALSE(delegate->tap_cancel());
593     EXPECT_TRUE(delegate->begin());
594     EXPECT_FALSE(delegate->double_tap());
595     EXPECT_FALSE(delegate->scroll_begin());
596     EXPECT_FALSE(delegate->scroll_update());
597     EXPECT_FALSE(delegate->scroll_end());
598     EXPECT_FALSE(delegate->long_press());
599
600     // Make sure there is enough delay before the touch is released so that it
601     // is recognized as a tap.
602     delegate->Reset();
603     ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
604                                kTouchId, press.time_stamp() +
605                                base::TimeDelta::FromMilliseconds(50));
606     release.set_radius_x(5);
607     release.set_radius_y(12);
608
609     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
610     EXPECT_TRUE(delegate->tap());
611     EXPECT_FALSE(delegate->tap_down());
612     EXPECT_FALSE(delegate->tap_cancel());
613     EXPECT_FALSE(delegate->begin());
614     EXPECT_TRUE(delegate->end());
615     EXPECT_FALSE(delegate->double_tap());
616     EXPECT_FALSE(delegate->scroll_begin());
617     EXPECT_FALSE(delegate->scroll_update());
618     EXPECT_FALSE(delegate->scroll_end());
619
620     EXPECT_EQ(1, delegate->tap_count());
621     gfx::Point actual_point(delegate->tap_location());
622     EXPECT_EQ(24, delegate->bounding_box().width());
623     EXPECT_EQ(24, delegate->bounding_box().height());
624     EXPECT_EQ(101, actual_point.x());
625     EXPECT_EQ(201, actual_point.y());
626  }
627
628  // Test with no ET_TOUCH_MOVED events but different touch points and radii.
629  {
630     delegate->Reset();
631     ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(365, 290),
632                              kTouchId, GetTime());
633     press.set_radius_x(8);
634     press.set_radius_y(14);
635     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
636     EXPECT_FALSE(delegate->tap());
637     EXPECT_TRUE(delegate->tap_down());
638     EXPECT_FALSE(delegate->tap_cancel());
639     EXPECT_TRUE(delegate->begin());
640     EXPECT_FALSE(delegate->double_tap());
641     EXPECT_FALSE(delegate->scroll_begin());
642     EXPECT_FALSE(delegate->scroll_update());
643     EXPECT_FALSE(delegate->scroll_end());
644     EXPECT_FALSE(delegate->long_press());
645
646     delegate->Reset();
647     ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(367, 291),
648                                kTouchId, press.time_stamp() +
649                                base::TimeDelta::FromMilliseconds(50));
650     release.set_radius_x(20);
651     release.set_radius_y(13);
652
653     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
654     EXPECT_TRUE(delegate->tap());
655     EXPECT_FALSE(delegate->tap_down());
656     EXPECT_FALSE(delegate->tap_cancel());
657     EXPECT_FALSE(delegate->begin());
658     EXPECT_TRUE(delegate->end());
659     EXPECT_FALSE(delegate->double_tap());
660     EXPECT_FALSE(delegate->scroll_begin());
661     EXPECT_FALSE(delegate->scroll_update());
662     EXPECT_FALSE(delegate->scroll_end());
663
664     EXPECT_EQ(1, delegate->tap_count());
665     gfx::Point actual_point(delegate->tap_location());
666     EXPECT_EQ(40, delegate->bounding_box().width());
667     EXPECT_EQ(40, delegate->bounding_box().height());
668     EXPECT_EQ(367, actual_point.x());
669     EXPECT_EQ(291, actual_point.y());
670  }
671
672  // Test with a single ET_TOUCH_MOVED event.
673  {
674     delegate->Reset();
675     ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(46, 205),
676                              kTouchId, GetTime());
677     press.set_radius_x(6);
678     press.set_radius_y(10);
679     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
680     EXPECT_FALSE(delegate->tap());
681     EXPECT_TRUE(delegate->tap_down());
682     EXPECT_FALSE(delegate->tap_cancel());
683     EXPECT_TRUE(delegate->begin());
684     EXPECT_FALSE(delegate->tap_cancel());
685     EXPECT_FALSE(delegate->double_tap());
686     EXPECT_FALSE(delegate->scroll_begin());
687     EXPECT_FALSE(delegate->scroll_update());
688     EXPECT_FALSE(delegate->scroll_end());
689     EXPECT_FALSE(delegate->long_press());
690
691     delegate->Reset();
692     ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(49, 204),
693                             kTouchId, press.time_stamp() +
694                             base::TimeDelta::FromMilliseconds(50));
695     move.set_radius_x(8);
696     move.set_radius_y(12);
697     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
698     EXPECT_FALSE(delegate->tap());
699     EXPECT_FALSE(delegate->tap_down());
700     EXPECT_FALSE(delegate->tap_cancel());
701     EXPECT_FALSE(delegate->begin());
702     EXPECT_FALSE(delegate->double_tap());
703     EXPECT_FALSE(delegate->scroll_begin());
704     EXPECT_FALSE(delegate->scroll_update());
705     EXPECT_FALSE(delegate->scroll_end());
706     EXPECT_FALSE(delegate->long_press());
707
708     delegate->Reset();
709     ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(49, 204),
710                                kTouchId, press.time_stamp() +
711                                base::TimeDelta::FromMilliseconds(50));
712     release.set_radius_x(4);
713     release.set_radius_y(8);
714
715     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
716     EXPECT_TRUE(delegate->tap());
717     EXPECT_FALSE(delegate->tap_down());
718     EXPECT_FALSE(delegate->tap_cancel());
719     EXPECT_FALSE(delegate->begin());
720     EXPECT_TRUE(delegate->end());
721     EXPECT_FALSE(delegate->double_tap());
722     EXPECT_FALSE(delegate->scroll_begin());
723     EXPECT_FALSE(delegate->scroll_update());
724     EXPECT_FALSE(delegate->scroll_end());
725
726     EXPECT_EQ(1, delegate->tap_count());
727     gfx::Point actual_point(delegate->tap_location());
728     EXPECT_EQ(25, delegate->bounding_box().width());
729     EXPECT_EQ(24, delegate->bounding_box().height());
730     EXPECT_EQ(48, actual_point.x());
731     EXPECT_EQ(204, actual_point.y());
732  }
733
734  // Test with a few ET_TOUCH_MOVED events.
735  {
736     delegate->Reset();
737     ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(400, 150),
738                              kTouchId, GetTime());
739     press.set_radius_x(7);
740     press.set_radius_y(10);
741     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
742     EXPECT_FALSE(delegate->tap());
743     EXPECT_TRUE(delegate->tap_down());
744     EXPECT_FALSE(delegate->tap_cancel());
745     EXPECT_TRUE(delegate->begin());
746     EXPECT_FALSE(delegate->double_tap());
747     EXPECT_FALSE(delegate->scroll_begin());
748     EXPECT_FALSE(delegate->scroll_update());
749     EXPECT_FALSE(delegate->scroll_end());
750     EXPECT_FALSE(delegate->long_press());
751
752     delegate->Reset();
753     ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(397, 151),
754                             kTouchId, press.time_stamp() +
755                             base::TimeDelta::FromMilliseconds(50));
756     move.set_radius_x(13);
757     move.set_radius_y(12);
758     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
759     EXPECT_FALSE(delegate->tap());
760     EXPECT_FALSE(delegate->tap_down());
761     EXPECT_FALSE(delegate->tap_cancel());
762     EXPECT_FALSE(delegate->begin());
763     EXPECT_FALSE(delegate->double_tap());
764     EXPECT_FALSE(delegate->scroll_begin());
765     EXPECT_FALSE(delegate->scroll_update());
766     EXPECT_FALSE(delegate->scroll_end());
767     EXPECT_FALSE(delegate->long_press());
768
769     delegate->Reset();
770     ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(397, 149),
771                              kTouchId, move.time_stamp() +
772                              base::TimeDelta::FromMilliseconds(50));
773     move1.set_radius_x(16);
774     move1.set_radius_y(16);
775     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
776     EXPECT_FALSE(delegate->tap());
777     EXPECT_FALSE(delegate->tap_down());
778     EXPECT_FALSE(delegate->tap_cancel());
779     EXPECT_FALSE(delegate->begin());
780     EXPECT_FALSE(delegate->double_tap());
781     EXPECT_FALSE(delegate->scroll_begin());
782     EXPECT_FALSE(delegate->scroll_update());
783     EXPECT_FALSE(delegate->scroll_end());
784     EXPECT_FALSE(delegate->long_press());
785
786     delegate->Reset();
787     ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(400, 150),
788                              kTouchId, move1.time_stamp() +
789                              base::TimeDelta::FromMilliseconds(50));
790     move2.set_radius_x(14);
791     move2.set_radius_y(10);
792     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
793     EXPECT_FALSE(delegate->tap());
794     EXPECT_FALSE(delegate->tap_down());
795     EXPECT_FALSE(delegate->tap_cancel());
796     EXPECT_FALSE(delegate->begin());
797     EXPECT_FALSE(delegate->double_tap());
798     EXPECT_FALSE(delegate->scroll_begin());
799     EXPECT_FALSE(delegate->scroll_update());
800     EXPECT_FALSE(delegate->scroll_end());
801     EXPECT_FALSE(delegate->long_press());
802
803     delegate->Reset();
804     ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(401, 149),
805                                kTouchId, press.time_stamp() +
806                                base::TimeDelta::FromMilliseconds(50));
807     release.set_radius_x(8);
808     release.set_radius_y(9);
809
810     root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
811     EXPECT_TRUE(delegate->tap());
812     EXPECT_FALSE(delegate->tap_down());
813     EXPECT_FALSE(delegate->tap_cancel());
814     EXPECT_FALSE(delegate->begin());
815     EXPECT_TRUE(delegate->end());
816     EXPECT_FALSE(delegate->double_tap());
817     EXPECT_FALSE(delegate->scroll_begin());
818     EXPECT_FALSE(delegate->scroll_update());
819     EXPECT_FALSE(delegate->scroll_end());
820
821     EXPECT_EQ(1, delegate->tap_count());
822     gfx::Point actual_point(delegate->tap_location());
823     EXPECT_EQ(33, delegate->bounding_box().width());
824     EXPECT_EQ(32, delegate->bounding_box().height());
825     EXPECT_EQ(397, actual_point.x());
826     EXPECT_EQ(149, actual_point.y());
827  }
828}
829
830// Check that appropriate touch events generate scroll gesture events.
831TEST_F(GestureRecognizerTest, GestureEventScroll) {
832  scoped_ptr<GestureEventConsumeDelegate> delegate(
833      new GestureEventConsumeDelegate());
834  const int kWindowWidth = 123;
835  const int kWindowHeight = 45;
836  const int kTouchId = 5;
837  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
838  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
839      delegate.get(), -1234, bounds, NULL));
840
841  delegate->Reset();
842  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
843                           kTouchId, GetTime());
844  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
845  EXPECT_2_EVENTS(delegate->events(),
846                  ui::ET_GESTURE_BEGIN,
847                  ui::ET_GESTURE_TAP_DOWN);
848
849  // Move the touch-point enough so that it is considered as a scroll. This
850  // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
851  // The first movement is diagonal, to ensure that we have a free scroll,
852  // and not a rail scroll.
853  SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
854  EXPECT_3_EVENTS(delegate->events(),
855                  ui::ET_GESTURE_TAP_CANCEL,
856                  ui::ET_GESTURE_SCROLL_BEGIN,
857                  ui::ET_GESTURE_SCROLL_UPDATE);
858  EXPECT_EQ(29, delegate->scroll_x());
859  EXPECT_EQ(29, delegate->scroll_y());
860  EXPECT_EQ(0, delegate->scroll_velocity_x());
861  EXPECT_EQ(0, delegate->scroll_velocity_y());
862  EXPECT_EQ(gfx::Point(1, 1).ToString(),
863            delegate->scroll_begin_position().ToString());
864
865  // When scrolling with a single finger, the bounding box of the gesture should
866  // be empty, since it's a single point and the radius for testing is zero.
867  EXPECT_TRUE(delegate->bounding_box().IsEmpty());
868
869  // Move some more to generate a few more scroll updates.
870  SendScrollEvent(root_window(), 110, 211, kTouchId, delegate.get());
871  EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
872  EXPECT_EQ(-20, delegate->scroll_x());
873  EXPECT_EQ(-19, delegate->scroll_y());
874  EXPECT_TRUE(delegate->bounding_box().IsEmpty());
875
876  SendScrollEvent(root_window(), 140, 215, kTouchId, delegate.get());
877  EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
878  EXPECT_EQ(30, delegate->scroll_x());
879  EXPECT_EQ(4, delegate->scroll_y());
880  EXPECT_TRUE(delegate->bounding_box().IsEmpty());
881
882  // Release the touch. This should end the scroll.
883  delegate->Reset();
884  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
885                             kTouchId, press.time_stamp() +
886                             base::TimeDelta::FromMilliseconds(50));
887  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
888  EXPECT_2_EVENTS(delegate->events(),
889                  ui::ET_GESTURE_SCROLL_END,
890                  ui::ET_GESTURE_END);
891  EXPECT_TRUE(delegate->bounding_box().IsEmpty());
892}
893
894// Check that the bounding box during a scroll event is correct.
895TEST_F(GestureRecognizerTest, GestureEventScrollBoundingBox) {
896  for (int radius = 1; radius <= 10; ++radius) {
897    ui::GestureConfiguration::set_default_radius(radius);
898    scoped_ptr<GestureEventConsumeDelegate> delegate(
899        new GestureEventConsumeDelegate());
900    const int kWindowWidth = 123;
901    const int kWindowHeight = 45;
902    const int kTouchId = 5;
903    gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
904    scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
905        delegate.get(), -1234, bounds, NULL));
906
907    const int kPositionX = 101;
908    const int kPositionY = 201;
909    delegate->Reset();
910    ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
911                             gfx::Point(kPositionX, kPositionY),
912                             kTouchId,
913                             GetTime());
914    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
915    EXPECT_EQ(gfx::Rect(kPositionX - radius,
916                        kPositionY - radius,
917                        radius * 2,
918                        radius * 2).ToString(),
919              delegate->bounding_box().ToString());
920
921    const int kScrollAmount = 50;
922    SendScrollEvents(root_window(), kPositionX, kPositionY, GetTime(),
923        1, 1, kTouchId, 1, kScrollAmount, delegate.get());
924    EXPECT_EQ(gfx::Point(1, 1).ToString(),
925              delegate->scroll_begin_position().ToString());
926    EXPECT_EQ(gfx::Rect(kPositionX + kScrollAmount - radius,
927                        kPositionY + kScrollAmount - radius,
928                        radius * 2,
929                        radius * 2).ToString(),
930              delegate->bounding_box().ToString());
931
932    // Release the touch. This should end the scroll.
933    delegate->Reset();
934    ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
935                               gfx::Point(kPositionX, kPositionY),
936                               kTouchId, press.time_stamp() +
937                               base::TimeDelta::FromMilliseconds(50));
938    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
939    EXPECT_EQ(gfx::Rect(kPositionX - radius,
940                        kPositionY - radius,
941                        radius * 2,
942                        radius * 2).ToString(),
943              delegate->bounding_box().ToString());
944  }
945  ui::GestureConfiguration::set_default_radius(0);
946}
947
948// Check Scroll End Events report correct velocities
949// if the user was on a horizontal rail
950TEST_F(GestureRecognizerTest, GestureEventHorizontalRailFling) {
951  scoped_ptr<GestureEventConsumeDelegate> delegate(
952      new GestureEventConsumeDelegate());
953  const int kTouchId = 7;
954  gfx::Rect bounds(0, 0, 1000, 1000);
955  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
956      delegate.get(), -1234, bounds, NULL));
957
958  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
959                           kTouchId, GetTime());
960  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
961
962  // Move the touch-point horizontally enough that it is considered a
963  // horizontal scroll.
964  SendScrollEvent(root_window(), 20, 1, kTouchId, delegate.get());
965  EXPECT_EQ(0, delegate->scroll_y());
966  EXPECT_EQ(20, delegate->scroll_x());
967
968  // Get a high x velocity, while still staying on the rail
969  SendScrollEvents(root_window(), 1, 1, press.time_stamp(),
970                   100, 10, kTouchId, 1,
971                   ui::GestureConfiguration::points_buffered_for_velocity(),
972                   delegate.get());
973  // The y-velocity during the scroll should be 0 since this is in a horizontal
974  // rail scroll.
975  EXPECT_GT(delegate->scroll_velocity_x(), 0);
976  EXPECT_EQ(0, delegate->scroll_velocity_y());
977
978  delegate->Reset();
979  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
980                             kTouchId, GetTime());
981  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
982
983  EXPECT_TRUE(delegate->fling());
984  EXPECT_FALSE(delegate->scroll_end());
985  EXPECT_GT(delegate->velocity_x(), 0);
986  EXPECT_EQ(0, delegate->velocity_y());
987}
988
989// Check Scroll End Events report correct velocities
990// if the user was on a vertical rail
991TEST_F(GestureRecognizerTest, GestureEventVerticalRailFling) {
992  scoped_ptr<GestureEventConsumeDelegate> delegate(
993      new GestureEventConsumeDelegate());
994  const int kTouchId = 7;
995  gfx::Rect bounds(0, 0, 1000, 1000);
996  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
997      delegate.get(), -1234, bounds, NULL));
998
999  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1000                           kTouchId, GetTime());
1001  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1002
1003  // Move the touch-point vertically enough that it is considered a
1004  // vertical scroll.
1005  SendScrollEvent(root_window(), 1, 20, kTouchId, delegate.get());
1006  EXPECT_EQ(20, delegate->scroll_y());
1007  EXPECT_EQ(0, delegate->scroll_x());
1008  EXPECT_EQ(0, delegate->scroll_velocity_x());
1009  EXPECT_EQ(0, delegate->scroll_velocity_y());
1010
1011  // Get a high y velocity, while still staying on the rail
1012  SendScrollEvents(root_window(), 1, 1, press.time_stamp(),
1013                   10, 100, kTouchId, 1,
1014                   ui::GestureConfiguration::points_buffered_for_velocity(),
1015                   delegate.get());
1016  EXPECT_EQ(0, delegate->scroll_velocity_x());
1017  EXPECT_GT(delegate->scroll_velocity_y(), 0);
1018
1019  delegate->Reset();
1020  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1021                             kTouchId, GetTime());
1022  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1023
1024  EXPECT_TRUE(delegate->fling());
1025  EXPECT_FALSE(delegate->scroll_end());
1026  EXPECT_EQ(0, delegate->velocity_x());
1027  EXPECT_GT(delegate->velocity_y(), 0);
1028}
1029
1030// Check Scroll End Events reports zero velocities
1031// if the user is not on a rail
1032TEST_F(GestureRecognizerTest, GestureEventNonRailFling) {
1033  scoped_ptr<GestureEventConsumeDelegate> delegate(
1034      new GestureEventConsumeDelegate());
1035  const int kTouchId = 7;
1036  gfx::Rect bounds(0, 0, 1000, 1000);
1037  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1038      delegate.get(), -1234, bounds, NULL));
1039
1040  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1041                           kTouchId, GetTime());
1042  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1043
1044  // Move the touch-point such that a non-rail scroll begins
1045  SendScrollEvent(root_window(), 20, 20, kTouchId, delegate.get());
1046  EXPECT_EQ(20, delegate->scroll_y());
1047  EXPECT_EQ(20, delegate->scroll_x());
1048
1049  SendScrollEvents(root_window(), 1, 1, press.time_stamp(),
1050                   10, 100, kTouchId, 1,
1051                   ui::GestureConfiguration::points_buffered_for_velocity(),
1052                   delegate.get());
1053
1054  delegate->Reset();
1055  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1056                             kTouchId, GetTime());
1057  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1058
1059  EXPECT_TRUE(delegate->fling());
1060  EXPECT_FALSE(delegate->scroll_end());
1061  EXPECT_GT(delegate->velocity_x(), 0);
1062  EXPECT_GT(delegate->velocity_y(), 0);
1063}
1064
1065// Check that appropriate touch events generate long press events
1066TEST_F(GestureRecognizerTest, GestureEventLongPress) {
1067  scoped_ptr<GestureEventConsumeDelegate> delegate(
1068      new GestureEventConsumeDelegate());
1069  const int kWindowWidth = 123;
1070  const int kWindowHeight = 45;
1071  const int kTouchId = 2;
1072  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1073  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1074      delegate.get(), -1234, bounds, NULL));
1075
1076  delegate->Reset();
1077
1078  TimerTestGestureRecognizer* gesture_recognizer =
1079      new TimerTestGestureRecognizer(root_window());
1080
1081  root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1082
1083  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1084                            kTouchId, GetTime());
1085  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1086  EXPECT_TRUE(delegate->tap_down());
1087  EXPECT_TRUE(delegate->begin());
1088  EXPECT_FALSE(delegate->tap_cancel());
1089
1090  // We haven't pressed long enough for a long press to occur
1091  EXPECT_FALSE(delegate->long_press());
1092
1093  // Wait until the timer runs out
1094  delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1095  EXPECT_TRUE(delegate->long_press());
1096  EXPECT_EQ(0, delegate->touch_id());
1097  EXPECT_FALSE(delegate->tap_cancel());
1098
1099  delegate->Reset();
1100  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1101                              kTouchId, GetTime());
1102  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1103  EXPECT_FALSE(delegate->long_press());
1104
1105  // Note the tap down isn't cancelled until the release
1106  EXPECT_TRUE(delegate->tap_cancel());
1107}
1108
1109// Check that scrolling cancels a long press
1110TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledByScroll) {
1111  scoped_ptr<GestureEventConsumeDelegate> delegate(
1112      new GestureEventConsumeDelegate());
1113  const int kWindowWidth = 123;
1114  const int kWindowHeight = 45;
1115  const int kTouchId = 6;
1116  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1117  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1118      delegate.get(), -1234, bounds, NULL));
1119
1120  delegate->Reset();
1121
1122  TimerTestGestureRecognizer* gesture_recognizer =
1123      new TimerTestGestureRecognizer(root_window());
1124  TimerTestGestureSequence* gesture_sequence =
1125      static_cast<TimerTestGestureSequence*>(
1126          gesture_recognizer->GetGestureSequenceForTesting(window.get()));
1127
1128  root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1129
1130  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1131                            kTouchId, GetTime());
1132  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1133  EXPECT_TRUE(delegate->tap_down());
1134
1135  // We haven't pressed long enough for a long press to occur
1136  EXPECT_FALSE(delegate->long_press());
1137  EXPECT_FALSE(delegate->tap_cancel());
1138
1139  // Scroll around, to cancel the long press
1140  SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
1141  // Wait until the timer runs out
1142  gesture_sequence->ForceTimeout();
1143  EXPECT_FALSE(delegate->long_press());
1144  EXPECT_TRUE(delegate->tap_cancel());
1145
1146  delegate->Reset();
1147  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1148                              kTouchId, GetTime());
1149  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1150  EXPECT_FALSE(delegate->long_press());
1151  EXPECT_FALSE(delegate->tap_cancel());
1152}
1153
1154// Check that appropriate touch events generate long tap events
1155TEST_F(GestureRecognizerTest, GestureEventLongTap) {
1156  scoped_ptr<GestureEventConsumeDelegate> delegate(
1157      new GestureEventConsumeDelegate());
1158  const int kWindowWidth = 123;
1159  const int kWindowHeight = 45;
1160  const int kTouchId = 2;
1161  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1162  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1163      delegate.get(), -1234, bounds, NULL));
1164
1165  delegate->Reset();
1166
1167  TimerTestGestureRecognizer* gesture_recognizer =
1168      new TimerTestGestureRecognizer(root_window());
1169
1170  root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1171
1172  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1173                            kTouchId, GetTime());
1174  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1175  EXPECT_TRUE(delegate->tap_down());
1176  EXPECT_TRUE(delegate->begin());
1177  EXPECT_FALSE(delegate->tap_cancel());
1178
1179  // We haven't pressed long enough for a long press to occur
1180  EXPECT_FALSE(delegate->long_press());
1181
1182  // Wait until the timer runs out
1183  delegate->WaitUntilReceivedGesture(ui::ET_GESTURE_LONG_PRESS);
1184  EXPECT_TRUE(delegate->long_press());
1185  EXPECT_EQ(0, delegate->touch_id());
1186  EXPECT_FALSE(delegate->tap_cancel());
1187
1188  delegate->Reset();
1189  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1190                              kTouchId, GetTime());
1191  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1192  EXPECT_FALSE(delegate->long_press());
1193  EXPECT_TRUE(delegate->long_tap());
1194
1195  // Note the tap down isn't cancelled until the release
1196  EXPECT_TRUE(delegate->tap_cancel());
1197}
1198
1199// Check that second tap cancels a long press
1200TEST_F(GestureRecognizerTest, GestureEventLongPressCancelledBySecondTap) {
1201  scoped_ptr<GestureEventConsumeDelegate> delegate(
1202      new GestureEventConsumeDelegate());
1203  const int kWindowWidth = 300;
1204  const int kWindowHeight = 400;
1205  const int kTouchId1 = 8;
1206  const int kTouchId2 = 2;
1207  gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1208  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1209      delegate.get(), -1234, bounds, NULL));
1210
1211  TimerTestGestureRecognizer* gesture_recognizer =
1212      new TimerTestGestureRecognizer(root_window());
1213  TimerTestGestureSequence* gesture_sequence =
1214      static_cast<TimerTestGestureSequence*>(
1215          gesture_recognizer->GetGestureSequenceForTesting(window.get()));
1216
1217  root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1218
1219  delegate->Reset();
1220  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1221                           kTouchId1, GetTime());
1222  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1223  EXPECT_TRUE(delegate->tap_down());
1224  EXPECT_TRUE(delegate->begin());
1225
1226  // We haven't pressed long enough for a long press to occur
1227  EXPECT_FALSE(delegate->long_press());
1228
1229  // Second tap, to cancel the long press
1230  delegate->Reset();
1231  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1232                            kTouchId2, GetTime());
1233  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1234  EXPECT_FALSE(delegate->tap_down());  // no touch down for second tap.
1235  EXPECT_TRUE(delegate->tap_cancel());
1236  EXPECT_TRUE(delegate->begin());
1237
1238  // Wait until the timer runs out
1239  gesture_sequence->ForceTimeout();
1240
1241  // No long press occurred
1242  EXPECT_FALSE(delegate->long_press());
1243
1244  delegate->Reset();
1245  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1246                              kTouchId1, GetTime());
1247  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1248  EXPECT_FALSE(delegate->long_press());
1249  EXPECT_TRUE(delegate->two_finger_tap());
1250  EXPECT_FALSE(delegate->tap_cancel());
1251}
1252
1253// Check that horizontal scroll gestures cause scrolls on horizontal rails.
1254// Also tests that horizontal rails can be broken.
1255TEST_F(GestureRecognizerTest, GestureEventHorizontalRailScroll) {
1256  scoped_ptr<GestureEventConsumeDelegate> delegate(
1257      new GestureEventConsumeDelegate());
1258  const int kTouchId = 7;
1259  gfx::Rect bounds(0, 0, 1000, 1000);
1260  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1261      delegate.get(), -1234, bounds, NULL));
1262
1263  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1264                           kTouchId, GetTime());
1265  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1266
1267  // Move the touch-point horizontally enough that it is considered a
1268  // horizontal scroll.
1269  SendScrollEvent(root_window(), 20, 1, kTouchId, delegate.get());
1270  EXPECT_EQ(0, delegate->scroll_y());
1271  EXPECT_EQ(20, delegate->scroll_x());
1272
1273  SendScrollEvent(root_window(), 25, 6, kTouchId, delegate.get());
1274  EXPECT_TRUE(delegate->scroll_update());
1275  EXPECT_EQ(5, delegate->scroll_x());
1276  // y shouldn't change, as we're on a horizontal rail.
1277  EXPECT_EQ(0, delegate->scroll_y());
1278  EXPECT_EQ(0, delegate->scroll_velocity_x());
1279  EXPECT_EQ(0, delegate->scroll_velocity_y());
1280
1281  // Send enough information that a velocity can be calculated for the gesture,
1282  // and we can break the rail
1283  SendScrollEvents(root_window(), 1, 1, press.time_stamp(),
1284                   1, 100, kTouchId, 1,
1285                   ui::GestureConfiguration::points_buffered_for_velocity(),
1286                   delegate.get());
1287  // Since the scroll is not longer railing, the velocity should be set for both
1288  // axis.
1289  EXPECT_GT(delegate->scroll_velocity_x(), 0);
1290  EXPECT_GT(delegate->scroll_velocity_y(), 0);
1291
1292  SendScrollEvent(root_window(), 0, 0, kTouchId, delegate.get());
1293  SendScrollEvent(root_window(), 5, 5, kTouchId, delegate.get());
1294
1295  // The rail should be broken
1296  EXPECT_TRUE(delegate->scroll_update());
1297  EXPECT_EQ(5, delegate->scroll_x());
1298  EXPECT_EQ(5, delegate->scroll_y());
1299}
1300
1301// Check that vertical scroll gestures cause scrolls on vertical rails.
1302// Also tests that vertical rails can be broken.
1303TEST_F(GestureRecognizerTest, GestureEventVerticalRailScroll) {
1304  scoped_ptr<GestureEventConsumeDelegate> delegate(
1305      new GestureEventConsumeDelegate());
1306  const int kTouchId = 7;
1307  gfx::Rect bounds(0, 0, 1000, 1000);
1308  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1309      delegate.get(), -1234, bounds, NULL));
1310
1311  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
1312                           kTouchId, GetTime());
1313  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1314
1315  // Move the touch-point vertically enough that it is considered a
1316  // vertical scroll.
1317  SendScrollEvent(root_window(), 1, 20, kTouchId, delegate.get());
1318  EXPECT_EQ(0, delegate->scroll_x());
1319  EXPECT_EQ(20, delegate->scroll_y());
1320
1321  SendScrollEvent(root_window(), 6, 25, kTouchId, delegate.get());
1322  EXPECT_TRUE(delegate->scroll_update());
1323  EXPECT_EQ(5, delegate->scroll_y());
1324  // x shouldn't change, as we're on a vertical rail.
1325  EXPECT_EQ(0, delegate->scroll_x());
1326  EXPECT_EQ(0, delegate->scroll_velocity_x());
1327  EXPECT_EQ(0, delegate->scroll_velocity_y());
1328
1329  // Send enough information that a velocity can be calculated for the gesture,
1330  // and we can break the rail
1331  SendScrollEvents(root_window(), 1, 1, press.time_stamp(),
1332                   100, 1, kTouchId, 1,
1333                   ui::GestureConfiguration::points_buffered_for_velocity(),
1334                   delegate.get());
1335  EXPECT_GT(delegate->scroll_velocity_x(), 0);
1336  EXPECT_GT(delegate->scroll_velocity_y(), 0);
1337
1338  SendScrollEvent(root_window(), 0, 0, kTouchId, delegate.get());
1339  SendScrollEvent(root_window(), 5, 5, kTouchId, delegate.get());
1340
1341  // The rail should be broken
1342  EXPECT_TRUE(delegate->scroll_update());
1343  EXPECT_EQ(5, delegate->scroll_x());
1344  EXPECT_EQ(5, delegate->scroll_y());
1345}
1346
1347TEST_F(GestureRecognizerTest, GestureTapFollowedByScroll) {
1348  // First, tap. Then, do a scroll using the same touch-id.
1349  scoped_ptr<GestureEventConsumeDelegate> delegate(
1350      new GestureEventConsumeDelegate());
1351  const int kWindowWidth = 123;
1352  const int kWindowHeight = 45;
1353  const int kTouchId = 3;
1354  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1355  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1356      delegate.get(), -1234, bounds, NULL));
1357
1358  delegate->Reset();
1359  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1360                           kTouchId, GetTime());
1361  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1362  EXPECT_FALSE(delegate->tap());
1363  EXPECT_TRUE(delegate->tap_down());
1364  EXPECT_FALSE(delegate->tap_cancel());
1365  EXPECT_FALSE(delegate->double_tap());
1366  EXPECT_FALSE(delegate->scroll_begin());
1367  EXPECT_FALSE(delegate->scroll_update());
1368  EXPECT_FALSE(delegate->scroll_end());
1369
1370  // Make sure there is enough delay before the touch is released so that it is
1371  // recognized as a tap.
1372  delegate->Reset();
1373  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1374                             kTouchId, press.time_stamp() +
1375                             base::TimeDelta::FromMilliseconds(50));
1376  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1377  EXPECT_TRUE(delegate->tap());
1378  EXPECT_FALSE(delegate->tap_down());
1379  EXPECT_FALSE(delegate->tap_cancel());
1380  EXPECT_FALSE(delegate->double_tap());
1381  EXPECT_FALSE(delegate->scroll_begin());
1382  EXPECT_FALSE(delegate->scroll_update());
1383  EXPECT_FALSE(delegate->scroll_end());
1384
1385  // Now, do a scroll gesture. Delay it sufficiently so that it doesn't trigger
1386  // a double-tap.
1387  delegate->Reset();
1388  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1389                            kTouchId, release.time_stamp() +
1390                    base::TimeDelta::FromMilliseconds(1000));
1391  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1392  EXPECT_FALSE(delegate->tap());
1393  EXPECT_TRUE(delegate->tap_down());
1394  EXPECT_FALSE(delegate->tap_cancel());
1395  EXPECT_FALSE(delegate->double_tap());
1396  EXPECT_FALSE(delegate->scroll_begin());
1397  EXPECT_FALSE(delegate->scroll_update());
1398  EXPECT_FALSE(delegate->scroll_end());
1399
1400  // Move the touch-point enough so that it is considered as a scroll. This
1401  // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1402  // The first movement is diagonal, to ensure that we have a free scroll,
1403  // and not a rail scroll.
1404  delegate->Reset();
1405  ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 230),
1406                          kTouchId, GetTime());
1407  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
1408  EXPECT_FALSE(delegate->tap());
1409  EXPECT_FALSE(delegate->tap_down());
1410  EXPECT_TRUE(delegate->tap_cancel());
1411  EXPECT_FALSE(delegate->double_tap());
1412  EXPECT_TRUE(delegate->scroll_begin());
1413  EXPECT_TRUE(delegate->scroll_update());
1414  EXPECT_FALSE(delegate->scroll_end());
1415  EXPECT_EQ(29, delegate->scroll_x());
1416  EXPECT_EQ(29, delegate->scroll_y());
1417
1418  // Move some more to generate a few more scroll updates.
1419  delegate->Reset();
1420  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(110, 211),
1421                           kTouchId, GetTime());
1422  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
1423  EXPECT_FALSE(delegate->tap());
1424  EXPECT_FALSE(delegate->tap_down());
1425  EXPECT_FALSE(delegate->tap_cancel());
1426  EXPECT_FALSE(delegate->double_tap());
1427  EXPECT_FALSE(delegate->scroll_begin());
1428  EXPECT_TRUE(delegate->scroll_update());
1429  EXPECT_FALSE(delegate->scroll_end());
1430  EXPECT_EQ(-20, delegate->scroll_x());
1431  EXPECT_EQ(-19, delegate->scroll_y());
1432
1433  delegate->Reset();
1434  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(140, 215),
1435                           kTouchId, GetTime());
1436  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
1437  EXPECT_FALSE(delegate->tap());
1438  EXPECT_FALSE(delegate->tap_down());
1439  EXPECT_FALSE(delegate->tap_cancel());
1440  EXPECT_FALSE(delegate->double_tap());
1441  EXPECT_FALSE(delegate->scroll_begin());
1442  EXPECT_TRUE(delegate->scroll_update());
1443  EXPECT_FALSE(delegate->scroll_end());
1444  EXPECT_EQ(30, delegate->scroll_x());
1445  EXPECT_EQ(4, delegate->scroll_y());
1446
1447  // Release the touch. This should end the scroll.
1448  delegate->Reset();
1449  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1450                              kTouchId, GetTime());
1451  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1452  EXPECT_FALSE(delegate->tap());
1453  EXPECT_FALSE(delegate->tap_down());
1454  EXPECT_FALSE(delegate->tap_cancel());
1455  EXPECT_FALSE(delegate->double_tap());
1456  EXPECT_FALSE(delegate->scroll_begin());
1457  EXPECT_FALSE(delegate->scroll_update());
1458  EXPECT_TRUE(delegate->scroll_end());
1459}
1460
1461TEST_F(GestureRecognizerTest, AsynchronousGestureRecognition) {
1462  scoped_ptr<QueueTouchEventDelegate> queued_delegate(
1463      new QueueTouchEventDelegate(root_window()));
1464  const int kWindowWidth = 123;
1465  const int kWindowHeight = 45;
1466  const int kTouchId1 = 6;
1467  const int kTouchId2 = 4;
1468  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
1469  scoped_ptr<aura::Window> queue(CreateTestWindowWithDelegate(
1470      queued_delegate.get(), -1234, bounds, NULL));
1471
1472  queued_delegate->set_window(queue.get());
1473
1474  // Touch down on the window. This should not generate any gesture event.
1475  queued_delegate->Reset();
1476  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1477                           kTouchId1, GetTime());
1478  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1479  EXPECT_FALSE(queued_delegate->tap());
1480  EXPECT_FALSE(queued_delegate->tap_down());
1481  EXPECT_FALSE(queued_delegate->tap_cancel());
1482  EXPECT_FALSE(queued_delegate->begin());
1483  EXPECT_FALSE(queued_delegate->double_tap());
1484  EXPECT_FALSE(queued_delegate->scroll_begin());
1485  EXPECT_FALSE(queued_delegate->scroll_update());
1486  EXPECT_FALSE(queued_delegate->scroll_end());
1487
1488  // Introduce some delay before the touch is released so that it is recognized
1489  // as a tap. However, this still should not create any gesture events.
1490  queued_delegate->Reset();
1491  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1492                             kTouchId1, press.time_stamp() +
1493                                 base::TimeDelta::FromMilliseconds(50));
1494  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1495  EXPECT_FALSE(queued_delegate->tap());
1496  EXPECT_FALSE(queued_delegate->tap_down());
1497  EXPECT_FALSE(queued_delegate->tap_cancel());
1498  EXPECT_FALSE(queued_delegate->begin());
1499  EXPECT_FALSE(queued_delegate->end());
1500  EXPECT_FALSE(queued_delegate->double_tap());
1501  EXPECT_FALSE(queued_delegate->scroll_begin());
1502  EXPECT_FALSE(queued_delegate->scroll_update());
1503  EXPECT_FALSE(queued_delegate->scroll_end());
1504
1505  // Create another window, and place a touch-down on it. This should create a
1506  // tap-down gesture.
1507  scoped_ptr<GestureEventConsumeDelegate> delegate(
1508      new GestureEventConsumeDelegate());
1509  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1510      delegate.get(), -2345, gfx::Rect(0, 0, 50, 50), NULL));
1511  delegate->Reset();
1512  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 20),
1513                            kTouchId2, GetTime());
1514  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1515  EXPECT_FALSE(delegate->tap());
1516  EXPECT_TRUE(delegate->tap_down());
1517  EXPECT_FALSE(delegate->tap_cancel());
1518  EXPECT_FALSE(queued_delegate->begin());
1519  EXPECT_FALSE(queued_delegate->end());
1520  EXPECT_FALSE(delegate->double_tap());
1521  EXPECT_FALSE(delegate->scroll_begin());
1522  EXPECT_FALSE(delegate->scroll_update());
1523  EXPECT_FALSE(delegate->scroll_end());
1524
1525  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(10, 20),
1526                              kTouchId2, GetTime());
1527  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
1528
1529  // Process the first queued event.
1530  queued_delegate->Reset();
1531  queued_delegate->ReceivedAck();
1532  EXPECT_FALSE(queued_delegate->tap());
1533  EXPECT_TRUE(queued_delegate->tap_down());
1534  EXPECT_TRUE(queued_delegate->begin());
1535  EXPECT_FALSE(queued_delegate->tap_cancel());
1536  EXPECT_FALSE(queued_delegate->end());
1537  EXPECT_FALSE(queued_delegate->double_tap());
1538  EXPECT_FALSE(queued_delegate->scroll_begin());
1539  EXPECT_FALSE(queued_delegate->scroll_update());
1540  EXPECT_FALSE(queued_delegate->scroll_end());
1541
1542  // Now, process the second queued event.
1543  queued_delegate->Reset();
1544  queued_delegate->ReceivedAck();
1545  EXPECT_TRUE(queued_delegate->tap());
1546  EXPECT_FALSE(queued_delegate->tap_down());
1547  EXPECT_FALSE(queued_delegate->tap_cancel());
1548  EXPECT_FALSE(queued_delegate->begin());
1549  EXPECT_TRUE(queued_delegate->end());
1550  EXPECT_FALSE(queued_delegate->double_tap());
1551  EXPECT_FALSE(queued_delegate->scroll_begin());
1552  EXPECT_FALSE(queued_delegate->scroll_update());
1553  EXPECT_FALSE(queued_delegate->scroll_end());
1554
1555  // Start all over. Press on the first window, then press again on the second
1556  // window. The second press should still go to the first window.
1557  queued_delegate->Reset();
1558  ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1559                            kTouchId1, GetTime());
1560  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press3);
1561  EXPECT_FALSE(queued_delegate->tap());
1562  EXPECT_FALSE(queued_delegate->tap_down());
1563  EXPECT_FALSE(queued_delegate->tap_cancel());
1564  EXPECT_FALSE(queued_delegate->begin());
1565  EXPECT_FALSE(queued_delegate->end());
1566  EXPECT_FALSE(queued_delegate->begin());
1567  EXPECT_FALSE(queued_delegate->end());
1568  EXPECT_FALSE(queued_delegate->double_tap());
1569  EXPECT_FALSE(queued_delegate->scroll_begin());
1570  EXPECT_FALSE(queued_delegate->scroll_update());
1571  EXPECT_FALSE(queued_delegate->scroll_end());
1572
1573  queued_delegate->Reset();
1574  delegate->Reset();
1575  ui::TouchEvent press4(ui::ET_TOUCH_PRESSED, gfx::Point(103, 203),
1576                            kTouchId2, GetTime());
1577  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press4);
1578  EXPECT_FALSE(delegate->tap());
1579  EXPECT_FALSE(delegate->tap_down());
1580  EXPECT_FALSE(delegate->tap_cancel());
1581  EXPECT_FALSE(delegate->begin());
1582  EXPECT_FALSE(delegate->end());
1583  EXPECT_FALSE(delegate->double_tap());
1584  EXPECT_FALSE(delegate->scroll_begin());
1585  EXPECT_FALSE(delegate->scroll_update());
1586  EXPECT_FALSE(delegate->scroll_end());
1587  EXPECT_FALSE(queued_delegate->tap());
1588  EXPECT_FALSE(queued_delegate->tap_down());
1589  EXPECT_FALSE(queued_delegate->tap_cancel());
1590  EXPECT_FALSE(queued_delegate->begin());
1591  EXPECT_FALSE(queued_delegate->end());
1592  EXPECT_FALSE(queued_delegate->double_tap());
1593  EXPECT_FALSE(queued_delegate->scroll_begin());
1594  EXPECT_FALSE(queued_delegate->scroll_update());
1595  EXPECT_FALSE(queued_delegate->scroll_end());
1596
1597  // Move the second touch-point enough so that it is considered a pinch. This
1598  // should generate both SCROLL_BEGIN and PINCH_BEGIN gestures.
1599  queued_delegate->Reset();
1600  delegate->Reset();
1601  int x_move = ui::GestureConfiguration::max_touch_move_in_pixels_for_click();
1602  ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(103 + x_move, 203),
1603                          kTouchId2, GetTime());
1604  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
1605  EXPECT_FALSE(delegate->tap());
1606  EXPECT_FALSE(delegate->tap_down());
1607  EXPECT_FALSE(delegate->tap_cancel());
1608  EXPECT_FALSE(delegate->begin());
1609  EXPECT_FALSE(delegate->double_tap());
1610  EXPECT_FALSE(delegate->scroll_begin());
1611  EXPECT_FALSE(delegate->scroll_update());
1612  EXPECT_FALSE(delegate->scroll_end());
1613  EXPECT_FALSE(queued_delegate->tap());
1614  EXPECT_FALSE(queued_delegate->tap_down());
1615  EXPECT_FALSE(queued_delegate->tap_cancel());
1616  EXPECT_FALSE(queued_delegate->begin());
1617  EXPECT_FALSE(queued_delegate->double_tap());
1618  EXPECT_FALSE(queued_delegate->scroll_begin());
1619  EXPECT_FALSE(queued_delegate->scroll_update());
1620  EXPECT_FALSE(queued_delegate->scroll_end());
1621
1622  queued_delegate->Reset();
1623  queued_delegate->ReceivedAck();
1624  EXPECT_FALSE(queued_delegate->tap());
1625  EXPECT_TRUE(queued_delegate->tap_down());
1626  EXPECT_TRUE(queued_delegate->begin());
1627  EXPECT_FALSE(queued_delegate->tap_cancel());
1628  EXPECT_FALSE(queued_delegate->end());
1629  EXPECT_FALSE(queued_delegate->double_tap());
1630  EXPECT_FALSE(queued_delegate->scroll_begin());
1631  EXPECT_FALSE(queued_delegate->scroll_update());
1632  EXPECT_FALSE(queued_delegate->scroll_end());
1633
1634  queued_delegate->Reset();
1635  queued_delegate->ReceivedAck();
1636  EXPECT_FALSE(queued_delegate->tap());
1637  EXPECT_FALSE(queued_delegate->tap_down());  // no touch down for second tap.
1638  EXPECT_TRUE(queued_delegate->tap_cancel());
1639  EXPECT_TRUE(queued_delegate->begin());
1640  EXPECT_FALSE(queued_delegate->end());
1641  EXPECT_FALSE(queued_delegate->double_tap());
1642  EXPECT_FALSE(queued_delegate->scroll_begin());
1643  EXPECT_FALSE(queued_delegate->scroll_update());
1644  EXPECT_FALSE(queued_delegate->scroll_end());
1645  EXPECT_FALSE(queued_delegate->pinch_begin());
1646  EXPECT_FALSE(queued_delegate->pinch_update());
1647  EXPECT_FALSE(queued_delegate->pinch_end());
1648
1649  queued_delegate->Reset();
1650  queued_delegate->ReceivedAck();
1651  EXPECT_FALSE(queued_delegate->tap());
1652  EXPECT_FALSE(queued_delegate->tap_down());
1653  EXPECT_FALSE(queued_delegate->tap_cancel());
1654  EXPECT_FALSE(queued_delegate->begin());
1655  EXPECT_FALSE(queued_delegate->end());
1656  EXPECT_FALSE(queued_delegate->double_tap());
1657  EXPECT_TRUE(queued_delegate->scroll_begin());
1658  EXPECT_FALSE(queued_delegate->scroll_update());
1659  EXPECT_FALSE(queued_delegate->scroll_end());
1660  EXPECT_TRUE(queued_delegate->pinch_begin());
1661  EXPECT_FALSE(queued_delegate->pinch_update());
1662  EXPECT_FALSE(queued_delegate->pinch_end());
1663}
1664
1665// Check that appropriate touch events generate pinch gesture events.
1666TEST_F(GestureRecognizerTest, GestureEventPinchFromScroll) {
1667  scoped_ptr<GestureEventConsumeDelegate> delegate(
1668      new GestureEventConsumeDelegate());
1669  const int kWindowWidth = 300;
1670  const int kWindowHeight = 400;
1671  const int kTouchId1 = 5;
1672  const int kTouchId2 = 3;
1673  gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1674  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1675      delegate.get(), -1234, bounds, NULL));
1676
1677  aura::RootWindow* root = root_window();
1678
1679  delegate->Reset();
1680  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
1681                           kTouchId1, GetTime());
1682  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1683  EXPECT_2_EVENTS(delegate->events(),
1684                  ui::ET_GESTURE_BEGIN,
1685                  ui::ET_GESTURE_TAP_DOWN);
1686
1687  // Move the touch-point enough so that it is considered as a scroll. This
1688  // should generate both SCROLL_BEGIN and SCROLL_UPDATE gestures.
1689  delegate->Reset();
1690  ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(130, 301),
1691                          kTouchId1, GetTime());
1692  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
1693  EXPECT_3_EVENTS(delegate->events(),
1694                  ui::ET_GESTURE_TAP_CANCEL,
1695                  ui::ET_GESTURE_SCROLL_BEGIN,
1696                  ui::ET_GESTURE_SCROLL_UPDATE);
1697
1698  // Press the second finger. It should cause pinch-begin. Note that we will not
1699  // transition to two finger tap here because the touch points are far enough.
1700  delegate->Reset();
1701  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1702                            kTouchId2, GetTime());
1703  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1704  EXPECT_2_EVENTS(delegate->events(),
1705                 ui::ET_GESTURE_BEGIN,
1706                 ui::ET_GESTURE_PINCH_BEGIN);
1707  EXPECT_EQ(gfx::Rect(10, 10, 120, 291).ToString(),
1708            delegate->bounding_box().ToString());
1709
1710  // Move the first finger.
1711  delegate->Reset();
1712  ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(95, 201),
1713                           kTouchId1, GetTime());
1714  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move3);
1715  EXPECT_2_EVENTS(delegate->events(),
1716                  ui::ET_GESTURE_PINCH_UPDATE,
1717                  ui::ET_GESTURE_SCROLL_UPDATE);
1718  EXPECT_EQ(gfx::Rect(10, 10, 85, 191).ToString(),
1719            delegate->bounding_box().ToString());
1720
1721  // Now move the second finger.
1722  delegate->Reset();
1723  ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
1724                           kTouchId2, GetTime());
1725  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move4);
1726  EXPECT_2_EVENTS(delegate->events(),
1727                  ui::ET_GESTURE_PINCH_UPDATE,
1728                  ui::ET_GESTURE_SCROLL_UPDATE);
1729  EXPECT_EQ(gfx::Rect(55, 15, 40, 186).ToString(),
1730            delegate->bounding_box().ToString());
1731
1732  // Release the first finger. This should end pinch.
1733  delegate->Reset();
1734  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1735                             kTouchId1, press.time_stamp() +
1736                             base::TimeDelta::FromMilliseconds(50));
1737  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1738  EXPECT_2_EVENTS(delegate->events(),
1739                 ui::ET_GESTURE_PINCH_END,
1740                 ui::ET_GESTURE_END);
1741  EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
1742            delegate->bounding_box().ToString());
1743
1744  // Move the second finger. This should still generate a scroll.
1745  delegate->Reset();
1746  ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
1747                           kTouchId2, GetTime());
1748  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move5);
1749  EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1750  EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1751}
1752
1753TEST_F(GestureRecognizerTest, GestureEventPinchFromScrollFromPinch) {
1754scoped_ptr<GestureEventConsumeDelegate> delegate(
1755      new GestureEventConsumeDelegate());
1756  const int kWindowWidth = 300;
1757  const int kWindowHeight = 400;
1758  const int kTouchId1 = 5;
1759  const int kTouchId2 = 3;
1760  gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1761  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1762      delegate.get(), -1234, bounds, NULL));
1763
1764  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
1765                           kTouchId1, GetTime());
1766  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1767  delegate->Reset();
1768  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1769                            kTouchId2, GetTime());
1770  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1771  // Since the touch points are far enough we will go to pinch rather than two
1772  // finger tap.
1773  EXPECT_TRUE(delegate->pinch_begin());
1774
1775  SendScrollEvent(root_window(), 130, 230, kTouchId1, delegate.get());
1776  EXPECT_TRUE(delegate->pinch_update());
1777
1778  // Pinch has started, now release the second finger
1779  delegate->Reset();
1780  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1781                             kTouchId1, GetTime());
1782  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1783  EXPECT_TRUE(delegate->pinch_end());
1784
1785  SendScrollEvent(root_window(), 130, 230, kTouchId2, delegate.get());
1786  EXPECT_TRUE(delegate->scroll_update());
1787
1788  // Pinch again
1789  delegate->Reset();
1790  ui::TouchEvent press3(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1791                            kTouchId1, GetTime());
1792  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press3);
1793  // Now the touch points are close. So we will go into two finger tap.
1794  // Move the touch-point enough to break two-finger-tap and enter pinch.
1795  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 202),
1796                          kTouchId1, GetTime());
1797  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
1798  EXPECT_TRUE(delegate->pinch_begin());
1799
1800  SendScrollEvent(root_window(), 130, 230, kTouchId1, delegate.get());
1801  EXPECT_TRUE(delegate->pinch_update());
1802}
1803
1804TEST_F(GestureRecognizerTest, GestureEventPinchFromTap) {
1805  scoped_ptr<GestureEventConsumeDelegate> delegate(
1806      new GestureEventConsumeDelegate());
1807  const int kWindowWidth = 300;
1808  const int kWindowHeight = 400;
1809  const int kTouchId1 = 3;
1810  const int kTouchId2 = 5;
1811  gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight);
1812  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
1813      delegate.get(), -1234, bounds, NULL));
1814
1815  aura::RootWindow* root = root_window();
1816
1817  delegate->Reset();
1818  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 301),
1819                           kTouchId1, GetTime());
1820  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1821  EXPECT_2_EVENTS(delegate->events(),
1822                  ui::ET_GESTURE_BEGIN,
1823                  ui::ET_GESTURE_TAP_DOWN);
1824  EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1825
1826  // Press the second finger far enough to break two finger tap. It should
1827  // instead cause a scroll-begin and pinch-begin.
1828  delegate->Reset();
1829  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
1830                            kTouchId2, GetTime());
1831  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
1832  EXPECT_4_EVENTS(delegate->events(),
1833                  ui::ET_GESTURE_TAP_CANCEL,
1834                  ui::ET_GESTURE_BEGIN,
1835                  ui::ET_GESTURE_PINCH_BEGIN,
1836                  ui::ET_GESTURE_SCROLL_BEGIN);
1837  EXPECT_EQ(gfx::Rect(10, 10, 91, 291).ToString(),
1838            delegate->bounding_box().ToString());
1839
1840  // Move the first finger.
1841  delegate->Reset();
1842  ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(65, 201),
1843                           kTouchId1, GetTime());
1844  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move3);
1845  EXPECT_2_EVENTS(delegate->events(),
1846                  ui::ET_GESTURE_PINCH_UPDATE,
1847                  ui::ET_GESTURE_SCROLL_UPDATE);
1848  EXPECT_EQ(gfx::Rect(10, 10, 55, 191).ToString(),
1849            delegate->bounding_box().ToString());
1850
1851  // Now move the second finger.
1852  delegate->Reset();
1853  ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(55, 15),
1854                           kTouchId2, GetTime());
1855  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move4);
1856  EXPECT_2_EVENTS(delegate->events(),
1857                  ui::ET_GESTURE_PINCH_UPDATE,
1858                  ui::ET_GESTURE_SCROLL_UPDATE);
1859  EXPECT_EQ(gfx::Rect(55, 15, 10, 186).ToString(),
1860            delegate->bounding_box().ToString());
1861
1862  // Release the first finger. This should end pinch.
1863  delegate->Reset();
1864  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1865                             kTouchId1, press.time_stamp() +
1866                             base::TimeDelta::FromMilliseconds(50));
1867  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
1868  EXPECT_2_EVENTS(delegate->events(),
1869                  ui::ET_GESTURE_PINCH_END,
1870                  ui::ET_GESTURE_END);
1871  EXPECT_EQ(gfx::Rect(55, 15, 46, 186).ToString(),
1872            delegate->bounding_box().ToString());
1873
1874  // Move the second finger. This should still generate a scroll.
1875  delegate->Reset();
1876  ui::TouchEvent move5(ui::ET_TOUCH_MOVED, gfx::Point(25, 10),
1877                           kTouchId2, GetTime());
1878  root->AsRootWindowHostDelegate()->OnHostTouchEvent(&move5);
1879  EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE);
1880  EXPECT_TRUE(delegate->bounding_box().IsEmpty());
1881}
1882
1883TEST_F(GestureRecognizerTest, GestureEventIgnoresDisconnectedEvents) {
1884  scoped_ptr<GestureEventConsumeDelegate> delegate(
1885      new GestureEventConsumeDelegate());
1886
1887  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
1888                              6, GetTime());
1889  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
1890  EXPECT_FALSE(delegate->tap());
1891  EXPECT_FALSE(delegate->tap_down());
1892}
1893
1894// Check that a touch is locked to the window of the closest current touch
1895// within max_separation_for_gesture_touches_in_pixels
1896TEST_F(GestureRecognizerTest, GestureEventTouchLockSelectsCorrectWindow) {
1897  ui::GestureRecognizer* gesture_recognizer =
1898      new ui::GestureRecognizerImpl(root_window());
1899  root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1900
1901  ui::GestureConsumer* target;
1902  const int kNumWindows = 4;
1903
1904  scoped_array<GestureEventConsumeDelegate*> delegates(
1905      new GestureEventConsumeDelegate*[kNumWindows]);
1906
1907  ui::GestureConfiguration::
1908      set_max_separation_for_gesture_touches_in_pixels(499);
1909
1910  scoped_array<gfx::Rect> window_bounds(new gfx::Rect[kNumWindows]);
1911  window_bounds[0] = gfx::Rect(0, 0, 1, 1);
1912  window_bounds[1] = gfx::Rect(500, 0, 1, 1);
1913  window_bounds[2] = gfx::Rect(0, 500, 1, 1);
1914  window_bounds[3] = gfx::Rect(500, 500, 1, 1);
1915
1916  scoped_array<aura::Window*> windows(new aura::Window*[kNumWindows]);
1917
1918  // Instantiate windows with |window_bounds| and touch each window at
1919  // its origin.
1920  for (int i = 0; i < kNumWindows; ++i) {
1921    delegates[i] = new GestureEventConsumeDelegate();
1922    windows[i] = CreateTestWindowWithDelegate(
1923        delegates[i], i, window_bounds[i], NULL);
1924    windows[i]->set_id(i);
1925    ui::TouchEvent press(ui::ET_TOUCH_PRESSED, window_bounds[i].origin(),
1926                             i, GetTime());
1927    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1928  }
1929
1930  // Touches should now be associated with the closest touch within
1931  // ui::GestureConfiguration::max_separation_for_gesture_touches_in_pixels
1932  target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 11));
1933  EXPECT_EQ("0", WindowIDAsString(target));
1934  target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 11));
1935  EXPECT_EQ("1", WindowIDAsString(target));
1936  target = gesture_recognizer->GetTargetForLocation(gfx::Point(11, 511));
1937  EXPECT_EQ("2", WindowIDAsString(target));
1938  target = gesture_recognizer->GetTargetForLocation(gfx::Point(511, 511));
1939  EXPECT_EQ("3", WindowIDAsString(target));
1940
1941  // Add a touch in the middle associated with windows[2]
1942  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(0, 500),
1943                           kNumWindows, GetTime());
1944  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
1945  ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(250, 250),
1946                          kNumWindows, GetTime());
1947  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
1948
1949  target = gesture_recognizer->GetTargetForLocation(gfx::Point(250, 250));
1950  EXPECT_EQ("2", WindowIDAsString(target));
1951
1952  // Make sure that ties are broken by distance to a current touch
1953  // Closer to the point in the bottom right.
1954  target = gesture_recognizer->GetTargetForLocation(gfx::Point(380, 380));
1955  EXPECT_EQ("3", WindowIDAsString(target));
1956
1957  // This touch is closer to the point in the middle
1958  target = gesture_recognizer->GetTargetForLocation(gfx::Point(300, 300));
1959  EXPECT_EQ("2", WindowIDAsString(target));
1960
1961  // A touch too far from other touches won't be locked to anything
1962  target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000));
1963  EXPECT_TRUE(target == NULL);
1964
1965  // Move a touch associated with windows[2] to 1000, 1000
1966  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(1000, 1000),
1967                           kNumWindows, GetTime());
1968  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
1969
1970  target = gesture_recognizer->GetTargetForLocation(gfx::Point(1000, 1000));
1971  EXPECT_EQ("2", WindowIDAsString(target));
1972
1973  for (int i = 0; i < kNumWindows; ++i) {
1974    // Delete windows before deleting delegates.
1975    delete windows[i];
1976    delete delegates[i];
1977  }
1978}
1979
1980// Check that touch events outside the root window are still handled
1981// by the root window's gesture sequence.
1982TEST_F(GestureRecognizerTest, GestureEventOutsideRootWindowTap) {
1983  TestGestureRecognizer* gesture_recognizer =
1984      new TestGestureRecognizer(root_window());
1985  root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
1986
1987  scoped_ptr<aura::Window> window(CreateTestWindowWithBounds(
1988      gfx::Rect(-100, -100, 2000, 2000), NULL));
1989
1990  ui::GestureSequence* window_gesture_sequence =
1991      gesture_recognizer->GetGestureSequenceForTesting(window.get());
1992
1993  ui::GestureSequence* root_window_gesture_sequence =
1994      gesture_recognizer->GetGestureSequenceForTesting(root_window());
1995
1996  gfx::Point pos1(-10, -10);
1997  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, pos1, 0, GetTime());
1998  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
1999
2000  gfx::Point pos2(1000, 1000);
2001  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, pos2, 1, GetTime());
2002  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2003
2004  // As these presses were outside the root window, they should be
2005  // associated with the root window.
2006  EXPECT_EQ(0, window_gesture_sequence->point_count());
2007  EXPECT_EQ(2, root_window_gesture_sequence->point_count());
2008}
2009
2010TEST_F(GestureRecognizerTest, NoTapWithPreventDefaultedRelease) {
2011  scoped_ptr<QueueTouchEventDelegate> delegate(
2012      new QueueTouchEventDelegate(root_window()));
2013  const int kTouchId = 2;
2014  gfx::Rect bounds(100, 200, 100, 100);
2015  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2016      delegate.get(), -1234, bounds, NULL));
2017  delegate->set_window(window.get());
2018
2019  delegate->Reset();
2020  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2021                           kTouchId, GetTime());
2022  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2023  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2024                             kTouchId, press.time_stamp() +
2025                             base::TimeDelta::FromMilliseconds(50));
2026  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2027
2028  delegate->Reset();
2029  delegate->ReceivedAck();
2030  EXPECT_TRUE(delegate->tap_down());
2031  delegate->Reset();
2032  delegate->ReceivedAckPreventDefaulted();
2033  EXPECT_FALSE(delegate->tap());
2034  EXPECT_TRUE(delegate->tap_cancel());
2035}
2036
2037TEST_F(GestureRecognizerTest, PinchScrollWithPreventDefaultedRelease) {
2038  scoped_ptr<QueueTouchEventDelegate> delegate(
2039      new QueueTouchEventDelegate(root_window()));
2040  const int kTouchId1 = 7;
2041  const int kTouchId2 = 5;
2042  gfx::Rect bounds(10, 20, 100, 100);
2043  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2044      delegate.get(), -1234, bounds, NULL));
2045  delegate->set_window(window.get());
2046
2047  delegate->Reset();
2048  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(15, 25), kTouchId1,
2049      GetTime());
2050  ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(20, 95), kTouchId1,
2051      press.time_stamp() + base::TimeDelta::FromMilliseconds(200));
2052  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(15, 25), kTouchId1,
2053      move.time_stamp() + base::TimeDelta::FromMilliseconds(50));
2054  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2055  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
2056  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2057  delegate->Reset();
2058
2059  // Ack the press event.
2060  delegate->ReceivedAck();
2061  EXPECT_TRUE(delegate->tap_down());
2062  delegate->Reset();
2063
2064  // Ack the move event.
2065  delegate->ReceivedAck();
2066  EXPECT_TRUE(delegate->tap_cancel());
2067  EXPECT_TRUE(delegate->scroll_begin());
2068  delegate->Reset();
2069
2070  // Ack the release event. Although the release event has been processed, it
2071  // should still generate a scroll-end event.
2072  delegate->ReceivedAckPreventDefaulted();
2073  EXPECT_TRUE(delegate->scroll_end());
2074
2075  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 25), kTouchId2,
2076      GetTime());
2077  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(45, 85), kTouchId2,
2078      press2.time_stamp() + base::TimeDelta::FromMilliseconds(1000));
2079  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(45, 85), kTouchId2,
2080      move2.time_stamp() + base::TimeDelta::FromMilliseconds(14));
2081
2082  // Do a pinch.
2083  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2084  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
2085  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2086  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
2087  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2088  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2089
2090  // Ack the press and move events.
2091  delegate->Reset();
2092  delegate->ReceivedAck();
2093  EXPECT_TRUE(delegate->begin());
2094  EXPECT_TRUE(delegate->tap_down());
2095
2096  delegate->Reset();
2097  delegate->ReceivedAck();
2098  EXPECT_TRUE(delegate->scroll_begin());
2099
2100  delegate->Reset();
2101  delegate->ReceivedAck();
2102  EXPECT_TRUE(delegate->begin());
2103  EXPECT_FALSE(delegate->pinch_begin());
2104
2105  delegate->Reset();
2106  delegate->ReceivedAck();
2107  EXPECT_TRUE(delegate->pinch_begin());
2108
2109  // Ack the first release. Although the release is processed, it should still
2110  // generate a pinch-end event.
2111  delegate->Reset();
2112  delegate->ReceivedAckPreventDefaulted();
2113  EXPECT_TRUE(delegate->pinch_end());
2114  EXPECT_TRUE(delegate->end());
2115
2116  delegate->Reset();
2117  delegate->ReceivedAckPreventDefaulted();
2118  EXPECT_TRUE(delegate->scroll_end());
2119  EXPECT_TRUE(delegate->end());
2120}
2121
2122TEST_F(GestureRecognizerTest, CaptureSendsGestureEnd) {
2123  scoped_ptr<GestureEventConsumeDelegate> delegate(
2124      new GestureEventConsumeDelegate());
2125  TestGestureRecognizer* gesture_recognizer =
2126      new TestGestureRecognizer(root_window());
2127  root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
2128
2129  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2130      delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), NULL));
2131  EventGenerator generator(root_window());
2132
2133  generator.MoveMouseRelativeTo(window.get(), gfx::Point(10, 10));
2134  generator.PressTouch();
2135  RunAllPendingInMessageLoop();
2136
2137  EXPECT_TRUE(delegate->tap_down());
2138
2139  scoped_ptr<aura::Window> capture(CreateTestWindowWithBounds(
2140      gfx::Rect(10, 10, 200, 200), NULL));
2141  capture->SetCapture();
2142  RunAllPendingInMessageLoop();
2143
2144  EXPECT_TRUE(delegate->end());
2145  EXPECT_TRUE(delegate->tap_cancel());
2146}
2147
2148TEST_F(GestureRecognizerTest, PressDoesNotCrash) {
2149  scoped_ptr<GestureEventConsumeDelegate> delegate(
2150      new GestureEventConsumeDelegate());
2151  TestGestureRecognizer* gesture_recognizer =
2152      new TestGestureRecognizer(root_window());
2153  root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
2154
2155  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2156      delegate.get(), -1234, gfx::Rect(10, 10, 300, 300), NULL));
2157
2158  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(45, 45), 7, GetTime());
2159  press.set_radius_x(40);
2160  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2161  EXPECT_TRUE(delegate->tap_down());
2162  EXPECT_EQ(gfx::Rect(5, 5, 80, 80).ToString(),
2163            delegate->bounding_box().ToString());
2164  delegate->Reset();
2165
2166  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(55, 45), 7, GetTime());
2167  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2168
2169  // This new press should not generate a tap-down.
2170  EXPECT_FALSE(delegate->begin());
2171  EXPECT_FALSE(delegate->tap_down());
2172  EXPECT_FALSE(delegate->tap_cancel());
2173  EXPECT_FALSE(delegate->scroll_begin());
2174}
2175
2176TEST_F(GestureRecognizerTest, TwoFingerTap) {
2177  scoped_ptr<GestureEventConsumeDelegate> delegate(
2178      new GestureEventConsumeDelegate());
2179  const int kWindowWidth = 123;
2180  const int kWindowHeight = 45;
2181  const int kTouchId1 = 2;
2182  const int kTouchId2 = 3;
2183  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2184  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2185      delegate.get(), -1234, bounds, NULL));
2186
2187  delegate->Reset();
2188  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2189                            kTouchId1, GetTime());
2190  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2191  EXPECT_FALSE(delegate->tap());
2192  EXPECT_TRUE(delegate->tap_down());
2193  EXPECT_FALSE(delegate->tap_cancel());
2194  EXPECT_FALSE(delegate->double_tap());
2195  EXPECT_FALSE(delegate->scroll_begin());
2196  EXPECT_FALSE(delegate->scroll_update());
2197  EXPECT_FALSE(delegate->scroll_end());
2198  EXPECT_FALSE(delegate->long_press());
2199  EXPECT_FALSE(delegate->two_finger_tap());
2200
2201  delegate->Reset();
2202  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2203                            kTouchId2, GetTime());
2204  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2205  EXPECT_FALSE(delegate->tap());
2206  EXPECT_FALSE(delegate->tap_down());  // no touch down for second tap.
2207  EXPECT_TRUE(delegate->tap_cancel());
2208  EXPECT_FALSE(delegate->double_tap());
2209  EXPECT_FALSE(delegate->scroll_begin());
2210  EXPECT_FALSE(delegate->scroll_update());
2211  EXPECT_FALSE(delegate->scroll_end());
2212  EXPECT_FALSE(delegate->long_press());
2213  EXPECT_FALSE(delegate->two_finger_tap());
2214
2215  // Little bit of touch move should not affect our state.
2216  delegate->Reset();
2217  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(102, 202),
2218                           kTouchId1, GetTime());
2219  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
2220  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(131, 202),
2221                           kTouchId2, GetTime());
2222  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
2223  EXPECT_FALSE(delegate->tap());
2224  EXPECT_FALSE(delegate->tap_down());
2225  EXPECT_FALSE(delegate->tap_cancel());
2226  EXPECT_FALSE(delegate->double_tap());
2227  EXPECT_FALSE(delegate->scroll_begin());
2228  EXPECT_FALSE(delegate->scroll_update());
2229  EXPECT_FALSE(delegate->scroll_end());
2230  EXPECT_FALSE(delegate->long_press());
2231  EXPECT_FALSE(delegate->two_finger_tap());
2232
2233  // Make sure there is enough delay before the touch is released so that it is
2234  // recognized as a tap.
2235  delegate->Reset();
2236  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2237                              kTouchId1, press1.time_stamp() +
2238                              base::TimeDelta::FromMilliseconds(50));
2239
2240  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2241  EXPECT_FALSE(delegate->tap());
2242  EXPECT_FALSE(delegate->tap_down());
2243  EXPECT_FALSE(delegate->tap_cancel());
2244  EXPECT_FALSE(delegate->double_tap());
2245  EXPECT_FALSE(delegate->scroll_begin());
2246  EXPECT_FALSE(delegate->scroll_update());
2247  EXPECT_FALSE(delegate->scroll_end());
2248  EXPECT_TRUE(delegate->two_finger_tap());
2249
2250  // Lift second finger.
2251  // Make sure there is enough delay before the touch is released so that it is
2252  // recognized as a tap.
2253  delegate->Reset();
2254  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2255                              kTouchId2, press2.time_stamp() +
2256                                  base::TimeDelta::FromMilliseconds(50));
2257
2258  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2259  EXPECT_FALSE(delegate->tap());
2260  EXPECT_FALSE(delegate->tap_down());
2261  EXPECT_FALSE(delegate->tap_cancel());
2262  EXPECT_FALSE(delegate->double_tap());
2263  EXPECT_FALSE(delegate->scroll_begin());
2264  EXPECT_FALSE(delegate->scroll_update());
2265  EXPECT_TRUE(delegate->scroll_end());
2266  EXPECT_FALSE(delegate->two_finger_tap());
2267}
2268
2269TEST_F(GestureRecognizerTest, TwoFingerTapExpired) {
2270  scoped_ptr<GestureEventConsumeDelegate> delegate(
2271      new GestureEventConsumeDelegate());
2272  const int kWindowWidth = 123;
2273  const int kWindowHeight = 45;
2274  const int kTouchId1 = 2;
2275  const int kTouchId2 = 3;
2276  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2277  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2278      delegate.get(), -1234, bounds, NULL));
2279
2280  delegate->Reset();
2281  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2282                            kTouchId1, GetTime());
2283  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2284
2285  delegate->Reset();
2286  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2287                            kTouchId2, GetTime());
2288  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2289
2290  // Send release event after sufficient delay so that two finger time expires.
2291  delegate->Reset();
2292  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2293                              kTouchId1, press1.time_stamp() +
2294                                  base::TimeDelta::FromMilliseconds(1000));
2295
2296  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2297  EXPECT_FALSE(delegate->two_finger_tap());
2298
2299  // Lift second finger.
2300  // Make sure there is enough delay before the touch is released so that it is
2301  // recognized as a tap.
2302  delegate->Reset();
2303  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(130, 201),
2304                              kTouchId2, press2.time_stamp() +
2305                                  base::TimeDelta::FromMilliseconds(50));
2306
2307  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2308  EXPECT_FALSE(delegate->two_finger_tap());
2309}
2310
2311TEST_F(GestureRecognizerTest, TwoFingerTapChangesToPinch) {
2312  scoped_ptr<GestureEventConsumeDelegate> delegate(
2313      new GestureEventConsumeDelegate());
2314  const int kWindowWidth = 123;
2315  const int kWindowHeight = 45;
2316  const int kTouchId1 = 2;
2317  const int kTouchId2 = 3;
2318
2319  // Test moving first finger
2320  {
2321    gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2322    scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2323        delegate.get(), -1234, bounds, NULL));
2324
2325    delegate->Reset();
2326    ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2327                              kTouchId1, GetTime());
2328    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2329
2330    delegate->Reset();
2331    ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2332                              kTouchId2, GetTime());
2333    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2334
2335    SendScrollEvent(root_window(), 130, 230, kTouchId1, delegate.get());
2336    EXPECT_FALSE(delegate->two_finger_tap());
2337    EXPECT_TRUE(delegate->pinch_begin());
2338
2339    // Make sure there is enough delay before the touch is released so that it
2340    // is recognized as a tap.
2341    delegate->Reset();
2342    ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2343                               kTouchId2, press1.time_stamp() +
2344                                   base::TimeDelta::FromMilliseconds(50));
2345
2346    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2347    EXPECT_FALSE(delegate->two_finger_tap());
2348    EXPECT_TRUE(delegate->pinch_end());
2349  }
2350
2351  // Test moving second finger
2352  {
2353    gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2354    scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2355        delegate.get(), -1234, bounds, NULL));
2356
2357    delegate->Reset();
2358    ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2359                              kTouchId1, GetTime());
2360    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2361
2362    delegate->Reset();
2363    ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2364                              kTouchId2, GetTime());
2365    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2366
2367    SendScrollEvent(root_window(), 101, 230, kTouchId2, delegate.get());
2368    EXPECT_FALSE(delegate->two_finger_tap());
2369    EXPECT_TRUE(delegate->pinch_begin());
2370
2371    // Make sure there is enough delay before the touch is released so that it
2372    // is recognized as a tap.
2373    delegate->Reset();
2374    ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2375                               kTouchId1, press1.time_stamp() +
2376                                   base::TimeDelta::FromMilliseconds(50));
2377
2378    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2379    EXPECT_FALSE(delegate->two_finger_tap());
2380    EXPECT_TRUE(delegate->pinch_end());
2381  }
2382}
2383
2384TEST_F(GestureRecognizerTest, MultiFingerSwipe) {
2385  scoped_ptr<GestureEventConsumeDelegate> delegate(
2386      new GestureEventConsumeDelegate());
2387  const int kWindowWidth = 123;
2388  const int kWindowHeight = 45;
2389
2390  gfx::Rect bounds(5, 10, kWindowWidth, kWindowHeight);
2391  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2392      delegate.get(), -1234, bounds, NULL));
2393
2394  const int kSteps = 15;
2395  const int kTouchPoints = 4;
2396  gfx::Point points[kTouchPoints] = {
2397    gfx::Point(10, 30),
2398    gfx::Point(30, 20),
2399    gfx::Point(50, 30),
2400    gfx::Point(80, 50)
2401  };
2402
2403  aura::test::EventGenerator generator(root_window(), window.get());
2404
2405  for (int count = 2; count <= kTouchPoints; ++count) {
2406    generator.GestureMultiFingerScroll(count, points, 15, kSteps, 0, -150);
2407    EXPECT_TRUE(delegate->swipe_up());
2408    delegate->Reset();
2409
2410    generator.GestureMultiFingerScroll(count, points, 15, kSteps, 0, 150);
2411    EXPECT_TRUE(delegate->swipe_down());
2412    delegate->Reset();
2413
2414    generator.GestureMultiFingerScroll(count, points, 15, kSteps, -150, 0);
2415    EXPECT_TRUE(delegate->swipe_left());
2416    delegate->Reset();
2417
2418    generator.GestureMultiFingerScroll(count, points, 15, kSteps, 150, 0);
2419    EXPECT_TRUE(delegate->swipe_right());
2420    delegate->Reset();
2421  }
2422}
2423
2424TEST_F(GestureRecognizerTest, TwoFingerTapCancelled) {
2425  scoped_ptr<GestureEventConsumeDelegate> delegate(
2426      new GestureEventConsumeDelegate());
2427  const int kWindowWidth = 123;
2428  const int kWindowHeight = 45;
2429  const int kTouchId1 = 2;
2430  const int kTouchId2 = 3;
2431
2432  // Test canceling first finger.
2433  {
2434    gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2435    scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2436        delegate.get(), -1234, bounds, NULL));
2437
2438    delegate->Reset();
2439    ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2440                              kTouchId1, GetTime());
2441    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2442
2443    delegate->Reset();
2444    ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2445                              kTouchId2, GetTime());
2446    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2447
2448    delegate->Reset();
2449    ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2450                              kTouchId1, GetTime());
2451    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&cancel);
2452    EXPECT_FALSE(delegate->two_finger_tap());
2453
2454    // Make sure there is enough delay before the touch is released so that it
2455    // is recognized as a tap.
2456    delegate->Reset();
2457    ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2458                               kTouchId2, press1.time_stamp() +
2459                                   base::TimeDelta::FromMilliseconds(50));
2460
2461    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2462    EXPECT_FALSE(delegate->two_finger_tap());
2463  }
2464
2465  // Test canceling second finger
2466  {
2467    gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2468    scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2469        delegate.get(), -1234, bounds, NULL));
2470
2471    delegate->Reset();
2472    ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2473                              kTouchId1, GetTime());
2474    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2475
2476    delegate->Reset();
2477    ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(130, 201),
2478                              kTouchId2, GetTime());
2479    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2480
2481    delegate->Reset();
2482    ui::TouchEvent cancel(ui::ET_TOUCH_CANCELLED, gfx::Point(130, 201),
2483                              kTouchId2, GetTime());
2484    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&cancel);
2485    EXPECT_FALSE(delegate->two_finger_tap());
2486
2487    // Make sure there is enough delay before the touch is released so that it
2488    // is recognized as a tap.
2489    delegate->Reset();
2490    ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2491                               kTouchId1, press1.time_stamp() +
2492                                   base::TimeDelta::FromMilliseconds(50));
2493
2494    root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2495    EXPECT_FALSE(delegate->two_finger_tap());
2496  }
2497}
2498
2499TEST_F(GestureRecognizerTest, VeryWideTwoFingerTouchDownShouldBeAPinch) {
2500  scoped_ptr<GestureEventConsumeDelegate> delegate(
2501      new GestureEventConsumeDelegate());
2502  const int kWindowWidth = 523;
2503  const int kWindowHeight = 45;
2504  const int kTouchId1 = 2;
2505  const int kTouchId2 = 3;
2506  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2507  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2508      delegate.get(), -1234, bounds, NULL));
2509
2510  delegate->Reset();
2511  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2512                            kTouchId1, GetTime());
2513  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2514  EXPECT_FALSE(delegate->tap());
2515  EXPECT_TRUE(delegate->tap_down());
2516  EXPECT_FALSE(delegate->tap_cancel());
2517  EXPECT_FALSE(delegate->double_tap());
2518  EXPECT_FALSE(delegate->scroll_begin());
2519  EXPECT_FALSE(delegate->scroll_update());
2520  EXPECT_FALSE(delegate->scroll_end());
2521  EXPECT_FALSE(delegate->long_press());
2522  EXPECT_FALSE(delegate->two_finger_tap());
2523
2524  delegate->Reset();
2525  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(430, 201),
2526                            kTouchId2, GetTime());
2527  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2528  EXPECT_FALSE(delegate->tap());
2529  EXPECT_FALSE(delegate->tap_down());  // no touch down for second tap.
2530  EXPECT_TRUE(delegate->tap_cancel());
2531  EXPECT_FALSE(delegate->double_tap());
2532  EXPECT_TRUE(delegate->scroll_begin());
2533  EXPECT_FALSE(delegate->scroll_update());
2534  EXPECT_FALSE(delegate->scroll_end());
2535  EXPECT_FALSE(delegate->long_press());
2536  EXPECT_FALSE(delegate->two_finger_tap());
2537  EXPECT_TRUE(delegate->pinch_begin());
2538}
2539
2540// Verifies if a window is the target of multiple touch-ids and we hide the
2541// window everything is cleaned up correctly.
2542TEST_F(GestureRecognizerTest, FlushAllOnHide) {
2543  scoped_ptr<GestureEventConsumeDelegate> delegate(
2544      new GestureEventConsumeDelegate());
2545  gfx::Rect bounds(0, 0, 200, 200);
2546  scoped_ptr<aura::Window> window(
2547      CreateTestWindowWithDelegate(delegate.get(), 0, bounds, NULL));
2548  const int kTouchId1 = 8;
2549  const int kTouchId2 = 2;
2550  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10),
2551                            kTouchId1, GetTime());
2552  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2553  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(20, 20),
2554                            kTouchId2, GetTime());
2555  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2556  window->Hide();
2557  EXPECT_EQ(NULL,
2558            root_window()->gesture_recognizer()->GetTouchLockedTarget(&press1));
2559  EXPECT_EQ(NULL,
2560            root_window()->gesture_recognizer()->GetTouchLockedTarget(&press2));
2561}
2562
2563TEST_F(GestureRecognizerTest, LongPressTimerStopsOnPreventDefaultedTouchMoves) {
2564  scoped_ptr<QueueTouchEventDelegate> delegate(
2565      new QueueTouchEventDelegate(root_window()));
2566  const int kTouchId = 2;
2567  gfx::Rect bounds(100, 200, 100, 100);
2568  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2569      delegate.get(), -1234, bounds, NULL));
2570  delegate->set_window(window.get());
2571
2572  TimerTestGestureRecognizer* gesture_recognizer =
2573      new TimerTestGestureRecognizer(root_window());
2574  TimerTestGestureSequence* gesture_sequence =
2575      static_cast<TimerTestGestureSequence*>(
2576          gesture_recognizer->GetGestureSequenceForTesting(window.get()));
2577
2578  root_window()->SetGestureRecognizerForTesting(gesture_recognizer);
2579
2580  delegate->Reset();
2581  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2582                           kTouchId, GetTime());
2583  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2584  // Scroll around, to cancel the long press
2585  SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
2586
2587  delegate->Reset();
2588  delegate->ReceivedAck();
2589  EXPECT_TRUE(delegate->tap_down());
2590  EXPECT_TRUE(gesture_sequence->IsTimerRunning());
2591
2592  delegate->Reset();
2593  delegate->ReceivedAckPreventDefaulted();
2594  EXPECT_FALSE(gesture_sequence->IsTimerRunning());
2595  gesture_sequence->ForceTimeout();
2596  EXPECT_FALSE(delegate->long_press());
2597}
2598
2599// Same as GestureEventConsumeDelegate, but consumes all the touch-move events.
2600class ConsumesTouchMovesDelegate : public GestureEventConsumeDelegate {
2601 public:
2602  ConsumesTouchMovesDelegate() : consume_touch_move_(true) {}
2603  virtual ~ConsumesTouchMovesDelegate() {}
2604
2605  void set_consume_touch_move(bool consume) { consume_touch_move_ = consume; }
2606
2607 private:
2608  virtual ui::EventResult OnTouchEvent(ui::TouchEvent* touch) OVERRIDE {
2609    if (consume_touch_move_ && touch->type() == ui::ET_TOUCH_MOVED)
2610      return ui::ER_HANDLED;
2611    return GestureEventConsumeDelegate::OnTouchEvent(touch);
2612  }
2613
2614  bool consume_touch_move_;
2615
2616  DISALLOW_COPY_AND_ASSIGN(ConsumesTouchMovesDelegate);
2617};
2618
2619// Same as GestureEventScroll, but tests that the behavior is the same
2620// even if all the touch-move events are consumed.
2621TEST_F(GestureRecognizerTest, GestureEventScrollTouchMoveConsumed) {
2622  scoped_ptr<ConsumesTouchMovesDelegate> delegate(
2623      new ConsumesTouchMovesDelegate());
2624  const int kWindowWidth = 123;
2625  const int kWindowHeight = 45;
2626  const int kTouchId = 5;
2627  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2628  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2629      delegate.get(), -1234, bounds, NULL));
2630
2631  delegate->Reset();
2632  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2633                           kTouchId, GetTime());
2634  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2635  EXPECT_FALSE(delegate->tap());
2636  EXPECT_TRUE(delegate->tap_down());
2637  EXPECT_FALSE(delegate->tap_cancel());
2638  EXPECT_TRUE(delegate->begin());
2639  EXPECT_FALSE(delegate->double_tap());
2640  EXPECT_FALSE(delegate->scroll_begin());
2641  EXPECT_FALSE(delegate->scroll_update());
2642  EXPECT_FALSE(delegate->scroll_end());
2643
2644  // Move the touch-point enough so that it would normally be considered a
2645  // scroll. But since the touch-moves will be consumed, the scroll should not
2646  // start.
2647  SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
2648  EXPECT_FALSE(delegate->tap());
2649  EXPECT_FALSE(delegate->tap_down());
2650  // TODO(rbyers): Really we should get the TapCancel here instead of below,
2651  // but this is a symptom of a larger issue: crbug.com/146397.
2652  EXPECT_FALSE(delegate->tap_cancel());
2653  EXPECT_FALSE(delegate->begin());
2654  EXPECT_FALSE(delegate->double_tap());
2655  EXPECT_FALSE(delegate->scroll_begin());
2656  EXPECT_FALSE(delegate->scroll_update());
2657  EXPECT_FALSE(delegate->scroll_end());
2658
2659  // Release the touch back at the start point. This should end without causing
2660  // a tap.
2661  delegate->Reset();
2662  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(130, 230),
2663                             kTouchId, press.time_stamp() +
2664                                 base::TimeDelta::FromMilliseconds(50));
2665  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2666  EXPECT_FALSE(delegate->tap());
2667  EXPECT_FALSE(delegate->tap_down());
2668  EXPECT_TRUE(delegate->tap_cancel());
2669  EXPECT_FALSE(delegate->begin());
2670  EXPECT_TRUE(delegate->end());
2671  EXPECT_FALSE(delegate->double_tap());
2672  EXPECT_FALSE(delegate->scroll_begin());
2673  EXPECT_FALSE(delegate->scroll_update());
2674  EXPECT_FALSE(delegate->scroll_end());
2675}
2676
2677// Like as GestureEventTouchMoveConsumed but tests the different behavior
2678// depending on whether the events were consumed before or after the scroll
2679// started.
2680TEST_F(GestureRecognizerTest, GestureEventScrollTouchMovePartialConsumed) {
2681  scoped_ptr<ConsumesTouchMovesDelegate> delegate(
2682      new ConsumesTouchMovesDelegate());
2683  const int kWindowWidth = 123;
2684  const int kWindowHeight = 45;
2685  const int kTouchId = 5;
2686  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2687  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2688      delegate.get(), -1234, bounds, NULL));
2689
2690  delegate->Reset();
2691  ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2692                           kTouchId, GetTime());
2693  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
2694  EXPECT_FALSE(delegate->tap());
2695  EXPECT_TRUE(delegate->tap_down());
2696  EXPECT_FALSE(delegate->tap_cancel());
2697  EXPECT_TRUE(delegate->begin());
2698  EXPECT_FALSE(delegate->double_tap());
2699  EXPECT_FALSE(delegate->scroll_begin());
2700  EXPECT_FALSE(delegate->scroll_update());
2701  EXPECT_FALSE(delegate->scroll_end());
2702
2703  // Move the touch-point enough so that it would normally be considered a
2704  // scroll. But since the touch-moves will be consumed, the scroll should not
2705  // start.
2706  SendScrollEvent(root_window(), 130, 230, kTouchId, delegate.get());
2707  EXPECT_FALSE(delegate->tap());
2708  EXPECT_FALSE(delegate->tap_down());
2709  // TODO(rbyers): Really we should get the TapCancel here instead of below,
2710  // but this is a symptom of a larger issue: crbug.com/146397.
2711  EXPECT_FALSE(delegate->tap_cancel());
2712  EXPECT_FALSE(delegate->begin());
2713  EXPECT_FALSE(delegate->double_tap());
2714  EXPECT_FALSE(delegate->scroll_begin());
2715  EXPECT_FALSE(delegate->scroll_update());
2716  EXPECT_FALSE(delegate->scroll_end());
2717
2718  // Now, stop consuming touch-move events, and move the touch-point again.
2719  delegate->set_consume_touch_move(false);
2720  SendScrollEvent(root_window(), 159, 259, kTouchId, delegate.get());
2721  EXPECT_FALSE(delegate->tap());
2722  EXPECT_FALSE(delegate->tap_down());
2723  EXPECT_TRUE(delegate->tap_cancel());
2724  EXPECT_FALSE(delegate->begin());
2725  EXPECT_FALSE(delegate->double_tap());
2726  EXPECT_TRUE(delegate->scroll_begin());
2727  EXPECT_TRUE(delegate->scroll_update());
2728  EXPECT_FALSE(delegate->scroll_end());
2729  // Consuming move events doesn't effect what the ultimate scroll position
2730  // will be if scrolling is later allowed to happen.
2731  EXPECT_EQ(58, delegate->scroll_x());
2732  EXPECT_EQ(58, delegate->scroll_y());
2733  EXPECT_EQ(gfx::Point(1, 1).ToString(),
2734            delegate->scroll_begin_position().ToString());
2735
2736  // Start consuming touch-move events again. However, since gesture-scroll has
2737  // already started, the touch-move events should still result in scroll-update
2738  // gestures.
2739  delegate->set_consume_touch_move(true);
2740
2741  // Move some more to generate a few more scroll updates.
2742  SendScrollEvent(root_window(), 110, 211, kTouchId, delegate.get());
2743  EXPECT_FALSE(delegate->tap());
2744  EXPECT_FALSE(delegate->tap_down());
2745  EXPECT_FALSE(delegate->tap_cancel());
2746  EXPECT_FALSE(delegate->begin());
2747  EXPECT_FALSE(delegate->double_tap());
2748  EXPECT_FALSE(delegate->scroll_begin());
2749  EXPECT_TRUE(delegate->scroll_update());
2750  EXPECT_FALSE(delegate->scroll_end());
2751  EXPECT_EQ(-49, delegate->scroll_x());
2752  EXPECT_EQ(-48, delegate->scroll_y());
2753
2754  SendScrollEvent(root_window(), 140, 215, kTouchId, delegate.get());
2755  EXPECT_FALSE(delegate->tap());
2756  EXPECT_FALSE(delegate->tap_down());
2757  EXPECT_FALSE(delegate->tap_cancel());
2758  EXPECT_FALSE(delegate->begin());
2759  EXPECT_FALSE(delegate->double_tap());
2760  EXPECT_FALSE(delegate->scroll_begin());
2761  EXPECT_TRUE(delegate->scroll_update());
2762  EXPECT_FALSE(delegate->scroll_end());
2763  EXPECT_EQ(30, delegate->scroll_x());
2764  EXPECT_EQ(4, delegate->scroll_y());
2765
2766  // Release the touch. This should end the scroll.
2767  delegate->Reset();
2768  ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2769                             kTouchId, press.time_stamp() +
2770                                 base::TimeDelta::FromMilliseconds(50));
2771  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
2772  EXPECT_FALSE(delegate->tap());
2773  EXPECT_FALSE(delegate->tap_down());
2774  EXPECT_FALSE(delegate->tap_cancel());
2775  EXPECT_FALSE(delegate->begin());
2776  EXPECT_TRUE(delegate->end());
2777  EXPECT_FALSE(delegate->double_tap());
2778  EXPECT_FALSE(delegate->scroll_begin());
2779  EXPECT_FALSE(delegate->scroll_update());
2780  EXPECT_TRUE(delegate->scroll_end());
2781}
2782
2783// Check that appropriate touch events generate double tap gesture events.
2784TEST_F(GestureRecognizerTest, GestureEventDoubleTap) {
2785  scoped_ptr<GestureEventConsumeDelegate> delegate(
2786      new GestureEventConsumeDelegate());
2787  const int kWindowWidth = 123;
2788  const int kWindowHeight = 45;
2789  const int kTouchId = 2;
2790  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2791  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2792      delegate.get(), -1234, bounds, NULL));
2793
2794  // First tap (tested in GestureEventTap)
2795  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(104, 201),
2796                            kTouchId, GetTime());
2797  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2798  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(104, 201),
2799                              kTouchId, press1.time_stamp() +
2800                                  base::TimeDelta::FromMilliseconds(50));
2801  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2802  delegate->Reset();
2803
2804  // Second tap
2805  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 203),
2806                            kTouchId, release1.time_stamp() +
2807                                base::TimeDelta::FromMilliseconds(200));
2808  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2809  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(102, 206),
2810                              kTouchId, press2.time_stamp() +
2811                                  base::TimeDelta::FromMilliseconds(50));
2812  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2813
2814  EXPECT_TRUE(delegate->tap());
2815  EXPECT_TRUE(delegate->tap_down());
2816  EXPECT_FALSE(delegate->tap_cancel());
2817  EXPECT_TRUE(delegate->begin());
2818  EXPECT_TRUE(delegate->end());
2819  EXPECT_TRUE(delegate->double_tap());
2820  EXPECT_FALSE(delegate->scroll_begin());
2821  EXPECT_FALSE(delegate->scroll_update());
2822  EXPECT_FALSE(delegate->scroll_end());
2823
2824  EXPECT_EQ(2, delegate->tap_count());
2825}
2826
2827// Check that we don't get a double tap when the two taps are far apart.
2828TEST_F(GestureRecognizerTest, TwoTapsFarApart) {
2829  scoped_ptr<GestureEventConsumeDelegate> delegate(
2830      new GestureEventConsumeDelegate());
2831  const int kWindowWidth = 123;
2832  const int kWindowHeight = 45;
2833  const int kTouchId = 2;
2834  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2835  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2836      delegate.get(), -1234, bounds, NULL));
2837
2838  // First tap (tested in GestureEventTap)
2839  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2840                   kTouchId, GetTime());
2841  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2842  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2843                              kTouchId, press1.time_stamp() +
2844                                  base::TimeDelta::FromMilliseconds(50));
2845  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2846  delegate->Reset();
2847
2848  // Second tap, close in time but far in distance
2849  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(201, 201),
2850                            kTouchId, release1.time_stamp() +
2851                                base::TimeDelta::FromMilliseconds(200));
2852  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2853  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(201, 201),
2854                              kTouchId, press2.time_stamp() +
2855                                  base::TimeDelta::FromMilliseconds(50));
2856  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2857
2858  EXPECT_TRUE(delegate->tap());
2859  EXPECT_TRUE(delegate->tap_down());
2860  EXPECT_FALSE(delegate->tap_cancel());
2861  EXPECT_TRUE(delegate->begin());
2862  EXPECT_TRUE(delegate->end());
2863  EXPECT_FALSE(delegate->double_tap());
2864  EXPECT_FALSE(delegate->scroll_begin());
2865  EXPECT_FALSE(delegate->scroll_update());
2866  EXPECT_FALSE(delegate->scroll_end());
2867
2868  EXPECT_EQ(1, delegate->tap_count());
2869}
2870
2871// Check that we don't get a double tap when the two taps have a long enough
2872// delay in between.
2873TEST_F(GestureRecognizerTest, TwoTapsWithDelayBetween) {
2874  scoped_ptr<GestureEventConsumeDelegate> delegate(
2875      new GestureEventConsumeDelegate());
2876  const int kWindowWidth = 123;
2877  const int kWindowHeight = 45;
2878  const int kTouchId = 2;
2879  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2880  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2881      delegate.get(), -1234, bounds, NULL));
2882
2883  // First tap (tested in GestureEventTap)
2884  ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2885                            kTouchId, GetTime());
2886  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2887  ui::TouchEvent release1(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2888                              kTouchId, press1.time_stamp() +
2889                                  base::TimeDelta::FromMilliseconds(50));
2890  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release1);
2891  delegate->Reset();
2892
2893  // Second tap, close in distance but after some delay
2894  ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(101, 201),
2895                            kTouchId, release1.time_stamp() +
2896                                base::TimeDelta::FromMilliseconds(2000));
2897  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2898  ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(101, 201),
2899                              kTouchId, press2.time_stamp() +
2900                                  base::TimeDelta::FromMilliseconds(50));
2901  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release2);
2902
2903  EXPECT_TRUE(delegate->tap());
2904  EXPECT_TRUE(delegate->tap_down());
2905  EXPECT_FALSE(delegate->tap_cancel());
2906  EXPECT_TRUE(delegate->begin());
2907  EXPECT_TRUE(delegate->end());
2908  EXPECT_FALSE(delegate->double_tap());
2909  EXPECT_FALSE(delegate->scroll_begin());
2910  EXPECT_FALSE(delegate->scroll_update());
2911  EXPECT_FALSE(delegate->scroll_end());
2912
2913  EXPECT_EQ(1, delegate->tap_count());
2914}
2915
2916// Checks that if the bounding-box of a gesture changes because of change in
2917// radius of a touch-point, and not because of change in position, then there
2918// are not gesture events from that.
2919TEST_F(GestureRecognizerTest, BoundingBoxRadiusChange) {
2920  scoped_ptr<GestureEventConsumeDelegate> delegate(
2921      new GestureEventConsumeDelegate());
2922  const int kWindowWidth = 234;
2923  const int kWindowHeight = 345;
2924  const int kTouchId = 5, kTouchId2 = 7;
2925  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2926  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2927      delegate.get(), -1234, bounds, NULL));
2928
2929  ui::TouchEvent press1(
2930      ui::ET_TOUCH_PRESSED, gfx::Point(101, 201), kTouchId, GetTime());
2931  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2932  EXPECT_TRUE(delegate->bounding_box().IsEmpty());
2933
2934  delegate->Reset();
2935
2936  ui::TouchEvent press2(
2937      ui::ET_TOUCH_PRESSED, gfx::Point(201, 201), kTouchId2,
2938      press1.time_stamp() + base::TimeDelta::FromMilliseconds(400));
2939  press2.set_radius_x(5);
2940  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press2);
2941  EXPECT_FALSE(delegate->pinch_begin());
2942  EXPECT_EQ(gfx::Rect(101, 201, 100, 0).ToString(),
2943            delegate->bounding_box().ToString());
2944
2945  delegate->Reset();
2946
2947  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(141, 201), kTouchId,
2948      press1.time_stamp() + base::TimeDelta::FromMilliseconds(40));
2949  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
2950  EXPECT_TRUE(delegate->pinch_begin());
2951  EXPECT_EQ(gfx::Rect(141, 201, 60, 0).ToString(),
2952            delegate->bounding_box().ToString());
2953
2954  delegate->Reset();
2955
2956  // The position doesn't move, but the radius changes.
2957  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 201), kTouchId,
2958      press2.time_stamp() + base::TimeDelta::FromMilliseconds(40));
2959  move2.set_radius_x(50);
2960  move2.set_radius_y(60);
2961  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
2962  EXPECT_FALSE(delegate->tap());
2963  EXPECT_FALSE(delegate->tap_cancel());
2964  EXPECT_FALSE(delegate->scroll_update());
2965  EXPECT_FALSE(delegate->pinch_update());
2966
2967  delegate->Reset();
2968}
2969
2970// Checks that slow scrolls deliver the correct deltas.
2971// In particular, fix for http;//crbug.com/150573.
2972TEST_F(GestureRecognizerTest, NoDriftInScroll) {
2973  ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(3);
2974  ui::GestureConfiguration::set_min_scroll_delta_squared(9);
2975  scoped_ptr<GestureEventConsumeDelegate> delegate(
2976      new GestureEventConsumeDelegate());
2977  const int kWindowWidth = 234;
2978  const int kWindowHeight = 345;
2979  const int kTouchId = 5;
2980  gfx::Rect bounds(100, 200, kWindowWidth, kWindowHeight);
2981  scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
2982      delegate.get(), -1234, bounds, NULL));
2983
2984  ui::TouchEvent press1(
2985      ui::ET_TOUCH_PRESSED, gfx::Point(101, 208), kTouchId, GetTime());
2986  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
2987  EXPECT_TRUE(delegate->begin());
2988
2989  delegate->Reset();
2990
2991  ui::TouchEvent move1(ui::ET_TOUCH_MOVED, gfx::Point(101, 206), kTouchId,
2992      press1.time_stamp() + base::TimeDelta::FromMilliseconds(40));
2993  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move1);
2994  EXPECT_FALSE(delegate->scroll_begin());
2995
2996  delegate->Reset();
2997
2998  ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
2999      press1.time_stamp() + base::TimeDelta::FromMilliseconds(40));
3000  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move2);
3001  EXPECT_TRUE(delegate->tap_cancel());
3002  EXPECT_TRUE(delegate->scroll_begin());
3003  EXPECT_TRUE(delegate->scroll_update());
3004  EXPECT_EQ(-4, delegate->scroll_y());
3005
3006  delegate->Reset();
3007
3008  ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(101, 204), kTouchId,
3009      press1.time_stamp() + base::TimeDelta::FromMilliseconds(40));
3010  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move3);
3011  EXPECT_FALSE(delegate->scroll_update());
3012
3013  delegate->Reset();
3014
3015  ui::TouchEvent move4(ui::ET_TOUCH_MOVED, gfx::Point(101, 203), kTouchId,
3016      press1.time_stamp() + base::TimeDelta::FromMilliseconds(40));
3017  root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move4);
3018  EXPECT_TRUE(delegate->scroll_update());
3019  EXPECT_EQ(-1, delegate->scroll_y());
3020
3021  delegate->Reset();
3022}
3023
3024}  // namespace test
3025}  // namespace aura
3026