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 <math.h>
      6 
      7 #include "base/basictypes.h"
      8 #include "base/command_line.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/strings/utf_string_conversions.h"
     11 #include "content/browser/renderer_host/input/gesture_event_queue.h"
     12 #include "content/browser/renderer_host/input/input_router_client.h"
     13 #include "content/browser/renderer_host/input/input_router_impl.h"
     14 #include "content/browser/renderer_host/input/mock_input_ack_handler.h"
     15 #include "content/browser/renderer_host/input/mock_input_router_client.h"
     16 #include "content/common/content_constants_internal.h"
     17 #include "content/common/edit_command.h"
     18 #include "content/common/input/synthetic_web_input_event_builders.h"
     19 #include "content/common/input/touch_action.h"
     20 #include "content/common/input/web_input_event_traits.h"
     21 #include "content/common/input_messages.h"
     22 #include "content/common/view_messages.h"
     23 #include "content/public/common/content_switches.h"
     24 #include "content/public/test/mock_render_process_host.h"
     25 #include "content/public/test/test_browser_context.h"
     26 #include "testing/gtest/include/gtest/gtest.h"
     27 #include "ui/events/keycodes/keyboard_codes.h"
     28 
     29 #if defined(USE_AURA)
     30 #include "content/browser/renderer_host/ui_events_helper.h"
     31 #include "ui/events/event.h"
     32 #endif
     33 
     34 using base::TimeDelta;
     35 using blink::WebGestureDevice;
     36 using blink::WebGestureEvent;
     37 using blink::WebKeyboardEvent;
     38 using blink::WebInputEvent;
     39 using blink::WebMouseEvent;
     40 using blink::WebMouseWheelEvent;
     41 using blink::WebTouchEvent;
     42 using blink::WebTouchPoint;
     43 
     44 namespace content {
     45 
     46 namespace {
     47 
     48 const WebInputEvent* GetInputEventFromMessage(const IPC::Message& message) {
     49   PickleIterator iter(message);
     50   const char* data;
     51   int data_length;
     52   if (!message.ReadData(&iter, &data, &data_length))
     53     return NULL;
     54   return reinterpret_cast<const WebInputEvent*>(data);
     55 }
     56 
     57 WebInputEvent& GetEventWithType(WebInputEvent::Type type) {
     58   WebInputEvent* event = NULL;
     59   if (WebInputEvent::isMouseEventType(type)) {
     60     static WebMouseEvent mouse;
     61     event = &mouse;
     62   } else if (WebInputEvent::isTouchEventType(type)) {
     63     static WebTouchEvent touch;
     64     event = &touch;
     65   } else if (WebInputEvent::isKeyboardEventType(type)) {
     66     static WebKeyboardEvent key;
     67     event = &key;
     68   } else if (WebInputEvent::isGestureEventType(type)) {
     69     static WebGestureEvent gesture;
     70     event = &gesture;
     71   } else if (type == WebInputEvent::MouseWheel) {
     72     static WebMouseWheelEvent wheel;
     73     event = &wheel;
     74   }
     75   CHECK(event);
     76   event->type = type;
     77   return *event;
     78 }
     79 
     80 bool GetIsShortcutFromHandleInputEventMessage(const IPC::Message* msg) {
     81   InputMsg_HandleInputEvent::Schema::Param param;
     82   InputMsg_HandleInputEvent::Read(msg, &param);
     83   return param.c;
     84 }
     85 
     86 template<typename MSG_T, typename ARG_T1>
     87 void ExpectIPCMessageWithArg1(const IPC::Message* msg, const ARG_T1& arg1) {
     88   ASSERT_EQ(MSG_T::ID, msg->type());
     89   typename MSG_T::Schema::Param param;
     90   ASSERT_TRUE(MSG_T::Read(msg, &param));
     91   EXPECT_EQ(arg1, param.a);
     92 }
     93 
     94 template<typename MSG_T, typename ARG_T1, typename ARG_T2>
     95 void ExpectIPCMessageWithArg2(const IPC::Message* msg,
     96                               const ARG_T1& arg1,
     97                               const ARG_T2& arg2) {
     98   ASSERT_EQ(MSG_T::ID, msg->type());
     99   typename MSG_T::Schema::Param param;
    100   ASSERT_TRUE(MSG_T::Read(msg, &param));
    101   EXPECT_EQ(arg1, param.a);
    102   EXPECT_EQ(arg2, param.b);
    103 }
    104 
    105 #if defined(USE_AURA)
    106 bool TouchEventsAreEquivalent(const ui::TouchEvent& first,
    107                               const ui::TouchEvent& second) {
    108   if (first.type() != second.type())
    109     return false;
    110   if (first.location() != second.location())
    111     return false;
    112   if (first.touch_id() != second.touch_id())
    113     return false;
    114   if (second.time_stamp().InSeconds() != first.time_stamp().InSeconds())
    115     return false;
    116   return true;
    117 }
    118 
    119 bool EventListIsSubset(const ScopedVector<ui::TouchEvent>& subset,
    120                        const ScopedVector<ui::TouchEvent>& set) {
    121   if (subset.size() > set.size())
    122     return false;
    123   for (size_t i = 0; i < subset.size(); ++i) {
    124     const ui::TouchEvent* first = subset[i];
    125     const ui::TouchEvent* second = set[i];
    126     bool equivalent = TouchEventsAreEquivalent(*first, *second);
    127     if (!equivalent)
    128       return false;
    129   }
    130 
    131   return true;
    132 }
    133 #endif  // defined(USE_AURA)
    134 
    135 // Expected function used for converting pinch scales to deltaY values.
    136 float PinchScaleToWheelDelta(float scale) {
    137   return 100.0 * log(scale);
    138 }
    139 
    140 }  // namespace
    141 
    142 class InputRouterImplTest : public testing::Test {
    143  public:
    144   InputRouterImplTest() {}
    145   virtual ~InputRouterImplTest() {}
    146 
    147  protected:
    148   // testing::Test
    149   virtual void SetUp() OVERRIDE {
    150     browser_context_.reset(new TestBrowserContext());
    151     process_.reset(new MockRenderProcessHost(browser_context_.get()));
    152     client_.reset(new MockInputRouterClient());
    153     ack_handler_.reset(new MockInputAckHandler());
    154     CommandLine* command_line = CommandLine::ForCurrentProcess();
    155     command_line->AppendSwitch(switches::kValidateInputEventStream);
    156     input_router_.reset(new InputRouterImpl(process_.get(),
    157                                             client_.get(),
    158                                             ack_handler_.get(),
    159                                             MSG_ROUTING_NONE,
    160                                             config_));
    161     client_->set_input_router(input_router());
    162     ack_handler_->set_input_router(input_router());
    163   }
    164 
    165   virtual void TearDown() OVERRIDE {
    166     // Process all pending tasks to avoid leaks.
    167     base::MessageLoop::current()->RunUntilIdle();
    168 
    169     input_router_.reset();
    170     client_.reset();
    171     process_.reset();
    172     browser_context_.reset();
    173   }
    174 
    175   void SetUpForTouchAckTimeoutTest(int timeout_ms) {
    176     config_.touch_config.touch_ack_timeout_delay =
    177         base::TimeDelta::FromMilliseconds(timeout_ms);
    178     config_.touch_config.touch_ack_timeout_supported = true;
    179     TearDown();
    180     SetUp();
    181   }
    182 
    183   void SimulateKeyboardEvent(WebInputEvent::Type type, bool is_shortcut) {
    184     WebKeyboardEvent event = SyntheticWebKeyboardEventBuilder::Build(type);
    185     NativeWebKeyboardEvent native_event;
    186     memcpy(&native_event, &event, sizeof(event));
    187     input_router_->SendKeyboardEvent(
    188         native_event,
    189         ui::LatencyInfo(),
    190         is_shortcut);
    191   }
    192 
    193   void SimulateWheelEvent(float dX, float dY, int modifiers, bool precise) {
    194     input_router_->SendWheelEvent(MouseWheelEventWithLatencyInfo(
    195         SyntheticWebMouseWheelEventBuilder::Build(dX, dY, modifiers, precise),
    196         ui::LatencyInfo()));
    197   }
    198 
    199   void SimulateMouseEvent(WebInputEvent::Type type, int x, int y) {
    200     input_router_->SendMouseEvent(MouseEventWithLatencyInfo(
    201         SyntheticWebMouseEventBuilder::Build(type, x, y, 0),
    202         ui::LatencyInfo()));
    203   }
    204 
    205   void SimulateWheelEventWithPhase(WebMouseWheelEvent::Phase phase) {
    206     input_router_->SendWheelEvent(MouseWheelEventWithLatencyInfo(
    207         SyntheticWebMouseWheelEventBuilder::Build(phase), ui::LatencyInfo()));
    208   }
    209 
    210   void SimulateGestureEvent(const WebGestureEvent& gesture) {
    211     input_router_->SendGestureEvent(
    212         GestureEventWithLatencyInfo(gesture, ui::LatencyInfo()));
    213   }
    214 
    215   void SimulateGestureEvent(WebInputEvent::Type type,
    216                             WebGestureDevice sourceDevice) {
    217     SimulateGestureEvent(
    218         SyntheticWebGestureEventBuilder::Build(type, sourceDevice));
    219   }
    220 
    221   void SimulateGestureScrollUpdateEvent(float dX, float dY, int modifiers) {
    222     SimulateGestureEvent(
    223         SyntheticWebGestureEventBuilder::BuildScrollUpdate(dX, dY, modifiers));
    224   }
    225 
    226   void SimulateGesturePinchUpdateEvent(float scale,
    227                                        float anchorX,
    228                                        float anchorY,
    229                                        int modifiers,
    230                                        WebGestureDevice sourceDevice) {
    231     SimulateGestureEvent(SyntheticWebGestureEventBuilder::BuildPinchUpdate(
    232         scale, anchorX, anchorY, modifiers, sourceDevice));
    233   }
    234 
    235   void SimulateGestureFlingStartEvent(float velocityX,
    236                                       float velocityY,
    237                                       WebGestureDevice sourceDevice) {
    238     SimulateGestureEvent(
    239         SyntheticWebGestureEventBuilder::BuildFling(velocityX,
    240                                                     velocityY,
    241                                                     sourceDevice));
    242   }
    243 
    244   void SetTouchTimestamp(base::TimeDelta timestamp) {
    245     touch_event_.SetTimestamp(timestamp);
    246   }
    247 
    248   void SendTouchEvent() {
    249     input_router_->SendTouchEvent(
    250         TouchEventWithLatencyInfo(touch_event_, ui::LatencyInfo()));
    251     touch_event_.ResetPoints();
    252   }
    253 
    254   int PressTouchPoint(int x, int y) {
    255     return touch_event_.PressPoint(x, y);
    256   }
    257 
    258   void MoveTouchPoint(int index, int x, int y) {
    259     touch_event_.MovePoint(index, x, y);
    260   }
    261 
    262   void ReleaseTouchPoint(int index) {
    263     touch_event_.ReleasePoint(index);
    264   }
    265 
    266   void CancelTouchPoint(int index) {
    267     touch_event_.CancelPoint(index);
    268   }
    269 
    270   void SendInputEventACK(blink::WebInputEvent::Type type,
    271                          InputEventAckState ack_result) {
    272     InputHostMsg_HandleInputEvent_ACK_Params ack;
    273     ack.type = type;
    274     ack.state = ack_result;
    275     input_router_->OnMessageReceived(InputHostMsg_HandleInputEvent_ACK(0, ack));
    276   }
    277 
    278   InputRouterImpl* input_router() const {
    279     return input_router_.get();
    280   }
    281 
    282   bool TouchEventQueueEmpty() const {
    283     return input_router()->touch_event_queue_.empty();
    284   }
    285 
    286   bool TouchEventTimeoutEnabled() const {
    287     return input_router()->touch_event_queue_.ack_timeout_enabled();
    288   }
    289 
    290   void Flush() const {
    291     return input_router_->Flush();
    292   }
    293 
    294   size_t GetAndResetDidFlushCount() {
    295     return client_->GetAndResetDidFlushCount();
    296   }
    297 
    298   bool HasPendingEvents() const {
    299     return input_router_->HasPendingEvents();
    300   }
    301 
    302   void OnHasTouchEventHandlers(bool has_handlers) {
    303     input_router_->OnMessageReceived(
    304         ViewHostMsg_HasTouchEventHandlers(0, has_handlers));
    305   }
    306 
    307   void OnSetTouchAction(content::TouchAction touch_action) {
    308     input_router_->OnMessageReceived(
    309         InputHostMsg_SetTouchAction(0, touch_action));
    310   }
    311 
    312   size_t GetSentMessageCountAndResetSink() {
    313     size_t count = process_->sink().message_count();
    314     process_->sink().ClearMessages();
    315     return count;
    316   }
    317 
    318   static void RunTasksAndWait(base::TimeDelta delay) {
    319     base::MessageLoop::current()->PostDelayedTask(
    320         FROM_HERE, base::MessageLoop::QuitClosure(), delay);
    321     base::MessageLoop::current()->Run();
    322   }
    323 
    324   InputRouterImpl::Config config_;
    325   scoped_ptr<MockRenderProcessHost> process_;
    326   scoped_ptr<MockInputRouterClient> client_;
    327   scoped_ptr<MockInputAckHandler> ack_handler_;
    328   scoped_ptr<InputRouterImpl> input_router_;
    329 
    330  private:
    331   base::MessageLoopForUI message_loop_;
    332   SyntheticWebTouchEvent touch_event_;
    333 
    334   scoped_ptr<TestBrowserContext> browser_context_;
    335 };
    336 
    337 TEST_F(InputRouterImplTest, CoalescesRangeSelection) {
    338   input_router_->SendInput(scoped_ptr<IPC::Message>(
    339       new InputMsg_SelectRange(0, gfx::Point(1, 2), gfx::Point(3, 4))));
    340   ExpectIPCMessageWithArg2<InputMsg_SelectRange>(
    341       process_->sink().GetMessageAt(0),
    342       gfx::Point(1, 2),
    343       gfx::Point(3, 4));
    344   EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
    345 
    346   // Send two more messages without acking.
    347   input_router_->SendInput(scoped_ptr<IPC::Message>(
    348       new InputMsg_SelectRange(0, gfx::Point(5, 6), gfx::Point(7, 8))));
    349   EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
    350 
    351   input_router_->SendInput(scoped_ptr<IPC::Message>(
    352       new InputMsg_SelectRange(0, gfx::Point(9, 10), gfx::Point(11, 12))));
    353   EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
    354 
    355   // Now ack the first message.
    356   {
    357     scoped_ptr<IPC::Message> response(new ViewHostMsg_SelectRange_ACK(0));
    358     input_router_->OnMessageReceived(*response);
    359   }
    360 
    361   // Verify that the two messages are coalesced into one message.
    362   ExpectIPCMessageWithArg2<InputMsg_SelectRange>(
    363       process_->sink().GetMessageAt(0),
    364       gfx::Point(9, 10),
    365       gfx::Point(11, 12));
    366   EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
    367 
    368   // Acking the coalesced msg should not send any more msg.
    369   {
    370     scoped_ptr<IPC::Message> response(new ViewHostMsg_SelectRange_ACK(0));
    371     input_router_->OnMessageReceived(*response);
    372   }
    373   EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
    374 }
    375 
    376 TEST_F(InputRouterImplTest, CoalescesCaretMove) {
    377   input_router_->SendInput(
    378       scoped_ptr<IPC::Message>(new InputMsg_MoveCaret(0, gfx::Point(1, 2))));
    379   ExpectIPCMessageWithArg1<InputMsg_MoveCaret>(
    380       process_->sink().GetMessageAt(0), gfx::Point(1, 2));
    381   EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
    382 
    383   // Send two more messages without acking.
    384   input_router_->SendInput(
    385       scoped_ptr<IPC::Message>(new InputMsg_MoveCaret(0, gfx::Point(5, 6))));
    386   EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
    387 
    388   input_router_->SendInput(
    389       scoped_ptr<IPC::Message>(new InputMsg_MoveCaret(0, gfx::Point(9, 10))));
    390   EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
    391 
    392   // Now ack the first message.
    393   {
    394     scoped_ptr<IPC::Message> response(new ViewHostMsg_MoveCaret_ACK(0));
    395     input_router_->OnMessageReceived(*response);
    396   }
    397 
    398   // Verify that the two messages are coalesced into one message.
    399   ExpectIPCMessageWithArg1<InputMsg_MoveCaret>(
    400       process_->sink().GetMessageAt(0), gfx::Point(9, 10));
    401   EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
    402 
    403   // Acking the coalesced msg should not send any more msg.
    404   {
    405     scoped_ptr<IPC::Message> response(new ViewHostMsg_MoveCaret_ACK(0));
    406     input_router_->OnMessageReceived(*response);
    407   }
    408   EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
    409 }
    410 
    411 TEST_F(InputRouterImplTest, HandledInputEvent) {
    412   client_->set_filter_state(INPUT_EVENT_ACK_STATE_CONSUMED);
    413 
    414   // Simulate a keyboard event.
    415   SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
    416 
    417   // Make sure no input event is sent to the renderer.
    418   EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
    419 
    420   // OnKeyboardEventAck should be triggered without actual ack.
    421   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    422 
    423   // As the event was acked already, keyboard event queue should be
    424   // empty.
    425   ASSERT_EQ(NULL, input_router_->GetLastKeyboardEvent());
    426 }
    427 
    428 TEST_F(InputRouterImplTest, ClientCanceledKeyboardEvent) {
    429   client_->set_filter_state(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
    430 
    431   // Simulate a keyboard event that has no consumer.
    432   SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
    433 
    434   // Make sure no input event is sent to the renderer.
    435   EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
    436   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    437 
    438 
    439   // Simulate a keyboard event that should be dropped.
    440   client_->set_filter_state(INPUT_EVENT_ACK_STATE_UNKNOWN);
    441   SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
    442 
    443   // Make sure no input event is sent to the renderer, and no ack is sent.
    444   EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
    445   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    446 }
    447 
    448 TEST_F(InputRouterImplTest, ShortcutKeyboardEvent) {
    449   SimulateKeyboardEvent(WebInputEvent::RawKeyDown, true);
    450   EXPECT_TRUE(GetIsShortcutFromHandleInputEventMessage(
    451       process_->sink().GetMessageAt(0)));
    452 
    453   process_->sink().ClearMessages();
    454 
    455   SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
    456   EXPECT_FALSE(GetIsShortcutFromHandleInputEventMessage(
    457       process_->sink().GetMessageAt(0)));
    458 }
    459 
    460 TEST_F(InputRouterImplTest, NoncorrespondingKeyEvents) {
    461   SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
    462 
    463   SendInputEventACK(WebInputEvent::KeyUp,
    464                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    465   EXPECT_TRUE(ack_handler_->unexpected_event_ack_called());
    466 }
    467 
    468 // Tests ported from RenderWidgetHostTest --------------------------------------
    469 
    470 TEST_F(InputRouterImplTest, HandleKeyEventsWeSent) {
    471   // Simulate a keyboard event.
    472   SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
    473   ASSERT_TRUE(input_router_->GetLastKeyboardEvent());
    474   EXPECT_EQ(WebInputEvent::RawKeyDown,
    475             input_router_->GetLastKeyboardEvent()->type);
    476 
    477   // Make sure we sent the input event to the renderer.
    478   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
    479                   InputMsg_HandleInputEvent::ID));
    480   process_->sink().ClearMessages();
    481 
    482   // Send the simulated response from the renderer back.
    483   SendInputEventACK(WebInputEvent::RawKeyDown,
    484                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    485   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    486   EXPECT_EQ(WebInputEvent::RawKeyDown,
    487             ack_handler_->acked_keyboard_event().type);
    488 }
    489 
    490 TEST_F(InputRouterImplTest, IgnoreKeyEventsWeDidntSend) {
    491   // Send a simulated, unrequested key response. We should ignore this.
    492   SendInputEventACK(WebInputEvent::RawKeyDown,
    493                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    494 
    495   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    496 }
    497 
    498 TEST_F(InputRouterImplTest, CoalescesWheelEvents) {
    499   // Simulate wheel events.
    500   SimulateWheelEvent(0, -5, 0, false);  // sent directly
    501   SimulateWheelEvent(0, -10, 0, false);  // enqueued
    502   SimulateWheelEvent(8, -6, 0, false);  // coalesced into previous event
    503   SimulateWheelEvent(9, -7, 1, false);  // enqueued, different modifiers
    504   SimulateWheelEvent(0, -10, 0, false);  // enqueued, different modifiers
    505   // Explicitly verify that PhaseEnd isn't coalesced to avoid bugs like
    506   // https://crbug.com/154740.
    507   SimulateWheelEventWithPhase(WebMouseWheelEvent::PhaseEnded);  // enqueued
    508 
    509   // Check that only the first event was sent.
    510   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
    511                   InputMsg_HandleInputEvent::ID));
    512   const WebInputEvent* input_event =
    513       GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
    514   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
    515   const WebMouseWheelEvent* wheel_event =
    516       static_cast<const WebMouseWheelEvent*>(input_event);
    517   EXPECT_EQ(0, wheel_event->deltaX);
    518   EXPECT_EQ(-5, wheel_event->deltaY);
    519   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    520 
    521   // Check that the ACK sends the second message immediately.
    522   SendInputEventACK(WebInputEvent::MouseWheel,
    523                     INPUT_EVENT_ACK_STATE_CONSUMED);
    524   // The coalesced events can queue up a delayed ack
    525   // so that additional input events can be processed before
    526   // we turn off coalescing.
    527   base::MessageLoop::current()->RunUntilIdle();
    528   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    529   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
    530           InputMsg_HandleInputEvent::ID));
    531   input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
    532   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
    533   wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
    534   EXPECT_EQ(8, wheel_event->deltaX);
    535   EXPECT_EQ(-10 + -6, wheel_event->deltaY);  // coalesced
    536   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    537 
    538   // Ack the second event (which had the third coalesced into it).
    539   SendInputEventACK(WebInputEvent::MouseWheel,
    540                     INPUT_EVENT_ACK_STATE_CONSUMED);
    541   base::MessageLoop::current()->RunUntilIdle();
    542   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    543   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
    544                   InputMsg_HandleInputEvent::ID));
    545   input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
    546   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
    547   wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
    548   EXPECT_EQ(9, wheel_event->deltaX);
    549   EXPECT_EQ(-7, wheel_event->deltaY);
    550   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    551 
    552   // Ack the fourth event.
    553   SendInputEventACK(WebInputEvent::MouseWheel,
    554                     INPUT_EVENT_ACK_STATE_CONSUMED);
    555   base::MessageLoop::current()->RunUntilIdle();
    556   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    557   EXPECT_TRUE(
    558       process_->sink().GetUniqueMessageMatching(InputMsg_HandleInputEvent::ID));
    559   input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
    560   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
    561   wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
    562   EXPECT_EQ(0, wheel_event->deltaX);
    563   EXPECT_EQ(-10, wheel_event->deltaY);
    564   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    565 
    566   // Ack the fifth event.
    567   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
    568   base::MessageLoop::current()->RunUntilIdle();
    569   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    570   EXPECT_TRUE(
    571       process_->sink().GetUniqueMessageMatching(InputMsg_HandleInputEvent::ID));
    572   input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
    573   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
    574   wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
    575   EXPECT_EQ(0, wheel_event->deltaX);
    576   EXPECT_EQ(0, wheel_event->deltaY);
    577   EXPECT_EQ(WebMouseWheelEvent::PhaseEnded, wheel_event->phase);
    578   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    579 
    580   // After the final ack, the queue should be empty.
    581   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
    582   base::MessageLoop::current()->RunUntilIdle();
    583   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    584   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    585 }
    586 
    587 // Tests that touch-events are queued properly.
    588 TEST_F(InputRouterImplTest, TouchEventQueue) {
    589   OnHasTouchEventHandlers(true);
    590 
    591   PressTouchPoint(1, 1);
    592   SendTouchEvent();
    593   EXPECT_TRUE(client_->GetAndResetFilterEventCalled());
    594   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    595   EXPECT_FALSE(TouchEventQueueEmpty());
    596 
    597   // The second touch should not be sent since one is already in queue.
    598   MoveTouchPoint(0, 5, 5);
    599   SendTouchEvent();
    600   EXPECT_FALSE(client_->GetAndResetFilterEventCalled());
    601   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    602   EXPECT_FALSE(TouchEventQueueEmpty());
    603 
    604   // Receive an ACK for the first touch-event.
    605   SendInputEventACK(WebInputEvent::TouchStart,
    606                     INPUT_EVENT_ACK_STATE_CONSUMED);
    607   EXPECT_FALSE(TouchEventQueueEmpty());
    608   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    609   EXPECT_EQ(WebInputEvent::TouchStart,
    610             ack_handler_->acked_touch_event().event.type);
    611   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    612 
    613   SendInputEventACK(WebInputEvent::TouchMove,
    614                     INPUT_EVENT_ACK_STATE_CONSUMED);
    615   EXPECT_TRUE(TouchEventQueueEmpty());
    616   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    617   EXPECT_EQ(WebInputEvent::TouchMove,
    618             ack_handler_->acked_touch_event().event.type);
    619   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    620 }
    621 
    622 // Tests that the touch-queue is emptied if a page stops listening for touch
    623 // events.
    624 TEST_F(InputRouterImplTest, TouchEventQueueFlush) {
    625   OnHasTouchEventHandlers(true);
    626   EXPECT_TRUE(client_->has_touch_handler());
    627   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    628   EXPECT_TRUE(TouchEventQueueEmpty());
    629 
    630   EXPECT_TRUE(input_router_->ShouldForwardTouchEvent());
    631 
    632   // Send a touch-press event.
    633   PressTouchPoint(1, 1);
    634   SendTouchEvent();
    635   EXPECT_FALSE(TouchEventQueueEmpty());
    636   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    637 
    638   // The page stops listening for touch-events. The touch-event queue should now
    639   // be emptied, but none of the queued touch-events should be sent to the
    640   // renderer.
    641   OnHasTouchEventHandlers(false);
    642   EXPECT_FALSE(client_->has_touch_handler());
    643   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    644   EXPECT_TRUE(TouchEventQueueEmpty());
    645   EXPECT_FALSE(input_router_->ShouldForwardTouchEvent());
    646 }
    647 
    648 #if defined(USE_AURA)
    649 // Tests that the acked events have correct state. (ui::Events are used only on
    650 // windows and aura)
    651 TEST_F(InputRouterImplTest, AckedTouchEventState) {
    652   input_router_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
    653   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    654   EXPECT_TRUE(TouchEventQueueEmpty());
    655   EXPECT_TRUE(input_router_->ShouldForwardTouchEvent());
    656 
    657   // Send a bunch of events, and make sure the ACKed events are correct.
    658   ScopedVector<ui::TouchEvent> expected_events;
    659 
    660   // Use a custom timestamp for all the events to test that the acked events
    661   // have the same timestamp;
    662   base::TimeDelta timestamp = base::Time::NowFromSystemTime() - base::Time();
    663   timestamp -= base::TimeDelta::FromSeconds(600);
    664 
    665   // Press the first finger.
    666   PressTouchPoint(1, 1);
    667   SetTouchTimestamp(timestamp);
    668   SendTouchEvent();
    669   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    670   expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
    671       gfx::Point(1, 1), 0, timestamp));
    672 
    673   // Move the finger.
    674   timestamp += base::TimeDelta::FromSeconds(10);
    675   MoveTouchPoint(0, 500, 500);
    676   SetTouchTimestamp(timestamp);
    677   SendTouchEvent();
    678   EXPECT_FALSE(TouchEventQueueEmpty());
    679   expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_MOVED,
    680       gfx::Point(500, 500), 0, timestamp));
    681 
    682   // Now press a second finger.
    683   timestamp += base::TimeDelta::FromSeconds(10);
    684   PressTouchPoint(2, 2);
    685   SetTouchTimestamp(timestamp);
    686   SendTouchEvent();
    687   EXPECT_FALSE(TouchEventQueueEmpty());
    688   expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
    689       gfx::Point(2, 2), 1, timestamp));
    690 
    691   // Move both fingers.
    692   timestamp += base::TimeDelta::FromSeconds(10);
    693   MoveTouchPoint(0, 10, 10);
    694   MoveTouchPoint(1, 20, 20);
    695   SetTouchTimestamp(timestamp);
    696   SendTouchEvent();
    697   EXPECT_FALSE(TouchEventQueueEmpty());
    698   expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_MOVED,
    699       gfx::Point(10, 10), 0, timestamp));
    700   expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_MOVED,
    701       gfx::Point(20, 20), 1, timestamp));
    702 
    703   // Receive the ACKs and make sure the generated events from the acked events
    704   // are correct.
    705   WebInputEvent::Type acks[] = { WebInputEvent::TouchStart,
    706                                  WebInputEvent::TouchMove,
    707                                  WebInputEvent::TouchStart,
    708                                  WebInputEvent::TouchMove };
    709 
    710   TouchEventCoordinateSystem coordinate_system = LOCAL_COORDINATES;
    711 #if !defined(OS_WIN)
    712   coordinate_system = SCREEN_COORDINATES;
    713 #endif
    714   for (size_t i = 0; i < arraysize(acks); ++i) {
    715     SendInputEventACK(acks[i],
    716                       INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    717     EXPECT_EQ(acks[i], ack_handler_->acked_touch_event().event.type);
    718     ScopedVector<ui::TouchEvent> acked;
    719 
    720     MakeUITouchEventsFromWebTouchEvents(
    721         ack_handler_->acked_touch_event(), &acked, coordinate_system);
    722     bool success = EventListIsSubset(acked, expected_events);
    723     EXPECT_TRUE(success) << "Failed on step: " << i;
    724     if (!success)
    725       break;
    726     expected_events.erase(expected_events.begin(),
    727                           expected_events.begin() + acked.size());
    728   }
    729 
    730   EXPECT_TRUE(TouchEventQueueEmpty());
    731   EXPECT_EQ(0U, expected_events.size());
    732 }
    733 #endif  // defined(USE_AURA)
    734 
    735 TEST_F(InputRouterImplTest, UnhandledWheelEvent) {
    736   // Simulate wheel events.
    737   SimulateWheelEvent(0, -5, 0, false);  // sent directly
    738   SimulateWheelEvent(0, -10, 0, false);  // enqueued
    739 
    740   // Check that only the first event was sent.
    741   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
    742                   InputMsg_HandleInputEvent::ID));
    743   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    744 
    745   // Indicate that the wheel event was unhandled.
    746   SendInputEventACK(WebInputEvent::MouseWheel,
    747                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    748 
    749   // Check that the correct unhandled wheel event was received.
    750   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    751   EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, ack_handler_->ack_state());
    752   EXPECT_EQ(ack_handler_->acked_wheel_event().deltaY, -5);
    753 
    754   // Check that the second event was sent.
    755   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
    756                   InputMsg_HandleInputEvent::ID));
    757   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    758 
    759   // Check that the correct unhandled wheel event was received.
    760   EXPECT_EQ(ack_handler_->acked_wheel_event().deltaY, -5);
    761 }
    762 
    763 TEST_F(InputRouterImplTest, TouchTypesIgnoringAck) {
    764   OnHasTouchEventHandlers(true);
    765   // Only acks for TouchCancel should always be ignored.
    766   ASSERT_FALSE(WebInputEventTraits::IgnoresAckDisposition(
    767       GetEventWithType(WebInputEvent::TouchStart)));
    768   ASSERT_FALSE(WebInputEventTraits::IgnoresAckDisposition(
    769       GetEventWithType(WebInputEvent::TouchMove)));
    770   ASSERT_FALSE(WebInputEventTraits::IgnoresAckDisposition(
    771       GetEventWithType(WebInputEvent::TouchEnd)));
    772 
    773   // Precede the TouchCancel with an appropriate TouchStart;
    774   PressTouchPoint(1, 1);
    775   SendTouchEvent();
    776   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
    777   ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
    778   ASSERT_EQ(1U, ack_handler_->GetAndResetAckCount());
    779   ASSERT_EQ(0, client_->in_flight_event_count());
    780 
    781   // The TouchCancel ack is always ignored.
    782   CancelTouchPoint(0);
    783   SendTouchEvent();
    784   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    785   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    786   EXPECT_EQ(0, client_->in_flight_event_count());
    787   EXPECT_FALSE(HasPendingEvents());
    788   SendInputEventACK(WebInputEvent::TouchCancel,
    789                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    790   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    791   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    792   EXPECT_FALSE(HasPendingEvents());
    793 }
    794 
    795 TEST_F(InputRouterImplTest, GestureTypesIgnoringAck) {
    796   // We test every gesture type, ensuring that the stream of gestures is valid.
    797   const int kEventTypesLength = 29;
    798   WebInputEvent::Type eventTypes[kEventTypesLength] = {
    799       WebInputEvent::GestureTapDown,
    800       WebInputEvent::GestureShowPress,
    801       WebInputEvent::GestureTapCancel,
    802       WebInputEvent::GestureScrollBegin,
    803       WebInputEvent::GestureFlingStart,
    804       WebInputEvent::GestureFlingCancel,
    805       WebInputEvent::GestureTapDown,
    806       WebInputEvent::GestureTap,
    807       WebInputEvent::GestureTapDown,
    808       WebInputEvent::GestureLongPress,
    809       WebInputEvent::GestureTapCancel,
    810       WebInputEvent::GestureLongTap,
    811       WebInputEvent::GestureTapDown,
    812       WebInputEvent::GestureTapUnconfirmed,
    813       WebInputEvent::GestureTapCancel,
    814       WebInputEvent::GestureTapDown,
    815       WebInputEvent::GestureDoubleTap,
    816       WebInputEvent::GestureTapDown,
    817       WebInputEvent::GestureTapCancel,
    818       WebInputEvent::GestureTwoFingerTap,
    819       WebInputEvent::GestureTapDown,
    820       WebInputEvent::GestureTapCancel,
    821       WebInputEvent::GestureScrollBegin,
    822       WebInputEvent::GestureScrollUpdate,
    823       WebInputEvent::GestureScrollUpdateWithoutPropagation,
    824       WebInputEvent::GesturePinchBegin,
    825       WebInputEvent::GesturePinchUpdate,
    826       WebInputEvent::GesturePinchEnd,
    827       WebInputEvent::GestureScrollEnd};
    828   for (int i = 0; i < kEventTypesLength; ++i) {
    829     WebInputEvent::Type type = eventTypes[i];
    830     if (!WebInputEventTraits::IgnoresAckDisposition(GetEventWithType(type))) {
    831       SimulateGestureEvent(type, blink::WebGestureDeviceTouchscreen);
    832       EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    833       EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    834       EXPECT_EQ(1, client_->in_flight_event_count());
    835       EXPECT_TRUE(HasPendingEvents());
    836 
    837       SendInputEventACK(type, INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    838       EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    839       EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    840       EXPECT_EQ(0, client_->in_flight_event_count());
    841       EXPECT_FALSE(HasPendingEvents());
    842       continue;
    843     }
    844 
    845     SimulateGestureEvent(type, blink::WebGestureDeviceTouchscreen);
    846     EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    847     EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    848     EXPECT_EQ(0, client_->in_flight_event_count());
    849     EXPECT_FALSE(HasPendingEvents());
    850   }
    851 }
    852 
    853 TEST_F(InputRouterImplTest, MouseTypesIgnoringAck) {
    854   int start_type = static_cast<int>(WebInputEvent::MouseDown);
    855   int end_type = static_cast<int>(WebInputEvent::ContextMenu);
    856   ASSERT_LT(start_type, end_type);
    857   for (int i = start_type; i <= end_type; ++i) {
    858     WebInputEvent::Type type = static_cast<WebInputEvent::Type>(i);
    859     int expected_in_flight_event_count =
    860         WebInputEventTraits::IgnoresAckDisposition(GetEventWithType(type)) ? 0
    861                                                                            : 1;
    862 
    863     // Note: Mouse event acks are never forwarded to the ack handler, so the key
    864     // result here is that ignored ack types don't affect the in-flight count.
    865     SimulateMouseEvent(type, 0, 0);
    866     EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    867     EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    868     EXPECT_EQ(expected_in_flight_event_count, client_->in_flight_event_count());
    869     if (expected_in_flight_event_count) {
    870       SendInputEventACK(type, INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    871       EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    872       EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    873       EXPECT_EQ(0, client_->in_flight_event_count());
    874     }
    875   }
    876 }
    877 
    878 // Guard against breaking changes to the list of ignored event ack types in
    879 // |WebInputEventTraits::IgnoresAckDisposition|.
    880 TEST_F(InputRouterImplTest, RequiredEventAckTypes) {
    881   const WebInputEvent::Type kRequiredEventAckTypes[] = {
    882     WebInputEvent::MouseMove,
    883     WebInputEvent::MouseWheel,
    884     WebInputEvent::RawKeyDown,
    885     WebInputEvent::KeyDown,
    886     WebInputEvent::KeyUp,
    887     WebInputEvent::Char,
    888     WebInputEvent::GestureScrollUpdate,
    889     WebInputEvent::GestureFlingStart,
    890     WebInputEvent::GestureFlingCancel,
    891     WebInputEvent::GesturePinchUpdate,
    892     WebInputEvent::TouchStart,
    893     WebInputEvent::TouchMove
    894   };
    895   for (size_t i = 0; i < arraysize(kRequiredEventAckTypes); ++i) {
    896     const WebInputEvent::Type required_ack_type = kRequiredEventAckTypes[i];
    897     EXPECT_FALSE(WebInputEventTraits::IgnoresAckDisposition(
    898         GetEventWithType(required_ack_type)));
    899   }
    900 }
    901 
    902 // Test that GestureShowPress, GestureTapDown and GestureTapCancel events don't
    903 // wait for ACKs.
    904 TEST_F(InputRouterImplTest, GestureTypesIgnoringAckInterleaved) {
    905   // Interleave a few events that do and do not ignore acks, ensuring that
    906   // ack-ignoring events aren't dispatched until all prior events which observe
    907   // their ack disposition have been dispatched.
    908 
    909   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
    910                        blink::WebGestureDeviceTouchscreen);
    911   ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
    912   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    913   EXPECT_EQ(0, client_->in_flight_event_count());
    914 
    915   SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
    916                        blink::WebGestureDeviceTouchscreen);
    917   ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
    918   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    919   EXPECT_EQ(1, client_->in_flight_event_count());
    920 
    921   SimulateGestureEvent(WebInputEvent::GestureTapDown,
    922                        blink::WebGestureDeviceTouchscreen);
    923   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    924   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    925   EXPECT_EQ(1, client_->in_flight_event_count());
    926 
    927   SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
    928                        blink::WebGestureDeviceTouchscreen);
    929   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    930   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    931 
    932   SimulateGestureEvent(WebInputEvent::GestureShowPress,
    933                        blink::WebGestureDeviceTouchscreen);
    934   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    935   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    936 
    937   SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
    938                        blink::WebGestureDeviceTouchscreen);
    939   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    940   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    941 
    942   SimulateGestureEvent(WebInputEvent::GestureTapCancel,
    943                        blink::WebGestureDeviceTouchscreen);
    944   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    945   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    946 
    947   // Now ack each ack-respecting event. Ack-ignoring events should not be
    948   // dispatched until all prior events which observe ack disposition have been
    949   // fired, at which point they should be sent immediately.  They should also
    950   // have no effect on the in-flight event count.
    951   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
    952                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    953   EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
    954   EXPECT_EQ(2U, ack_handler_->GetAndResetAckCount());
    955   EXPECT_EQ(1, client_->in_flight_event_count());
    956 
    957   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
    958                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    959   EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
    960   EXPECT_EQ(2U, ack_handler_->GetAndResetAckCount());
    961   EXPECT_EQ(1, client_->in_flight_event_count());
    962 
    963   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
    964                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
    965   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    966   EXPECT_EQ(2U, ack_handler_->GetAndResetAckCount());
    967   EXPECT_EQ(0, client_->in_flight_event_count());
    968 }
    969 
    970 // Test that GestureShowPress events don't get out of order due to
    971 // ignoring their acks.
    972 TEST_F(InputRouterImplTest, GestureShowPressIsInOrder) {
    973   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
    974                        blink::WebGestureDeviceTouchscreen);
    975   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    976   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    977 
    978 
    979   // GesturePinchBegin ignores its ack.
    980   SimulateGestureEvent(WebInputEvent::GesturePinchBegin,
    981                        blink::WebGestureDeviceTouchscreen);
    982   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    983   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
    984 
    985   // GesturePinchUpdate waits for an ack.
    986   // This also verifies that GesturePinchUpdates for touchscreen are sent
    987   // to the renderer (in contrast to the TrackpadPinchUpdate test).
    988   SimulateGestureEvent(WebInputEvent::GesturePinchUpdate,
    989                        blink::WebGestureDeviceTouchscreen);
    990   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
    991   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    992 
    993   SimulateGestureEvent(WebInputEvent::GestureShowPress,
    994                        blink::WebGestureDeviceTouchscreen);
    995   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
    996   // The ShowPress, though it ignores ack, is still stuck in the queue
    997   // behind the PinchUpdate which requires an ack.
    998   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
    999 
   1000   SimulateGestureEvent(WebInputEvent::GestureShowPress,
   1001                        blink::WebGestureDeviceTouchscreen);
   1002   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1003   // ShowPress has entered the queue.
   1004   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
   1005 
   1006   SendInputEventACK(WebInputEvent::GesturePinchUpdate,
   1007                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1008   // Now that the Tap has been ACKed, the ShowPress events should receive
   1009   // synthetic acks, and fire immediately.
   1010   EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
   1011   EXPECT_EQ(3U, ack_handler_->GetAndResetAckCount());
   1012 }
   1013 
   1014 // Test that touch ack timeout behavior is properly toggled by view update flags
   1015 // and allowed touch actions.
   1016 TEST_F(InputRouterImplTest, TouchAckTimeoutConfigured) {
   1017   const int timeout_ms = 1;
   1018   SetUpForTouchAckTimeoutTest(timeout_ms);
   1019   ASSERT_TRUE(TouchEventTimeoutEnabled());
   1020 
   1021   // Verify that the touch ack timeout fires upon the delayed ack.
   1022   PressTouchPoint(1, 1);
   1023   SendTouchEvent();
   1024   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
   1025   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1026   RunTasksAndWait(base::TimeDelta::FromMilliseconds(timeout_ms + 1));
   1027 
   1028   // The timed-out event should have been ack'ed.
   1029   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1030   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1031 
   1032   // Ack'ing the timed-out event should fire a TouchCancel.
   1033   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
   1034   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
   1035   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1036 
   1037   // The remainder of the touch sequence should be dropped.
   1038   ReleaseTouchPoint(0);
   1039   SendTouchEvent();
   1040   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1041   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1042   ASSERT_TRUE(TouchEventTimeoutEnabled());
   1043 
   1044   // A fixed page scale or mobile viewport should disable the touch timeout.
   1045   input_router()->OnViewUpdated(InputRouter::FIXED_PAGE_SCALE);
   1046   EXPECT_FALSE(TouchEventTimeoutEnabled());
   1047 
   1048   input_router()->OnViewUpdated(InputRouter::VIEW_FLAGS_NONE);
   1049   EXPECT_TRUE(TouchEventTimeoutEnabled());
   1050 
   1051   input_router()->OnViewUpdated(InputRouter::MOBILE_VIEWPORT);
   1052   EXPECT_FALSE(TouchEventTimeoutEnabled());
   1053 
   1054   input_router()->OnViewUpdated(InputRouter::MOBILE_VIEWPORT |
   1055                                 InputRouter::FIXED_PAGE_SCALE);
   1056   EXPECT_FALSE(TouchEventTimeoutEnabled());
   1057 
   1058   input_router()->OnViewUpdated(InputRouter::VIEW_FLAGS_NONE);
   1059   EXPECT_TRUE(TouchEventTimeoutEnabled());
   1060 
   1061   // TOUCH_ACTION_NONE (and no other touch-action) should disable the timeout.
   1062   OnHasTouchEventHandlers(true);
   1063   PressTouchPoint(1, 1);
   1064   SendTouchEvent();
   1065   OnSetTouchAction(TOUCH_ACTION_PAN_Y);
   1066   EXPECT_TRUE(TouchEventTimeoutEnabled());
   1067   ReleaseTouchPoint(0);
   1068   SendTouchEvent();
   1069   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
   1070   SendInputEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED);
   1071 
   1072   PressTouchPoint(1, 1);
   1073   SendTouchEvent();
   1074   OnSetTouchAction(TOUCH_ACTION_NONE);
   1075   EXPECT_FALSE(TouchEventTimeoutEnabled());
   1076   ReleaseTouchPoint(0);
   1077   SendTouchEvent();
   1078   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
   1079   SendInputEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED);
   1080 
   1081   // As the touch-action is reset by a new touch sequence, the timeout behavior
   1082   // should be restored.
   1083   PressTouchPoint(1, 1);
   1084   SendTouchEvent();
   1085   EXPECT_TRUE(TouchEventTimeoutEnabled());
   1086 }
   1087 
   1088 // Test that a touch sequenced preceded by TOUCH_ACTION_NONE is not affected by
   1089 // the touch timeout.
   1090 TEST_F(InputRouterImplTest,
   1091        TouchAckTimeoutDisabledForTouchSequenceAfterTouchActionNone) {
   1092   const int timeout_ms = 1;
   1093   SetUpForTouchAckTimeoutTest(timeout_ms);
   1094   ASSERT_TRUE(TouchEventTimeoutEnabled());
   1095   OnHasTouchEventHandlers(true);
   1096 
   1097   // Start a touch sequence.
   1098   PressTouchPoint(1, 1);
   1099   SendTouchEvent();
   1100 
   1101   // TOUCH_ACTION_NONE should disable the timeout.
   1102   OnSetTouchAction(TOUCH_ACTION_NONE);
   1103   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
   1104   EXPECT_FALSE(TouchEventTimeoutEnabled());
   1105 
   1106   // End the touch sequence.
   1107   ReleaseTouchPoint(0);
   1108   SendTouchEvent();
   1109   SendInputEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED);
   1110   EXPECT_FALSE(TouchEventTimeoutEnabled());
   1111   ack_handler_->GetAndResetAckCount();
   1112   GetSentMessageCountAndResetSink();
   1113 
   1114   // Start another touch sequence.  While this does restore the touch timeout
   1115   // the timeout will not apply until the *next* touch sequence. This affords a
   1116   // touch-action response from the renderer without racing against the timeout.
   1117   PressTouchPoint(1, 1);
   1118   SendTouchEvent();
   1119   EXPECT_TRUE(TouchEventTimeoutEnabled());
   1120   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1121 
   1122   // Delay the ack.  The timeout should *not* fire.
   1123   RunTasksAndWait(base::TimeDelta::FromMilliseconds(timeout_ms + 1));
   1124   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
   1125   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1126 
   1127   // Finally send the ack.  The touch sequence should not have been cancelled.
   1128   SendInputEventACK(WebInputEvent::TouchStart,
   1129                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1130   EXPECT_TRUE(TouchEventTimeoutEnabled());
   1131   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1132   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1133 
   1134   // End the sequence.
   1135   ReleaseTouchPoint(0);
   1136   SendTouchEvent();
   1137   SendInputEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED);
   1138   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1139   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1140 
   1141   // A new touch sequence should (finally) be subject to the timeout.
   1142   PressTouchPoint(1, 1);
   1143   SendTouchEvent();
   1144   EXPECT_TRUE(TouchEventTimeoutEnabled());
   1145   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
   1146   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1147 
   1148   // Wait for the touch ack timeout to fire.
   1149   RunTasksAndWait(base::TimeDelta::FromMilliseconds(timeout_ms + 1));
   1150   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1151 }
   1152 
   1153 // Test that TouchActionFilter::ResetTouchAction is called before the
   1154 // first touch event for a touch sequence reaches the renderer.
   1155 TEST_F(InputRouterImplTest, TouchActionResetBeforeEventReachesRenderer) {
   1156   OnHasTouchEventHandlers(true);
   1157 
   1158   // Sequence 1.
   1159   PressTouchPoint(1, 1);
   1160   SendTouchEvent();
   1161   OnSetTouchAction(TOUCH_ACTION_NONE);
   1162   MoveTouchPoint(0, 50, 50);
   1163   SendTouchEvent();
   1164   ReleaseTouchPoint(0);
   1165   SendTouchEvent();
   1166 
   1167   // Sequence 2.
   1168   PressTouchPoint(1, 1);
   1169   SendTouchEvent();
   1170   MoveTouchPoint(0, 50, 50);
   1171   SendTouchEvent();
   1172   ReleaseTouchPoint(0);
   1173   SendTouchEvent();
   1174 
   1175   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
   1176   SendInputEventACK(WebInputEvent::TouchMove, INPUT_EVENT_ACK_STATE_CONSUMED);
   1177 
   1178   // Ensure touch action is still none, as the next touch start hasn't been
   1179   // acked yet. ScrollBegin and ScrollEnd don't require acks.
   1180   EXPECT_EQ(3U, GetSentMessageCountAndResetSink());
   1181   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   1182                        blink::WebGestureDeviceTouchscreen);
   1183   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1184   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   1185                        blink::WebGestureDeviceTouchscreen);
   1186   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1187 
   1188   // This allows the next touch sequence to start.
   1189   SendInputEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED);
   1190 
   1191   // Ensure touch action has been set to auto, as a new touch sequence has
   1192   // started.
   1193   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
   1194   SendInputEventACK(WebInputEvent::TouchMove, INPUT_EVENT_ACK_STATE_CONSUMED);
   1195   EXPECT_EQ(3U, GetSentMessageCountAndResetSink());
   1196   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   1197                        blink::WebGestureDeviceTouchscreen);
   1198   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1199   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   1200                        blink::WebGestureDeviceTouchscreen);
   1201   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1202   SendInputEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED);
   1203 }
   1204 
   1205 // Test that TouchActionFilter::ResetTouchAction is called when a new touch
   1206 // sequence has no consumer.
   1207 TEST_F(InputRouterImplTest, TouchActionResetWhenTouchHasNoConsumer) {
   1208   OnHasTouchEventHandlers(true);
   1209 
   1210   // Sequence 1.
   1211   PressTouchPoint(1, 1);
   1212   SendTouchEvent();
   1213   MoveTouchPoint(0, 50, 50);
   1214   SendTouchEvent();
   1215   OnSetTouchAction(TOUCH_ACTION_NONE);
   1216   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
   1217   SendInputEventACK(WebInputEvent::TouchMove, INPUT_EVENT_ACK_STATE_CONSUMED);
   1218 
   1219   ReleaseTouchPoint(0);
   1220   SendTouchEvent();
   1221 
   1222   // Sequence 2
   1223   PressTouchPoint(1, 1);
   1224   SendTouchEvent();
   1225   MoveTouchPoint(0, 50, 50);
   1226   SendTouchEvent();
   1227   ReleaseTouchPoint(0);
   1228   SendTouchEvent();
   1229 
   1230   // Ensure we have touch-action:none. ScrollBegin and ScrollEnd don't require
   1231   // acks.
   1232   EXPECT_EQ(3U, GetSentMessageCountAndResetSink());
   1233   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   1234                        blink::WebGestureDeviceTouchscreen);
   1235   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1236   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   1237                        blink::WebGestureDeviceTouchscreen);
   1238   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1239 
   1240   SendInputEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED);
   1241   SendInputEventACK(WebInputEvent::TouchStart,
   1242                     INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
   1243 
   1244   // Ensure touch action has been set to auto, as the touch had no consumer.
   1245   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1246   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   1247                        blink::WebGestureDeviceTouchscreen);
   1248   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1249   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   1250                        blink::WebGestureDeviceTouchscreen);
   1251   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1252 }
   1253 
   1254 // Test that TouchActionFilter::ResetTouchAction is called when the touch
   1255 // handler is removed.
   1256 TEST_F(InputRouterImplTest, TouchActionResetWhenTouchHandlerRemoved) {
   1257   // Touch sequence with touch handler.
   1258   OnHasTouchEventHandlers(true);
   1259   PressTouchPoint(1, 1);
   1260   SendTouchEvent();
   1261   MoveTouchPoint(0, 50, 50);
   1262   SendTouchEvent();
   1263   OnSetTouchAction(TOUCH_ACTION_NONE);
   1264   ReleaseTouchPoint(0);
   1265   SendTouchEvent();
   1266   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1267 
   1268   // Ensure we have touch-action:none, suppressing scroll events.
   1269   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
   1270   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1271   SendInputEventACK(WebInputEvent::TouchMove,
   1272                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1273   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1274   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   1275                        blink::WebGestureDeviceTouchscreen);
   1276   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1277 
   1278   SendInputEventACK(WebInputEvent::TouchEnd,
   1279                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1280   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   1281                        blink::WebGestureDeviceTouchscreen);
   1282   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1283 
   1284   // Sequence without a touch handler. Note that in this case, the view may not
   1285   // necessarily forward touches to the router (as no touch handler exists).
   1286   OnHasTouchEventHandlers(false);
   1287 
   1288   // Ensure touch action has been set to auto, as the touch handler has been
   1289   // removed.
   1290   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   1291                        blink::WebGestureDeviceTouchscreen);
   1292   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1293   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   1294                        blink::WebGestureDeviceTouchscreen);
   1295   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1296 }
   1297 
   1298 // Test that the double tap gesture depends on the touch action of the first
   1299 // tap.
   1300 TEST_F(InputRouterImplTest, DoubleTapGestureDependsOnFirstTap) {
   1301   OnHasTouchEventHandlers(true);
   1302 
   1303   // Sequence 1.
   1304   PressTouchPoint(1, 1);
   1305   SendTouchEvent();
   1306   OnSetTouchAction(TOUCH_ACTION_NONE);
   1307   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
   1308 
   1309   ReleaseTouchPoint(0);
   1310   SendTouchEvent();
   1311 
   1312   // Sequence 2
   1313   PressTouchPoint(1, 1);
   1314   SendTouchEvent();
   1315 
   1316   // First tap.
   1317   EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
   1318   SimulateGestureEvent(WebInputEvent::GestureTapDown,
   1319                        blink::WebGestureDeviceTouchscreen);
   1320   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1321 
   1322   // The GestureTapUnconfirmed is converted into a tap, as the touch action is
   1323   // none.
   1324   SimulateGestureEvent(WebInputEvent::GestureTapUnconfirmed,
   1325                        blink::WebGestureDeviceTouchscreen);
   1326   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1327   // This test will become invalid if GestureTap stops requiring an ack.
   1328   ASSERT_FALSE(WebInputEventTraits::IgnoresAckDisposition(
   1329       GetEventWithType(WebInputEvent::GestureTap)));
   1330   EXPECT_EQ(2, client_->in_flight_event_count());
   1331   SendInputEventACK(WebInputEvent::GestureTap,
   1332                     INPUT_EVENT_ACK_STATE_CONSUMED);
   1333   EXPECT_EQ(1, client_->in_flight_event_count());
   1334 
   1335   // This tap gesture is dropped, since the GestureTapUnconfirmed was turned
   1336   // into a tap.
   1337   SimulateGestureEvent(WebInputEvent::GestureTap,
   1338                        blink::WebGestureDeviceTouchscreen);
   1339   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1340 
   1341   SendInputEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED);
   1342   SendInputEventACK(WebInputEvent::TouchStart,
   1343                     INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
   1344 
   1345   // Second Tap.
   1346   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1347   SimulateGestureEvent(WebInputEvent::GestureTapDown,
   1348                        blink::WebGestureDeviceTouchscreen);
   1349   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1350 
   1351   // Although the touch-action is now auto, the double tap still won't be
   1352   // dispatched, because the first tap occured when the touch-action was none.
   1353   SimulateGestureEvent(WebInputEvent::GestureDoubleTap,
   1354                        blink::WebGestureDeviceTouchscreen);
   1355   // This test will become invalid if GestureDoubleTap stops requiring an ack.
   1356   ASSERT_FALSE(WebInputEventTraits::IgnoresAckDisposition(
   1357       GetEventWithType(WebInputEvent::GestureDoubleTap)));
   1358   EXPECT_EQ(1, client_->in_flight_event_count());
   1359   SendInputEventACK(WebInputEvent::GestureTap, INPUT_EVENT_ACK_STATE_CONSUMED);
   1360   EXPECT_EQ(0, client_->in_flight_event_count());
   1361 }
   1362 
   1363 // Test that the router will call the client's |DidFlush| after all events have
   1364 // been dispatched following a call to |Flush|.
   1365 TEST_F(InputRouterImplTest, InputFlush) {
   1366   EXPECT_FALSE(HasPendingEvents());
   1367 
   1368   // Flushing an empty router should immediately trigger DidFlush.
   1369   Flush();
   1370   EXPECT_EQ(1U, GetAndResetDidFlushCount());
   1371   EXPECT_FALSE(HasPendingEvents());
   1372 
   1373   // Queue a TouchStart.
   1374   OnHasTouchEventHandlers(true);
   1375   PressTouchPoint(1, 1);
   1376   SendTouchEvent();
   1377   EXPECT_TRUE(HasPendingEvents());
   1378 
   1379   // DidFlush should be called only after the event is ack'ed.
   1380   Flush();
   1381   EXPECT_EQ(0U, GetAndResetDidFlushCount());
   1382   SendInputEventACK(WebInputEvent::TouchStart,
   1383                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1384   EXPECT_EQ(1U, GetAndResetDidFlushCount());
   1385 
   1386   // Ensure different types of enqueued events will prevent the DidFlush call
   1387   // until all such events have been fully dispatched.
   1388   MoveTouchPoint(0, 50, 50);
   1389   SendTouchEvent();
   1390   ASSERT_TRUE(HasPendingEvents());
   1391   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   1392                        blink::WebGestureDeviceTouchscreen);
   1393   SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
   1394                        blink::WebGestureDeviceTouchscreen);
   1395   SimulateGestureEvent(WebInputEvent::GesturePinchBegin,
   1396                        blink::WebGestureDeviceTouchscreen);
   1397   SimulateGestureEvent(WebInputEvent::GesturePinchUpdate,
   1398                        blink::WebGestureDeviceTouchscreen);
   1399   Flush();
   1400   EXPECT_EQ(0U, GetAndResetDidFlushCount());
   1401 
   1402   // Repeated flush calls should have no effect.
   1403   Flush();
   1404   EXPECT_EQ(0U, GetAndResetDidFlushCount());
   1405 
   1406   // There are still pending gestures.
   1407   SendInputEventACK(WebInputEvent::TouchMove,
   1408                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1409   EXPECT_EQ(0U, GetAndResetDidFlushCount());
   1410   EXPECT_TRUE(HasPendingEvents());
   1411 
   1412   // One more gesture to go.
   1413   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   1414                     INPUT_EVENT_ACK_STATE_CONSUMED);
   1415   EXPECT_EQ(0U, GetAndResetDidFlushCount());
   1416   EXPECT_TRUE(HasPendingEvents());
   1417 
   1418   // The final ack'ed gesture should trigger the DidFlush.
   1419   SendInputEventACK(WebInputEvent::GesturePinchUpdate,
   1420                     INPUT_EVENT_ACK_STATE_CONSUMED);
   1421   EXPECT_EQ(1U, GetAndResetDidFlushCount());
   1422   EXPECT_FALSE(HasPendingEvents());
   1423 }
   1424 
   1425 // Test that GesturePinchUpdate is handled specially for trackpad
   1426 TEST_F(InputRouterImplTest, TouchpadPinchUpdate) {
   1427   // GesturePinchUpdate for trackpad sends synthetic wheel events.
   1428   // Note that the Touchscreen case is verified as NOT doing this as
   1429   // part of the ShowPressIsInOrder test.
   1430 
   1431   SimulateGesturePinchUpdateEvent(
   1432       1.5f, 20, 25, 0, blink::WebGestureDeviceTouchpad);
   1433 
   1434   // Verify we actually sent a special wheel event to the renderer.
   1435   const WebInputEvent* input_event =
   1436       GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
   1437   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
   1438   const WebMouseWheelEvent* wheel_event =
   1439       static_cast<const WebMouseWheelEvent*>(input_event);
   1440   EXPECT_EQ(20, wheel_event->x);
   1441   EXPECT_EQ(25, wheel_event->y);
   1442   EXPECT_EQ(20, wheel_event->globalX);
   1443   EXPECT_EQ(25, wheel_event->globalY);
   1444   EXPECT_EQ(20, wheel_event->windowX);
   1445   EXPECT_EQ(25, wheel_event->windowY);
   1446   EXPECT_EQ(PinchScaleToWheelDelta(1.5), wheel_event->deltaY);
   1447   EXPECT_EQ(0, wheel_event->deltaX);
   1448   EXPECT_EQ(1, wheel_event->hasPreciseScrollingDeltas);
   1449   EXPECT_EQ(1, wheel_event->wheelTicksY);
   1450   EXPECT_EQ(0, wheel_event->wheelTicksX);
   1451   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1452 
   1453   // Indicate that the wheel event was unhandled.
   1454   SendInputEventACK(WebInputEvent::MouseWheel,
   1455                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1456 
   1457   // Check that the correct unhandled pinch event was received.
   1458   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1459   ASSERT_EQ(WebInputEvent::GesturePinchUpdate, ack_handler_->ack_event_type());
   1460   EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, ack_handler_->ack_state());
   1461   EXPECT_EQ(1.5f, ack_handler_->acked_gesture_event().data.pinchUpdate.scale);
   1462   EXPECT_EQ(0, client_->in_flight_event_count());
   1463 
   1464   // Second a second pinch event.
   1465   SimulateGesturePinchUpdateEvent(
   1466       0.3f, 20, 25, 0, blink::WebGestureDeviceTouchpad);
   1467   input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
   1468   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
   1469   wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
   1470   EXPECT_FLOAT_EQ(PinchScaleToWheelDelta(0.3f), wheel_event->deltaY);
   1471   EXPECT_EQ(1, wheel_event->hasPreciseScrollingDeltas);
   1472   EXPECT_EQ(-1, wheel_event->wheelTicksY);
   1473   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1474 
   1475   // Indicate that the wheel event was handled this time.
   1476   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
   1477 
   1478   // Check that the correct HANDLED pinch event was received.
   1479   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1480   EXPECT_EQ(WebInputEvent::GesturePinchUpdate, ack_handler_->ack_event_type());
   1481   EXPECT_EQ(INPUT_EVENT_ACK_STATE_CONSUMED, ack_handler_->ack_state());
   1482   EXPECT_FLOAT_EQ(0.3f,
   1483                   ack_handler_->acked_gesture_event().data.pinchUpdate.scale);
   1484 }
   1485 
   1486 // Test that touchpad pinch events are coalesced property, with their synthetic
   1487 // wheel events getting the right ACKs.
   1488 TEST_F(InputRouterImplTest, TouchpadPinchCoalescing) {
   1489   // Send the first pinch.
   1490   SimulateGesturePinchUpdateEvent(
   1491       1.5f, 20, 25, 0, blink::WebGestureDeviceTouchpad);
   1492 
   1493   // Verify we sent the wheel event to the renderer.
   1494   const WebInputEvent* input_event =
   1495       GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
   1496   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
   1497   const WebMouseWheelEvent* wheel_event =
   1498       static_cast<const WebMouseWheelEvent*>(input_event);
   1499   EXPECT_EQ(PinchScaleToWheelDelta(1.5f), wheel_event->deltaY);
   1500   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1501   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
   1502   EXPECT_EQ(1, client_->in_flight_event_count());
   1503 
   1504   // Send a second pinch, this should be queued in the GestureEventQueue.
   1505   SimulateGesturePinchUpdateEvent(
   1506       1.6f, 20, 25, 0, blink::WebGestureDeviceTouchpad);
   1507   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1508   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
   1509 
   1510   // Send a third pinch, this should be coalesced into the second in the
   1511   // GestureEventQueue.
   1512   SimulateGesturePinchUpdateEvent(
   1513       1.7f, 20, 25, 0, blink::WebGestureDeviceTouchpad);
   1514   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1515   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
   1516 
   1517   // Indicate that the first wheel event was unhandled and verify the ACK.
   1518   SendInputEventACK(WebInputEvent::MouseWheel,
   1519                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1520   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1521   EXPECT_EQ(WebInputEvent::GesturePinchUpdate, ack_handler_->ack_event_type());
   1522   EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, ack_handler_->ack_state());
   1523   EXPECT_EQ(1.5f, ack_handler_->acked_gesture_event().data.pinchUpdate.scale);
   1524 
   1525   // Verify a second wheel event was sent representing the 2nd and 3rd pinch
   1526   // events.
   1527   EXPECT_EQ(1, client_->in_flight_event_count());
   1528   input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
   1529   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
   1530   wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
   1531   EXPECT_FLOAT_EQ(PinchScaleToWheelDelta(1.6f * 1.7f),
   1532                   PinchScaleToWheelDelta(1.6f) + PinchScaleToWheelDelta(1.7f));
   1533   EXPECT_FLOAT_EQ(PinchScaleToWheelDelta(1.6f * 1.7f), wheel_event->deltaY);
   1534   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1535   EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
   1536 
   1537   // Indicate that the second wheel event was handled and verify the ACK.
   1538   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
   1539   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1540   EXPECT_EQ(WebInputEvent::GesturePinchUpdate, ack_handler_->ack_event_type());
   1541   EXPECT_EQ(INPUT_EVENT_ACK_STATE_CONSUMED, ack_handler_->ack_state());
   1542   EXPECT_FLOAT_EQ(1.6f * 1.7f,
   1543                   ack_handler_->acked_gesture_event().data.pinchUpdate.scale);
   1544 }
   1545 
   1546 // Test interleaving pinch and wheel events.
   1547 TEST_F(InputRouterImplTest, TouchpadPinchAndWheel) {
   1548   // Simulate queued wheel and pinch events events.
   1549   // Note that in practice interleaving pinch and wheel events should be rare
   1550   // (eg. requires the use of a mouse and trackpad at the same time).
   1551 
   1552   // Use the control modifier to match the synthetic wheel events so that
   1553   // they're elligble for coalescing.
   1554   int mod = WebInputEvent::ControlKey;
   1555 
   1556   // Event 1: sent directly.
   1557   SimulateWheelEvent(0, -5, mod, true);
   1558   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1559 
   1560   // Event 2: enqueued in InputRouter.
   1561   SimulateWheelEvent(0, -10, mod, true);
   1562   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1563 
   1564   // Event 3: enqueued in InputRouter, not coalesced into #2.
   1565   SimulateGesturePinchUpdateEvent(
   1566       1.5f, 20, 25, 0, blink::WebGestureDeviceTouchpad);
   1567   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1568 
   1569   // Event 4: enqueued in GestureEventQueue.
   1570   SimulateGesturePinchUpdateEvent(
   1571       1.2f, 20, 25, 0, blink::WebGestureDeviceTouchpad);
   1572   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1573 
   1574   // Event 5: coalesced into wheel event for #3.
   1575   SimulateWheelEvent(2, 0, mod, true);
   1576   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1577 
   1578   // Send ack for #1.
   1579   SendInputEventACK(WebInputEvent::MouseWheel,
   1580                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1581   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1582   EXPECT_EQ(WebInputEvent::MouseWheel, ack_handler_->ack_event_type());
   1583 
   1584   // Verify we sent #2.
   1585   ASSERT_EQ(1U, process_->sink().message_count());
   1586   const WebInputEvent* input_event =
   1587       GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
   1588   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
   1589   const WebMouseWheelEvent* wheel_event =
   1590       static_cast<const WebMouseWheelEvent*>(input_event);
   1591   EXPECT_EQ(0, wheel_event->deltaX);
   1592   EXPECT_EQ(-10, wheel_event->deltaY);
   1593   EXPECT_EQ(mod, wheel_event->modifiers);
   1594   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1595 
   1596   // Send ack for #2.
   1597   SendInputEventACK(WebInputEvent::MouseWheel,
   1598                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1599   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1600   EXPECT_EQ(WebInputEvent::MouseWheel, ack_handler_->ack_event_type());
   1601 
   1602   // Verify we sent #3 (with #5 coalesced in).
   1603   ASSERT_EQ(1U, process_->sink().message_count());
   1604   input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
   1605   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
   1606   wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
   1607   EXPECT_EQ(2, wheel_event->deltaX);
   1608   EXPECT_EQ(PinchScaleToWheelDelta(1.5f), wheel_event->deltaY);
   1609   EXPECT_EQ(mod, wheel_event->modifiers);
   1610   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1611 
   1612   // Send ack for #3.
   1613   SendInputEventACK(WebInputEvent::MouseWheel,
   1614                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1615   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1616   EXPECT_EQ(WebInputEvent::GesturePinchUpdate, ack_handler_->ack_event_type());
   1617 
   1618   // Verify we sent #4.
   1619   ASSERT_EQ(1U, process_->sink().message_count());
   1620   input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
   1621   ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
   1622   wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
   1623   EXPECT_EQ(0, wheel_event->deltaX);
   1624   EXPECT_FLOAT_EQ(PinchScaleToWheelDelta(1.2f), wheel_event->deltaY);
   1625   EXPECT_EQ(mod, wheel_event->modifiers);
   1626   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1627 
   1628   // Send ack for #4.
   1629   SendInputEventACK(WebInputEvent::MouseWheel,
   1630                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1631   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1632   EXPECT_EQ(WebInputEvent::GesturePinchUpdate, ack_handler_->ack_event_type());
   1633 }
   1634 
   1635 // Test proper handling of touchpad Gesture{Pinch,Scroll}Update sequences.
   1636 TEST_F(InputRouterImplTest, TouchpadPinchAndScrollUpdate) {
   1637   // The first scroll should be sent immediately.
   1638   SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
   1639                        blink::WebGestureDeviceTouchpad);
   1640   ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
   1641   EXPECT_EQ(1, client_->in_flight_event_count());
   1642 
   1643   // Subsequent scroll and pinch events should remain queued, coalescing as
   1644   // more trackpad events arrive.
   1645   SimulateGestureEvent(WebInputEvent::GesturePinchUpdate,
   1646                        blink::WebGestureDeviceTouchpad);
   1647   ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
   1648   EXPECT_EQ(1, client_->in_flight_event_count());
   1649 
   1650   SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
   1651                        blink::WebGestureDeviceTouchpad);
   1652   ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
   1653   EXPECT_EQ(1, client_->in_flight_event_count());
   1654 
   1655   SimulateGestureEvent(WebInputEvent::GesturePinchUpdate,
   1656                        blink::WebGestureDeviceTouchpad);
   1657   ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
   1658   EXPECT_EQ(1, client_->in_flight_event_count());
   1659 
   1660   SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
   1661                        blink::WebGestureDeviceTouchpad);
   1662   ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
   1663   EXPECT_EQ(1, client_->in_flight_event_count());
   1664 
   1665   // Ack'ing the first scroll should trigger both the coalesced scroll and the
   1666   // coalesced pinch events (which is sent to the renderer as a wheel event).
   1667   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   1668                     INPUT_EVENT_ACK_STATE_CONSUMED);
   1669   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1670   EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
   1671   EXPECT_EQ(2, client_->in_flight_event_count());
   1672 
   1673   // Ack the second scroll.
   1674   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   1675                     INPUT_EVENT_ACK_STATE_CONSUMED);
   1676   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1677   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1678   EXPECT_EQ(1, client_->in_flight_event_count());
   1679 
   1680   // Ack the wheel event.
   1681   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
   1682   EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
   1683   EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
   1684   EXPECT_EQ(0, client_->in_flight_event_count());
   1685 }
   1686 
   1687 // Test proper routing of overscroll notifications received either from
   1688 // event acks or from |DidOverscroll| IPC messages.
   1689 TEST_F(InputRouterImplTest, OverscrollDispatch) {
   1690   DidOverscrollParams overscroll;
   1691   overscroll.accumulated_overscroll = gfx::Vector2dF(-14, 14);
   1692   overscroll.latest_overscroll_delta = gfx::Vector2dF(-7, 0);
   1693   overscroll.current_fling_velocity = gfx::Vector2dF(-1, 0);
   1694 
   1695   input_router_->OnMessageReceived(InputHostMsg_DidOverscroll(0, overscroll));
   1696   DidOverscrollParams client_overscroll = client_->GetAndResetOverscroll();
   1697   EXPECT_EQ(overscroll.accumulated_overscroll,
   1698             client_overscroll.accumulated_overscroll);
   1699   EXPECT_EQ(overscroll.latest_overscroll_delta,
   1700             client_overscroll.latest_overscroll_delta);
   1701   EXPECT_EQ(overscroll.current_fling_velocity,
   1702             client_overscroll.current_fling_velocity);
   1703 
   1704   DidOverscrollParams wheel_overscroll;
   1705   wheel_overscroll.accumulated_overscroll = gfx::Vector2dF(7, -7);
   1706   wheel_overscroll.latest_overscroll_delta = gfx::Vector2dF(3, 0);
   1707   wheel_overscroll.current_fling_velocity = gfx::Vector2dF(1, 0);
   1708 
   1709   SimulateWheelEvent(3, 0, 0, false);
   1710   InputHostMsg_HandleInputEvent_ACK_Params ack;
   1711   ack.type = WebInputEvent::MouseWheel;
   1712   ack.state = INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
   1713   ack.overscroll.reset(new DidOverscrollParams(wheel_overscroll));
   1714   input_router_->OnMessageReceived(InputHostMsg_HandleInputEvent_ACK(0, ack));
   1715 
   1716   client_overscroll = client_->GetAndResetOverscroll();
   1717   EXPECT_EQ(wheel_overscroll.accumulated_overscroll,
   1718             client_overscroll.accumulated_overscroll);
   1719   EXPECT_EQ(wheel_overscroll.latest_overscroll_delta,
   1720             client_overscroll.latest_overscroll_delta);
   1721   EXPECT_EQ(wheel_overscroll.current_fling_velocity,
   1722             client_overscroll.current_fling_velocity);
   1723 }
   1724 
   1725 }  // namespace content
   1726