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