Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / input / input_router_impl_unittest.cc
blob034eb5ba27b2b3331739dd2c0ff9348903f5fa92
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.
5 #include <math.h>
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "content/browser/renderer_host/input/gesture_event_queue.h"
15 #include "content/browser/renderer_host/input/input_router_client.h"
16 #include "content/browser/renderer_host/input/input_router_impl.h"
17 #include "content/browser/renderer_host/input/mock_input_ack_handler.h"
18 #include "content/browser/renderer_host/input/mock_input_router_client.h"
19 #include "content/common/content_constants_internal.h"
20 #include "content/common/edit_command.h"
21 #include "content/common/input/synthetic_web_input_event_builders.h"
22 #include "content/common/input/touch_action.h"
23 #include "content/common/input/web_input_event_traits.h"
24 #include "content/common/input_messages.h"
25 #include "content/common/view_messages.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/test/mock_render_process_host.h"
28 #include "content/public/test/test_browser_context.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "ui/events/keycodes/keyboard_codes.h"
32 #if defined(USE_AURA)
33 #include "content/browser/renderer_host/ui_events_helper.h"
34 #include "ui/events/event.h"
35 #endif
37 using base::TimeDelta;
38 using blink::WebGestureDevice;
39 using blink::WebGestureEvent;
40 using blink::WebKeyboardEvent;
41 using blink::WebInputEvent;
42 using blink::WebMouseEvent;
43 using blink::WebMouseWheelEvent;
44 using blink::WebTouchEvent;
45 using blink::WebTouchPoint;
47 namespace content {
49 namespace {
51 const WebInputEvent* GetInputEventFromMessage(const IPC::Message& message) {
52 base::PickleIterator iter(message);
53 const char* data;
54 int data_length;
55 if (!iter.ReadData(&data, &data_length))
56 return NULL;
57 return reinterpret_cast<const WebInputEvent*>(data);
60 WebInputEvent& GetEventWithType(WebInputEvent::Type type) {
61 WebInputEvent* event = NULL;
62 if (WebInputEvent::isMouseEventType(type)) {
63 static WebMouseEvent mouse;
64 event = &mouse;
65 } else if (WebInputEvent::isTouchEventType(type)) {
66 static WebTouchEvent touch;
67 event = &touch;
68 } else if (WebInputEvent::isKeyboardEventType(type)) {
69 static WebKeyboardEvent key;
70 event = &key;
71 } else if (WebInputEvent::isGestureEventType(type)) {
72 static WebGestureEvent gesture;
73 event = &gesture;
74 } else if (type == WebInputEvent::MouseWheel) {
75 static WebMouseWheelEvent wheel;
76 event = &wheel;
78 CHECK(event);
79 event->type = type;
80 return *event;
83 bool GetIsShortcutFromHandleInputEventMessage(const IPC::Message* msg) {
84 InputMsg_HandleInputEvent::Schema::Param param;
85 InputMsg_HandleInputEvent::Read(msg, &param);
86 return base::get<2>(param);
89 template<typename MSG_T, typename ARG_T1>
90 void ExpectIPCMessageWithArg1(const IPC::Message* msg, const ARG_T1& arg1) {
91 ASSERT_EQ(MSG_T::ID, msg->type());
92 typename MSG_T::Schema::Param param;
93 ASSERT_TRUE(MSG_T::Read(msg, &param));
94 EXPECT_EQ(arg1, base::get<0>(param));
97 template<typename MSG_T, typename ARG_T1, typename ARG_T2>
98 void ExpectIPCMessageWithArg2(const IPC::Message* msg,
99 const ARG_T1& arg1,
100 const ARG_T2& arg2) {
101 ASSERT_EQ(MSG_T::ID, msg->type());
102 typename MSG_T::Schema::Param param;
103 ASSERT_TRUE(MSG_T::Read(msg, &param));
104 EXPECT_EQ(arg1, base::get<0>(param));
105 EXPECT_EQ(arg2, base::get<1>(param));
108 #if defined(USE_AURA)
109 bool TouchEventsAreEquivalent(const ui::TouchEvent& first,
110 const ui::TouchEvent& second) {
111 if (first.type() != second.type())
112 return false;
113 if (first.location() != second.location())
114 return false;
115 if (first.touch_id() != second.touch_id())
116 return false;
117 if (second.time_stamp().InSeconds() != first.time_stamp().InSeconds())
118 return false;
119 return true;
122 bool EventListIsSubset(const ScopedVector<ui::TouchEvent>& subset,
123 const ScopedVector<ui::TouchEvent>& set) {
124 if (subset.size() > set.size())
125 return false;
126 for (size_t i = 0; i < subset.size(); ++i) {
127 const ui::TouchEvent* first = subset[i];
128 const ui::TouchEvent* second = set[i];
129 bool equivalent = TouchEventsAreEquivalent(*first, *second);
130 if (!equivalent)
131 return false;
134 return true;
136 #endif // defined(USE_AURA)
138 } // namespace
140 class InputRouterImplTest : public testing::Test {
141 public:
142 InputRouterImplTest() {}
143 ~InputRouterImplTest() override {}
145 protected:
146 // testing::Test
147 void SetUp() override {
148 browser_context_.reset(new TestBrowserContext());
149 process_.reset(new MockRenderProcessHost(browser_context_.get()));
150 client_.reset(new MockInputRouterClient());
151 ack_handler_.reset(new MockInputAckHandler());
152 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
153 command_line->AppendSwitch(switches::kValidateInputEventStream);
154 input_router_.reset(new InputRouterImpl(process_.get(),
155 client_.get(),
156 ack_handler_.get(),
157 MSG_ROUTING_NONE,
158 config_));
159 client_->set_input_router(input_router());
160 ack_handler_->set_input_router(input_router());
163 void TearDown() override {
164 // Process all pending tasks to avoid leaks.
165 base::MessageLoop::current()->RunUntilIdle();
167 input_router_.reset();
168 client_.reset();
169 process_.reset();
170 browser_context_.reset();
173 void SetUpForTouchAckTimeoutTest(int desktop_timeout_ms,
174 int mobile_timeout_ms) {
175 config_.touch_config.desktop_touch_ack_timeout_delay =
176 base::TimeDelta::FromMilliseconds(desktop_timeout_ms);
177 config_.touch_config.mobile_touch_ack_timeout_delay =
178 base::TimeDelta::FromMilliseconds(mobile_timeout_ms);
179 config_.touch_config.touch_ack_timeout_supported = true;
180 TearDown();
181 SetUp();
182 input_router()->NotifySiteIsMobileOptimized(false);
185 void SimulateKeyboardEvent(WebInputEvent::Type type, bool is_shortcut) {
186 WebKeyboardEvent event = SyntheticWebKeyboardEventBuilder::Build(type);
187 NativeWebKeyboardEvent native_event;
188 memcpy(&native_event, &event, sizeof(event));
189 NativeWebKeyboardEventWithLatencyInfo key_event(native_event);
190 input_router_->SendKeyboardEvent(key_event, is_shortcut);
193 void SimulateWheelEvent(float dX, float dY, int modifiers, bool precise) {
194 input_router_->SendWheelEvent(MouseWheelEventWithLatencyInfo(
195 SyntheticWebMouseWheelEventBuilder::Build(dX, dY, modifiers, precise)));
198 void SimulateMouseEvent(WebInputEvent::Type type, int x, int y) {
199 input_router_->SendMouseEvent(MouseEventWithLatencyInfo(
200 SyntheticWebMouseEventBuilder::Build(type, x, y, 0)));
203 void SimulateWheelEventWithPhase(WebMouseWheelEvent::Phase phase) {
204 input_router_->SendWheelEvent(MouseWheelEventWithLatencyInfo(
205 SyntheticWebMouseWheelEventBuilder::Build(phase)));
208 void SimulateGestureEvent(WebGestureEvent gesture) {
209 // Ensure non-zero touchscreen fling velocities, as the router will
210 // validate aganst such.
211 if (gesture.type == WebInputEvent::GestureFlingStart &&
212 gesture.sourceDevice == blink::WebGestureDeviceTouchscreen &&
213 !gesture.data.flingStart.velocityX &&
214 !gesture.data.flingStart.velocityY) {
215 gesture.data.flingStart.velocityX = 5.f;
218 input_router_->SendGestureEvent(GestureEventWithLatencyInfo(gesture));
221 void SimulateGestureEvent(WebInputEvent::Type type,
222 WebGestureDevice source_device) {
223 SimulateGestureEvent(
224 SyntheticWebGestureEventBuilder::Build(type, source_device));
227 void SimulateGestureScrollUpdateEvent(float dX,
228 float dY,
229 int modifiers,
230 WebGestureDevice source_device) {
231 SimulateGestureEvent(SyntheticWebGestureEventBuilder::BuildScrollUpdate(
232 dX, dY, modifiers, source_device));
235 void SimulateGesturePinchUpdateEvent(float scale,
236 float anchor_x,
237 float anchor_y,
238 int modifiers,
239 WebGestureDevice source_device) {
240 SimulateGestureEvent(SyntheticWebGestureEventBuilder::BuildPinchUpdate(
241 scale, anchor_x, anchor_y, modifiers, source_device));
244 void SimulateGestureFlingStartEvent(float velocity_x,
245 float velocity_y,
246 WebGestureDevice source_device) {
247 SimulateGestureEvent(SyntheticWebGestureEventBuilder::BuildFling(
248 velocity_x, velocity_y, source_device));
251 void SetTouchTimestamp(base::TimeDelta timestamp) {
252 touch_event_.SetTimestamp(timestamp);
255 uint32 SendTouchEvent() {
256 uint32 touch_event_id = touch_event_.uniqueTouchEventId;
257 input_router_->SendTouchEvent(TouchEventWithLatencyInfo(touch_event_));
258 touch_event_.ResetPoints();
259 return touch_event_id;
262 int PressTouchPoint(int x, int y) {
263 return touch_event_.PressPoint(x, y);
266 void MoveTouchPoint(int index, int x, int y) {
267 touch_event_.MovePoint(index, x, y);
270 void ReleaseTouchPoint(int index) {
271 touch_event_.ReleasePoint(index);
274 void CancelTouchPoint(int index) {
275 touch_event_.CancelPoint(index);
278 void SendInputEventACK(blink::WebInputEvent::Type type,
279 InputEventAckState ack_result) {
280 DCHECK(!WebInputEvent::isTouchEventType(type));
281 InputEventAck ack(type, ack_result);
282 input_router_->OnMessageReceived(InputHostMsg_HandleInputEvent_ACK(0, ack));
285 void SendTouchEventACK(blink::WebInputEvent::Type type,
286 InputEventAckState ack_result,
287 uint32 touch_event_id) {
288 DCHECK(WebInputEvent::isTouchEventType(type));
289 InputEventAck ack(type, ack_result, touch_event_id);
290 input_router_->OnMessageReceived(InputHostMsg_HandleInputEvent_ACK(0, ack));
293 InputRouterImpl* input_router() const {
294 return input_router_.get();
297 bool TouchEventQueueEmpty() const {
298 return input_router()->touch_event_queue_.empty();
301 bool TouchEventTimeoutEnabled() const {
302 return input_router()->touch_event_queue_.IsAckTimeoutEnabled();
305 void RequestNotificationWhenFlushed() const {
306 return input_router_->RequestNotificationWhenFlushed();
309 size_t GetAndResetDidFlushCount() {
310 return client_->GetAndResetDidFlushCount();
313 bool HasPendingEvents() const {
314 return input_router_->HasPendingEvents();
317 void OnHasTouchEventHandlers(bool has_handlers) {
318 input_router_->OnMessageReceived(
319 ViewHostMsg_HasTouchEventHandlers(0, has_handlers));
322 void OnSetTouchAction(content::TouchAction touch_action) {
323 input_router_->OnMessageReceived(
324 InputHostMsg_SetTouchAction(0, touch_action));
327 size_t GetSentMessageCountAndResetSink() {
328 size_t count = process_->sink().message_count();
329 process_->sink().ClearMessages();
330 return count;
333 static void RunTasksAndWait(base::TimeDelta delay) {
334 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
335 FROM_HERE, base::MessageLoop::QuitClosure(), delay);
336 base::MessageLoop::current()->Run();
339 InputRouterImpl::Config config_;
340 scoped_ptr<MockRenderProcessHost> process_;
341 scoped_ptr<MockInputRouterClient> client_;
342 scoped_ptr<MockInputAckHandler> ack_handler_;
343 scoped_ptr<InputRouterImpl> input_router_;
345 private:
346 base::MessageLoopForUI message_loop_;
347 SyntheticWebTouchEvent touch_event_;
349 scoped_ptr<TestBrowserContext> browser_context_;
352 TEST_F(InputRouterImplTest, CoalescesRangeSelection) {
353 input_router_->SendInput(scoped_ptr<IPC::Message>(
354 new InputMsg_SelectRange(0, gfx::Point(1, 2), gfx::Point(3, 4))));
355 ExpectIPCMessageWithArg2<InputMsg_SelectRange>(
356 process_->sink().GetMessageAt(0),
357 gfx::Point(1, 2),
358 gfx::Point(3, 4));
359 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
361 // Send two more messages without acking.
362 input_router_->SendInput(scoped_ptr<IPC::Message>(
363 new InputMsg_SelectRange(0, gfx::Point(5, 6), gfx::Point(7, 8))));
364 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
366 input_router_->SendInput(scoped_ptr<IPC::Message>(
367 new InputMsg_SelectRange(0, gfx::Point(9, 10), gfx::Point(11, 12))));
368 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
370 // Now ack the first message.
372 scoped_ptr<IPC::Message> response(new InputHostMsg_SelectRange_ACK(0));
373 input_router_->OnMessageReceived(*response);
376 // Verify that the two messages are coalesced into one message.
377 ExpectIPCMessageWithArg2<InputMsg_SelectRange>(
378 process_->sink().GetMessageAt(0),
379 gfx::Point(9, 10),
380 gfx::Point(11, 12));
381 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
383 // Acking the coalesced msg should not send any more msg.
385 scoped_ptr<IPC::Message> response(new InputHostMsg_SelectRange_ACK(0));
386 input_router_->OnMessageReceived(*response);
388 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
391 TEST_F(InputRouterImplTest, CoalescesMoveRangeSelectionExtent) {
392 input_router_->SendInput(scoped_ptr<IPC::Message>(
393 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(1, 2))));
394 ExpectIPCMessageWithArg1<InputMsg_MoveRangeSelectionExtent>(
395 process_->sink().GetMessageAt(0),
396 gfx::Point(1, 2));
397 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
399 // Send two more messages without acking.
400 input_router_->SendInput(scoped_ptr<IPC::Message>(
401 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(3, 4))));
402 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
404 input_router_->SendInput(scoped_ptr<IPC::Message>(
405 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(5, 6))));
406 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
408 // Now ack the first message.
410 scoped_ptr<IPC::Message> response(
411 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
412 input_router_->OnMessageReceived(*response);
415 // Verify that the two messages are coalesced into one message.
416 ExpectIPCMessageWithArg1<InputMsg_MoveRangeSelectionExtent>(
417 process_->sink().GetMessageAt(0),
418 gfx::Point(5, 6));
419 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
421 // Acking the coalesced msg should not send any more msg.
423 scoped_ptr<IPC::Message> response(
424 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
425 input_router_->OnMessageReceived(*response);
427 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
430 TEST_F(InputRouterImplTest, InterleaveSelectRangeAndMoveRangeSelectionExtent) {
431 // Send first message: SelectRange.
432 input_router_->SendInput(scoped_ptr<IPC::Message>(
433 new InputMsg_SelectRange(0, gfx::Point(1, 2), gfx::Point(3, 4))));
434 ExpectIPCMessageWithArg2<InputMsg_SelectRange>(
435 process_->sink().GetMessageAt(0),
436 gfx::Point(1, 2),
437 gfx::Point(3, 4));
438 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
440 // Send second message: MoveRangeSelectionExtent.
441 input_router_->SendInput(scoped_ptr<IPC::Message>(
442 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(5, 6))));
443 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
445 // Send third message: SelectRange.
446 input_router_->SendInput(scoped_ptr<IPC::Message>(
447 new InputMsg_SelectRange(0, gfx::Point(7, 8), gfx::Point(9, 10))));
448 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
450 // Ack the messages and verify that they're not coalesced and that they're in
451 // correct order.
453 // Ack the first message.
455 scoped_ptr<IPC::Message> response(
456 new InputHostMsg_SelectRange_ACK(0));
457 input_router_->OnMessageReceived(*response);
460 ExpectIPCMessageWithArg1<InputMsg_MoveRangeSelectionExtent>(
461 process_->sink().GetMessageAt(0),
462 gfx::Point(5, 6));
463 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
465 // Ack the second message.
467 scoped_ptr<IPC::Message> response(
468 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
469 input_router_->OnMessageReceived(*response);
472 ExpectIPCMessageWithArg2<InputMsg_SelectRange>(
473 process_->sink().GetMessageAt(0),
474 gfx::Point(7, 8),
475 gfx::Point(9, 10));
476 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
478 // Ack the third message.
480 scoped_ptr<IPC::Message> response(
481 new InputHostMsg_SelectRange_ACK(0));
482 input_router_->OnMessageReceived(*response);
484 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
487 TEST_F(InputRouterImplTest,
488 CoalescesInterleavedSelectRangeAndMoveRangeSelectionExtent) {
489 // Send interleaved SelectRange and MoveRangeSelectionExtent messages. They
490 // should be coalesced as shown by the arrows.
491 // > SelectRange
492 // MoveRangeSelectionExtent
493 // MoveRangeSelectionExtent
494 // > MoveRangeSelectionExtent
495 // SelectRange
496 // > SelectRange
497 // > MoveRangeSelectionExtent
499 input_router_->SendInput(scoped_ptr<IPC::Message>(
500 new InputMsg_SelectRange(0, gfx::Point(1, 2), gfx::Point(3, 4))));
501 ExpectIPCMessageWithArg2<InputMsg_SelectRange>(
502 process_->sink().GetMessageAt(0),
503 gfx::Point(1, 2),
504 gfx::Point(3, 4));
505 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
507 input_router_->SendInput(scoped_ptr<IPC::Message>(
508 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(5, 6))));
509 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
511 input_router_->SendInput(scoped_ptr<IPC::Message>(
512 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(7, 8))));
513 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
515 input_router_->SendInput(scoped_ptr<IPC::Message>(
516 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(9, 10))));
517 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
519 input_router_->SendInput(scoped_ptr<IPC::Message>(
520 new InputMsg_SelectRange(0, gfx::Point(11, 12), gfx::Point(13, 14))));
521 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
523 input_router_->SendInput(scoped_ptr<IPC::Message>(
524 new InputMsg_SelectRange(0, gfx::Point(15, 16), gfx::Point(17, 18))));
525 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
527 input_router_->SendInput(scoped_ptr<IPC::Message>(
528 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(19, 20))));
529 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
531 // Ack the first message.
533 scoped_ptr<IPC::Message> response(
534 new InputHostMsg_SelectRange_ACK(0));
535 input_router_->OnMessageReceived(*response);
538 // Verify that the three MoveRangeSelectionExtent messages are coalesced into
539 // one message.
540 ExpectIPCMessageWithArg1<InputMsg_MoveRangeSelectionExtent>(
541 process_->sink().GetMessageAt(0),
542 gfx::Point(9, 10));
543 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
545 // Ack the second message.
547 scoped_ptr<IPC::Message> response(
548 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
549 input_router_->OnMessageReceived(*response);
552 // Verify that the two SelectRange messages are coalesced into one message.
553 ExpectIPCMessageWithArg2<InputMsg_SelectRange>(
554 process_->sink().GetMessageAt(0),
555 gfx::Point(15, 16),
556 gfx::Point(17, 18));
557 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
559 // Ack the third message.
561 scoped_ptr<IPC::Message> response(
562 new InputHostMsg_SelectRange_ACK(0));
563 input_router_->OnMessageReceived(*response);
566 // Verify the fourth message.
567 ExpectIPCMessageWithArg1<InputMsg_MoveRangeSelectionExtent>(
568 process_->sink().GetMessageAt(0),
569 gfx::Point(19, 20));
570 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
572 // Ack the fourth message.
574 scoped_ptr<IPC::Message> response(
575 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
576 input_router_->OnMessageReceived(*response);
578 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
581 TEST_F(InputRouterImplTest, CoalescesCaretMove) {
582 input_router_->SendInput(
583 scoped_ptr<IPC::Message>(new InputMsg_MoveCaret(0, gfx::Point(1, 2))));
584 ExpectIPCMessageWithArg1<InputMsg_MoveCaret>(
585 process_->sink().GetMessageAt(0), gfx::Point(1, 2));
586 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
588 // Send two more messages without acking.
589 input_router_->SendInput(
590 scoped_ptr<IPC::Message>(new InputMsg_MoveCaret(0, gfx::Point(5, 6))));
591 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
593 input_router_->SendInput(
594 scoped_ptr<IPC::Message>(new InputMsg_MoveCaret(0, gfx::Point(9, 10))));
595 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
597 // Now ack the first message.
599 scoped_ptr<IPC::Message> response(new InputHostMsg_MoveCaret_ACK(0));
600 input_router_->OnMessageReceived(*response);
603 // Verify that the two messages are coalesced into one message.
604 ExpectIPCMessageWithArg1<InputMsg_MoveCaret>(
605 process_->sink().GetMessageAt(0), gfx::Point(9, 10));
606 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
608 // Acking the coalesced msg should not send any more msg.
610 scoped_ptr<IPC::Message> response(new InputHostMsg_MoveCaret_ACK(0));
611 input_router_->OnMessageReceived(*response);
613 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
616 TEST_F(InputRouterImplTest, HandledInputEvent) {
617 client_->set_filter_state(INPUT_EVENT_ACK_STATE_CONSUMED);
619 // Simulate a keyboard event.
620 SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
622 // Make sure no input event is sent to the renderer.
623 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
625 // OnKeyboardEventAck should be triggered without actual ack.
626 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
628 // As the event was acked already, keyboard event queue should be
629 // empty.
630 ASSERT_EQ(NULL, input_router_->GetLastKeyboardEvent());
633 TEST_F(InputRouterImplTest, ClientCanceledKeyboardEvent) {
634 client_->set_filter_state(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
636 // Simulate a keyboard event that has no consumer.
637 SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
639 // Make sure no input event is sent to the renderer.
640 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
641 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
644 // Simulate a keyboard event that should be dropped.
645 client_->set_filter_state(INPUT_EVENT_ACK_STATE_UNKNOWN);
646 SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
648 // Make sure no input event is sent to the renderer, and no ack is sent.
649 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
650 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
653 TEST_F(InputRouterImplTest, ShortcutKeyboardEvent) {
654 SimulateKeyboardEvent(WebInputEvent::RawKeyDown, true);
655 EXPECT_TRUE(GetIsShortcutFromHandleInputEventMessage(
656 process_->sink().GetMessageAt(0)));
658 process_->sink().ClearMessages();
660 SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
661 EXPECT_FALSE(GetIsShortcutFromHandleInputEventMessage(
662 process_->sink().GetMessageAt(0)));
665 TEST_F(InputRouterImplTest, NoncorrespondingKeyEvents) {
666 SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
668 SendInputEventACK(WebInputEvent::KeyUp,
669 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
670 EXPECT_TRUE(ack_handler_->unexpected_event_ack_called());
673 // Tests ported from RenderWidgetHostTest --------------------------------------
675 TEST_F(InputRouterImplTest, HandleKeyEventsWeSent) {
676 // Simulate a keyboard event.
677 SimulateKeyboardEvent(WebInputEvent::RawKeyDown, false);
678 ASSERT_TRUE(input_router_->GetLastKeyboardEvent());
679 EXPECT_EQ(WebInputEvent::RawKeyDown,
680 input_router_->GetLastKeyboardEvent()->type);
682 // Make sure we sent the input event to the renderer.
683 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
684 InputMsg_HandleInputEvent::ID));
685 process_->sink().ClearMessages();
687 // Send the simulated response from the renderer back.
688 SendInputEventACK(WebInputEvent::RawKeyDown,
689 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
690 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
691 EXPECT_EQ(WebInputEvent::RawKeyDown,
692 ack_handler_->acked_keyboard_event().type);
695 TEST_F(InputRouterImplTest, IgnoreKeyEventsWeDidntSend) {
696 // Send a simulated, unrequested key response. We should ignore this.
697 SendInputEventACK(WebInputEvent::RawKeyDown,
698 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
700 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
703 TEST_F(InputRouterImplTest, CoalescesWheelEvents) {
704 // Simulate wheel events.
705 SimulateWheelEvent(0, -5, 0, false); // sent directly
706 SimulateWheelEvent(0, -10, 0, false); // enqueued
707 SimulateWheelEvent(8, -6, 0, false); // coalesced into previous event
708 SimulateWheelEvent(9, -7, 1, false); // enqueued, different modifiers
709 SimulateWheelEvent(0, -10, 0, false); // enqueued, different modifiers
710 // Explicitly verify that PhaseEnd isn't coalesced to avoid bugs like
711 // https://crbug.com/154740.
712 SimulateWheelEventWithPhase(WebMouseWheelEvent::PhaseEnded); // enqueued
714 // Check that only the first event was sent.
715 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
716 InputMsg_HandleInputEvent::ID));
717 const WebInputEvent* input_event =
718 GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
719 ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
720 const WebMouseWheelEvent* wheel_event =
721 static_cast<const WebMouseWheelEvent*>(input_event);
722 EXPECT_EQ(0, wheel_event->deltaX);
723 EXPECT_EQ(-5, wheel_event->deltaY);
724 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
726 // Check that the ACK sends the second message immediately.
727 SendInputEventACK(WebInputEvent::MouseWheel,
728 INPUT_EVENT_ACK_STATE_CONSUMED);
729 // The coalesced events can queue up a delayed ack
730 // so that additional input events can be processed before
731 // we turn off coalescing.
732 base::MessageLoop::current()->RunUntilIdle();
733 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
734 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
735 InputMsg_HandleInputEvent::ID));
736 input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
737 ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
738 wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
739 EXPECT_EQ(8, wheel_event->deltaX);
740 EXPECT_EQ(-10 + -6, wheel_event->deltaY); // coalesced
741 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
743 // Ack the second event (which had the third coalesced into it).
744 SendInputEventACK(WebInputEvent::MouseWheel,
745 INPUT_EVENT_ACK_STATE_CONSUMED);
746 base::MessageLoop::current()->RunUntilIdle();
747 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
748 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
749 InputMsg_HandleInputEvent::ID));
750 input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
751 ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
752 wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
753 EXPECT_EQ(9, wheel_event->deltaX);
754 EXPECT_EQ(-7, wheel_event->deltaY);
755 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
757 // Ack the fourth event.
758 SendInputEventACK(WebInputEvent::MouseWheel,
759 INPUT_EVENT_ACK_STATE_CONSUMED);
760 base::MessageLoop::current()->RunUntilIdle();
761 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
762 EXPECT_TRUE(
763 process_->sink().GetUniqueMessageMatching(InputMsg_HandleInputEvent::ID));
764 input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
765 ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
766 wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
767 EXPECT_EQ(0, wheel_event->deltaX);
768 EXPECT_EQ(-10, wheel_event->deltaY);
769 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
771 // Ack the fifth event.
772 SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
773 base::MessageLoop::current()->RunUntilIdle();
774 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
775 EXPECT_TRUE(
776 process_->sink().GetUniqueMessageMatching(InputMsg_HandleInputEvent::ID));
777 input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
778 ASSERT_EQ(WebInputEvent::MouseWheel, input_event->type);
779 wheel_event = static_cast<const WebMouseWheelEvent*>(input_event);
780 EXPECT_EQ(0, wheel_event->deltaX);
781 EXPECT_EQ(0, wheel_event->deltaY);
782 EXPECT_EQ(WebMouseWheelEvent::PhaseEnded, wheel_event->phase);
783 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
785 // After the final ack, the queue should be empty.
786 SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
787 base::MessageLoop::current()->RunUntilIdle();
788 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
789 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
792 // Tests that touch-events are queued properly.
793 TEST_F(InputRouterImplTest, TouchEventQueue) {
794 OnHasTouchEventHandlers(true);
796 PressTouchPoint(1, 1);
797 uint32 touch_press_event_id = SendTouchEvent();
798 EXPECT_TRUE(client_->GetAndResetFilterEventCalled());
799 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
800 EXPECT_FALSE(TouchEventQueueEmpty());
802 // The second touch should not be sent since one is already in queue.
803 MoveTouchPoint(0, 5, 5);
804 uint32 touch_move_event_id = SendTouchEvent();
805 EXPECT_FALSE(client_->GetAndResetFilterEventCalled());
806 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
807 EXPECT_FALSE(TouchEventQueueEmpty());
809 // Receive an ACK for the first touch-event.
810 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
811 touch_press_event_id);
812 EXPECT_FALSE(TouchEventQueueEmpty());
813 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
814 EXPECT_EQ(WebInputEvent::TouchStart,
815 ack_handler_->acked_touch_event().event.type);
816 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
818 SendTouchEventACK(WebInputEvent::TouchMove, INPUT_EVENT_ACK_STATE_CONSUMED,
819 touch_move_event_id);
820 EXPECT_TRUE(TouchEventQueueEmpty());
821 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
822 EXPECT_EQ(WebInputEvent::TouchMove,
823 ack_handler_->acked_touch_event().event.type);
824 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
827 // Tests that the touch-queue is emptied after a page stops listening for touch
828 // events and the outstanding ack is received.
829 TEST_F(InputRouterImplTest, TouchEventQueueFlush) {
830 OnHasTouchEventHandlers(true);
831 EXPECT_TRUE(client_->has_touch_handler());
832 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
833 EXPECT_TRUE(TouchEventQueueEmpty());
835 // Send a touch-press event.
836 PressTouchPoint(1, 1);
837 uint32 touch_press_event_id = SendTouchEvent();
838 MoveTouchPoint(0, 2, 2);
839 MoveTouchPoint(0, 3, 3);
840 EXPECT_FALSE(TouchEventQueueEmpty());
841 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
843 // The page stops listening for touch-events. Note that flushing is deferred
844 // until the outstanding ack is received.
845 OnHasTouchEventHandlers(false);
846 EXPECT_FALSE(client_->has_touch_handler());
847 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
848 EXPECT_FALSE(TouchEventQueueEmpty());
850 // After the ack, the touch-event queue should be empty, and none of the
851 // flushed touch-events should have been sent to the renderer.
852 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
853 touch_press_event_id);
854 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
855 EXPECT_TRUE(TouchEventQueueEmpty());
858 #if defined(USE_AURA)
859 // Tests that the acked events have correct state. (ui::Events are used only on
860 // windows and aura)
861 TEST_F(InputRouterImplTest, AckedTouchEventState) {
862 input_router_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
863 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
864 EXPECT_TRUE(TouchEventQueueEmpty());
866 // Send a bunch of events, and make sure the ACKed events are correct.
867 ScopedVector<ui::TouchEvent> expected_events;
869 // Use a custom timestamp for all the events to test that the acked events
870 // have the same timestamp;
871 base::TimeDelta timestamp = base::Time::NowFromSystemTime() - base::Time();
872 timestamp -= base::TimeDelta::FromSeconds(600);
874 // Press the first finger.
875 PressTouchPoint(1, 1);
876 SetTouchTimestamp(timestamp);
877 uint32 touch_press_event_id1 = SendTouchEvent();
878 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
879 expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
880 gfx::Point(1, 1), 0, timestamp));
882 // Move the finger.
883 timestamp += base::TimeDelta::FromSeconds(10);
884 MoveTouchPoint(0, 500, 500);
885 SetTouchTimestamp(timestamp);
886 uint32 touch_move_event_id1 = SendTouchEvent();
887 EXPECT_FALSE(TouchEventQueueEmpty());
888 expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_MOVED,
889 gfx::Point(500, 500), 0, timestamp));
891 // Now press a second finger.
892 timestamp += base::TimeDelta::FromSeconds(10);
893 PressTouchPoint(2, 2);
894 SetTouchTimestamp(timestamp);
895 uint32 touch_press_event_id2 = SendTouchEvent();
896 EXPECT_FALSE(TouchEventQueueEmpty());
897 expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_PRESSED,
898 gfx::Point(2, 2), 1, timestamp));
900 // Move both fingers.
901 timestamp += base::TimeDelta::FromSeconds(10);
902 MoveTouchPoint(0, 10, 10);
903 MoveTouchPoint(1, 20, 20);
904 SetTouchTimestamp(timestamp);
905 uint32 touch_move_event_id2 = SendTouchEvent();
906 EXPECT_FALSE(TouchEventQueueEmpty());
907 expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_MOVED,
908 gfx::Point(10, 10), 0, timestamp));
909 expected_events.push_back(new ui::TouchEvent(ui::ET_TOUCH_MOVED,
910 gfx::Point(20, 20), 1, timestamp));
912 // Receive the ACKs and make sure the generated events from the acked events
913 // are correct.
914 WebInputEvent::Type acks[] = { WebInputEvent::TouchStart,
915 WebInputEvent::TouchMove,
916 WebInputEvent::TouchStart,
917 WebInputEvent::TouchMove };
919 uint32 touch_event_ids[] = {touch_press_event_id1,
920 touch_move_event_id1,
921 touch_press_event_id2,
922 touch_move_event_id2};
924 TouchEventCoordinateSystem coordinate_system = LOCAL_COORDINATES;
925 #if !defined(OS_WIN)
926 coordinate_system = SCREEN_COORDINATES;
927 #endif
928 for (size_t i = 0; i < arraysize(acks); ++i) {
929 SendTouchEventACK(acks[i], INPUT_EVENT_ACK_STATE_NOT_CONSUMED,
930 touch_event_ids[i]);
931 EXPECT_EQ(acks[i], ack_handler_->acked_touch_event().event.type);
932 ScopedVector<ui::TouchEvent> acked;
934 MakeUITouchEventsFromWebTouchEvents(
935 ack_handler_->acked_touch_event(), &acked, coordinate_system);
936 bool success = EventListIsSubset(acked, expected_events);
937 EXPECT_TRUE(success) << "Failed on step: " << i;
938 if (!success)
939 break;
940 expected_events.erase(expected_events.begin(),
941 expected_events.begin() + acked.size());
944 EXPECT_TRUE(TouchEventQueueEmpty());
945 EXPECT_EQ(0U, expected_events.size());
947 #endif // defined(USE_AURA)
949 TEST_F(InputRouterImplTest, UnhandledWheelEvent) {
950 // Simulate wheel events.
951 SimulateWheelEvent(0, -5, 0, false); // sent directly
952 SimulateWheelEvent(0, -10, 0, false); // enqueued
954 // Check that only the first event was sent.
955 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
956 InputMsg_HandleInputEvent::ID));
957 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
959 // Indicate that the wheel event was unhandled.
960 SendInputEventACK(WebInputEvent::MouseWheel,
961 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
963 // Check that the correct unhandled wheel event was received.
964 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
965 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, ack_handler_->ack_state());
966 EXPECT_EQ(ack_handler_->acked_wheel_event().deltaY, -5);
968 // Check that the second event was sent.
969 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
970 InputMsg_HandleInputEvent::ID));
971 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
973 // Check that the correct unhandled wheel event was received.
974 EXPECT_EQ(ack_handler_->acked_wheel_event().deltaY, -5);
977 TEST_F(InputRouterImplTest, TouchTypesIgnoringAck) {
978 OnHasTouchEventHandlers(true);
979 // Only acks for TouchCancel should always be ignored.
980 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
981 GetEventWithType(WebInputEvent::TouchStart)));
982 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
983 GetEventWithType(WebInputEvent::TouchMove)));
984 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
985 GetEventWithType(WebInputEvent::TouchEnd)));
987 // Precede the TouchCancel with an appropriate TouchStart;
988 PressTouchPoint(1, 1);
989 uint32 touch_press_event_id = SendTouchEvent();
990 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
991 touch_press_event_id);
992 ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
993 ASSERT_EQ(1U, ack_handler_->GetAndResetAckCount());
994 ASSERT_EQ(0, client_->in_flight_event_count());
996 // The TouchCancel ack is always ignored.
997 CancelTouchPoint(0);
998 uint32 touch_cancel_event_id = SendTouchEvent();
999 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1000 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1001 EXPECT_EQ(0, client_->in_flight_event_count());
1002 EXPECT_FALSE(HasPendingEvents());
1003 SendTouchEventACK(WebInputEvent::TouchCancel,
1004 INPUT_EVENT_ACK_STATE_NOT_CONSUMED, touch_cancel_event_id);
1005 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1006 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1007 EXPECT_FALSE(HasPendingEvents());
1010 TEST_F(InputRouterImplTest, GestureTypesIgnoringAck) {
1011 // We test every gesture type, ensuring that the stream of gestures is valid.
1012 const WebInputEvent::Type eventTypes[] = {
1013 WebInputEvent::GestureTapDown,
1014 WebInputEvent::GestureShowPress,
1015 WebInputEvent::GestureTapCancel,
1016 WebInputEvent::GestureScrollBegin,
1017 WebInputEvent::GestureFlingStart,
1018 WebInputEvent::GestureFlingCancel,
1019 WebInputEvent::GestureTapDown,
1020 WebInputEvent::GestureTap,
1021 WebInputEvent::GestureTapDown,
1022 WebInputEvent::GestureLongPress,
1023 WebInputEvent::GestureTapCancel,
1024 WebInputEvent::GestureLongTap,
1025 WebInputEvent::GestureTapDown,
1026 WebInputEvent::GestureTapUnconfirmed,
1027 WebInputEvent::GestureTapCancel,
1028 WebInputEvent::GestureTapDown,
1029 WebInputEvent::GestureDoubleTap,
1030 WebInputEvent::GestureTapDown,
1031 WebInputEvent::GestureTapCancel,
1032 WebInputEvent::GestureTwoFingerTap,
1033 WebInputEvent::GestureTapDown,
1034 WebInputEvent::GestureTapCancel,
1035 WebInputEvent::GestureScrollBegin,
1036 WebInputEvent::GestureScrollUpdate,
1037 WebInputEvent::GesturePinchBegin,
1038 WebInputEvent::GesturePinchUpdate,
1039 WebInputEvent::GesturePinchEnd,
1040 WebInputEvent::GestureScrollEnd};
1041 for (size_t i = 0; i < arraysize(eventTypes); ++i) {
1042 WebInputEvent::Type type = eventTypes[i];
1043 if (WebInputEventTraits::WillReceiveAckFromRenderer(
1044 GetEventWithType(type))) {
1045 SimulateGestureEvent(type, blink::WebGestureDeviceTouchscreen);
1046 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1047 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1048 EXPECT_EQ(1, client_->in_flight_event_count());
1049 EXPECT_TRUE(HasPendingEvents());
1051 SendInputEventACK(type, INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1052 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1053 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1054 EXPECT_EQ(0, client_->in_flight_event_count());
1055 EXPECT_FALSE(HasPendingEvents());
1056 continue;
1059 SimulateGestureEvent(type, blink::WebGestureDeviceTouchscreen);
1060 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1061 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1062 EXPECT_EQ(0, client_->in_flight_event_count());
1063 EXPECT_FALSE(HasPendingEvents());
1067 TEST_F(InputRouterImplTest, MouseTypesIgnoringAck) {
1068 int start_type = static_cast<int>(WebInputEvent::MouseDown);
1069 int end_type = static_cast<int>(WebInputEvent::ContextMenu);
1070 ASSERT_LT(start_type, end_type);
1071 for (int i = start_type; i <= end_type; ++i) {
1072 WebInputEvent::Type type = static_cast<WebInputEvent::Type>(i);
1073 int expected_in_flight_event_count =
1074 !WebInputEventTraits::WillReceiveAckFromRenderer(GetEventWithType(type))
1076 : 1;
1078 // Note: Only MouseMove ack is forwarded to the ack handler.
1079 SimulateMouseEvent(type, 0, 0);
1080 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1081 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1082 EXPECT_EQ(expected_in_flight_event_count, client_->in_flight_event_count());
1083 if (expected_in_flight_event_count) {
1084 SendInputEventACK(type, INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1085 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1086 uint32 expected_ack_count = type == WebInputEvent::MouseMove ? 1 : 0;
1087 EXPECT_EQ(expected_ack_count, ack_handler_->GetAndResetAckCount());
1088 EXPECT_EQ(0, client_->in_flight_event_count());
1093 // Guard against breaking changes to the list of ignored event ack types in
1094 // |WebInputEventTraits::WillReceiveAckFromRenderer|.
1095 TEST_F(InputRouterImplTest, RequiredEventAckTypes) {
1096 const WebInputEvent::Type kRequiredEventAckTypes[] = {
1097 WebInputEvent::MouseMove,
1098 WebInputEvent::MouseWheel,
1099 WebInputEvent::RawKeyDown,
1100 WebInputEvent::KeyDown,
1101 WebInputEvent::KeyUp,
1102 WebInputEvent::Char,
1103 WebInputEvent::GestureScrollUpdate,
1104 WebInputEvent::GestureFlingStart,
1105 WebInputEvent::GestureFlingCancel,
1106 WebInputEvent::GesturePinchUpdate,
1107 WebInputEvent::TouchStart,
1108 WebInputEvent::TouchMove
1110 for (size_t i = 0; i < arraysize(kRequiredEventAckTypes); ++i) {
1111 const WebInputEvent::Type required_ack_type = kRequiredEventAckTypes[i];
1112 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
1113 GetEventWithType(required_ack_type)));
1117 // Test that GestureShowPress, GestureTapDown and GestureTapCancel events don't
1118 // wait for ACKs.
1119 TEST_F(InputRouterImplTest, GestureTypesIgnoringAckInterleaved) {
1120 // Interleave a few events that do and do not ignore acks, ensuring that
1121 // ack-ignoring events aren't dispatched until all prior events which observe
1122 // their ack disposition have been dispatched.
1124 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1125 blink::WebGestureDeviceTouchscreen);
1126 ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
1127 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1128 EXPECT_EQ(0, client_->in_flight_event_count());
1130 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
1131 blink::WebGestureDeviceTouchscreen);
1132 ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
1133 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1134 EXPECT_EQ(1, client_->in_flight_event_count());
1136 SimulateGestureEvent(WebInputEvent::GestureTapDown,
1137 blink::WebGestureDeviceTouchscreen);
1138 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1139 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1140 EXPECT_EQ(1, client_->in_flight_event_count());
1142 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
1143 blink::WebGestureDeviceTouchscreen);
1144 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1145 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1147 SimulateGestureEvent(WebInputEvent::GestureShowPress,
1148 blink::WebGestureDeviceTouchscreen);
1149 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1150 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1152 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
1153 blink::WebGestureDeviceTouchscreen);
1154 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1155 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1157 SimulateGestureEvent(WebInputEvent::GestureTapCancel,
1158 blink::WebGestureDeviceTouchscreen);
1159 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1160 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1162 // Now ack each ack-respecting event. Ack-ignoring events should not be
1163 // dispatched until all prior events which observe ack disposition have been
1164 // fired, at which point they should be sent immediately. They should also
1165 // have no effect on the in-flight event count.
1166 SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1167 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1168 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1169 EXPECT_EQ(2U, ack_handler_->GetAndResetAckCount());
1170 EXPECT_EQ(1, client_->in_flight_event_count());
1172 SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1173 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1174 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1175 EXPECT_EQ(2U, ack_handler_->GetAndResetAckCount());
1176 EXPECT_EQ(1, client_->in_flight_event_count());
1178 SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1179 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1180 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1181 EXPECT_EQ(2U, ack_handler_->GetAndResetAckCount());
1182 EXPECT_EQ(0, client_->in_flight_event_count());
1185 // Test that GestureShowPress events don't get out of order due to
1186 // ignoring their acks.
1187 TEST_F(InputRouterImplTest, GestureShowPressIsInOrder) {
1188 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1189 blink::WebGestureDeviceTouchscreen);
1190 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1191 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1194 // GesturePinchBegin ignores its ack.
1195 SimulateGestureEvent(WebInputEvent::GesturePinchBegin,
1196 blink::WebGestureDeviceTouchscreen);
1197 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1198 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1200 // GesturePinchUpdate waits for an ack.
1201 // This also verifies that GesturePinchUpdates for touchscreen are sent
1202 // to the renderer (in contrast to the TrackpadPinchUpdate test).
1203 SimulateGestureEvent(WebInputEvent::GesturePinchUpdate,
1204 blink::WebGestureDeviceTouchscreen);
1205 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1206 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1208 SimulateGestureEvent(WebInputEvent::GestureShowPress,
1209 blink::WebGestureDeviceTouchscreen);
1210 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1211 // The ShowPress, though it ignores ack, is still stuck in the queue
1212 // behind the PinchUpdate which requires an ack.
1213 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1215 SimulateGestureEvent(WebInputEvent::GestureShowPress,
1216 blink::WebGestureDeviceTouchscreen);
1217 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1218 // ShowPress has entered the queue.
1219 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1221 SendInputEventACK(WebInputEvent::GesturePinchUpdate,
1222 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1223 // Now that the Tap has been ACKed, the ShowPress events should receive
1224 // synthetic acks, and fire immediately.
1225 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1226 EXPECT_EQ(3U, ack_handler_->GetAndResetAckCount());
1229 // Test that touch ack timeout behavior is properly configured for
1230 // mobile-optimized sites and allowed touch actions.
1231 TEST_F(InputRouterImplTest, TouchAckTimeoutConfigured) {
1232 const int kDesktopTimeoutMs = 1;
1233 const int kMobileTimeoutMs = 0;
1234 SetUpForTouchAckTimeoutTest(kDesktopTimeoutMs, kMobileTimeoutMs);
1235 ASSERT_TRUE(TouchEventTimeoutEnabled());
1237 // Verify that the touch ack timeout fires upon the delayed ack.
1238 PressTouchPoint(1, 1);
1239 uint32 touch_press_event_id1 = SendTouchEvent();
1240 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1241 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1242 RunTasksAndWait(base::TimeDelta::FromMilliseconds(kDesktopTimeoutMs + 1));
1244 // The timed-out event should have been ack'ed.
1245 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1246 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1248 // Ack'ing the timed-out event should fire a TouchCancel.
1249 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
1250 touch_press_event_id1);
1251 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1252 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1254 // The remainder of the touch sequence should be dropped.
1255 ReleaseTouchPoint(0);
1256 SendTouchEvent();
1257 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1258 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1259 ASSERT_TRUE(TouchEventTimeoutEnabled());
1261 // A mobile-optimized site should use the mobile timeout. For this test that
1262 // timeout value is 0, which disables the timeout.
1263 input_router()->NotifySiteIsMobileOptimized(true);
1264 EXPECT_FALSE(TouchEventTimeoutEnabled());
1266 input_router()->NotifySiteIsMobileOptimized(false);
1267 EXPECT_TRUE(TouchEventTimeoutEnabled());
1269 // TOUCH_ACTION_NONE (and no other touch-action) should disable the timeout.
1270 OnHasTouchEventHandlers(true);
1271 PressTouchPoint(1, 1);
1272 uint32 touch_press_event_id2 = SendTouchEvent();
1273 OnSetTouchAction(TOUCH_ACTION_PAN_Y);
1274 EXPECT_TRUE(TouchEventTimeoutEnabled());
1275 ReleaseTouchPoint(0);
1276 uint32 touch_release_event_id2 = SendTouchEvent();
1277 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
1278 touch_press_event_id2);
1279 SendTouchEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED,
1280 touch_release_event_id2);
1282 PressTouchPoint(1, 1);
1283 uint32 touch_press_event_id3 = SendTouchEvent();
1284 OnSetTouchAction(TOUCH_ACTION_NONE);
1285 EXPECT_FALSE(TouchEventTimeoutEnabled());
1286 ReleaseTouchPoint(0);
1287 uint32 touch_release_event_id3 = SendTouchEvent();
1288 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
1289 touch_press_event_id3);
1290 SendTouchEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED,
1291 touch_release_event_id3);
1293 // As the touch-action is reset by a new touch sequence, the timeout behavior
1294 // should be restored.
1295 PressTouchPoint(1, 1);
1296 SendTouchEvent();
1297 EXPECT_TRUE(TouchEventTimeoutEnabled());
1300 // Test that a touch sequenced preceded by TOUCH_ACTION_NONE is not affected by
1301 // the touch timeout.
1302 TEST_F(InputRouterImplTest,
1303 TouchAckTimeoutDisabledForTouchSequenceAfterTouchActionNone) {
1304 const int kDesktopTimeoutMs = 1;
1305 const int kMobileTimeoutMs = 2;
1306 SetUpForTouchAckTimeoutTest(kDesktopTimeoutMs, kMobileTimeoutMs);
1307 ASSERT_TRUE(TouchEventTimeoutEnabled());
1308 OnHasTouchEventHandlers(true);
1310 // Start a touch sequence.
1311 PressTouchPoint(1, 1);
1312 uint32 touch_press_event_id = SendTouchEvent();
1313 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1315 // TOUCH_ACTION_NONE should disable the timeout.
1316 OnSetTouchAction(TOUCH_ACTION_NONE);
1317 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
1318 touch_press_event_id);
1319 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1320 EXPECT_FALSE(TouchEventTimeoutEnabled());
1322 MoveTouchPoint(0, 1, 2);
1323 uint32 touch_move_event_id = SendTouchEvent();
1324 EXPECT_FALSE(TouchEventTimeoutEnabled());
1325 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1327 // Delay the move ack. The timeout should not fire.
1328 RunTasksAndWait(base::TimeDelta::FromMilliseconds(kDesktopTimeoutMs + 1));
1329 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1330 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1331 SendTouchEventACK(WebInputEvent::TouchMove, INPUT_EVENT_ACK_STATE_CONSUMED,
1332 touch_move_event_id);
1333 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1335 // End the touch sequence.
1336 ReleaseTouchPoint(0);
1337 uint32 touch_release_event_id = SendTouchEvent();
1338 SendTouchEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED,
1339 touch_release_event_id);
1340 EXPECT_FALSE(TouchEventTimeoutEnabled());
1341 ack_handler_->GetAndResetAckCount();
1342 GetSentMessageCountAndResetSink();
1344 // Start another touch sequence. This should restore the touch timeout.
1345 PressTouchPoint(1, 1);
1346 SendTouchEvent();
1347 EXPECT_TRUE(TouchEventTimeoutEnabled());
1348 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1349 EXPECT_EQ(0U, ack_handler_->GetAndResetAckCount());
1351 // Wait for the touch ack timeout to fire.
1352 RunTasksAndWait(base::TimeDelta::FromMilliseconds(kDesktopTimeoutMs + 1));
1353 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1356 // Test that TouchActionFilter::ResetTouchAction is called before the
1357 // first touch event for a touch sequence reaches the renderer.
1358 TEST_F(InputRouterImplTest, TouchActionResetBeforeEventReachesRenderer) {
1359 OnHasTouchEventHandlers(true);
1361 // Sequence 1.
1362 PressTouchPoint(1, 1);
1363 uint32 touch_press_event_id1 = SendTouchEvent();
1364 OnSetTouchAction(TOUCH_ACTION_NONE);
1365 MoveTouchPoint(0, 50, 50);
1366 uint32 touch_move_event_id1 = SendTouchEvent();
1367 ReleaseTouchPoint(0);
1368 uint32 touch_release_event_id1 = SendTouchEvent();
1370 // Sequence 2.
1371 PressTouchPoint(1, 1);
1372 uint32 touch_press_event_id2 = SendTouchEvent();
1373 MoveTouchPoint(0, 50, 50);
1374 uint32 touch_move_event_id2 = SendTouchEvent();
1375 ReleaseTouchPoint(0);
1376 uint32 touch_release_event_id2 = SendTouchEvent();
1378 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
1379 touch_press_event_id1);
1380 SendTouchEventACK(WebInputEvent::TouchMove, INPUT_EVENT_ACK_STATE_CONSUMED,
1381 touch_move_event_id1);
1383 // Ensure touch action is still none, as the next touch start hasn't been
1384 // acked yet. ScrollBegin and ScrollEnd don't require acks.
1385 EXPECT_EQ(3U, GetSentMessageCountAndResetSink());
1386 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1387 blink::WebGestureDeviceTouchscreen);
1388 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1389 SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1390 blink::WebGestureDeviceTouchscreen);
1391 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1393 // This allows the next touch sequence to start.
1394 SendTouchEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED,
1395 touch_release_event_id1);
1397 // Ensure touch action has been set to auto, as a new touch sequence has
1398 // started.
1399 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
1400 touch_press_event_id2);
1401 SendTouchEventACK(WebInputEvent::TouchMove, INPUT_EVENT_ACK_STATE_CONSUMED,
1402 touch_move_event_id2);
1403 EXPECT_EQ(3U, GetSentMessageCountAndResetSink());
1404 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1405 blink::WebGestureDeviceTouchscreen);
1406 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1407 SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1408 blink::WebGestureDeviceTouchscreen);
1409 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1410 SendTouchEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED,
1411 touch_release_event_id2);
1414 // Test that TouchActionFilter::ResetTouchAction is called when a new touch
1415 // sequence has no consumer.
1416 TEST_F(InputRouterImplTest, TouchActionResetWhenTouchHasNoConsumer) {
1417 OnHasTouchEventHandlers(true);
1419 // Sequence 1.
1420 PressTouchPoint(1, 1);
1421 uint32 touch_press_event_id1 = SendTouchEvent();
1422 MoveTouchPoint(0, 50, 50);
1423 uint32 touch_move_event_id1 = SendTouchEvent();
1424 OnSetTouchAction(TOUCH_ACTION_NONE);
1425 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
1426 touch_press_event_id1);
1427 SendTouchEventACK(WebInputEvent::TouchMove, INPUT_EVENT_ACK_STATE_CONSUMED,
1428 touch_move_event_id1);
1430 ReleaseTouchPoint(0);
1431 uint32 touch_release_event_id1 = SendTouchEvent();
1433 // Sequence 2
1434 PressTouchPoint(1, 1);
1435 uint32 touch_press_event_id2 = SendTouchEvent();
1436 MoveTouchPoint(0, 50, 50);
1437 SendTouchEvent();
1438 ReleaseTouchPoint(0);
1439 SendTouchEvent();
1441 // Ensure we have touch-action:none. ScrollBegin and ScrollEnd don't require
1442 // acks.
1443 EXPECT_EQ(3U, GetSentMessageCountAndResetSink());
1444 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1445 blink::WebGestureDeviceTouchscreen);
1446 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1447 SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1448 blink::WebGestureDeviceTouchscreen);
1449 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1451 SendTouchEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED,
1452 touch_release_event_id1);
1453 SendTouchEventACK(WebInputEvent::TouchStart,
1454 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS,
1455 touch_press_event_id2);
1457 // Ensure touch action has been set to auto, as the touch had no consumer.
1458 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1459 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1460 blink::WebGestureDeviceTouchscreen);
1461 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1462 SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1463 blink::WebGestureDeviceTouchscreen);
1464 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1467 // Test that TouchActionFilter::ResetTouchAction is called when the touch
1468 // handler is removed.
1469 TEST_F(InputRouterImplTest, TouchActionResetWhenTouchHandlerRemoved) {
1470 // Touch sequence with touch handler.
1471 OnHasTouchEventHandlers(true);
1472 PressTouchPoint(1, 1);
1473 uint32 touch_press_event_id = SendTouchEvent();
1474 MoveTouchPoint(0, 50, 50);
1475 uint32 touch_move_event_id = SendTouchEvent();
1476 OnSetTouchAction(TOUCH_ACTION_NONE);
1477 ReleaseTouchPoint(0);
1478 uint32 touch_release_event_id = SendTouchEvent();
1479 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1481 // Ensure we have touch-action:none, suppressing scroll events.
1482 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
1483 touch_press_event_id);
1484 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1485 SendTouchEventACK(WebInputEvent::TouchMove,
1486 INPUT_EVENT_ACK_STATE_NOT_CONSUMED, touch_move_event_id);
1487 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1488 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1489 blink::WebGestureDeviceTouchscreen);
1490 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1492 SendTouchEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_NOT_CONSUMED,
1493 touch_release_event_id);
1494 SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1495 blink::WebGestureDeviceTouchscreen);
1496 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1498 // Sequence without a touch handler. Note that in this case, the view may not
1499 // necessarily forward touches to the router (as no touch handler exists).
1500 OnHasTouchEventHandlers(false);
1502 // Ensure touch action has been set to auto, as the touch handler has been
1503 // removed.
1504 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1505 blink::WebGestureDeviceTouchscreen);
1506 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1507 SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1508 blink::WebGestureDeviceTouchscreen);
1509 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1512 // Test that the double tap gesture depends on the touch action of the first
1513 // tap.
1514 TEST_F(InputRouterImplTest, DoubleTapGestureDependsOnFirstTap) {
1515 OnHasTouchEventHandlers(true);
1517 // Sequence 1.
1518 PressTouchPoint(1, 1);
1519 uint32 touch_press_event_id1 = SendTouchEvent();
1520 OnSetTouchAction(TOUCH_ACTION_NONE);
1521 SendTouchEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED,
1522 touch_press_event_id1);
1524 ReleaseTouchPoint(0);
1525 uint32 touch_release_event_id = SendTouchEvent();
1527 // Sequence 2
1528 PressTouchPoint(1, 1);
1529 uint32 touch_press_event_id2 = SendTouchEvent();
1531 // First tap.
1532 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1533 SimulateGestureEvent(WebInputEvent::GestureTapDown,
1534 blink::WebGestureDeviceTouchscreen);
1535 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1537 // The GestureTapUnconfirmed is converted into a tap, as the touch action is
1538 // none.
1539 SimulateGestureEvent(WebInputEvent::GestureTapUnconfirmed,
1540 blink::WebGestureDeviceTouchscreen);
1541 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1542 // This test will become invalid if GestureTap stops requiring an ack.
1543 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
1544 GetEventWithType(WebInputEvent::GestureTap)));
1545 EXPECT_EQ(2, client_->in_flight_event_count());
1546 SendInputEventACK(WebInputEvent::GestureTap,
1547 INPUT_EVENT_ACK_STATE_CONSUMED);
1548 EXPECT_EQ(1, client_->in_flight_event_count());
1550 // This tap gesture is dropped, since the GestureTapUnconfirmed was turned
1551 // into a tap.
1552 SimulateGestureEvent(WebInputEvent::GestureTap,
1553 blink::WebGestureDeviceTouchscreen);
1554 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1556 SendTouchEventACK(WebInputEvent::TouchEnd, INPUT_EVENT_ACK_STATE_CONSUMED,
1557 touch_release_event_id);
1558 SendTouchEventACK(WebInputEvent::TouchStart,
1559 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS,
1560 touch_press_event_id2);
1562 // Second Tap.
1563 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1564 SimulateGestureEvent(WebInputEvent::GestureTapDown,
1565 blink::WebGestureDeviceTouchscreen);
1566 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1568 // Although the touch-action is now auto, the double tap still won't be
1569 // dispatched, because the first tap occured when the touch-action was none.
1570 SimulateGestureEvent(WebInputEvent::GestureDoubleTap,
1571 blink::WebGestureDeviceTouchscreen);
1572 // This test will become invalid if GestureDoubleTap stops requiring an ack.
1573 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
1574 GetEventWithType(WebInputEvent::GestureDoubleTap)));
1575 EXPECT_EQ(1, client_->in_flight_event_count());
1576 SendInputEventACK(WebInputEvent::GestureTap, INPUT_EVENT_ACK_STATE_CONSUMED);
1577 EXPECT_EQ(0, client_->in_flight_event_count());
1580 // Test that the router will call the client's |DidFlush| after all events have
1581 // been dispatched following a call to |Flush|.
1582 TEST_F(InputRouterImplTest, InputFlush) {
1583 EXPECT_FALSE(HasPendingEvents());
1585 // Flushing an empty router should immediately trigger DidFlush.
1586 RequestNotificationWhenFlushed();
1587 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1588 EXPECT_FALSE(HasPendingEvents());
1590 // Queue a TouchStart.
1591 OnHasTouchEventHandlers(true);
1592 PressTouchPoint(1, 1);
1593 uint32 touch_press_event_id = SendTouchEvent();
1594 EXPECT_TRUE(HasPendingEvents());
1596 // DidFlush should be called only after the event is ack'ed.
1597 RequestNotificationWhenFlushed();
1598 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1599 SendTouchEventACK(WebInputEvent::TouchStart,
1600 INPUT_EVENT_ACK_STATE_NOT_CONSUMED, touch_press_event_id);
1601 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1603 // Ensure different types of enqueued events will prevent the DidFlush call
1604 // until all such events have been fully dispatched.
1605 MoveTouchPoint(0, 50, 50);
1606 uint32 touch_move_event_id = SendTouchEvent();
1607 ASSERT_TRUE(HasPendingEvents());
1608 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1609 blink::WebGestureDeviceTouchscreen);
1610 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
1611 blink::WebGestureDeviceTouchscreen);
1612 SimulateGestureEvent(WebInputEvent::GesturePinchBegin,
1613 blink::WebGestureDeviceTouchscreen);
1614 SimulateGestureEvent(WebInputEvent::GesturePinchUpdate,
1615 blink::WebGestureDeviceTouchscreen);
1616 RequestNotificationWhenFlushed();
1617 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1619 // Repeated flush calls should have no effect.
1620 RequestNotificationWhenFlushed();
1621 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1623 // There are still pending gestures.
1624 SendTouchEventACK(WebInputEvent::TouchMove,
1625 INPUT_EVENT_ACK_STATE_NOT_CONSUMED, touch_move_event_id);
1626 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1627 EXPECT_TRUE(HasPendingEvents());
1629 // One more gesture to go.
1630 SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1631 INPUT_EVENT_ACK_STATE_CONSUMED);
1632 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1633 EXPECT_TRUE(HasPendingEvents());
1635 // The final ack'ed gesture should trigger the DidFlush.
1636 SendInputEventACK(WebInputEvent::GesturePinchUpdate,
1637 INPUT_EVENT_ACK_STATE_CONSUMED);
1638 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1639 EXPECT_FALSE(HasPendingEvents());
1642 // Test that the router will call the client's |DidFlush| after all fling
1643 // animations have completed.
1644 TEST_F(InputRouterImplTest, InputFlushAfterFling) {
1645 EXPECT_FALSE(HasPendingEvents());
1647 // Simulate a fling.
1648 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1649 blink::WebGestureDeviceTouchscreen);
1650 SimulateGestureEvent(WebInputEvent::GestureFlingStart,
1651 blink::WebGestureDeviceTouchscreen);
1652 EXPECT_TRUE(HasPendingEvents());
1654 // If the fling is unconsumed, the flush is complete.
1655 RequestNotificationWhenFlushed();
1656 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1657 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1658 blink::WebGestureDeviceTouchscreen);
1659 SendInputEventACK(WebInputEvent::GestureFlingStart,
1660 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1661 EXPECT_FALSE(HasPendingEvents());
1662 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1664 // Simulate a second fling.
1665 SimulateGestureEvent(WebInputEvent::GestureFlingStart,
1666 blink::WebGestureDeviceTouchscreen);
1667 EXPECT_TRUE(HasPendingEvents());
1669 // If the fling is consumed, the flush is complete only when the renderer
1670 // reports that is has ended.
1671 RequestNotificationWhenFlushed();
1672 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1673 SendInputEventACK(WebInputEvent::GestureFlingStart,
1674 INPUT_EVENT_ACK_STATE_CONSUMED);
1675 EXPECT_TRUE(HasPendingEvents());
1676 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1678 // The fling end notification should signal that the router is flushed.
1679 input_router()->OnMessageReceived(InputHostMsg_DidStopFlinging(0));
1680 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1682 // Even flings consumed by the client require a fling-end notification.
1683 client_->set_filter_state(INPUT_EVENT_ACK_STATE_CONSUMED);
1684 SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1685 blink::WebGestureDeviceTouchscreen);
1686 SimulateGestureEvent(WebInputEvent::GestureFlingStart,
1687 blink::WebGestureDeviceTouchscreen);
1688 ASSERT_TRUE(HasPendingEvents());
1689 RequestNotificationWhenFlushed();
1690 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1691 input_router()->OnMessageReceived(InputHostMsg_DidStopFlinging(0));
1692 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1695 // Test that GesturePinchUpdate is handled specially for trackpad
1696 TEST_F(InputRouterImplTest, TouchpadPinchUpdate) {
1697 // GesturePinchUpdate for trackpad sends synthetic wheel events.
1698 // Note that the Touchscreen case is verified as NOT doing this as
1699 // part of the ShowPressIsInOrder test.
1701 SimulateGesturePinchUpdateEvent(
1702 1.5f, 20, 25, 0, blink::WebGestureDeviceTouchpad);
1704 // Verify we actually sent a special wheel event to the renderer.
1705 const WebInputEvent* input_event =
1706 GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
1707 ASSERT_EQ(WebInputEvent::GesturePinchUpdate, input_event->type);
1708 const WebGestureEvent* gesture_event =
1709 static_cast<const WebGestureEvent*>(input_event);
1710 EXPECT_EQ(20, gesture_event->x);
1711 EXPECT_EQ(25, gesture_event->y);
1712 EXPECT_EQ(20, gesture_event->globalX);
1713 EXPECT_EQ(25, gesture_event->globalY);
1714 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1716 // Indicate that the wheel event was unhandled.
1717 SendInputEventACK(WebInputEvent::GesturePinchUpdate,
1718 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1720 // Check that the correct unhandled pinch event was received.
1721 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1722 ASSERT_EQ(WebInputEvent::GesturePinchUpdate, ack_handler_->ack_event_type());
1723 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, ack_handler_->ack_state());
1724 EXPECT_EQ(1.5f, ack_handler_->acked_gesture_event().data.pinchUpdate.scale);
1725 EXPECT_EQ(0, client_->in_flight_event_count());
1727 // Second a second pinch event.
1728 SimulateGesturePinchUpdateEvent(
1729 0.3f, 20, 25, 0, blink::WebGestureDeviceTouchpad);
1730 input_event = GetInputEventFromMessage(*process_->sink().GetMessageAt(0));
1731 ASSERT_EQ(WebInputEvent::GesturePinchUpdate, input_event->type);
1732 gesture_event = static_cast<const WebGestureEvent*>(input_event);
1733 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1735 // Indicate that the wheel event was handled this time.
1736 SendInputEventACK(WebInputEvent::GesturePinchUpdate,
1737 INPUT_EVENT_ACK_STATE_CONSUMED);
1739 // Check that the correct HANDLED pinch event was received.
1740 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1741 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, ack_handler_->ack_event_type());
1742 EXPECT_EQ(INPUT_EVENT_ACK_STATE_CONSUMED, ack_handler_->ack_state());
1743 EXPECT_FLOAT_EQ(0.3f,
1744 ack_handler_->acked_gesture_event().data.pinchUpdate.scale);
1747 // Test proper handling of touchpad Gesture{Pinch,Scroll}Update sequences.
1748 TEST_F(InputRouterImplTest, TouchpadPinchAndScrollUpdate) {
1749 // The first scroll should be sent immediately.
1750 SimulateGestureScrollUpdateEvent(1.5f, 0.f, 0,
1751 blink::WebGestureDeviceTouchpad);
1752 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate,
1753 blink::WebGestureDeviceTouchpad);
1754 ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
1755 EXPECT_EQ(1, client_->in_flight_event_count());
1757 // Subsequent scroll and pinch events should remain queued, coalescing as
1758 // more trackpad events arrive.
1759 SimulateGesturePinchUpdateEvent(1.5f, 20, 25, 0,
1760 blink::WebGestureDeviceTouchpad);
1761 ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
1762 EXPECT_EQ(1, client_->in_flight_event_count());
1764 SimulateGestureScrollUpdateEvent(1.5f, 1.5f, 0,
1765 blink::WebGestureDeviceTouchpad);
1766 ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
1767 EXPECT_EQ(1, client_->in_flight_event_count());
1769 SimulateGesturePinchUpdateEvent(1.5f, 20, 25, 0,
1770 blink::WebGestureDeviceTouchpad);
1771 ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
1772 EXPECT_EQ(1, client_->in_flight_event_count());
1774 SimulateGestureScrollUpdateEvent(0.f, 1.5f, 0,
1775 blink::WebGestureDeviceTouchpad);
1776 ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
1777 EXPECT_EQ(1, client_->in_flight_event_count());
1779 // Ack'ing the first scroll should trigger both the coalesced scroll and the
1780 // coalesced pinch events (which is sent to the renderer as a wheel event).
1781 SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1782 INPUT_EVENT_ACK_STATE_CONSUMED);
1783 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1784 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1785 EXPECT_EQ(2, client_->in_flight_event_count());
1787 // Ack the second scroll.
1788 SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1789 INPUT_EVENT_ACK_STATE_CONSUMED);
1790 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1791 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1792 EXPECT_EQ(1, client_->in_flight_event_count());
1794 // Ack the wheel event.
1795 SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
1796 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1797 EXPECT_EQ(1U, ack_handler_->GetAndResetAckCount());
1798 EXPECT_EQ(0, client_->in_flight_event_count());
1801 // Test proper routing of overscroll notifications received either from
1802 // event acks or from |DidOverscroll| IPC messages.
1803 TEST_F(InputRouterImplTest, OverscrollDispatch) {
1804 DidOverscrollParams overscroll;
1805 overscroll.accumulated_overscroll = gfx::Vector2dF(-14, 14);
1806 overscroll.latest_overscroll_delta = gfx::Vector2dF(-7, 0);
1807 overscroll.current_fling_velocity = gfx::Vector2dF(-1, 0);
1809 input_router_->OnMessageReceived(InputHostMsg_DidOverscroll(0, overscroll));
1810 DidOverscrollParams client_overscroll = client_->GetAndResetOverscroll();
1811 EXPECT_EQ(overscroll.accumulated_overscroll,
1812 client_overscroll.accumulated_overscroll);
1813 EXPECT_EQ(overscroll.latest_overscroll_delta,
1814 client_overscroll.latest_overscroll_delta);
1815 EXPECT_EQ(overscroll.current_fling_velocity,
1816 client_overscroll.current_fling_velocity);
1818 DidOverscrollParams wheel_overscroll;
1819 wheel_overscroll.accumulated_overscroll = gfx::Vector2dF(7, -7);
1820 wheel_overscroll.latest_overscroll_delta = gfx::Vector2dF(3, 0);
1821 wheel_overscroll.current_fling_velocity = gfx::Vector2dF(1, 0);
1823 SimulateWheelEvent(3, 0, 0, false);
1824 InputEventAck ack(WebInputEvent::MouseWheel,
1825 INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1826 ack.overscroll.reset(new DidOverscrollParams(wheel_overscroll));
1827 input_router_->OnMessageReceived(InputHostMsg_HandleInputEvent_ACK(0, ack));
1829 client_overscroll = client_->GetAndResetOverscroll();
1830 EXPECT_EQ(wheel_overscroll.accumulated_overscroll,
1831 client_overscroll.accumulated_overscroll);
1832 EXPECT_EQ(wheel_overscroll.latest_overscroll_delta,
1833 client_overscroll.latest_overscroll_delta);
1834 EXPECT_EQ(wheel_overscroll.current_fling_velocity,
1835 client_overscroll.current_fling_velocity);
1838 } // namespace content