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 "base/logging.h" |
| 10 #include "remoting/protocol/protocol_mock_objects.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace remoting { |
| 15 |
| 16 using ::testing::_; |
| 17 using protocol::MockInputStub; |
| 18 using protocol::TouchEvent; |
| 19 using protocol::TouchEventPoint; |
| 20 |
| 21 namespace { |
| 22 |
| 23 const float kDefaultRadius = 30.0f; |
| 24 |
| 25 // If the rounding error for the coordinates checked in |
| 26 // TouchPointCoordinateEqual is within 1 pixel diff, it should be acceptable. |
| 27 const float kEpsilon = 1.0f; |
| 28 |
| 29 MATCHER_P(TouchPointCoordinateEqual, |
| 30 expected_event, |
| 31 "Expect point coordinates equal.") { |
| 32 EXPECT_EQ(expected_event.touch_points().size(), arg.touch_points().size()); |
| 33 if (arg.touch_points().size() != expected_event.touch_points().size()) |
| 34 return false; |
| 35 |
| 36 for (int i = 0; i < expected_event.touch_points().size(); ++i) { |
| 37 const TouchEventPoint& arg_point = arg.touch_points(i); |
| 38 const TouchEventPoint& expected_point = expected_event.touch_points(i); |
| 39 EXPECT_NEAR(expected_point.x(), arg_point.x(), kEpsilon); |
| 40 if (std::abs(expected_point.x() - arg_point.x()) >= kEpsilon) |
| 41 return false; |
| 42 |
| 43 EXPECT_NEAR(expected_point.y(), arg_point.y(), kEpsilon); |
| 44 if (std::abs(expected_point.y() - arg_point.y()) >= kEpsilon) |
| 45 return false; |
| 46 } |
| 47 return true; |
| 48 } |
| 49 |
| 50 struct Coordinate { |
| 51 float x; |
| 52 float y; |
| 53 }; |
| 54 |
| 55 } // namespace |
| 56 |
| 57 class TouchInputScalerTest : public ::testing::Test { |
| 58 protected: |
| 59 TouchInputScalerTest() : touch_input_scaler_(&mock_stub_) {} |
| 60 |
| 61 void AddInputCoordinate(const Coordinate& coordinate) { |
| 62 coordinates_.push_back(coordinate); |
| 63 } |
| 64 |
| 65 void AddDefaultTestCoordinate() { |
| 66 coordinates_.push_back({1.0f, 1.0f}); |
| 67 } |
| 68 |
| 69 void InjectTestTouchEvent() { |
| 70 CHECK(!coordinates_.empty()); |
| 71 TouchEvent e; |
| 72 e.set_event_type(TouchEvent::TOUCH_POINT_MOVE); |
| 73 |
| 74 uint32_t id = 1; |
| 75 for (const Coordinate& coordinate : coordinates_) { |
| 76 TouchEventPoint* point = e.add_touch_points(); |
| 77 point->set_id(id++); |
| 78 point->set_x(coordinate.x); |
| 79 point->set_y(coordinate.y); |
| 80 point->set_radius_x(kDefaultRadius); |
| 81 point->set_radius_y(kDefaultRadius); |
| 82 } |
| 83 |
| 84 touch_input_scaler_.InjectTouchEvent(e); |
| 85 } |
| 86 |
| 87 void SetInputDimensions(int width, int height) { |
| 88 touch_input_scaler_.set_input_size(webrtc::DesktopSize(width, height)); |
| 89 } |
| 90 |
| 91 void SetOutputDimensions(int width, int height) { |
| 92 touch_input_scaler_.set_output_size(webrtc::DesktopSize(width, height)); |
| 93 } |
| 94 |
| 95 MockInputStub mock_stub_; |
| 96 TouchInputScaler touch_input_scaler_; |
| 97 |
| 98 private: |
| 99 std::vector<Coordinate> coordinates_; |
| 100 }; |
| 101 |
| 102 // TouchInputFilter require both input and output dimensions. |
| 103 // These test verify that no events are forwarded to the next InputStub if |
| 104 // either dimensions are not set. |
| 105 TEST_F(TouchInputScalerTest, BothDimensionsZero) { |
| 106 AddDefaultTestCoordinate(); |
| 107 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); |
| 108 InjectTestTouchEvent(); |
| 109 } |
| 110 |
| 111 TEST_F(TouchInputScalerTest, SetOnlyInputDimensions) { |
| 112 SetInputDimensions(50, 60); |
| 113 AddDefaultTestCoordinate(); |
| 114 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); |
| 115 InjectTestTouchEvent(); |
| 116 } |
| 117 |
| 118 TEST_F(TouchInputScalerTest, SetOnlyOutputDimensions) { |
| 119 SetOutputDimensions(50, 60); |
| 120 AddDefaultTestCoordinate(); |
| 121 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); |
| 122 InjectTestTouchEvent(); |
| 123 } |
| 124 |
| 125 // The x,y coordinate fall in the desktop size. |
| 126 TEST_F(TouchInputScalerTest, NoClampingNoScaling) { |
| 127 SetInputDimensions(50, 60); |
| 128 SetOutputDimensions(50, 60); |
| 129 |
| 130 AddInputCoordinate({10.0f, 15.0f}); |
| 131 TouchEvent expected_out; |
| 132 TouchEventPoint* point = expected_out.add_touch_points(); |
| 133 point->set_x(10.0f); |
| 134 point->set_y(15.0f); |
| 135 |
| 136 EXPECT_CALL(mock_stub_, |
| 137 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); |
| 138 InjectTestTouchEvent(); |
| 139 } |
| 140 |
| 141 // Make sure clamping works. |
| 142 TEST_F(TouchInputScalerTest, ClampingNoScaling) { |
| 143 SetInputDimensions(50, 60); |
| 144 SetOutputDimensions(50, 60); |
| 145 |
| 146 // Note that this could happen if touch started in the chromoting window but |
| 147 // the finger moved off the windows. |
| 148 AddInputCoordinate({-1.0f, 1.0f}); |
| 149 TouchEvent expected_out; |
| 150 TouchEventPoint* point = expected_out.add_touch_points(); |
| 151 point->set_x(0.0f); |
| 152 point->set_y(1.0f); |
| 153 |
| 154 EXPECT_CALL(mock_stub_, |
| 155 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); |
| 156 InjectTestTouchEvent(); |
| 157 } |
| 158 |
| 159 TEST_F(TouchInputScalerTest, ClampingMultiplePointsNoScaling) { |
| 160 SetInputDimensions(50, 60); |
| 161 SetOutputDimensions(50, 60); |
| 162 |
| 163 AddInputCoordinate({-1.0f, 1.0f}); |
| 164 TouchEvent expected_out; |
| 165 TouchEventPoint* point = expected_out.add_touch_points(); |
| 166 point->set_x(0.0f); |
| 167 point->set_y(1.0f); |
| 168 |
| 169 AddInputCoordinate({-2.0f, 1.0f}); |
| 170 point = expected_out.add_touch_points(); |
| 171 point->set_x(0.0f); |
| 172 point->set_y(1.0f); |
| 173 |
| 174 AddInputCoordinate({-3.0f, -1.0f}); |
| 175 point = expected_out.add_touch_points(); |
| 176 point->set_x(0.0f); |
| 177 point->set_y(0.0f); |
| 178 |
| 179 AddInputCoordinate({100.0f, 100.0f}); |
| 180 point = expected_out.add_touch_points(); |
| 181 // 1 less than max width and height. |
| 182 point->set_x(49.0f); |
| 183 point->set_y(59.0f); |
| 184 |
| 185 EXPECT_CALL(mock_stub_, |
| 186 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); |
| 187 InjectTestTouchEvent(); |
| 188 } |
| 189 |
| 190 // Verify up scaling works. All coordinates should fall inside the output |
| 191 // dimensions, after scaling, i.e. no clamping testing. |
| 192 TEST_F(TouchInputScalerTest, UpScalingNoClamp) { |
| 193 const int kInputDimension = 20; |
| 194 const int kScalingFactor = 2; |
| 195 SetInputDimensions(kInputDimension, kInputDimension); |
| 196 SetOutputDimensions(kInputDimension * kScalingFactor, |
| 197 kInputDimension * kScalingFactor); |
| 198 |
| 199 AddInputCoordinate({1.0f, 1.0f}); |
| 200 TouchEvent expected_out; |
| 201 TouchEventPoint* point = expected_out.add_touch_points(); |
| 202 point->set_x(1.0f * kScalingFactor); |
| 203 point->set_y(1.0f * kScalingFactor); |
| 204 |
| 205 AddInputCoordinate({5.0f, 3.0f}); |
| 206 point = expected_out.add_touch_points(); |
| 207 point->set_x(5.0f * kScalingFactor); |
| 208 point->set_y(3.0f * kScalingFactor); |
| 209 EXPECT_CALL(mock_stub_, |
| 210 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); |
| 211 InjectTestTouchEvent(); |
| 212 } |
| 213 |
| 214 // Verify up scaling works. |
| 215 TEST_F(TouchInputScalerTest, UpScaling) { |
| 216 const int kInputDimension = 20; |
| 217 const int kScalingFactor = 2; |
| 218 SetInputDimensions(kInputDimension, kInputDimension); |
| 219 SetOutputDimensions(kInputDimension * kScalingFactor, |
| 220 kInputDimension * kScalingFactor); |
| 221 |
| 222 AddInputCoordinate({25.0f, 25.0f}); |
| 223 TouchEvent expected_out; |
| 224 TouchEventPoint* point = expected_out.add_touch_points(); |
| 225 point->set_x(39.0f); |
| 226 point->set_y(39.0f); |
| 227 |
| 228 EXPECT_CALL(mock_stub_, |
| 229 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); |
| 230 InjectTestTouchEvent(); |
| 231 } |
| 232 |
| 233 // Verify down scaling works. All coordinates should fall inside the output |
| 234 // dimensions, after scaling, i.e. no clamping testing. |
| 235 TEST_F(TouchInputScalerTest, DownScalingNoClamp) { |
| 236 const int kOutputDimension = 20; |
| 237 const int kDownScalingFactor = 2; |
| 238 SetInputDimensions(kOutputDimension * kDownScalingFactor, |
| 239 kOutputDimension * kDownScalingFactor); |
| 240 SetOutputDimensions(kOutputDimension, kOutputDimension); |
| 241 |
| 242 AddInputCoordinate({2.0f, 2.0f}); |
| 243 TouchEvent expected_out; |
| 244 TouchEventPoint* point = expected_out.add_touch_points(); |
| 245 point->set_x(2.0f / kDownScalingFactor); |
| 246 point->set_y(2.0f / kDownScalingFactor); |
| 247 |
| 248 AddInputCoordinate({6.0f, 3.0f}); |
| 249 point = expected_out.add_touch_points(); |
| 250 point->set_x(6.0f / kDownScalingFactor); |
| 251 point->set_y(3.0f / kDownScalingFactor); |
| 252 EXPECT_CALL(mock_stub_, |
| 253 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); |
| 254 InjectTestTouchEvent(); |
| 255 } |
| 256 |
| 257 // Verify down scaling works. |
| 258 TEST_F(TouchInputScalerTest, DownScaling) { |
| 259 const int kOutputDimension = 20; |
| 260 const int kDownScalingFactor = 2; |
| 261 SetInputDimensions(kOutputDimension * kDownScalingFactor, |
| 262 kOutputDimension * kDownScalingFactor); |
| 263 SetOutputDimensions(kOutputDimension, kOutputDimension); |
| 264 |
| 265 AddInputCoordinate({-20.0f, 10.0f}); |
| 266 TouchEvent expected_out; |
| 267 TouchEventPoint* point = expected_out.add_touch_points(); |
| 268 point->set_x(0.0f); |
| 269 point->set_y(10.0f / kDownScalingFactor); |
| 270 |
| 271 AddInputCoordinate({10.0f, -20.0f}); |
| 272 point = expected_out.add_touch_points(); |
| 273 point->set_x(10.0f / kDownScalingFactor); |
| 274 point->set_y(0.0f); |
| 275 |
| 276 AddInputCoordinate({6.0f, 80.0f}); |
| 277 point = expected_out.add_touch_points(); |
| 278 point->set_x(6.0f / kDownScalingFactor); |
| 279 point->set_y(kOutputDimension - 1); |
| 280 |
| 281 AddInputCoordinate({80.0f, 6.0f}); |
| 282 point = expected_out.add_touch_points(); |
| 283 point->set_x(kOutputDimension - 1); |
| 284 point->set_y(6.0f / kDownScalingFactor); |
| 285 |
| 286 EXPECT_CALL(mock_stub_, |
| 287 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); |
| 288 InjectTestTouchEvent(); |
| 289 } |
| 290 |
| 291 } // namespace remoting |
OLD | NEW |