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/synthetic_gesture_params.h"
9 #include "content/common/input/synthetic_pinch_gesture_params.h"
10 #include "content/common/input/synthetic_smooth_drag_gesture_params.h"
11 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h"
12 #include "content/common/input_messages.h"
13 #include "ipc/ipc_message.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/web/WebInputEvent.h"
20 typedef ScopedVector
<InputEvent
> InputEvents
;
22 class InputParamTraitsTest
: public testing::Test
{
24 static void Compare(const InputEvent
* a
, const InputEvent
* b
) {
25 EXPECT_EQ(!!a
->web_event
, !!b
->web_event
);
26 if (a
->web_event
&& b
->web_event
) {
27 const size_t a_size
= a
->web_event
->size
;
28 ASSERT_EQ(a_size
, b
->web_event
->size
);
29 EXPECT_EQ(0, memcmp(a
->web_event
.get(), b
->web_event
.get(), a_size
));
31 EXPECT_EQ(a
->latency_info
.latency_components
.size(),
32 b
->latency_info
.latency_components
.size());
33 EXPECT_EQ(a
->is_keyboard_shortcut
, b
->is_keyboard_shortcut
);
36 static void Compare(const InputEvents
* a
, const InputEvents
* b
) {
37 for (size_t i
= 0; i
< a
->size(); ++i
)
38 Compare((*a
)[i
], (*b
)[i
]);
41 static void Compare(const SyntheticSmoothScrollGestureParams
* a
,
42 const SyntheticSmoothScrollGestureParams
* b
) {
43 EXPECT_EQ(a
->gesture_source_type
, b
->gesture_source_type
);
44 EXPECT_EQ(a
->anchor
, b
->anchor
);
45 EXPECT_EQ(a
->distances
.size(), b
->distances
.size());
46 for (size_t i
= 0; i
< a
->distances
.size(); i
++)
47 EXPECT_EQ(a
->distances
[i
], b
->distances
[i
]);
48 EXPECT_EQ(a
->prevent_fling
, b
->prevent_fling
);
49 EXPECT_EQ(a
->speed_in_pixels_s
, b
->speed_in_pixels_s
);
52 static void Compare(const SyntheticSmoothDragGestureParams
* a
,
53 const SyntheticSmoothDragGestureParams
* b
) {
54 EXPECT_EQ(a
->gesture_source_type
, b
->gesture_source_type
);
55 EXPECT_EQ(a
->start_point
, b
->start_point
);
56 EXPECT_EQ(a
->distances
.size(), b
->distances
.size());
57 for (size_t i
= 0; i
< a
->distances
.size(); i
++)
58 EXPECT_EQ(a
->distances
[i
], b
->distances
[i
]);
59 EXPECT_EQ(a
->speed_in_pixels_s
, b
->speed_in_pixels_s
);
62 static void Compare(const SyntheticPinchGestureParams
* a
,
63 const SyntheticPinchGestureParams
* b
) {
64 EXPECT_EQ(a
->gesture_source_type
, b
->gesture_source_type
);
65 EXPECT_EQ(a
->scale_factor
, b
->scale_factor
);
66 EXPECT_EQ(a
->anchor
, b
->anchor
);
67 EXPECT_EQ(a
->relative_pointer_speed_in_pixels_s
,
68 b
->relative_pointer_speed_in_pixels_s
);
71 static void Compare(const SyntheticTapGestureParams
* a
,
72 const SyntheticTapGestureParams
* b
) {
73 EXPECT_EQ(a
->gesture_source_type
, b
->gesture_source_type
);
74 EXPECT_EQ(a
->position
, b
->position
);
75 EXPECT_EQ(a
->duration_ms
, b
->duration_ms
);
78 static void Compare(const SyntheticGesturePacket
* a
,
79 const SyntheticGesturePacket
* b
) {
82 ASSERT_EQ(!!a
->gesture_params(), !!b
->gesture_params());
83 if (!a
->gesture_params()) return;
84 ASSERT_EQ(a
->gesture_params()->GetGestureType(),
85 b
->gesture_params()->GetGestureType());
86 switch (a
->gesture_params()->GetGestureType()) {
87 case SyntheticGestureParams::SMOOTH_SCROLL_GESTURE
:
88 Compare(SyntheticSmoothScrollGestureParams::Cast(a
->gesture_params()),
89 SyntheticSmoothScrollGestureParams::Cast(b
->gesture_params()));
91 case SyntheticGestureParams::SMOOTH_DRAG_GESTURE
:
92 Compare(SyntheticSmoothDragGestureParams::Cast(a
->gesture_params()),
93 SyntheticSmoothDragGestureParams::Cast(b
->gesture_params()));
95 case SyntheticGestureParams::PINCH_GESTURE
:
96 Compare(SyntheticPinchGestureParams::Cast(a
->gesture_params()),
97 SyntheticPinchGestureParams::Cast(b
->gesture_params()));
99 case SyntheticGestureParams::TAP_GESTURE
:
100 Compare(SyntheticTapGestureParams::Cast(a
->gesture_params()),
101 SyntheticTapGestureParams::Cast(b
->gesture_params()));
106 static void Verify(const InputEvents
& events_in
) {
108 IPC::ParamTraits
<InputEvents
>::Write(&msg
, events_in
);
110 InputEvents events_out
;
111 base::PickleIterator
iter(msg
);
112 EXPECT_TRUE(IPC::ParamTraits
<InputEvents
>::Read(&msg
, &iter
, &events_out
));
114 Compare(&events_in
, &events_out
);
116 // Perform a sanity check that logging doesn't explode.
117 std::string events_in_string
;
118 IPC::ParamTraits
<InputEvents
>::Log(events_in
, &events_in_string
);
119 std::string events_out_string
;
120 IPC::ParamTraits
<InputEvents
>::Log(events_out
, &events_out_string
);
121 ASSERT_FALSE(events_in_string
.empty());
122 EXPECT_EQ(events_in_string
, events_out_string
);
125 static void Verify(const SyntheticGesturePacket
& packet_in
) {
127 IPC::ParamTraits
<SyntheticGesturePacket
>::Write(&msg
, packet_in
);
129 SyntheticGesturePacket packet_out
;
130 base::PickleIterator
iter(msg
);
131 EXPECT_TRUE(IPC::ParamTraits
<SyntheticGesturePacket
>::Read(&msg
, &iter
,
134 Compare(&packet_in
, &packet_out
);
136 // Perform a sanity check that logging doesn't explode.
137 std::string packet_in_string
;
138 IPC::ParamTraits
<SyntheticGesturePacket
>::Log(packet_in
, &packet_in_string
);
139 std::string packet_out_string
;
140 IPC::ParamTraits
<SyntheticGesturePacket
>::Log(packet_out
,
142 ASSERT_FALSE(packet_in_string
.empty());
143 EXPECT_EQ(packet_in_string
, packet_out_string
);
147 TEST_F(InputParamTraitsTest
, UninitializedEvents
) {
151 IPC::WriteParam(&msg
, event
);
153 InputEvent event_out
;
154 base::PickleIterator
iter(msg
);
155 EXPECT_FALSE(IPC::ReadParam(&msg
, &iter
, &event_out
));
158 TEST_F(InputParamTraitsTest
, InitializedEvents
) {
161 ui::LatencyInfo latency
;
163 blink::WebKeyboardEvent key_event
;
164 key_event
.type
= blink::WebInputEvent::RawKeyDown
;
165 key_event
.nativeKeyCode
= 5;
166 events
.push_back(new InputEvent(key_event
, latency
, false));
168 blink::WebMouseWheelEvent wheel_event
;
169 wheel_event
.type
= blink::WebInputEvent::MouseWheel
;
170 wheel_event
.deltaX
= 10;
171 latency
.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
, 1, 1);
172 events
.push_back(new InputEvent(wheel_event
, latency
, false));
174 blink::WebMouseEvent mouse_event
;
175 mouse_event
.type
= blink::WebInputEvent::MouseDown
;
177 latency
.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_UI_COMPONENT
, 2, 2);
178 events
.push_back(new InputEvent(mouse_event
, latency
, false));
180 blink::WebGestureEvent gesture_event
;
181 gesture_event
.type
= blink::WebInputEvent::GestureScrollBegin
;
182 gesture_event
.x
= -1;
183 events
.push_back(new InputEvent(gesture_event
, latency
, false));
185 blink::WebTouchEvent touch_event
;
186 touch_event
.type
= blink::WebInputEvent::TouchStart
;
187 touch_event
.touchesLength
= 1;
188 touch_event
.touches
[0].radiusX
= 1;
189 events
.push_back(new InputEvent(touch_event
, latency
, false));
194 TEST_F(InputParamTraitsTest
, InvalidSyntheticGestureParams
) {
196 // Write invalid value for SyntheticGestureParams::GestureType.
197 WriteParam(&msg
, -3);
199 SyntheticGesturePacket packet_out
;
200 base::PickleIterator
iter(msg
);
202 IPC::ParamTraits
<SyntheticGesturePacket
>::Read(&msg
, &iter
, &packet_out
));
205 TEST_F(InputParamTraitsTest
, SyntheticSmoothScrollGestureParams
) {
206 scoped_ptr
<SyntheticSmoothScrollGestureParams
> gesture_params(
207 new SyntheticSmoothScrollGestureParams
);
208 gesture_params
->gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
209 gesture_params
->anchor
.SetPoint(234, 345);
210 gesture_params
->distances
.push_back(gfx::Vector2d(123, -789));
211 gesture_params
->distances
.push_back(gfx::Vector2d(-78, 43));
212 gesture_params
->prevent_fling
= false;
213 gesture_params
->speed_in_pixels_s
= 456;
214 ASSERT_EQ(SyntheticGestureParams::SMOOTH_SCROLL_GESTURE
,
215 gesture_params
->GetGestureType());
216 SyntheticGesturePacket packet_in
;
217 packet_in
.set_gesture_params(gesture_params
.Pass());
222 TEST_F(InputParamTraitsTest
, SyntheticPinchGestureParams
) {
223 scoped_ptr
<SyntheticPinchGestureParams
> gesture_params(
224 new SyntheticPinchGestureParams
);
225 gesture_params
->gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
226 gesture_params
->scale_factor
= 2.3f
;
227 gesture_params
->anchor
.SetPoint(234, 345);
228 gesture_params
->relative_pointer_speed_in_pixels_s
= 456;
229 ASSERT_EQ(SyntheticGestureParams::PINCH_GESTURE
,
230 gesture_params
->GetGestureType());
231 SyntheticGesturePacket packet_in
;
232 packet_in
.set_gesture_params(gesture_params
.Pass());
237 TEST_F(InputParamTraitsTest
, SyntheticTapGestureParams
) {
238 scoped_ptr
<SyntheticTapGestureParams
> gesture_params(
239 new SyntheticTapGestureParams
);
240 gesture_params
->gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
241 gesture_params
->position
.SetPoint(798, 233);
242 gesture_params
->duration_ms
= 13;
243 ASSERT_EQ(SyntheticGestureParams::TAP_GESTURE
,
244 gesture_params
->GetGestureType());
245 SyntheticGesturePacket packet_in
;
246 packet_in
.set_gesture_params(gesture_params
.Pass());
252 } // namespace content