Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: content/common/input/web_input_event_traits_unittest.cc

Issue 2110363003: Consolidate WebInputEvent Coalescing unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit diff Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/input/web_input_event_traits.h" 5 #include "content/common/input/web_input_event_traits.h"
6 6
7 #include <limits>
8
9 #include "content/common/input/event_with_latency_info.h"
10 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/web/WebInputEvent.h" 8 #include "third_party/WebKit/public/web/WebInputEvent.h"
12 9
13 using blink::WebGestureEvent; 10 using blink::WebGestureEvent;
14 using blink::WebInputEvent; 11 using blink::WebInputEvent;
15 using blink::WebKeyboardEvent; 12 using blink::WebKeyboardEvent;
16 using blink::WebMouseEvent; 13 using blink::WebMouseEvent;
17 using blink::WebMouseWheelEvent; 14 using blink::WebMouseWheelEvent;
18 using blink::WebTouchEvent; 15 using blink::WebTouchEvent;
19 using blink::WebTouchPoint;
20 using std::numeric_limits;
21 16
22 namespace content { 17 namespace content {
23 namespace { 18 namespace {
24 19
25 // TODO(tapted): Move the Coalesce tests to event_with_latency_info_unittest.cc. 20 using WebInputEventTraitsTest = testing::Test;
26 // Merge this test harness into EventWithLatencyInfoTest.
27 // http://crbug.com/625000.
28 class WebInputEventTraitsTest : public testing::Test {
29 protected:
30 static WebTouchPoint CreateTouchPoint(WebTouchPoint::State state, int id) {
31 WebTouchPoint touch;
32 touch.state = state;
33 touch.id = id;
34 return touch;
35 }
36
37 static TouchEventWithLatencyInfo CreateTouch(WebInputEvent::Type type) {
38 return CreateTouch(type, 1);
39 }
40
41 static TouchEventWithLatencyInfo CreateTouch(WebInputEvent::Type type,
42 unsigned touch_count) {
43 WebTouchEvent event;
44 event.touchesLength = touch_count;
45 event.type = type;
46 return TouchEventWithLatencyInfo(event);
47 }
48
49 static GestureEventWithLatencyInfo CreateGesture(WebInputEvent::Type type,
50 float x,
51 float y) {
52 WebGestureEvent event;
53 event.type = type;
54 event.x = x;
55 event.y = y;
56 return GestureEventWithLatencyInfo(event);
57 }
58
59 static MouseWheelEventWithLatencyInfo CreateMouseWheel(float deltaX,
60 float deltaY) {
61 WebMouseWheelEvent event;
62 event.type = WebInputEvent::MouseWheel;
63 event.deltaX = deltaX;
64 event.deltaY = deltaY;
65 return MouseWheelEventWithLatencyInfo(event);
66 }
67 };
68
69 template <class T>
70 bool CanCoalesce(const T& event_to_coalesce, const T& event) {
71 return event.CanCoalesceWith(event_to_coalesce);
72 }
73
74 template <class T>
75 void Coalesce(const T& event_to_coalesce, T* event) {
76 return event->CoalesceWith(event_to_coalesce);
77 }
78
79 TEST_F(WebInputEventTraitsTest, TouchEventCoalescing) {
80 TouchEventWithLatencyInfo touch0 = CreateTouch(WebInputEvent::TouchStart);
81 TouchEventWithLatencyInfo touch1 = CreateTouch(WebInputEvent::TouchMove);
82
83 // Non touch-moves won't coalesce.
84 EXPECT_FALSE(CanCoalesce(touch0, touch0));
85
86 // Touches of different types won't coalesce.
87 EXPECT_FALSE(CanCoalesce(touch0, touch1));
88
89 // Touch moves with idential touch lengths and touch ids should coalesce.
90 EXPECT_TRUE(CanCoalesce(touch1, touch1));
91
92 // Touch moves with different touch ids should not coalesce.
93 touch0 = CreateTouch(WebInputEvent::TouchMove);
94 touch1 = CreateTouch(WebInputEvent::TouchMove);
95 touch0.event.touches[0].id = 7;
96 EXPECT_FALSE(CanCoalesce(touch0, touch1));
97 touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
98 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
99 touch0.event.touches[0].id = 1;
100 touch1.event.touches[0].id = 0;
101 EXPECT_FALSE(CanCoalesce(touch0, touch1));
102
103 // Touch moves with different touch lengths should not coalesce.
104 touch0 = CreateTouch(WebInputEvent::TouchMove, 1);
105 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
106 EXPECT_FALSE(CanCoalesce(touch0, touch1));
107
108 // Touch moves with identical touch ids in different orders should coalesce.
109 touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
110 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
111 touch0.event.touches[0] = touch1.event.touches[1] =
112 CreateTouchPoint(WebTouchPoint::StateMoved, 1);
113 touch0.event.touches[1] = touch1.event.touches[0] =
114 CreateTouchPoint(WebTouchPoint::StateMoved, 0);
115 EXPECT_TRUE(CanCoalesce(touch0, touch1));
116
117 // Pointers with the same ID's should coalesce.
118 touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
119 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
120 touch0.event.touches[0] = touch1.event.touches[1] =
121 CreateTouchPoint(WebTouchPoint::StateMoved, 1);
122 Coalesce(touch0, &touch1);
123 ASSERT_EQ(1, touch1.event.touches[0].id);
124 ASSERT_EQ(0, touch1.event.touches[1].id);
125 EXPECT_EQ(WebTouchPoint::StateUndefined, touch1.event.touches[1].state);
126 EXPECT_EQ(WebTouchPoint::StateMoved, touch1.event.touches[0].state);
127
128 // Movement from now-stationary pointers should be preserved.
129 touch0 = touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
130 touch0.event.touches[0] = CreateTouchPoint(WebTouchPoint::StateMoved, 1);
131 touch1.event.touches[1] = CreateTouchPoint(WebTouchPoint::StateStationary, 1);
132 touch0.event.touches[1] = CreateTouchPoint(WebTouchPoint::StateStationary, 0);
133 touch1.event.touches[0] = CreateTouchPoint(WebTouchPoint::StateMoved, 0);
134 Coalesce(touch0, &touch1);
135 ASSERT_EQ(1, touch1.event.touches[0].id);
136 ASSERT_EQ(0, touch1.event.touches[1].id);
137 EXPECT_EQ(WebTouchPoint::StateMoved, touch1.event.touches[0].state);
138 EXPECT_EQ(WebTouchPoint::StateMoved, touch1.event.touches[1].state);
139
140 // Touch moves with different dispatchTypes coalesce.
141 touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
142 touch0.event.dispatchType = WebInputEvent::DispatchType::Blocking;
143 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
144 touch1.event.dispatchType = WebInputEvent::DispatchType::EventNonBlocking;
145 touch0.event.touches[0] = touch1.event.touches[1] =
146 CreateTouchPoint(WebTouchPoint::StateMoved, 1);
147 touch0.event.touches[1] = touch1.event.touches[0] =
148 CreateTouchPoint(WebTouchPoint::StateMoved, 0);
149 EXPECT_TRUE(CanCoalesce(touch0, touch1));
150 Coalesce(touch0, &touch1);
151 ASSERT_EQ(WebInputEvent::DispatchType::Blocking, touch1.event.dispatchType);
152
153 touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
154 touch0.event.dispatchType =
155 WebInputEvent::DispatchType::ListenersForcedNonBlockingPassive;
156 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
157 touch1.event.dispatchType =
158 WebInputEvent::DispatchType::ListenersNonBlockingPassive;
159 touch0.event.touches[0] = touch1.event.touches[1] =
160 CreateTouchPoint(WebTouchPoint::StateMoved, 1);
161 touch0.event.touches[1] = touch1.event.touches[0] =
162 CreateTouchPoint(WebTouchPoint::StateMoved, 0);
163 EXPECT_TRUE(CanCoalesce(touch0, touch1));
164 Coalesce(touch0, &touch1);
165 ASSERT_EQ(WebInputEvent::DispatchType::ListenersNonBlockingPassive,
166 touch1.event.dispatchType);
167 }
168
169 TEST_F(WebInputEventTraitsTest, PinchEventCoalescing) {
170 GestureEventWithLatencyInfo pinch0 =
171 CreateGesture(WebInputEvent::GesturePinchBegin, 1, 1);
172 GestureEventWithLatencyInfo pinch1 =
173 CreateGesture(WebInputEvent::GesturePinchUpdate, 2, 2);
174
175 // Only GesturePinchUpdate's coalesce.
176 EXPECT_FALSE(CanCoalesce(pinch0, pinch0));
177
178 // Pinch gestures of different types should not coalesce.
179 EXPECT_FALSE(CanCoalesce(pinch0, pinch1));
180
181 // Pinches with different focal points should not coalesce.
182 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
183 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 2, 2);
184 EXPECT_FALSE(CanCoalesce(pinch0, pinch1));
185 EXPECT_TRUE(CanCoalesce(pinch0, pinch0));
186
187 // Coalesced scales are multiplicative.
188 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
189 pinch0.event.data.pinchUpdate.scale = 2.f;
190 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
191 pinch1.event.data.pinchUpdate.scale = 3.f;
192 EXPECT_TRUE(CanCoalesce(pinch0, pinch0));
193 Coalesce(pinch0, &pinch1);
194 EXPECT_EQ(2.f * 3.f, pinch1.event.data.pinchUpdate.scale);
195
196 // Scales have a minimum value and can never reach 0.
197 ASSERT_GT(numeric_limits<float>::min(), 0);
198 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
199 pinch0.event.data.pinchUpdate.scale = numeric_limits<float>::min() * 2.0f;
200 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
201 pinch1.event.data.pinchUpdate.scale = numeric_limits<float>::min() * 5.0f;
202 EXPECT_TRUE(CanCoalesce(pinch0, pinch1));
203 Coalesce(pinch0, &pinch1);
204 EXPECT_EQ(numeric_limits<float>::min(), pinch1.event.data.pinchUpdate.scale);
205
206 // Scales have a maximum value and can never reach Infinity.
207 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
208 pinch0.event.data.pinchUpdate.scale = numeric_limits<float>::max() / 2.0f;
209 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
210 pinch1.event.data.pinchUpdate.scale = 10.0f;
211 EXPECT_TRUE(CanCoalesce(pinch0, pinch1));
212 Coalesce(pinch0, &pinch1);
213 EXPECT_EQ(numeric_limits<float>::max(), pinch1.event.data.pinchUpdate.scale);
214 }
215
216 TEST_F(WebInputEventTraitsTest, WebMouseWheelEventCoalescing) {
217 MouseWheelEventWithLatencyInfo mouse_wheel_0 = CreateMouseWheel(1, 1);
218 MouseWheelEventWithLatencyInfo mouse_wheel_1 = CreateMouseWheel(2, 2);
219
220 // WebMouseWheelEvent objects with same values except different deltaX and
221 // deltaY should coalesce.
222 EXPECT_TRUE(CanCoalesce(mouse_wheel_0, mouse_wheel_1));
223
224 // WebMouseWheelEvent objects with different modifiers should not coalesce.
225 mouse_wheel_0 = CreateMouseWheel(1, 1);
226 mouse_wheel_1 = CreateMouseWheel(1, 1);
227 mouse_wheel_0.event.modifiers = blink::WebInputEvent::ControlKey;
228 mouse_wheel_1.event.modifiers = blink::WebInputEvent::ShiftKey;
229 EXPECT_FALSE(CanCoalesce(mouse_wheel_0, mouse_wheel_1));
230 }
231
232 // Coalescing preserves the newer timestamp.
233 TEST_F(WebInputEventTraitsTest, TimestampCoalescing) {
234 MouseWheelEventWithLatencyInfo mouse_wheel_0 = CreateMouseWheel(1, 1);
235 mouse_wheel_0.event.timeStampSeconds = 5.0;
236 MouseWheelEventWithLatencyInfo mouse_wheel_1 = CreateMouseWheel(2, 2);
237 mouse_wheel_1.event.timeStampSeconds = 10.0;
238
239 EXPECT_TRUE(CanCoalesce(mouse_wheel_0, mouse_wheel_1));
240 Coalesce(mouse_wheel_1, &mouse_wheel_0);
241 EXPECT_EQ(10.0, mouse_wheel_0.event.timeStampSeconds);
242 }
243 21
244 // Very basic smoke test to ensure stringification doesn't explode. 22 // Very basic smoke test to ensure stringification doesn't explode.
245 TEST_F(WebInputEventTraitsTest, ToString) { 23 TEST_F(WebInputEventTraitsTest, ToString) {
246 WebKeyboardEvent key; 24 WebKeyboardEvent key;
247 key.type = WebInputEvent::RawKeyDown; 25 key.type = WebInputEvent::RawKeyDown;
248 EXPECT_FALSE(WebInputEventTraits::ToString(key).empty()); 26 EXPECT_FALSE(WebInputEventTraits::ToString(key).empty());
249 27
250 WebMouseEvent mouse; 28 WebMouseEvent mouse;
251 mouse.type = WebInputEvent::MouseMove; 29 mouse.type = WebInputEvent::MouseMove;
252 EXPECT_FALSE(WebInputEventTraits::ToString(mouse).empty()); 30 EXPECT_FALSE(WebInputEventTraits::ToString(mouse).empty());
253 31
254 WebMouseWheelEvent mouse_wheel; 32 WebMouseWheelEvent mouse_wheel;
255 mouse_wheel.type = WebInputEvent::MouseWheel; 33 mouse_wheel.type = WebInputEvent::MouseWheel;
256 EXPECT_FALSE(WebInputEventTraits::ToString(mouse_wheel).empty()); 34 EXPECT_FALSE(WebInputEventTraits::ToString(mouse_wheel).empty());
257 35
258 WebGestureEvent gesture; 36 WebGestureEvent gesture;
259 gesture.type = WebInputEvent::GesturePinchBegin; 37 gesture.type = WebInputEvent::GesturePinchBegin;
260 gesture.x = 1; 38 gesture.x = 1;
261 gesture.y = 1; 39 gesture.y = 1;
262 EXPECT_FALSE(WebInputEventTraits::ToString(gesture).empty()); 40 EXPECT_FALSE(WebInputEventTraits::ToString(gesture).empty());
263 41
264 WebTouchEvent touch; 42 WebTouchEvent touch;
265 touch.type = WebInputEvent::TouchStart; 43 touch.type = WebInputEvent::TouchStart;
266 touch.touchesLength = 1; 44 touch.touchesLength = 1;
267 EXPECT_FALSE(WebInputEventTraits::ToString(touch).empty()); 45 EXPECT_FALSE(WebInputEventTraits::ToString(touch).empty());
268 } 46 }
269 47
270 } // namespace 48 } // namespace
271 } // namespace content 49 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698