1 // Copyright 2014 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/web_input_event_traits.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/public/web/WebInputEvent.h"
12 using blink::WebGestureEvent
;
13 using blink::WebInputEvent
;
14 using blink::WebKeyboardEvent
;
15 using blink::WebMouseEvent
;
16 using blink::WebMouseWheelEvent
;
17 using blink::WebTouchEvent
;
18 using blink::WebTouchPoint
;
19 using std::numeric_limits
;
24 class WebInputEventTraitsTest
: public testing::Test
{
26 static WebTouchPoint
CreateTouchPoint(WebTouchPoint::State state
, int id
) {
33 static WebTouchEvent
CreateTouch(WebInputEvent::Type type
) {
34 return CreateTouch(type
, 1);
37 static WebTouchEvent
CreateTouch(WebInputEvent::Type type
,
38 unsigned touch_count
) {
40 event
.touchesLength
= touch_count
;
45 static WebGestureEvent
CreateGesture(WebInputEvent::Type type
,
48 WebGestureEvent event
;
56 TEST_F(WebInputEventTraitsTest
, TouchEventCoalescing
) {
57 WebTouchEvent touch0
= CreateTouch(WebInputEvent::TouchStart
);
58 WebTouchEvent touch1
= CreateTouch(WebInputEvent::TouchMove
);
60 // Non touch-moves won't coalesce.
61 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch0
));
63 // Touches of different types won't coalesce.
64 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
66 // Touch moves with idential touch lengths and touch ids should coalesce.
67 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch1
, touch1
));
69 // Touch moves with different touch ids should not coalesce.
70 touch0
= CreateTouch(WebInputEvent::TouchMove
);
71 touch1
= CreateTouch(WebInputEvent::TouchMove
);
72 touch0
.touches
[0].id
= 7;
73 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
74 touch0
= CreateTouch(WebInputEvent::TouchMove
, 2);
75 touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
76 touch0
.touches
[0].id
= 1;
77 touch1
.touches
[0].id
= 0;
78 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
80 // Touch moves with different touch lengths should not coalesce.
81 touch0
= CreateTouch(WebInputEvent::TouchMove
, 1);
82 touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
83 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
85 // Touch moves with identical touch ids in different orders should coalesce.
86 touch0
= CreateTouch(WebInputEvent::TouchMove
, 2);
87 touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
88 touch0
.touches
[0] = touch1
.touches
[1] =
89 CreateTouchPoint(WebTouchPoint::StateMoved
, 1);
90 touch0
.touches
[1] = touch1
.touches
[0] =
91 CreateTouchPoint(WebTouchPoint::StateMoved
, 0);
92 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
94 // Pointers with the same ID's should coalesce.
95 touch0
= CreateTouch(WebInputEvent::TouchMove
, 2);
96 touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
97 touch0
.touches
[0] = touch1
.touches
[1] =
98 CreateTouchPoint(WebTouchPoint::StateMoved
, 1);
99 WebInputEventTraits::Coalesce(touch0
, &touch1
);
100 ASSERT_EQ(1, touch1
.touches
[0].id
);
101 ASSERT_EQ(0, touch1
.touches
[1].id
);
102 EXPECT_EQ(WebTouchPoint::StateUndefined
, touch1
.touches
[1].state
);
103 EXPECT_EQ(WebTouchPoint::StateMoved
, touch1
.touches
[0].state
);
105 // Movement from now-stationary pointers should be preserved.
106 touch0
= touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
107 touch0
.touches
[0] = CreateTouchPoint(WebTouchPoint::StateMoved
, 1);
108 touch1
.touches
[1] = CreateTouchPoint(WebTouchPoint::StateStationary
, 1);
109 touch0
.touches
[1] = CreateTouchPoint(WebTouchPoint::StateStationary
, 0);
110 touch1
.touches
[0] = CreateTouchPoint(WebTouchPoint::StateMoved
, 0);
111 WebInputEventTraits::Coalesce(touch0
, &touch1
);
112 ASSERT_EQ(1, touch1
.touches
[0].id
);
113 ASSERT_EQ(0, touch1
.touches
[1].id
);
114 EXPECT_EQ(WebTouchPoint::StateMoved
, touch1
.touches
[0].state
);
115 EXPECT_EQ(WebTouchPoint::StateMoved
, touch1
.touches
[1].state
);
118 TEST_F(WebInputEventTraitsTest
, PinchEventCoalescing
) {
119 WebGestureEvent pinch0
=
120 CreateGesture(WebInputEvent::GesturePinchBegin
, 1, 1);
121 WebGestureEvent pinch1
=
122 CreateGesture(WebInputEvent::GesturePinchUpdate
, 2, 2);
124 // Only GesturePinchUpdate's coalesce.
125 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0
, pinch0
));
127 // Pinch gestures of different types should not coalesce.
128 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0
, pinch1
));
130 // Pinches with different focal points should not coalesce.
131 pinch0
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
132 pinch1
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 2, 2);
133 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0
, pinch1
));
134 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0
, pinch0
));
136 // Coalesced scales are multiplicative.
137 pinch0
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
138 pinch0
.data
.pinchUpdate
.scale
= 2.f
;
139 pinch1
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
140 pinch1
.data
.pinchUpdate
.scale
= 3.f
;
141 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0
, pinch0
));
142 WebInputEventTraits::Coalesce(pinch0
, &pinch1
);
143 EXPECT_EQ(2.f
* 3.f
, pinch1
.data
.pinchUpdate
.scale
);
145 // Scales have a minimum value and can never reach 0.
146 ASSERT_GT(numeric_limits
<float>::min(), 0);
147 pinch0
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
148 pinch0
.data
.pinchUpdate
.scale
= numeric_limits
<float>::min() * 2.0f
;
149 pinch1
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
150 pinch1
.data
.pinchUpdate
.scale
= numeric_limits
<float>::min() * 5.0f
;
151 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0
, pinch1
));
152 WebInputEventTraits::Coalesce(pinch0
, &pinch1
);
153 EXPECT_EQ(numeric_limits
<float>::min(), pinch1
.data
.pinchUpdate
.scale
);
155 // Scales have a maximum value and can never reach Infinity.
156 pinch0
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
157 pinch0
.data
.pinchUpdate
.scale
= numeric_limits
<float>::max() / 2.0f
;
158 pinch1
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
159 pinch1
.data
.pinchUpdate
.scale
= 10.0f
;
160 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0
, pinch1
));
161 WebInputEventTraits::Coalesce(pinch0
, &pinch1
);
162 EXPECT_EQ(numeric_limits
<float>::max(), pinch1
.data
.pinchUpdate
.scale
);
165 // Very basic smoke test to ensure stringification doesn't explode.
166 TEST_F(WebInputEventTraitsTest
, ToString
) {
167 WebKeyboardEvent key
;
168 key
.type
= WebInputEvent::RawKeyDown
;
169 EXPECT_FALSE(WebInputEventTraits::ToString(key
).empty());
172 mouse
.type
= WebInputEvent::MouseMove
;
173 EXPECT_FALSE(WebInputEventTraits::ToString(mouse
).empty());
175 WebMouseWheelEvent mouse_wheel
;
176 mouse_wheel
.type
= WebInputEvent::MouseWheel
;
177 EXPECT_FALSE(WebInputEventTraits::ToString(mouse_wheel
).empty());
179 WebGestureEvent gesture
=
180 CreateGesture(WebInputEvent::GesturePinchBegin
, 1, 1);
181 EXPECT_FALSE(WebInputEventTraits::ToString(gesture
).empty());
183 WebTouchEvent touch
= CreateTouch(WebInputEvent::TouchStart
);
184 EXPECT_FALSE(WebInputEventTraits::ToString(touch
).empty());
188 } // namespace content