ExtensionInstallDialogView: fix scrolling behavior on Views (Win,Linux)
[chromium-blink-merge.git] / remoting / client / plugin / touch_input_scaler_unittest.cc
blob18e66118b0c6659bb02d9c5b401aea66ab49fd9e
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.
5 #include "remoting/client/plugin/touch_input_scaler.h"
7 #include <cmath>
9 #include "remoting/protocol/protocol_mock_objects.h"
10 #include "remoting/protocol/test_event_matchers.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using ::testing::_;
15 using ::testing::AllOf;
16 using ::testing::PrintToString;
18 namespace remoting {
20 using protocol::MockInputStub;
21 using protocol::TouchEvent;
22 using protocol::TouchEventPoint;
23 using protocol::test::EqualsTouchPointCoordinates;
24 using protocol::test::EqualsTouchPointRadii;
26 namespace {
28 const float kDefaultRadius = 30.0f;
29 const float kDefaultXCoord = 1.0f;
30 const float kDefaultYCoord = 1.0f;
32 struct PointInfo {
33 PointInfo(float x, float y)
34 : PointInfo(x, y, kDefaultRadius, kDefaultRadius) {}
35 PointInfo(float x, float y, float radius_x, float radius_y)
36 : x(x), y(y), radius_x(radius_x), radius_y(radius_y) {}
38 float x;
39 float y;
40 float radius_x;
41 float radius_y;
44 const PointInfo kDefaultPointInfo = {kDefaultXCoord,
45 kDefaultYCoord,
46 kDefaultRadius,
47 kDefaultRadius};
49 } // namespace
51 class TouchInputScalerTest : public ::testing::Test {
52 protected:
53 TouchInputScalerTest() : touch_input_scaler_(&mock_stub_) {}
55 void AddInputCoordinate(const PointInfo& point_info) {
56 point_infos_.push_back(point_info);
59 void AddDefaultTestPoint() { point_infos_.push_back(kDefaultPointInfo); }
61 void InjectTestTouchEvent() {
62 TouchEvent e;
63 e.set_event_type(TouchEvent::TOUCH_POINT_MOVE);
65 uint32_t id = 1;
66 for (const PointInfo& point_info : point_infos_) {
67 TouchEventPoint* point = e.add_touch_points();
68 point->set_id(id++);
69 point->set_x(point_info.x);
70 point->set_y(point_info.y);
71 point->set_radius_x(point_info.radius_x);
72 point->set_radius_y(point_info.radius_y);
75 touch_input_scaler_.InjectTouchEvent(e);
78 void SetInputDimensions(int width, int height) {
79 touch_input_scaler_.set_input_size(webrtc::DesktopSize(width, height));
82 void SetOutputDimensions(int width, int height) {
83 touch_input_scaler_.set_output_size(webrtc::DesktopSize(width, height));
86 MockInputStub mock_stub_;
87 TouchInputScaler touch_input_scaler_;
89 private:
90 std::vector<PointInfo> point_infos_;
92 DISALLOW_COPY_AND_ASSIGN(TouchInputScalerTest);
95 // TouchInputFilter require both input and output dimensions.
96 // These test verify that no events are forwarded to the next InputStub if
97 // either dimensions are not set or 0.
98 TEST_F(TouchInputScalerTest, NoDimensionsSet) {
99 AddDefaultTestPoint();
100 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0);
101 InjectTestTouchEvent();
104 TEST_F(TouchInputScalerTest, BothDimensionsZero) {
105 SetInputDimensions(0, 0);
106 SetOutputDimensions(0, 0);
107 AddDefaultTestPoint();
108 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0);
109 InjectTestTouchEvent();
112 TEST_F(TouchInputScalerTest, SetOnlyInputDimensions) {
113 SetInputDimensions(50, 60);
114 AddDefaultTestPoint();
115 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0);
116 InjectTestTouchEvent();
119 TEST_F(TouchInputScalerTest, SetOnlyOutputDimensions) {
120 SetOutputDimensions(50, 60);
121 AddDefaultTestPoint();
122 EXPECT_CALL(mock_stub_, InjectTouchEvent(_)).Times(0);
123 InjectTestTouchEvent();
126 // The x,y coordinate fall in the desktop size.
127 TEST_F(TouchInputScalerTest, NoClampingNoScaling) {
128 SetInputDimensions(50, 60);
129 SetOutputDimensions(50, 60);
131 AddInputCoordinate({10.0f, 15.0f});
132 TouchEvent expected_out;
133 TouchEventPoint* point = expected_out.add_touch_points();
134 point->set_x(10.0f);
135 point->set_y(15.0f);
137 EXPECT_CALL(mock_stub_,
138 InjectTouchEvent(EqualsTouchPointCoordinates(expected_out)));
139 InjectTestTouchEvent();
142 // Make sure clamping works.
143 TEST_F(TouchInputScalerTest, ClampingNoScaling) {
144 SetInputDimensions(50, 60);
145 SetOutputDimensions(50, 60);
147 // Note that this could happen if touch started in the chromoting window but
148 // the finger moved off the windows.
149 AddInputCoordinate({-1.0f, 1.0f});
150 TouchEvent expected_out;
151 TouchEventPoint* point = expected_out.add_touch_points();
152 point->set_x(0.0f);
153 point->set_y(1.0f);
155 EXPECT_CALL(mock_stub_,
156 InjectTouchEvent(EqualsTouchPointCoordinates(expected_out)));
157 InjectTestTouchEvent();
160 TEST_F(TouchInputScalerTest, ClampingMultiplePointsNoScaling) {
161 SetInputDimensions(50, 60);
162 SetOutputDimensions(50, 60);
164 AddInputCoordinate({-1.0f, 1.0f}); // Fall off left.
165 TouchEvent expected_out;
166 TouchEventPoint* point = expected_out.add_touch_points();
167 point->set_x(0.0f);
168 point->set_y(1.0f);
170 AddInputCoordinate({100.0f, 1.0f}); // Fall off right.
171 point = expected_out.add_touch_points();
172 point->set_x(49.0f);
173 point->set_y(1.0f);
175 AddInputCoordinate({20.0f, 15.0f}); // Should not be clamped.
176 point = expected_out.add_touch_points();
177 point->set_x(20.0f);
178 point->set_y(15.0f);
180 AddInputCoordinate({3.0f, -1.0f}); // Fall off above.
181 point = expected_out.add_touch_points();
182 point->set_x(3.0f);
183 point->set_y(0.0f);
185 AddInputCoordinate({10.0f, 200.0f}); // Fall off below.
186 point = expected_out.add_touch_points();
187 point->set_x(10.0f);
188 point->set_y(59.0f);
190 EXPECT_CALL(mock_stub_,
191 InjectTouchEvent(EqualsTouchPointCoordinates(expected_out)));
192 InjectTestTouchEvent();
195 // Verify up-scaling works. All coordinates should fall inside the output
196 // dimensions after scaling.
197 TEST_F(TouchInputScalerTest, UpScalingNoClamping) {
198 SetInputDimensions(20, 20);
199 SetOutputDimensions(40, 40);
201 AddInputCoordinate({1.0f, 1.0f});
202 TouchEvent expected_out;
203 TouchEventPoint* point = expected_out.add_touch_points();
204 point->set_x(2.0f);
205 point->set_y(2.0f);
207 AddInputCoordinate({1.2f, 4.2f});
208 point = expected_out.add_touch_points();
209 point->set_x(2.4f);
210 point->set_y(8.4f);
212 EXPECT_CALL(mock_stub_,
213 InjectTouchEvent(EqualsTouchPointCoordinates(expected_out)));
214 InjectTestTouchEvent();
217 // Verify up-scaling works with clamping.
218 TEST_F(TouchInputScalerTest, UpScalingWithClamping) {
219 SetInputDimensions(20, 20);
220 SetOutputDimensions(40, 40);
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);
228 EXPECT_CALL(mock_stub_,
229 InjectTouchEvent(EqualsTouchPointCoordinates(expected_out)));
230 InjectTestTouchEvent();
233 // Verify down scaling works. All coordinates should fall inside the output
234 // dimensions after scaling.
235 TEST_F(TouchInputScalerTest, DownScalingNoClamping) {
236 SetInputDimensions(40, 40);
237 SetOutputDimensions(20, 20);
239 AddInputCoordinate({2.0f, 2.0f});
240 TouchEvent expected_out;
241 TouchEventPoint* point = expected_out.add_touch_points();
242 point->set_x(1.0f);
243 point->set_y(1.0f);
245 AddInputCoordinate({6.0f, 3.0f});
246 point = expected_out.add_touch_points();
247 point->set_x(3.0);
248 point->set_y(1.5f);
250 EXPECT_CALL(mock_stub_,
251 InjectTouchEvent(EqualsTouchPointCoordinates(expected_out)));
252 InjectTestTouchEvent();
255 // Verify down scaling works with clamping.
256 TEST_F(TouchInputScalerTest, DownScalingWithClamping) {
257 SetInputDimensions(40, 40);
258 SetOutputDimensions(20, 20);
260 AddInputCoordinate({-20.0f, 10.0f});
261 TouchEvent expected_out;
262 TouchEventPoint* point = expected_out.add_touch_points();
263 point->set_x(0.0f);
264 point->set_y(5.0f);
266 AddInputCoordinate({10.0f, -20.0f});
267 point = expected_out.add_touch_points();
268 point->set_x(5.0f);
269 point->set_y(0.0f);
271 AddInputCoordinate({6.0f, 80.0f});
272 point = expected_out.add_touch_points();
273 point->set_x(3.0f);
274 point->set_y(19.0f);
276 AddInputCoordinate({80.0f, 6.0f});
277 point = expected_out.add_touch_points();
278 point->set_x(19.0f);
279 point->set_y(3.0f);
281 EXPECT_CALL(mock_stub_,
282 InjectTouchEvent(EqualsTouchPointCoordinates(expected_out)));
283 InjectTestTouchEvent();
286 // Verify that the radii are up-scaled.
287 TEST_F(TouchInputScalerTest, UpScaleRadii) {
288 SetInputDimensions(20, 20);
289 SetOutputDimensions(40, 40);
291 AddInputCoordinate({0.0f, 0.0f, 1.0f, 2.0f});
292 TouchEvent expected_out;
293 TouchEventPoint* point = expected_out.add_touch_points();
294 point->set_radius_x(2.0f);
295 point->set_radius_y(4.0f);
297 EXPECT_CALL(mock_stub_,
298 InjectTouchEvent(EqualsTouchPointRadii(expected_out)));
299 InjectTestTouchEvent();
302 // Verify that the radii are down-scaled.
303 TEST_F(TouchInputScalerTest, DownScaleRadii) {
304 SetInputDimensions(20, 20);
305 SetOutputDimensions(10, 10);
307 AddInputCoordinate({0.0f, 0.0f, 5.0f, 4.0f});
308 TouchEvent expected_out;
309 TouchEventPoint* point = expected_out.add_touch_points();
310 point->set_radius_x(2.5f);
311 point->set_radius_y(2.0f);
313 EXPECT_CALL(mock_stub_,
314 InjectTouchEvent(EqualsTouchPointRadii(expected_out)));
315 InjectTestTouchEvent();
318 // Verify that up-scaling with clamping works for x,y coordinates and radii all
319 // work together.
320 TEST_F(TouchInputScalerTest, UpScaleCoordinatesAndRadii) {
321 SetInputDimensions(20, 20);
322 SetOutputDimensions(40, 40);
324 AddInputCoordinate({5.0f, 12.0f, 3.0f, 2.0f});
325 TouchEvent expected_out;
326 TouchEventPoint* point = expected_out.add_touch_points();
327 point->set_x(10.0f);
328 point->set_y(24.0f);
329 point->set_radius_x(6.0f);
330 point->set_radius_y(4.0f);
332 // Make sure clamping and scaling all work.
333 AddInputCoordinate({22.0f, -1.0f, 8.0f, 3.0f});
334 point = expected_out.add_touch_points();
335 point->set_x(39.0f);
336 point->set_y(0.0f);
337 point->set_radius_x(16.0f);
338 point->set_radius_y(6.0f);
340 EXPECT_CALL(mock_stub_,
341 InjectTouchEvent(AllOf(EqualsTouchPointCoordinates(expected_out),
342 EqualsTouchPointRadii(expected_out))));
343 InjectTestTouchEvent();
346 // Verify that down-scaling with clamping works for x,y coordinates and radii
347 // all work together.
348 TEST_F(TouchInputScalerTest, DownScaleCoordinatesAndRadii) {
349 SetInputDimensions(60, 60);
350 SetOutputDimensions(20, 20);
352 AddInputCoordinate({50.0f, 24.0f, 10.0f, 9.0f});
353 TouchEvent expected_out;
354 TouchEventPoint* point = expected_out.add_touch_points();
355 point->set_x(16.666f);
356 point->set_y(8.0f);
357 point->set_radius_x(3.333f);
358 point->set_radius_y(3.0f);
360 // Make sure clamping and scaling all work.
361 AddInputCoordinate({70.0f, 82.0f, 8.0f, 3.0f});
362 point = expected_out.add_touch_points();
363 point->set_x(19.0f);
364 point->set_y(19.0f);
365 point->set_radius_x(2.666f);
366 point->set_radius_y(1.0f);
368 EXPECT_CALL(mock_stub_,
369 InjectTouchEvent(AllOf(EqualsTouchPointCoordinates(expected_out),
370 EqualsTouchPointRadii(expected_out))));
371 InjectTestTouchEvent();
374 } // namespace remoting