OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/common/input/web_input_event_traits.h" | |
6 | |
7 #include <limits> | |
8 | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
11 | |
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; | |
20 | |
21 namespace content { | |
22 namespace { | |
23 | |
24 class WebInputEventTraitsTest : public testing::Test { | |
25 protected: | |
26 static WebTouchPoint CreateTouchPoint(WebTouchPoint::State state, int id) { | |
27 WebTouchPoint touch; | |
28 touch.state = state; | |
29 touch.id = id; | |
30 return touch; | |
31 } | |
32 | |
33 static WebTouchEvent CreateTouch(WebInputEvent::Type type) { | |
34 return CreateTouch(type, 1); | |
35 } | |
36 | |
37 static WebTouchEvent CreateTouch(WebInputEvent::Type type, | |
38 unsigned touch_count) { | |
39 WebTouchEvent event; | |
40 event.touchesLength = touch_count; | |
41 event.type = type; | |
42 return event; | |
43 } | |
44 | |
45 static WebGestureEvent CreateGesture(WebInputEvent::Type type, | |
46 float x, | |
47 float y) { | |
48 WebGestureEvent event; | |
49 event.type = type; | |
50 event.x = x; | |
51 event.y = y; | |
52 return event; | |
53 } | |
54 | |
55 static WebMouseWheelEvent CreateMouseWheel(float deltaX, | |
56 float deltaY, | |
57 bool canScroll) { | |
58 WebMouseWheelEvent event; | |
59 event.type = WebInputEvent::MouseWheel; | |
60 event.deltaX = deltaX; | |
61 event.deltaY = deltaY; | |
62 event.canScroll = canScroll; | |
63 return event; | |
64 } | |
65 }; | |
66 | |
67 TEST_F(WebInputEventTraitsTest, TouchEventCoalescing) { | |
68 WebTouchEvent touch0 = CreateTouch(WebInputEvent::TouchStart); | |
69 WebTouchEvent touch1 = CreateTouch(WebInputEvent::TouchMove); | |
70 | |
71 // Non touch-moves won't coalesce. | |
72 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch0)); | |
73 | |
74 // Touches of different types won't coalesce. | |
75 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1)); | |
76 | |
77 // Touch moves with idential touch lengths and touch ids should coalesce. | |
78 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch1, touch1)); | |
79 | |
80 // Touch moves with different touch ids should not coalesce. | |
81 touch0 = CreateTouch(WebInputEvent::TouchMove); | |
82 touch1 = CreateTouch(WebInputEvent::TouchMove); | |
83 touch0.touches[0].id = 7; | |
84 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1)); | |
85 touch0 = CreateTouch(WebInputEvent::TouchMove, 2); | |
86 touch1 = CreateTouch(WebInputEvent::TouchMove, 2); | |
87 touch0.touches[0].id = 1; | |
88 touch1.touches[0].id = 0; | |
89 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1)); | |
90 | |
91 // Touch moves with different touch lengths should not coalesce. | |
92 touch0 = CreateTouch(WebInputEvent::TouchMove, 1); | |
93 touch1 = CreateTouch(WebInputEvent::TouchMove, 2); | |
94 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1)); | |
95 | |
96 // Touch moves with identical touch ids in different orders should coalesce. | |
97 touch0 = CreateTouch(WebInputEvent::TouchMove, 2); | |
98 touch1 = CreateTouch(WebInputEvent::TouchMove, 2); | |
99 touch0.touches[0] = touch1.touches[1] = | |
100 CreateTouchPoint(WebTouchPoint::StateMoved, 1); | |
101 touch0.touches[1] = touch1.touches[0] = | |
102 CreateTouchPoint(WebTouchPoint::StateMoved, 0); | |
103 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch0, touch1)); | |
104 | |
105 // Pointers with the same ID's should coalesce. | |
106 touch0 = CreateTouch(WebInputEvent::TouchMove, 2); | |
107 touch1 = CreateTouch(WebInputEvent::TouchMove, 2); | |
108 touch0.touches[0] = touch1.touches[1] = | |
109 CreateTouchPoint(WebTouchPoint::StateMoved, 1); | |
110 WebInputEventTraits::Coalesce(touch0, &touch1); | |
111 ASSERT_EQ(1, touch1.touches[0].id); | |
112 ASSERT_EQ(0, touch1.touches[1].id); | |
113 EXPECT_EQ(WebTouchPoint::StateUndefined, touch1.touches[1].state); | |
114 EXPECT_EQ(WebTouchPoint::StateMoved, touch1.touches[0].state); | |
115 | |
116 // Movement from now-stationary pointers should be preserved. | |
117 touch0 = touch1 = CreateTouch(WebInputEvent::TouchMove, 2); | |
118 touch0.touches[0] = CreateTouchPoint(WebTouchPoint::StateMoved, 1); | |
119 touch1.touches[1] = CreateTouchPoint(WebTouchPoint::StateStationary, 1); | |
120 touch0.touches[1] = CreateTouchPoint(WebTouchPoint::StateStationary, 0); | |
121 touch1.touches[0] = CreateTouchPoint(WebTouchPoint::StateMoved, 0); | |
122 WebInputEventTraits::Coalesce(touch0, &touch1); | |
123 ASSERT_EQ(1, touch1.touches[0].id); | |
124 ASSERT_EQ(0, touch1.touches[1].id); | |
125 EXPECT_EQ(WebTouchPoint::StateMoved, touch1.touches[0].state); | |
126 EXPECT_EQ(WebTouchPoint::StateMoved, touch1.touches[1].state); | |
127 } | |
128 | |
129 TEST_F(WebInputEventTraitsTest, PinchEventCoalescing) { | |
130 WebGestureEvent pinch0 = | |
131 CreateGesture(WebInputEvent::GesturePinchBegin, 1, 1); | |
132 WebGestureEvent pinch1 = | |
133 CreateGesture(WebInputEvent::GesturePinchUpdate, 2, 2); | |
134 | |
135 // Only GesturePinchUpdate's coalesce. | |
136 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch0)); | |
137 | |
138 // Pinch gestures of different types should not coalesce. | |
139 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch1)); | |
140 | |
141 // Pinches with different focal points should not coalesce. | |
142 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1); | |
143 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 2, 2); | |
144 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch1)); | |
145 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch0)); | |
146 | |
147 // Coalesced scales are multiplicative. | |
148 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1); | |
149 pinch0.data.pinchUpdate.scale = 2.f; | |
150 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1); | |
151 pinch1.data.pinchUpdate.scale = 3.f; | |
152 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch0)); | |
153 WebInputEventTraits::Coalesce(pinch0, &pinch1); | |
154 EXPECT_EQ(2.f * 3.f, pinch1.data.pinchUpdate.scale); | |
155 | |
156 // Scales have a minimum value and can never reach 0. | |
157 ASSERT_GT(numeric_limits<float>::min(), 0); | |
158 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1); | |
159 pinch0.data.pinchUpdate.scale = numeric_limits<float>::min() * 2.0f; | |
160 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1); | |
161 pinch1.data.pinchUpdate.scale = numeric_limits<float>::min() * 5.0f; | |
162 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch1)); | |
163 WebInputEventTraits::Coalesce(pinch0, &pinch1); | |
164 EXPECT_EQ(numeric_limits<float>::min(), pinch1.data.pinchUpdate.scale); | |
165 | |
166 // Scales have a maximum value and can never reach Infinity. | |
167 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1); | |
168 pinch0.data.pinchUpdate.scale = numeric_limits<float>::max() / 2.0f; | |
169 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1); | |
170 pinch1.data.pinchUpdate.scale = 10.0f; | |
171 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch1)); | |
172 WebInputEventTraits::Coalesce(pinch0, &pinch1); | |
173 EXPECT_EQ(numeric_limits<float>::max(), pinch1.data.pinchUpdate.scale); | |
174 } | |
175 | |
176 TEST_F(WebInputEventTraitsTest, WebMouseWheelEventCoalescing) { | |
177 WebMouseWheelEvent mouse_wheel_0 = | |
178 CreateMouseWheel(1, 1, true); | |
179 WebMouseWheelEvent mouse_wheel_1 = | |
180 CreateMouseWheel(2, 2, true); | |
181 | |
182 // WebMouseWheelEvent objects with same values except different deltaX and | |
183 // deltaY should coalesce. | |
184 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(mouse_wheel_0, mouse_wheel_1)); | |
185 | |
186 mouse_wheel_0 = CreateMouseWheel(1, 1, true); | |
187 mouse_wheel_1 = CreateMouseWheel(1, 1, false); | |
188 | |
189 // WebMouseWheelEvent objects with different canScroll values should not | |
190 // coalesce. | |
191 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(mouse_wheel_0, mouse_wheel_1)); | |
192 | |
193 // WebMouseWheelEvent objects with different modifiers should not coalesce. | |
194 mouse_wheel_0 = CreateMouseWheel(1, 1, true); | |
195 mouse_wheel_1 = CreateMouseWheel(1, 1, true); | |
196 mouse_wheel_0.modifiers = blink::WebInputEvent::ControlKey; | |
197 mouse_wheel_1.modifiers = blink::WebInputEvent::ShiftKey; | |
198 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(mouse_wheel_0, mouse_wheel_1)); | |
199 } | |
200 | |
201 // Coalescing preserves the newer timestamp. | |
202 TEST_F(WebInputEventTraitsTest, TimestampCoalescing) { | |
203 WebMouseWheelEvent mouse_wheel_0 = CreateMouseWheel(1, 1, true); | |
204 mouse_wheel_0.timeStampSeconds = 5.0; | |
205 WebMouseWheelEvent mouse_wheel_1 = CreateMouseWheel(2, 2, true); | |
206 mouse_wheel_1.timeStampSeconds = 10.0; | |
207 | |
208 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(mouse_wheel_0, mouse_wheel_1)); | |
209 WebInputEventTraits::Coalesce(mouse_wheel_1, &mouse_wheel_0); | |
210 EXPECT_EQ(10.0, mouse_wheel_0.timeStampSeconds); | |
211 } | |
212 | |
213 // Very basic smoke test to ensure stringification doesn't explode. | |
214 TEST_F(WebInputEventTraitsTest, ToString) { | |
215 WebKeyboardEvent key; | |
216 key.type = WebInputEvent::RawKeyDown; | |
217 EXPECT_FALSE(WebInputEventTraits::ToString(key).empty()); | |
218 | |
219 WebMouseEvent mouse; | |
220 mouse.type = WebInputEvent::MouseMove; | |
221 EXPECT_FALSE(WebInputEventTraits::ToString(mouse).empty()); | |
222 | |
223 WebMouseWheelEvent mouse_wheel; | |
224 mouse_wheel.type = WebInputEvent::MouseWheel; | |
225 EXPECT_FALSE(WebInputEventTraits::ToString(mouse_wheel).empty()); | |
226 | |
227 WebGestureEvent gesture = | |
228 CreateGesture(WebInputEvent::GesturePinchBegin, 1, 1); | |
229 EXPECT_FALSE(WebInputEventTraits::ToString(gesture).empty()); | |
230 | |
231 WebTouchEvent touch = CreateTouch(WebInputEvent::TouchStart); | |
232 EXPECT_FALSE(WebInputEventTraits::ToString(touch).empty()); | |
233 } | |
234 | |
235 } // namespace | |
236 } // namespace content | |
OLD | NEW |