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