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.
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"
33 #include "content/browser/renderer_host/ui_events_helper.h"
34 #include "ui/events/event.h"
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
;
51 const WebInputEvent
* GetInputEventFromMessage(const IPC::Message
& message
) {
52 base::PickleIterator
iter(message
);
55 if (!iter
.ReadData(&data
, &data_length
))
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
;
65 } else if (WebInputEvent::isTouchEventType(type
)) {
66 static WebTouchEvent touch
;
68 } else if (WebInputEvent::isKeyboardEventType(type
)) {
69 static WebKeyboardEvent key
;
71 } else if (WebInputEvent::isGestureEventType(type
)) {
72 static WebGestureEvent gesture
;
74 } else if (type
== WebInputEvent::MouseWheel
) {
75 static WebMouseWheelEvent wheel
;
83 bool GetIsShortcutFromHandleInputEventMessage(const IPC::Message
* msg
) {
84 InputMsg_HandleInputEvent::Schema::Param param
;
85 InputMsg_HandleInputEvent::Read(msg
, ¶m
);
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
, ¶m
));
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
,
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
, ¶m
));
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())
113 if (first
.location() != second
.location())
115 if (first
.touch_id() != second
.touch_id())
117 if (second
.time_stamp().InSeconds() != first
.time_stamp().InSeconds())
122 bool EventListIsSubset(const ScopedVector
<ui::TouchEvent
>& subset
,
123 const ScopedVector
<ui::TouchEvent
>& set
) {
124 if (subset
.size() > set
.size())
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
);
136 #endif // defined(USE_AURA)
140 class InputRouterImplTest
: public testing::Test
{
142 InputRouterImplTest() {}
143 ~InputRouterImplTest() override
{}
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(),
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();
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;
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 input_router_
->SendKeyboardEvent(
195 void SimulateWheelEvent(float dX
, float dY
, int modifiers
, bool precise
) {
196 input_router_
->SendWheelEvent(MouseWheelEventWithLatencyInfo(
197 SyntheticWebMouseWheelEventBuilder::Build(dX
, dY
, modifiers
, precise
)));
200 void SimulateMouseEvent(WebInputEvent::Type type
, int x
, int y
) {
201 input_router_
->SendMouseEvent(MouseEventWithLatencyInfo(
202 SyntheticWebMouseEventBuilder::Build(type
, x
, y
, 0)));
205 void SimulateWheelEventWithPhase(WebMouseWheelEvent::Phase phase
) {
206 input_router_
->SendWheelEvent(MouseWheelEventWithLatencyInfo(
207 SyntheticWebMouseWheelEventBuilder::Build(phase
)));
210 void SimulateGestureEvent(WebGestureEvent gesture
) {
211 // Ensure non-zero touchscreen fling velocities, as the router will
212 // validate aganst such.
213 if (gesture
.type
== WebInputEvent::GestureFlingStart
&&
214 gesture
.sourceDevice
== blink::WebGestureDeviceTouchscreen
&&
215 !gesture
.data
.flingStart
.velocityX
&&
216 !gesture
.data
.flingStart
.velocityY
) {
217 gesture
.data
.flingStart
.velocityX
= 5.f
;
220 input_router_
->SendGestureEvent(GestureEventWithLatencyInfo(gesture
));
223 void SimulateGestureEvent(WebInputEvent::Type type
,
224 WebGestureDevice source_device
) {
225 SimulateGestureEvent(
226 SyntheticWebGestureEventBuilder::Build(type
, source_device
));
229 void SimulateGestureScrollUpdateEvent(float dX
,
232 WebGestureDevice source_device
) {
233 SimulateGestureEvent(SyntheticWebGestureEventBuilder::BuildScrollUpdate(
234 dX
, dY
, modifiers
, source_device
));
237 void SimulateGesturePinchUpdateEvent(float scale
,
241 WebGestureDevice source_device
) {
242 SimulateGestureEvent(SyntheticWebGestureEventBuilder::BuildPinchUpdate(
243 scale
, anchor_x
, anchor_y
, modifiers
, source_device
));
246 void SimulateGestureFlingStartEvent(float velocity_x
,
248 WebGestureDevice source_device
) {
249 SimulateGestureEvent(SyntheticWebGestureEventBuilder::BuildFling(
250 velocity_x
, velocity_y
, source_device
));
253 void SetTouchTimestamp(base::TimeDelta timestamp
) {
254 touch_event_
.SetTimestamp(timestamp
);
257 uint32
SendTouchEvent() {
258 uint32 touch_event_id
= touch_event_
.uniqueTouchEventId
;
259 input_router_
->SendTouchEvent(TouchEventWithLatencyInfo(touch_event_
));
260 touch_event_
.ResetPoints();
261 return touch_event_id
;
264 int PressTouchPoint(int x
, int y
) {
265 return touch_event_
.PressPoint(x
, y
);
268 void MoveTouchPoint(int index
, int x
, int y
) {
269 touch_event_
.MovePoint(index
, x
, y
);
272 void ReleaseTouchPoint(int index
) {
273 touch_event_
.ReleasePoint(index
);
276 void CancelTouchPoint(int index
) {
277 touch_event_
.CancelPoint(index
);
280 void SendInputEventACK(blink::WebInputEvent::Type type
,
281 InputEventAckState ack_result
) {
282 DCHECK(!WebInputEvent::isTouchEventType(type
));
283 InputEventAck
ack(type
, ack_result
);
284 input_router_
->OnMessageReceived(InputHostMsg_HandleInputEvent_ACK(0, ack
));
287 void SendTouchEventACK(blink::WebInputEvent::Type type
,
288 InputEventAckState ack_result
,
289 uint32 touch_event_id
) {
290 DCHECK(WebInputEvent::isTouchEventType(type
));
291 InputEventAck
ack(type
, ack_result
, touch_event_id
);
292 input_router_
->OnMessageReceived(InputHostMsg_HandleInputEvent_ACK(0, ack
));
295 InputRouterImpl
* input_router() const {
296 return input_router_
.get();
299 bool TouchEventQueueEmpty() const {
300 return input_router()->touch_event_queue_
.empty();
303 bool TouchEventTimeoutEnabled() const {
304 return input_router()->touch_event_queue_
.IsAckTimeoutEnabled();
307 void RequestNotificationWhenFlushed() const {
308 return input_router_
->RequestNotificationWhenFlushed();
311 size_t GetAndResetDidFlushCount() {
312 return client_
->GetAndResetDidFlushCount();
315 bool HasPendingEvents() const {
316 return input_router_
->HasPendingEvents();
319 void OnHasTouchEventHandlers(bool has_handlers
) {
320 input_router_
->OnMessageReceived(
321 ViewHostMsg_HasTouchEventHandlers(0, has_handlers
));
324 void OnSetTouchAction(content::TouchAction touch_action
) {
325 input_router_
->OnMessageReceived(
326 InputHostMsg_SetTouchAction(0, touch_action
));
329 size_t GetSentMessageCountAndResetSink() {
330 size_t count
= process_
->sink().message_count();
331 process_
->sink().ClearMessages();
335 static void RunTasksAndWait(base::TimeDelta delay
) {
336 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
337 FROM_HERE
, base::MessageLoop::QuitClosure(), delay
);
338 base::MessageLoop::current()->Run();
341 InputRouterImpl::Config config_
;
342 scoped_ptr
<MockRenderProcessHost
> process_
;
343 scoped_ptr
<MockInputRouterClient
> client_
;
344 scoped_ptr
<MockInputAckHandler
> ack_handler_
;
345 scoped_ptr
<InputRouterImpl
> input_router_
;
348 base::MessageLoopForUI message_loop_
;
349 SyntheticWebTouchEvent touch_event_
;
351 scoped_ptr
<TestBrowserContext
> browser_context_
;
354 TEST_F(InputRouterImplTest
, CoalescesRangeSelection
) {
355 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
356 new InputMsg_SelectRange(0, gfx::Point(1, 2), gfx::Point(3, 4))));
357 ExpectIPCMessageWithArg2
<InputMsg_SelectRange
>(
358 process_
->sink().GetMessageAt(0),
361 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
363 // Send two more messages without acking.
364 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
365 new InputMsg_SelectRange(0, gfx::Point(5, 6), gfx::Point(7, 8))));
366 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
368 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
369 new InputMsg_SelectRange(0, gfx::Point(9, 10), gfx::Point(11, 12))));
370 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
372 // Now ack the first message.
374 scoped_ptr
<IPC::Message
> response(new InputHostMsg_SelectRange_ACK(0));
375 input_router_
->OnMessageReceived(*response
);
378 // Verify that the two messages are coalesced into one message.
379 ExpectIPCMessageWithArg2
<InputMsg_SelectRange
>(
380 process_
->sink().GetMessageAt(0),
383 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
385 // Acking the coalesced msg should not send any more msg.
387 scoped_ptr
<IPC::Message
> response(new InputHostMsg_SelectRange_ACK(0));
388 input_router_
->OnMessageReceived(*response
);
390 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
393 TEST_F(InputRouterImplTest
, CoalescesMoveRangeSelectionExtent
) {
394 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
395 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(1, 2))));
396 ExpectIPCMessageWithArg1
<InputMsg_MoveRangeSelectionExtent
>(
397 process_
->sink().GetMessageAt(0),
399 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
401 // Send two more messages without acking.
402 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
403 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(3, 4))));
404 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
406 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
407 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(5, 6))));
408 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
410 // Now ack the first message.
412 scoped_ptr
<IPC::Message
> response(
413 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
414 input_router_
->OnMessageReceived(*response
);
417 // Verify that the two messages are coalesced into one message.
418 ExpectIPCMessageWithArg1
<InputMsg_MoveRangeSelectionExtent
>(
419 process_
->sink().GetMessageAt(0),
421 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
423 // Acking the coalesced msg should not send any more msg.
425 scoped_ptr
<IPC::Message
> response(
426 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
427 input_router_
->OnMessageReceived(*response
);
429 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
432 TEST_F(InputRouterImplTest
, InterleaveSelectRangeAndMoveRangeSelectionExtent
) {
433 // Send first message: SelectRange.
434 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
435 new InputMsg_SelectRange(0, gfx::Point(1, 2), gfx::Point(3, 4))));
436 ExpectIPCMessageWithArg2
<InputMsg_SelectRange
>(
437 process_
->sink().GetMessageAt(0),
440 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
442 // Send second message: MoveRangeSelectionExtent.
443 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
444 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(5, 6))));
445 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
447 // Send third message: SelectRange.
448 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
449 new InputMsg_SelectRange(0, gfx::Point(7, 8), gfx::Point(9, 10))));
450 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
452 // Ack the messages and verify that they're not coalesced and that they're in
455 // Ack the first message.
457 scoped_ptr
<IPC::Message
> response(
458 new InputHostMsg_SelectRange_ACK(0));
459 input_router_
->OnMessageReceived(*response
);
462 ExpectIPCMessageWithArg1
<InputMsg_MoveRangeSelectionExtent
>(
463 process_
->sink().GetMessageAt(0),
465 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
467 // Ack the second message.
469 scoped_ptr
<IPC::Message
> response(
470 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
471 input_router_
->OnMessageReceived(*response
);
474 ExpectIPCMessageWithArg2
<InputMsg_SelectRange
>(
475 process_
->sink().GetMessageAt(0),
478 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
480 // Ack the third message.
482 scoped_ptr
<IPC::Message
> response(
483 new InputHostMsg_SelectRange_ACK(0));
484 input_router_
->OnMessageReceived(*response
);
486 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
489 TEST_F(InputRouterImplTest
,
490 CoalescesInterleavedSelectRangeAndMoveRangeSelectionExtent
) {
491 // Send interleaved SelectRange and MoveRangeSelectionExtent messages. They
492 // should be coalesced as shown by the arrows.
494 // MoveRangeSelectionExtent
495 // MoveRangeSelectionExtent
496 // > MoveRangeSelectionExtent
499 // > MoveRangeSelectionExtent
501 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
502 new InputMsg_SelectRange(0, gfx::Point(1, 2), gfx::Point(3, 4))));
503 ExpectIPCMessageWithArg2
<InputMsg_SelectRange
>(
504 process_
->sink().GetMessageAt(0),
507 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
509 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
510 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(5, 6))));
511 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
513 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
514 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(7, 8))));
515 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
517 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
518 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(9, 10))));
519 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
521 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
522 new InputMsg_SelectRange(0, gfx::Point(11, 12), gfx::Point(13, 14))));
523 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
525 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
526 new InputMsg_SelectRange(0, gfx::Point(15, 16), gfx::Point(17, 18))));
527 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
529 input_router_
->SendInput(scoped_ptr
<IPC::Message
>(
530 new InputMsg_MoveRangeSelectionExtent(0, gfx::Point(19, 20))));
531 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
533 // Ack the first message.
535 scoped_ptr
<IPC::Message
> response(
536 new InputHostMsg_SelectRange_ACK(0));
537 input_router_
->OnMessageReceived(*response
);
540 // Verify that the three MoveRangeSelectionExtent messages are coalesced into
542 ExpectIPCMessageWithArg1
<InputMsg_MoveRangeSelectionExtent
>(
543 process_
->sink().GetMessageAt(0),
545 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
547 // Ack the second message.
549 scoped_ptr
<IPC::Message
> response(
550 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
551 input_router_
->OnMessageReceived(*response
);
554 // Verify that the two SelectRange messages are coalesced into one message.
555 ExpectIPCMessageWithArg2
<InputMsg_SelectRange
>(
556 process_
->sink().GetMessageAt(0),
559 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
561 // Ack the third message.
563 scoped_ptr
<IPC::Message
> response(
564 new InputHostMsg_SelectRange_ACK(0));
565 input_router_
->OnMessageReceived(*response
);
568 // Verify the fourth message.
569 ExpectIPCMessageWithArg1
<InputMsg_MoveRangeSelectionExtent
>(
570 process_
->sink().GetMessageAt(0),
572 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
574 // Ack the fourth message.
576 scoped_ptr
<IPC::Message
> response(
577 new InputHostMsg_MoveRangeSelectionExtent_ACK(0));
578 input_router_
->OnMessageReceived(*response
);
580 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
583 TEST_F(InputRouterImplTest
, CoalescesCaretMove
) {
584 input_router_
->SendInput(
585 scoped_ptr
<IPC::Message
>(new InputMsg_MoveCaret(0, gfx::Point(1, 2))));
586 ExpectIPCMessageWithArg1
<InputMsg_MoveCaret
>(
587 process_
->sink().GetMessageAt(0), gfx::Point(1, 2));
588 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
590 // Send two more messages without acking.
591 input_router_
->SendInput(
592 scoped_ptr
<IPC::Message
>(new InputMsg_MoveCaret(0, gfx::Point(5, 6))));
593 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
595 input_router_
->SendInput(
596 scoped_ptr
<IPC::Message
>(new InputMsg_MoveCaret(0, gfx::Point(9, 10))));
597 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
599 // Now ack the first message.
601 scoped_ptr
<IPC::Message
> response(new InputHostMsg_MoveCaret_ACK(0));
602 input_router_
->OnMessageReceived(*response
);
605 // Verify that the two messages are coalesced into one message.
606 ExpectIPCMessageWithArg1
<InputMsg_MoveCaret
>(
607 process_
->sink().GetMessageAt(0), gfx::Point(9, 10));
608 EXPECT_EQ(1u, GetSentMessageCountAndResetSink());
610 // Acking the coalesced msg should not send any more msg.
612 scoped_ptr
<IPC::Message
> response(new InputHostMsg_MoveCaret_ACK(0));
613 input_router_
->OnMessageReceived(*response
);
615 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
618 TEST_F(InputRouterImplTest
, HandledInputEvent
) {
619 client_
->set_filter_state(INPUT_EVENT_ACK_STATE_CONSUMED
);
621 // Simulate a keyboard event.
622 SimulateKeyboardEvent(WebInputEvent::RawKeyDown
, false);
624 // Make sure no input event is sent to the renderer.
625 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
627 // OnKeyboardEventAck should be triggered without actual ack.
628 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
630 // As the event was acked already, keyboard event queue should be
632 ASSERT_EQ(NULL
, input_router_
->GetLastKeyboardEvent());
635 TEST_F(InputRouterImplTest
, ClientCanceledKeyboardEvent
) {
636 client_
->set_filter_state(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS
);
638 // Simulate a keyboard event that has no consumer.
639 SimulateKeyboardEvent(WebInputEvent::RawKeyDown
, false);
641 // Make sure no input event is sent to the renderer.
642 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
643 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
646 // Simulate a keyboard event that should be dropped.
647 client_
->set_filter_state(INPUT_EVENT_ACK_STATE_UNKNOWN
);
648 SimulateKeyboardEvent(WebInputEvent::RawKeyDown
, false);
650 // Make sure no input event is sent to the renderer, and no ack is sent.
651 EXPECT_EQ(0u, GetSentMessageCountAndResetSink());
652 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
655 TEST_F(InputRouterImplTest
, ShortcutKeyboardEvent
) {
656 SimulateKeyboardEvent(WebInputEvent::RawKeyDown
, true);
657 EXPECT_TRUE(GetIsShortcutFromHandleInputEventMessage(
658 process_
->sink().GetMessageAt(0)));
660 process_
->sink().ClearMessages();
662 SimulateKeyboardEvent(WebInputEvent::RawKeyDown
, false);
663 EXPECT_FALSE(GetIsShortcutFromHandleInputEventMessage(
664 process_
->sink().GetMessageAt(0)));
667 TEST_F(InputRouterImplTest
, NoncorrespondingKeyEvents
) {
668 SimulateKeyboardEvent(WebInputEvent::RawKeyDown
, false);
670 SendInputEventACK(WebInputEvent::KeyUp
,
671 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
672 EXPECT_TRUE(ack_handler_
->unexpected_event_ack_called());
675 // Tests ported from RenderWidgetHostTest --------------------------------------
677 TEST_F(InputRouterImplTest
, HandleKeyEventsWeSent
) {
678 // Simulate a keyboard event.
679 SimulateKeyboardEvent(WebInputEvent::RawKeyDown
, false);
680 ASSERT_TRUE(input_router_
->GetLastKeyboardEvent());
681 EXPECT_EQ(WebInputEvent::RawKeyDown
,
682 input_router_
->GetLastKeyboardEvent()->type
);
684 // Make sure we sent the input event to the renderer.
685 EXPECT_TRUE(process_
->sink().GetUniqueMessageMatching(
686 InputMsg_HandleInputEvent::ID
));
687 process_
->sink().ClearMessages();
689 // Send the simulated response from the renderer back.
690 SendInputEventACK(WebInputEvent::RawKeyDown
,
691 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
692 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
693 EXPECT_EQ(WebInputEvent::RawKeyDown
,
694 ack_handler_
->acked_keyboard_event().type
);
697 TEST_F(InputRouterImplTest
, IgnoreKeyEventsWeDidntSend
) {
698 // Send a simulated, unrequested key response. We should ignore this.
699 SendInputEventACK(WebInputEvent::RawKeyDown
,
700 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
702 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
705 TEST_F(InputRouterImplTest
, CoalescesWheelEvents
) {
706 // Simulate wheel events.
707 SimulateWheelEvent(0, -5, 0, false); // sent directly
708 SimulateWheelEvent(0, -10, 0, false); // enqueued
709 SimulateWheelEvent(8, -6, 0, false); // coalesced into previous event
710 SimulateWheelEvent(9, -7, 1, false); // enqueued, different modifiers
711 SimulateWheelEvent(0, -10, 0, false); // enqueued, different modifiers
712 // Explicitly verify that PhaseEnd isn't coalesced to avoid bugs like
713 // https://crbug.com/154740.
714 SimulateWheelEventWithPhase(WebMouseWheelEvent::PhaseEnded
); // enqueued
716 // Check that only the first event was sent.
717 EXPECT_TRUE(process_
->sink().GetUniqueMessageMatching(
718 InputMsg_HandleInputEvent::ID
));
719 const WebInputEvent
* input_event
=
720 GetInputEventFromMessage(*process_
->sink().GetMessageAt(0));
721 ASSERT_EQ(WebInputEvent::MouseWheel
, input_event
->type
);
722 const WebMouseWheelEvent
* wheel_event
=
723 static_cast<const WebMouseWheelEvent
*>(input_event
);
724 EXPECT_EQ(0, wheel_event
->deltaX
);
725 EXPECT_EQ(-5, wheel_event
->deltaY
);
726 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
728 // Check that the ACK sends the second message immediately.
729 SendInputEventACK(WebInputEvent::MouseWheel
,
730 INPUT_EVENT_ACK_STATE_CONSUMED
);
731 // The coalesced events can queue up a delayed ack
732 // so that additional input events can be processed before
733 // we turn off coalescing.
734 base::MessageLoop::current()->RunUntilIdle();
735 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
736 EXPECT_TRUE(process_
->sink().GetUniqueMessageMatching(
737 InputMsg_HandleInputEvent::ID
));
738 input_event
= GetInputEventFromMessage(*process_
->sink().GetMessageAt(0));
739 ASSERT_EQ(WebInputEvent::MouseWheel
, input_event
->type
);
740 wheel_event
= static_cast<const WebMouseWheelEvent
*>(input_event
);
741 EXPECT_EQ(8, wheel_event
->deltaX
);
742 EXPECT_EQ(-10 + -6, wheel_event
->deltaY
); // coalesced
743 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
745 // Ack the second event (which had the third coalesced into it).
746 SendInputEventACK(WebInputEvent::MouseWheel
,
747 INPUT_EVENT_ACK_STATE_CONSUMED
);
748 base::MessageLoop::current()->RunUntilIdle();
749 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
750 EXPECT_TRUE(process_
->sink().GetUniqueMessageMatching(
751 InputMsg_HandleInputEvent::ID
));
752 input_event
= GetInputEventFromMessage(*process_
->sink().GetMessageAt(0));
753 ASSERT_EQ(WebInputEvent::MouseWheel
, input_event
->type
);
754 wheel_event
= static_cast<const WebMouseWheelEvent
*>(input_event
);
755 EXPECT_EQ(9, wheel_event
->deltaX
);
756 EXPECT_EQ(-7, wheel_event
->deltaY
);
757 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
759 // Ack the fourth event.
760 SendInputEventACK(WebInputEvent::MouseWheel
,
761 INPUT_EVENT_ACK_STATE_CONSUMED
);
762 base::MessageLoop::current()->RunUntilIdle();
763 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
765 process_
->sink().GetUniqueMessageMatching(InputMsg_HandleInputEvent::ID
));
766 input_event
= GetInputEventFromMessage(*process_
->sink().GetMessageAt(0));
767 ASSERT_EQ(WebInputEvent::MouseWheel
, input_event
->type
);
768 wheel_event
= static_cast<const WebMouseWheelEvent
*>(input_event
);
769 EXPECT_EQ(0, wheel_event
->deltaX
);
770 EXPECT_EQ(-10, wheel_event
->deltaY
);
771 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
773 // Ack the fifth event.
774 SendInputEventACK(WebInputEvent::MouseWheel
, INPUT_EVENT_ACK_STATE_CONSUMED
);
775 base::MessageLoop::current()->RunUntilIdle();
776 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
778 process_
->sink().GetUniqueMessageMatching(InputMsg_HandleInputEvent::ID
));
779 input_event
= GetInputEventFromMessage(*process_
->sink().GetMessageAt(0));
780 ASSERT_EQ(WebInputEvent::MouseWheel
, input_event
->type
);
781 wheel_event
= static_cast<const WebMouseWheelEvent
*>(input_event
);
782 EXPECT_EQ(0, wheel_event
->deltaX
);
783 EXPECT_EQ(0, wheel_event
->deltaY
);
784 EXPECT_EQ(WebMouseWheelEvent::PhaseEnded
, wheel_event
->phase
);
785 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
787 // After the final ack, the queue should be empty.
788 SendInputEventACK(WebInputEvent::MouseWheel
, INPUT_EVENT_ACK_STATE_CONSUMED
);
789 base::MessageLoop::current()->RunUntilIdle();
790 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
791 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
794 // Tests that touch-events are queued properly.
795 TEST_F(InputRouterImplTest
, TouchEventQueue
) {
796 OnHasTouchEventHandlers(true);
798 PressTouchPoint(1, 1);
799 uint32 touch_press_event_id
= SendTouchEvent();
800 EXPECT_TRUE(client_
->GetAndResetFilterEventCalled());
801 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
802 EXPECT_FALSE(TouchEventQueueEmpty());
804 // The second touch should not be sent since one is already in queue.
805 MoveTouchPoint(0, 5, 5);
806 uint32 touch_move_event_id
= SendTouchEvent();
807 EXPECT_FALSE(client_
->GetAndResetFilterEventCalled());
808 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
809 EXPECT_FALSE(TouchEventQueueEmpty());
811 // Receive an ACK for the first touch-event.
812 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
813 touch_press_event_id
);
814 EXPECT_FALSE(TouchEventQueueEmpty());
815 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
816 EXPECT_EQ(WebInputEvent::TouchStart
,
817 ack_handler_
->acked_touch_event().event
.type
);
818 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
820 SendTouchEventACK(WebInputEvent::TouchMove
, INPUT_EVENT_ACK_STATE_CONSUMED
,
821 touch_move_event_id
);
822 EXPECT_TRUE(TouchEventQueueEmpty());
823 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
824 EXPECT_EQ(WebInputEvent::TouchMove
,
825 ack_handler_
->acked_touch_event().event
.type
);
826 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
829 // Tests that the touch-queue is emptied after a page stops listening for touch
830 // events and the outstanding ack is received.
831 TEST_F(InputRouterImplTest
, TouchEventQueueFlush
) {
832 OnHasTouchEventHandlers(true);
833 EXPECT_TRUE(client_
->has_touch_handler());
834 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
835 EXPECT_TRUE(TouchEventQueueEmpty());
837 // Send a touch-press event.
838 PressTouchPoint(1, 1);
839 uint32 touch_press_event_id
= SendTouchEvent();
840 MoveTouchPoint(0, 2, 2);
841 MoveTouchPoint(0, 3, 3);
842 EXPECT_FALSE(TouchEventQueueEmpty());
843 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
845 // The page stops listening for touch-events. Note that flushing is deferred
846 // until the outstanding ack is received.
847 OnHasTouchEventHandlers(false);
848 EXPECT_FALSE(client_
->has_touch_handler());
849 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
850 EXPECT_FALSE(TouchEventQueueEmpty());
852 // After the ack, the touch-event queue should be empty, and none of the
853 // flushed touch-events should have been sent to the renderer.
854 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
855 touch_press_event_id
);
856 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
857 EXPECT_TRUE(TouchEventQueueEmpty());
860 #if defined(USE_AURA)
861 // Tests that the acked events have correct state. (ui::Events are used only on
863 TEST_F(InputRouterImplTest
, AckedTouchEventState
) {
864 input_router_
->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
865 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
866 EXPECT_TRUE(TouchEventQueueEmpty());
868 // Send a bunch of events, and make sure the ACKed events are correct.
869 ScopedVector
<ui::TouchEvent
> expected_events
;
871 // Use a custom timestamp for all the events to test that the acked events
872 // have the same timestamp;
873 base::TimeDelta timestamp
= base::Time::NowFromSystemTime() - base::Time();
874 timestamp
-= base::TimeDelta::FromSeconds(600);
876 // Press the first finger.
877 PressTouchPoint(1, 1);
878 SetTouchTimestamp(timestamp
);
879 uint32 touch_press_event_id1
= SendTouchEvent();
880 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
881 expected_events
.push_back(new ui::TouchEvent(ui::ET_TOUCH_PRESSED
,
882 gfx::Point(1, 1), 0, timestamp
));
885 timestamp
+= base::TimeDelta::FromSeconds(10);
886 MoveTouchPoint(0, 500, 500);
887 SetTouchTimestamp(timestamp
);
888 uint32 touch_move_event_id1
= SendTouchEvent();
889 EXPECT_FALSE(TouchEventQueueEmpty());
890 expected_events
.push_back(new ui::TouchEvent(ui::ET_TOUCH_MOVED
,
891 gfx::Point(500, 500), 0, timestamp
));
893 // Now press a second finger.
894 timestamp
+= base::TimeDelta::FromSeconds(10);
895 PressTouchPoint(2, 2);
896 SetTouchTimestamp(timestamp
);
897 uint32 touch_press_event_id2
= SendTouchEvent();
898 EXPECT_FALSE(TouchEventQueueEmpty());
899 expected_events
.push_back(new ui::TouchEvent(ui::ET_TOUCH_PRESSED
,
900 gfx::Point(2, 2), 1, timestamp
));
902 // Move both fingers.
903 timestamp
+= base::TimeDelta::FromSeconds(10);
904 MoveTouchPoint(0, 10, 10);
905 MoveTouchPoint(1, 20, 20);
906 SetTouchTimestamp(timestamp
);
907 uint32 touch_move_event_id2
= SendTouchEvent();
908 EXPECT_FALSE(TouchEventQueueEmpty());
909 expected_events
.push_back(new ui::TouchEvent(ui::ET_TOUCH_MOVED
,
910 gfx::Point(10, 10), 0, timestamp
));
911 expected_events
.push_back(new ui::TouchEvent(ui::ET_TOUCH_MOVED
,
912 gfx::Point(20, 20), 1, timestamp
));
914 // Receive the ACKs and make sure the generated events from the acked events
916 WebInputEvent::Type acks
[] = { WebInputEvent::TouchStart
,
917 WebInputEvent::TouchMove
,
918 WebInputEvent::TouchStart
,
919 WebInputEvent::TouchMove
};
921 uint32 touch_event_ids
[] = {touch_press_event_id1
,
922 touch_move_event_id1
,
923 touch_press_event_id2
,
924 touch_move_event_id2
};
926 TouchEventCoordinateSystem coordinate_system
= LOCAL_COORDINATES
;
928 coordinate_system
= SCREEN_COORDINATES
;
930 for (size_t i
= 0; i
< arraysize(acks
); ++i
) {
931 SendTouchEventACK(acks
[i
], INPUT_EVENT_ACK_STATE_NOT_CONSUMED
,
933 EXPECT_EQ(acks
[i
], ack_handler_
->acked_touch_event().event
.type
);
934 ScopedVector
<ui::TouchEvent
> acked
;
936 MakeUITouchEventsFromWebTouchEvents(
937 ack_handler_
->acked_touch_event(), &acked
, coordinate_system
);
938 bool success
= EventListIsSubset(acked
, expected_events
);
939 EXPECT_TRUE(success
) << "Failed on step: " << i
;
942 expected_events
.erase(expected_events
.begin(),
943 expected_events
.begin() + acked
.size());
946 EXPECT_TRUE(TouchEventQueueEmpty());
947 EXPECT_EQ(0U, expected_events
.size());
949 #endif // defined(USE_AURA)
951 TEST_F(InputRouterImplTest
, UnhandledWheelEvent
) {
952 // Simulate wheel events.
953 SimulateWheelEvent(0, -5, 0, false); // sent directly
954 SimulateWheelEvent(0, -10, 0, false); // enqueued
956 // Check that only the first event was sent.
957 EXPECT_TRUE(process_
->sink().GetUniqueMessageMatching(
958 InputMsg_HandleInputEvent::ID
));
959 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
961 // Indicate that the wheel event was unhandled.
962 SendInputEventACK(WebInputEvent::MouseWheel
,
963 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
965 // Check that the correct unhandled wheel event was received.
966 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
967 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED
, ack_handler_
->ack_state());
968 EXPECT_EQ(ack_handler_
->acked_wheel_event().deltaY
, -5);
970 // Check that the second event was sent.
971 EXPECT_TRUE(process_
->sink().GetUniqueMessageMatching(
972 InputMsg_HandleInputEvent::ID
));
973 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
975 // Check that the correct unhandled wheel event was received.
976 EXPECT_EQ(ack_handler_
->acked_wheel_event().deltaY
, -5);
979 TEST_F(InputRouterImplTest
, TouchTypesIgnoringAck
) {
980 OnHasTouchEventHandlers(true);
981 // Only acks for TouchCancel should always be ignored.
982 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
983 GetEventWithType(WebInputEvent::TouchStart
)));
984 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
985 GetEventWithType(WebInputEvent::TouchMove
)));
986 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
987 GetEventWithType(WebInputEvent::TouchEnd
)));
989 // Precede the TouchCancel with an appropriate TouchStart;
990 PressTouchPoint(1, 1);
991 uint32 touch_press_event_id
= SendTouchEvent();
992 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
993 touch_press_event_id
);
994 ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
995 ASSERT_EQ(1U, ack_handler_
->GetAndResetAckCount());
996 ASSERT_EQ(0, client_
->in_flight_event_count());
998 // The TouchCancel ack is always ignored.
1000 uint32 touch_cancel_event_id
= SendTouchEvent();
1001 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1002 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1003 EXPECT_EQ(0, client_
->in_flight_event_count());
1004 EXPECT_FALSE(HasPendingEvents());
1005 SendTouchEventACK(WebInputEvent::TouchCancel
,
1006 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
, touch_cancel_event_id
);
1007 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1008 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1009 EXPECT_FALSE(HasPendingEvents());
1012 TEST_F(InputRouterImplTest
, GestureTypesIgnoringAck
) {
1013 // We test every gesture type, ensuring that the stream of gestures is valid.
1014 const WebInputEvent::Type eventTypes
[] = {
1015 WebInputEvent::GestureTapDown
,
1016 WebInputEvent::GestureShowPress
,
1017 WebInputEvent::GestureTapCancel
,
1018 WebInputEvent::GestureScrollBegin
,
1019 WebInputEvent::GestureFlingStart
,
1020 WebInputEvent::GestureFlingCancel
,
1021 WebInputEvent::GestureTapDown
,
1022 WebInputEvent::GestureTap
,
1023 WebInputEvent::GestureTapDown
,
1024 WebInputEvent::GestureLongPress
,
1025 WebInputEvent::GestureTapCancel
,
1026 WebInputEvent::GestureLongTap
,
1027 WebInputEvent::GestureTapDown
,
1028 WebInputEvent::GestureTapUnconfirmed
,
1029 WebInputEvent::GestureTapCancel
,
1030 WebInputEvent::GestureTapDown
,
1031 WebInputEvent::GestureDoubleTap
,
1032 WebInputEvent::GestureTapDown
,
1033 WebInputEvent::GestureTapCancel
,
1034 WebInputEvent::GestureTwoFingerTap
,
1035 WebInputEvent::GestureTapDown
,
1036 WebInputEvent::GestureTapCancel
,
1037 WebInputEvent::GestureScrollBegin
,
1038 WebInputEvent::GestureScrollUpdate
,
1039 WebInputEvent::GesturePinchBegin
,
1040 WebInputEvent::GesturePinchUpdate
,
1041 WebInputEvent::GesturePinchEnd
,
1042 WebInputEvent::GestureScrollEnd
};
1043 for (size_t i
= 0; i
< arraysize(eventTypes
); ++i
) {
1044 WebInputEvent::Type type
= eventTypes
[i
];
1045 if (WebInputEventTraits::WillReceiveAckFromRenderer(
1046 GetEventWithType(type
))) {
1047 SimulateGestureEvent(type
, blink::WebGestureDeviceTouchscreen
);
1048 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1049 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1050 EXPECT_EQ(1, client_
->in_flight_event_count());
1051 EXPECT_TRUE(HasPendingEvents());
1053 SendInputEventACK(type
, INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
1054 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1055 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1056 EXPECT_EQ(0, client_
->in_flight_event_count());
1057 EXPECT_FALSE(HasPendingEvents());
1061 SimulateGestureEvent(type
, blink::WebGestureDeviceTouchscreen
);
1062 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1063 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1064 EXPECT_EQ(0, client_
->in_flight_event_count());
1065 EXPECT_FALSE(HasPendingEvents());
1069 TEST_F(InputRouterImplTest
, MouseTypesIgnoringAck
) {
1070 int start_type
= static_cast<int>(WebInputEvent::MouseDown
);
1071 int end_type
= static_cast<int>(WebInputEvent::ContextMenu
);
1072 ASSERT_LT(start_type
, end_type
);
1073 for (int i
= start_type
; i
<= end_type
; ++i
) {
1074 WebInputEvent::Type type
= static_cast<WebInputEvent::Type
>(i
);
1075 int expected_in_flight_event_count
=
1076 !WebInputEventTraits::WillReceiveAckFromRenderer(GetEventWithType(type
))
1080 // Note: Mouse event acks are never forwarded to the ack handler, so the key
1081 // result here is that ignored ack types don't affect the in-flight count.
1082 SimulateMouseEvent(type
, 0, 0);
1083 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1084 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1085 EXPECT_EQ(expected_in_flight_event_count
, client_
->in_flight_event_count());
1086 if (expected_in_flight_event_count
) {
1087 SendInputEventACK(type
, INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
1088 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1089 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1090 EXPECT_EQ(0, client_
->in_flight_event_count());
1095 // Guard against breaking changes to the list of ignored event ack types in
1096 // |WebInputEventTraits::WillReceiveAckFromRenderer|.
1097 TEST_F(InputRouterImplTest
, RequiredEventAckTypes
) {
1098 const WebInputEvent::Type kRequiredEventAckTypes
[] = {
1099 WebInputEvent::MouseMove
,
1100 WebInputEvent::MouseWheel
,
1101 WebInputEvent::RawKeyDown
,
1102 WebInputEvent::KeyDown
,
1103 WebInputEvent::KeyUp
,
1104 WebInputEvent::Char
,
1105 WebInputEvent::GestureScrollUpdate
,
1106 WebInputEvent::GestureFlingStart
,
1107 WebInputEvent::GestureFlingCancel
,
1108 WebInputEvent::GesturePinchUpdate
,
1109 WebInputEvent::TouchStart
,
1110 WebInputEvent::TouchMove
1112 for (size_t i
= 0; i
< arraysize(kRequiredEventAckTypes
); ++i
) {
1113 const WebInputEvent::Type required_ack_type
= kRequiredEventAckTypes
[i
];
1114 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
1115 GetEventWithType(required_ack_type
)));
1119 // Test that GestureShowPress, GestureTapDown and GestureTapCancel events don't
1121 TEST_F(InputRouterImplTest
, GestureTypesIgnoringAckInterleaved
) {
1122 // Interleave a few events that do and do not ignore acks, ensuring that
1123 // ack-ignoring events aren't dispatched until all prior events which observe
1124 // their ack disposition have been dispatched.
1126 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1127 blink::WebGestureDeviceTouchscreen
);
1128 ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
1129 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1130 EXPECT_EQ(0, client_
->in_flight_event_count());
1132 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate
,
1133 blink::WebGestureDeviceTouchscreen
);
1134 ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
1135 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1136 EXPECT_EQ(1, client_
->in_flight_event_count());
1138 SimulateGestureEvent(WebInputEvent::GestureTapDown
,
1139 blink::WebGestureDeviceTouchscreen
);
1140 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1141 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1142 EXPECT_EQ(1, client_
->in_flight_event_count());
1144 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate
,
1145 blink::WebGestureDeviceTouchscreen
);
1146 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1147 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1149 SimulateGestureEvent(WebInputEvent::GestureShowPress
,
1150 blink::WebGestureDeviceTouchscreen
);
1151 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1152 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1154 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate
,
1155 blink::WebGestureDeviceTouchscreen
);
1156 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1157 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1159 SimulateGestureEvent(WebInputEvent::GestureTapCancel
,
1160 blink::WebGestureDeviceTouchscreen
);
1161 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1162 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1164 // Now ack each ack-respecting event. Ack-ignoring events should not be
1165 // dispatched until all prior events which observe ack disposition have been
1166 // fired, at which point they should be sent immediately. They should also
1167 // have no effect on the in-flight event count.
1168 SendInputEventACK(WebInputEvent::GestureScrollUpdate
,
1169 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
1170 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1171 EXPECT_EQ(2U, ack_handler_
->GetAndResetAckCount());
1172 EXPECT_EQ(1, client_
->in_flight_event_count());
1174 SendInputEventACK(WebInputEvent::GestureScrollUpdate
,
1175 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
1176 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1177 EXPECT_EQ(2U, ack_handler_
->GetAndResetAckCount());
1178 EXPECT_EQ(1, client_
->in_flight_event_count());
1180 SendInputEventACK(WebInputEvent::GestureScrollUpdate
,
1181 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
1182 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1183 EXPECT_EQ(2U, ack_handler_
->GetAndResetAckCount());
1184 EXPECT_EQ(0, client_
->in_flight_event_count());
1187 // Test that GestureShowPress events don't get out of order due to
1188 // ignoring their acks.
1189 TEST_F(InputRouterImplTest
, GestureShowPressIsInOrder
) {
1190 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1191 blink::WebGestureDeviceTouchscreen
);
1192 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1193 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1196 // GesturePinchBegin ignores its ack.
1197 SimulateGestureEvent(WebInputEvent::GesturePinchBegin
,
1198 blink::WebGestureDeviceTouchscreen
);
1199 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1200 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1202 // GesturePinchUpdate waits for an ack.
1203 // This also verifies that GesturePinchUpdates for touchscreen are sent
1204 // to the renderer (in contrast to the TrackpadPinchUpdate test).
1205 SimulateGestureEvent(WebInputEvent::GesturePinchUpdate
,
1206 blink::WebGestureDeviceTouchscreen
);
1207 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1208 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1210 SimulateGestureEvent(WebInputEvent::GestureShowPress
,
1211 blink::WebGestureDeviceTouchscreen
);
1212 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1213 // The ShowPress, though it ignores ack, is still stuck in the queue
1214 // behind the PinchUpdate which requires an ack.
1215 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1217 SimulateGestureEvent(WebInputEvent::GestureShowPress
,
1218 blink::WebGestureDeviceTouchscreen
);
1219 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1220 // ShowPress has entered the queue.
1221 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1223 SendInputEventACK(WebInputEvent::GesturePinchUpdate
,
1224 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
1225 // Now that the Tap has been ACKed, the ShowPress events should receive
1226 // synthetic acks, and fire immediately.
1227 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1228 EXPECT_EQ(3U, ack_handler_
->GetAndResetAckCount());
1231 // Test that touch ack timeout behavior is properly configured for
1232 // mobile-optimized sites and allowed touch actions.
1233 TEST_F(InputRouterImplTest
, TouchAckTimeoutConfigured
) {
1234 const int kDesktopTimeoutMs
= 1;
1235 const int kMobileTimeoutMs
= 0;
1236 SetUpForTouchAckTimeoutTest(kDesktopTimeoutMs
, kMobileTimeoutMs
);
1237 ASSERT_TRUE(TouchEventTimeoutEnabled());
1239 // Verify that the touch ack timeout fires upon the delayed ack.
1240 PressTouchPoint(1, 1);
1241 uint32 touch_press_event_id1
= SendTouchEvent();
1242 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1243 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1244 RunTasksAndWait(base::TimeDelta::FromMilliseconds(kDesktopTimeoutMs
+ 1));
1246 // The timed-out event should have been ack'ed.
1247 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1248 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1250 // Ack'ing the timed-out event should fire a TouchCancel.
1251 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1252 touch_press_event_id1
);
1253 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1254 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1256 // The remainder of the touch sequence should be dropped.
1257 ReleaseTouchPoint(0);
1259 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1260 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1261 ASSERT_TRUE(TouchEventTimeoutEnabled());
1263 // A mobile-optimized site should use the mobile timeout. For this test that
1264 // timeout value is 0, which disables the timeout.
1265 input_router()->NotifySiteIsMobileOptimized(true);
1266 EXPECT_FALSE(TouchEventTimeoutEnabled());
1268 input_router()->NotifySiteIsMobileOptimized(false);
1269 EXPECT_TRUE(TouchEventTimeoutEnabled());
1271 // TOUCH_ACTION_NONE (and no other touch-action) should disable the timeout.
1272 OnHasTouchEventHandlers(true);
1273 PressTouchPoint(1, 1);
1274 uint32 touch_press_event_id2
= SendTouchEvent();
1275 OnSetTouchAction(TOUCH_ACTION_PAN_Y
);
1276 EXPECT_TRUE(TouchEventTimeoutEnabled());
1277 ReleaseTouchPoint(0);
1278 uint32 touch_release_event_id2
= SendTouchEvent();
1279 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1280 touch_press_event_id2
);
1281 SendTouchEventACK(WebInputEvent::TouchEnd
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1282 touch_release_event_id2
);
1284 PressTouchPoint(1, 1);
1285 uint32 touch_press_event_id3
= SendTouchEvent();
1286 OnSetTouchAction(TOUCH_ACTION_NONE
);
1287 EXPECT_FALSE(TouchEventTimeoutEnabled());
1288 ReleaseTouchPoint(0);
1289 uint32 touch_release_event_id3
= SendTouchEvent();
1290 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1291 touch_press_event_id3
);
1292 SendTouchEventACK(WebInputEvent::TouchEnd
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1293 touch_release_event_id3
);
1295 // As the touch-action is reset by a new touch sequence, the timeout behavior
1296 // should be restored.
1297 PressTouchPoint(1, 1);
1299 EXPECT_TRUE(TouchEventTimeoutEnabled());
1302 // Test that a touch sequenced preceded by TOUCH_ACTION_NONE is not affected by
1303 // the touch timeout.
1304 TEST_F(InputRouterImplTest
,
1305 TouchAckTimeoutDisabledForTouchSequenceAfterTouchActionNone
) {
1306 const int kDesktopTimeoutMs
= 1;
1307 const int kMobileTimeoutMs
= 2;
1308 SetUpForTouchAckTimeoutTest(kDesktopTimeoutMs
, kMobileTimeoutMs
);
1309 ASSERT_TRUE(TouchEventTimeoutEnabled());
1310 OnHasTouchEventHandlers(true);
1312 // Start a touch sequence.
1313 PressTouchPoint(1, 1);
1314 uint32 touch_press_event_id
= SendTouchEvent();
1315 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1317 // TOUCH_ACTION_NONE should disable the timeout.
1318 OnSetTouchAction(TOUCH_ACTION_NONE
);
1319 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1320 touch_press_event_id
);
1321 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1322 EXPECT_FALSE(TouchEventTimeoutEnabled());
1324 MoveTouchPoint(0, 1, 2);
1325 uint32 touch_move_event_id
= SendTouchEvent();
1326 EXPECT_FALSE(TouchEventTimeoutEnabled());
1327 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1329 // Delay the move ack. The timeout should not fire.
1330 RunTasksAndWait(base::TimeDelta::FromMilliseconds(kDesktopTimeoutMs
+ 1));
1331 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1332 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1333 SendTouchEventACK(WebInputEvent::TouchMove
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1334 touch_move_event_id
);
1335 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1337 // End the touch sequence.
1338 ReleaseTouchPoint(0);
1339 uint32 touch_release_event_id
= SendTouchEvent();
1340 SendTouchEventACK(WebInputEvent::TouchEnd
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1341 touch_release_event_id
);
1342 EXPECT_FALSE(TouchEventTimeoutEnabled());
1343 ack_handler_
->GetAndResetAckCount();
1344 GetSentMessageCountAndResetSink();
1346 // Start another touch sequence. This should restore the touch timeout.
1347 PressTouchPoint(1, 1);
1349 EXPECT_TRUE(TouchEventTimeoutEnabled());
1350 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1351 EXPECT_EQ(0U, ack_handler_
->GetAndResetAckCount());
1353 // Wait for the touch ack timeout to fire.
1354 RunTasksAndWait(base::TimeDelta::FromMilliseconds(kDesktopTimeoutMs
+ 1));
1355 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1358 // Test that TouchActionFilter::ResetTouchAction is called before the
1359 // first touch event for a touch sequence reaches the renderer.
1360 TEST_F(InputRouterImplTest
, TouchActionResetBeforeEventReachesRenderer
) {
1361 OnHasTouchEventHandlers(true);
1364 PressTouchPoint(1, 1);
1365 uint32 touch_press_event_id1
= SendTouchEvent();
1366 OnSetTouchAction(TOUCH_ACTION_NONE
);
1367 MoveTouchPoint(0, 50, 50);
1368 uint32 touch_move_event_id1
= SendTouchEvent();
1369 ReleaseTouchPoint(0);
1370 uint32 touch_release_event_id1
= SendTouchEvent();
1373 PressTouchPoint(1, 1);
1374 uint32 touch_press_event_id2
= SendTouchEvent();
1375 MoveTouchPoint(0, 50, 50);
1376 uint32 touch_move_event_id2
= SendTouchEvent();
1377 ReleaseTouchPoint(0);
1378 uint32 touch_release_event_id2
= SendTouchEvent();
1380 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1381 touch_press_event_id1
);
1382 SendTouchEventACK(WebInputEvent::TouchMove
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1383 touch_move_event_id1
);
1385 // Ensure touch action is still none, as the next touch start hasn't been
1386 // acked yet. ScrollBegin and ScrollEnd don't require acks.
1387 EXPECT_EQ(3U, GetSentMessageCountAndResetSink());
1388 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1389 blink::WebGestureDeviceTouchscreen
);
1390 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1391 SimulateGestureEvent(WebInputEvent::GestureScrollEnd
,
1392 blink::WebGestureDeviceTouchscreen
);
1393 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1395 // This allows the next touch sequence to start.
1396 SendTouchEventACK(WebInputEvent::TouchEnd
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1397 touch_release_event_id1
);
1399 // Ensure touch action has been set to auto, as a new touch sequence has
1401 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1402 touch_press_event_id2
);
1403 SendTouchEventACK(WebInputEvent::TouchMove
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1404 touch_move_event_id2
);
1405 EXPECT_EQ(3U, GetSentMessageCountAndResetSink());
1406 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1407 blink::WebGestureDeviceTouchscreen
);
1408 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1409 SimulateGestureEvent(WebInputEvent::GestureScrollEnd
,
1410 blink::WebGestureDeviceTouchscreen
);
1411 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1412 SendTouchEventACK(WebInputEvent::TouchEnd
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1413 touch_release_event_id2
);
1416 // Test that TouchActionFilter::ResetTouchAction is called when a new touch
1417 // sequence has no consumer.
1418 TEST_F(InputRouterImplTest
, TouchActionResetWhenTouchHasNoConsumer
) {
1419 OnHasTouchEventHandlers(true);
1422 PressTouchPoint(1, 1);
1423 uint32 touch_press_event_id1
= SendTouchEvent();
1424 MoveTouchPoint(0, 50, 50);
1425 uint32 touch_move_event_id1
= SendTouchEvent();
1426 OnSetTouchAction(TOUCH_ACTION_NONE
);
1427 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1428 touch_press_event_id1
);
1429 SendTouchEventACK(WebInputEvent::TouchMove
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1430 touch_move_event_id1
);
1432 ReleaseTouchPoint(0);
1433 uint32 touch_release_event_id1
= SendTouchEvent();
1436 PressTouchPoint(1, 1);
1437 uint32 touch_press_event_id2
= SendTouchEvent();
1438 MoveTouchPoint(0, 50, 50);
1440 ReleaseTouchPoint(0);
1443 // Ensure we have touch-action:none. ScrollBegin and ScrollEnd don't require
1445 EXPECT_EQ(3U, GetSentMessageCountAndResetSink());
1446 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1447 blink::WebGestureDeviceTouchscreen
);
1448 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1449 SimulateGestureEvent(WebInputEvent::GestureScrollEnd
,
1450 blink::WebGestureDeviceTouchscreen
);
1451 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1453 SendTouchEventACK(WebInputEvent::TouchEnd
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1454 touch_release_event_id1
);
1455 SendTouchEventACK(WebInputEvent::TouchStart
,
1456 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS
,
1457 touch_press_event_id2
);
1459 // Ensure touch action has been set to auto, as the touch had no consumer.
1460 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1461 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1462 blink::WebGestureDeviceTouchscreen
);
1463 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1464 SimulateGestureEvent(WebInputEvent::GestureScrollEnd
,
1465 blink::WebGestureDeviceTouchscreen
);
1466 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1469 // Test that TouchActionFilter::ResetTouchAction is called when the touch
1470 // handler is removed.
1471 TEST_F(InputRouterImplTest
, TouchActionResetWhenTouchHandlerRemoved
) {
1472 // Touch sequence with touch handler.
1473 OnHasTouchEventHandlers(true);
1474 PressTouchPoint(1, 1);
1475 uint32 touch_press_event_id
= SendTouchEvent();
1476 MoveTouchPoint(0, 50, 50);
1477 uint32 touch_move_event_id
= SendTouchEvent();
1478 OnSetTouchAction(TOUCH_ACTION_NONE
);
1479 ReleaseTouchPoint(0);
1480 uint32 touch_release_event_id
= SendTouchEvent();
1481 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1483 // Ensure we have touch-action:none, suppressing scroll events.
1484 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1485 touch_press_event_id
);
1486 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1487 SendTouchEventACK(WebInputEvent::TouchMove
,
1488 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
, touch_move_event_id
);
1489 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1490 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1491 blink::WebGestureDeviceTouchscreen
);
1492 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1494 SendTouchEventACK(WebInputEvent::TouchEnd
, INPUT_EVENT_ACK_STATE_NOT_CONSUMED
,
1495 touch_release_event_id
);
1496 SimulateGestureEvent(WebInputEvent::GestureScrollEnd
,
1497 blink::WebGestureDeviceTouchscreen
);
1498 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1500 // Sequence without a touch handler. Note that in this case, the view may not
1501 // necessarily forward touches to the router (as no touch handler exists).
1502 OnHasTouchEventHandlers(false);
1504 // Ensure touch action has been set to auto, as the touch handler has been
1506 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1507 blink::WebGestureDeviceTouchscreen
);
1508 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1509 SimulateGestureEvent(WebInputEvent::GestureScrollEnd
,
1510 blink::WebGestureDeviceTouchscreen
);
1511 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1514 // Test that the double tap gesture depends on the touch action of the first
1516 TEST_F(InputRouterImplTest
, DoubleTapGestureDependsOnFirstTap
) {
1517 OnHasTouchEventHandlers(true);
1520 PressTouchPoint(1, 1);
1521 uint32 touch_press_event_id1
= SendTouchEvent();
1522 OnSetTouchAction(TOUCH_ACTION_NONE
);
1523 SendTouchEventACK(WebInputEvent::TouchStart
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1524 touch_press_event_id1
);
1526 ReleaseTouchPoint(0);
1527 uint32 touch_release_event_id
= SendTouchEvent();
1530 PressTouchPoint(1, 1);
1531 uint32 touch_press_event_id2
= SendTouchEvent();
1534 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1535 SimulateGestureEvent(WebInputEvent::GestureTapDown
,
1536 blink::WebGestureDeviceTouchscreen
);
1537 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1539 // The GestureTapUnconfirmed is converted into a tap, as the touch action is
1541 SimulateGestureEvent(WebInputEvent::GestureTapUnconfirmed
,
1542 blink::WebGestureDeviceTouchscreen
);
1543 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1544 // This test will become invalid if GestureTap stops requiring an ack.
1545 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
1546 GetEventWithType(WebInputEvent::GestureTap
)));
1547 EXPECT_EQ(2, client_
->in_flight_event_count());
1548 SendInputEventACK(WebInputEvent::GestureTap
,
1549 INPUT_EVENT_ACK_STATE_CONSUMED
);
1550 EXPECT_EQ(1, client_
->in_flight_event_count());
1552 // This tap gesture is dropped, since the GestureTapUnconfirmed was turned
1554 SimulateGestureEvent(WebInputEvent::GestureTap
,
1555 blink::WebGestureDeviceTouchscreen
);
1556 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1558 SendTouchEventACK(WebInputEvent::TouchEnd
, INPUT_EVENT_ACK_STATE_CONSUMED
,
1559 touch_release_event_id
);
1560 SendTouchEventACK(WebInputEvent::TouchStart
,
1561 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS
,
1562 touch_press_event_id2
);
1565 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1566 SimulateGestureEvent(WebInputEvent::GestureTapDown
,
1567 blink::WebGestureDeviceTouchscreen
);
1568 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1570 // Although the touch-action is now auto, the double tap still won't be
1571 // dispatched, because the first tap occured when the touch-action was none.
1572 SimulateGestureEvent(WebInputEvent::GestureDoubleTap
,
1573 blink::WebGestureDeviceTouchscreen
);
1574 // This test will become invalid if GestureDoubleTap stops requiring an ack.
1575 ASSERT_TRUE(WebInputEventTraits::WillReceiveAckFromRenderer(
1576 GetEventWithType(WebInputEvent::GestureDoubleTap
)));
1577 EXPECT_EQ(1, client_
->in_flight_event_count());
1578 SendInputEventACK(WebInputEvent::GestureTap
, INPUT_EVENT_ACK_STATE_CONSUMED
);
1579 EXPECT_EQ(0, client_
->in_flight_event_count());
1582 // Test that the router will call the client's |DidFlush| after all events have
1583 // been dispatched following a call to |Flush|.
1584 TEST_F(InputRouterImplTest
, InputFlush
) {
1585 EXPECT_FALSE(HasPendingEvents());
1587 // Flushing an empty router should immediately trigger DidFlush.
1588 RequestNotificationWhenFlushed();
1589 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1590 EXPECT_FALSE(HasPendingEvents());
1592 // Queue a TouchStart.
1593 OnHasTouchEventHandlers(true);
1594 PressTouchPoint(1, 1);
1595 uint32 touch_press_event_id
= SendTouchEvent();
1596 EXPECT_TRUE(HasPendingEvents());
1598 // DidFlush should be called only after the event is ack'ed.
1599 RequestNotificationWhenFlushed();
1600 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1601 SendTouchEventACK(WebInputEvent::TouchStart
,
1602 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
, touch_press_event_id
);
1603 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1605 // Ensure different types of enqueued events will prevent the DidFlush call
1606 // until all such events have been fully dispatched.
1607 MoveTouchPoint(0, 50, 50);
1608 uint32 touch_move_event_id
= SendTouchEvent();
1609 ASSERT_TRUE(HasPendingEvents());
1610 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1611 blink::WebGestureDeviceTouchscreen
);
1612 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate
,
1613 blink::WebGestureDeviceTouchscreen
);
1614 SimulateGestureEvent(WebInputEvent::GesturePinchBegin
,
1615 blink::WebGestureDeviceTouchscreen
);
1616 SimulateGestureEvent(WebInputEvent::GesturePinchUpdate
,
1617 blink::WebGestureDeviceTouchscreen
);
1618 RequestNotificationWhenFlushed();
1619 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1621 // Repeated flush calls should have no effect.
1622 RequestNotificationWhenFlushed();
1623 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1625 // There are still pending gestures.
1626 SendTouchEventACK(WebInputEvent::TouchMove
,
1627 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
, touch_move_event_id
);
1628 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1629 EXPECT_TRUE(HasPendingEvents());
1631 // One more gesture to go.
1632 SendInputEventACK(WebInputEvent::GestureScrollUpdate
,
1633 INPUT_EVENT_ACK_STATE_CONSUMED
);
1634 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1635 EXPECT_TRUE(HasPendingEvents());
1637 // The final ack'ed gesture should trigger the DidFlush.
1638 SendInputEventACK(WebInputEvent::GesturePinchUpdate
,
1639 INPUT_EVENT_ACK_STATE_CONSUMED
);
1640 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1641 EXPECT_FALSE(HasPendingEvents());
1644 // Test that the router will call the client's |DidFlush| after all fling
1645 // animations have completed.
1646 TEST_F(InputRouterImplTest
, InputFlushAfterFling
) {
1647 EXPECT_FALSE(HasPendingEvents());
1649 // Simulate a fling.
1650 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1651 blink::WebGestureDeviceTouchscreen
);
1652 SimulateGestureEvent(WebInputEvent::GestureFlingStart
,
1653 blink::WebGestureDeviceTouchscreen
);
1654 EXPECT_TRUE(HasPendingEvents());
1656 // If the fling is unconsumed, the flush is complete.
1657 RequestNotificationWhenFlushed();
1658 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1659 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1660 blink::WebGestureDeviceTouchscreen
);
1661 SendInputEventACK(WebInputEvent::GestureFlingStart
,
1662 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
1663 EXPECT_FALSE(HasPendingEvents());
1664 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1666 // Simulate a second fling.
1667 SimulateGestureEvent(WebInputEvent::GestureFlingStart
,
1668 blink::WebGestureDeviceTouchscreen
);
1669 EXPECT_TRUE(HasPendingEvents());
1671 // If the fling is consumed, the flush is complete only when the renderer
1672 // reports that is has ended.
1673 RequestNotificationWhenFlushed();
1674 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1675 SendInputEventACK(WebInputEvent::GestureFlingStart
,
1676 INPUT_EVENT_ACK_STATE_CONSUMED
);
1677 EXPECT_TRUE(HasPendingEvents());
1678 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1680 // The fling end notification should signal that the router is flushed.
1681 input_router()->OnMessageReceived(InputHostMsg_DidStopFlinging(0));
1682 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1684 // Even flings consumed by the client require a fling-end notification.
1685 client_
->set_filter_state(INPUT_EVENT_ACK_STATE_CONSUMED
);
1686 SimulateGestureEvent(WebInputEvent::GestureScrollBegin
,
1687 blink::WebGestureDeviceTouchscreen
);
1688 SimulateGestureEvent(WebInputEvent::GestureFlingStart
,
1689 blink::WebGestureDeviceTouchscreen
);
1690 ASSERT_TRUE(HasPendingEvents());
1691 RequestNotificationWhenFlushed();
1692 EXPECT_EQ(0U, GetAndResetDidFlushCount());
1693 input_router()->OnMessageReceived(InputHostMsg_DidStopFlinging(0));
1694 EXPECT_EQ(1U, GetAndResetDidFlushCount());
1697 // Test that GesturePinchUpdate is handled specially for trackpad
1698 TEST_F(InputRouterImplTest
, TouchpadPinchUpdate
) {
1699 // GesturePinchUpdate for trackpad sends synthetic wheel events.
1700 // Note that the Touchscreen case is verified as NOT doing this as
1701 // part of the ShowPressIsInOrder test.
1703 SimulateGesturePinchUpdateEvent(
1704 1.5f
, 20, 25, 0, blink::WebGestureDeviceTouchpad
);
1706 // Verify we actually sent a special wheel event to the renderer.
1707 const WebInputEvent
* input_event
=
1708 GetInputEventFromMessage(*process_
->sink().GetMessageAt(0));
1709 ASSERT_EQ(WebInputEvent::GesturePinchUpdate
, input_event
->type
);
1710 const WebGestureEvent
* gesture_event
=
1711 static_cast<const WebGestureEvent
*>(input_event
);
1712 EXPECT_EQ(20, gesture_event
->x
);
1713 EXPECT_EQ(25, gesture_event
->y
);
1714 EXPECT_EQ(20, gesture_event
->globalX
);
1715 EXPECT_EQ(25, gesture_event
->globalY
);
1716 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1718 // Indicate that the wheel event was unhandled.
1719 SendInputEventACK(WebInputEvent::GesturePinchUpdate
,
1720 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
1722 // Check that the correct unhandled pinch event was received.
1723 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1724 ASSERT_EQ(WebInputEvent::GesturePinchUpdate
, ack_handler_
->ack_event_type());
1725 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED
, ack_handler_
->ack_state());
1726 EXPECT_EQ(1.5f
, ack_handler_
->acked_gesture_event().data
.pinchUpdate
.scale
);
1727 EXPECT_EQ(0, client_
->in_flight_event_count());
1729 // Second a second pinch event.
1730 SimulateGesturePinchUpdateEvent(
1731 0.3f
, 20, 25, 0, blink::WebGestureDeviceTouchpad
);
1732 input_event
= GetInputEventFromMessage(*process_
->sink().GetMessageAt(0));
1733 ASSERT_EQ(WebInputEvent::GesturePinchUpdate
, input_event
->type
);
1734 gesture_event
= static_cast<const WebGestureEvent
*>(input_event
);
1735 EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
1737 // Indicate that the wheel event was handled this time.
1738 SendInputEventACK(WebInputEvent::GesturePinchUpdate
,
1739 INPUT_EVENT_ACK_STATE_CONSUMED
);
1741 // Check that the correct HANDLED pinch event was received.
1742 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1743 EXPECT_EQ(WebInputEvent::GesturePinchUpdate
, ack_handler_
->ack_event_type());
1744 EXPECT_EQ(INPUT_EVENT_ACK_STATE_CONSUMED
, ack_handler_
->ack_state());
1745 EXPECT_FLOAT_EQ(0.3f
,
1746 ack_handler_
->acked_gesture_event().data
.pinchUpdate
.scale
);
1749 // Test proper handling of touchpad Gesture{Pinch,Scroll}Update sequences.
1750 TEST_F(InputRouterImplTest
, TouchpadPinchAndScrollUpdate
) {
1751 // The first scroll should be sent immediately.
1752 SimulateGestureScrollUpdateEvent(1.5f
, 0.f
, 0,
1753 blink::WebGestureDeviceTouchpad
);
1754 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate
,
1755 blink::WebGestureDeviceTouchpad
);
1756 ASSERT_EQ(1U, GetSentMessageCountAndResetSink());
1757 EXPECT_EQ(1, client_
->in_flight_event_count());
1759 // Subsequent scroll and pinch events should remain queued, coalescing as
1760 // more trackpad events arrive.
1761 SimulateGesturePinchUpdateEvent(1.5f
, 20, 25, 0,
1762 blink::WebGestureDeviceTouchpad
);
1763 ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
1764 EXPECT_EQ(1, client_
->in_flight_event_count());
1766 SimulateGestureScrollUpdateEvent(1.5f
, 1.5f
, 0,
1767 blink::WebGestureDeviceTouchpad
);
1768 ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
1769 EXPECT_EQ(1, client_
->in_flight_event_count());
1771 SimulateGesturePinchUpdateEvent(1.5f
, 20, 25, 0,
1772 blink::WebGestureDeviceTouchpad
);
1773 ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
1774 EXPECT_EQ(1, client_
->in_flight_event_count());
1776 SimulateGestureScrollUpdateEvent(0.f
, 1.5f
, 0,
1777 blink::WebGestureDeviceTouchpad
);
1778 ASSERT_EQ(0U, GetSentMessageCountAndResetSink());
1779 EXPECT_EQ(1, client_
->in_flight_event_count());
1781 // Ack'ing the first scroll should trigger both the coalesced scroll and the
1782 // coalesced pinch events (which is sent to the renderer as a wheel event).
1783 SendInputEventACK(WebInputEvent::GestureScrollUpdate
,
1784 INPUT_EVENT_ACK_STATE_CONSUMED
);
1785 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1786 EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
1787 EXPECT_EQ(2, client_
->in_flight_event_count());
1789 // Ack the second scroll.
1790 SendInputEventACK(WebInputEvent::GestureScrollUpdate
,
1791 INPUT_EVENT_ACK_STATE_CONSUMED
);
1792 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1793 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1794 EXPECT_EQ(1, client_
->in_flight_event_count());
1796 // Ack the wheel event.
1797 SendInputEventACK(WebInputEvent::MouseWheel
, INPUT_EVENT_ACK_STATE_CONSUMED
);
1798 EXPECT_EQ(0U, GetSentMessageCountAndResetSink());
1799 EXPECT_EQ(1U, ack_handler_
->GetAndResetAckCount());
1800 EXPECT_EQ(0, client_
->in_flight_event_count());
1803 // Test proper routing of overscroll notifications received either from
1804 // event acks or from |DidOverscroll| IPC messages.
1805 TEST_F(InputRouterImplTest
, OverscrollDispatch
) {
1806 DidOverscrollParams overscroll
;
1807 overscroll
.accumulated_overscroll
= gfx::Vector2dF(-14, 14);
1808 overscroll
.latest_overscroll_delta
= gfx::Vector2dF(-7, 0);
1809 overscroll
.current_fling_velocity
= gfx::Vector2dF(-1, 0);
1811 input_router_
->OnMessageReceived(InputHostMsg_DidOverscroll(0, overscroll
));
1812 DidOverscrollParams client_overscroll
= client_
->GetAndResetOverscroll();
1813 EXPECT_EQ(overscroll
.accumulated_overscroll
,
1814 client_overscroll
.accumulated_overscroll
);
1815 EXPECT_EQ(overscroll
.latest_overscroll_delta
,
1816 client_overscroll
.latest_overscroll_delta
);
1817 EXPECT_EQ(overscroll
.current_fling_velocity
,
1818 client_overscroll
.current_fling_velocity
);
1820 DidOverscrollParams wheel_overscroll
;
1821 wheel_overscroll
.accumulated_overscroll
= gfx::Vector2dF(7, -7);
1822 wheel_overscroll
.latest_overscroll_delta
= gfx::Vector2dF(3, 0);
1823 wheel_overscroll
.current_fling_velocity
= gfx::Vector2dF(1, 0);
1825 SimulateWheelEvent(3, 0, 0, false);
1826 InputEventAck
ack(WebInputEvent::MouseWheel
,
1827 INPUT_EVENT_ACK_STATE_NOT_CONSUMED
);
1828 ack
.overscroll
.reset(new DidOverscrollParams(wheel_overscroll
));
1829 input_router_
->OnMessageReceived(InputHostMsg_HandleInputEvent_ACK(0, ack
));
1831 client_overscroll
= client_
->GetAndResetOverscroll();
1832 EXPECT_EQ(wheel_overscroll
.accumulated_overscroll
,
1833 client_overscroll
.accumulated_overscroll
);
1834 EXPECT_EQ(wheel_overscroll
.latest_overscroll_delta
,
1835 client_overscroll
.latest_overscroll_delta
);
1836 EXPECT_EQ(wheel_overscroll
.current_fling_velocity
,
1837 client_overscroll
.current_fling_velocity
);
1840 } // namespace content