1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <array>
18 #include <math.h>
19
20 #include <android-base/properties.h>
21 #include <attestation/HmacKeyManager.h>
22 #include <binder/Parcel.h>
23 #include <gtest/gtest.h>
24 #include <input/Input.h>
25
26 namespace android {
27
28 // Default display id.
29 static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
30
31 class BaseTest : public testing::Test {
32 protected:
33 static constexpr std::array<uint8_t, 32> HMAC = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
34 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
35 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
36 };
37
38 // --- PointerCoordsTest ---
39
40 class PointerCoordsTest : public BaseTest {
41 };
42
TEST_F(PointerCoordsTest,ClearSetsBitsToZero)43 TEST_F(PointerCoordsTest, ClearSetsBitsToZero) {
44 PointerCoords coords;
45 coords.clear();
46
47 ASSERT_EQ(0ULL, coords.bits);
48 }
49
TEST_F(PointerCoordsTest,AxisValues)50 TEST_F(PointerCoordsTest, AxisValues) {
51 PointerCoords coords;
52 coords.clear();
53
54 // Check invariants when no axes are present.
55 ASSERT_EQ(0, coords.getAxisValue(0))
56 << "getAxisValue should return zero because axis is not present";
57 ASSERT_EQ(0, coords.getAxisValue(1))
58 << "getAxisValue should return zero because axis is not present";
59
60 // Set first axis.
61 ASSERT_EQ(OK, coords.setAxisValue(1, 5));
62 ASSERT_EQ(5, coords.values[0]);
63 ASSERT_EQ(0x4000000000000000ULL, coords.bits);
64
65 ASSERT_EQ(0, coords.getAxisValue(0))
66 << "getAxisValue should return zero because axis is not present";
67 ASSERT_EQ(5, coords.getAxisValue(1))
68 << "getAxisValue should return value of axis";
69
70 // Set an axis with a higher id than all others. (appending value at the end)
71 ASSERT_EQ(OK, coords.setAxisValue(3, 2));
72 ASSERT_EQ(0x5000000000000000ULL, coords.bits);
73 ASSERT_EQ(5, coords.values[0]);
74 ASSERT_EQ(2, coords.values[1]);
75
76 ASSERT_EQ(0, coords.getAxisValue(0))
77 << "getAxisValue should return zero because axis is not present";
78 ASSERT_EQ(5, coords.getAxisValue(1))
79 << "getAxisValue should return value of axis";
80 ASSERT_EQ(0, coords.getAxisValue(2))
81 << "getAxisValue should return zero because axis is not present";
82 ASSERT_EQ(2, coords.getAxisValue(3))
83 << "getAxisValue should return value of axis";
84
85 // Set an axis with an id lower than all others. (prepending value at beginning)
86 ASSERT_EQ(OK, coords.setAxisValue(0, 4));
87 ASSERT_EQ(0xd000000000000000ULL, coords.bits);
88 ASSERT_EQ(4, coords.values[0]);
89 ASSERT_EQ(5, coords.values[1]);
90 ASSERT_EQ(2, coords.values[2]);
91
92 ASSERT_EQ(4, coords.getAxisValue(0))
93 << "getAxisValue should return value of axis";
94 ASSERT_EQ(5, coords.getAxisValue(1))
95 << "getAxisValue should return value of axis";
96 ASSERT_EQ(0, coords.getAxisValue(2))
97 << "getAxisValue should return zero because axis is not present";
98 ASSERT_EQ(2, coords.getAxisValue(3))
99 << "getAxisValue should return value of axis";
100
101 // Set an axis with an id between the others. (inserting value in the middle)
102 ASSERT_EQ(OK, coords.setAxisValue(2, 1));
103 ASSERT_EQ(0xf000000000000000ULL, coords.bits);
104 ASSERT_EQ(4, coords.values[0]);
105 ASSERT_EQ(5, coords.values[1]);
106 ASSERT_EQ(1, coords.values[2]);
107 ASSERT_EQ(2, coords.values[3]);
108
109 ASSERT_EQ(4, coords.getAxisValue(0))
110 << "getAxisValue should return value of axis";
111 ASSERT_EQ(5, coords.getAxisValue(1))
112 << "getAxisValue should return value of axis";
113 ASSERT_EQ(1, coords.getAxisValue(2))
114 << "getAxisValue should return value of axis";
115 ASSERT_EQ(2, coords.getAxisValue(3))
116 << "getAxisValue should return value of axis";
117
118 // Set an existing axis value in place.
119 ASSERT_EQ(OK, coords.setAxisValue(1, 6));
120 ASSERT_EQ(0xf000000000000000ULL, coords.bits);
121 ASSERT_EQ(4, coords.values[0]);
122 ASSERT_EQ(6, coords.values[1]);
123 ASSERT_EQ(1, coords.values[2]);
124 ASSERT_EQ(2, coords.values[3]);
125
126 ASSERT_EQ(4, coords.getAxisValue(0))
127 << "getAxisValue should return value of axis";
128 ASSERT_EQ(6, coords.getAxisValue(1))
129 << "getAxisValue should return value of axis";
130 ASSERT_EQ(1, coords.getAxisValue(2))
131 << "getAxisValue should return value of axis";
132 ASSERT_EQ(2, coords.getAxisValue(3))
133 << "getAxisValue should return value of axis";
134
135 // Set maximum number of axes.
136 for (size_t axis = 4; axis < PointerCoords::MAX_AXES; axis++) {
137 ASSERT_EQ(OK, coords.setAxisValue(axis, axis));
138 }
139 ASSERT_EQ(PointerCoords::MAX_AXES, __builtin_popcountll(coords.bits));
140
141 // Try to set one more axis beyond maximum number.
142 // Ensure bits are unchanged.
143 ASSERT_EQ(NO_MEMORY, coords.setAxisValue(PointerCoords::MAX_AXES, 100));
144 ASSERT_EQ(PointerCoords::MAX_AXES, __builtin_popcountll(coords.bits));
145 }
146
TEST_F(PointerCoordsTest,Parcel)147 TEST_F(PointerCoordsTest, Parcel) {
148 Parcel parcel;
149
150 PointerCoords inCoords;
151 inCoords.clear();
152 PointerCoords outCoords;
153
154 // Round trip with empty coords.
155 inCoords.writeToParcel(&parcel);
156 parcel.setDataPosition(0);
157 outCoords.readFromParcel(&parcel);
158
159 ASSERT_EQ(0ULL, outCoords.bits);
160
161 // Round trip with some values.
162 parcel.freeData();
163 inCoords.setAxisValue(2, 5);
164 inCoords.setAxisValue(5, 8);
165
166 inCoords.writeToParcel(&parcel);
167 parcel.setDataPosition(0);
168 outCoords.readFromParcel(&parcel);
169
170 ASSERT_EQ(outCoords.bits, inCoords.bits);
171 ASSERT_EQ(outCoords.values[0], inCoords.values[0]);
172 ASSERT_EQ(outCoords.values[1], inCoords.values[1]);
173 }
174
175
176 // --- KeyEventTest ---
177
178 class KeyEventTest : public BaseTest {
179 };
180
TEST_F(KeyEventTest,Properties)181 TEST_F(KeyEventTest, Properties) {
182 KeyEvent event;
183
184 // Initialize and get properties.
185 constexpr nsecs_t ARBITRARY_DOWN_TIME = 1;
186 constexpr nsecs_t ARBITRARY_EVENT_TIME = 2;
187 const int32_t id = InputEvent::nextId();
188 event.initialize(id, 2, AINPUT_SOURCE_GAMEPAD, DISPLAY_ID, HMAC, AKEY_EVENT_ACTION_DOWN,
189 AKEY_EVENT_FLAG_FROM_SYSTEM, AKEYCODE_BUTTON_X, 121, AMETA_ALT_ON, 1,
190 ARBITRARY_DOWN_TIME, ARBITRARY_EVENT_TIME);
191
192 ASSERT_EQ(id, event.getId());
193 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event.getType());
194 ASSERT_EQ(2, event.getDeviceId());
195 ASSERT_EQ(AINPUT_SOURCE_GAMEPAD, event.getSource());
196 ASSERT_EQ(DISPLAY_ID, event.getDisplayId());
197 EXPECT_EQ(HMAC, event.getHmac());
198 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, event.getAction());
199 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, event.getFlags());
200 ASSERT_EQ(AKEYCODE_BUTTON_X, event.getKeyCode());
201 ASSERT_EQ(121, event.getScanCode());
202 ASSERT_EQ(AMETA_ALT_ON, event.getMetaState());
203 ASSERT_EQ(1, event.getRepeatCount());
204 ASSERT_EQ(ARBITRARY_DOWN_TIME, event.getDownTime());
205 ASSERT_EQ(ARBITRARY_EVENT_TIME, event.getEventTime());
206
207 // Set source.
208 event.setSource(AINPUT_SOURCE_JOYSTICK);
209 ASSERT_EQ(AINPUT_SOURCE_JOYSTICK, event.getSource());
210
211 // Set display id.
212 constexpr int32_t newDisplayId = 2;
213 event.setDisplayId(newDisplayId);
214 ASSERT_EQ(newDisplayId, event.getDisplayId());
215 }
216
217
218 // --- MotionEventTest ---
219
220 class MotionEventTest : public BaseTest {
221 protected:
222 static constexpr nsecs_t ARBITRARY_DOWN_TIME = 1;
223 static constexpr nsecs_t ARBITRARY_EVENT_TIME = 2;
224 static constexpr float X_SCALE = 2.0;
225 static constexpr float Y_SCALE = 3.0;
226 static constexpr float X_OFFSET = 1;
227 static constexpr float Y_OFFSET = 1.1;
228
229 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
230
231 int32_t mId;
232 ui::Transform mTransform;
233
234 void SetUp() override;
235 void TearDown() override;
236
237 void initializeEventWithHistory(MotionEvent* event);
238 void assertEqualsEventWithHistory(const MotionEvent* event);
239 };
240
241 const std::optional<bool> MotionEventTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
242 !base::GetProperty("persist.debug.per_window_input_rotation", "").empty()
243 ? std::optional(base::GetBoolProperty("persist.debug.per_window_input_rotation", false))
244 : std::nullopt;
245
SetUp()246 void MotionEventTest::SetUp() {
247 // Ensure per_window_input_rotation is enabled.
248 base::SetProperty("persist.debug.per_window_input_rotation", "true");
249 }
250
TearDown()251 void MotionEventTest::TearDown() {
252 const auto val = INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE.has_value()
253 ? (*INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE ? "true" : "false")
254 : "";
255 base::SetProperty("persist.debug.per_window_input_rotation", val);
256 }
257
initializeEventWithHistory(MotionEvent * event)258 void MotionEventTest::initializeEventWithHistory(MotionEvent* event) {
259 mId = InputEvent::nextId();
260 mTransform.set({X_SCALE, 0, X_OFFSET, 0, Y_SCALE, Y_OFFSET, 0, 0, 1});
261
262 PointerProperties pointerProperties[2];
263 pointerProperties[0].clear();
264 pointerProperties[0].id = 1;
265 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
266 pointerProperties[1].clear();
267 pointerProperties[1].id = 2;
268 pointerProperties[1].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
269
270 PointerCoords pointerCoords[2];
271 pointerCoords[0].clear();
272 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 10);
273 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 11);
274 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 12);
275 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 13);
276 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 14);
277 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 15);
278 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 16);
279 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 17);
280 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 18);
281 pointerCoords[1].clear();
282 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_X, 20);
283 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_Y, 21);
284 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 22);
285 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 23);
286 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 24);
287 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 25);
288 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 26);
289 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 27);
290 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 28);
291 event->initialize(mId, 2, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, HMAC,
292 AMOTION_EVENT_ACTION_MOVE, 0, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED,
293 AMOTION_EVENT_EDGE_FLAG_TOP, AMETA_ALT_ON, AMOTION_EVENT_BUTTON_PRIMARY,
294 MotionClassification::NONE, mTransform, 2.0f, 2.1f,
295 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
296 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
297 ARBITRARY_DOWN_TIME, ARBITRARY_EVENT_TIME, 2, pointerProperties,
298 pointerCoords);
299
300 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 110);
301 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 111);
302 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 112);
303 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 113);
304 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 114);
305 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 115);
306 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 116);
307 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 117);
308 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 118);
309 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_X, 120);
310 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_Y, 121);
311 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 122);
312 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 123);
313 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 124);
314 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 125);
315 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 126);
316 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 127);
317 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 128);
318 event->addSample(ARBITRARY_EVENT_TIME + 1, pointerCoords);
319
320 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 210);
321 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 211);
322 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 212);
323 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 213);
324 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 214);
325 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 215);
326 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 216);
327 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 217);
328 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 218);
329 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_X, 220);
330 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_Y, 221);
331 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 222);
332 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 223);
333 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 224);
334 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 225);
335 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 226);
336 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 227);
337 pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 228);
338 event->addSample(ARBITRARY_EVENT_TIME + 2, pointerCoords);
339 }
340
assertEqualsEventWithHistory(const MotionEvent * event)341 void MotionEventTest::assertEqualsEventWithHistory(const MotionEvent* event) {
342 // Check properties.
343 ASSERT_EQ(mId, event->getId());
344 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
345 ASSERT_EQ(2, event->getDeviceId());
346 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, event->getSource());
347 ASSERT_EQ(DISPLAY_ID, event->getDisplayId());
348 EXPECT_EQ(HMAC, event->getHmac());
349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, event->getAction());
350 ASSERT_EQ(AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED, event->getFlags());
351 ASSERT_EQ(AMOTION_EVENT_EDGE_FLAG_TOP, event->getEdgeFlags());
352 ASSERT_EQ(AMETA_ALT_ON, event->getMetaState());
353 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, event->getButtonState());
354 ASSERT_EQ(MotionClassification::NONE, event->getClassification());
355 EXPECT_EQ(mTransform, event->getTransform());
356 ASSERT_EQ(X_OFFSET, event->getXOffset());
357 ASSERT_EQ(Y_OFFSET, event->getYOffset());
358 ASSERT_EQ(2.0f, event->getXPrecision());
359 ASSERT_EQ(2.1f, event->getYPrecision());
360 ASSERT_EQ(ARBITRARY_DOWN_TIME, event->getDownTime());
361
362 ASSERT_EQ(2U, event->getPointerCount());
363 ASSERT_EQ(1, event->getPointerId(0));
364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, event->getToolType(0));
365 ASSERT_EQ(2, event->getPointerId(1));
366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, event->getToolType(1));
367
368 ASSERT_EQ(2U, event->getHistorySize());
369
370 // Check data.
371 ASSERT_EQ(ARBITRARY_EVENT_TIME, event->getHistoricalEventTime(0));
372 ASSERT_EQ(ARBITRARY_EVENT_TIME + 1, event->getHistoricalEventTime(1));
373 ASSERT_EQ(ARBITRARY_EVENT_TIME + 2, event->getEventTime());
374
375 ASSERT_EQ(11, event->getHistoricalRawPointerCoords(0, 0)->
376 getAxisValue(AMOTION_EVENT_AXIS_Y));
377 ASSERT_EQ(21, event->getHistoricalRawPointerCoords(1, 0)->
378 getAxisValue(AMOTION_EVENT_AXIS_Y));
379 ASSERT_EQ(111, event->getHistoricalRawPointerCoords(0, 1)->
380 getAxisValue(AMOTION_EVENT_AXIS_Y));
381 ASSERT_EQ(121, event->getHistoricalRawPointerCoords(1, 1)->
382 getAxisValue(AMOTION_EVENT_AXIS_Y));
383 ASSERT_EQ(211, event->getRawPointerCoords(0)->
384 getAxisValue(AMOTION_EVENT_AXIS_Y));
385 ASSERT_EQ(221, event->getRawPointerCoords(1)->
386 getAxisValue(AMOTION_EVENT_AXIS_Y));
387
388 ASSERT_EQ(11, event->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_Y, 0, 0));
389 ASSERT_EQ(21, event->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_Y, 1, 0));
390 ASSERT_EQ(111, event->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_Y, 0, 1));
391 ASSERT_EQ(121, event->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_Y, 1, 1));
392 ASSERT_EQ(211, event->getRawAxisValue(AMOTION_EVENT_AXIS_Y, 0));
393 ASSERT_EQ(221, event->getRawAxisValue(AMOTION_EVENT_AXIS_Y, 1));
394
395 ASSERT_EQ(10, event->getHistoricalRawX(0, 0));
396 ASSERT_EQ(20, event->getHistoricalRawX(1, 0));
397 ASSERT_EQ(110, event->getHistoricalRawX(0, 1));
398 ASSERT_EQ(120, event->getHistoricalRawX(1, 1));
399 ASSERT_EQ(210, event->getRawX(0));
400 ASSERT_EQ(220, event->getRawX(1));
401
402 ASSERT_EQ(11, event->getHistoricalRawY(0, 0));
403 ASSERT_EQ(21, event->getHistoricalRawY(1, 0));
404 ASSERT_EQ(111, event->getHistoricalRawY(0, 1));
405 ASSERT_EQ(121, event->getHistoricalRawY(1, 1));
406 ASSERT_EQ(211, event->getRawY(0));
407 ASSERT_EQ(221, event->getRawY(1));
408
409 ASSERT_EQ(X_OFFSET + 10 * X_SCALE, event->getHistoricalX(0, 0));
410 ASSERT_EQ(X_OFFSET + 20 * X_SCALE, event->getHistoricalX(1, 0));
411 ASSERT_EQ(X_OFFSET + 110 * X_SCALE, event->getHistoricalX(0, 1));
412 ASSERT_EQ(X_OFFSET + 120 * X_SCALE, event->getHistoricalX(1, 1));
413 ASSERT_EQ(X_OFFSET + 210 * X_SCALE, event->getX(0));
414 ASSERT_EQ(X_OFFSET + 220 * X_SCALE, event->getX(1));
415
416 ASSERT_EQ(Y_OFFSET + 11 * Y_SCALE, event->getHistoricalY(0, 0));
417 ASSERT_EQ(Y_OFFSET + 21 * Y_SCALE, event->getHistoricalY(1, 0));
418 ASSERT_EQ(Y_OFFSET + 111 * Y_SCALE, event->getHistoricalY(0, 1));
419 ASSERT_EQ(Y_OFFSET + 121 * Y_SCALE, event->getHistoricalY(1, 1));
420 ASSERT_EQ(Y_OFFSET + 211 * Y_SCALE, event->getY(0));
421 ASSERT_EQ(Y_OFFSET + 221 * Y_SCALE, event->getY(1));
422
423 ASSERT_EQ(12, event->getHistoricalPressure(0, 0));
424 ASSERT_EQ(22, event->getHistoricalPressure(1, 0));
425 ASSERT_EQ(112, event->getHistoricalPressure(0, 1));
426 ASSERT_EQ(122, event->getHistoricalPressure(1, 1));
427 ASSERT_EQ(212, event->getPressure(0));
428 ASSERT_EQ(222, event->getPressure(1));
429
430 ASSERT_EQ(13, event->getHistoricalSize(0, 0));
431 ASSERT_EQ(23, event->getHistoricalSize(1, 0));
432 ASSERT_EQ(113, event->getHistoricalSize(0, 1));
433 ASSERT_EQ(123, event->getHistoricalSize(1, 1));
434 ASSERT_EQ(213, event->getSize(0));
435 ASSERT_EQ(223, event->getSize(1));
436
437 ASSERT_EQ(14, event->getHistoricalTouchMajor(0, 0));
438 ASSERT_EQ(24, event->getHistoricalTouchMajor(1, 0));
439 ASSERT_EQ(114, event->getHistoricalTouchMajor(0, 1));
440 ASSERT_EQ(124, event->getHistoricalTouchMajor(1, 1));
441 ASSERT_EQ(214, event->getTouchMajor(0));
442 ASSERT_EQ(224, event->getTouchMajor(1));
443
444 ASSERT_EQ(15, event->getHistoricalTouchMinor(0, 0));
445 ASSERT_EQ(25, event->getHistoricalTouchMinor(1, 0));
446 ASSERT_EQ(115, event->getHistoricalTouchMinor(0, 1));
447 ASSERT_EQ(125, event->getHistoricalTouchMinor(1, 1));
448 ASSERT_EQ(215, event->getTouchMinor(0));
449 ASSERT_EQ(225, event->getTouchMinor(1));
450
451 ASSERT_EQ(16, event->getHistoricalToolMajor(0, 0));
452 ASSERT_EQ(26, event->getHistoricalToolMajor(1, 0));
453 ASSERT_EQ(116, event->getHistoricalToolMajor(0, 1));
454 ASSERT_EQ(126, event->getHistoricalToolMajor(1, 1));
455 ASSERT_EQ(216, event->getToolMajor(0));
456 ASSERT_EQ(226, event->getToolMajor(1));
457
458 ASSERT_EQ(17, event->getHistoricalToolMinor(0, 0));
459 ASSERT_EQ(27, event->getHistoricalToolMinor(1, 0));
460 ASSERT_EQ(117, event->getHistoricalToolMinor(0, 1));
461 ASSERT_EQ(127, event->getHistoricalToolMinor(1, 1));
462 ASSERT_EQ(217, event->getToolMinor(0));
463 ASSERT_EQ(227, event->getToolMinor(1));
464
465 ASSERT_EQ(18, event->getHistoricalOrientation(0, 0));
466 ASSERT_EQ(28, event->getHistoricalOrientation(1, 0));
467 ASSERT_EQ(118, event->getHistoricalOrientation(0, 1));
468 ASSERT_EQ(128, event->getHistoricalOrientation(1, 1));
469 ASSERT_EQ(218, event->getOrientation(0));
470 ASSERT_EQ(228, event->getOrientation(1));
471 }
472
TEST_F(MotionEventTest,Properties)473 TEST_F(MotionEventTest, Properties) {
474 MotionEvent event;
475
476 // Initialize, add samples and check properties.
477 initializeEventWithHistory(&event);
478 ASSERT_NO_FATAL_FAILURE(assertEqualsEventWithHistory(&event));
479
480 // Set source.
481 event.setSource(AINPUT_SOURCE_JOYSTICK);
482 ASSERT_EQ(AINPUT_SOURCE_JOYSTICK, event.getSource());
483
484 // Set displayId.
485 constexpr int32_t newDisplayId = 2;
486 event.setDisplayId(newDisplayId);
487 ASSERT_EQ(newDisplayId, event.getDisplayId());
488
489 // Set action.
490 event.setAction(AMOTION_EVENT_ACTION_CANCEL);
491 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, event.getAction());
492
493 // Set meta state.
494 event.setMetaState(AMETA_CTRL_ON);
495 ASSERT_EQ(AMETA_CTRL_ON, event.getMetaState());
496 }
497
TEST_F(MotionEventTest,CopyFrom_KeepHistory)498 TEST_F(MotionEventTest, CopyFrom_KeepHistory) {
499 MotionEvent event;
500 initializeEventWithHistory(&event);
501
502 MotionEvent copy;
503 copy.copyFrom(&event, true /*keepHistory*/);
504
505 ASSERT_NO_FATAL_FAILURE(assertEqualsEventWithHistory(&event));
506 }
507
TEST_F(MotionEventTest,CopyFrom_DoNotKeepHistory)508 TEST_F(MotionEventTest, CopyFrom_DoNotKeepHistory) {
509 MotionEvent event;
510 initializeEventWithHistory(&event);
511
512 MotionEvent copy;
513 copy.copyFrom(&event, false /*keepHistory*/);
514
515 ASSERT_EQ(event.getPointerCount(), copy.getPointerCount());
516 ASSERT_EQ(0U, copy.getHistorySize());
517
518 ASSERT_EQ(event.getPointerId(0), copy.getPointerId(0));
519 ASSERT_EQ(event.getPointerId(1), copy.getPointerId(1));
520
521 ASSERT_EQ(event.getEventTime(), copy.getEventTime());
522
523 ASSERT_EQ(event.getX(0), copy.getX(0));
524 }
525
TEST_F(MotionEventTest,OffsetLocation)526 TEST_F(MotionEventTest, OffsetLocation) {
527 MotionEvent event;
528 initializeEventWithHistory(&event);
529
530 event.offsetLocation(5.0f, -2.0f);
531
532 ASSERT_EQ(X_OFFSET + 5.0f, event.getXOffset());
533 ASSERT_EQ(Y_OFFSET - 2.0f, event.getYOffset());
534 }
535
TEST_F(MotionEventTest,Scale)536 TEST_F(MotionEventTest, Scale) {
537 MotionEvent event;
538 initializeEventWithHistory(&event);
539
540 event.scale(2.0f);
541
542 ASSERT_EQ(X_OFFSET * 2, event.getXOffset());
543 ASSERT_EQ(Y_OFFSET * 2, event.getYOffset());
544
545 ASSERT_EQ(210 * 2, event.getRawX(0));
546 ASSERT_EQ(211 * 2, event.getRawY(0));
547 ASSERT_EQ((X_OFFSET + 210 * X_SCALE) * 2, event.getX(0));
548 ASSERT_EQ((Y_OFFSET + 211 * Y_SCALE) * 2, event.getY(0));
549 ASSERT_EQ(212, event.getPressure(0));
550 ASSERT_EQ(213, event.getSize(0));
551 ASSERT_EQ(214 * 2, event.getTouchMajor(0));
552 ASSERT_EQ(215 * 2, event.getTouchMinor(0));
553 ASSERT_EQ(216 * 2, event.getToolMajor(0));
554 ASSERT_EQ(217 * 2, event.getToolMinor(0));
555 ASSERT_EQ(218, event.getOrientation(0));
556 }
557
TEST_F(MotionEventTest,Parcel)558 TEST_F(MotionEventTest, Parcel) {
559 Parcel parcel;
560
561 MotionEvent inEvent;
562 initializeEventWithHistory(&inEvent);
563 MotionEvent outEvent;
564
565 // Round trip.
566 inEvent.writeToParcel(&parcel);
567 parcel.setDataPosition(0);
568 outEvent.readFromParcel(&parcel);
569
570 ASSERT_NO_FATAL_FAILURE(assertEqualsEventWithHistory(&outEvent));
571 }
572
setRotationMatrix(std::array<float,9> & matrix,float angle)573 static void setRotationMatrix(std::array<float, 9>& matrix, float angle) {
574 float sin = sinf(angle);
575 float cos = cosf(angle);
576 matrix[0] = cos;
577 matrix[1] = -sin;
578 matrix[2] = 0;
579 matrix[3] = sin;
580 matrix[4] = cos;
581 matrix[5] = 0;
582 matrix[6] = 0;
583 matrix[7] = 0;
584 matrix[8] = 1.0f;
585 }
586
TEST_F(MotionEventTest,Transform)587 TEST_F(MotionEventTest, Transform) {
588 // Generate some points on a circle.
589 // Each point 'i' is a point on a circle of radius ROTATION centered at (3,2) at an angle
590 // of ARC * i degrees clockwise relative to the Y axis.
591 // The geometrical representation is irrelevant to the test, it's just easy to generate
592 // and check rotation. We set the orientation to the same angle.
593 // Coordinate system: down is increasing Y, right is increasing X.
594 const float PI_180 = float(M_PI / 180);
595 const float RADIUS = 10;
596 const float ARC = 36;
597 const float ROTATION = ARC * 2;
598
599 const size_t pointerCount = 11;
600 PointerProperties pointerProperties[pointerCount];
601 PointerCoords pointerCoords[pointerCount];
602 for (size_t i = 0; i < pointerCount; i++) {
603 float angle = float(i * ARC * PI_180);
604 pointerProperties[i].clear();
605 pointerProperties[i].id = i;
606 pointerCoords[i].clear();
607 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, sinf(angle) * RADIUS + 3);
608 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, -cosf(angle) * RADIUS + 2);
609 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, angle);
610 }
611 MotionEvent event;
612 ui::Transform identityTransform;
613 event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_UNKNOWN, DISPLAY_ID,
614 INVALID_HMAC, AMOTION_EVENT_ACTION_MOVE, 0 /*actionButton*/, 0 /*flags*/,
615 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0 /*buttonState*/,
616 MotionClassification::NONE, identityTransform, 0 /*xPrecision*/,
617 0 /*yPrecision*/, 3 + RADIUS /*xCursorPosition*/, 2 /*yCursorPosition*/,
618 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
619 0 /*downTime*/, 0 /*eventTime*/, pointerCount, pointerProperties,
620 pointerCoords);
621 float originalRawX = 0 + 3;
622 float originalRawY = -RADIUS + 2;
623
624 // Check original raw X and Y assumption.
625 ASSERT_NEAR(originalRawX, event.getRawX(0), 0.001);
626 ASSERT_NEAR(originalRawY, event.getRawY(0), 0.001);
627
628 // Now translate the motion event so the circle's origin is at (0,0).
629 event.offsetLocation(-3, -2);
630
631 // Offsetting the location should preserve the raw X and Y of the first point.
632 ASSERT_NEAR(originalRawX, event.getRawX(0), 0.001);
633 ASSERT_NEAR(originalRawY, event.getRawY(0), 0.001);
634
635 // Apply a rotation about the origin by ROTATION degrees clockwise.
636 std::array<float, 9> matrix;
637 setRotationMatrix(matrix, ROTATION * PI_180);
638 event.transform(matrix);
639
640 // Check the points.
641 for (size_t i = 0; i < pointerCount; i++) {
642 float angle = float((i * ARC + ROTATION) * PI_180);
643 ASSERT_NEAR(sinf(angle) * RADIUS, event.getX(i), 0.001);
644 ASSERT_NEAR(-cosf(angle) * RADIUS, event.getY(i), 0.001);
645 ASSERT_NEAR(tanf(angle), tanf(event.getOrientation(i)), 0.1);
646 }
647
648 // Check cursor positions. The original cursor position is at (3 + RADIUS, 2), where the center
649 // of the circle is (3, 2), so the cursor position is to the right of the center of the circle.
650 // The choice of triangular functions in this test defines the angle of rotation clockwise
651 // relative to the y-axis. Therefore the cursor position's angle is 90 degrees. Here we swap the
652 // triangular function so that we don't have to add the 90 degrees.
653 ASSERT_NEAR(cosf(PI_180 * ROTATION) * RADIUS, event.getXCursorPosition(), 0.001);
654 ASSERT_NEAR(sinf(PI_180 * ROTATION) * RADIUS, event.getYCursorPosition(), 0.001);
655
656 // Applying the transformation should preserve the raw X and Y of the first point.
657 ASSERT_NEAR(originalRawX, event.getRawX(0), 0.001);
658 ASSERT_NEAR(originalRawY, event.getRawY(0), 0.001);
659 }
660
createTouchDownEvent(int x,int y,ui::Transform transform)661 MotionEvent createTouchDownEvent(int x, int y, ui::Transform transform) {
662 std::vector<PointerProperties> pointerProperties;
663 pointerProperties.push_back(PointerProperties{/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER});
664 std::vector<PointerCoords> pointerCoords;
665 pointerCoords.emplace_back().clear();
666 pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_X, x);
667 pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, y);
668 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
669 MotionEvent event;
670 event.initialize(InputEvent::nextId(), /* deviceId */ 1, AINPUT_SOURCE_TOUCHSCREEN,
671 /* displayId */ 0, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
672 /* actionButton */ 0, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
673 /* buttonState */ 0, MotionClassification::NONE, transform,
674 /* xPrecision */ 0, /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
675 AMOTION_EVENT_INVALID_CURSOR_POSITION, /* displayWidth */ 400,
676 /* displayHeight */ 800, eventTime, eventTime, pointerCoords.size(),
677 pointerProperties.data(), pointerCoords.data());
678 return event;
679 }
680
TEST_F(MotionEventTest,ApplyTransform)681 TEST_F(MotionEventTest, ApplyTransform) {
682 // Create a rotate-90 transform with an offset (like a window which isn't fullscreen).
683 ui::Transform identity;
684 ui::Transform xform(ui::Transform::ROT_90, 800, 400);
685 xform.set(xform.tx() + 20, xform.ty() + 40);
686 MotionEvent event = createTouchDownEvent(60, 100, xform);
687 ASSERT_EQ(700, event.getRawX(0));
688 ASSERT_EQ(60, event.getRawY(0));
689 ASSERT_NE(event.getRawX(0), event.getX(0));
690 ASSERT_NE(event.getRawY(0), event.getY(0));
691
692 MotionEvent changedEvent = createTouchDownEvent(60, 100, identity);
693 const std::array<float, 9> rowMajor{xform[0][0], xform[1][0], xform[2][0],
694 xform[0][1], xform[1][1], xform[2][1],
695 xform[0][2], xform[1][2], xform[2][2]};
696 changedEvent.applyTransform(rowMajor);
697
698 // transformContent effectively rotates the raw coordinates, so those should now include
699 // both rotation AND offset
700 ASSERT_EQ(720, changedEvent.getRawX(0));
701 ASSERT_EQ(100, changedEvent.getRawY(0));
702
703 // The transformed output should be the same then
704 ASSERT_NEAR(event.getX(0), changedEvent.getX(0), 0.001);
705 ASSERT_NEAR(event.getY(0), changedEvent.getY(0), 0.001);
706 }
707
TEST_F(MotionEventTest,RawCompatTransform)708 TEST_F(MotionEventTest, RawCompatTransform) {
709 {
710 // Make sure raw is raw regardless of transform translation.
711 ui::Transform xform;
712 xform.set(20, 40);
713 MotionEvent event = createTouchDownEvent(60, 100, xform);
714 ASSERT_EQ(60, event.getRawX(0));
715 ASSERT_EQ(100, event.getRawY(0));
716 ASSERT_NE(event.getRawX(0), event.getX(0));
717 ASSERT_NE(event.getRawY(0), event.getY(0));
718 }
719
720 // Next check that getRaw contains rotation (for compatibility) but otherwise is still
721 // "Screen-space". The following tests check all 3 rotations.
722 {
723 // Create a rotate-90 transform with an offset (like a window which isn't fullscreen).
724 ui::Transform xform(ui::Transform::ROT_90, 800, 400);
725 xform.set(xform.tx() + 20, xform.ty() + 40);
726 MotionEvent event = createTouchDownEvent(60, 100, xform);
727 ASSERT_EQ(700, event.getRawX(0));
728 ASSERT_EQ(60, event.getRawY(0));
729 ASSERT_NE(event.getRawX(0), event.getX(0));
730 ASSERT_NE(event.getRawY(0), event.getY(0));
731 }
732
733 {
734 // Same as above, but check rotate-180.
735 ui::Transform xform(ui::Transform::ROT_180, 400, 800);
736 xform.set(xform.tx() + 20, xform.ty() + 40);
737 MotionEvent event = createTouchDownEvent(60, 100, xform);
738 ASSERT_EQ(340, event.getRawX(0));
739 ASSERT_EQ(700, event.getRawY(0));
740 }
741
742 {
743 // Same as above, but check rotate-270.
744 ui::Transform xform(ui::Transform::ROT_270, 800, 400);
745 xform.set(xform.tx() + 20, xform.ty() + 40);
746 MotionEvent event = createTouchDownEvent(60, 100, xform);
747 ASSERT_EQ(100, event.getRawX(0));
748 ASSERT_EQ(340, event.getRawY(0));
749 }
750 }
751
TEST_F(MotionEventTest,Initialize_SetsClassification)752 TEST_F(MotionEventTest, Initialize_SetsClassification) {
753 std::array<MotionClassification, 3> classifications = {
754 MotionClassification::NONE,
755 MotionClassification::AMBIGUOUS_GESTURE,
756 MotionClassification::DEEP_PRESS,
757 };
758
759 MotionEvent event;
760 constexpr size_t pointerCount = 1;
761 PointerProperties pointerProperties[pointerCount];
762 PointerCoords pointerCoords[pointerCount];
763 for (size_t i = 0; i < pointerCount; i++) {
764 pointerProperties[i].clear();
765 pointerProperties[i].id = i;
766 pointerCoords[i].clear();
767 }
768
769 ui::Transform identityTransform;
770 for (MotionClassification classification : classifications) {
771 event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_TOUCHSCREEN,
772 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
773 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, classification,
774 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
775 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
776 AMOTION_EVENT_INVALID_DISPLAY_SIZE, 0 /*downTime*/, 0 /*eventTime*/,
777 pointerCount, pointerProperties, pointerCoords);
778 ASSERT_EQ(classification, event.getClassification());
779 }
780 }
781
TEST_F(MotionEventTest,Initialize_SetsCursorPosition)782 TEST_F(MotionEventTest, Initialize_SetsCursorPosition) {
783 MotionEvent event;
784 constexpr size_t pointerCount = 1;
785 PointerProperties pointerProperties[pointerCount];
786 PointerCoords pointerCoords[pointerCount];
787 for (size_t i = 0; i < pointerCount; i++) {
788 pointerProperties[i].clear();
789 pointerProperties[i].id = i;
790 pointerCoords[i].clear();
791 }
792
793 ui::Transform identityTransform;
794 event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_MOUSE, DISPLAY_ID,
795 INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, AMOTION_EVENT_EDGE_FLAG_NONE,
796 AMETA_NONE, 0, MotionClassification::NONE, identityTransform, 0, 0,
797 280 /*xCursorPosition*/, 540 /*yCursorPosition*/,
798 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
799 0 /*downTime*/, 0 /*eventTime*/, pointerCount, pointerProperties,
800 pointerCoords);
801 event.offsetLocation(20, 60);
802 ASSERT_EQ(280, event.getRawXCursorPosition());
803 ASSERT_EQ(540, event.getRawYCursorPosition());
804 ASSERT_EQ(300, event.getXCursorPosition());
805 ASSERT_EQ(600, event.getYCursorPosition());
806 }
807
TEST_F(MotionEventTest,SetCursorPosition)808 TEST_F(MotionEventTest, SetCursorPosition) {
809 MotionEvent event;
810 initializeEventWithHistory(&event);
811 event.setSource(AINPUT_SOURCE_MOUSE);
812
813 event.setCursorPosition(3, 4);
814 ASSERT_EQ(3, event.getXCursorPosition());
815 ASSERT_EQ(4, event.getYCursorPosition());
816 }
817
818 } // namespace android
819