Home | History | Annotate | Download | only in input
      1 // Copyright 2013 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/basictypes.h"
      6 #include "base/logging.h"
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "content/browser/renderer_host/input/timeout_monitor.h"
     10 #include "content/browser/renderer_host/input/touch_event_queue.h"
     11 #include "content/common/input/synthetic_web_input_event_builders.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 #include "third_party/WebKit/public/web/WebInputEvent.h"
     14 
     15 using blink::WebGestureEvent;
     16 using blink::WebInputEvent;
     17 using blink::WebTouchEvent;
     18 using blink::WebTouchPoint;
     19 
     20 namespace content {
     21 namespace {
     22 const size_t kDefaultTouchTimeoutDelayMs = 10;
     23 }
     24 
     25 class TouchEventQueueTest : public testing::Test,
     26                             public TouchEventQueueClient {
     27  public:
     28   TouchEventQueueTest()
     29       : sent_event_count_(0),
     30         acked_event_count_(0),
     31         last_acked_event_state_(INPUT_EVENT_ACK_STATE_UNKNOWN) {}
     32 
     33   virtual ~TouchEventQueueTest() {}
     34 
     35   // testing::Test
     36   virtual void SetUp() OVERRIDE {
     37     queue_.reset(new TouchEventQueue(this));
     38   }
     39 
     40   virtual void TearDown() OVERRIDE {
     41     queue_.reset();
     42   }
     43 
     44   // TouchEventQueueClient
     45   virtual void SendTouchEventImmediately(
     46       const TouchEventWithLatencyInfo& event) OVERRIDE {
     47     ++sent_event_count_;
     48     last_sent_event_ = event.event;
     49     if (sync_ack_result_)
     50       SendTouchEventACK(*sync_ack_result_.Pass());
     51   }
     52 
     53   virtual void OnTouchEventAck(
     54       const TouchEventWithLatencyInfo& event,
     55       InputEventAckState ack_result) OVERRIDE {
     56     ++acked_event_count_;
     57     last_acked_event_ = event.event;
     58     last_acked_event_state_ = ack_result;
     59     if (followup_touch_event_) {
     60       scoped_ptr<WebTouchEvent> followup_touch_event =
     61           followup_touch_event_.Pass();
     62       SendTouchEvent(*followup_touch_event);
     63     }
     64     if (followup_gesture_event_) {
     65       scoped_ptr<WebGestureEvent> followup_gesture_event =
     66           followup_gesture_event_.Pass();
     67       queue_->OnGestureScrollEvent(
     68           GestureEventWithLatencyInfo(*followup_gesture_event,
     69                                       ui::LatencyInfo()));
     70     }
     71   }
     72 
     73  protected:
     74 
     75   void SetUpForTimeoutTesting(size_t timeout_delay_ms) {
     76     queue_->SetAckTimeoutEnabled(true, timeout_delay_ms);
     77   }
     78 
     79   void SendTouchEvent(const WebTouchEvent& event) {
     80     queue_->QueueEvent(TouchEventWithLatencyInfo(event, ui::LatencyInfo()));
     81   }
     82 
     83   void SendGestureEvent(WebInputEvent::Type type) {
     84     WebGestureEvent event;
     85     event.type = type;
     86     queue_->OnGestureScrollEvent(
     87         GestureEventWithLatencyInfo(event, ui::LatencyInfo()));
     88   }
     89 
     90   void SendTouchEventACK(InputEventAckState ack_result) {
     91     queue_->ProcessTouchAck(ack_result, ui::LatencyInfo());
     92   }
     93 
     94   void SetFollowupEvent(const WebTouchEvent& event) {
     95     followup_touch_event_.reset(new WebTouchEvent(event));
     96   }
     97 
     98   void SetFollowupEvent(const WebGestureEvent& event) {
     99     followup_gesture_event_.reset(new WebGestureEvent(event));
    100   }
    101 
    102   void SetSyncAckResult(InputEventAckState sync_ack_result) {
    103     sync_ack_result_.reset(new InputEventAckState(sync_ack_result));
    104   }
    105 
    106   void PressTouchPoint(int x, int y) {
    107     touch_event_.PressPoint(x, y);
    108     SendTouchEvent();
    109   }
    110 
    111   void MoveTouchPoint(int index, int x, int y) {
    112     touch_event_.MovePoint(index, x, y);
    113     SendTouchEvent();
    114   }
    115 
    116   void MoveTouchPoints(int index0, int x0, int y0, int index1, int x1, int y1) {
    117     touch_event_.MovePoint(index0, x0, y0);
    118     touch_event_.MovePoint(index1, x1, y1);
    119     SendTouchEvent();
    120   }
    121 
    122   void ReleaseTouchPoint(int index) {
    123     touch_event_.ReleasePoint(index);
    124     SendTouchEvent();
    125   }
    126 
    127   void CancelTouchPoint(int index) {
    128     touch_event_.CancelPoint(index);
    129     SendTouchEvent();
    130   }
    131 
    132   size_t GetAndResetAckedEventCount() {
    133     size_t count = acked_event_count_;
    134     acked_event_count_ = 0;
    135     return count;
    136   }
    137 
    138   size_t GetAndResetSentEventCount() {
    139     size_t count = sent_event_count_;
    140     sent_event_count_ = 0;
    141     return count;
    142   }
    143 
    144   bool IsPendingAckTouchStart() const {
    145     return queue_->IsPendingAckTouchStart();
    146   }
    147 
    148   void Flush() {
    149     queue_->FlushQueue();
    150   }
    151 
    152   void SetEnableTouchForwarding(bool enabled) {
    153     queue_->no_touch_to_renderer_ = !enabled;
    154   }
    155 
    156   bool WillForwardTouchEvents() {
    157     return !queue_->no_touch_to_renderer_ && !queue_->HasTimeoutEvent();
    158   }
    159 
    160   bool IsTimeoutRunning() {
    161     return queue_->IsTimeoutRunningForTesting();
    162   }
    163 
    164   size_t queued_event_count() const {
    165     return queue_->size();
    166   }
    167 
    168   const WebTouchEvent& latest_event() const {
    169     return queue_->GetLatestEventForTesting().event;
    170   }
    171 
    172   const WebTouchEvent& acked_event() const {
    173     return last_acked_event_;
    174   }
    175 
    176   const WebTouchEvent& sent_event() const {
    177     return last_sent_event_;
    178   }
    179 
    180   InputEventAckState acked_event_state() const {
    181     return last_acked_event_state_;
    182   }
    183 
    184   void set_no_touch_to_renderer(bool no_touch) {
    185     queue_->no_touch_to_renderer_ = no_touch;
    186   }
    187 
    188   bool no_touch_to_renderer() const {
    189     return queue_->no_touch_to_renderer_;
    190   }
    191 
    192  private:
    193   void SendTouchEvent() {
    194     SendTouchEvent(touch_event_);
    195     touch_event_.ResetPoints();
    196   }
    197 
    198   scoped_ptr<TouchEventQueue> queue_;
    199   size_t sent_event_count_;
    200   size_t acked_event_count_;
    201   WebTouchEvent last_sent_event_;
    202   WebTouchEvent last_acked_event_;
    203   InputEventAckState last_acked_event_state_;
    204   SyntheticWebTouchEvent touch_event_;
    205   scoped_ptr<WebTouchEvent> followup_touch_event_;
    206   scoped_ptr<WebGestureEvent> followup_gesture_event_;
    207   scoped_ptr<InputEventAckState> sync_ack_result_;
    208   base::MessageLoopForUI message_loop_;
    209 };
    210 
    211 
    212 // Tests that touch-events are queued properly.
    213 TEST_F(TouchEventQueueTest, Basic) {
    214   PressTouchPoint(1, 1);
    215   EXPECT_EQ(1U, queued_event_count());
    216   EXPECT_EQ(1U, GetAndResetSentEventCount());
    217 
    218   // The second touch should not be sent since one is already in queue.
    219   MoveTouchPoint(0, 5, 5);
    220   EXPECT_EQ(2U, queued_event_count());
    221   EXPECT_EQ(0U, GetAndResetSentEventCount());
    222 
    223   // Receive an ACK for the first touch-event.
    224   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    225   EXPECT_EQ(1U, queued_event_count());
    226   EXPECT_EQ(1U, GetAndResetSentEventCount());
    227   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    228   EXPECT_EQ(WebInputEvent::TouchStart, acked_event().type);
    229 
    230   // Receive an ACK for the second touch-event.
    231   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    232   EXPECT_EQ(0U, queued_event_count());
    233   EXPECT_EQ(0U, GetAndResetSentEventCount());
    234   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    235   EXPECT_EQ(WebInputEvent::TouchMove, acked_event().type);
    236 }
    237 
    238 // Tests that the touch-queue is emptied if a page stops listening for touch
    239 // events.
    240 TEST_F(TouchEventQueueTest, Flush) {
    241   Flush();
    242   EXPECT_EQ(0U, queued_event_count());
    243   EXPECT_EQ(0U, GetAndResetSentEventCount());
    244 
    245   // Send a touch-press event.
    246   PressTouchPoint(1, 1);
    247   EXPECT_EQ(1U, GetAndResetSentEventCount());
    248 
    249   ReleaseTouchPoint(0);
    250 
    251   // Events will be queued until the first sent event is ack'ed.
    252   for (int i = 5; i < 15; ++i) {
    253     PressTouchPoint(1, 1);
    254     MoveTouchPoint(0, i, i);
    255     ReleaseTouchPoint(0);
    256   }
    257   EXPECT_EQ(32U, queued_event_count());
    258   EXPECT_EQ(0U, GetAndResetSentEventCount());
    259 
    260   // Receive an ACK for the first touch-event. One of the queued touch-event
    261   // should be forwarded.
    262   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    263   EXPECT_EQ(31U, queued_event_count());
    264   EXPECT_EQ(1U, GetAndResetSentEventCount());
    265   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    266   EXPECT_EQ(WebInputEvent::TouchStart, acked_event().type);
    267 
    268   // Flush the queue. The touch-event queue should now be emptied, but none of
    269   // the queued touch-events should be sent to the renderer.
    270   Flush();
    271   EXPECT_EQ(0U, queued_event_count());
    272   EXPECT_EQ(0U, GetAndResetSentEventCount());
    273   EXPECT_EQ(31U, GetAndResetAckedEventCount());
    274 }
    275 
    276 // Tests that touch-events are coalesced properly in the queue.
    277 TEST_F(TouchEventQueueTest, Coalesce) {
    278   // Send a touch-press event.
    279   PressTouchPoint(1, 1);
    280   EXPECT_EQ(1U, GetAndResetSentEventCount());
    281 
    282   // Send a few touch-move events, followed by a touch-release event. All the
    283   // touch-move events should be coalesced into a single event.
    284   for (int i = 5; i < 15; ++i)
    285     MoveTouchPoint(0, i, i);
    286 
    287   EXPECT_EQ(0U, GetAndResetSentEventCount());
    288   ReleaseTouchPoint(0);
    289   EXPECT_EQ(0U, GetAndResetSentEventCount());
    290   EXPECT_EQ(3U, queued_event_count());
    291 
    292   // ACK the press.  Coalesced touch-move events should be sent.
    293   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    294   EXPECT_EQ(2U, queued_event_count());
    295   EXPECT_EQ(1U, GetAndResetSentEventCount());
    296   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    297   EXPECT_EQ(WebInputEvent::TouchStart, acked_event().type);
    298   EXPECT_EQ(INPUT_EVENT_ACK_STATE_CONSUMED, acked_event_state());
    299 
    300   // ACK the moves.
    301   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    302   EXPECT_EQ(1U, queued_event_count());
    303   EXPECT_EQ(1U, GetAndResetSentEventCount());
    304   EXPECT_EQ(10U, GetAndResetAckedEventCount());
    305   EXPECT_EQ(WebInputEvent::TouchMove, acked_event().type);
    306 
    307   // ACK the release.
    308   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    309   EXPECT_EQ(0U, queued_event_count());
    310   EXPECT_EQ(0U, GetAndResetSentEventCount());
    311   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    312   EXPECT_EQ(WebInputEvent::TouchEnd, acked_event().type);
    313 }
    314 
    315 // Tests that an event that has already been sent but hasn't been ack'ed yet
    316 // doesn't get coalesced with newer events.
    317 TEST_F(TouchEventQueueTest, SentTouchEventDoesNotCoalesce) {
    318   // Send a touch-press event.
    319   PressTouchPoint(1, 1);
    320   EXPECT_EQ(1U, GetAndResetSentEventCount());
    321 
    322   // Send a few touch-move events, followed by a touch-release event. All the
    323   // touch-move events should be coalesced into a single event.
    324   for (int i = 5; i < 15; ++i)
    325     MoveTouchPoint(0, i, i);
    326 
    327   EXPECT_EQ(0U, GetAndResetSentEventCount());
    328   EXPECT_EQ(2U, queued_event_count());
    329 
    330   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    331   EXPECT_EQ(1U, GetAndResetSentEventCount());
    332   EXPECT_EQ(1U, queued_event_count());
    333 
    334   // The coalesced touch-move event has been sent to the renderer. Any new
    335   // touch-move event should not be coalesced with the sent event.
    336   MoveTouchPoint(0, 5, 5);
    337   EXPECT_EQ(2U, queued_event_count());
    338 
    339   MoveTouchPoint(0, 7, 7);
    340   EXPECT_EQ(2U, queued_event_count());
    341 }
    342 
    343 // Tests that coalescing works correctly for multi-touch events.
    344 TEST_F(TouchEventQueueTest, MultiTouch) {
    345   // Press the first finger.
    346   PressTouchPoint(1, 1);
    347   EXPECT_EQ(1U, GetAndResetSentEventCount());
    348 
    349   // Move the finger.
    350   MoveTouchPoint(0, 5, 5);
    351   EXPECT_EQ(2U, queued_event_count());
    352 
    353   // Now press a second finger.
    354   PressTouchPoint(2, 2);
    355   EXPECT_EQ(3U, queued_event_count());
    356 
    357   // Move both fingers.
    358   MoveTouchPoints(0, 10, 10, 1, 20, 20);
    359   MoveTouchPoint(1, 20, 20);
    360   EXPECT_EQ(4U, queued_event_count());
    361 
    362   // Move only one finger now.
    363   MoveTouchPoint(0, 15, 15);
    364   EXPECT_EQ(4U, queued_event_count());
    365 
    366   // Move the other finger.
    367   MoveTouchPoint(1, 25, 25);
    368   EXPECT_EQ(4U, queued_event_count());
    369 
    370   // Make sure both fingers are marked as having been moved in the coalesced
    371   // event.
    372   const WebTouchEvent& event = latest_event();
    373   EXPECT_EQ(WebTouchPoint::StateMoved, event.touches[0].state);
    374   EXPECT_EQ(WebTouchPoint::StateMoved, event.touches[1].state);
    375 }
    376 
    377 // Tests that if a touch-event queue is destroyed in response to a touch-event
    378 // in the renderer, then there is no crash when the ACK for that touch-event
    379 // comes back.
    380 TEST_F(TouchEventQueueTest, AckAfterQueueFlushed) {
    381   // Send some touch-events to the renderer.
    382   PressTouchPoint(1, 1);
    383   EXPECT_EQ(1U, GetAndResetSentEventCount());
    384   EXPECT_EQ(1U, queued_event_count());
    385 
    386   MoveTouchPoint(0, 10, 10);
    387   EXPECT_EQ(0U, GetAndResetSentEventCount());
    388   EXPECT_EQ(2U, queued_event_count());
    389 
    390   // Receive an ACK for the press. This should cause the queued touch-move to
    391   // be sent to the renderer.
    392   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    393   EXPECT_EQ(1U, GetAndResetSentEventCount());
    394   EXPECT_EQ(1U, queued_event_count());
    395 
    396   Flush();
    397   EXPECT_EQ(0U, GetAndResetSentEventCount());
    398   EXPECT_EQ(0U, queued_event_count());
    399 
    400   // Now receive an ACK for the move.
    401   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    402   EXPECT_EQ(0U, GetAndResetSentEventCount());
    403   EXPECT_EQ(0U, queued_event_count());
    404 }
    405 
    406 // Tests that touch-move events are not sent to the renderer if the preceding
    407 // touch-press event did not have a consumer (and consequently, did not hit the
    408 // main thread in the renderer). Also tests that all queued/coalesced touch
    409 // events are flushed immediately when the ACK for the touch-press comes back
    410 // with NO_CONSUMER status.
    411 TEST_F(TouchEventQueueTest, NoConsumer) {
    412   // The first touch-press should reach the renderer.
    413   PressTouchPoint(1, 1);
    414   EXPECT_EQ(1U, GetAndResetSentEventCount());
    415 
    416   // The second touch should not be sent since one is already in queue.
    417   MoveTouchPoint(0, 5, 5);
    418   EXPECT_EQ(0U, GetAndResetSentEventCount());
    419   EXPECT_EQ(2U, queued_event_count());
    420 
    421   // Receive an ACK for the first touch-event. This should release the queued
    422   // touch-event, but it should not be sent to the renderer.
    423   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
    424   EXPECT_EQ(0U, queued_event_count());
    425   EXPECT_EQ(WebInputEvent::TouchMove, acked_event().type);
    426   EXPECT_EQ(2U, GetAndResetAckedEventCount());
    427   EXPECT_EQ(0U, GetAndResetSentEventCount());
    428 
    429   // Send a release event. This should not reach the renderer.
    430   ReleaseTouchPoint(0);
    431   EXPECT_EQ(0U, GetAndResetSentEventCount());
    432   EXPECT_EQ(WebInputEvent::TouchEnd, acked_event().type);
    433   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    434 
    435   // Send a press-event, followed by move and release events, and another press
    436   // event, before the ACK for the first press event comes back. All of the
    437   // events should be queued first. After the NO_CONSUMER ack for the first
    438   // touch-press, all events upto the second touch-press should be flushed.
    439   PressTouchPoint(10, 10);
    440   EXPECT_EQ(1U, GetAndResetSentEventCount());
    441 
    442   MoveTouchPoint(0, 5, 5);
    443   MoveTouchPoint(0, 6, 5);
    444   ReleaseTouchPoint(0);
    445 
    446   PressTouchPoint(6, 5);
    447   EXPECT_EQ(0U, GetAndResetSentEventCount());
    448   // The queue should hold the first sent touch-press event, the coalesced
    449   // touch-move event, the touch-end event and the second touch-press event.
    450   EXPECT_EQ(4U, queued_event_count());
    451 
    452   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
    453   EXPECT_EQ(1U, GetAndResetSentEventCount());
    454   EXPECT_EQ(WebInputEvent::TouchEnd, acked_event().type);
    455   EXPECT_EQ(4U, GetAndResetAckedEventCount());
    456   EXPECT_EQ(1U, queued_event_count());
    457 
    458   // ACK the second press event as NO_CONSUMER too.
    459   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
    460   EXPECT_EQ(0U, GetAndResetSentEventCount());
    461   EXPECT_EQ(WebInputEvent::TouchStart, acked_event().type);
    462   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    463   EXPECT_EQ(0U, queued_event_count());
    464 
    465   // Send a second press event. Even though the first touch had NO_CONSUMER,
    466   // this press event should reach the renderer.
    467   PressTouchPoint(1, 1);
    468   EXPECT_EQ(1U, GetAndResetSentEventCount());
    469   EXPECT_EQ(1U, queued_event_count());
    470 }
    471 
    472 TEST_F(TouchEventQueueTest, ConsumerIgnoreMultiFinger) {
    473   // Press two touch points and move them around a bit. The renderer consumes
    474   // the events for the first touch point, but returns NO_CONSUMER_EXISTS for
    475   // the second touch point.
    476 
    477   PressTouchPoint(1, 1);
    478   EXPECT_EQ(1U, GetAndResetSentEventCount());
    479 
    480   MoveTouchPoint(0, 5, 5);
    481 
    482   PressTouchPoint(10, 10);
    483 
    484   MoveTouchPoint(0, 2, 2);
    485 
    486   MoveTouchPoint(1, 4, 10);
    487 
    488   MoveTouchPoints(0, 10, 10, 1, 20, 20);
    489 
    490   // Since the first touch-press is still pending ACK, no other event should
    491   // have been sent to the renderer.
    492   EXPECT_EQ(0U, GetAndResetSentEventCount());
    493   // The queue includes the two presses, the first touch-move of the first
    494   // point, and a coalesced touch-move of both points.
    495   EXPECT_EQ(4U, queued_event_count());
    496 
    497   // ACK the first press as CONSUMED. This should cause the first touch-move of
    498   // the first touch-point to be dispatched.
    499   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    500   EXPECT_EQ(1U, GetAndResetSentEventCount());
    501   EXPECT_EQ(3U, queued_event_count());
    502 
    503   // ACK the first move as CONSUMED.
    504   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    505   EXPECT_EQ(1U, GetAndResetSentEventCount());
    506   EXPECT_EQ(2U, queued_event_count());
    507 
    508   // ACK the second press as NO_CONSUMER_EXISTS. This will dequeue the coalesced
    509   // touch-move event (which contains both touch points). Although the second
    510   // touch-point does not need to be sent to the renderer, the first touch-point
    511   // did move, and so the coalesced touch-event will be sent to the renderer.
    512   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
    513   EXPECT_EQ(1U, GetAndResetSentEventCount());
    514   EXPECT_EQ(1U, queued_event_count());
    515 
    516   // ACK the coalesced move as NOT_CONSUMED.
    517   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    518   EXPECT_EQ(0U, GetAndResetSentEventCount());
    519   EXPECT_EQ(0U, queued_event_count());
    520 
    521   // Move just the second touch point. Because the first touch point did not
    522   // move, this event should not reach the renderer.
    523   MoveTouchPoint(1, 30, 30);
    524   EXPECT_EQ(0U, GetAndResetSentEventCount());
    525   EXPECT_EQ(0U, queued_event_count());
    526 
    527   // Move just the first touch point. This should reach the renderer.
    528   MoveTouchPoint(0, 10, 10);
    529   EXPECT_EQ(1U, GetAndResetSentEventCount());
    530   EXPECT_EQ(1U, queued_event_count());
    531 
    532   // Move both fingers. This event should reach the renderer (after the ACK of
    533   // the previous move event is received), because the first touch point did
    534   // move.
    535   MoveTouchPoints(0, 15, 15, 1, 25, 25);
    536   EXPECT_EQ(0U, GetAndResetSentEventCount());
    537 
    538   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    539   EXPECT_EQ(1U, GetAndResetSentEventCount());
    540   EXPECT_EQ(1U, queued_event_count());
    541 
    542   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    543   EXPECT_EQ(0U, GetAndResetSentEventCount());
    544   EXPECT_EQ(0U, queued_event_count());
    545 
    546   // Release the first finger. Then move the second finger around some, then
    547   // press another finger. Once the release event is ACKed, the move events of
    548   // the second finger should be immediately released to the view, and the
    549   // touch-press event should be dispatched to the renderer.
    550   ReleaseTouchPoint(0);
    551   EXPECT_EQ(1U, GetAndResetSentEventCount());
    552   EXPECT_EQ(1U, queued_event_count());
    553 
    554   MoveTouchPoint(1, 40, 40);
    555 
    556   MoveTouchPoint(1, 50, 50);
    557 
    558   PressTouchPoint(1, 1);
    559 
    560   MoveTouchPoint(1, 30, 30);
    561   EXPECT_EQ(0U, GetAndResetSentEventCount());
    562   EXPECT_EQ(4U, queued_event_count());
    563 
    564   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    565   EXPECT_EQ(1U, GetAndResetSentEventCount());
    566   EXPECT_EQ(2U, queued_event_count());
    567   EXPECT_EQ(WebInputEvent::TouchMove, acked_event().type);
    568 
    569   // ACK the press with NO_CONSUMED_EXISTS. This should release the queued
    570   // touch-move events to the view.
    571   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
    572   EXPECT_EQ(0U, GetAndResetSentEventCount());
    573   EXPECT_EQ(0U, queued_event_count());
    574   EXPECT_EQ(WebInputEvent::TouchMove, acked_event().type);
    575 
    576   ReleaseTouchPoint(2);
    577   ReleaseTouchPoint(1);
    578   EXPECT_EQ(0U, GetAndResetSentEventCount());
    579   EXPECT_EQ(0U, queued_event_count());
    580 }
    581 
    582 // Tests that touch-event's enqueued via a touch ack are properly handled.
    583 TEST_F(TouchEventQueueTest, AckWithFollowupEvents) {
    584   // Queue a touch down.
    585   PressTouchPoint(1, 1);
    586   EXPECT_EQ(1U, queued_event_count());
    587   EXPECT_EQ(1U, GetAndResetSentEventCount());
    588   EXPECT_EQ(0U, GetAndResetAckedEventCount());
    589 
    590   // Create a touch event that will be queued synchronously by a touch ack.
    591   // Note, this will be triggered by all subsequent touch acks.
    592   WebTouchEvent followup_event;
    593   followup_event.type = WebInputEvent::TouchStart;
    594   followup_event.touchesLength = 1;
    595   followup_event.touches[0].id = 1;
    596   followup_event.touches[0].state = WebTouchPoint::StatePressed;
    597   SetFollowupEvent(followup_event);
    598 
    599   // Receive an ACK for the press. This should cause the followup touch-move to
    600   // be sent to the renderer.
    601   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    602   EXPECT_EQ(1U, queued_event_count());
    603   EXPECT_EQ(1U, GetAndResetSentEventCount());
    604   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    605   EXPECT_EQ(INPUT_EVENT_ACK_STATE_CONSUMED, acked_event_state());
    606   EXPECT_EQ(WebInputEvent::TouchStart, acked_event().type);
    607 
    608   // Queue another event.
    609   MoveTouchPoint(0, 2, 2);
    610   EXPECT_EQ(2U, queued_event_count());
    611 
    612   // Receive an ACK for the touch-move followup event. This should cause the
    613   // subsequent touch move event be sent to the renderer.
    614   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    615   EXPECT_EQ(1U, queued_event_count());
    616   EXPECT_EQ(1U, GetAndResetSentEventCount());
    617   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    618 }
    619 
    620 // Tests that touch-events can be synchronously ack'ed.
    621 TEST_F(TouchEventQueueTest, SynchronousAcks) {
    622   // TouchStart
    623   SetSyncAckResult(INPUT_EVENT_ACK_STATE_CONSUMED);
    624   PressTouchPoint(1, 1);
    625   EXPECT_EQ(0U, queued_event_count());
    626   EXPECT_EQ(1U, GetAndResetSentEventCount());
    627   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    628 
    629   // TouchMove
    630   SetSyncAckResult(INPUT_EVENT_ACK_STATE_CONSUMED);
    631   MoveTouchPoint(0, 2, 2);
    632   EXPECT_EQ(0U, queued_event_count());
    633   EXPECT_EQ(1U, GetAndResetSentEventCount());
    634   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    635 
    636   // TouchEnd
    637   SetSyncAckResult(INPUT_EVENT_ACK_STATE_CONSUMED);
    638   ReleaseTouchPoint(0);
    639   EXPECT_EQ(0U, queued_event_count());
    640   EXPECT_EQ(1U, GetAndResetSentEventCount());
    641   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    642 
    643   // TouchCancel (first inserting a TouchStart so the TouchCancel will be sent)
    644   PressTouchPoint(1, 1);
    645   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    646   EXPECT_EQ(0U, queued_event_count());
    647   EXPECT_EQ(1U, GetAndResetSentEventCount());
    648   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    649 
    650   SetSyncAckResult(INPUT_EVENT_ACK_STATE_CONSUMED);
    651   CancelTouchPoint(0);
    652   EXPECT_EQ(0U, queued_event_count());
    653   EXPECT_EQ(1U, GetAndResetSentEventCount());
    654   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    655 }
    656 
    657 // Tests that followup events triggered by an immediate ack from
    658 // TouchEventQueue::QueueEvent() are properly handled.
    659 TEST_F(TouchEventQueueTest, ImmediateAckWithFollowupEvents) {
    660   // Create a touch event that will be queued synchronously by a touch ack.
    661   WebTouchEvent followup_event;
    662   followup_event.type = WebInputEvent::TouchStart;
    663   followup_event.touchesLength = 1;
    664   followup_event.touches[0].id = 1;
    665   followup_event.touches[0].state = WebTouchPoint::StatePressed;
    666   SetFollowupEvent(followup_event);
    667 
    668   // Now, enqueue a stationary touch that will not be forwarded.  This should be
    669   // immediately ack'ed with "NO_CONSUMER_EXISTS".  The followup event should
    670   // then be enqueued and immediately sent to the renderer.
    671   WebTouchEvent stationary_event;
    672   stationary_event.touchesLength = 1;
    673   stationary_event.type = WebInputEvent::TouchMove;
    674   stationary_event.touches[0].id = 1;
    675   stationary_event.touches[0].state = WebTouchPoint::StateStationary;
    676   SendTouchEvent(stationary_event);
    677 
    678   EXPECT_EQ(1U, queued_event_count());
    679   EXPECT_EQ(1U, GetAndResetSentEventCount());
    680   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    681   EXPECT_EQ(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, acked_event_state());
    682   EXPECT_EQ(WebInputEvent::TouchMove, acked_event().type);
    683 }
    684 
    685 // Tests basic TouchEvent forwarding suppression.
    686 TEST_F(TouchEventQueueTest, NoTouchBasic) {
    687   // Disable TouchEvent forwarding.
    688   SetEnableTouchForwarding(false);
    689   MoveTouchPoint(0, 30, 5);
    690   EXPECT_EQ(0U, GetAndResetSentEventCount());
    691   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    692 
    693   // TouchMove should not be sent to renderer.
    694   MoveTouchPoint(0, 65, 10);
    695   EXPECT_EQ(0U, GetAndResetSentEventCount());
    696   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    697 
    698   // TouchEnd should not be sent to renderer.
    699   ReleaseTouchPoint(0);
    700   EXPECT_EQ(0U, GetAndResetSentEventCount());
    701   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    702 
    703   // TouchStart should not be sent to renderer.
    704   PressTouchPoint(5, 5);
    705   EXPECT_EQ(0U, GetAndResetSentEventCount());
    706   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    707 
    708   // Enable TouchEvent forwarding.
    709   SetEnableTouchForwarding(true);
    710 
    711   PressTouchPoint(80, 10);
    712   EXPECT_EQ(1U, GetAndResetSentEventCount());
    713   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    714   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    715 
    716   MoveTouchPoint(0, 80, 20);
    717   EXPECT_EQ(1U, GetAndResetSentEventCount());
    718   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    719   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    720 
    721   ReleaseTouchPoint(0);
    722   EXPECT_EQ(1U, GetAndResetSentEventCount());
    723   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    724   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    725 }
    726 
    727 // Tests that no TouchEvents are sent to renderer during scrolling.
    728 TEST_F(TouchEventQueueTest, NoTouchOnScroll) {
    729   // Queue a TouchStart.
    730   PressTouchPoint(0, 1);
    731   EXPECT_EQ(1U, GetAndResetSentEventCount());
    732   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    733   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    734 
    735   MoveTouchPoint(0, 20, 5);
    736   EXPECT_EQ(1U, queued_event_count());
    737   EXPECT_EQ(1U, GetAndResetSentEventCount());
    738 
    739   // Queue another TouchStart.
    740   PressTouchPoint(20, 20);
    741   EXPECT_EQ(2U, queued_event_count());
    742   EXPECT_EQ(0U, GetAndResetSentEventCount());
    743   EXPECT_EQ(WebInputEvent::TouchStart, latest_event().type);
    744 
    745   // GestureScrollBegin inserts a synthetic TouchCancel before the TouchStart.
    746   WebGestureEvent followup_scroll;
    747   followup_scroll.type = WebInputEvent::GestureScrollBegin;
    748   SetFollowupEvent(followup_scroll);
    749   ASSERT_TRUE(WillForwardTouchEvents());
    750   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    751   EXPECT_FALSE(WillForwardTouchEvents());
    752   EXPECT_EQ(1U, GetAndResetSentEventCount());
    753   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    754   EXPECT_EQ(2U, queued_event_count());
    755   EXPECT_EQ(WebInputEvent::TouchCancel, sent_event().type);
    756   EXPECT_EQ(WebInputEvent::TouchStart, latest_event().type);
    757 
    758   // Acking the TouchCancel will result in dispatch of the next TouchStart.
    759   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    760   // The synthetic TouchCancel should not reach client, only the TouchStart.
    761   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    762   EXPECT_EQ(0U, GetAndResetSentEventCount());
    763   EXPECT_EQ(WebInputEvent::TouchStart, acked_event().type);
    764 
    765   // TouchMove should not be sent to the renderer.
    766   MoveTouchPoint(0, 30, 5);
    767   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    768   EXPECT_EQ(0U, GetAndResetSentEventCount());
    769   EXPECT_EQ(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, acked_event_state());
    770 
    771   // GestureScrollUpdates should not change affect touch forwarding.
    772   SendGestureEvent(WebInputEvent::GestureScrollUpdate);
    773   EXPECT_FALSE(WillForwardTouchEvents());
    774 
    775   // TouchEnd should not be sent to the renderer.
    776   ReleaseTouchPoint(0);
    777   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    778   EXPECT_EQ(0U, GetAndResetSentEventCount());
    779   EXPECT_EQ(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, acked_event_state());
    780 
    781   // GestureScrollEnd will resume the sending of TouchEvents to renderer.
    782   SendGestureEvent(blink::WebInputEvent::GestureScrollEnd);
    783   EXPECT_TRUE(WillForwardTouchEvents());
    784 
    785   // Now TouchEvents should be forwarded normally.
    786   PressTouchPoint(80, 10);
    787   EXPECT_EQ(1U, GetAndResetSentEventCount());
    788   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    789   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    790 
    791   MoveTouchPoint(0, 80, 20);
    792   EXPECT_EQ(1U, GetAndResetSentEventCount());
    793   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    794   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    795 
    796   ReleaseTouchPoint(0);
    797   EXPECT_EQ(1U, GetAndResetSentEventCount());
    798   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    799   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    800 }
    801 
    802 // Tests that IsTouchStartPendingAck works correctly.
    803 TEST_F(TouchEventQueueTest, PendingStart) {
    804 
    805   EXPECT_FALSE(IsPendingAckTouchStart());
    806 
    807   // Send the touchstart for one point (#1).
    808   PressTouchPoint(1, 1);
    809   EXPECT_EQ(1U, queued_event_count());
    810   EXPECT_TRUE(IsPendingAckTouchStart());
    811 
    812   // Send a touchmove for that point (#2).
    813   MoveTouchPoint(0, 5, 5);
    814   EXPECT_EQ(2U, queued_event_count());
    815   EXPECT_TRUE(IsPendingAckTouchStart());
    816 
    817   // Ack the touchstart (#1).
    818   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    819   EXPECT_EQ(1U, queued_event_count());
    820   EXPECT_FALSE(IsPendingAckTouchStart());
    821 
    822   // Send a touchstart for another point (#3).
    823   PressTouchPoint(10, 10);
    824   EXPECT_EQ(2U, queued_event_count());
    825   EXPECT_FALSE(IsPendingAckTouchStart());
    826 
    827   // Ack the touchmove (#2).
    828   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    829   EXPECT_EQ(1U, queued_event_count());
    830   EXPECT_TRUE(IsPendingAckTouchStart());
    831 
    832   // Send a touchstart for a third point (#4).
    833   PressTouchPoint(15, 15);
    834   EXPECT_EQ(2U, queued_event_count());
    835   EXPECT_TRUE(IsPendingAckTouchStart());
    836 
    837   // Ack the touchstart for the second point (#3).
    838   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    839   EXPECT_EQ(1U, queued_event_count());
    840   EXPECT_TRUE(IsPendingAckTouchStart());
    841 
    842   // Ack the touchstart for the third point (#4).
    843   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    844   EXPECT_EQ(0U, queued_event_count());
    845   EXPECT_FALSE(IsPendingAckTouchStart());
    846 }
    847 
    848 // Tests that the touch timeout is started when sending certain touch types.
    849 TEST_F(TouchEventQueueTest, TouchTimeoutTypes) {
    850   SetUpForTimeoutTesting(kDefaultTouchTimeoutDelayMs);
    851 
    852   // Sending a TouchStart will start the timeout.
    853   PressTouchPoint(0, 1);
    854   EXPECT_TRUE(IsTimeoutRunning());
    855   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    856   EXPECT_FALSE(IsTimeoutRunning());
    857 
    858   // A TouchMove should start the timeout.
    859   MoveTouchPoint(0, 5, 5);
    860   EXPECT_TRUE(IsTimeoutRunning());
    861   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    862   EXPECT_FALSE(IsTimeoutRunning());
    863 
    864   // A TouchEnd should not start the timeout.
    865   ReleaseTouchPoint(0);
    866   EXPECT_FALSE(IsTimeoutRunning());
    867   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    868   EXPECT_FALSE(IsTimeoutRunning());
    869 
    870   // A TouchCancel should not start the timeout.
    871   PressTouchPoint(0, 1);
    872   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    873   ASSERT_FALSE(IsTimeoutRunning());
    874   CancelTouchPoint(0);
    875   EXPECT_FALSE(IsTimeoutRunning());
    876   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    877   EXPECT_FALSE(IsTimeoutRunning());
    878 }
    879 
    880 // Tests that a delayed TouchEvent ack will trigger a TouchCancel timeout,
    881 // disabling touch forwarding until the next TouchStart is received after
    882 // the timeout events are ack'ed.
    883 TEST_F(TouchEventQueueTest, TouchTimeoutBasic) {
    884   SetUpForTimeoutTesting(kDefaultTouchTimeoutDelayMs);
    885 
    886   // Queue a TouchStart.
    887   GetAndResetSentEventCount();
    888   GetAndResetAckedEventCount();
    889   PressTouchPoint(0, 1);
    890   ASSERT_EQ(1U, GetAndResetSentEventCount());
    891   ASSERT_EQ(0U, GetAndResetAckedEventCount());
    892   EXPECT_TRUE(IsTimeoutRunning());
    893   EXPECT_TRUE(WillForwardTouchEvents());
    894 
    895   // Delay the ack.
    896   base::MessageLoop::current()->PostDelayedTask(
    897       FROM_HERE,
    898       base::MessageLoop::QuitClosure(),
    899       base::TimeDelta::FromMilliseconds(kDefaultTouchTimeoutDelayMs * 2));
    900   base::MessageLoop::current()->Run();
    901 
    902   // The timeout should have fired, synthetically ack'ing the timed-out event.
    903   // TouchEvent forwarding is disabled until the ack is received for the
    904   // timed-out event and the future cancel event.
    905   EXPECT_FALSE(IsTimeoutRunning());
    906   EXPECT_FALSE(WillForwardTouchEvents());
    907   EXPECT_EQ(0U, GetAndResetSentEventCount());
    908   EXPECT_EQ(1U, GetAndResetAckedEventCount());
    909 
    910   // Ack'ing the original event should trigger a cancel event.
    911   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    912   EXPECT_FALSE(IsTimeoutRunning());
    913   EXPECT_FALSE(WillForwardTouchEvents());
    914   EXPECT_EQ(0U, GetAndResetAckedEventCount());
    915   EXPECT_EQ(1U, GetAndResetSentEventCount());
    916 
    917   // Touch events should not be forwarded until we receive the cancel acks.
    918   PressTouchPoint(0, 1);
    919   ASSERT_EQ(0U, GetAndResetSentEventCount());
    920   ASSERT_EQ(1U, GetAndResetAckedEventCount());
    921 
    922   // The synthetic TouchCancel ack should not reach the client, but should
    923   // resume touch forwarding.
    924   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    925   EXPECT_EQ(0U, GetAndResetSentEventCount());
    926   EXPECT_EQ(0U, GetAndResetAckedEventCount());
    927   EXPECT_TRUE(WillForwardTouchEvents());
    928 
    929   // Subsequent events should be handled normally.
    930   PressTouchPoint(0, 1);
    931   EXPECT_EQ(1U, GetAndResetSentEventCount());
    932   EXPECT_EQ(0U, GetAndResetAckedEventCount());
    933 }
    934 
    935 // Tests that the timeout is never started if the renderer consumes
    936 // a TouchEvent from the current touch sequence.
    937 TEST_F(TouchEventQueueTest, NoTouchTimeoutIfRendererIsConsumingGesture) {
    938   SetUpForTimeoutTesting(kDefaultTouchTimeoutDelayMs);
    939 
    940   // Queue a TouchStart.
    941   PressTouchPoint(0, 1);
    942   ASSERT_TRUE(IsTimeoutRunning());
    943 
    944   // Mark the event as consumed. This should prevent the timeout from
    945   // being activated on subsequent TouchEvents in this gesture.
    946   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
    947   EXPECT_FALSE(IsTimeoutRunning());
    948 
    949   // A TouchMove should not start the timeout.
    950   MoveTouchPoint(0, 5, 5);
    951   EXPECT_FALSE(IsTimeoutRunning());
    952   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    953 
    954   // A secondary TouchStart should not start the timeout.
    955   PressTouchPoint(1, 0);
    956   EXPECT_FALSE(IsTimeoutRunning());
    957   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    958 
    959   // A TouchEnd should not start the timeout.
    960   ReleaseTouchPoint(1);
    961   EXPECT_FALSE(IsTimeoutRunning());
    962   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    963 
    964   // A TouchCancel should not start the timeout.
    965   CancelTouchPoint(0);
    966   EXPECT_FALSE(IsTimeoutRunning());
    967 }
    968 
    969 // Tests that the timeout is never started if the ack is synchronous.
    970 TEST_F(TouchEventQueueTest, NoTouchTimeoutIfAckIsSynchronous) {
    971   SetUpForTimeoutTesting(kDefaultTouchTimeoutDelayMs);
    972 
    973   // Queue a TouchStart.
    974   SetSyncAckResult(INPUT_EVENT_ACK_STATE_CONSUMED);
    975   ASSERT_FALSE(IsTimeoutRunning());
    976   PressTouchPoint(0, 1);
    977   EXPECT_FALSE(IsTimeoutRunning());
    978 }
    979 
    980 // Tests that a TouchCancel timeout plays nice when the timed out touch stream
    981 // turns into a scroll gesture sequence.
    982 TEST_F(TouchEventQueueTest, TouchTimeoutWithFollowupGesture) {
    983   SetUpForTimeoutTesting(kDefaultTouchTimeoutDelayMs);
    984 
    985   // Queue a TouchStart.
    986   PressTouchPoint(0, 1);
    987   EXPECT_TRUE(IsTimeoutRunning());
    988   EXPECT_TRUE(WillForwardTouchEvents());
    989   EXPECT_EQ(1U, GetAndResetSentEventCount());
    990 
    991   // The cancelled sequence may turn into a scroll gesture.
    992   WebGestureEvent followup_scroll;
    993   followup_scroll.type = WebInputEvent::GestureScrollBegin;
    994   SetFollowupEvent(followup_scroll);
    995 
    996   // Delay the ack.
    997   base::MessageLoop::current()->PostDelayedTask(
    998       FROM_HERE,
    999       base::MessageLoop::QuitClosure(),
   1000       base::TimeDelta::FromMilliseconds(kDefaultTouchTimeoutDelayMs * 2));
   1001   base::MessageLoop::current()->Run();
   1002 
   1003   // The timeout should have fired, disabling touch forwarding until both acks
   1004   // are received, acking the timed out event.
   1005   EXPECT_FALSE(IsTimeoutRunning());
   1006   EXPECT_FALSE(WillForwardTouchEvents());
   1007   EXPECT_EQ(0U, GetAndResetSentEventCount());
   1008   EXPECT_EQ(1U, GetAndResetAckedEventCount());
   1009 
   1010   // Ack the original event, triggering a TouchCancel.
   1011   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
   1012   EXPECT_FALSE(IsTimeoutRunning());
   1013   EXPECT_FALSE(WillForwardTouchEvents());
   1014   EXPECT_EQ(1U, GetAndResetSentEventCount());
   1015   EXPECT_EQ(0U, GetAndResetAckedEventCount());
   1016 
   1017   // Ack the cancel event.  Normally, this would resume touch forwarding,
   1018   // but we're still within a scroll gesture so it remains disabled.
   1019   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
   1020   EXPECT_FALSE(IsTimeoutRunning());
   1021   EXPECT_FALSE(WillForwardTouchEvents());
   1022   EXPECT_EQ(0U, GetAndResetSentEventCount());
   1023   EXPECT_EQ(0U, GetAndResetAckedEventCount());
   1024 
   1025   // Try to forward a touch event.
   1026   GetAndResetSentEventCount();
   1027   GetAndResetAckedEventCount();
   1028   PressTouchPoint(0, 1);
   1029   EXPECT_FALSE(IsTimeoutRunning());
   1030   EXPECT_EQ(0U, GetAndResetSentEventCount());
   1031   EXPECT_EQ(1U, GetAndResetAckedEventCount());
   1032 
   1033   // Now end the scroll sequence, resuming touch handling.
   1034   SendGestureEvent(blink::WebInputEvent::GestureScrollEnd);
   1035   EXPECT_TRUE(WillForwardTouchEvents());
   1036   PressTouchPoint(0, 1);
   1037   EXPECT_TRUE(IsTimeoutRunning());
   1038   EXPECT_EQ(1U, GetAndResetSentEventCount());
   1039   EXPECT_EQ(0U, GetAndResetAckedEventCount());
   1040 }
   1041 
   1042 // Tests that a TouchCancel timeout plays nice when the timed out touch stream
   1043 // turns into a scroll gesture sequence, but the original event acks are
   1044 // significantly delayed.
   1045 TEST_F(TouchEventQueueTest, TouchTimeoutWithFollowupGestureAndDelayedAck) {
   1046   SetUpForTimeoutTesting(kDefaultTouchTimeoutDelayMs);
   1047 
   1048   // Queue a TouchStart.
   1049   PressTouchPoint(0, 1);
   1050   EXPECT_TRUE(IsTimeoutRunning());
   1051   EXPECT_TRUE(WillForwardTouchEvents());
   1052   EXPECT_EQ(1U, GetAndResetSentEventCount());
   1053 
   1054   // The cancelled sequence may turn into a scroll gesture.
   1055   WebGestureEvent followup_scroll;
   1056   followup_scroll.type = WebInputEvent::GestureScrollBegin;
   1057   SetFollowupEvent(followup_scroll);
   1058 
   1059   // Delay the ack.
   1060   base::MessageLoop::current()->PostDelayedTask(
   1061       FROM_HERE,
   1062       base::MessageLoop::QuitClosure(),
   1063       base::TimeDelta::FromMilliseconds(kDefaultTouchTimeoutDelayMs * 2));
   1064   base::MessageLoop::current()->Run();
   1065 
   1066   // The timeout should have fired, disabling touch forwarding until both acks
   1067   // are received and acking the timed out event.
   1068   EXPECT_FALSE(IsTimeoutRunning());
   1069   EXPECT_FALSE(WillForwardTouchEvents());
   1070   EXPECT_EQ(0U, GetAndResetSentEventCount());
   1071   EXPECT_EQ(1U, GetAndResetAckedEventCount());
   1072 
   1073   // Try to forward a touch event.
   1074   GetAndResetSentEventCount();
   1075   GetAndResetAckedEventCount();
   1076   PressTouchPoint(0, 1);
   1077   EXPECT_FALSE(IsTimeoutRunning());
   1078   EXPECT_EQ(0U, GetAndResetSentEventCount());
   1079   EXPECT_EQ(1U, GetAndResetAckedEventCount());
   1080 
   1081   // Now end the scroll sequence.  Events will not be forwarded until the two
   1082   // outstanding touch acks are received.
   1083   SendGestureEvent(blink::WebInputEvent::GestureScrollEnd);
   1084   PressTouchPoint(0, 1);
   1085   EXPECT_FALSE(IsTimeoutRunning());
   1086   EXPECT_EQ(0U, GetAndResetSentEventCount());
   1087   EXPECT_EQ(1U, GetAndResetAckedEventCount());
   1088 
   1089   // Ack the original event, triggering a cancel.
   1090   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
   1091   EXPECT_EQ(1U, GetAndResetSentEventCount());
   1092   EXPECT_EQ(0U, GetAndResetAckedEventCount());
   1093 
   1094   // Ack the cancel event, resuming touch forwarding.
   1095   SendTouchEventACK(INPUT_EVENT_ACK_STATE_CONSUMED);
   1096   EXPECT_EQ(0U, GetAndResetSentEventCount());
   1097   EXPECT_EQ(0U, GetAndResetAckedEventCount());
   1098 
   1099   PressTouchPoint(0, 1);
   1100   EXPECT_TRUE(IsTimeoutRunning());
   1101   EXPECT_EQ(1U, GetAndResetSentEventCount());
   1102 }
   1103 
   1104 // Tests that a delayed TouchEvent ack will not trigger a TouchCancel timeout if
   1105 // the timed-out event had no consumer.
   1106 TEST_F(TouchEventQueueTest, NoCancelOnTouchTimeoutWithoutConsumer) {
   1107   SetUpForTimeoutTesting(kDefaultTouchTimeoutDelayMs);
   1108 
   1109   // Queue a TouchStart.
   1110   PressTouchPoint(0, 1);
   1111   ASSERT_EQ(1U, GetAndResetSentEventCount());
   1112   ASSERT_EQ(0U, GetAndResetAckedEventCount());
   1113   EXPECT_TRUE(IsTimeoutRunning());
   1114   EXPECT_TRUE(WillForwardTouchEvents());
   1115 
   1116   // Delay the ack.
   1117   base::MessageLoop::current()->PostDelayedTask(
   1118       FROM_HERE,
   1119       base::MessageLoop::QuitClosure(),
   1120       base::TimeDelta::FromMilliseconds(kDefaultTouchTimeoutDelayMs * 2));
   1121   base::MessageLoop::current()->Run();
   1122 
   1123   // The timeout should have fired, synthetically ack'ing the timed out event.
   1124   // TouchEvent forwarding is disabled until the original ack is received.
   1125   EXPECT_FALSE(IsTimeoutRunning());
   1126   EXPECT_FALSE(WillForwardTouchEvents());
   1127   EXPECT_EQ(0U, GetAndResetSentEventCount());
   1128   EXPECT_EQ(1U, GetAndResetAckedEventCount());
   1129 
   1130   // Touch events should not be forwarded until we receive the original ack.
   1131   PressTouchPoint(0, 1);
   1132   ASSERT_EQ(0U, GetAndResetSentEventCount());
   1133   ASSERT_EQ(1U, GetAndResetAckedEventCount());
   1134 
   1135   // Ack'ing the original event should not trigger a cancel event, as the
   1136   // TouchStart had no consumer.  However, it should re-enable touch forwarding.
   1137   SendTouchEventACK(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
   1138   EXPECT_FALSE(IsTimeoutRunning());
   1139   EXPECT_TRUE(WillForwardTouchEvents());
   1140   EXPECT_EQ(0U, GetAndResetAckedEventCount());
   1141   EXPECT_EQ(0U, GetAndResetSentEventCount());
   1142 
   1143   // Subsequent events should be handled normally.
   1144   PressTouchPoint(0, 1);
   1145   EXPECT_EQ(1U, GetAndResetSentEventCount());
   1146   EXPECT_EQ(0U, GetAndResetAckedEventCount());
   1147 }
   1148 }  // namespace content
   1149