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