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