1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/common/input/input_param_traits.h"
7 #include "content/common/input/event_packet.h"
8 #include "content/common/input/input_event.h"
9 #include "content/common/input/ipc_input_event_payload.h"
10 #include "content/common/input/web_input_event_payload.h"
11 #include "content/common/input_messages.h"
12 #include "ipc/ipc_message.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/web/WebInputEvent.h"
19 class InputParamTraitsTest
: public testing::Test
{
21 void Compare(const WebInputEventPayload
* a
, const WebInputEventPayload
* b
) {
22 EXPECT_EQ(!!a
->web_event(), !!b
->web_event());
23 if (a
->web_event() && b
->web_event()) {
24 const size_t a_size
= a
->web_event()->size
;
25 ASSERT_EQ(a_size
, b
->web_event()->size
);
26 EXPECT_EQ(0, memcmp(a
->web_event(), b
->web_event(), a_size
));
28 EXPECT_EQ(a
->latency_info().latency_components
.size(),
29 b
->latency_info().latency_components
.size());
30 EXPECT_EQ(a
->is_keyboard_shortcut(), b
->is_keyboard_shortcut());
33 void Compare(const IPCInputEventPayload
* a
, const IPCInputEventPayload
* b
) {
34 EXPECT_EQ(!!a
->message
, !!b
->message
);
35 if (a
->message
&& b
->message
) {
36 EXPECT_EQ(a
->message
->type(), b
->message
->type());
37 EXPECT_EQ(a
->message
->routing_id(), b
->message
->routing_id());
41 void Compare(const InputEvent::Payload
* a
, const InputEvent::Payload
* b
) {
45 switch (a
->GetType()) {
46 case InputEvent::Payload::IPC_MESSAGE
:
47 Compare(IPCInputEventPayload::Cast(a
), IPCInputEventPayload::Cast(b
));
49 case InputEvent::Payload::WEB_INPUT_EVENT
:
50 Compare(WebInputEventPayload::Cast(a
), WebInputEventPayload::Cast(b
));
56 void Compare(const InputEvent
* a
, const InputEvent
* b
) {
57 EXPECT_EQ(a
->id(), b
->id());
58 EXPECT_EQ(a
->valid(), b
->valid());
59 Compare(a
->payload(), b
->payload());
62 void Compare(const EventPacket
* a
, const EventPacket
* b
) {
63 EXPECT_EQ(a
->id(), b
->id());
64 ASSERT_EQ(a
->size(), b
->size());
65 for (size_t i
= 0; i
< a
->size(); ++i
)
66 Compare(a
->events()[i
], b
->events()[i
]);
69 void Verify(const EventPacket
& packet_in
) {
71 IPC::ParamTraits
<EventPacket
>::Write(&msg
, packet_in
);
73 EventPacket packet_out
;
74 PickleIterator
iter(msg
);
75 EXPECT_TRUE(IPC::ParamTraits
<EventPacket
>::Read(&msg
, &iter
, &packet_out
));
77 Compare(&packet_in
, &packet_out
);
79 // Perform a sanity check that logging doesn't explode.
80 std::string packet_in_string
;
81 IPC::ParamTraits
<EventPacket
>::Log(packet_in
, &packet_in_string
);
82 std::string packet_out_string
;
83 IPC::ParamTraits
<EventPacket
>::Log(packet_out
, &packet_out_string
);
84 ASSERT_FALSE(packet_in_string
.empty());
85 EXPECT_EQ(packet_in_string
, packet_out_string
);
89 TEST_F(InputParamTraitsTest
, EventPacketEmpty
) {
90 EventPacket packet_in
;
92 IPC::ParamTraits
<EventPacket
>::Write(&msg
, packet_in
);
94 EventPacket packet_out
;
95 PickleIterator
iter(msg
);
96 EXPECT_TRUE(IPC::ParamTraits
<EventPacket
>::Read(&msg
, &iter
, &packet_out
));
98 Compare(&packet_in
, &packet_out
);
101 TEST_F(InputParamTraitsTest
, EventPacketUninitializedEvents
) {
102 EventPacket packet_in
;
104 packet_in
.Add(InputEvent::Create(1, WebInputEventPayload::Create()));
105 packet_in
.Add(InputEvent::Create(2, IPCInputEventPayload::Create()));
108 IPC::ParamTraits
<EventPacket
>::Write(&msg
, packet_in
);
110 EventPacket packet_out
;
111 PickleIterator
iter(msg
);
112 EXPECT_FALSE(IPC::ParamTraits
<EventPacket
>::Read(&msg
, &iter
, &packet_out
));
115 TEST_F(InputParamTraitsTest
, EventPacketIPCEvents
) {
116 EventPacket packet_in
;
120 InputEvent::Create(1,
121 IPCInputEventPayload::Create(
122 scoped_ptr
<IPC::Message
>(new InputMsg_Undo(1)))));
123 packet_in
.Add(InputEvent::Create(
125 IPCInputEventPayload::Create(
126 scoped_ptr
<IPC::Message
>(new InputMsg_SetFocus(2, true)))));
130 TEST_F(InputParamTraitsTest
, EventPacketWebInputEvents
) {
131 EventPacket packet_in
;
134 ui::LatencyInfo latency
;
136 int64 next_event_id
= 1;
137 WebKit::WebKeyboardEvent key_event
;
138 key_event
.type
= WebKit::WebInputEvent::RawKeyDown
;
139 key_event
.nativeKeyCode
= 5;
140 packet_in
.Add(InputEvent::Create(
141 ++next_event_id
, WebInputEventPayload::Create(key_event
, latency
, true)));
143 WebKit::WebMouseWheelEvent wheel_event
;
144 wheel_event
.type
= WebKit::WebInputEvent::MouseWheel
;
145 wheel_event
.deltaX
= 10;
146 latency
.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_RWH_COMPONENT
, 1, 1);
147 packet_in
.Add(InputEvent::Create(
149 WebInputEventPayload::Create(wheel_event
, latency
, false)));
151 WebKit::WebMouseEvent mouse_event
;
152 mouse_event
.type
= WebKit::WebInputEvent::MouseDown
;
154 latency
.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_UI_COMPONENT
, 2, 2);
155 packet_in
.Add(InputEvent::Create(
157 WebInputEventPayload::Create(mouse_event
, latency
, false)));
159 WebKit::WebGestureEvent gesture_event
;
160 gesture_event
.type
= WebKit::WebInputEvent::GestureScrollBegin
;
161 gesture_event
.x
= -1;
162 packet_in
.Add(InputEvent::Create(
164 WebInputEventPayload::Create(gesture_event
, latency
, false)));
166 WebKit::WebTouchEvent touch_event
;
167 touch_event
.type
= WebKit::WebInputEvent::TouchStart
;
168 touch_event
.touchesLength
= 1;
169 touch_event
.touches
[0].radiusX
= 1;
170 packet_in
.Add(InputEvent::Create(
172 WebInputEventPayload::Create(touch_event
, latency
, false)));
177 TEST_F(InputParamTraitsTest
, EventPacketMixedEvents
) {
178 EventPacket packet_in
;
180 int64 next_event_id
= 1;
182 // Add a mix of IPC and WebInputEvents.
184 InputEvent::Create(++next_event_id
,
185 IPCInputEventPayload::Create(
186 scoped_ptr
<IPC::Message
>(new InputMsg_Undo(1)))));
188 ui::LatencyInfo latency
;
189 WebKit::WebKeyboardEvent key_event
;
190 key_event
.type
= WebKit::WebInputEvent::RawKeyDown
;
191 key_event
.nativeKeyCode
= 5;
192 packet_in
.Add(InputEvent::Create(
193 ++next_event_id
, WebInputEventPayload::Create(key_event
, latency
, true)));
195 packet_in
.Add(InputEvent::Create(
197 IPCInputEventPayload::Create(
198 scoped_ptr
<IPC::Message
>(new InputMsg_SetFocus(2, true)))));
200 WebKit::WebMouseWheelEvent wheel_event
;
201 wheel_event
.type
= WebKit::WebInputEvent::MouseWheel
;
202 wheel_event
.deltaX
= 10;
203 packet_in
.Add(InputEvent::Create(
205 WebInputEventPayload::Create(wheel_event
, latency
, false)));
211 } // namespace content