OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "remoting/client/plugin/touch_input_scaler.h" | |
6 | |
7 #include <cmath> | |
8 | |
9 #include "remoting/protocol/protocol_mock_objects.h" | |
10 #include "testing/gmock/include/gmock/gmock.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 namespace remoting { | |
14 | |
15 using ::testing::_; | |
16 using protocol::MockInputStub; | |
17 using protocol::TouchEvent; | |
18 using protocol::TouchEventPoint; | |
19 | |
20 namespace { | |
21 | |
22 // If the rounding error for the coordinates checked in TouchPoint* matcher are | |
23 // within 1 pixel diff, it should be acceptable. | |
24 const float kEpsilon = 1.0f; | |
25 | |
26 MATCHER_P(TouchPointCoordinateEqual, | |
27 expected_event, | |
28 "Expect point coordinates equal.") { | |
29 EXPECT_EQ(expected_event.touch_points().size(), arg.touch_points().size()); | |
30 if (arg.touch_points().size() != expected_event.touch_points().size()) | |
31 return false; | |
32 | |
33 for (int i = 0; i < expected_event.touch_points().size(); ++i) { | |
34 const TouchEventPoint& arg_point = arg.touch_points(i); | |
35 const TouchEventPoint& expected_point = expected_event.touch_points(i); | |
36 EXPECT_NEAR(expected_point.x(), arg_point.x(), kEpsilon); | |
Wez
2015/02/13 16:51:46
Why do you need both the EXPECT and the if()?
Rintaro Kuroiwa
2015/02/17 19:52:12
These are mainly for debugging. The error messages
Wez
2015/02/23 23:24:07
Can you specify your own logging output for the fa
Rintaro Kuroiwa
2015/02/24 23:43:10
Done. Added logging functions.
| |
37 if (std::abs(expected_point.x() - arg_point.x()) >= kEpsilon) | |
38 return false; | |
39 | |
40 EXPECT_NEAR(expected_point.y(), arg_point.y(), kEpsilon); | |
41 if (std::abs(expected_point.y() - arg_point.y()) >= kEpsilon) | |
42 return false; | |
43 } | |
44 return true; | |
45 } | |
46 | |
47 MATCHER_P(TouchPointRadiiEqual, expected_event, "Expected point radii equal.") { | |
48 EXPECT_EQ(expected_event.touch_points().size(), arg.touch_points().size()); | |
49 if (arg.touch_points().size() != expected_event.touch_points().size()) | |
50 return false; | |
51 | |
52 for (int i = 0; i < expected_event.touch_points().size(); ++i) { | |
53 const TouchEventPoint& arg_point = arg.touch_points(i); | |
54 const TouchEventPoint& expected_point = expected_event.touch_points(i); | |
55 EXPECT_NEAR(expected_point.radius_x(), arg_point.radius_x(), kEpsilon); | |
Wez
2015/02/13 16:51:46
Ditto
Rintaro Kuroiwa
2015/02/17 19:52:12
Done.
| |
56 if (std::abs(expected_point.radius_x() - arg_point.radius_x()) >= kEpsilon) | |
57 return false; | |
58 | |
59 EXPECT_NEAR(expected_point.radius_y(), arg_point.radius_y(), kEpsilon); | |
60 if (std::abs(expected_point.radius_y() - arg_point.radius_y()) >= kEpsilon) | |
61 return false; | |
62 } | |
63 return true; | |
64 } | |
65 | |
66 const float kDefaultRadius = 30.0f; | |
67 const float kDefaultXCoord = 1.0f; | |
68 const float kDefaultYCoord = 1.0f; | |
69 | |
70 struct PointInfo { | |
71 PointInfo(float x, float y) | |
72 : PointInfo(x, y, kDefaultRadius, kDefaultRadius) {} | |
73 PointInfo(float x, float y, float radius_x, float radius_y) | |
74 : x(x), y(y), radius_x(radius_x), radius_y(radius_y) {} | |
75 | |
76 float x; | |
77 float y; | |
78 float radius_x; | |
79 float radius_y; | |
80 }; | |
81 | |
82 const PointInfo kDefaultPointInfo = {kDefaultXCoord, | |
83 kDefaultYCoord, | |
84 kDefaultRadius, | |
85 kDefaultRadius}; | |
86 | |
87 } // namespace | |
88 | |
89 class TouchInputScalerTest : public ::testing::Test { | |
90 protected: | |
91 TouchInputScalerTest() : touch_input_scaler_(&mock_stub_) {} | |
92 | |
93 void AddInputCoordinate(const PointInfo& point_info) { | |
94 point_infos_.push_back(point_info); | |
95 } | |
96 | |
97 void AddDefaultTestPoint() { point_infos_.push_back(kDefaultPointInfo); } | |
98 | |
99 void InjectTestTouchEvent() { | |
100 TouchEvent e; | |
101 e.set_event_type(TouchEvent::TOUCH_POINT_MOVE); | |
102 | |
103 uint32_t id = 1; | |
104 for (const PointInfo& point_info : point_infos_) { | |
105 TouchEventPoint* point = e.add_touch_points(); | |
106 point->set_id(id++); | |
107 point->set_x(point_info.x); | |
108 point->set_y(point_info.y); | |
109 point->set_radius_x(point_info.radius_x); | |
110 point->set_radius_y(point_info.radius_y); | |
111 } | |
112 | |
113 touch_input_scaler_.InjectTouchEvent(e); | |
114 } | |
115 | |
116 void SetInputDimensions(int width, int height) { | |
117 touch_input_scaler_.set_input_size(webrtc::DesktopSize(width, height)); | |
118 } | |
119 | |
120 void SetOutputDimensions(int width, int height) { | |
121 touch_input_scaler_.set_output_size(webrtc::DesktopSize(width, height)); | |
122 } | |
123 | |
124 MockInputStub mock_stub_; | |
125 TouchInputScaler touch_input_scaler_; | |
126 | |
127 private: | |
128 std::vector<PointInfo> point_infos_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(TouchInputScalerTest); | |
131 }; | |
132 | |
133 // TouchInputFilter require both input and output dimensions. | |
134 // These test verify that no events are forwarded to the next InputStub if | |
135 // either dimensions are not set or 0. | |
136 TEST_F(TouchInputScalerTest, NoDimensionsSet) { | |
137 AddDefaultTestPoint(); | |
138 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); | |
139 InjectTestTouchEvent(); | |
140 } | |
141 | |
142 TEST_F(TouchInputScalerTest, BothDimensionsZero) { | |
143 SetInputDimensions(0, 0); | |
144 SetOutputDimensions(0, 0); | |
145 AddDefaultTestPoint(); | |
146 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); | |
147 InjectTestTouchEvent(); | |
148 } | |
149 | |
150 TEST_F(TouchInputScalerTest, SetOnlyInputDimensions) { | |
151 SetInputDimensions(50, 60); | |
152 AddDefaultTestPoint(); | |
153 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); | |
154 InjectTestTouchEvent(); | |
155 } | |
156 | |
157 TEST_F(TouchInputScalerTest, SetOnlyOutputDimensions) { | |
158 SetOutputDimensions(50, 60); | |
159 AddDefaultTestPoint(); | |
160 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); | |
161 InjectTestTouchEvent(); | |
162 } | |
163 | |
164 // The x,y coordinate fall in the desktop size. | |
165 TEST_F(TouchInputScalerTest, NoClampingNoScaling) { | |
166 SetInputDimensions(50, 60); | |
167 SetOutputDimensions(50, 60); | |
168 | |
169 AddInputCoordinate({10.0f, 15.0f}); | |
170 TouchEvent expected_out; | |
171 TouchEventPoint* point = expected_out.add_touch_points(); | |
172 point->set_x(10.0f); | |
173 point->set_y(15.0f); | |
174 | |
175 EXPECT_CALL(mock_stub_, | |
176 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
177 InjectTestTouchEvent(); | |
178 } | |
179 | |
180 // Make sure clamping works. | |
181 TEST_F(TouchInputScalerTest, ClampingNoScaling) { | |
182 SetInputDimensions(50, 60); | |
183 SetOutputDimensions(50, 60); | |
184 | |
185 // Note that this could happen if touch started in the chromoting window but | |
186 // the finger moved off the windows. | |
187 AddInputCoordinate({-1.0f, 1.0f}); | |
188 TouchEvent expected_out; | |
189 TouchEventPoint* point = expected_out.add_touch_points(); | |
190 point->set_x(0.0f); | |
191 point->set_y(1.0f); | |
192 | |
193 EXPECT_CALL(mock_stub_, | |
194 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
195 InjectTestTouchEvent(); | |
196 } | |
197 | |
198 TEST_F(TouchInputScalerTest, ClampingMultiplePointsNoScaling) { | |
199 SetInputDimensions(50, 60); | |
200 SetOutputDimensions(50, 60); | |
201 | |
202 AddInputCoordinate({-1.0f, 1.0f}); // Fall off left. | |
203 TouchEvent expected_out; | |
204 TouchEventPoint* point = expected_out.add_touch_points(); | |
205 point->set_x(0.0f); | |
206 point->set_y(1.0f); | |
207 | |
208 AddInputCoordinate({100.0f, 1.0f}); // Fall off right. | |
209 point = expected_out.add_touch_points(); | |
210 point->set_x(49.0f); | |
211 point->set_y(1.0f); | |
212 | |
213 AddInputCoordinate({20.0f, 15.0f}); // Should not be clamped. | |
214 point = expected_out.add_touch_points(); | |
215 point->set_x(20.0f); | |
216 point->set_y(15.0f); | |
217 | |
218 AddInputCoordinate({3.0f, -1.0f}); // Fall off above. | |
219 point = expected_out.add_touch_points(); | |
220 point->set_x(3.0f); | |
221 point->set_y(0.0f); | |
222 | |
223 AddInputCoordinate({10.0f, 200.0f}); // Fall off below. | |
224 point = expected_out.add_touch_points(); | |
225 point->set_x(10.0f); | |
226 point->set_y(59.0f); | |
227 | |
228 EXPECT_CALL(mock_stub_, | |
229 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
230 InjectTestTouchEvent(); | |
231 } | |
232 | |
233 // Verify up-scaling works. All coordinates should fall inside the output | |
234 // dimensions after scaling. | |
235 TEST_F(TouchInputScalerTest, UpScalingNoClamping) { | |
236 SetInputDimensions(20, 20); | |
237 SetOutputDimensions(40, 40); | |
238 | |
239 AddInputCoordinate({1.0f, 1.0f}); | |
240 TouchEvent expected_out; | |
241 TouchEventPoint* point = expected_out.add_touch_points(); | |
242 point->set_x(2.0f); | |
243 point->set_y(2.0f); | |
244 | |
245 AddInputCoordinate({1.2f, 4.2f}); | |
246 point = expected_out.add_touch_points(); | |
247 point->set_x(2.4f); | |
248 point->set_y(8.4f); | |
249 | |
250 EXPECT_CALL(mock_stub_, | |
251 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
252 InjectTestTouchEvent(); | |
253 } | |
254 | |
255 // Verify up-scaling works with clamping. | |
256 TEST_F(TouchInputScalerTest, UpScalingWithClamping) { | |
257 SetInputDimensions(20, 20); | |
258 SetOutputDimensions(40, 40); | |
259 | |
260 AddInputCoordinate({25.0f, 25.0f}); | |
261 TouchEvent expected_out; | |
262 TouchEventPoint* point = expected_out.add_touch_points(); | |
263 point->set_x(39.0f); | |
264 point->set_y(39.0f); | |
265 | |
266 EXPECT_CALL(mock_stub_, | |
267 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
268 InjectTestTouchEvent(); | |
269 } | |
270 | |
271 // Verify down scaling works. All coordinates should fall inside the output | |
272 // dimensions after scaling. | |
273 TEST_F(TouchInputScalerTest, DownScalingNoClamping) { | |
274 SetInputDimensions(40, 40); | |
275 SetOutputDimensions(20, 20); | |
276 | |
277 AddInputCoordinate({2.0f, 2.0f}); | |
278 TouchEvent expected_out; | |
279 TouchEventPoint* point = expected_out.add_touch_points(); | |
280 point->set_x(1.0f); | |
281 point->set_y(1.0f); | |
282 | |
283 AddInputCoordinate({6.0f, 3.0f}); | |
284 point = expected_out.add_touch_points(); | |
285 point->set_x(3.0); | |
286 point->set_y(1.5f); | |
287 | |
288 EXPECT_CALL(mock_stub_, | |
289 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
290 InjectTestTouchEvent(); | |
291 } | |
292 | |
293 // Verify down scaling works with clamping. | |
294 TEST_F(TouchInputScalerTest, DownScalingWithClamping) { | |
295 SetInputDimensions(40, 40); | |
296 SetOutputDimensions(20, 20); | |
297 | |
298 AddInputCoordinate({-20.0f, 10.0f}); | |
299 TouchEvent expected_out; | |
300 TouchEventPoint* point = expected_out.add_touch_points(); | |
301 point->set_x(0.0f); | |
302 point->set_y(5.0f); | |
303 | |
304 AddInputCoordinate({10.0f, -20.0f}); | |
305 point = expected_out.add_touch_points(); | |
306 point->set_x(5.0f); | |
307 point->set_y(0.0f); | |
308 | |
309 AddInputCoordinate({6.0f, 80.0f}); | |
310 point = expected_out.add_touch_points(); | |
311 point->set_x(3.0f); | |
312 point->set_y(19.0f); | |
313 | |
314 AddInputCoordinate({80.0f, 6.0f}); | |
315 point = expected_out.add_touch_points(); | |
316 point->set_x(19.0f); | |
317 point->set_y(3.0f); | |
318 | |
319 EXPECT_CALL(mock_stub_, | |
320 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
321 InjectTestTouchEvent(); | |
322 } | |
323 | |
324 // Verify that the radii are up-scaled. | |
325 TEST_F(TouchInputScalerTest, UpScaleRadii) { | |
326 SetInputDimensions(20, 20); | |
327 SetOutputDimensions(40, 40); | |
328 | |
329 AddInputCoordinate({0.0f, 0.0f, 1.0f, 2.0f}); | |
330 TouchEvent expected_out; | |
331 TouchEventPoint* point = expected_out.add_touch_points(); | |
332 point->set_radius_x(2.0f); | |
333 point->set_radius_y(4.0f); | |
334 | |
335 EXPECT_CALL(mock_stub_, InjectTouchEvent(TouchPointRadiiEqual(expected_out))); | |
336 InjectTestTouchEvent(); | |
337 } | |
338 | |
339 // Verify that the radii are down-scaled. | |
340 TEST_F(TouchInputScalerTest, DownScaleRadii) { | |
341 SetInputDimensions(20, 20); | |
342 SetOutputDimensions(10, 10); | |
343 | |
344 AddInputCoordinate({0.0f, 0.0f, 5.0f, 4.0f}); | |
345 TouchEvent expected_out; | |
346 TouchEventPoint* point = expected_out.add_touch_points(); | |
347 point->set_radius_x(2.5f); | |
348 point->set_radius_y(2.0f); | |
349 | |
350 EXPECT_CALL(mock_stub_, InjectTouchEvent(TouchPointRadiiEqual(expected_out))); | |
351 InjectTestTouchEvent(); | |
352 } | |
353 | |
354 // Verify that up-scaling with claming works for x,y coordinates and radii all | |
Wez
2015/02/13 16:51:46
clamping
Rintaro Kuroiwa
2015/02/17 19:52:12
Done.
| |
355 // work together. | |
356 TEST_F(TouchInputScalerTest, UpScaleCoordinatesAndRadii) { | |
357 SetInputDimensions(20, 20); | |
358 SetOutputDimensions(40, 40); | |
359 | |
360 AddInputCoordinate({5.0f, 12.0f, 3.0f, 2.0f}); | |
361 TouchEvent expected_out; | |
362 TouchEventPoint* point = expected_out.add_touch_points(); | |
363 point->set_x(10.0f); | |
364 point->set_y(24.0f); | |
365 point->set_radius_x(6.0f); | |
366 point->set_radius_y(4.0f); | |
367 | |
368 // Make sure clamping and scaling all work. | |
369 AddInputCoordinate({22.0f, -1.0f, 8.0f, 3.0f}); | |
370 point = expected_out.add_touch_points(); | |
371 point->set_x(39.0f); | |
372 point->set_y(0.0f); | |
373 point->set_radius_x(16.0f); | |
374 point->set_radius_y(6.0f); | |
375 | |
376 EXPECT_CALL(mock_stub_, InjectTouchEvent(::testing::AllOf( | |
377 TouchPointCoordinateEqual(expected_out), | |
378 TouchPointRadiiEqual(expected_out)))); | |
379 InjectTestTouchEvent(); | |
380 } | |
381 | |
382 // Verify that down-scaling with clamping works for x,y coordinates and radii | |
383 // all work together. | |
384 TEST_F(TouchInputScalerTest, DownScaleCoordinatesAndRadii) { | |
385 SetInputDimensions(60, 60); | |
386 SetOutputDimensions(20, 20); | |
387 | |
388 AddInputCoordinate({50.0f, 24.0f, 10.0f, 9.0f}); | |
389 TouchEvent expected_out; | |
390 TouchEventPoint* point = expected_out.add_touch_points(); | |
391 point->set_x(16.666f); | |
392 point->set_y(8.0f); | |
393 point->set_radius_x(3.333f); | |
394 point->set_radius_y(3.0f); | |
395 | |
396 // Make sure clamping and scaling all work. | |
397 AddInputCoordinate({70.0f, 82.0f, 8.0f, 3.0f}); | |
398 point = expected_out.add_touch_points(); | |
399 point->set_x(19.0f); | |
400 point->set_y(19.0f); | |
401 point->set_radius_x(2.666f); | |
402 point->set_radius_y(1.0f); | |
403 | |
404 EXPECT_CALL(mock_stub_, InjectTouchEvent(::testing::AllOf( | |
405 TouchPointCoordinateEqual(expected_out), | |
406 TouchPointRadiiEqual(expected_out)))); | |
407 InjectTestTouchEvent(); | |
408 } | |
409 | |
410 } // namespace remoting | |
OLD | NEW |