blob: 84c5ad6dd90138b244e8f1036e28e7d0a57f76cc [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 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 "../InputReader.h"
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080018#include "TestInputListener.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Michael Wrightd02c5b62014-02-10 15:10:22 -080020#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080021#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <math.h>
23
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080024
Michael Wrightd02c5b62014-02-10 15:10:22 -080025namespace android {
26
27// An arbitrary time value.
28static const nsecs_t ARBITRARY_TIME = 1234;
29
30// Arbitrary display properties.
31static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070032static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080033static const int32_t DISPLAY_WIDTH = 480;
34static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070035static const int32_t VIRTUAL_DISPLAY_ID = 1;
36static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
37static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070038static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070039static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
41// Error tolerance for floating point assertions.
42static const float EPSILON = 0.001f;
43
44template<typename T>
45static inline T min(T a, T b) {
46 return a < b ? a : b;
47}
48
49static inline float avg(float x, float y) {
50 return (x + y) / 2;
51}
52
53
54// --- FakePointerController ---
55
56class FakePointerController : public PointerControllerInterface {
57 bool mHaveBounds;
58 float mMinX, mMinY, mMaxX, mMaxY;
59 float mX, mY;
60 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080061 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
63protected:
64 virtual ~FakePointerController() { }
65
66public:
67 FakePointerController() :
68 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080069 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080070 }
71
72 void setBounds(float minX, float minY, float maxX, float maxY) {
73 mHaveBounds = true;
74 mMinX = minX;
75 mMinY = minY;
76 mMaxX = maxX;
77 mMaxY = maxY;
78 }
79
Arthur Hungc7ad2d02018-12-18 17:41:29 +080080 void setDisplayId(int32_t displayId) {
81 mDisplayId = displayId;
82 }
83
Michael Wrightd02c5b62014-02-10 15:10:22 -080084 virtual void setPosition(float x, float y) {
85 mX = x;
86 mY = y;
87 }
88
89 virtual void setButtonState(int32_t buttonState) {
90 mButtonState = buttonState;
91 }
92
93 virtual int32_t getButtonState() const {
94 return mButtonState;
95 }
96
97 virtual void getPosition(float* outX, float* outY) const {
98 *outX = mX;
99 *outY = mY;
100 }
101
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800102 virtual int32_t getDisplayId() const {
103 return mDisplayId;
104 }
105
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106private:
107 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
108 *outMinX = mMinX;
109 *outMinY = mMinY;
110 *outMaxX = mMaxX;
111 *outMaxY = mMaxY;
112 return mHaveBounds;
113 }
114
115 virtual void move(float deltaX, float deltaY) {
116 mX += deltaX;
117 if (mX < mMinX) mX = mMinX;
118 if (mX > mMaxX) mX = mMaxX;
119 mY += deltaY;
120 if (mY < mMinY) mY = mMinY;
121 if (mY > mMaxY) mY = mMaxY;
122 }
123
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100124 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125 }
126
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100127 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 }
129
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100130 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 }
132
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100133 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134 }
135
136 virtual void clearSpots() {
137 }
138};
139
140
141// --- FakeInputReaderPolicy ---
142
143class FakeInputReaderPolicy : public InputReaderPolicyInterface {
144 InputReaderConfiguration mConfig;
145 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
146 Vector<InputDeviceInfo> mInputDevices;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100147 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700148 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149
150protected:
151 virtual ~FakeInputReaderPolicy() { }
152
153public:
154 FakeInputReaderPolicy() {
155 }
156
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700157 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100158 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100159 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700160 }
161
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700162 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
163 return mConfig.getDisplayViewportByUniqueId(uniqueId);
164 }
165 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
166 return mConfig.getDisplayViewportByType(type);
167 }
168
169 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
170 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700171 }
172
173 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700174 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
175 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700176 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700177 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700178 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100179 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 }
181
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100182 void addExcludedDeviceName(const std::string& deviceName) {
183 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 }
185
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700186 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
187 mConfig.portAssociations.insert({inputPort, displayPort});
188 }
189
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700190 void addDisabledDevice(int32_t deviceId) {
191 ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
192 bool currentlyEnabled = index < 0;
193 if (currentlyEnabled) {
194 mConfig.disabledDevices.add(deviceId);
195 }
196 }
197
198 void removeDisabledDevice(int32_t deviceId) {
199 ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
200 bool currentlyEnabled = index < 0;
201 if (!currentlyEnabled) {
202 mConfig.disabledDevices.remove(deviceId);
203 }
204 }
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
207 mPointerControllers.add(deviceId, controller);
208 }
209
210 const InputReaderConfiguration* getReaderConfiguration() const {
211 return &mConfig;
212 }
213
214 const Vector<InputDeviceInfo>& getInputDevices() const {
215 return mInputDevices;
216 }
217
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100218 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700219 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700220 return transform;
221 }
222
223 void setTouchAffineTransformation(const TouchAffineTransformation t) {
224 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800225 }
226
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800227 void setPointerCapture(bool enabled) {
228 mConfig.pointerCapture = enabled;
229 }
230
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700232 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700233 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
234 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700235 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
236 || orientation == DISPLAY_ORIENTATION_270);
237 DisplayViewport v;
238 v.displayId = displayId;
239 v.orientation = orientation;
240 v.logicalLeft = 0;
241 v.logicalTop = 0;
242 v.logicalRight = isRotated ? height : width;
243 v.logicalBottom = isRotated ? width : height;
244 v.physicalLeft = 0;
245 v.physicalTop = 0;
246 v.physicalRight = isRotated ? height : width;
247 v.physicalBottom = isRotated ? width : height;
248 v.deviceWidth = isRotated ? height : width;
249 v.deviceHeight = isRotated ? width : height;
250 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700251 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100252 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700253 return v;
254 }
255
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
257 *outConfig = mConfig;
258 }
259
260 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
261 return mPointerControllers.valueFor(deviceId);
262 }
263
264 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
265 mInputDevices = inputDevices;
266 }
267
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100268 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700269 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 }
271
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100272 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
273 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274 }
275};
276
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277// --- FakeEventHub ---
278
279class FakeEventHub : public EventHubInterface {
280 struct KeyInfo {
281 int32_t keyCode;
282 uint32_t flags;
283 };
284
285 struct Device {
286 InputDeviceIdentifier identifier;
287 uint32_t classes;
288 PropertyMap configuration;
289 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
290 KeyedVector<int, bool> relativeAxes;
291 KeyedVector<int32_t, int32_t> keyCodeStates;
292 KeyedVector<int32_t, int32_t> scanCodeStates;
293 KeyedVector<int32_t, int32_t> switchStates;
294 KeyedVector<int32_t, int32_t> absoluteAxisValue;
295 KeyedVector<int32_t, KeyInfo> keysByScanCode;
296 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
297 KeyedVector<int32_t, bool> leds;
298 Vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700299 bool enabled;
300
301 status_t enable() {
302 enabled = true;
303 return OK;
304 }
305
306 status_t disable() {
307 enabled = false;
308 return OK;
309 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700311 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700312 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 }
314 };
315
316 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100317 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 List<RawEvent> mEvents;
319
320protected:
321 virtual ~FakeEventHub() {
322 for (size_t i = 0; i < mDevices.size(); i++) {
323 delete mDevices.valueAt(i);
324 }
325 }
326
327public:
328 FakeEventHub() { }
329
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100330 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 Device* device = new Device(classes);
332 device->identifier.name = name;
333 mDevices.add(deviceId, device);
334
335 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
336 }
337
338 void removeDevice(int32_t deviceId) {
339 delete mDevices.valueFor(deviceId);
340 mDevices.removeItem(deviceId);
341
342 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
343 }
344
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700345 bool isDeviceEnabled(int32_t deviceId) {
346 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700347 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700348 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
349 return false;
350 }
351 return device->enabled;
352 }
353
354 status_t enableDevice(int32_t deviceId) {
355 status_t result;
356 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700357 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700358 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
359 return BAD_VALUE;
360 }
361 if (device->enabled) {
362 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
363 return OK;
364 }
365 result = device->enable();
366 return result;
367 }
368
369 status_t disableDevice(int32_t deviceId) {
370 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700371 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700372 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
373 return BAD_VALUE;
374 }
375 if (!device->enabled) {
376 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
377 return OK;
378 }
379 return device->disable();
380 }
381
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 void finishDeviceScan() {
383 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
384 }
385
386 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
387 Device* device = getDevice(deviceId);
388 device->configuration.addProperty(key, value);
389 }
390
391 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
392 Device* device = getDevice(deviceId);
393 device->configuration.addAll(configuration);
394 }
395
396 void addAbsoluteAxis(int32_t deviceId, int axis,
397 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
398 Device* device = getDevice(deviceId);
399
400 RawAbsoluteAxisInfo info;
401 info.valid = true;
402 info.minValue = minValue;
403 info.maxValue = maxValue;
404 info.flat = flat;
405 info.fuzz = fuzz;
406 info.resolution = resolution;
407 device->absoluteAxes.add(axis, info);
408 }
409
410 void addRelativeAxis(int32_t deviceId, int32_t axis) {
411 Device* device = getDevice(deviceId);
412 device->relativeAxes.add(axis, true);
413 }
414
415 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
416 Device* device = getDevice(deviceId);
417 device->keyCodeStates.replaceValueFor(keyCode, state);
418 }
419
420 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
421 Device* device = getDevice(deviceId);
422 device->scanCodeStates.replaceValueFor(scanCode, state);
423 }
424
425 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
426 Device* device = getDevice(deviceId);
427 device->switchStates.replaceValueFor(switchCode, state);
428 }
429
430 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
431 Device* device = getDevice(deviceId);
432 device->absoluteAxisValue.replaceValueFor(axis, value);
433 }
434
435 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
436 int32_t keyCode, uint32_t flags) {
437 Device* device = getDevice(deviceId);
438 KeyInfo info;
439 info.keyCode = keyCode;
440 info.flags = flags;
441 if (scanCode) {
442 device->keysByScanCode.add(scanCode, info);
443 }
444 if (usageCode) {
445 device->keysByUsageCode.add(usageCode, info);
446 }
447 }
448
449 void addLed(int32_t deviceId, int32_t led, bool initialState) {
450 Device* device = getDevice(deviceId);
451 device->leds.add(led, initialState);
452 }
453
454 bool getLedState(int32_t deviceId, int32_t led) {
455 Device* device = getDevice(deviceId);
456 return device->leds.valueFor(led);
457 }
458
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100459 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 return mExcludedDevices;
461 }
462
463 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
464 Device* device = getDevice(deviceId);
465 device->virtualKeys.push(definition);
466 }
467
468 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
469 int32_t code, int32_t value) {
470 RawEvent event;
471 event.when = when;
472 event.deviceId = deviceId;
473 event.type = type;
474 event.code = code;
475 event.value = value;
476 mEvents.push_back(event);
477
478 if (type == EV_ABS) {
479 setAbsoluteAxisValue(deviceId, code, value);
480 }
481 }
482
483 void assertQueueIsEmpty() {
484 ASSERT_EQ(size_t(0), mEvents.size())
485 << "Expected the event queue to be empty (fully consumed).";
486 }
487
488private:
489 Device* getDevice(int32_t deviceId) const {
490 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100491 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492 }
493
494 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
495 Device* device = getDevice(deviceId);
496 return device ? device->classes : 0;
497 }
498
499 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
500 Device* device = getDevice(deviceId);
501 return device ? device->identifier : InputDeviceIdentifier();
502 }
503
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100504 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505 return 0;
506 }
507
508 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
509 Device* device = getDevice(deviceId);
510 if (device) {
511 *outConfiguration = device->configuration;
512 }
513 }
514
515 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
516 RawAbsoluteAxisInfo* outAxisInfo) const {
517 Device* device = getDevice(deviceId);
518 if (device) {
519 ssize_t index = device->absoluteAxes.indexOfKey(axis);
520 if (index >= 0) {
521 *outAxisInfo = device->absoluteAxes.valueAt(index);
522 return OK;
523 }
524 }
525 outAxisInfo->clear();
526 return -1;
527 }
528
529 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
530 Device* device = getDevice(deviceId);
531 if (device) {
532 return device->relativeAxes.indexOfKey(axis) >= 0;
533 }
534 return false;
535 }
536
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100537 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 return false;
539 }
540
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700541 virtual status_t mapKey(int32_t deviceId,
542 int32_t scanCode, int32_t usageCode, int32_t metaState,
543 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544 Device* device = getDevice(deviceId);
545 if (device) {
546 const KeyInfo* key = getKey(device, scanCode, usageCode);
547 if (key) {
548 if (outKeycode) {
549 *outKeycode = key->keyCode;
550 }
551 if (outFlags) {
552 *outFlags = key->flags;
553 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700554 if (outMetaState) {
555 *outMetaState = metaState;
556 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 return OK;
558 }
559 }
560 return NAME_NOT_FOUND;
561 }
562
563 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
564 if (usageCode) {
565 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
566 if (index >= 0) {
567 return &device->keysByUsageCode.valueAt(index);
568 }
569 }
570 if (scanCode) {
571 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
572 if (index >= 0) {
573 return &device->keysByScanCode.valueAt(index);
574 }
575 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700576 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100579 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 return NAME_NOT_FOUND;
581 }
582
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100583 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 mExcludedDevices = devices;
585 }
586
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100587 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 if (mEvents.empty()) {
589 return 0;
590 }
591
592 *buffer = *mEvents.begin();
593 mEvents.erase(mEvents.begin());
594 return 1;
595 }
596
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800597 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
598 return {};
599 }
600
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
602 Device* device = getDevice(deviceId);
603 if (device) {
604 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
605 if (index >= 0) {
606 return device->scanCodeStates.valueAt(index);
607 }
608 }
609 return AKEY_STATE_UNKNOWN;
610 }
611
612 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
613 Device* device = getDevice(deviceId);
614 if (device) {
615 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
616 if (index >= 0) {
617 return device->keyCodeStates.valueAt(index);
618 }
619 }
620 return AKEY_STATE_UNKNOWN;
621 }
622
623 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
624 Device* device = getDevice(deviceId);
625 if (device) {
626 ssize_t index = device->switchStates.indexOfKey(sw);
627 if (index >= 0) {
628 return device->switchStates.valueAt(index);
629 }
630 }
631 return AKEY_STATE_UNKNOWN;
632 }
633
634 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
635 int32_t* outValue) const {
636 Device* device = getDevice(deviceId);
637 if (device) {
638 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
639 if (index >= 0) {
640 *outValue = device->absoluteAxisValue.valueAt(index);
641 return OK;
642 }
643 }
644 *outValue = 0;
645 return -1;
646 }
647
648 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
649 uint8_t* outFlags) const {
650 bool result = false;
651 Device* device = getDevice(deviceId);
652 if (device) {
653 for (size_t i = 0; i < numCodes; i++) {
654 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
655 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
656 outFlags[i] = 1;
657 result = true;
658 }
659 }
660 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
661 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
662 outFlags[i] = 1;
663 result = true;
664 }
665 }
666 }
667 }
668 return result;
669 }
670
671 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
672 Device* device = getDevice(deviceId);
673 if (device) {
674 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
675 return index >= 0;
676 }
677 return false;
678 }
679
680 virtual bool hasLed(int32_t deviceId, int32_t led) const {
681 Device* device = getDevice(deviceId);
682 return device && device->leds.indexOfKey(led) >= 0;
683 }
684
685 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
686 Device* device = getDevice(deviceId);
687 if (device) {
688 ssize_t index = device->leds.indexOfKey(led);
689 if (index >= 0) {
690 device->leds.replaceValueAt(led, on);
691 } else {
692 ADD_FAILURE()
693 << "Attempted to set the state of an LED that the EventHub declared "
694 "was not present. led=" << led;
695 }
696 }
697 }
698
699 virtual void getVirtualKeyDefinitions(int32_t deviceId,
700 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
701 outVirtualKeys.clear();
702
703 Device* device = getDevice(deviceId);
704 if (device) {
705 outVirtualKeys.appendVector(device->virtualKeys);
706 }
707 }
708
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100709 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700710 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 }
712
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100713 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 return false;
715 }
716
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100717 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 }
719
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100720 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 }
722
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100723 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 return false;
725 }
726
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800727 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 }
729
730 virtual void monitor() {
731 }
732
733 virtual void requestReopenDevices() {
734 }
735
736 virtual void wake() {
737 }
738};
739
740
741// --- FakeInputReaderContext ---
742
743class FakeInputReaderContext : public InputReaderContext {
744 sp<EventHubInterface> mEventHub;
745 sp<InputReaderPolicyInterface> mPolicy;
746 sp<InputListenerInterface> mListener;
747 int32_t mGlobalMetaState;
748 bool mUpdateGlobalMetaStateWasCalled;
749 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800750 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751
752public:
753 FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
754 const sp<InputReaderPolicyInterface>& policy,
755 const sp<InputListenerInterface>& listener) :
756 mEventHub(eventHub), mPolicy(policy), mListener(listener),
Prabir Pradhan42611e02018-11-27 14:04:02 -0800757 mGlobalMetaState(0), mNextSequenceNum(1) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 }
759
760 virtual ~FakeInputReaderContext() { }
761
762 void assertUpdateGlobalMetaStateWasCalled() {
763 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
764 << "Expected updateGlobalMetaState() to have been called.";
765 mUpdateGlobalMetaStateWasCalled = false;
766 }
767
768 void setGlobalMetaState(int32_t state) {
769 mGlobalMetaState = state;
770 }
771
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800772 uint32_t getGeneration() {
773 return mGeneration;
774 }
775
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776private:
777 virtual void updateGlobalMetaState() {
778 mUpdateGlobalMetaStateWasCalled = true;
779 }
780
781 virtual int32_t getGlobalMetaState() {
782 return mGlobalMetaState;
783 }
784
785 virtual EventHubInterface* getEventHub() {
786 return mEventHub.get();
787 }
788
789 virtual InputReaderPolicyInterface* getPolicy() {
790 return mPolicy.get();
791 }
792
793 virtual InputListenerInterface* getListener() {
794 return mListener.get();
795 }
796
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100797 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 }
799
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100800 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801 return false;
802 }
803
804 virtual void fadePointer() {
805 }
806
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100807 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 }
809
810 virtual int32_t bumpGeneration() {
811 return ++mGeneration;
812 }
Michael Wright842500e2015-03-13 17:32:02 -0700813
814 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) {
815
816 }
817
818 virtual void dispatchExternalStylusState(const StylusState&) {
819
820 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800821
822 virtual uint32_t getNextSequenceNum() {
823 return mNextSequenceNum++;
824 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825};
826
827
828// --- FakeInputMapper ---
829
830class FakeInputMapper : public InputMapper {
831 uint32_t mSources;
832 int32_t mKeyboardType;
833 int32_t mMetaState;
834 KeyedVector<int32_t, int32_t> mKeyCodeStates;
835 KeyedVector<int32_t, int32_t> mScanCodeStates;
836 KeyedVector<int32_t, int32_t> mSwitchStates;
837 Vector<int32_t> mSupportedKeyCodes;
838 RawEvent mLastEvent;
839
840 bool mConfigureWasCalled;
841 bool mResetWasCalled;
842 bool mProcessWasCalled;
843
Arthur Hungc23540e2018-11-29 20:42:11 +0800844 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845public:
846 FakeInputMapper(InputDevice* device, uint32_t sources) :
847 InputMapper(device),
848 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
849 mMetaState(0),
850 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
851 }
852
853 virtual ~FakeInputMapper() { }
854
855 void setKeyboardType(int32_t keyboardType) {
856 mKeyboardType = keyboardType;
857 }
858
859 void setMetaState(int32_t metaState) {
860 mMetaState = metaState;
861 }
862
863 void assertConfigureWasCalled() {
864 ASSERT_TRUE(mConfigureWasCalled)
865 << "Expected configure() to have been called.";
866 mConfigureWasCalled = false;
867 }
868
869 void assertResetWasCalled() {
870 ASSERT_TRUE(mResetWasCalled)
871 << "Expected reset() to have been called.";
872 mResetWasCalled = false;
873 }
874
Yi Kong9b14ac62018-07-17 13:48:38 -0700875 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 ASSERT_TRUE(mProcessWasCalled)
877 << "Expected process() to have been called.";
878 if (outLastEvent) {
879 *outLastEvent = mLastEvent;
880 }
881 mProcessWasCalled = false;
882 }
883
884 void setKeyCodeState(int32_t keyCode, int32_t state) {
885 mKeyCodeStates.replaceValueFor(keyCode, state);
886 }
887
888 void setScanCodeState(int32_t scanCode, int32_t state) {
889 mScanCodeStates.replaceValueFor(scanCode, state);
890 }
891
892 void setSwitchState(int32_t switchCode, int32_t state) {
893 mSwitchStates.replaceValueFor(switchCode, state);
894 }
895
896 void addSupportedKeyCode(int32_t keyCode) {
897 mSupportedKeyCodes.add(keyCode);
898 }
899
900private:
901 virtual uint32_t getSources() {
902 return mSources;
903 }
904
905 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
906 InputMapper::populateDeviceInfo(deviceInfo);
907
908 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
909 deviceInfo->setKeyboardType(mKeyboardType);
910 }
911 }
912
Arthur Hungc23540e2018-11-29 20:42:11 +0800913 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800915
916 // Find the associated viewport if exist.
917 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
918 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
919 mViewport = config->getDisplayViewportByPort(*displayPort);
920 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 }
922
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100923 virtual void reset(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924 mResetWasCalled = true;
925 }
926
927 virtual void process(const RawEvent* rawEvent) {
928 mLastEvent = *rawEvent;
929 mProcessWasCalled = true;
930 }
931
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100932 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
934 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
935 }
936
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100937 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
939 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
940 }
941
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100942 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943 ssize_t index = mSwitchStates.indexOfKey(switchCode);
944 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
945 }
946
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100947 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948 const int32_t* keyCodes, uint8_t* outFlags) {
949 bool result = false;
950 for (size_t i = 0; i < numCodes; i++) {
951 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
952 if (keyCodes[i] == mSupportedKeyCodes[j]) {
953 outFlags[i] = 1;
954 result = true;
955 }
956 }
957 }
958 return result;
959 }
960
961 virtual int32_t getMetaState() {
962 return mMetaState;
963 }
964
965 virtual void fadePointer() {
966 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800967
968 virtual std::optional<int32_t> getAssociatedDisplay() {
969 if (mViewport) {
970 return std::make_optional(mViewport->displayId);
971 }
972 return std::nullopt;
973 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974};
975
976
977// --- InstrumentedInputReader ---
978
979class InstrumentedInputReader : public InputReader {
980 InputDevice* mNextDevice;
981
982public:
983 InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
984 const sp<InputReaderPolicyInterface>& policy,
985 const sp<InputListenerInterface>& listener) :
986 InputReader(eventHub, policy, listener),
Yi Kong9b14ac62018-07-17 13:48:38 -0700987 mNextDevice(nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800988 }
989
990 virtual ~InstrumentedInputReader() {
991 if (mNextDevice) {
992 delete mNextDevice;
993 }
994 }
995
996 void setNextDevice(InputDevice* device) {
997 mNextDevice = device;
998 }
999
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001000 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Arthur Hungc23540e2018-11-29 20:42:11 +08001001 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002 InputDeviceIdentifier identifier;
1003 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001004 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005 int32_t generation = deviceId + 1;
1006 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
1007 classes);
1008 }
1009
1010protected:
1011 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
1012 const InputDeviceIdentifier& identifier, uint32_t classes) {
1013 if (mNextDevice) {
1014 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001015 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016 return device;
1017 }
1018 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1019 }
1020
1021 friend class InputReaderTest;
1022};
1023
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001024// --- InputReaderPolicyTest ---
1025class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001026protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001027 sp<FakeInputReaderPolicy> mFakePolicy;
1028
1029 virtual void SetUp() {
1030 mFakePolicy = new FakeInputReaderPolicy();
1031 }
1032 virtual void TearDown() {
1033 mFakePolicy.clear();
1034 }
1035};
1036
1037/**
1038 * Check that empty set of viewports is an acceptable configuration.
1039 * Also try to get internal viewport two different ways - by type and by uniqueId.
1040 *
1041 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1042 * Such configuration is not currently allowed.
1043 */
1044TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001045 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001046
1047 // We didn't add any viewports yet, so there shouldn't be any.
1048 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001049 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001050 ASSERT_FALSE(internalViewport);
1051
1052 // Add an internal viewport, then clear it
1053 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001054 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001055
1056 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001057 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001058 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001059 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001060
1061 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001062 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001063 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001064 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001065
1066 mFakePolicy->clearViewports();
1067 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001068 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001069 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001070 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001071 ASSERT_FALSE(internalViewport);
1072}
1073
1074TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1075 const std::string internalUniqueId = "local:0";
1076 const std::string externalUniqueId = "local:1";
1077 const std::string virtualUniqueId1 = "virtual:2";
1078 const std::string virtualUniqueId2 = "virtual:3";
1079 constexpr int32_t virtualDisplayId1 = 2;
1080 constexpr int32_t virtualDisplayId2 = 3;
1081
1082 // Add an internal viewport
1083 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001084 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001085 // Add an external viewport
1086 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001087 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001088 // Add an virtual viewport
1089 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001090 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001091 // Add another virtual viewport
1092 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001093 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001094
1095 // Check matching by type for internal
1096 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001097 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001098 ASSERT_TRUE(internalViewport);
1099 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1100
1101 // Check matching by type for external
1102 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001103 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001104 ASSERT_TRUE(externalViewport);
1105 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1106
1107 // Check matching by uniqueId for virtual viewport #1
1108 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001109 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001110 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001111 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001112 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1113 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1114
1115 // Check matching by uniqueId for virtual viewport #2
1116 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001117 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001118 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001119 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001120 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1121 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1122}
1123
1124
1125/**
1126 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1127 * that lookup works by checking display id.
1128 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1129 */
1130TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1131 const std::string uniqueId1 = "uniqueId1";
1132 const std::string uniqueId2 = "uniqueId2";
1133 constexpr int32_t displayId1 = 2;
1134 constexpr int32_t displayId2 = 3;
1135
1136 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1137 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1138 for (const ViewportType& type : types) {
1139 mFakePolicy->clearViewports();
1140 // Add a viewport
1141 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001142 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001143 // Add another viewport
1144 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001145 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001146
1147 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001148 std::optional<DisplayViewport> viewport1 =
1149 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001150 ASSERT_TRUE(viewport1);
1151 ASSERT_EQ(displayId1, viewport1->displayId);
1152 ASSERT_EQ(type, viewport1->type);
1153
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001154 std::optional<DisplayViewport> viewport2 =
1155 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156 ASSERT_TRUE(viewport2);
1157 ASSERT_EQ(displayId2, viewport2->displayId);
1158 ASSERT_EQ(type, viewport2->type);
1159
1160 // When there are multiple viewports of the same kind, and uniqueId is not specified
1161 // in the call to getDisplayViewport, then that situation is not supported.
1162 // The viewports can be stored in any order, so we cannot rely on the order, since that
1163 // is just implementation detail.
1164 // However, we can check that it still returns *a* viewport, we just cannot assert
1165 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001166 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001167 ASSERT_TRUE(someViewport);
1168 }
1169}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001170
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001171/**
1172 * Check getDisplayViewportByPort
1173 */
1174TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1175 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1176 const std::string uniqueId1 = "uniqueId1";
1177 const std::string uniqueId2 = "uniqueId2";
1178 constexpr int32_t displayId1 = 1;
1179 constexpr int32_t displayId2 = 2;
1180 const uint8_t hdmi1 = 0;
1181 const uint8_t hdmi2 = 1;
1182 const uint8_t hdmi3 = 2;
1183
1184 mFakePolicy->clearViewports();
1185 // Add a viewport that's associated with some display port that's not of interest.
1186 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1187 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1188 // Add another viewport, connected to HDMI1 port
1189 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1190 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1191
1192 // Check that correct display viewport was returned by comparing the display ports.
1193 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1194 ASSERT_TRUE(hdmi1Viewport);
1195 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1196 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1197
1198 // Check that we can still get the same viewport using the uniqueId
1199 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1200 ASSERT_TRUE(hdmi1Viewport);
1201 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1202 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1203 ASSERT_EQ(type, hdmi1Viewport->type);
1204
1205 // Check that we cannot find a port with "HDMI2", because we never added one
1206 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1207 ASSERT_FALSE(hdmi2Viewport);
1208}
1209
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210// --- InputReaderTest ---
1211
1212class InputReaderTest : public testing::Test {
1213protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001214 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 sp<FakeInputReaderPolicy> mFakePolicy;
1216 sp<FakeEventHub> mFakeEventHub;
1217 sp<InstrumentedInputReader> mReader;
1218
1219 virtual void SetUp() {
1220 mFakeEventHub = new FakeEventHub();
1221 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001222 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001223
1224 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1225 }
1226
1227 virtual void TearDown() {
1228 mReader.clear();
1229
1230 mFakeListener.clear();
1231 mFakePolicy.clear();
1232 mFakeEventHub.clear();
1233 }
1234
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001235 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236 const PropertyMap* configuration) {
1237 mFakeEventHub->addDevice(deviceId, name, classes);
1238
1239 if (configuration) {
1240 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1241 }
1242 mFakeEventHub->finishDeviceScan();
1243 mReader->loopOnce();
1244 mReader->loopOnce();
1245 mFakeEventHub->assertQueueIsEmpty();
1246 }
1247
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001248 void disableDevice(int32_t deviceId, InputDevice* device) {
1249 mFakePolicy->addDisabledDevice(deviceId);
1250 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1251 }
1252
1253 void enableDevice(int32_t deviceId, InputDevice* device) {
1254 mFakePolicy->removeDisabledDevice(deviceId);
1255 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1256 }
1257
1258 void configureDevice(uint32_t changes, InputDevice* device) {
1259 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1260 }
1261
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001263 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001264 const PropertyMap* configuration) {
1265 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1266 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1267 device->addMapper(mapper);
1268 mReader->setNextDevice(device);
1269 addDevice(deviceId, name, classes, configuration);
1270 return mapper;
1271 }
1272};
1273
1274TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001275 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001276 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001277 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001278 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001280
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 Vector<InputDeviceInfo> inputDevices;
1282 mReader->getInputDevices(inputDevices);
1283
1284 ASSERT_EQ(1U, inputDevices.size());
1285 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001286 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001287 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1288 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1289 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1290
1291 // Should also have received a notification describing the new input devices.
1292 inputDevices = mFakePolicy->getInputDevices();
1293 ASSERT_EQ(1U, inputDevices.size());
1294 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001295 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001296 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1297 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1298 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1299}
1300
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001301TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1302 constexpr int32_t deviceId = 1;
1303 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001304 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001305 // Must add at least one mapper or the device will be ignored!
1306 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1307 device->addMapper(mapper);
1308 mReader->setNextDevice(device);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001309 addDevice(deviceId, "fake", deviceClass, nullptr);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001310
Yi Kong9b14ac62018-07-17 13:48:38 -07001311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001312
1313 NotifyDeviceResetArgs resetArgs;
1314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1315 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1316 ASSERT_EQ(deviceId, resetArgs.deviceId);
1317
1318 ASSERT_EQ(device->isEnabled(), true);
1319 disableDevice(deviceId, device);
1320 mReader->loopOnce();
1321
1322 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1323 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1324 ASSERT_EQ(deviceId, resetArgs.deviceId);
1325 ASSERT_EQ(device->isEnabled(), false);
1326
1327 disableDevice(deviceId, device);
1328 mReader->loopOnce();
1329 mFakeListener->assertNotifyDeviceResetWasNotCalled();
1330 mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1331 ASSERT_EQ(device->isEnabled(), false);
1332
1333 enableDevice(deviceId, device);
1334 mReader->loopOnce();
1335 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1336 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1337 ASSERT_EQ(deviceId, resetArgs.deviceId);
1338 ASSERT_EQ(device->isEnabled(), true);
1339}
1340
Michael Wrightd02c5b62014-02-10 15:10:22 -08001341TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001342 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001343 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001344 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001345 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1346
1347 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1348 AINPUT_SOURCE_ANY, AKEYCODE_A))
1349 << "Should return unknown when the device id is >= 0 but unknown.";
1350
1351 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1352 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1353 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1354
1355 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1356 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1357 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1358
1359 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1360 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1361 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1362
1363 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1364 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1365 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1366}
1367
1368TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001369 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001370 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001371 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001372 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1373
1374 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1375 AINPUT_SOURCE_ANY, KEY_A))
1376 << "Should return unknown when the device id is >= 0 but unknown.";
1377
1378 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1379 AINPUT_SOURCE_TRACKBALL, KEY_A))
1380 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1381
1382 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1383 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1384 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1385
1386 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1387 AINPUT_SOURCE_TRACKBALL, KEY_A))
1388 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1389
1390 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1391 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1392 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1393}
1394
1395TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001396 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001397 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001398 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1400
1401 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1402 AINPUT_SOURCE_ANY, SW_LID))
1403 << "Should return unknown when the device id is >= 0 but unknown.";
1404
1405 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1406 AINPUT_SOURCE_TRACKBALL, SW_LID))
1407 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1408
1409 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1410 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1411 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1412
1413 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1414 AINPUT_SOURCE_TRACKBALL, SW_LID))
1415 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1416
1417 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1418 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1419 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1420}
1421
1422TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001423 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001424 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001425 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001426
Michael Wrightd02c5b62014-02-10 15:10:22 -08001427 mapper->addSupportedKeyCode(AKEYCODE_A);
1428 mapper->addSupportedKeyCode(AKEYCODE_B);
1429
1430 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1431 uint8_t flags[4] = { 0, 0, 0, 1 };
1432
1433 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1434 << "Should return false when device id is >= 0 but unknown.";
1435 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1436
1437 flags[3] = 1;
1438 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1439 << "Should return false when device id is valid but the sources are not supported by the device.";
1440 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1441
1442 flags[3] = 1;
1443 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1444 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1445 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1446
1447 flags[3] = 1;
1448 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1449 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1450 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1451
1452 flags[3] = 1;
1453 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1454 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1455 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1456}
1457
1458TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001459 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001460
1461 NotifyConfigurationChangedArgs args;
1462
1463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1464 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1465}
1466
1467TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001468 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001469 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001470 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001471
1472 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1473 mReader->loopOnce();
1474 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1475
1476 RawEvent event;
1477 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1478 ASSERT_EQ(0, event.when);
1479 ASSERT_EQ(1, event.deviceId);
1480 ASSERT_EQ(EV_KEY, event.type);
1481 ASSERT_EQ(KEY_A, event.code);
1482 ASSERT_EQ(1, event.value);
1483}
1484
Prabir Pradhan42611e02018-11-27 14:04:02 -08001485TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1486 constexpr int32_t deviceId = 1;
1487 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001488 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001489 // Must add at least one mapper or the device will be ignored!
1490 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1491 device->addMapper(mapper);
1492 mReader->setNextDevice(device);
1493 addDevice(deviceId, "fake", deviceClass, nullptr);
1494
1495 NotifyDeviceResetArgs resetArgs;
1496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1497 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1498
1499 disableDevice(deviceId, device);
1500 mReader->loopOnce();
1501 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1502 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1503 prevSequenceNum = resetArgs.sequenceNum;
1504
1505 enableDevice(deviceId, device);
1506 mReader->loopOnce();
1507 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1508 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1509 prevSequenceNum = resetArgs.sequenceNum;
1510
1511 disableDevice(deviceId, device);
1512 mReader->loopOnce();
1513 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1514 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1515 prevSequenceNum = resetArgs.sequenceNum;
1516}
1517
Arthur Hungc23540e2018-11-29 20:42:11 +08001518TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1519 constexpr int32_t deviceId = 1;
1520 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1521 const char* DEVICE_LOCATION = "USB1";
1522 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1523 DEVICE_LOCATION);
1524 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1525 device->addMapper(mapper);
1526 mReader->setNextDevice(device);
1527 addDevice(deviceId, "fake", deviceClass, nullptr);
1528
1529 const uint8_t hdmi1 = 1;
1530
1531 // Associated touch screen with second display.
1532 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1533
1534 // Add default and second display.
1535 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1536 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1537 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1538 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1539 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1540 mReader->loopOnce();
1541
1542 // Check device.
1543 ASSERT_EQ(deviceId, device->getId());
1544 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1545 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
1546}
1547
Michael Wrightd02c5b62014-02-10 15:10:22 -08001548
1549// --- InputDeviceTest ---
1550
1551class InputDeviceTest : public testing::Test {
1552protected:
1553 static const char* DEVICE_NAME;
1554 static const int32_t DEVICE_ID;
1555 static const int32_t DEVICE_GENERATION;
1556 static const int32_t DEVICE_CONTROLLER_NUMBER;
1557 static const uint32_t DEVICE_CLASSES;
1558
1559 sp<FakeEventHub> mFakeEventHub;
1560 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001561 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562 FakeInputReaderContext* mFakeContext;
1563
1564 InputDevice* mDevice;
1565
1566 virtual void SetUp() {
1567 mFakeEventHub = new FakeEventHub();
1568 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001569 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1571
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001572 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 InputDeviceIdentifier identifier;
1574 identifier.name = DEVICE_NAME;
1575 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1576 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1577 }
1578
1579 virtual void TearDown() {
1580 delete mDevice;
1581
1582 delete mFakeContext;
1583 mFakeListener.clear();
1584 mFakePolicy.clear();
1585 mFakeEventHub.clear();
1586 }
1587};
1588
1589const char* InputDeviceTest::DEVICE_NAME = "device";
1590const int32_t InputDeviceTest::DEVICE_ID = 1;
1591const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1592const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1593const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1594 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1595
1596TEST_F(InputDeviceTest, ImmutableProperties) {
1597 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001598 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1600}
1601
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1603 ASSERT_EQ(mDevice->isEnabled(), true);
1604}
1605
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1607 // Configuration.
1608 InputReaderConfiguration config;
1609 mDevice->configure(ARBITRARY_TIME, &config, 0);
1610
1611 // Reset.
1612 mDevice->reset(ARBITRARY_TIME);
1613
1614 NotifyDeviceResetArgs resetArgs;
1615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1616 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1617 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1618
1619 // Metadata.
1620 ASSERT_TRUE(mDevice->isIgnored());
1621 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1622
1623 InputDeviceInfo info;
1624 mDevice->getDeviceInfo(&info);
1625 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001626 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001627 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1628 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1629
1630 // State queries.
1631 ASSERT_EQ(0, mDevice->getMetaState());
1632
1633 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1634 << "Ignored device should return unknown key code state.";
1635 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1636 << "Ignored device should return unknown scan code state.";
1637 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1638 << "Ignored device should return unknown switch state.";
1639
1640 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1641 uint8_t flags[2] = { 0, 1 };
1642 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1643 << "Ignored device should never mark any key codes.";
1644 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1645 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1646}
1647
1648TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1649 // Configuration.
1650 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1651
1652 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1653 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1654 mapper1->setMetaState(AMETA_ALT_ON);
1655 mapper1->addSupportedKeyCode(AKEYCODE_A);
1656 mapper1->addSupportedKeyCode(AKEYCODE_B);
1657 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1658 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1659 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1660 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1661 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1662 mDevice->addMapper(mapper1);
1663
1664 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1665 mapper2->setMetaState(AMETA_SHIFT_ON);
1666 mDevice->addMapper(mapper2);
1667
1668 InputReaderConfiguration config;
1669 mDevice->configure(ARBITRARY_TIME, &config, 0);
1670
1671 String8 propertyValue;
1672 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1673 << "Device should have read configuration during configuration phase.";
1674 ASSERT_STREQ("value", propertyValue.string());
1675
1676 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1677 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1678
1679 // Reset
1680 mDevice->reset(ARBITRARY_TIME);
1681 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1682 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1683
1684 NotifyDeviceResetArgs resetArgs;
1685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1686 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1687 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1688
1689 // Metadata.
1690 ASSERT_FALSE(mDevice->isIgnored());
1691 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1692
1693 InputDeviceInfo info;
1694 mDevice->getDeviceInfo(&info);
1695 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001696 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001697 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1698 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1699
1700 // State queries.
1701 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1702 << "Should query mappers and combine meta states.";
1703
1704 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1705 << "Should return unknown key code state when source not supported.";
1706 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1707 << "Should return unknown scan code state when source not supported.";
1708 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1709 << "Should return unknown switch state when source not supported.";
1710
1711 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1712 << "Should query mapper when source is supported.";
1713 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1714 << "Should query mapper when source is supported.";
1715 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1716 << "Should query mapper when source is supported.";
1717
1718 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1719 uint8_t flags[4] = { 0, 0, 0, 1 };
1720 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1721 << "Should do nothing when source is unsupported.";
1722 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1723 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1724 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1725 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1726
1727 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1728 << "Should query mapper when source is supported.";
1729 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1730 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1731 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1732 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1733
1734 // Event handling.
1735 RawEvent event;
1736 mDevice->process(&event, 1);
1737
1738 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1739 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1740}
1741
1742
1743// --- InputMapperTest ---
1744
1745class InputMapperTest : public testing::Test {
1746protected:
1747 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001748 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001749 static const int32_t DEVICE_ID;
1750 static const int32_t DEVICE_GENERATION;
1751 static const int32_t DEVICE_CONTROLLER_NUMBER;
1752 static const uint32_t DEVICE_CLASSES;
1753
1754 sp<FakeEventHub> mFakeEventHub;
1755 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001756 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001757 FakeInputReaderContext* mFakeContext;
1758 InputDevice* mDevice;
1759
1760 virtual void SetUp() {
1761 mFakeEventHub = new FakeEventHub();
1762 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001763 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001764 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1765 InputDeviceIdentifier identifier;
1766 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001767 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1769 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1770
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001771 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001772 }
1773
1774 virtual void TearDown() {
1775 delete mDevice;
1776 delete mFakeContext;
1777 mFakeListener.clear();
1778 mFakePolicy.clear();
1779 mFakeEventHub.clear();
1780 }
1781
1782 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001783 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784 }
1785
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001786 void configureDevice(uint32_t changes) {
1787 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1788 }
1789
Michael Wrightd02c5b62014-02-10 15:10:22 -08001790 void addMapperAndConfigure(InputMapper* mapper) {
1791 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001792 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001793 mDevice->reset(ARBITRARY_TIME);
1794 }
1795
1796 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001797 int32_t orientation, const std::string& uniqueId,
1798 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001799 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001800 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001801 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1802 }
1803
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001804 void clearViewports() {
1805 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001806 }
1807
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001808 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809 int32_t code, int32_t value) {
1810 RawEvent event;
1811 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001812 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001813 event.type = type;
1814 event.code = code;
1815 event.value = value;
1816 mapper->process(&event);
1817 }
1818
1819 static void assertMotionRange(const InputDeviceInfo& info,
1820 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1821 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001822 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001823 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1824 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1825 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1826 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1827 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1828 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1829 }
1830
1831 static void assertPointerCoords(const PointerCoords& coords,
1832 float x, float y, float pressure, float size,
1833 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1834 float orientation, float distance) {
1835 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1836 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1837 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1838 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1839 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1840 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1841 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1842 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1843 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1844 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1845 }
1846
1847 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1848 float actualX, actualY;
1849 controller->getPosition(&actualX, &actualY);
1850 ASSERT_NEAR(x, actualX, 1);
1851 ASSERT_NEAR(y, actualY, 1);
1852 }
1853};
1854
1855const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001856const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857const int32_t InputMapperTest::DEVICE_ID = 1;
1858const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1859const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1860const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1861
1862
1863// --- SwitchInputMapperTest ---
1864
1865class SwitchInputMapperTest : public InputMapperTest {
1866protected:
1867};
1868
1869TEST_F(SwitchInputMapperTest, GetSources) {
1870 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1871 addMapperAndConfigure(mapper);
1872
1873 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1874}
1875
1876TEST_F(SwitchInputMapperTest, GetSwitchState) {
1877 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1878 addMapperAndConfigure(mapper);
1879
1880 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1881 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1882
1883 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1884 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1885}
1886
1887TEST_F(SwitchInputMapperTest, Process) {
1888 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1889 addMapperAndConfigure(mapper);
1890
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001891 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
1892 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1893 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
1894 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001895
1896 NotifySwitchArgs args;
1897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1898 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08001899 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1900 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001901 args.switchMask);
1902 ASSERT_EQ(uint32_t(0), args.policyFlags);
1903}
1904
1905
1906// --- KeyboardInputMapperTest ---
1907
1908class KeyboardInputMapperTest : public InputMapperTest {
1909protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001910 const std::string UNIQUE_ID = "local:0";
1911
1912 void prepareDisplay(int32_t orientation);
1913
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914 void testDPadKeyRotation(KeyboardInputMapper* mapper,
1915 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1916};
1917
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001918/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
1919 * orientation.
1920 */
1921void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
1922 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001923 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001924}
1925
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001927 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001928 NotifyKeyArgs args;
1929
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001930 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1932 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1933 ASSERT_EQ(originalScanCode, args.scanCode);
1934 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1935
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001936 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1938 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1939 ASSERT_EQ(originalScanCode, args.scanCode);
1940 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1941}
1942
1943
1944TEST_F(KeyboardInputMapperTest, GetSources) {
1945 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1946 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1947 addMapperAndConfigure(mapper);
1948
1949 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1950}
1951
1952TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
1953 const int32_t USAGE_A = 0x070004;
1954 const int32_t USAGE_UNKNOWN = 0x07ffff;
1955 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
1956 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
1957
1958 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1959 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1960 addMapperAndConfigure(mapper);
1961
1962 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001963 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001964 NotifyKeyArgs args;
1965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1966 ASSERT_EQ(DEVICE_ID, args.deviceId);
1967 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1968 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1969 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1970 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1971 ASSERT_EQ(KEY_HOME, args.scanCode);
1972 ASSERT_EQ(AMETA_NONE, args.metaState);
1973 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1974 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1975 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1976
1977 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001978 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1980 ASSERT_EQ(DEVICE_ID, args.deviceId);
1981 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1982 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1983 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1984 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1985 ASSERT_EQ(KEY_HOME, args.scanCode);
1986 ASSERT_EQ(AMETA_NONE, args.metaState);
1987 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1988 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1989 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1990
1991 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001992 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
1993 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1995 ASSERT_EQ(DEVICE_ID, args.deviceId);
1996 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1997 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1998 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1999 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2000 ASSERT_EQ(0, args.scanCode);
2001 ASSERT_EQ(AMETA_NONE, args.metaState);
2002 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2003 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2004 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2005
2006 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002007 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2008 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2010 ASSERT_EQ(DEVICE_ID, args.deviceId);
2011 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2012 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2014 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2015 ASSERT_EQ(0, args.scanCode);
2016 ASSERT_EQ(AMETA_NONE, args.metaState);
2017 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2018 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2019 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2020
2021 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002022 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2023 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2025 ASSERT_EQ(DEVICE_ID, args.deviceId);
2026 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2027 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2028 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2029 ASSERT_EQ(0, args.keyCode);
2030 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2031 ASSERT_EQ(AMETA_NONE, args.metaState);
2032 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2033 ASSERT_EQ(0U, args.policyFlags);
2034 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2035
2036 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002037 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2038 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2040 ASSERT_EQ(DEVICE_ID, args.deviceId);
2041 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2042 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2043 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2044 ASSERT_EQ(0, args.keyCode);
2045 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2046 ASSERT_EQ(AMETA_NONE, args.metaState);
2047 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2048 ASSERT_EQ(0U, args.policyFlags);
2049 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2050}
2051
2052TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2053 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2054 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2055
2056 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2057 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2058 addMapperAndConfigure(mapper);
2059
2060 // Initial metastate.
2061 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2062
2063 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002064 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002065 NotifyKeyArgs args;
2066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2067 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2069 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2070
2071 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002072 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2074 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2076
2077 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002078 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2080 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2082
2083 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002084 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2086 ASSERT_EQ(AMETA_NONE, args.metaState);
2087 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2088 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2089}
2090
2091TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2092 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2093 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2094 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2095 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2096
2097 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2098 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2099 addMapperAndConfigure(mapper);
2100
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002101 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002102 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2103 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2104 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2105 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2106 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2107 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2108 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2109 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2110}
2111
2112TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2113 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2114 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2115 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2116 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2117
2118 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2119 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2120 addConfigurationProperty("keyboard.orientationAware", "1");
2121 addMapperAndConfigure(mapper);
2122
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002123 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002124 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2125 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2126 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2127 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2128 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2129 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2130 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2131 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2132
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002133 clearViewports();
2134 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002135 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2136 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
2137 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2138 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
2139 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2140 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
2141 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2142 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
2143
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002144 clearViewports();
2145 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002146 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2147 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
2148 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2149 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
2150 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2151 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
2152 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2153 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
2154
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002155 clearViewports();
2156 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002157 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2158 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
2159 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2160 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
2161 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2162 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
2163 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2164 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
2165
2166 // Special case: if orientation changes while key is down, we still emit the same keycode
2167 // in the key up as we did in the key down.
2168 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002169 clearViewports();
2170 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002171 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2173 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2174 ASSERT_EQ(KEY_UP, args.scanCode);
2175 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2176
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002177 clearViewports();
2178 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002179 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2181 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2182 ASSERT_EQ(KEY_UP, args.scanCode);
2183 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2184}
2185
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002186TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2187 // If the keyboard is not orientation aware,
2188 // key events should not be associated with a specific display id
2189 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2190
2191 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2192 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2193 addMapperAndConfigure(mapper);
2194 NotifyKeyArgs args;
2195
2196 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002197 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002199 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2201 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2202
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002203 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002204 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002206 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2208 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2209}
2210
2211TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2212 // If the keyboard is orientation aware,
2213 // key events should be associated with the internal viewport
2214 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2215
2216 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2217 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2218 addConfigurationProperty("keyboard.orientationAware", "1");
2219 addMapperAndConfigure(mapper);
2220 NotifyKeyArgs args;
2221
2222 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2223 // ^--- already checked by the previous test
2224
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002225 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002226 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002227 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002229 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2231 ASSERT_EQ(DISPLAY_ID, args.displayId);
2232
2233 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002234 clearViewports();
2235 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002236 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002237 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002239 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2241 ASSERT_EQ(newDisplayId, args.displayId);
2242}
2243
Michael Wrightd02c5b62014-02-10 15:10:22 -08002244TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2245 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2246 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2247 addMapperAndConfigure(mapper);
2248
2249 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2250 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2251
2252 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2253 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2254}
2255
2256TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2257 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2258 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2259 addMapperAndConfigure(mapper);
2260
2261 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2262 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2263
2264 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2265 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2266}
2267
2268TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2269 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2270 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2271 addMapperAndConfigure(mapper);
2272
2273 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2274
2275 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2276 uint8_t flags[2] = { 0, 0 };
2277 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2278 ASSERT_TRUE(flags[0]);
2279 ASSERT_FALSE(flags[1]);
2280}
2281
2282TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2283 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2284 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2285 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2286 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2287 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2288 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2289
2290 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2291 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2292 addMapperAndConfigure(mapper);
2293
2294 // Initialization should have turned all of the lights off.
2295 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2296 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2297 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2298
2299 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002300 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2301 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002302 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2303 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2304 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2305 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2306
2307 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002308 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2309 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002310 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2311 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2312 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2313 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2314
2315 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002316 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2317 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002318 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2319 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2320 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2321 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2322
2323 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002324 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2325 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002326 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2327 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2328 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2329 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2330
2331 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002332 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2333 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002334 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2335 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2336 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2337 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2338
2339 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002340 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2341 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002342 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2343 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2344 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2345 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2346}
2347
2348
2349// --- CursorInputMapperTest ---
2350
2351class CursorInputMapperTest : public InputMapperTest {
2352protected:
2353 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2354
2355 sp<FakePointerController> mFakePointerController;
2356
2357 virtual void SetUp() {
2358 InputMapperTest::SetUp();
2359
2360 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002361 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002362 }
2363
2364 void testMotionRotation(CursorInputMapper* mapper,
2365 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002366
2367 void prepareDisplay(int32_t orientation) {
2368 const std::string uniqueId = "local:0";
2369 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2370 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2371 orientation, uniqueId, NO_PORT, viewportType);
2372 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002373};
2374
2375const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2376
2377void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2378 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2379 NotifyMotionArgs args;
2380
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002381 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2382 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2383 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2387 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2388 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2389 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2390}
2391
2392TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2393 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2394 addConfigurationProperty("cursor.mode", "pointer");
2395 addMapperAndConfigure(mapper);
2396
2397 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2398}
2399
2400TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2401 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2402 addConfigurationProperty("cursor.mode", "navigation");
2403 addMapperAndConfigure(mapper);
2404
2405 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2406}
2407
2408TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2409 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2410 addConfigurationProperty("cursor.mode", "pointer");
2411 addMapperAndConfigure(mapper);
2412
2413 InputDeviceInfo info;
2414 mapper->populateDeviceInfo(&info);
2415
2416 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002417 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2418 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2420 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2421
2422 // When the bounds are set, then there should be a valid motion range.
2423 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2424
2425 InputDeviceInfo info2;
2426 mapper->populateDeviceInfo(&info2);
2427
2428 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2429 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2430 1, 800 - 1, 0.0f, 0.0f));
2431 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2432 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2433 2, 480 - 1, 0.0f, 0.0f));
2434 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2435 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2436 0.0f, 1.0f, 0.0f, 0.0f));
2437}
2438
2439TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2440 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2441 addConfigurationProperty("cursor.mode", "navigation");
2442 addMapperAndConfigure(mapper);
2443
2444 InputDeviceInfo info;
2445 mapper->populateDeviceInfo(&info);
2446
2447 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2448 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2449 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2450 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2451 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2452 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2453 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2454 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2455 0.0f, 1.0f, 0.0f, 0.0f));
2456}
2457
2458TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2459 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2460 addConfigurationProperty("cursor.mode", "navigation");
2461 addMapperAndConfigure(mapper);
2462
2463 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2464
2465 NotifyMotionArgs args;
2466
2467 // Button press.
2468 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002469 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2470 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2472 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2473 ASSERT_EQ(DEVICE_ID, args.deviceId);
2474 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2475 ASSERT_EQ(uint32_t(0), args.policyFlags);
2476 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2477 ASSERT_EQ(0, args.flags);
2478 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2479 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2480 ASSERT_EQ(0, args.edgeFlags);
2481 ASSERT_EQ(uint32_t(1), args.pointerCount);
2482 ASSERT_EQ(0, args.pointerProperties[0].id);
2483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2485 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2486 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2487 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2488 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2489
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2491 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2492 ASSERT_EQ(DEVICE_ID, args.deviceId);
2493 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2494 ASSERT_EQ(uint32_t(0), args.policyFlags);
2495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2496 ASSERT_EQ(0, args.flags);
2497 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2498 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2499 ASSERT_EQ(0, args.edgeFlags);
2500 ASSERT_EQ(uint32_t(1), args.pointerCount);
2501 ASSERT_EQ(0, args.pointerProperties[0].id);
2502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2504 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2505 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2506 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2507 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2508
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002510 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2511 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2513 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2514 ASSERT_EQ(DEVICE_ID, args.deviceId);
2515 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2516 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2518 ASSERT_EQ(0, args.flags);
2519 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2520 ASSERT_EQ(0, args.buttonState);
2521 ASSERT_EQ(0, args.edgeFlags);
2522 ASSERT_EQ(uint32_t(1), args.pointerCount);
2523 ASSERT_EQ(0, args.pointerProperties[0].id);
2524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2526 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2527 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2528 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2529 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2530
2531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2532 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2533 ASSERT_EQ(DEVICE_ID, args.deviceId);
2534 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2535 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002536 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2537 ASSERT_EQ(0, args.flags);
2538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2539 ASSERT_EQ(0, args.buttonState);
2540 ASSERT_EQ(0, args.edgeFlags);
2541 ASSERT_EQ(uint32_t(1), args.pointerCount);
2542 ASSERT_EQ(0, args.pointerProperties[0].id);
2543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2545 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2546 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2547 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2548 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2549}
2550
2551TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2552 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2553 addConfigurationProperty("cursor.mode", "navigation");
2554 addMapperAndConfigure(mapper);
2555
2556 NotifyMotionArgs args;
2557
2558 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002559 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2560 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2564 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2565
2566 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002567 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2568 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2572 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2573}
2574
2575TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2576 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2577 addConfigurationProperty("cursor.mode", "navigation");
2578 addMapperAndConfigure(mapper);
2579
2580 NotifyMotionArgs args;
2581
2582 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002583 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2584 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2586 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2588 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2589
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2591 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2593 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2594
Michael Wrightd02c5b62014-02-10 15:10:22 -08002595 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002596 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2597 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2601 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2602
2603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2606 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2607}
2608
2609TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2610 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2611 addConfigurationProperty("cursor.mode", "navigation");
2612 addMapperAndConfigure(mapper);
2613
2614 NotifyMotionArgs args;
2615
2616 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002617 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2618 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2619 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2620 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2624 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2625 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2626
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2628 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2630 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2631 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2632
Michael Wrightd02c5b62014-02-10 15:10:22 -08002633 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002634 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2635 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2636 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2640 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2641 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2642
2643 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002644 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2645 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2649 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2650
2651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2654 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2655}
2656
2657TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2658 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2659 addConfigurationProperty("cursor.mode", "navigation");
2660 addMapperAndConfigure(mapper);
2661
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002662 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2664 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2665 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2666 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2667 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2668 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2669 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2670 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2671}
2672
2673TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2674 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2675 addConfigurationProperty("cursor.mode", "navigation");
2676 addConfigurationProperty("cursor.orientationAware", "1");
2677 addMapperAndConfigure(mapper);
2678
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002679 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2681 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2682 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2683 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2684 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2685 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2686 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2687 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2688
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002689 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2691 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2692 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2693 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2694 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2695 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2696 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2697 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2698
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002699 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2701 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2702 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2703 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2704 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2705 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2706 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2707 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2708
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002709 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2711 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2712 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2713 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2714 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2715 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2716 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2717 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2718}
2719
2720TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2721 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2722 addConfigurationProperty("cursor.mode", "pointer");
2723 addMapperAndConfigure(mapper);
2724
2725 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2726 mFakePointerController->setPosition(100, 200);
2727 mFakePointerController->setButtonState(0);
2728
2729 NotifyMotionArgs motionArgs;
2730 NotifyKeyArgs keyArgs;
2731
2732 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002733 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2734 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2736 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2737 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2738 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2740 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2741
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2744 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2745 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2747 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2748
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002749 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2750 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753 ASSERT_EQ(0, motionArgs.buttonState);
2754 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2756 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2757
2758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002759 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760 ASSERT_EQ(0, motionArgs.buttonState);
2761 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2763 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2764
2765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002767 ASSERT_EQ(0, motionArgs.buttonState);
2768 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2770 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2771
2772 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002773 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2774 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2775 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2777 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2778 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2779 motionArgs.buttonState);
2780 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2781 mFakePointerController->getButtonState());
2782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2783 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2784
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2786 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2787 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2788 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2789 mFakePointerController->getButtonState());
2790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2791 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2792
2793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2795 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2796 motionArgs.buttonState);
2797 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2798 mFakePointerController->getButtonState());
2799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2800 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2801
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002802 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
2803 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002805 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2807 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2809 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2810
2811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002813 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2814 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2816 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2817
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002818 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2819 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2822 ASSERT_EQ(0, motionArgs.buttonState);
2823 ASSERT_EQ(0, mFakePointerController->getButtonState());
2824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2825 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002826 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2827 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002828
2829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830 ASSERT_EQ(0, motionArgs.buttonState);
2831 ASSERT_EQ(0, mFakePointerController->getButtonState());
2832 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2834 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002835
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2837 ASSERT_EQ(0, motionArgs.buttonState);
2838 ASSERT_EQ(0, mFakePointerController->getButtonState());
2839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2841 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2842
2843 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002844 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
2845 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2847 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2848 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002849
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002851 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2853 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2855 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2856
2857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2859 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2860 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2862 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2863
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002864 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
2865 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002867 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002868 ASSERT_EQ(0, motionArgs.buttonState);
2869 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2871 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2872
2873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002874 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002875 ASSERT_EQ(0, motionArgs.buttonState);
2876 ASSERT_EQ(0, mFakePointerController->getButtonState());
2877
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2879 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2881 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2882 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2883
2884 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002885 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
2886 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2888 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2889 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002890
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2894 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2896 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2897
2898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2899 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2900 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2901 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2903 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2904
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002905 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
2906 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002908 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909 ASSERT_EQ(0, motionArgs.buttonState);
2910 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2912 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002913
2914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2915 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2916 ASSERT_EQ(0, motionArgs.buttonState);
2917 ASSERT_EQ(0, mFakePointerController->getButtonState());
2918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2919 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2920
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2922 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2923 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2924
2925 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002926 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
2927 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2929 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2930 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002931
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002933 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2935 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2937 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2938
2939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2941 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2942 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2944 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2945
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002946 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
2947 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002949 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950 ASSERT_EQ(0, motionArgs.buttonState);
2951 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2953 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002954
2955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2956 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2957 ASSERT_EQ(0, motionArgs.buttonState);
2958 ASSERT_EQ(0, mFakePointerController->getButtonState());
2959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2960 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2961
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2963 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2964 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2965
2966 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002967 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
2968 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2970 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2971 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002972
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002974 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2976 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2978 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2979
2980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2982 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2983 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2985 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2986
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002987 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
2988 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002990 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991 ASSERT_EQ(0, motionArgs.buttonState);
2992 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2994 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002995
2996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2998 ASSERT_EQ(0, motionArgs.buttonState);
2999 ASSERT_EQ(0, mFakePointerController->getButtonState());
3000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3001 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3002
Michael Wrightd02c5b62014-02-10 15:10:22 -08003003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3004 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3005 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3006}
3007
3008TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3009 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3010 addConfigurationProperty("cursor.mode", "pointer");
3011 addMapperAndConfigure(mapper);
3012
3013 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3014 mFakePointerController->setPosition(100, 200);
3015 mFakePointerController->setButtonState(0);
3016
3017 NotifyMotionArgs args;
3018
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003019 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3020 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3021 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003023 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3024 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3026 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3027 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3028}
3029
3030TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3031 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3032 addConfigurationProperty("cursor.mode", "pointer");
3033 mFakePolicy->setPointerCapture(true);
3034 addMapperAndConfigure(mapper);
3035
3036 NotifyDeviceResetArgs resetArgs;
3037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3038 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3039 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3040
3041 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3042 mFakePointerController->setPosition(100, 200);
3043 mFakePointerController->setButtonState(0);
3044
3045 NotifyMotionArgs args;
3046
3047 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003048 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3049 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3050 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3052 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3055 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3056 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3057
3058 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003059 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3060 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3062 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3063 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3065 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3067 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3070 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3071
3072 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003073 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3074 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3076 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3079 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3081 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3082 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3084 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3085
3086 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003087 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3088 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3089 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3091 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3094 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3095 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3096
3097 // Disable pointer capture and check that the device generation got bumped
3098 // and events are generated the usual way.
3099 const uint32_t generation = mFakeContext->getGeneration();
3100 mFakePolicy->setPointerCapture(false);
3101 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3102 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3103
3104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3105 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3106 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3107
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003108 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3109 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3110 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3112 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3115 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3116 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3117}
3118
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003119TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3120 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3121 addMapperAndConfigure(mapper);
3122
3123 // Setup PointerController for second display.
3124 constexpr int32_t SECOND_DISPLAY_ID = 1;
3125 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3126 mFakePointerController->setPosition(100, 200);
3127 mFakePointerController->setButtonState(0);
3128 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3129
3130 NotifyMotionArgs args;
3131 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3132 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3133 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3135 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3136 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3138 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3139 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3140 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3141}
3142
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143
3144// --- TouchInputMapperTest ---
3145
3146class TouchInputMapperTest : public InputMapperTest {
3147protected:
3148 static const int32_t RAW_X_MIN;
3149 static const int32_t RAW_X_MAX;
3150 static const int32_t RAW_Y_MIN;
3151 static const int32_t RAW_Y_MAX;
3152 static const int32_t RAW_TOUCH_MIN;
3153 static const int32_t RAW_TOUCH_MAX;
3154 static const int32_t RAW_TOOL_MIN;
3155 static const int32_t RAW_TOOL_MAX;
3156 static const int32_t RAW_PRESSURE_MIN;
3157 static const int32_t RAW_PRESSURE_MAX;
3158 static const int32_t RAW_ORIENTATION_MIN;
3159 static const int32_t RAW_ORIENTATION_MAX;
3160 static const int32_t RAW_DISTANCE_MIN;
3161 static const int32_t RAW_DISTANCE_MAX;
3162 static const int32_t RAW_TILT_MIN;
3163 static const int32_t RAW_TILT_MAX;
3164 static const int32_t RAW_ID_MIN;
3165 static const int32_t RAW_ID_MAX;
3166 static const int32_t RAW_SLOT_MIN;
3167 static const int32_t RAW_SLOT_MAX;
3168 static const float X_PRECISION;
3169 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003170 static const float X_PRECISION_VIRTUAL;
3171 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172
3173 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003174 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175
3176 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3177
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003178 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003179 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003180
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181 enum Axes {
3182 POSITION = 1 << 0,
3183 TOUCH = 1 << 1,
3184 TOOL = 1 << 2,
3185 PRESSURE = 1 << 3,
3186 ORIENTATION = 1 << 4,
3187 MINOR = 1 << 5,
3188 ID = 1 << 6,
3189 DISTANCE = 1 << 7,
3190 TILT = 1 << 8,
3191 SLOT = 1 << 9,
3192 TOOL_TYPE = 1 << 10,
3193 };
3194
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003195 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3196 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003197 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003199 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 int32_t toRawX(float displayX);
3201 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003202 float toCookedX(float rawX, float rawY);
3203 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003205 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003207 float toDisplayY(int32_t rawY, int32_t displayHeight);
3208
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209};
3210
3211const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3212const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3213const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3214const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3215const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3216const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3217const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3218const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003219const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3220const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3222const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3223const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3224const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3225const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3226const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3227const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3228const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3229const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3230const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3231const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3232const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003233const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3234 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3235const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3236 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003237const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3238 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239
3240const float TouchInputMapperTest::GEOMETRIC_SCALE =
3241 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3242 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3243
3244const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3245 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3246 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3247};
3248
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003249void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003250 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003251 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3252}
3253
3254void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3255 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3256 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257}
3258
Santos Cordonfa5cf462017-04-05 10:37:00 -07003259void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003260 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3261 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003262 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003263}
3264
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265void TouchInputMapperTest::prepareVirtualKeys() {
3266 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3267 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3268 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3269 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3270}
3271
Jason Gerecke489fda82012-09-07 17:19:40 -07003272void TouchInputMapperTest::prepareLocationCalibration() {
3273 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3274}
3275
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276int32_t TouchInputMapperTest::toRawX(float displayX) {
3277 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3278}
3279
3280int32_t TouchInputMapperTest::toRawY(float displayY) {
3281 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3282}
3283
Jason Gerecke489fda82012-09-07 17:19:40 -07003284float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3285 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3286 return rawX;
3287}
3288
3289float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3290 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3291 return rawY;
3292}
3293
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003295 return toDisplayX(rawX, DISPLAY_WIDTH);
3296}
3297
3298float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3299 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300}
3301
3302float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003303 return toDisplayY(rawY, DISPLAY_HEIGHT);
3304}
3305
3306float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3307 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308}
3309
3310
3311// --- SingleTouchInputMapperTest ---
3312
3313class SingleTouchInputMapperTest : public TouchInputMapperTest {
3314protected:
3315 void prepareButtons();
3316 void prepareAxes(int axes);
3317
3318 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3319 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3320 void processUp(SingleTouchInputMapper* mappery);
3321 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3322 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3323 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3324 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3325 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3326 void processSync(SingleTouchInputMapper* mapper);
3327};
3328
3329void SingleTouchInputMapperTest::prepareButtons() {
3330 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3331}
3332
3333void SingleTouchInputMapperTest::prepareAxes(int axes) {
3334 if (axes & POSITION) {
3335 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3336 RAW_X_MIN, RAW_X_MAX, 0, 0);
3337 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3338 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3339 }
3340 if (axes & PRESSURE) {
3341 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3342 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3343 }
3344 if (axes & TOOL) {
3345 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3346 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3347 }
3348 if (axes & DISTANCE) {
3349 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3350 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3351 }
3352 if (axes & TILT) {
3353 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3354 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3355 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3356 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3357 }
3358}
3359
3360void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003361 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3362 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3363 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364}
3365
3366void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003367 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3368 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369}
3370
3371void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003372 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373}
3374
3375void SingleTouchInputMapperTest::processPressure(
3376 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003377 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378}
3379
3380void SingleTouchInputMapperTest::processToolMajor(
3381 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003382 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383}
3384
3385void SingleTouchInputMapperTest::processDistance(
3386 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003387 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388}
3389
3390void SingleTouchInputMapperTest::processTilt(
3391 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003392 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3393 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394}
3395
3396void SingleTouchInputMapperTest::processKey(
3397 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003398 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399}
3400
3401void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003402 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403}
3404
3405
3406TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3407 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3408 prepareButtons();
3409 prepareAxes(POSITION);
3410 addMapperAndConfigure(mapper);
3411
3412 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3413}
3414
3415TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3416 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3417 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3418 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3419 prepareButtons();
3420 prepareAxes(POSITION);
3421 addMapperAndConfigure(mapper);
3422
3423 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3424}
3425
3426TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3427 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3428 prepareButtons();
3429 prepareAxes(POSITION);
3430 addConfigurationProperty("touch.deviceType", "touchPad");
3431 addMapperAndConfigure(mapper);
3432
3433 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3434}
3435
3436TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3437 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3438 prepareButtons();
3439 prepareAxes(POSITION);
3440 addConfigurationProperty("touch.deviceType", "touchScreen");
3441 addMapperAndConfigure(mapper);
3442
3443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3444}
3445
3446TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3447 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3448 addConfigurationProperty("touch.deviceType", "touchScreen");
3449 prepareDisplay(DISPLAY_ORIENTATION_0);
3450 prepareButtons();
3451 prepareAxes(POSITION);
3452 prepareVirtualKeys();
3453 addMapperAndConfigure(mapper);
3454
3455 // Unknown key.
3456 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3457
3458 // Virtual key is down.
3459 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3460 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3461 processDown(mapper, x, y);
3462 processSync(mapper);
3463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3464
3465 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3466
3467 // Virtual key is up.
3468 processUp(mapper);
3469 processSync(mapper);
3470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3471
3472 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3473}
3474
3475TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3476 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3477 addConfigurationProperty("touch.deviceType", "touchScreen");
3478 prepareDisplay(DISPLAY_ORIENTATION_0);
3479 prepareButtons();
3480 prepareAxes(POSITION);
3481 prepareVirtualKeys();
3482 addMapperAndConfigure(mapper);
3483
3484 // Unknown key.
3485 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3486
3487 // Virtual key is down.
3488 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3489 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3490 processDown(mapper, x, y);
3491 processSync(mapper);
3492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3493
3494 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3495
3496 // Virtual key is up.
3497 processUp(mapper);
3498 processSync(mapper);
3499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3500
3501 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3502}
3503
3504TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3505 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3506 addConfigurationProperty("touch.deviceType", "touchScreen");
3507 prepareDisplay(DISPLAY_ORIENTATION_0);
3508 prepareButtons();
3509 prepareAxes(POSITION);
3510 prepareVirtualKeys();
3511 addMapperAndConfigure(mapper);
3512
3513 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3514 uint8_t flags[2] = { 0, 0 };
3515 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3516 ASSERT_TRUE(flags[0]);
3517 ASSERT_FALSE(flags[1]);
3518}
3519
3520TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3521 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3522 addConfigurationProperty("touch.deviceType", "touchScreen");
3523 prepareDisplay(DISPLAY_ORIENTATION_0);
3524 prepareButtons();
3525 prepareAxes(POSITION);
3526 prepareVirtualKeys();
3527 addMapperAndConfigure(mapper);
3528
3529 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3530
3531 NotifyKeyArgs args;
3532
3533 // Press virtual key.
3534 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3535 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3536 processDown(mapper, x, y);
3537 processSync(mapper);
3538
3539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3540 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3541 ASSERT_EQ(DEVICE_ID, args.deviceId);
3542 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3543 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3544 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3545 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3546 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3547 ASSERT_EQ(KEY_HOME, args.scanCode);
3548 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3549 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3550
3551 // Release virtual key.
3552 processUp(mapper);
3553 processSync(mapper);
3554
3555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3556 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3557 ASSERT_EQ(DEVICE_ID, args.deviceId);
3558 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3559 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3560 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3561 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3562 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3563 ASSERT_EQ(KEY_HOME, args.scanCode);
3564 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3565 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3566
3567 // Should not have sent any motions.
3568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3569}
3570
3571TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3572 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3573 addConfigurationProperty("touch.deviceType", "touchScreen");
3574 prepareDisplay(DISPLAY_ORIENTATION_0);
3575 prepareButtons();
3576 prepareAxes(POSITION);
3577 prepareVirtualKeys();
3578 addMapperAndConfigure(mapper);
3579
3580 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3581
3582 NotifyKeyArgs keyArgs;
3583
3584 // Press virtual key.
3585 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3586 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3587 processDown(mapper, x, y);
3588 processSync(mapper);
3589
3590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3591 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3592 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3593 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3594 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3595 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3596 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3597 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3598 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3599 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3600 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3601
3602 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3603 // into the display area.
3604 y -= 100;
3605 processMove(mapper, x, y);
3606 processSync(mapper);
3607
3608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3609 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3610 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3611 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3612 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3613 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3614 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3615 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3616 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3617 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3618 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3619 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3620
3621 NotifyMotionArgs motionArgs;
3622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3623 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3624 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3625 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3626 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3627 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3628 ASSERT_EQ(0, motionArgs.flags);
3629 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3630 ASSERT_EQ(0, motionArgs.buttonState);
3631 ASSERT_EQ(0, motionArgs.edgeFlags);
3632 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3636 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3637 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3638 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3639 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3640
3641 // Keep moving out of bounds. Should generate a pointer move.
3642 y -= 50;
3643 processMove(mapper, x, y);
3644 processSync(mapper);
3645
3646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3648 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3649 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3650 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3652 ASSERT_EQ(0, motionArgs.flags);
3653 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3654 ASSERT_EQ(0, motionArgs.buttonState);
3655 ASSERT_EQ(0, motionArgs.edgeFlags);
3656 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3660 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3661 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3662 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3663 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3664
3665 // Release out of bounds. Should generate a pointer up.
3666 processUp(mapper);
3667 processSync(mapper);
3668
3669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3670 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3671 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3672 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3673 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3674 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3675 ASSERT_EQ(0, motionArgs.flags);
3676 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3677 ASSERT_EQ(0, motionArgs.buttonState);
3678 ASSERT_EQ(0, motionArgs.edgeFlags);
3679 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3680 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3683 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3684 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3685 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3686 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3687
3688 // Should not have sent any more keys or motions.
3689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3691}
3692
3693TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3694 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3695 addConfigurationProperty("touch.deviceType", "touchScreen");
3696 prepareDisplay(DISPLAY_ORIENTATION_0);
3697 prepareButtons();
3698 prepareAxes(POSITION);
3699 prepareVirtualKeys();
3700 addMapperAndConfigure(mapper);
3701
3702 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3703
3704 NotifyMotionArgs motionArgs;
3705
3706 // Initially go down out of bounds.
3707 int32_t x = -10;
3708 int32_t y = -10;
3709 processDown(mapper, x, y);
3710 processSync(mapper);
3711
3712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3713
3714 // Move into the display area. Should generate a pointer down.
3715 x = 50;
3716 y = 75;
3717 processMove(mapper, x, y);
3718 processSync(mapper);
3719
3720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3721 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3722 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3723 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3724 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3726 ASSERT_EQ(0, motionArgs.flags);
3727 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3728 ASSERT_EQ(0, motionArgs.buttonState);
3729 ASSERT_EQ(0, motionArgs.edgeFlags);
3730 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3731 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3734 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3735 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3736 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3737 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3738
3739 // Release. Should generate a pointer up.
3740 processUp(mapper);
3741 processSync(mapper);
3742
3743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3747 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3748 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3749 ASSERT_EQ(0, motionArgs.flags);
3750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3751 ASSERT_EQ(0, motionArgs.buttonState);
3752 ASSERT_EQ(0, motionArgs.edgeFlags);
3753 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3757 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3758 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3759 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3761
3762 // Should not have sent any more keys or motions.
3763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3765}
3766
Santos Cordonfa5cf462017-04-05 10:37:00 -07003767TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3768 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3769 addConfigurationProperty("touch.deviceType", "touchScreen");
3770 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3771
3772 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3773 prepareButtons();
3774 prepareAxes(POSITION);
3775 prepareVirtualKeys();
3776 addMapperAndConfigure(mapper);
3777
3778 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3779
3780 NotifyMotionArgs motionArgs;
3781
3782 // Down.
3783 int32_t x = 100;
3784 int32_t y = 125;
3785 processDown(mapper, x, y);
3786 processSync(mapper);
3787
3788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3789 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3790 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3791 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3792 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3793 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3794 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3795 ASSERT_EQ(0, motionArgs.flags);
3796 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3797 ASSERT_EQ(0, motionArgs.buttonState);
3798 ASSERT_EQ(0, motionArgs.edgeFlags);
3799 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3803 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3804 1, 0, 0, 0, 0, 0, 0, 0));
3805 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3806 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3808
3809 // Move.
3810 x += 50;
3811 y += 75;
3812 processMove(mapper, x, y);
3813 processSync(mapper);
3814
3815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3817 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3818 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3819 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3820 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3822 ASSERT_EQ(0, motionArgs.flags);
3823 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3824 ASSERT_EQ(0, motionArgs.buttonState);
3825 ASSERT_EQ(0, motionArgs.edgeFlags);
3826 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3827 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3830 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3831 1, 0, 0, 0, 0, 0, 0, 0));
3832 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3833 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3834 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3835
3836 // Up.
3837 processUp(mapper);
3838 processSync(mapper);
3839
3840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3841 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3842 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3843 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3844 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3845 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3846 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3847 ASSERT_EQ(0, motionArgs.flags);
3848 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3849 ASSERT_EQ(0, motionArgs.buttonState);
3850 ASSERT_EQ(0, motionArgs.edgeFlags);
3851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3855 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3856 1, 0, 0, 0, 0, 0, 0, 0));
3857 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3858 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3859 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3860
3861 // Should not have sent any more keys or motions.
3862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3864}
3865
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
3867 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3868 addConfigurationProperty("touch.deviceType", "touchScreen");
3869 prepareDisplay(DISPLAY_ORIENTATION_0);
3870 prepareButtons();
3871 prepareAxes(POSITION);
3872 prepareVirtualKeys();
3873 addMapperAndConfigure(mapper);
3874
3875 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3876
3877 NotifyMotionArgs motionArgs;
3878
3879 // Down.
3880 int32_t x = 100;
3881 int32_t y = 125;
3882 processDown(mapper, x, y);
3883 processSync(mapper);
3884
3885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3886 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3887 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3888 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3889 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3890 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3891 ASSERT_EQ(0, motionArgs.flags);
3892 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3893 ASSERT_EQ(0, motionArgs.buttonState);
3894 ASSERT_EQ(0, motionArgs.edgeFlags);
3895 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3896 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3899 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3900 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3901 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3902 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3903
3904 // Move.
3905 x += 50;
3906 y += 75;
3907 processMove(mapper, x, y);
3908 processSync(mapper);
3909
3910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3911 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3912 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3913 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3914 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3916 ASSERT_EQ(0, motionArgs.flags);
3917 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3918 ASSERT_EQ(0, motionArgs.buttonState);
3919 ASSERT_EQ(0, motionArgs.edgeFlags);
3920 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3924 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3925 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3926 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3927 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3928
3929 // Up.
3930 processUp(mapper);
3931 processSync(mapper);
3932
3933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3934 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3935 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3936 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3937 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3938 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3939 ASSERT_EQ(0, motionArgs.flags);
3940 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3941 ASSERT_EQ(0, motionArgs.buttonState);
3942 ASSERT_EQ(0, motionArgs.edgeFlags);
3943 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3944 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3947 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3948 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3949 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3950 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3951
3952 // Should not have sent any more keys or motions.
3953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3955}
3956
3957TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3958 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3959 addConfigurationProperty("touch.deviceType", "touchScreen");
3960 prepareButtons();
3961 prepareAxes(POSITION);
3962 addConfigurationProperty("touch.orientationAware", "0");
3963 addMapperAndConfigure(mapper);
3964
3965 NotifyMotionArgs args;
3966
3967 // Rotation 90.
3968 prepareDisplay(DISPLAY_ORIENTATION_90);
3969 processDown(mapper, toRawX(50), toRawY(75));
3970 processSync(mapper);
3971
3972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3973 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3974 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3975
3976 processUp(mapper);
3977 processSync(mapper);
3978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3979}
3980
3981TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
3982 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3983 addConfigurationProperty("touch.deviceType", "touchScreen");
3984 prepareButtons();
3985 prepareAxes(POSITION);
3986 addMapperAndConfigure(mapper);
3987
3988 NotifyMotionArgs args;
3989
3990 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003991 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992 prepareDisplay(DISPLAY_ORIENTATION_0);
3993 processDown(mapper, toRawX(50), toRawY(75));
3994 processSync(mapper);
3995
3996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3997 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3998 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3999
4000 processUp(mapper);
4001 processSync(mapper);
4002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4003
4004 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004005 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006 prepareDisplay(DISPLAY_ORIENTATION_90);
4007 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4008 processSync(mapper);
4009
4010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4011 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4012 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4013
4014 processUp(mapper);
4015 processSync(mapper);
4016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4017
4018 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004019 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020 prepareDisplay(DISPLAY_ORIENTATION_180);
4021 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4022 processSync(mapper);
4023
4024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4025 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4026 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4027
4028 processUp(mapper);
4029 processSync(mapper);
4030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4031
4032 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004033 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 prepareDisplay(DISPLAY_ORIENTATION_270);
4035 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4036 processSync(mapper);
4037
4038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4039 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4040 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4041
4042 processUp(mapper);
4043 processSync(mapper);
4044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4045}
4046
4047TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4048 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4049 addConfigurationProperty("touch.deviceType", "touchScreen");
4050 prepareDisplay(DISPLAY_ORIENTATION_0);
4051 prepareButtons();
4052 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4053 addMapperAndConfigure(mapper);
4054
4055 // These calculations are based on the input device calibration documentation.
4056 int32_t rawX = 100;
4057 int32_t rawY = 200;
4058 int32_t rawPressure = 10;
4059 int32_t rawToolMajor = 12;
4060 int32_t rawDistance = 2;
4061 int32_t rawTiltX = 30;
4062 int32_t rawTiltY = 110;
4063
4064 float x = toDisplayX(rawX);
4065 float y = toDisplayY(rawY);
4066 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4067 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4068 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4069 float distance = float(rawDistance);
4070
4071 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4072 float tiltScale = M_PI / 180;
4073 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4074 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4075 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4076 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4077
4078 processDown(mapper, rawX, rawY);
4079 processPressure(mapper, rawPressure);
4080 processToolMajor(mapper, rawToolMajor);
4081 processDistance(mapper, rawDistance);
4082 processTilt(mapper, rawTiltX, rawTiltY);
4083 processSync(mapper);
4084
4085 NotifyMotionArgs args;
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4088 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4089 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4090}
4091
Jason Gerecke489fda82012-09-07 17:19:40 -07004092TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4093 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4094 addConfigurationProperty("touch.deviceType", "touchScreen");
4095 prepareDisplay(DISPLAY_ORIENTATION_0);
4096 prepareLocationCalibration();
4097 prepareButtons();
4098 prepareAxes(POSITION);
4099 addMapperAndConfigure(mapper);
4100
4101 int32_t rawX = 100;
4102 int32_t rawY = 200;
4103
4104 float x = toDisplayX(toCookedX(rawX, rawY));
4105 float y = toDisplayY(toCookedY(rawX, rawY));
4106
4107 processDown(mapper, rawX, rawY);
4108 processSync(mapper);
4109
4110 NotifyMotionArgs args;
4111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4113 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4114}
4115
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4117 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4118 addConfigurationProperty("touch.deviceType", "touchScreen");
4119 prepareDisplay(DISPLAY_ORIENTATION_0);
4120 prepareButtons();
4121 prepareAxes(POSITION);
4122 addMapperAndConfigure(mapper);
4123
4124 NotifyMotionArgs motionArgs;
4125 NotifyKeyArgs keyArgs;
4126
4127 processDown(mapper, 100, 200);
4128 processSync(mapper);
4129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4130 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4131 ASSERT_EQ(0, motionArgs.buttonState);
4132
4133 // press BTN_LEFT, release BTN_LEFT
4134 processKey(mapper, BTN_LEFT, 1);
4135 processSync(mapper);
4136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4138 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4139
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4142 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4143
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 processKey(mapper, BTN_LEFT, 0);
4145 processSync(mapper);
4146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004149
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004152 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153
4154 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4155 processKey(mapper, BTN_RIGHT, 1);
4156 processKey(mapper, BTN_MIDDLE, 1);
4157 processSync(mapper);
4158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4160 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4161 motionArgs.buttonState);
4162
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4164 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4165 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4166
4167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4168 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4169 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4170 motionArgs.buttonState);
4171
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 processKey(mapper, BTN_RIGHT, 0);
4173 processSync(mapper);
4174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004177
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004180 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181
4182 processKey(mapper, BTN_MIDDLE, 0);
4183 processSync(mapper);
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004187
4188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004190 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191
4192 // press BTN_BACK, release BTN_BACK
4193 processKey(mapper, BTN_BACK, 1);
4194 processSync(mapper);
4195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4196 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4197 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004198
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004201 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4202
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4205 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206
4207 processKey(mapper, BTN_BACK, 0);
4208 processSync(mapper);
4209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004212
4213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004215 ASSERT_EQ(0, motionArgs.buttonState);
4216
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4218 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4219 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4220
4221 // press BTN_SIDE, release BTN_SIDE
4222 processKey(mapper, BTN_SIDE, 1);
4223 processSync(mapper);
4224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4225 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4226 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004227
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004230 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4231
4232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4234 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235
4236 processKey(mapper, BTN_SIDE, 0);
4237 processSync(mapper);
4238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241
4242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004244 ASSERT_EQ(0, motionArgs.buttonState);
4245
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4247 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4248 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4249
4250 // press BTN_FORWARD, release BTN_FORWARD
4251 processKey(mapper, BTN_FORWARD, 1);
4252 processSync(mapper);
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4254 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4255 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4260
4261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4263 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264
4265 processKey(mapper, BTN_FORWARD, 0);
4266 processSync(mapper);
4267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270
4271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004273 ASSERT_EQ(0, motionArgs.buttonState);
4274
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4276 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4277 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4278
4279 // press BTN_EXTRA, release BTN_EXTRA
4280 processKey(mapper, BTN_EXTRA, 1);
4281 processSync(mapper);
4282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4283 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4284 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004288 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4289
4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4292 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293
4294 processKey(mapper, BTN_EXTRA, 0);
4295 processSync(mapper);
4296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004299
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302 ASSERT_EQ(0, motionArgs.buttonState);
4303
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4305 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4306 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4307
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4309
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 // press BTN_STYLUS, release BTN_STYLUS
4311 processKey(mapper, BTN_STYLUS, 1);
4312 processSync(mapper);
4313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004315 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4316
4317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4319 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320
4321 processKey(mapper, BTN_STYLUS, 0);
4322 processSync(mapper);
4323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326
4327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004329 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330
4331 // press BTN_STYLUS2, release BTN_STYLUS2
4332 processKey(mapper, BTN_STYLUS2, 1);
4333 processSync(mapper);
4334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4337
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4340 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341
4342 processKey(mapper, BTN_STYLUS2, 0);
4343 processSync(mapper);
4344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347
4348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351
4352 // release touch
4353 processUp(mapper);
4354 processSync(mapper);
4355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4356 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4357 ASSERT_EQ(0, motionArgs.buttonState);
4358}
4359
4360TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4361 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4362 addConfigurationProperty("touch.deviceType", "touchScreen");
4363 prepareDisplay(DISPLAY_ORIENTATION_0);
4364 prepareButtons();
4365 prepareAxes(POSITION);
4366 addMapperAndConfigure(mapper);
4367
4368 NotifyMotionArgs motionArgs;
4369
4370 // default tool type is finger
4371 processDown(mapper, 100, 200);
4372 processSync(mapper);
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4374 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4376
4377 // eraser
4378 processKey(mapper, BTN_TOOL_RUBBER, 1);
4379 processSync(mapper);
4380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4383
4384 // stylus
4385 processKey(mapper, BTN_TOOL_RUBBER, 0);
4386 processKey(mapper, BTN_TOOL_PEN, 1);
4387 processSync(mapper);
4388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4391
4392 // brush
4393 processKey(mapper, BTN_TOOL_PEN, 0);
4394 processKey(mapper, BTN_TOOL_BRUSH, 1);
4395 processSync(mapper);
4396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4399
4400 // pencil
4401 processKey(mapper, BTN_TOOL_BRUSH, 0);
4402 processKey(mapper, BTN_TOOL_PENCIL, 1);
4403 processSync(mapper);
4404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4407
4408 // airbrush
4409 processKey(mapper, BTN_TOOL_PENCIL, 0);
4410 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4411 processSync(mapper);
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4415
4416 // mouse
4417 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4418 processKey(mapper, BTN_TOOL_MOUSE, 1);
4419 processSync(mapper);
4420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4423
4424 // lens
4425 processKey(mapper, BTN_TOOL_MOUSE, 0);
4426 processKey(mapper, BTN_TOOL_LENS, 1);
4427 processSync(mapper);
4428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4431
4432 // double-tap
4433 processKey(mapper, BTN_TOOL_LENS, 0);
4434 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4435 processSync(mapper);
4436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4439
4440 // triple-tap
4441 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4442 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4443 processSync(mapper);
4444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4447
4448 // quad-tap
4449 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4450 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4451 processSync(mapper);
4452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4455
4456 // finger
4457 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4458 processKey(mapper, BTN_TOOL_FINGER, 1);
4459 processSync(mapper);
4460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4463
4464 // stylus trumps finger
4465 processKey(mapper, BTN_TOOL_PEN, 1);
4466 processSync(mapper);
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4470
4471 // eraser trumps stylus
4472 processKey(mapper, BTN_TOOL_RUBBER, 1);
4473 processSync(mapper);
4474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4477
4478 // mouse trumps eraser
4479 processKey(mapper, BTN_TOOL_MOUSE, 1);
4480 processSync(mapper);
4481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4484
4485 // back to default tool type
4486 processKey(mapper, BTN_TOOL_MOUSE, 0);
4487 processKey(mapper, BTN_TOOL_RUBBER, 0);
4488 processKey(mapper, BTN_TOOL_PEN, 0);
4489 processKey(mapper, BTN_TOOL_FINGER, 0);
4490 processSync(mapper);
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4494}
4495
4496TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4497 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4498 addConfigurationProperty("touch.deviceType", "touchScreen");
4499 prepareDisplay(DISPLAY_ORIENTATION_0);
4500 prepareButtons();
4501 prepareAxes(POSITION);
4502 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4503 addMapperAndConfigure(mapper);
4504
4505 NotifyMotionArgs motionArgs;
4506
4507 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4508 processKey(mapper, BTN_TOOL_FINGER, 1);
4509 processMove(mapper, 100, 200);
4510 processSync(mapper);
4511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4514 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4515
4516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4519 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4520
4521 // move a little
4522 processMove(mapper, 150, 250);
4523 processSync(mapper);
4524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4527 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4528
4529 // down when BTN_TOUCH is pressed, pressure defaults to 1
4530 processKey(mapper, BTN_TOUCH, 1);
4531 processSync(mapper);
4532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4536
4537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4538 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4540 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4541
4542 // up when BTN_TOUCH is released, hover restored
4543 processKey(mapper, BTN_TOUCH, 0);
4544 processSync(mapper);
4545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4546 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4548 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4549
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4553 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4554
4555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4558 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4559
4560 // exit hover when pointer goes away
4561 processKey(mapper, BTN_TOOL_FINGER, 0);
4562 processSync(mapper);
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4566 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4567}
4568
4569TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4570 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4571 addConfigurationProperty("touch.deviceType", "touchScreen");
4572 prepareDisplay(DISPLAY_ORIENTATION_0);
4573 prepareButtons();
4574 prepareAxes(POSITION | PRESSURE);
4575 addMapperAndConfigure(mapper);
4576
4577 NotifyMotionArgs motionArgs;
4578
4579 // initially hovering because pressure is 0
4580 processDown(mapper, 100, 200);
4581 processPressure(mapper, 0);
4582 processSync(mapper);
4583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4586 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4587
4588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4589 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4591 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4592
4593 // move a little
4594 processMove(mapper, 150, 250);
4595 processSync(mapper);
4596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4599 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4600
4601 // down when pressure is non-zero
4602 processPressure(mapper, RAW_PRESSURE_MAX);
4603 processSync(mapper);
4604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4605 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4607 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4608
4609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4610 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4612 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4613
4614 // up when pressure becomes 0, hover restored
4615 processPressure(mapper, 0);
4616 processSync(mapper);
4617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4620 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4621
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4625 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4626
4627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4628 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4630 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4631
4632 // exit hover when pointer goes away
4633 processUp(mapper);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4638 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4639}
4640
Dan Harmsaca28402018-12-17 13:55:20 -08004641
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642// --- MultiTouchInputMapperTest ---
4643
4644class MultiTouchInputMapperTest : public TouchInputMapperTest {
4645protected:
4646 void prepareAxes(int axes);
4647
4648 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4649 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4650 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4651 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4652 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4653 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4654 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4655 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4656 void processId(MultiTouchInputMapper* mapper, int32_t id);
4657 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4658 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4659 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004660 void processTimestamp(MultiTouchInputMapper* mapper, uint32_t value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 void processMTSync(MultiTouchInputMapper* mapper);
4662 void processSync(MultiTouchInputMapper* mapper);
4663};
4664
4665void MultiTouchInputMapperTest::prepareAxes(int axes) {
4666 if (axes & POSITION) {
4667 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4668 RAW_X_MIN, RAW_X_MAX, 0, 0);
4669 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4670 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4671 }
4672 if (axes & TOUCH) {
4673 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4674 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4675 if (axes & MINOR) {
4676 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4677 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4678 }
4679 }
4680 if (axes & TOOL) {
4681 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4682 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4683 if (axes & MINOR) {
4684 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4685 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4686 }
4687 }
4688 if (axes & ORIENTATION) {
4689 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4690 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4691 }
4692 if (axes & PRESSURE) {
4693 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4694 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4695 }
4696 if (axes & DISTANCE) {
4697 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4698 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4699 }
4700 if (axes & ID) {
4701 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4702 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4703 }
4704 if (axes & SLOT) {
4705 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4706 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4707 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4708 }
4709 if (axes & TOOL_TYPE) {
4710 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4711 0, MT_TOOL_MAX, 0, 0);
4712 }
4713}
4714
4715void MultiTouchInputMapperTest::processPosition(
4716 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004717 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4718 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719}
4720
4721void MultiTouchInputMapperTest::processTouchMajor(
4722 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004723 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724}
4725
4726void MultiTouchInputMapperTest::processTouchMinor(
4727 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004728 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729}
4730
4731void MultiTouchInputMapperTest::processToolMajor(
4732 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004733 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734}
4735
4736void MultiTouchInputMapperTest::processToolMinor(
4737 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004738 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739}
4740
4741void MultiTouchInputMapperTest::processOrientation(
4742 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004743 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744}
4745
4746void MultiTouchInputMapperTest::processPressure(
4747 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004748 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749}
4750
4751void MultiTouchInputMapperTest::processDistance(
4752 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004753 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754}
4755
4756void MultiTouchInputMapperTest::processId(
4757 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004758 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759}
4760
4761void MultiTouchInputMapperTest::processSlot(
4762 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004763 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764}
4765
4766void MultiTouchInputMapperTest::processToolType(
4767 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004768 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769}
4770
4771void MultiTouchInputMapperTest::processKey(
4772 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004773 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774}
4775
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004776void MultiTouchInputMapperTest::processTimestamp(MultiTouchInputMapper* mapper, uint32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004777 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, value);
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004778}
4779
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004781 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782}
4783
4784void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004785 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786}
4787
4788
4789TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4790 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4791 addConfigurationProperty("touch.deviceType", "touchScreen");
4792 prepareDisplay(DISPLAY_ORIENTATION_0);
4793 prepareAxes(POSITION);
4794 prepareVirtualKeys();
4795 addMapperAndConfigure(mapper);
4796
4797 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4798
4799 NotifyMotionArgs motionArgs;
4800
4801 // Two fingers down at once.
4802 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4803 processPosition(mapper, x1, y1);
4804 processMTSync(mapper);
4805 processPosition(mapper, x2, y2);
4806 processMTSync(mapper);
4807 processSync(mapper);
4808
4809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4810 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4811 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4812 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4813 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4814 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4815 ASSERT_EQ(0, motionArgs.flags);
4816 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4817 ASSERT_EQ(0, motionArgs.buttonState);
4818 ASSERT_EQ(0, motionArgs.edgeFlags);
4819 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4820 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4823 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4824 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4825 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4826 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4827
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4830 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4831 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4832 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4833 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4834 motionArgs.action);
4835 ASSERT_EQ(0, motionArgs.flags);
4836 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4837 ASSERT_EQ(0, motionArgs.buttonState);
4838 ASSERT_EQ(0, motionArgs.edgeFlags);
4839 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4842 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4845 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4847 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4848 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4849 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4850 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4851
4852 // Move.
4853 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4854 processPosition(mapper, x1, y1);
4855 processMTSync(mapper);
4856 processPosition(mapper, x2, y2);
4857 processMTSync(mapper);
4858 processSync(mapper);
4859
4860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4861 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4862 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4863 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4864 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4866 ASSERT_EQ(0, motionArgs.flags);
4867 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4868 ASSERT_EQ(0, motionArgs.buttonState);
4869 ASSERT_EQ(0, motionArgs.edgeFlags);
4870 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4871 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4873 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4876 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4878 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4879 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4880 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4881 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4882
4883 // First finger up.
4884 x2 += 15; y2 -= 20;
4885 processPosition(mapper, x2, y2);
4886 processMTSync(mapper);
4887 processSync(mapper);
4888
4889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4890 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4891 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4892 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4893 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4894 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4895 motionArgs.action);
4896 ASSERT_EQ(0, motionArgs.flags);
4897 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4898 ASSERT_EQ(0, motionArgs.buttonState);
4899 ASSERT_EQ(0, motionArgs.edgeFlags);
4900 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4901 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4903 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4906 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4908 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4909 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4910 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4911 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4912
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4914 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4915 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4916 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4917 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4919 ASSERT_EQ(0, motionArgs.flags);
4920 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4921 ASSERT_EQ(0, motionArgs.buttonState);
4922 ASSERT_EQ(0, motionArgs.edgeFlags);
4923 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4924 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4927 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4928 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4929 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4930 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4931
4932 // Move.
4933 x2 += 20; y2 -= 25;
4934 processPosition(mapper, x2, y2);
4935 processMTSync(mapper);
4936 processSync(mapper);
4937
4938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4939 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4940 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4941 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4942 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4944 ASSERT_EQ(0, motionArgs.flags);
4945 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4946 ASSERT_EQ(0, motionArgs.buttonState);
4947 ASSERT_EQ(0, motionArgs.edgeFlags);
4948 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4949 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4952 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4953 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4954 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4955 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4956
4957 // New finger down.
4958 int32_t x3 = 700, y3 = 300;
4959 processPosition(mapper, x2, y2);
4960 processMTSync(mapper);
4961 processPosition(mapper, x3, y3);
4962 processMTSync(mapper);
4963 processSync(mapper);
4964
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4966 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4967 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4968 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4969 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4970 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4971 motionArgs.action);
4972 ASSERT_EQ(0, motionArgs.flags);
4973 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4974 ASSERT_EQ(0, motionArgs.buttonState);
4975 ASSERT_EQ(0, motionArgs.edgeFlags);
4976 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4977 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4979 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4982 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4984 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4985 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4986 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4987 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4988
4989 // Second finger up.
4990 x3 += 30; y3 -= 20;
4991 processPosition(mapper, x3, y3);
4992 processMTSync(mapper);
4993 processSync(mapper);
4994
4995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4996 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4997 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4998 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4999 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5000 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5001 motionArgs.action);
5002 ASSERT_EQ(0, motionArgs.flags);
5003 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5004 ASSERT_EQ(0, motionArgs.buttonState);
5005 ASSERT_EQ(0, motionArgs.edgeFlags);
5006 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5007 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5009 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5012 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5014 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5015 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5016 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5017 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5018
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5020 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5021 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5022 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5023 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5025 ASSERT_EQ(0, motionArgs.flags);
5026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5027 ASSERT_EQ(0, motionArgs.buttonState);
5028 ASSERT_EQ(0, motionArgs.edgeFlags);
5029 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5030 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5033 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5034 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5035 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5036 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5037
5038 // Last finger up.
5039 processMTSync(mapper);
5040 processSync(mapper);
5041
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5043 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5044 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5045 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5046 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5047 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5048 ASSERT_EQ(0, motionArgs.flags);
5049 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5050 ASSERT_EQ(0, motionArgs.buttonState);
5051 ASSERT_EQ(0, motionArgs.edgeFlags);
5052 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5053 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5056 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5057 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5058 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5060
5061 // Should not have sent any more keys or motions.
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5064}
5065
5066TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5067 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5068 addConfigurationProperty("touch.deviceType", "touchScreen");
5069 prepareDisplay(DISPLAY_ORIENTATION_0);
5070 prepareAxes(POSITION | ID);
5071 prepareVirtualKeys();
5072 addMapperAndConfigure(mapper);
5073
5074 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5075
5076 NotifyMotionArgs motionArgs;
5077
5078 // Two fingers down at once.
5079 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5080 processPosition(mapper, x1, y1);
5081 processId(mapper, 1);
5082 processMTSync(mapper);
5083 processPosition(mapper, x2, y2);
5084 processId(mapper, 2);
5085 processMTSync(mapper);
5086 processSync(mapper);
5087
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5090 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5094 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5095
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5097 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5098 motionArgs.action);
5099 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5102 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5105 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5107 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5108
5109 // Move.
5110 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5111 processPosition(mapper, x1, y1);
5112 processId(mapper, 1);
5113 processMTSync(mapper);
5114 processPosition(mapper, x2, y2);
5115 processId(mapper, 2);
5116 processMTSync(mapper);
5117 processSync(mapper);
5118
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5121 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5122 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5124 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5127 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5129 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5130
5131 // First finger up.
5132 x2 += 15; y2 -= 20;
5133 processPosition(mapper, x2, y2);
5134 processId(mapper, 2);
5135 processMTSync(mapper);
5136 processSync(mapper);
5137
5138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5139 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5140 motionArgs.action);
5141 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5142 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5144 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5147 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5149 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5150
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5153 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5154 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5157 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5158
5159 // Move.
5160 x2 += 20; y2 -= 25;
5161 processPosition(mapper, x2, y2);
5162 processId(mapper, 2);
5163 processMTSync(mapper);
5164 processSync(mapper);
5165
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5168 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5169 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5172 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5173
5174 // New finger down.
5175 int32_t x3 = 700, y3 = 300;
5176 processPosition(mapper, x2, y2);
5177 processId(mapper, 2);
5178 processMTSync(mapper);
5179 processPosition(mapper, x3, y3);
5180 processId(mapper, 3);
5181 processMTSync(mapper);
5182 processSync(mapper);
5183
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5185 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5186 motionArgs.action);
5187 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5188 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5190 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5193 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5195 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5196
5197 // Second finger up.
5198 x3 += 30; y3 -= 20;
5199 processPosition(mapper, x3, y3);
5200 processId(mapper, 3);
5201 processMTSync(mapper);
5202 processSync(mapper);
5203
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5205 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5206 motionArgs.action);
5207 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5208 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5210 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5213 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5215 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5216
5217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5219 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5220 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5223 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5224
5225 // Last finger up.
5226 processMTSync(mapper);
5227 processSync(mapper);
5228
5229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5230 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5231 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5232 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5235 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5236
5237 // Should not have sent any more keys or motions.
5238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5240}
5241
5242TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5243 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5244 addConfigurationProperty("touch.deviceType", "touchScreen");
5245 prepareDisplay(DISPLAY_ORIENTATION_0);
5246 prepareAxes(POSITION | ID | SLOT);
5247 prepareVirtualKeys();
5248 addMapperAndConfigure(mapper);
5249
5250 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5251
5252 NotifyMotionArgs motionArgs;
5253
5254 // Two fingers down at once.
5255 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5256 processPosition(mapper, x1, y1);
5257 processId(mapper, 1);
5258 processSlot(mapper, 1);
5259 processPosition(mapper, x2, y2);
5260 processId(mapper, 2);
5261 processSync(mapper);
5262
5263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5264 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5265 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5266 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5269 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5270
5271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5272 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5273 motionArgs.action);
5274 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5275 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5277 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5280 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5282 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5283
5284 // Move.
5285 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5286 processSlot(mapper, 0);
5287 processPosition(mapper, x1, y1);
5288 processSlot(mapper, 1);
5289 processPosition(mapper, x2, y2);
5290 processSync(mapper);
5291
5292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5294 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5295 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5297 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5300 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5302 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5303
5304 // First finger up.
5305 x2 += 15; y2 -= 20;
5306 processSlot(mapper, 0);
5307 processId(mapper, -1);
5308 processSlot(mapper, 1);
5309 processPosition(mapper, x2, y2);
5310 processSync(mapper);
5311
5312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5313 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5314 motionArgs.action);
5315 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5316 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5318 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5321 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5323 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5324
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5327 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5328 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5331 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5332
5333 // Move.
5334 x2 += 20; y2 -= 25;
5335 processPosition(mapper, x2, y2);
5336 processSync(mapper);
5337
5338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5340 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5341 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5344 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5345
5346 // New finger down.
5347 int32_t x3 = 700, y3 = 300;
5348 processPosition(mapper, x2, y2);
5349 processSlot(mapper, 0);
5350 processId(mapper, 3);
5351 processPosition(mapper, x3, y3);
5352 processSync(mapper);
5353
5354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5355 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5356 motionArgs.action);
5357 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5358 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5360 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5363 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5365 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5366
5367 // Second finger up.
5368 x3 += 30; y3 -= 20;
5369 processSlot(mapper, 1);
5370 processId(mapper, -1);
5371 processSlot(mapper, 0);
5372 processPosition(mapper, x3, y3);
5373 processSync(mapper);
5374
5375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5376 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5377 motionArgs.action);
5378 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5379 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5381 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5384 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5386 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5387
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5390 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5391 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5394 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5395
5396 // Last finger up.
5397 processId(mapper, -1);
5398 processSync(mapper);
5399
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5401 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5402 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5403 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5406 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5407
5408 // Should not have sent any more keys or motions.
5409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5411}
5412
5413TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5414 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5415 addConfigurationProperty("touch.deviceType", "touchScreen");
5416 prepareDisplay(DISPLAY_ORIENTATION_0);
5417 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5418 addMapperAndConfigure(mapper);
5419
5420 // These calculations are based on the input device calibration documentation.
5421 int32_t rawX = 100;
5422 int32_t rawY = 200;
5423 int32_t rawTouchMajor = 7;
5424 int32_t rawTouchMinor = 6;
5425 int32_t rawToolMajor = 9;
5426 int32_t rawToolMinor = 8;
5427 int32_t rawPressure = 11;
5428 int32_t rawDistance = 0;
5429 int32_t rawOrientation = 3;
5430 int32_t id = 5;
5431
5432 float x = toDisplayX(rawX);
5433 float y = toDisplayY(rawY);
5434 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5435 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5436 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5437 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5438 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5439 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5440 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5441 float distance = float(rawDistance);
5442
5443 processPosition(mapper, rawX, rawY);
5444 processTouchMajor(mapper, rawTouchMajor);
5445 processTouchMinor(mapper, rawTouchMinor);
5446 processToolMajor(mapper, rawToolMajor);
5447 processToolMinor(mapper, rawToolMinor);
5448 processPressure(mapper, rawPressure);
5449 processOrientation(mapper, rawOrientation);
5450 processDistance(mapper, rawDistance);
5451 processId(mapper, id);
5452 processMTSync(mapper);
5453 processSync(mapper);
5454
5455 NotifyMotionArgs args;
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5457 ASSERT_EQ(0, args.pointerProperties[0].id);
5458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5459 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5460 orientation, distance));
5461}
5462
5463TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5464 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5465 addConfigurationProperty("touch.deviceType", "touchScreen");
5466 prepareDisplay(DISPLAY_ORIENTATION_0);
5467 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5468 addConfigurationProperty("touch.size.calibration", "geometric");
5469 addMapperAndConfigure(mapper);
5470
5471 // These calculations are based on the input device calibration documentation.
5472 int32_t rawX = 100;
5473 int32_t rawY = 200;
5474 int32_t rawTouchMajor = 140;
5475 int32_t rawTouchMinor = 120;
5476 int32_t rawToolMajor = 180;
5477 int32_t rawToolMinor = 160;
5478
5479 float x = toDisplayX(rawX);
5480 float y = toDisplayY(rawY);
5481 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5482 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5483 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5484 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5485 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5486
5487 processPosition(mapper, rawX, rawY);
5488 processTouchMajor(mapper, rawTouchMajor);
5489 processTouchMinor(mapper, rawTouchMinor);
5490 processToolMajor(mapper, rawToolMajor);
5491 processToolMinor(mapper, rawToolMinor);
5492 processMTSync(mapper);
5493 processSync(mapper);
5494
5495 NotifyMotionArgs args;
5496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5498 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5499}
5500
5501TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5502 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5503 addConfigurationProperty("touch.deviceType", "touchScreen");
5504 prepareDisplay(DISPLAY_ORIENTATION_0);
5505 prepareAxes(POSITION | TOUCH | TOOL);
5506 addConfigurationProperty("touch.size.calibration", "diameter");
5507 addConfigurationProperty("touch.size.scale", "10");
5508 addConfigurationProperty("touch.size.bias", "160");
5509 addConfigurationProperty("touch.size.isSummed", "1");
5510 addMapperAndConfigure(mapper);
5511
5512 // These calculations are based on the input device calibration documentation.
5513 // Note: We only provide a single common touch/tool value because the device is assumed
5514 // not to emit separate values for each pointer (isSummed = 1).
5515 int32_t rawX = 100;
5516 int32_t rawY = 200;
5517 int32_t rawX2 = 150;
5518 int32_t rawY2 = 250;
5519 int32_t rawTouchMajor = 5;
5520 int32_t rawToolMajor = 8;
5521
5522 float x = toDisplayX(rawX);
5523 float y = toDisplayY(rawY);
5524 float x2 = toDisplayX(rawX2);
5525 float y2 = toDisplayY(rawY2);
5526 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5527 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5528 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5529
5530 processPosition(mapper, rawX, rawY);
5531 processTouchMajor(mapper, rawTouchMajor);
5532 processToolMajor(mapper, rawToolMajor);
5533 processMTSync(mapper);
5534 processPosition(mapper, rawX2, rawY2);
5535 processTouchMajor(mapper, rawTouchMajor);
5536 processToolMajor(mapper, rawToolMajor);
5537 processMTSync(mapper);
5538 processSync(mapper);
5539
5540 NotifyMotionArgs args;
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5542 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5543
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5545 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5546 args.action);
5547 ASSERT_EQ(size_t(2), args.pointerCount);
5548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5549 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5551 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5552}
5553
5554TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5555 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5556 addConfigurationProperty("touch.deviceType", "touchScreen");
5557 prepareDisplay(DISPLAY_ORIENTATION_0);
5558 prepareAxes(POSITION | TOUCH | TOOL);
5559 addConfigurationProperty("touch.size.calibration", "area");
5560 addConfigurationProperty("touch.size.scale", "43");
5561 addConfigurationProperty("touch.size.bias", "3");
5562 addMapperAndConfigure(mapper);
5563
5564 // These calculations are based on the input device calibration documentation.
5565 int32_t rawX = 100;
5566 int32_t rawY = 200;
5567 int32_t rawTouchMajor = 5;
5568 int32_t rawToolMajor = 8;
5569
5570 float x = toDisplayX(rawX);
5571 float y = toDisplayY(rawY);
5572 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5573 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5574 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5575
5576 processPosition(mapper, rawX, rawY);
5577 processTouchMajor(mapper, rawTouchMajor);
5578 processToolMajor(mapper, rawToolMajor);
5579 processMTSync(mapper);
5580 processSync(mapper);
5581
5582 NotifyMotionArgs args;
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5585 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5586}
5587
5588TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5589 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5590 addConfigurationProperty("touch.deviceType", "touchScreen");
5591 prepareDisplay(DISPLAY_ORIENTATION_0);
5592 prepareAxes(POSITION | PRESSURE);
5593 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5594 addConfigurationProperty("touch.pressure.scale", "0.01");
5595 addMapperAndConfigure(mapper);
5596
Michael Wrightaa449c92017-12-13 21:21:43 +00005597 InputDeviceInfo info;
5598 mapper->populateDeviceInfo(&info);
5599 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5600 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5601 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5602
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 // These calculations are based on the input device calibration documentation.
5604 int32_t rawX = 100;
5605 int32_t rawY = 200;
5606 int32_t rawPressure = 60;
5607
5608 float x = toDisplayX(rawX);
5609 float y = toDisplayY(rawY);
5610 float pressure = float(rawPressure) * 0.01f;
5611
5612 processPosition(mapper, rawX, rawY);
5613 processPressure(mapper, rawPressure);
5614 processMTSync(mapper);
5615 processSync(mapper);
5616
5617 NotifyMotionArgs args;
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5620 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5621}
5622
5623TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5624 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5625 addConfigurationProperty("touch.deviceType", "touchScreen");
5626 prepareDisplay(DISPLAY_ORIENTATION_0);
5627 prepareAxes(POSITION | ID | SLOT);
5628 addMapperAndConfigure(mapper);
5629
5630 NotifyMotionArgs motionArgs;
5631 NotifyKeyArgs keyArgs;
5632
5633 processId(mapper, 1);
5634 processPosition(mapper, 100, 200);
5635 processSync(mapper);
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5637 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5638 ASSERT_EQ(0, motionArgs.buttonState);
5639
5640 // press BTN_LEFT, release BTN_LEFT
5641 processKey(mapper, BTN_LEFT, 1);
5642 processSync(mapper);
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5645 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5646
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5648 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5649 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5650
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 processKey(mapper, BTN_LEFT, 0);
5652 processSync(mapper);
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005656
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005659 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660
5661 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5662 processKey(mapper, BTN_RIGHT, 1);
5663 processKey(mapper, BTN_MIDDLE, 1);
5664 processSync(mapper);
5665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5667 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5668 motionArgs.buttonState);
5669
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5672 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5673
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5675 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5676 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5677 motionArgs.buttonState);
5678
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 processKey(mapper, BTN_RIGHT, 0);
5680 processSync(mapper);
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005682 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005684
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005687 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688
5689 processKey(mapper, BTN_MIDDLE, 0);
5690 processSync(mapper);
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005697 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698
5699 // press BTN_BACK, release BTN_BACK
5700 processKey(mapper, BTN_BACK, 1);
5701 processSync(mapper);
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5703 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5704 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005705
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005708 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5712 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713
5714 processKey(mapper, BTN_BACK, 0);
5715 processSync(mapper);
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005719
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005722 ASSERT_EQ(0, motionArgs.buttonState);
5723
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5725 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5726 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5727
5728 // press BTN_SIDE, release BTN_SIDE
5729 processKey(mapper, BTN_SIDE, 1);
5730 processSync(mapper);
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5732 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5733 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005734
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005737 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5738
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5740 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5741 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742
5743 processKey(mapper, BTN_SIDE, 0);
5744 processSync(mapper);
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005746 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005751 ASSERT_EQ(0, motionArgs.buttonState);
5752
Michael Wrightd02c5b62014-02-10 15:10:22 -08005753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5754 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5755 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5756
5757 // press BTN_FORWARD, release BTN_FORWARD
5758 processKey(mapper, BTN_FORWARD, 1);
5759 processSync(mapper);
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5761 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5762 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005763
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005766 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5767
5768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5769 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5770 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771
5772 processKey(mapper, BTN_FORWARD, 0);
5773 processSync(mapper);
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005775 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005777
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005780 ASSERT_EQ(0, motionArgs.buttonState);
5781
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5783 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5784 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5785
5786 // press BTN_EXTRA, release BTN_EXTRA
5787 processKey(mapper, BTN_EXTRA, 1);
5788 processSync(mapper);
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5790 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5791 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005792
Michael Wrightd02c5b62014-02-10 15:10:22 -08005793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5796
5797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5798 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5799 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800
5801 processKey(mapper, BTN_EXTRA, 0);
5802 processSync(mapper);
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005806
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005809 ASSERT_EQ(0, motionArgs.buttonState);
5810
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5812 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5813 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5814
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5816
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817 // press BTN_STYLUS, release BTN_STYLUS
5818 processKey(mapper, BTN_STYLUS, 1);
5819 processSync(mapper);
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005822 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5823
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5825 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5826 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827
5828 processKey(mapper, BTN_STYLUS, 0);
5829 processSync(mapper);
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005831 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005833
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005836 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837
5838 // press BTN_STYLUS2, release BTN_STYLUS2
5839 processKey(mapper, BTN_STYLUS2, 1);
5840 processSync(mapper);
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005843 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5844
5845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5846 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5847 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848
5849 processKey(mapper, BTN_STYLUS2, 0);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005854
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005857 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858
5859 // release touch
5860 processId(mapper, -1);
5861 processSync(mapper);
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5863 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5864 ASSERT_EQ(0, motionArgs.buttonState);
5865}
5866
5867TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
5868 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5869 addConfigurationProperty("touch.deviceType", "touchScreen");
5870 prepareDisplay(DISPLAY_ORIENTATION_0);
5871 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
5872 addMapperAndConfigure(mapper);
5873
5874 NotifyMotionArgs motionArgs;
5875
5876 // default tool type is finger
5877 processId(mapper, 1);
5878 processPosition(mapper, 100, 200);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5883
5884 // eraser
5885 processKey(mapper, BTN_TOOL_RUBBER, 1);
5886 processSync(mapper);
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5890
5891 // stylus
5892 processKey(mapper, BTN_TOOL_RUBBER, 0);
5893 processKey(mapper, BTN_TOOL_PEN, 1);
5894 processSync(mapper);
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5898
5899 // brush
5900 processKey(mapper, BTN_TOOL_PEN, 0);
5901 processKey(mapper, BTN_TOOL_BRUSH, 1);
5902 processSync(mapper);
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5906
5907 // pencil
5908 processKey(mapper, BTN_TOOL_BRUSH, 0);
5909 processKey(mapper, BTN_TOOL_PENCIL, 1);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5914
5915 // airbrush
5916 processKey(mapper, BTN_TOOL_PENCIL, 0);
5917 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5918 processSync(mapper);
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5921 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5922
5923 // mouse
5924 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5925 processKey(mapper, BTN_TOOL_MOUSE, 1);
5926 processSync(mapper);
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5930
5931 // lens
5932 processKey(mapper, BTN_TOOL_MOUSE, 0);
5933 processKey(mapper, BTN_TOOL_LENS, 1);
5934 processSync(mapper);
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5938
5939 // double-tap
5940 processKey(mapper, BTN_TOOL_LENS, 0);
5941 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5942 processSync(mapper);
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5946
5947 // triple-tap
5948 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5949 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5950 processSync(mapper);
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5954
5955 // quad-tap
5956 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5957 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5958 processSync(mapper);
5959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5962
5963 // finger
5964 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5965 processKey(mapper, BTN_TOOL_FINGER, 1);
5966 processSync(mapper);
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5970
5971 // stylus trumps finger
5972 processKey(mapper, BTN_TOOL_PEN, 1);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5977
5978 // eraser trumps stylus
5979 processKey(mapper, BTN_TOOL_RUBBER, 1);
5980 processSync(mapper);
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5984
5985 // mouse trumps eraser
5986 processKey(mapper, BTN_TOOL_MOUSE, 1);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5991
5992 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
5993 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
5994 processSync(mapper);
5995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5998
5999 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6000 processToolType(mapper, MT_TOOL_PEN);
6001 processSync(mapper);
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6005
6006 // back to default tool type
6007 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6008 processKey(mapper, BTN_TOOL_MOUSE, 0);
6009 processKey(mapper, BTN_TOOL_RUBBER, 0);
6010 processKey(mapper, BTN_TOOL_PEN, 0);
6011 processKey(mapper, BTN_TOOL_FINGER, 0);
6012 processSync(mapper);
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6016}
6017
6018TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6019 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6020 addConfigurationProperty("touch.deviceType", "touchScreen");
6021 prepareDisplay(DISPLAY_ORIENTATION_0);
6022 prepareAxes(POSITION | ID | SLOT);
6023 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6024 addMapperAndConfigure(mapper);
6025
6026 NotifyMotionArgs motionArgs;
6027
6028 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6029 processId(mapper, 1);
6030 processPosition(mapper, 100, 200);
6031 processSync(mapper);
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6035 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6036
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6040 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6041
6042 // move a little
6043 processPosition(mapper, 150, 250);
6044 processSync(mapper);
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6046 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6048 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6049
6050 // down when BTN_TOUCH is pressed, pressure defaults to 1
6051 processKey(mapper, BTN_TOUCH, 1);
6052 processSync(mapper);
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6054 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6056 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6057
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6059 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6061 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6062
6063 // up when BTN_TOUCH is released, hover restored
6064 processKey(mapper, BTN_TOUCH, 0);
6065 processSync(mapper);
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6069 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6070
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6072 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6074 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6075
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6077 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6079 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6080
6081 // exit hover when pointer goes away
6082 processId(mapper, -1);
6083 processSync(mapper);
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6087 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6088}
6089
6090TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6091 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6092 addConfigurationProperty("touch.deviceType", "touchScreen");
6093 prepareDisplay(DISPLAY_ORIENTATION_0);
6094 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6095 addMapperAndConfigure(mapper);
6096
6097 NotifyMotionArgs motionArgs;
6098
6099 // initially hovering because pressure is 0
6100 processId(mapper, 1);
6101 processPosition(mapper, 100, 200);
6102 processPressure(mapper, 0);
6103 processSync(mapper);
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6105 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6107 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6108
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6112 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6113
6114 // move a little
6115 processPosition(mapper, 150, 250);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6120 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6121
6122 // down when pressure becomes non-zero
6123 processPressure(mapper, RAW_PRESSURE_MAX);
6124 processSync(mapper);
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6126 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6128 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6129
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6131 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6133 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6134
6135 // up when pressure becomes 0, hover restored
6136 processPressure(mapper, 0);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6139 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6141 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6142
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6144 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6146 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6147
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6149 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6151 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6152
6153 // exit hover when pointer goes away
6154 processId(mapper, -1);
6155 processSync(mapper);
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6157 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6159 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6160}
6161
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08006162TEST_F(MultiTouchInputMapperTest, Process_HandlesTimestamp) {
6163 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6164
6165 addConfigurationProperty("touch.deviceType", "touchScreen");
6166 prepareDisplay(DISPLAY_ORIENTATION_0);
6167 prepareAxes(POSITION);
6168 addMapperAndConfigure(mapper);
6169 NotifyMotionArgs args;
6170
6171 // By default, deviceTimestamp should be zero
6172 processPosition(mapper, 100, 100);
6173 processMTSync(mapper);
6174 processSync(mapper);
6175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6176 ASSERT_EQ(0U, args.deviceTimestamp);
6177
6178 // Now the timestamp of 1000 is reported by evdev and should appear in MotionArgs
6179 processPosition(mapper, 0, 0);
6180 processTimestamp(mapper, 1000);
6181 processMTSync(mapper);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6184 ASSERT_EQ(1000U, args.deviceTimestamp);
6185}
6186
Siarhei Vishniakoueaf7acd2018-01-09 12:35:51 -08006187TEST_F(MultiTouchInputMapperTest, WhenMapperIsReset_TimestampIsCleared) {
6188 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6189
6190 addConfigurationProperty("touch.deviceType", "touchScreen");
6191 prepareDisplay(DISPLAY_ORIENTATION_0);
6192 prepareAxes(POSITION);
6193 addMapperAndConfigure(mapper);
6194 NotifyMotionArgs args;
6195
6196 // Send a touch event with a timestamp
6197 processPosition(mapper, 100, 100);
6198 processTimestamp(mapper, 1);
6199 processMTSync(mapper);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6202 ASSERT_EQ(1U, args.deviceTimestamp);
6203
6204 // Since the data accumulates, and new timestamp has not arrived, deviceTimestamp won't change
6205 processPosition(mapper, 100, 200);
6206 processMTSync(mapper);
6207 processSync(mapper);
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6209 ASSERT_EQ(1U, args.deviceTimestamp);
6210
6211 mapper->reset(/* when */ 0);
6212 // After the mapper is reset, deviceTimestamp should become zero again
6213 processPosition(mapper, 100, 300);
6214 processMTSync(mapper);
6215 processSync(mapper);
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6217 ASSERT_EQ(0U, args.deviceTimestamp);
6218}
6219
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006220/**
6221 * Set the input device port <--> display port associations, and check that the
6222 * events are routed to the display that matches the display port.
6223 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6224 */
6225TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6226 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6227 const std::string usb2 = "USB2";
6228 const uint8_t hdmi1 = 0;
6229 const uint8_t hdmi2 = 1;
6230 const std::string secondaryUniqueId = "uniqueId2";
6231 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6232
6233 addConfigurationProperty("touch.deviceType", "touchScreen");
6234 prepareAxes(POSITION);
6235 addMapperAndConfigure(mapper);
6236
6237 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6238 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6239
6240 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6241 // for this input device is specified, and the matching viewport is not present,
6242 // the input device should be disabled (at the mapper level).
6243
6244 // Add viewport for display 2 on hdmi2
6245 prepareSecondaryDisplay(type, hdmi2);
6246 // Send a touch event
6247 processPosition(mapper, 100, 100);
6248 processSync(mapper);
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6250
6251 // Add viewport for display 1 on hdmi1
6252 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6253 // Send a touch event again
6254 processPosition(mapper, 100, 100);
6255 processSync(mapper);
6256
6257 NotifyMotionArgs args;
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6259 ASSERT_EQ(DISPLAY_ID, args.displayId);
6260}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261
Arthur Hung41a712e2018-11-22 19:41:03 +08006262/**
6263 * Expect fallback to internal viewport if device is external and external viewport is not present.
6264 */
6265TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6266 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6267 prepareAxes(POSITION);
6268 addConfigurationProperty("touch.deviceType", "touchScreen");
6269 prepareDisplay(DISPLAY_ORIENTATION_0);
6270 mDevice->setExternal(true);
6271 addMapperAndConfigure(mapper);
6272
6273 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6274
6275 NotifyMotionArgs motionArgs;
6276
6277 // Expect the event to be sent to the internal viewport,
6278 // because an external viewport is not present.
6279 processPosition(mapper, 100, 100);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6282 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6283
6284 // Expect the event to be sent to the external viewport if it is present.
6285 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6286 processPosition(mapper, 100, 100);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6290}
6291
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006292TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6293 // Setup PointerController for second display.
6294 sp<FakePointerController> fakePointerController = new FakePointerController();
6295 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6296 fakePointerController->setPosition(100, 200);
6297 fakePointerController->setButtonState(0);
6298 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6299 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6300
6301 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6302 prepareDisplay(DISPLAY_ORIENTATION_0);
6303 prepareAxes(POSITION);
6304 addMapperAndConfigure(mapper);
6305
6306 // Check source is mouse that would obtain the PointerController.
6307 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6308
6309 NotifyMotionArgs motionArgs;
6310 processPosition(mapper, 100, 100);
6311 processSync(mapper);
6312
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6315 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6316}
6317
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318} // namespace android