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