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/input_event.h"
8 #include "content/common/input_messages.h"
9 #include "ipc/ipc_message.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/web/WebInputEvent.h"
16 typedef ScopedVector
<InputEvent
> InputEvents
;
18 class InputParamTraitsTest
: public testing::Test
{
20 static void Compare(const InputEvent
* a
, const InputEvent
* b
) {
21 EXPECT_EQ(!!a
->web_event
, !!b
->web_event
);
22 if (a
->web_event
&& b
->web_event
) {
23 const size_t a_size
= a
->web_event
->size
;
24 ASSERT_EQ(a_size
, b
->web_event
->size
);
25 EXPECT_EQ(0, memcmp(a
->web_event
.get(), b
->web_event
.get(), a_size
));
27 EXPECT_EQ(a
->latency_info
.latency_components
.size(),
28 b
->latency_info
.latency_components
.size());
29 EXPECT_EQ(a
->is_keyboard_shortcut
, b
->is_keyboard_shortcut
);
32 static void Compare(const InputEvents
* a
, const InputEvents
* b
) {
33 for (size_t i
= 0; i
< a
->size(); ++i
)
34 Compare((*a
)[i
], (*b
)[i
]);
37 static void Compare(const SyntheticSmoothScrollGestureParams
* a
,
38 const SyntheticSmoothScrollGestureParams
* b
) {
39 EXPECT_EQ(a
->gesture_source_type
, b
->gesture_source_type
);
40 EXPECT_EQ(a
->distance
, b
->distance
);
41 EXPECT_EQ(a
->anchor_x
, b
->anchor_x
);
42 EXPECT_EQ(a
->anchor_y
, b
->anchor_y
);
45 static void Compare(const SyntheticGesturePacket
* a
,
46 const SyntheticGesturePacket
* b
) {
49 ASSERT_EQ(!!a
->gesture_params(), !!b
->gesture_params());
50 if (!a
->gesture_params()) return;
51 ASSERT_EQ(a
->gesture_params()->GetGestureType(),
52 b
->gesture_params()->GetGestureType());
53 switch (a
->gesture_params()->GetGestureType()) {
54 case SyntheticGestureParams::SMOOTH_SCROLL_GESTURE
:
55 Compare(SyntheticSmoothScrollGestureParams::Cast(a
->gesture_params()),
56 SyntheticSmoothScrollGestureParams::Cast(b
->gesture_params()));
61 static void Verify(const InputEvents
& events_in
) {
63 IPC::ParamTraits
<InputEvents
>::Write(&msg
, events_in
);
65 InputEvents events_out
;
66 PickleIterator
iter(msg
);
67 EXPECT_TRUE(IPC::ParamTraits
<InputEvents
>::Read(&msg
, &iter
, &events_out
));
69 Compare(&events_in
, &events_out
);
71 // Perform a sanity check that logging doesn't explode.
72 std::string events_in_string
;
73 IPC::ParamTraits
<InputEvents
>::Log(events_in
, &events_in_string
);
74 std::string events_out_string
;
75 IPC::ParamTraits
<InputEvents
>::Log(events_out
, &events_out_string
);
76 ASSERT_FALSE(events_in_string
.empty());
77 EXPECT_EQ(events_in_string
, events_out_string
);
80 static void Verify(const SyntheticGesturePacket
& packet_in
) {
82 IPC::ParamTraits
<SyntheticGesturePacket
>::Write(&msg
, packet_in
);
84 SyntheticGesturePacket packet_out
;
85 PickleIterator
iter(msg
);
86 EXPECT_TRUE(IPC::ParamTraits
<SyntheticGesturePacket
>::Read(&msg
, &iter
,
89 Compare(&packet_in
, &packet_out
);
91 // Perform a sanity check that logging doesn't explode.
92 std::string packet_in_string
;
93 IPC::ParamTraits
<SyntheticGesturePacket
>::Log(packet_in
, &packet_in_string
);
94 std::string packet_out_string
;
95 IPC::ParamTraits
<SyntheticGesturePacket
>::Log(packet_out
,
97 ASSERT_FALSE(packet_in_string
.empty());
98 EXPECT_EQ(packet_in_string
, packet_out_string
);
102 TEST_F(InputParamTraitsTest
, UninitializedEvents
) {
106 IPC::WriteParam(&msg
, event
);
108 InputEvent event_out
;
109 PickleIterator
iter(msg
);
110 EXPECT_FALSE(IPC::ReadParam(&msg
, &iter
, &event_out
));
113 TEST_F(InputParamTraitsTest
, InitializedEvents
) {
116 ui::LatencyInfo latency
;
118 blink::WebKeyboardEvent key_event
;
119 key_event
.type
= blink::WebInputEvent::RawKeyDown
;
120 key_event
.nativeKeyCode
= 5;
121 events
.push_back(new InputEvent(key_event
, latency
, false));
123 blink::WebMouseWheelEvent wheel_event
;
124 wheel_event
.type
= blink::WebInputEvent::MouseWheel
;
125 wheel_event
.deltaX
= 10;
126 latency
.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
, 1, 1);
127 events
.push_back(new InputEvent(wheel_event
, latency
, false));
129 blink::WebMouseEvent mouse_event
;
130 mouse_event
.type
= blink::WebInputEvent::MouseDown
;
132 latency
.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_UI_COMPONENT
, 2, 2);
133 events
.push_back(new InputEvent(mouse_event
, latency
, false));
135 blink::WebGestureEvent gesture_event
;
136 gesture_event
.type
= blink::WebInputEvent::GestureScrollBegin
;
137 gesture_event
.x
= -1;
138 events
.push_back(new InputEvent(gesture_event
, latency
, false));
140 blink::WebTouchEvent touch_event
;
141 touch_event
.type
= blink::WebInputEvent::TouchStart
;
142 touch_event
.touchesLength
= 1;
143 touch_event
.touches
[0].radiusX
= 1;
144 events
.push_back(new InputEvent(touch_event
, latency
, false));
149 TEST_F(InputParamTraitsTest
, InvalidSyntheticGestureParams
) {
151 // Write invalid value for SyntheticGestureParams::GestureType.
152 WriteParam(&msg
, -3);
154 SyntheticGesturePacket packet_out
;
155 PickleIterator
iter(msg
);
157 IPC::ParamTraits
<SyntheticGesturePacket
>::Read(&msg
, &iter
, &packet_out
));
160 TEST_F(InputParamTraitsTest
, SyntheticSmoothScrollGestureParams
) {
161 scoped_ptr
<SyntheticSmoothScrollGestureParams
> gesture_params(
162 new SyntheticSmoothScrollGestureParams
);
163 gesture_params
->gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
164 gesture_params
->distance
= 123;
165 gesture_params
->anchor_x
= 234;
166 gesture_params
->anchor_y
= 345;
167 ASSERT_EQ(SyntheticGestureParams::SMOOTH_SCROLL_GESTURE
,
168 gesture_params
->GetGestureType());
169 SyntheticGesturePacket packet_in
;
170 packet_in
.set_gesture_params(gesture_params
.PassAs
<SyntheticGestureParams
>());
176 } // namespace content