Chromium Code Reviews| 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" | |
|
Wez
2015/02/05 02:09:06
If you remove the CHECKs you shouldn't need this i
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 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; | |
|
Wez
2015/02/05 02:09:07
nit: Suggest moving this down to near the PointInf
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 24 | |
| 25 // If the rounding error for the coordinates checked in | |
| 26 // TouchPointCoordinateEqual is within 1 pixel diff, it should be acceptable. | |
|
Wez
2015/02/05 02:09:07
nit: Suggest "Require touch point coordinates to b
Rintaro Kuroiwa
2015/02/06 23:35:01
I think off by 1 pixel is acceptable.
Wez
2015/02/13 16:51:45
Acknowledged.
| |
| 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 MATCHER_P(TouchPointRadiiEqual, expected_event, "Expected point radii equal.") { | |
| 51 EXPECT_EQ(expected_event.touch_points().size(), arg.touch_points().size()); | |
| 52 if (arg.touch_points().size() != expected_event.touch_points().size()) | |
| 53 return false; | |
| 54 | |
| 55 for (int i = 0; i < expected_event.touch_points().size(); ++i) { | |
| 56 const TouchEventPoint& arg_point = arg.touch_points(i); | |
| 57 const TouchEventPoint& expected_point = expected_event.touch_points(i); | |
| 58 EXPECT_NEAR(expected_point.radius_x(), arg_point.radius_x(), kEpsilon); | |
| 59 if (std::abs(expected_point.radius_x() - arg_point.radius_x()) >= kEpsilon) | |
| 60 return false; | |
| 61 | |
| 62 EXPECT_NEAR(expected_point.radius_y(), arg_point.radius_y(), kEpsilon); | |
| 63 if (std::abs(expected_point.radius_y() - arg_point.radius_y()) >= kEpsilon) | |
| 64 return false; | |
| 65 } | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 struct PointInfo { | |
| 70 PointInfo(float x, float y) | |
| 71 : PointInfo(x, y, kDefaultRadius, kDefaultRadius) {} | |
| 72 PointInfo(float x, float y, float radius_x, float radius_y) | |
| 73 : x(x), y(y), radius_x(radius_x), radius_y(radius_y) {} | |
| 74 | |
| 75 float x; | |
| 76 float y; | |
| 77 float radius_x; | |
| 78 float radius_y; | |
| 79 }; | |
| 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 AddDefaultTestCoordinate() { point_infos_.push_back({1.0f, 1.0f}); } | |
|
Wez
2015/02/05 02:09:07
Earlier you defined kDefaultRadius; consider defin
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 92 | |
| 93 void InjectTestTouchEvent() { | |
| 94 CHECK(!point_infos_.empty()); | |
|
Wez
2015/02/05 02:09:06
In gtest we use ASSERT rather than CHECK.
Rintaro Kuroiwa
2015/02/06 23:35:00
I thought CHECK is more appropriate here because i
| |
| 95 TouchEvent e; | |
| 96 e.set_event_type(TouchEvent::TOUCH_POINT_MOVE); | |
| 97 | |
| 98 uint32_t id = 1; | |
| 99 for (const PointInfo& point_info : point_infos_) { | |
| 100 TouchEventPoint* point = e.add_touch_points(); | |
| 101 point->set_id(id++); | |
| 102 point->set_x(point_info.x); | |
| 103 point->set_y(point_info.y); | |
| 104 point->set_radius_x(point_info.radius_x); | |
| 105 point->set_radius_y(point_info.radius_y); | |
| 106 } | |
| 107 | |
| 108 touch_input_scaler_.InjectTouchEvent(e); | |
| 109 } | |
| 110 | |
| 111 void SetInputDimensions(int width, int height) { | |
| 112 touch_input_scaler_.set_input_size(webrtc::DesktopSize(width, height)); | |
| 113 } | |
| 114 | |
| 115 void SetOutputDimensions(int width, int height) { | |
| 116 touch_input_scaler_.set_output_size(webrtc::DesktopSize(width, height)); | |
| 117 } | |
| 118 | |
| 119 MockInputStub mock_stub_; | |
| 120 TouchInputScaler touch_input_scaler_; | |
| 121 | |
| 122 private: | |
| 123 std::vector<PointInfo> point_infos_; | |
| 124 }; | |
|
Wez
2015/02/05 02:09:07
DISALLOW_COPY_AND_ASSIGN()
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 125 | |
| 126 // TouchInputFilter require both input and output dimensions. | |
| 127 // These test verify that no events are forwarded to the next InputStub if | |
| 128 // either dimensions are not set. | |
|
Wez
2015/02/05 02:09:07
Suggest explicitly setting the dimensions to zero,
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 129 TEST_F(TouchInputScalerTest, BothDimensionsZero) { | |
| 130 AddDefaultTestCoordinate(); | |
| 131 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); | |
| 132 InjectTestTouchEvent(); | |
| 133 } | |
| 134 | |
| 135 TEST_F(TouchInputScalerTest, SetOnlyInputDimensions) { | |
| 136 SetInputDimensions(50, 60); | |
| 137 AddDefaultTestCoordinate(); | |
| 138 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); | |
| 139 InjectTestTouchEvent(); | |
| 140 } | |
| 141 | |
| 142 TEST_F(TouchInputScalerTest, SetOnlyOutputDimensions) { | |
| 143 SetOutputDimensions(50, 60); | |
| 144 AddDefaultTestCoordinate(); | |
| 145 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0); | |
| 146 InjectTestTouchEvent(); | |
| 147 } | |
| 148 | |
| 149 // The x,y coordinate fall in the desktop size. | |
| 150 TEST_F(TouchInputScalerTest, NoClampingNoScaling) { | |
| 151 SetInputDimensions(50, 60); | |
| 152 SetOutputDimensions(50, 60); | |
| 153 | |
| 154 AddInputCoordinate({10.0f, 15.0f}); | |
| 155 TouchEvent expected_out; | |
| 156 TouchEventPoint* point = expected_out.add_touch_points(); | |
| 157 point->set_x(10.0f); | |
| 158 point->set_y(15.0f); | |
| 159 | |
| 160 EXPECT_CALL(mock_stub_, | |
| 161 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
| 162 InjectTestTouchEvent(); | |
| 163 } | |
| 164 | |
| 165 // Make sure clamping works. | |
|
Wez
2015/02/05 02:09:07
nit: Suggest rewording this to be similar to the w
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 166 TEST_F(TouchInputScalerTest, ClampingNoScaling) { | |
| 167 SetInputDimensions(50, 60); | |
| 168 SetOutputDimensions(50, 60); | |
| 169 | |
| 170 // Note that this could happen if touch started in the chromoting window but | |
| 171 // the finger moved off the windows. | |
| 172 AddInputCoordinate({-1.0f, 1.0f}); | |
| 173 TouchEvent expected_out; | |
| 174 TouchEventPoint* point = expected_out.add_touch_points(); | |
| 175 point->set_x(0.0f); | |
| 176 point->set_y(1.0f); | |
|
Wez
2015/02/05 02:09:06
Does it matter that the radius of the event may fa
Rintaro Kuroiwa
2015/02/06 23:35:01
This test does not due to the matcher that is used
| |
| 177 | |
| 178 EXPECT_CALL(mock_stub_, | |
| 179 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
| 180 InjectTestTouchEvent(); | |
| 181 } | |
| 182 | |
| 183 TEST_F(TouchInputScalerTest, ClampingMultiplePointsNoScaling) { | |
|
Wez
2015/02/05 02:09:07
Suggest including an un-clamped point in this test
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 184 SetInputDimensions(50, 60); | |
| 185 SetOutputDimensions(50, 60); | |
| 186 | |
| 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 AddInputCoordinate({-2.0f, 1.0f}); | |
| 194 point = expected_out.add_touch_points(); | |
| 195 point->set_x(0.0f); | |
| 196 point->set_y(1.0f); | |
| 197 | |
| 198 AddInputCoordinate({-3.0f, -1.0f}); | |
| 199 point = expected_out.add_touch_points(); | |
| 200 point->set_x(0.0f); | |
| 201 point->set_y(0.0f); | |
| 202 | |
| 203 AddInputCoordinate({100.0f, 100.0f}); | |
| 204 point = expected_out.add_touch_points(); | |
| 205 // 1 less than max width and height. | |
| 206 point->set_x(49.0f); | |
| 207 point->set_y(59.0f); | |
| 208 | |
| 209 EXPECT_CALL(mock_stub_, | |
| 210 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
| 211 InjectTestTouchEvent(); | |
| 212 } | |
| 213 | |
| 214 // Verify up scaling works. All coordinates should fall inside the output | |
|
Wez
2015/02/05 02:09:07
nit: up-scaling
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 215 // dimensions, after scaling, i.e. no clamping testing. | |
|
Wez
2015/02/05 02:09:06
not sure what "no clamping testing" means
Rintaro Kuroiwa
2015/02/06 23:35:01
Meaning this should only test scaling and should n
| |
| 216 TEST_F(TouchInputScalerTest, UpScalingNoClamp) { | |
| 217 const int kInputDimension = 20; | |
| 218 const int kUpScalingFactor = 2; | |
| 219 SetInputDimensions(kInputDimension, kInputDimension); | |
| 220 SetOutputDimensions(kInputDimension * kUpScalingFactor, | |
| 221 kInputDimension * kUpScalingFactor); | |
| 222 | |
| 223 AddInputCoordinate({1.0f, 1.0f}); | |
| 224 TouchEvent expected_out; | |
| 225 TouchEventPoint* point = expected_out.add_touch_points(); | |
| 226 point->set_x(1.0f * kUpScalingFactor); | |
| 227 point->set_y(1.0f * kUpScalingFactor); | |
|
Wez
2015/02/05 02:09:07
You're calculating the expected output from the sa
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 228 | |
| 229 AddInputCoordinate({5.0f, 3.0f}); | |
| 230 point = expected_out.add_touch_points(); | |
| 231 point->set_x(5.0f * kUpScalingFactor); | |
| 232 point->set_y(3.0f * kUpScalingFactor); | |
| 233 EXPECT_CALL(mock_stub_, | |
| 234 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
| 235 InjectTestTouchEvent(); | |
| 236 } | |
| 237 | |
| 238 // Verify up scaling works. | |
|
Wez
2015/02/05 02:09:06
You mean works, with clamping?
Rintaro Kuroiwa
2015/02/06 23:35:01
Yes. Done.
| |
| 239 TEST_F(TouchInputScalerTest, UpScaling) { | |
| 240 const int kInputDimension = 20; | |
| 241 const int kUpScalingFactor = 2; | |
| 242 SetInputDimensions(kInputDimension, kInputDimension); | |
| 243 SetOutputDimensions(kInputDimension * kUpScalingFactor, | |
| 244 kInputDimension * kUpScalingFactor); | |
| 245 | |
| 246 AddInputCoordinate({25.0f, 25.0f}); | |
| 247 TouchEvent expected_out; | |
| 248 TouchEventPoint* point = expected_out.add_touch_points(); | |
| 249 point->set_x(39.0f); | |
| 250 point->set_y(39.0f); | |
| 251 | |
| 252 EXPECT_CALL(mock_stub_, | |
| 253 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
| 254 InjectTestTouchEvent(); | |
| 255 } | |
| 256 | |
| 257 // Verify down scaling works. All coordinates should fall inside the output | |
| 258 // dimensions, after scaling, i.e. no clamping testing. | |
| 259 TEST_F(TouchInputScalerTest, DownScalingNoClamp) { | |
|
Wez
2015/02/05 02:09:07
NoClamping
Rintaro Kuroiwa
2015/02/06 23:35:01
Done. Renamed to UpScalingNoClamping as well.
| |
| 260 const int kOutputDimension = 20; | |
| 261 const int kDownScalingFactor = 2; | |
| 262 SetInputDimensions(kOutputDimension * kDownScalingFactor, | |
| 263 kOutputDimension * kDownScalingFactor); | |
| 264 SetOutputDimensions(kOutputDimension, kOutputDimension); | |
| 265 | |
| 266 AddInputCoordinate({2.0f, 2.0f}); | |
| 267 TouchEvent expected_out; | |
| 268 TouchEventPoint* point = expected_out.add_touch_points(); | |
| 269 point->set_x(2.0f / kDownScalingFactor); | |
| 270 point->set_y(2.0f / kDownScalingFactor); | |
| 271 | |
| 272 AddInputCoordinate({6.0f, 3.0f}); | |
| 273 point = expected_out.add_touch_points(); | |
| 274 point->set_x(6.0f / kDownScalingFactor); | |
| 275 point->set_y(3.0f / kDownScalingFactor); | |
| 276 EXPECT_CALL(mock_stub_, | |
| 277 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
| 278 InjectTestTouchEvent(); | |
| 279 } | |
| 280 | |
| 281 // Verify down scaling works. | |
|
Wez
2015/02/05 02:09:07
As above
Rintaro Kuroiwa
2015/02/06 23:35:01
Done.
| |
| 282 TEST_F(TouchInputScalerTest, DownScaling) { | |
| 283 const int kOutputDimension = 20; | |
| 284 const int kDownScalingFactor = 2; | |
| 285 SetInputDimensions(kOutputDimension * kDownScalingFactor, | |
| 286 kOutputDimension * kDownScalingFactor); | |
| 287 SetOutputDimensions(kOutputDimension, kOutputDimension); | |
| 288 | |
| 289 AddInputCoordinate({-20.0f, 10.0f}); | |
| 290 TouchEvent expected_out; | |
| 291 TouchEventPoint* point = expected_out.add_touch_points(); | |
| 292 point->set_x(0.0f); | |
| 293 point->set_y(10.0f / kDownScalingFactor); | |
| 294 | |
| 295 AddInputCoordinate({10.0f, -20.0f}); | |
| 296 point = expected_out.add_touch_points(); | |
| 297 point->set_x(10.0f / kDownScalingFactor); | |
| 298 point->set_y(0.0f); | |
| 299 | |
| 300 AddInputCoordinate({6.0f, 80.0f}); | |
| 301 point = expected_out.add_touch_points(); | |
| 302 point->set_x(6.0f / kDownScalingFactor); | |
| 303 point->set_y(kOutputDimension - 1); | |
| 304 | |
| 305 AddInputCoordinate({80.0f, 6.0f}); | |
| 306 point = expected_out.add_touch_points(); | |
| 307 point->set_x(kOutputDimension - 1); | |
| 308 point->set_y(6.0f / kDownScalingFactor); | |
| 309 | |
| 310 EXPECT_CALL(mock_stub_, | |
| 311 InjectTouchEvent(TouchPointCoordinateEqual(expected_out))); | |
| 312 InjectTestTouchEvent(); | |
| 313 } | |
| 314 | |
| 315 // Scaling up by factor of 2. | |
|
Wez
2015/02/05 02:09:06
This comment doesn't seem to relate to the test?
Rintaro Kuroiwa
2015/02/06 23:35:01
Yes, the comment is a test detail, not for explain
| |
| 316 TEST_F(TouchInputScalerTest, UpScaleRadii) { | |
| 317 SetInputDimensions(20, 20); | |
| 318 SetOutputDimensions(40, 40); | |
| 319 | |
| 320 AddInputCoordinate({0.0f, 0.0f, 1.0f, 2.0f}); | |
| 321 TouchEvent expected_out; | |
| 322 TouchEventPoint* point = expected_out.add_touch_points(); | |
| 323 point->set_radius_x(2.0f); | |
| 324 point->set_radius_y(4.0f); | |
| 325 | |
| 326 EXPECT_CALL(mock_stub_, InjectTouchEvent(TouchPointRadiiEqual(expected_out))); | |
| 327 InjectTestTouchEvent(); | |
| 328 } | |
| 329 | |
| 330 // Scaling down by factor of 2. | |
|
Wez
2015/02/05 02:09:07
As above.
Rintaro Kuroiwa
2015/02/06 23:35:01
same.
| |
| 331 TEST_F(TouchInputScalerTest, DownScaleRadii) { | |
| 332 SetInputDimensions(20, 20); | |
| 333 SetOutputDimensions(10, 10); | |
| 334 | |
| 335 AddInputCoordinate({0.0f, 0.0f, 5.0f, 4.0f}); | |
| 336 TouchEvent expected_out; | |
| 337 TouchEventPoint* point = expected_out.add_touch_points(); | |
| 338 point->set_radius_x(2.5f); | |
| 339 point->set_radius_y(2.0f); | |
| 340 | |
| 341 EXPECT_CALL(mock_stub_, InjectTouchEvent(TouchPointRadiiEqual(expected_out))); | |
| 342 InjectTestTouchEvent(); | |
| 343 } | |
| 344 | |
| 345 } // namespace remoting | |
| OLD | NEW |