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