blob: d35302885d21abbb5ff6d92e85603e60eeba8a89 [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
Arthur Hung7c645402019-01-25 17:45:42 +0800106 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
107 return mSpotsByDisplay;
108 }
109
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110private:
111 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
112 *outMinX = mMinX;
113 *outMinY = mMinY;
114 *outMaxX = mMaxX;
115 *outMaxY = mMaxY;
116 return mHaveBounds;
117 }
118
119 virtual void move(float deltaX, float deltaY) {
120 mX += deltaX;
121 if (mX < mMinX) mX = mMinX;
122 if (mX > mMaxX) mX = mMaxX;
123 mY += deltaY;
124 if (mY < mMinY) mY = mMinY;
125 if (mY > mMaxY) mY = mMaxY;
126 }
127
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100128 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129 }
130
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100131 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 }
133
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100134 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 }
136
Arthur Hung7c645402019-01-25 17:45:42 +0800137 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
138 int32_t displayId) {
139 std::vector<int32_t> newSpots;
140 // Add spots for fingers that are down.
141 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
142 uint32_t id = idBits.clearFirstMarkedBit();
143 newSpots.push_back(id);
144 }
145
146 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147 }
148
149 virtual void clearSpots() {
150 }
Arthur Hung7c645402019-01-25 17:45:42 +0800151
152 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153};
154
155
156// --- FakeInputReaderPolicy ---
157
158class FakeInputReaderPolicy : public InputReaderPolicyInterface {
159 InputReaderConfiguration mConfig;
160 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800161 std::vector<InputDeviceInfo> mInputDevices;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100162 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700163 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
165protected:
166 virtual ~FakeInputReaderPolicy() { }
167
168public:
169 FakeInputReaderPolicy() {
170 }
171
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700172 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100173 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100174 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700175 }
176
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700177 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
178 return mConfig.getDisplayViewportByUniqueId(uniqueId);
179 }
180 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
181 return mConfig.getDisplayViewportByType(type);
182 }
183
184 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
185 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700186 }
187
188 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700189 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
190 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700191 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700192 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700193 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100194 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 }
196
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100197 void addExcludedDeviceName(const std::string& deviceName) {
198 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199 }
200
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700201 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
202 mConfig.portAssociations.insert({inputPort, displayPort});
203 }
204
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700205 void addDisabledDevice(int32_t deviceId) {
206 ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
207 bool currentlyEnabled = index < 0;
208 if (currentlyEnabled) {
209 mConfig.disabledDevices.add(deviceId);
210 }
211 }
212
213 void removeDisabledDevice(int32_t deviceId) {
214 ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
215 bool currentlyEnabled = index < 0;
216 if (!currentlyEnabled) {
217 mConfig.disabledDevices.remove(deviceId);
218 }
219 }
220
Michael Wrightd02c5b62014-02-10 15:10:22 -0800221 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
222 mPointerControllers.add(deviceId, controller);
223 }
224
225 const InputReaderConfiguration* getReaderConfiguration() const {
226 return &mConfig;
227 }
228
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800229 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 return mInputDevices;
231 }
232
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100233 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700234 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700235 return transform;
236 }
237
238 void setTouchAffineTransformation(const TouchAffineTransformation t) {
239 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800240 }
241
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800242 void setPointerCapture(bool enabled) {
243 mConfig.pointerCapture = enabled;
244 }
245
Arthur Hung7c645402019-01-25 17:45:42 +0800246 void setShowTouches(bool enabled) {
247 mConfig.showTouches = enabled;
248 }
249
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700251 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700252 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
253 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700254 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
255 || orientation == DISPLAY_ORIENTATION_270);
256 DisplayViewport v;
257 v.displayId = displayId;
258 v.orientation = orientation;
259 v.logicalLeft = 0;
260 v.logicalTop = 0;
261 v.logicalRight = isRotated ? height : width;
262 v.logicalBottom = isRotated ? width : height;
263 v.physicalLeft = 0;
264 v.physicalTop = 0;
265 v.physicalRight = isRotated ? height : width;
266 v.physicalBottom = isRotated ? width : height;
267 v.deviceWidth = isRotated ? height : width;
268 v.deviceHeight = isRotated ? width : height;
269 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700270 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100271 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700272 return v;
273 }
274
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
276 *outConfig = mConfig;
277 }
278
279 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
280 return mPointerControllers.valueFor(deviceId);
281 }
282
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800283 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 mInputDevices = inputDevices;
285 }
286
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100287 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700288 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 }
290
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100291 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
292 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 }
294};
295
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296// --- FakeEventHub ---
297
298class FakeEventHub : public EventHubInterface {
299 struct KeyInfo {
300 int32_t keyCode;
301 uint32_t flags;
302 };
303
304 struct Device {
305 InputDeviceIdentifier identifier;
306 uint32_t classes;
307 PropertyMap configuration;
308 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
309 KeyedVector<int, bool> relativeAxes;
310 KeyedVector<int32_t, int32_t> keyCodeStates;
311 KeyedVector<int32_t, int32_t> scanCodeStates;
312 KeyedVector<int32_t, int32_t> switchStates;
313 KeyedVector<int32_t, int32_t> absoluteAxisValue;
314 KeyedVector<int32_t, KeyInfo> keysByScanCode;
315 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
316 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800317 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700318 bool enabled;
319
320 status_t enable() {
321 enabled = true;
322 return OK;
323 }
324
325 status_t disable() {
326 enabled = false;
327 return OK;
328 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700330 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700331 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332 }
333 };
334
335 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100336 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 List<RawEvent> mEvents;
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600338 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339
340protected:
341 virtual ~FakeEventHub() {
342 for (size_t i = 0; i < mDevices.size(); i++) {
343 delete mDevices.valueAt(i);
344 }
345 }
346
347public:
348 FakeEventHub() { }
349
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100350 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 Device* device = new Device(classes);
352 device->identifier.name = name;
353 mDevices.add(deviceId, device);
354
355 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
356 }
357
358 void removeDevice(int32_t deviceId) {
359 delete mDevices.valueFor(deviceId);
360 mDevices.removeItem(deviceId);
361
362 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
363 }
364
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700365 bool isDeviceEnabled(int32_t deviceId) {
366 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700367 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700368 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
369 return false;
370 }
371 return device->enabled;
372 }
373
374 status_t enableDevice(int32_t deviceId) {
375 status_t result;
376 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700377 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700378 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
379 return BAD_VALUE;
380 }
381 if (device->enabled) {
382 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
383 return OK;
384 }
385 result = device->enable();
386 return result;
387 }
388
389 status_t disableDevice(int32_t deviceId) {
390 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700391 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700392 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
393 return BAD_VALUE;
394 }
395 if (!device->enabled) {
396 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
397 return OK;
398 }
399 return device->disable();
400 }
401
Michael Wrightd02c5b62014-02-10 15:10:22 -0800402 void finishDeviceScan() {
403 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
404 }
405
406 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
407 Device* device = getDevice(deviceId);
408 device->configuration.addProperty(key, value);
409 }
410
411 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
412 Device* device = getDevice(deviceId);
413 device->configuration.addAll(configuration);
414 }
415
416 void addAbsoluteAxis(int32_t deviceId, int axis,
417 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
418 Device* device = getDevice(deviceId);
419
420 RawAbsoluteAxisInfo info;
421 info.valid = true;
422 info.minValue = minValue;
423 info.maxValue = maxValue;
424 info.flat = flat;
425 info.fuzz = fuzz;
426 info.resolution = resolution;
427 device->absoluteAxes.add(axis, info);
428 }
429
430 void addRelativeAxis(int32_t deviceId, int32_t axis) {
431 Device* device = getDevice(deviceId);
432 device->relativeAxes.add(axis, true);
433 }
434
435 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
436 Device* device = getDevice(deviceId);
437 device->keyCodeStates.replaceValueFor(keyCode, state);
438 }
439
440 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
441 Device* device = getDevice(deviceId);
442 device->scanCodeStates.replaceValueFor(scanCode, state);
443 }
444
445 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
446 Device* device = getDevice(deviceId);
447 device->switchStates.replaceValueFor(switchCode, state);
448 }
449
450 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
451 Device* device = getDevice(deviceId);
452 device->absoluteAxisValue.replaceValueFor(axis, value);
453 }
454
455 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
456 int32_t keyCode, uint32_t flags) {
457 Device* device = getDevice(deviceId);
458 KeyInfo info;
459 info.keyCode = keyCode;
460 info.flags = flags;
461 if (scanCode) {
462 device->keysByScanCode.add(scanCode, info);
463 }
464 if (usageCode) {
465 device->keysByUsageCode.add(usageCode, info);
466 }
467 }
468
469 void addLed(int32_t deviceId, int32_t led, bool initialState) {
470 Device* device = getDevice(deviceId);
471 device->leds.add(led, initialState);
472 }
473
474 bool getLedState(int32_t deviceId, int32_t led) {
475 Device* device = getDevice(deviceId);
476 return device->leds.valueFor(led);
477 }
478
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100479 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480 return mExcludedDevices;
481 }
482
483 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
484 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800485 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486 }
487
488 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
489 int32_t code, int32_t value) {
490 RawEvent event;
491 event.when = when;
492 event.deviceId = deviceId;
493 event.type = type;
494 event.code = code;
495 event.value = value;
496 mEvents.push_back(event);
497
498 if (type == EV_ABS) {
499 setAbsoluteAxisValue(deviceId, code, value);
500 }
501 }
502
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600503 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
504 std::vector<TouchVideoFrame>> videoFrames) {
505 mVideoFrames = std::move(videoFrames);
506 }
507
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508 void assertQueueIsEmpty() {
509 ASSERT_EQ(size_t(0), mEvents.size())
510 << "Expected the event queue to be empty (fully consumed).";
511 }
512
513private:
514 Device* getDevice(int32_t deviceId) const {
515 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100516 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 }
518
519 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
520 Device* device = getDevice(deviceId);
521 return device ? device->classes : 0;
522 }
523
524 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
525 Device* device = getDevice(deviceId);
526 return device ? device->identifier : InputDeviceIdentifier();
527 }
528
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100529 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530 return 0;
531 }
532
533 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
534 Device* device = getDevice(deviceId);
535 if (device) {
536 *outConfiguration = device->configuration;
537 }
538 }
539
540 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
541 RawAbsoluteAxisInfo* outAxisInfo) const {
542 Device* device = getDevice(deviceId);
543 if (device) {
544 ssize_t index = device->absoluteAxes.indexOfKey(axis);
545 if (index >= 0) {
546 *outAxisInfo = device->absoluteAxes.valueAt(index);
547 return OK;
548 }
549 }
550 outAxisInfo->clear();
551 return -1;
552 }
553
554 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
555 Device* device = getDevice(deviceId);
556 if (device) {
557 return device->relativeAxes.indexOfKey(axis) >= 0;
558 }
559 return false;
560 }
561
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100562 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563 return false;
564 }
565
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700566 virtual status_t mapKey(int32_t deviceId,
567 int32_t scanCode, int32_t usageCode, int32_t metaState,
568 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569 Device* device = getDevice(deviceId);
570 if (device) {
571 const KeyInfo* key = getKey(device, scanCode, usageCode);
572 if (key) {
573 if (outKeycode) {
574 *outKeycode = key->keyCode;
575 }
576 if (outFlags) {
577 *outFlags = key->flags;
578 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700579 if (outMetaState) {
580 *outMetaState = metaState;
581 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582 return OK;
583 }
584 }
585 return NAME_NOT_FOUND;
586 }
587
588 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
589 if (usageCode) {
590 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
591 if (index >= 0) {
592 return &device->keysByUsageCode.valueAt(index);
593 }
594 }
595 if (scanCode) {
596 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
597 if (index >= 0) {
598 return &device->keysByScanCode.valueAt(index);
599 }
600 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700601 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 }
603
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100604 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 return NAME_NOT_FOUND;
606 }
607
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100608 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 mExcludedDevices = devices;
610 }
611
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100612 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 if (mEvents.empty()) {
614 return 0;
615 }
616
617 *buffer = *mEvents.begin();
618 mEvents.erase(mEvents.begin());
619 return 1;
620 }
621
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800622 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600623 auto it = mVideoFrames.find(deviceId);
624 if (it != mVideoFrames.end()) {
625 std::vector<TouchVideoFrame> frames = std::move(it->second);
626 mVideoFrames.erase(deviceId);
627 return frames;
628 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800629 return {};
630 }
631
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
633 Device* device = getDevice(deviceId);
634 if (device) {
635 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
636 if (index >= 0) {
637 return device->scanCodeStates.valueAt(index);
638 }
639 }
640 return AKEY_STATE_UNKNOWN;
641 }
642
643 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
644 Device* device = getDevice(deviceId);
645 if (device) {
646 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
647 if (index >= 0) {
648 return device->keyCodeStates.valueAt(index);
649 }
650 }
651 return AKEY_STATE_UNKNOWN;
652 }
653
654 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
655 Device* device = getDevice(deviceId);
656 if (device) {
657 ssize_t index = device->switchStates.indexOfKey(sw);
658 if (index >= 0) {
659 return device->switchStates.valueAt(index);
660 }
661 }
662 return AKEY_STATE_UNKNOWN;
663 }
664
665 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
666 int32_t* outValue) const {
667 Device* device = getDevice(deviceId);
668 if (device) {
669 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
670 if (index >= 0) {
671 *outValue = device->absoluteAxisValue.valueAt(index);
672 return OK;
673 }
674 }
675 *outValue = 0;
676 return -1;
677 }
678
679 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
680 uint8_t* outFlags) const {
681 bool result = false;
682 Device* device = getDevice(deviceId);
683 if (device) {
684 for (size_t i = 0; i < numCodes; i++) {
685 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
686 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
687 outFlags[i] = 1;
688 result = true;
689 }
690 }
691 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
692 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
693 outFlags[i] = 1;
694 result = true;
695 }
696 }
697 }
698 }
699 return result;
700 }
701
702 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
703 Device* device = getDevice(deviceId);
704 if (device) {
705 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
706 return index >= 0;
707 }
708 return false;
709 }
710
711 virtual bool hasLed(int32_t deviceId, int32_t led) const {
712 Device* device = getDevice(deviceId);
713 return device && device->leds.indexOfKey(led) >= 0;
714 }
715
716 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
717 Device* device = getDevice(deviceId);
718 if (device) {
719 ssize_t index = device->leds.indexOfKey(led);
720 if (index >= 0) {
721 device->leds.replaceValueAt(led, on);
722 } else {
723 ADD_FAILURE()
724 << "Attempted to set the state of an LED that the EventHub declared "
725 "was not present. led=" << led;
726 }
727 }
728 }
729
730 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800731 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732 outVirtualKeys.clear();
733
734 Device* device = getDevice(deviceId);
735 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800736 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 }
738 }
739
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100740 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700741 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 }
743
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100744 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 return false;
746 }
747
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100748 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 }
750
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100751 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 }
753
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100754 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 return false;
756 }
757
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800758 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800759 }
760
761 virtual void monitor() {
762 }
763
764 virtual void requestReopenDevices() {
765 }
766
767 virtual void wake() {
768 }
769};
770
771
772// --- FakeInputReaderContext ---
773
774class FakeInputReaderContext : public InputReaderContext {
775 sp<EventHubInterface> mEventHub;
776 sp<InputReaderPolicyInterface> mPolicy;
777 sp<InputListenerInterface> mListener;
778 int32_t mGlobalMetaState;
779 bool mUpdateGlobalMetaStateWasCalled;
780 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800781 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800782
783public:
784 FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
785 const sp<InputReaderPolicyInterface>& policy,
786 const sp<InputListenerInterface>& listener) :
787 mEventHub(eventHub), mPolicy(policy), mListener(listener),
Prabir Pradhan42611e02018-11-27 14:04:02 -0800788 mGlobalMetaState(0), mNextSequenceNum(1) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789 }
790
791 virtual ~FakeInputReaderContext() { }
792
793 void assertUpdateGlobalMetaStateWasCalled() {
794 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
795 << "Expected updateGlobalMetaState() to have been called.";
796 mUpdateGlobalMetaStateWasCalled = false;
797 }
798
799 void setGlobalMetaState(int32_t state) {
800 mGlobalMetaState = state;
801 }
802
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800803 uint32_t getGeneration() {
804 return mGeneration;
805 }
806
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807private:
808 virtual void updateGlobalMetaState() {
809 mUpdateGlobalMetaStateWasCalled = true;
810 }
811
812 virtual int32_t getGlobalMetaState() {
813 return mGlobalMetaState;
814 }
815
816 virtual EventHubInterface* getEventHub() {
817 return mEventHub.get();
818 }
819
820 virtual InputReaderPolicyInterface* getPolicy() {
821 return mPolicy.get();
822 }
823
824 virtual InputListenerInterface* getListener() {
825 return mListener.get();
826 }
827
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100828 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 }
830
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100831 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800832 return false;
833 }
834
835 virtual void fadePointer() {
836 }
837
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100838 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839 }
840
841 virtual int32_t bumpGeneration() {
842 return ++mGeneration;
843 }
Michael Wright842500e2015-03-13 17:32:02 -0700844
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800845 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700846
847 }
848
849 virtual void dispatchExternalStylusState(const StylusState&) {
850
851 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800852
853 virtual uint32_t getNextSequenceNum() {
854 return mNextSequenceNum++;
855 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856};
857
858
859// --- FakeInputMapper ---
860
861class FakeInputMapper : public InputMapper {
862 uint32_t mSources;
863 int32_t mKeyboardType;
864 int32_t mMetaState;
865 KeyedVector<int32_t, int32_t> mKeyCodeStates;
866 KeyedVector<int32_t, int32_t> mScanCodeStates;
867 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800868 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 RawEvent mLastEvent;
870
871 bool mConfigureWasCalled;
872 bool mResetWasCalled;
873 bool mProcessWasCalled;
874
Arthur Hungc23540e2018-11-29 20:42:11 +0800875 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876public:
877 FakeInputMapper(InputDevice* device, uint32_t sources) :
878 InputMapper(device),
879 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
880 mMetaState(0),
881 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
882 }
883
884 virtual ~FakeInputMapper() { }
885
886 void setKeyboardType(int32_t keyboardType) {
887 mKeyboardType = keyboardType;
888 }
889
890 void setMetaState(int32_t metaState) {
891 mMetaState = metaState;
892 }
893
894 void assertConfigureWasCalled() {
895 ASSERT_TRUE(mConfigureWasCalled)
896 << "Expected configure() to have been called.";
897 mConfigureWasCalled = false;
898 }
899
900 void assertResetWasCalled() {
901 ASSERT_TRUE(mResetWasCalled)
902 << "Expected reset() to have been called.";
903 mResetWasCalled = false;
904 }
905
Yi Kong9b14ac62018-07-17 13:48:38 -0700906 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 ASSERT_TRUE(mProcessWasCalled)
908 << "Expected process() to have been called.";
909 if (outLastEvent) {
910 *outLastEvent = mLastEvent;
911 }
912 mProcessWasCalled = false;
913 }
914
915 void setKeyCodeState(int32_t keyCode, int32_t state) {
916 mKeyCodeStates.replaceValueFor(keyCode, state);
917 }
918
919 void setScanCodeState(int32_t scanCode, int32_t state) {
920 mScanCodeStates.replaceValueFor(scanCode, state);
921 }
922
923 void setSwitchState(int32_t switchCode, int32_t state) {
924 mSwitchStates.replaceValueFor(switchCode, state);
925 }
926
927 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800928 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 }
930
931private:
932 virtual uint32_t getSources() {
933 return mSources;
934 }
935
936 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
937 InputMapper::populateDeviceInfo(deviceInfo);
938
939 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
940 deviceInfo->setKeyboardType(mKeyboardType);
941 }
942 }
943
Arthur Hungc23540e2018-11-29 20:42:11 +0800944 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800946
947 // Find the associated viewport if exist.
948 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
949 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
950 mViewport = config->getDisplayViewportByPort(*displayPort);
951 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 }
953
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100954 virtual void reset(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 mResetWasCalled = true;
956 }
957
958 virtual void process(const RawEvent* rawEvent) {
959 mLastEvent = *rawEvent;
960 mProcessWasCalled = true;
961 }
962
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100963 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
965 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
966 }
967
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100968 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
970 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
971 }
972
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100973 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 ssize_t index = mSwitchStates.indexOfKey(switchCode);
975 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
976 }
977
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100978 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979 const int32_t* keyCodes, uint8_t* outFlags) {
980 bool result = false;
981 for (size_t i = 0; i < numCodes; i++) {
982 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
983 if (keyCodes[i] == mSupportedKeyCodes[j]) {
984 outFlags[i] = 1;
985 result = true;
986 }
987 }
988 }
989 return result;
990 }
991
992 virtual int32_t getMetaState() {
993 return mMetaState;
994 }
995
996 virtual void fadePointer() {
997 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800998
999 virtual std::optional<int32_t> getAssociatedDisplay() {
1000 if (mViewport) {
1001 return std::make_optional(mViewport->displayId);
1002 }
1003 return std::nullopt;
1004 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005};
1006
1007
1008// --- InstrumentedInputReader ---
1009
1010class InstrumentedInputReader : public InputReader {
1011 InputDevice* mNextDevice;
1012
1013public:
1014 InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
1015 const sp<InputReaderPolicyInterface>& policy,
1016 const sp<InputListenerInterface>& listener) :
1017 InputReader(eventHub, policy, listener),
Yi Kong9b14ac62018-07-17 13:48:38 -07001018 mNextDevice(nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019 }
1020
1021 virtual ~InstrumentedInputReader() {
1022 if (mNextDevice) {
1023 delete mNextDevice;
1024 }
1025 }
1026
1027 void setNextDevice(InputDevice* device) {
1028 mNextDevice = device;
1029 }
1030
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001031 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Arthur Hungc23540e2018-11-29 20:42:11 +08001032 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001033 InputDeviceIdentifier identifier;
1034 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001035 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036 int32_t generation = deviceId + 1;
1037 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
1038 classes);
1039 }
1040
1041protected:
1042 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
1043 const InputDeviceIdentifier& identifier, uint32_t classes) {
1044 if (mNextDevice) {
1045 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001046 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047 return device;
1048 }
1049 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1050 }
1051
1052 friend class InputReaderTest;
1053};
1054
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001055// --- InputReaderPolicyTest ---
1056class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001057protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001058 sp<FakeInputReaderPolicy> mFakePolicy;
1059
1060 virtual void SetUp() {
1061 mFakePolicy = new FakeInputReaderPolicy();
1062 }
1063 virtual void TearDown() {
1064 mFakePolicy.clear();
1065 }
1066};
1067
1068/**
1069 * Check that empty set of viewports is an acceptable configuration.
1070 * Also try to get internal viewport two different ways - by type and by uniqueId.
1071 *
1072 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1073 * Such configuration is not currently allowed.
1074 */
1075TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001076 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001077
1078 // We didn't add any viewports yet, so there shouldn't be any.
1079 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001080 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001081 ASSERT_FALSE(internalViewport);
1082
1083 // Add an internal viewport, then clear it
1084 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001085 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001086
1087 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001088 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001089 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001090 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001091
1092 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001093 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001094 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001095 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001096
1097 mFakePolicy->clearViewports();
1098 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001099 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001100 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001101 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001102 ASSERT_FALSE(internalViewport);
1103}
1104
1105TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1106 const std::string internalUniqueId = "local:0";
1107 const std::string externalUniqueId = "local:1";
1108 const std::string virtualUniqueId1 = "virtual:2";
1109 const std::string virtualUniqueId2 = "virtual:3";
1110 constexpr int32_t virtualDisplayId1 = 2;
1111 constexpr int32_t virtualDisplayId2 = 3;
1112
1113 // Add an internal viewport
1114 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001115 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001116 // Add an external viewport
1117 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001118 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001119 // Add an virtual viewport
1120 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001121 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001122 // Add another virtual viewport
1123 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001124 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001125
1126 // Check matching by type for internal
1127 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001128 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001129 ASSERT_TRUE(internalViewport);
1130 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1131
1132 // Check matching by type for external
1133 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001134 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001135 ASSERT_TRUE(externalViewport);
1136 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1137
1138 // Check matching by uniqueId for virtual viewport #1
1139 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001140 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001141 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001142 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001143 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1144 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1145
1146 // Check matching by uniqueId for virtual viewport #2
1147 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001148 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001149 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001150 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001151 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1152 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1153}
1154
1155
1156/**
1157 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1158 * that lookup works by checking display id.
1159 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1160 */
1161TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1162 const std::string uniqueId1 = "uniqueId1";
1163 const std::string uniqueId2 = "uniqueId2";
1164 constexpr int32_t displayId1 = 2;
1165 constexpr int32_t displayId2 = 3;
1166
1167 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1168 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1169 for (const ViewportType& type : types) {
1170 mFakePolicy->clearViewports();
1171 // Add a viewport
1172 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001173 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001174 // Add another viewport
1175 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001176 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177
1178 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001179 std::optional<DisplayViewport> viewport1 =
1180 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181 ASSERT_TRUE(viewport1);
1182 ASSERT_EQ(displayId1, viewport1->displayId);
1183 ASSERT_EQ(type, viewport1->type);
1184
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001185 std::optional<DisplayViewport> viewport2 =
1186 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187 ASSERT_TRUE(viewport2);
1188 ASSERT_EQ(displayId2, viewport2->displayId);
1189 ASSERT_EQ(type, viewport2->type);
1190
1191 // When there are multiple viewports of the same kind, and uniqueId is not specified
1192 // in the call to getDisplayViewport, then that situation is not supported.
1193 // The viewports can be stored in any order, so we cannot rely on the order, since that
1194 // is just implementation detail.
1195 // However, we can check that it still returns *a* viewport, we just cannot assert
1196 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001197 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001198 ASSERT_TRUE(someViewport);
1199 }
1200}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001202/**
1203 * Check getDisplayViewportByPort
1204 */
1205TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1206 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1207 const std::string uniqueId1 = "uniqueId1";
1208 const std::string uniqueId2 = "uniqueId2";
1209 constexpr int32_t displayId1 = 1;
1210 constexpr int32_t displayId2 = 2;
1211 const uint8_t hdmi1 = 0;
1212 const uint8_t hdmi2 = 1;
1213 const uint8_t hdmi3 = 2;
1214
1215 mFakePolicy->clearViewports();
1216 // Add a viewport that's associated with some display port that's not of interest.
1217 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1218 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1219 // Add another viewport, connected to HDMI1 port
1220 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1221 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1222
1223 // Check that correct display viewport was returned by comparing the display ports.
1224 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1225 ASSERT_TRUE(hdmi1Viewport);
1226 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1227 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1228
1229 // Check that we can still get the same viewport using the uniqueId
1230 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1231 ASSERT_TRUE(hdmi1Viewport);
1232 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1233 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1234 ASSERT_EQ(type, hdmi1Viewport->type);
1235
1236 // Check that we cannot find a port with "HDMI2", because we never added one
1237 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1238 ASSERT_FALSE(hdmi2Viewport);
1239}
1240
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241// --- InputReaderTest ---
1242
1243class InputReaderTest : public testing::Test {
1244protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001245 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246 sp<FakeInputReaderPolicy> mFakePolicy;
1247 sp<FakeEventHub> mFakeEventHub;
1248 sp<InstrumentedInputReader> mReader;
1249
1250 virtual void SetUp() {
1251 mFakeEventHub = new FakeEventHub();
1252 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001253 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254
1255 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1256 }
1257
1258 virtual void TearDown() {
1259 mReader.clear();
1260
1261 mFakeListener.clear();
1262 mFakePolicy.clear();
1263 mFakeEventHub.clear();
1264 }
1265
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001266 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267 const PropertyMap* configuration) {
1268 mFakeEventHub->addDevice(deviceId, name, classes);
1269
1270 if (configuration) {
1271 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1272 }
1273 mFakeEventHub->finishDeviceScan();
1274 mReader->loopOnce();
1275 mReader->loopOnce();
1276 mFakeEventHub->assertQueueIsEmpty();
1277 }
1278
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001279 void disableDevice(int32_t deviceId, InputDevice* device) {
1280 mFakePolicy->addDisabledDevice(deviceId);
1281 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1282 }
1283
1284 void enableDevice(int32_t deviceId, InputDevice* device) {
1285 mFakePolicy->removeDisabledDevice(deviceId);
1286 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1287 }
1288
1289 void configureDevice(uint32_t changes, InputDevice* device) {
1290 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1291 }
1292
Michael Wrightd02c5b62014-02-10 15:10:22 -08001293 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001294 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295 const PropertyMap* configuration) {
1296 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1297 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1298 device->addMapper(mapper);
1299 mReader->setNextDevice(device);
1300 addDevice(deviceId, name, classes, configuration);
1301 return mapper;
1302 }
1303};
1304
1305TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001306 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001307 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001308 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001309 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001310
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001311
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001312 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313 mReader->getInputDevices(inputDevices);
1314
1315 ASSERT_EQ(1U, inputDevices.size());
1316 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001317 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1319 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1320 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1321
1322 // Should also have received a notification describing the new input devices.
1323 inputDevices = mFakePolicy->getInputDevices();
1324 ASSERT_EQ(1U, inputDevices.size());
1325 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001326 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001327 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1328 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1329 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1330}
1331
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001332TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1333 constexpr int32_t deviceId = 1;
1334 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001335 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001336 // Must add at least one mapper or the device will be ignored!
1337 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1338 device->addMapper(mapper);
1339 mReader->setNextDevice(device);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001340 addDevice(deviceId, "fake", deviceClass, nullptr);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001341
Yi Kong9b14ac62018-07-17 13:48:38 -07001342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001343
1344 NotifyDeviceResetArgs resetArgs;
1345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1346 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1347 ASSERT_EQ(deviceId, resetArgs.deviceId);
1348
1349 ASSERT_EQ(device->isEnabled(), true);
1350 disableDevice(deviceId, device);
1351 mReader->loopOnce();
1352
1353 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1354 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1355 ASSERT_EQ(deviceId, resetArgs.deviceId);
1356 ASSERT_EQ(device->isEnabled(), false);
1357
1358 disableDevice(deviceId, device);
1359 mReader->loopOnce();
1360 mFakeListener->assertNotifyDeviceResetWasNotCalled();
1361 mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1362 ASSERT_EQ(device->isEnabled(), false);
1363
1364 enableDevice(deviceId, device);
1365 mReader->loopOnce();
1366 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1367 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1368 ASSERT_EQ(deviceId, resetArgs.deviceId);
1369 ASSERT_EQ(device->isEnabled(), true);
1370}
1371
Michael Wrightd02c5b62014-02-10 15:10:22 -08001372TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001373 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001374 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001375 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001376 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1377
1378 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1379 AINPUT_SOURCE_ANY, AKEYCODE_A))
1380 << "Should return unknown when the device id is >= 0 but unknown.";
1381
1382 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1383 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1384 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1385
1386 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1387 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1388 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1389
1390 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1391 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1392 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1393
1394 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1395 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1396 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1397}
1398
1399TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001400 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001401 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001402 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001403 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1404
1405 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1406 AINPUT_SOURCE_ANY, KEY_A))
1407 << "Should return unknown when the device id is >= 0 but unknown.";
1408
1409 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1410 AINPUT_SOURCE_TRACKBALL, KEY_A))
1411 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1412
1413 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1414 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1415 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1416
1417 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1418 AINPUT_SOURCE_TRACKBALL, KEY_A))
1419 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1420
1421 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1422 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1423 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1424}
1425
1426TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001427 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001428 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001429 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001430 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1431
1432 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1433 AINPUT_SOURCE_ANY, SW_LID))
1434 << "Should return unknown when the device id is >= 0 but unknown.";
1435
1436 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1437 AINPUT_SOURCE_TRACKBALL, SW_LID))
1438 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1439
1440 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1441 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1442 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1443
1444 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1445 AINPUT_SOURCE_TRACKBALL, SW_LID))
1446 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1447
1448 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1449 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1450 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1451}
1452
1453TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001454 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001455 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001456 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001457
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458 mapper->addSupportedKeyCode(AKEYCODE_A);
1459 mapper->addSupportedKeyCode(AKEYCODE_B);
1460
1461 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1462 uint8_t flags[4] = { 0, 0, 0, 1 };
1463
1464 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1465 << "Should return false when device id is >= 0 but unknown.";
1466 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1467
1468 flags[3] = 1;
1469 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1470 << "Should return false when device id is valid but the sources are not supported by the device.";
1471 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1472
1473 flags[3] = 1;
1474 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1475 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1476 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1477
1478 flags[3] = 1;
1479 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1480 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1481 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1482
1483 flags[3] = 1;
1484 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1485 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1486 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1487}
1488
1489TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001490 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001491
1492 NotifyConfigurationChangedArgs args;
1493
1494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1495 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1496}
1497
1498TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001499 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001500 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001501 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001502
1503 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1504 mReader->loopOnce();
1505 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1506
1507 RawEvent event;
1508 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1509 ASSERT_EQ(0, event.when);
1510 ASSERT_EQ(1, event.deviceId);
1511 ASSERT_EQ(EV_KEY, event.type);
1512 ASSERT_EQ(KEY_A, event.code);
1513 ASSERT_EQ(1, event.value);
1514}
1515
Prabir Pradhan42611e02018-11-27 14:04:02 -08001516TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1517 constexpr int32_t deviceId = 1;
1518 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001519 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001520 // Must add at least one mapper or the device will be ignored!
1521 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1522 device->addMapper(mapper);
1523 mReader->setNextDevice(device);
1524 addDevice(deviceId, "fake", deviceClass, nullptr);
1525
1526 NotifyDeviceResetArgs resetArgs;
1527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1528 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1529
1530 disableDevice(deviceId, device);
1531 mReader->loopOnce();
1532 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1533 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1534 prevSequenceNum = resetArgs.sequenceNum;
1535
1536 enableDevice(deviceId, device);
1537 mReader->loopOnce();
1538 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1539 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1540 prevSequenceNum = resetArgs.sequenceNum;
1541
1542 disableDevice(deviceId, device);
1543 mReader->loopOnce();
1544 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1545 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1546 prevSequenceNum = resetArgs.sequenceNum;
1547}
1548
Arthur Hungc23540e2018-11-29 20:42:11 +08001549TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1550 constexpr int32_t deviceId = 1;
1551 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1552 const char* DEVICE_LOCATION = "USB1";
1553 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1554 DEVICE_LOCATION);
1555 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1556 device->addMapper(mapper);
1557 mReader->setNextDevice(device);
1558 addDevice(deviceId, "fake", deviceClass, nullptr);
1559
1560 const uint8_t hdmi1 = 1;
1561
1562 // Associated touch screen with second display.
1563 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1564
1565 // Add default and second display.
1566 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1567 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1568 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1569 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1570 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1571 mReader->loopOnce();
1572
1573 // Check device.
1574 ASSERT_EQ(deviceId, device->getId());
1575 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1576 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
1577}
1578
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579
1580// --- InputDeviceTest ---
1581
1582class InputDeviceTest : public testing::Test {
1583protected:
1584 static const char* DEVICE_NAME;
1585 static const int32_t DEVICE_ID;
1586 static const int32_t DEVICE_GENERATION;
1587 static const int32_t DEVICE_CONTROLLER_NUMBER;
1588 static const uint32_t DEVICE_CLASSES;
1589
1590 sp<FakeEventHub> mFakeEventHub;
1591 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001592 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593 FakeInputReaderContext* mFakeContext;
1594
1595 InputDevice* mDevice;
1596
1597 virtual void SetUp() {
1598 mFakeEventHub = new FakeEventHub();
1599 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001600 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1602
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001603 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604 InputDeviceIdentifier identifier;
1605 identifier.name = DEVICE_NAME;
1606 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1607 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1608 }
1609
1610 virtual void TearDown() {
1611 delete mDevice;
1612
1613 delete mFakeContext;
1614 mFakeListener.clear();
1615 mFakePolicy.clear();
1616 mFakeEventHub.clear();
1617 }
1618};
1619
1620const char* InputDeviceTest::DEVICE_NAME = "device";
1621const int32_t InputDeviceTest::DEVICE_ID = 1;
1622const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1623const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1624const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1625 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1626
1627TEST_F(InputDeviceTest, ImmutableProperties) {
1628 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001629 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1631}
1632
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001633TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1634 ASSERT_EQ(mDevice->isEnabled(), true);
1635}
1636
Michael Wrightd02c5b62014-02-10 15:10:22 -08001637TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1638 // Configuration.
1639 InputReaderConfiguration config;
1640 mDevice->configure(ARBITRARY_TIME, &config, 0);
1641
1642 // Reset.
1643 mDevice->reset(ARBITRARY_TIME);
1644
1645 NotifyDeviceResetArgs resetArgs;
1646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1647 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1648 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1649
1650 // Metadata.
1651 ASSERT_TRUE(mDevice->isIgnored());
1652 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1653
1654 InputDeviceInfo info;
1655 mDevice->getDeviceInfo(&info);
1656 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001657 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1659 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1660
1661 // State queries.
1662 ASSERT_EQ(0, mDevice->getMetaState());
1663
1664 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1665 << "Ignored device should return unknown key code state.";
1666 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1667 << "Ignored device should return unknown scan code state.";
1668 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1669 << "Ignored device should return unknown switch state.";
1670
1671 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1672 uint8_t flags[2] = { 0, 1 };
1673 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1674 << "Ignored device should never mark any key codes.";
1675 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1676 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1677}
1678
1679TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1680 // Configuration.
1681 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1682
1683 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1684 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1685 mapper1->setMetaState(AMETA_ALT_ON);
1686 mapper1->addSupportedKeyCode(AKEYCODE_A);
1687 mapper1->addSupportedKeyCode(AKEYCODE_B);
1688 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1689 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1690 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1691 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1692 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1693 mDevice->addMapper(mapper1);
1694
1695 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1696 mapper2->setMetaState(AMETA_SHIFT_ON);
1697 mDevice->addMapper(mapper2);
1698
1699 InputReaderConfiguration config;
1700 mDevice->configure(ARBITRARY_TIME, &config, 0);
1701
1702 String8 propertyValue;
1703 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1704 << "Device should have read configuration during configuration phase.";
1705 ASSERT_STREQ("value", propertyValue.string());
1706
1707 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1708 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1709
1710 // Reset
1711 mDevice->reset(ARBITRARY_TIME);
1712 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1713 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1714
1715 NotifyDeviceResetArgs resetArgs;
1716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1717 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1718 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1719
1720 // Metadata.
1721 ASSERT_FALSE(mDevice->isIgnored());
1722 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1723
1724 InputDeviceInfo info;
1725 mDevice->getDeviceInfo(&info);
1726 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001727 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001728 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1729 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1730
1731 // State queries.
1732 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1733 << "Should query mappers and combine meta states.";
1734
1735 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1736 << "Should return unknown key code state when source not supported.";
1737 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1738 << "Should return unknown scan code state when source not supported.";
1739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1740 << "Should return unknown switch state when source not supported.";
1741
1742 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1743 << "Should query mapper when source is supported.";
1744 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1745 << "Should query mapper when source is supported.";
1746 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1747 << "Should query mapper when source is supported.";
1748
1749 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1750 uint8_t flags[4] = { 0, 0, 0, 1 };
1751 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1752 << "Should do nothing when source is unsupported.";
1753 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1754 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1755 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1756 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1757
1758 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1759 << "Should query mapper when source is supported.";
1760 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1761 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1762 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1763 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1764
1765 // Event handling.
1766 RawEvent event;
1767 mDevice->process(&event, 1);
1768
1769 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1770 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1771}
1772
1773
1774// --- InputMapperTest ---
1775
1776class InputMapperTest : public testing::Test {
1777protected:
1778 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001779 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001780 static const int32_t DEVICE_ID;
1781 static const int32_t DEVICE_GENERATION;
1782 static const int32_t DEVICE_CONTROLLER_NUMBER;
1783 static const uint32_t DEVICE_CLASSES;
1784
1785 sp<FakeEventHub> mFakeEventHub;
1786 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001787 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001788 FakeInputReaderContext* mFakeContext;
1789 InputDevice* mDevice;
1790
1791 virtual void SetUp() {
1792 mFakeEventHub = new FakeEventHub();
1793 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001794 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001795 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1796 InputDeviceIdentifier identifier;
1797 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001798 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1800 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1801
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001802 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803 }
1804
1805 virtual void TearDown() {
1806 delete mDevice;
1807 delete mFakeContext;
1808 mFakeListener.clear();
1809 mFakePolicy.clear();
1810 mFakeEventHub.clear();
1811 }
1812
1813 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001814 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001815 }
1816
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001817 void configureDevice(uint32_t changes) {
1818 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1819 }
1820
Michael Wrightd02c5b62014-02-10 15:10:22 -08001821 void addMapperAndConfigure(InputMapper* mapper) {
1822 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001823 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824 mDevice->reset(ARBITRARY_TIME);
1825 }
1826
1827 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001828 int32_t orientation, const std::string& uniqueId,
1829 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001830 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001831 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001832 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1833 }
1834
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001835 void clearViewports() {
1836 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837 }
1838
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001839 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001840 int32_t code, int32_t value) {
1841 RawEvent event;
1842 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001843 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001844 event.type = type;
1845 event.code = code;
1846 event.value = value;
1847 mapper->process(&event);
1848 }
1849
1850 static void assertMotionRange(const InputDeviceInfo& info,
1851 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1852 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001853 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001854 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1855 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1856 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1857 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1858 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1859 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1860 }
1861
1862 static void assertPointerCoords(const PointerCoords& coords,
1863 float x, float y, float pressure, float size,
1864 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1865 float orientation, float distance) {
1866 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1867 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1868 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1869 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1870 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1871 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1872 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1873 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1874 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1875 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1876 }
1877
1878 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1879 float actualX, actualY;
1880 controller->getPosition(&actualX, &actualY);
1881 ASSERT_NEAR(x, actualX, 1);
1882 ASSERT_NEAR(y, actualY, 1);
1883 }
1884};
1885
1886const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001887const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001888const int32_t InputMapperTest::DEVICE_ID = 1;
1889const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1890const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1891const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1892
1893
1894// --- SwitchInputMapperTest ---
1895
1896class SwitchInputMapperTest : public InputMapperTest {
1897protected:
1898};
1899
1900TEST_F(SwitchInputMapperTest, GetSources) {
1901 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1902 addMapperAndConfigure(mapper);
1903
1904 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1905}
1906
1907TEST_F(SwitchInputMapperTest, GetSwitchState) {
1908 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1909 addMapperAndConfigure(mapper);
1910
1911 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1912 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1913
1914 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1915 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1916}
1917
1918TEST_F(SwitchInputMapperTest, Process) {
1919 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1920 addMapperAndConfigure(mapper);
1921
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001922 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
1923 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1924 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
1925 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926
1927 NotifySwitchArgs args;
1928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1929 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08001930 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1931 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001932 args.switchMask);
1933 ASSERT_EQ(uint32_t(0), args.policyFlags);
1934}
1935
1936
1937// --- KeyboardInputMapperTest ---
1938
1939class KeyboardInputMapperTest : public InputMapperTest {
1940protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001941 const std::string UNIQUE_ID = "local:0";
1942
1943 void prepareDisplay(int32_t orientation);
1944
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945 void testDPadKeyRotation(KeyboardInputMapper* mapper,
1946 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1947};
1948
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001949/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
1950 * orientation.
1951 */
1952void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
1953 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001954 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001955}
1956
Michael Wrightd02c5b62014-02-10 15:10:22 -08001957void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001958 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001959 NotifyKeyArgs args;
1960
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001961 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1963 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1964 ASSERT_EQ(originalScanCode, args.scanCode);
1965 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1966
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001967 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1969 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1970 ASSERT_EQ(originalScanCode, args.scanCode);
1971 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1972}
1973
1974
1975TEST_F(KeyboardInputMapperTest, GetSources) {
1976 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1977 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1978 addMapperAndConfigure(mapper);
1979
1980 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1981}
1982
1983TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
1984 const int32_t USAGE_A = 0x070004;
1985 const int32_t USAGE_UNKNOWN = 0x07ffff;
1986 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
1987 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
1988
1989 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1990 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1991 addMapperAndConfigure(mapper);
1992
1993 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001994 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001995 NotifyKeyArgs args;
1996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1997 ASSERT_EQ(DEVICE_ID, args.deviceId);
1998 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1999 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2000 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2001 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2002 ASSERT_EQ(KEY_HOME, args.scanCode);
2003 ASSERT_EQ(AMETA_NONE, args.metaState);
2004 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2005 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2006 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2007
2008 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002009 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2011 ASSERT_EQ(DEVICE_ID, args.deviceId);
2012 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2013 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2014 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2015 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2016 ASSERT_EQ(KEY_HOME, args.scanCode);
2017 ASSERT_EQ(AMETA_NONE, args.metaState);
2018 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2019 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2020 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2021
2022 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002023 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2024 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2026 ASSERT_EQ(DEVICE_ID, args.deviceId);
2027 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2028 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2029 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2030 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2031 ASSERT_EQ(0, args.scanCode);
2032 ASSERT_EQ(AMETA_NONE, args.metaState);
2033 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2034 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2035 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2036
2037 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002038 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2039 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2041 ASSERT_EQ(DEVICE_ID, args.deviceId);
2042 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2043 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2044 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2045 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2046 ASSERT_EQ(0, args.scanCode);
2047 ASSERT_EQ(AMETA_NONE, args.metaState);
2048 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2049 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2050 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2051
2052 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002053 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2054 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2056 ASSERT_EQ(DEVICE_ID, args.deviceId);
2057 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2058 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2059 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2060 ASSERT_EQ(0, args.keyCode);
2061 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2062 ASSERT_EQ(AMETA_NONE, args.metaState);
2063 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2064 ASSERT_EQ(0U, args.policyFlags);
2065 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2066
2067 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002068 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2069 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2071 ASSERT_EQ(DEVICE_ID, args.deviceId);
2072 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2073 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2074 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2075 ASSERT_EQ(0, args.keyCode);
2076 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2077 ASSERT_EQ(AMETA_NONE, args.metaState);
2078 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2079 ASSERT_EQ(0U, args.policyFlags);
2080 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2081}
2082
2083TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2084 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2085 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2086
2087 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2088 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2089 addMapperAndConfigure(mapper);
2090
2091 // Initial metastate.
2092 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2093
2094 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002095 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096 NotifyKeyArgs args;
2097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2098 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2100 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2101
2102 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002103 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2105 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2106 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2107
2108 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002109 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2111 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2112 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2113
2114 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002115 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2117 ASSERT_EQ(AMETA_NONE, args.metaState);
2118 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2119 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2120}
2121
2122TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2123 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2124 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2125 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2126 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2127
2128 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2129 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2130 addMapperAndConfigure(mapper);
2131
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002132 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2134 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2135 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2136 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2137 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2138 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2139 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2140 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2141}
2142
2143TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2144 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2145 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2146 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2147 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2148
2149 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2150 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2151 addConfigurationProperty("keyboard.orientationAware", "1");
2152 addMapperAndConfigure(mapper);
2153
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002154 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002155 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2156 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2157 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2158 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2159 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2160 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2161 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2162 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2163
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002164 clearViewports();
2165 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002166 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2167 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
2168 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2169 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
2170 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2171 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
2172 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2173 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
2174
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002175 clearViewports();
2176 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002177 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2178 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
2179 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2180 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
2181 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2182 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
2183 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2184 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
2185
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002186 clearViewports();
2187 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002188 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2189 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
2190 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2191 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
2192 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2193 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
2194 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2195 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
2196
2197 // Special case: if orientation changes while key is down, we still emit the same keycode
2198 // in the key up as we did in the key down.
2199 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002200 clearViewports();
2201 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002202 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2204 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2205 ASSERT_EQ(KEY_UP, args.scanCode);
2206 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2207
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002208 clearViewports();
2209 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002210 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2212 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2213 ASSERT_EQ(KEY_UP, args.scanCode);
2214 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2215}
2216
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002217TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2218 // If the keyboard is not orientation aware,
2219 // key events should not be associated with a specific display id
2220 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2221
2222 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2223 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2224 addMapperAndConfigure(mapper);
2225 NotifyKeyArgs args;
2226
2227 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002228 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002230 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2232 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2233
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002234 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002235 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002237 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2239 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2240}
2241
2242TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2243 // If the keyboard is orientation aware,
2244 // key events should be associated with the internal viewport
2245 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2246
2247 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2248 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2249 addConfigurationProperty("keyboard.orientationAware", "1");
2250 addMapperAndConfigure(mapper);
2251 NotifyKeyArgs args;
2252
2253 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2254 // ^--- already checked by the previous test
2255
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002256 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002257 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002258 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002260 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2262 ASSERT_EQ(DISPLAY_ID, args.displayId);
2263
2264 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002265 clearViewports();
2266 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002267 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002268 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002270 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2272 ASSERT_EQ(newDisplayId, args.displayId);
2273}
2274
Michael Wrightd02c5b62014-02-10 15:10:22 -08002275TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2276 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2277 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2278 addMapperAndConfigure(mapper);
2279
2280 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2281 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2282
2283 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2284 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2285}
2286
2287TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2288 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2289 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2290 addMapperAndConfigure(mapper);
2291
2292 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2293 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2294
2295 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2296 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2297}
2298
2299TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2300 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2301 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2302 addMapperAndConfigure(mapper);
2303
2304 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2305
2306 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2307 uint8_t flags[2] = { 0, 0 };
2308 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2309 ASSERT_TRUE(flags[0]);
2310 ASSERT_FALSE(flags[1]);
2311}
2312
2313TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2314 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2315 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2316 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2317 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2318 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2319 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2320
2321 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2322 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2323 addMapperAndConfigure(mapper);
2324
2325 // Initialization should have turned all of the lights off.
2326 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2327 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2328 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2329
2330 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002331 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2332 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002333 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2334 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2335 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2336 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2337
2338 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002339 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2340 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2342 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2343 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2344 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2345
2346 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002347 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2348 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002349 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2350 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2351 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2352 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2353
2354 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002355 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2356 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002357 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2358 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2359 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2360 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2361
2362 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002363 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2364 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2366 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2367 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2368 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2369
2370 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002371 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2372 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002373 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2374 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2375 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2376 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2377}
2378
2379
2380// --- CursorInputMapperTest ---
2381
2382class CursorInputMapperTest : public InputMapperTest {
2383protected:
2384 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2385
2386 sp<FakePointerController> mFakePointerController;
2387
2388 virtual void SetUp() {
2389 InputMapperTest::SetUp();
2390
2391 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002392 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393 }
2394
2395 void testMotionRotation(CursorInputMapper* mapper,
2396 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002397
2398 void prepareDisplay(int32_t orientation) {
2399 const std::string uniqueId = "local:0";
2400 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2401 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2402 orientation, uniqueId, NO_PORT, viewportType);
2403 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404};
2405
2406const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2407
2408void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2409 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2410 NotifyMotionArgs args;
2411
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002412 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2413 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2414 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2418 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2419 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2420 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2421}
2422
2423TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2424 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2425 addConfigurationProperty("cursor.mode", "pointer");
2426 addMapperAndConfigure(mapper);
2427
2428 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2429}
2430
2431TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2432 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2433 addConfigurationProperty("cursor.mode", "navigation");
2434 addMapperAndConfigure(mapper);
2435
2436 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2437}
2438
2439TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2440 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2441 addConfigurationProperty("cursor.mode", "pointer");
2442 addMapperAndConfigure(mapper);
2443
2444 InputDeviceInfo info;
2445 mapper->populateDeviceInfo(&info);
2446
2447 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002448 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2449 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2451 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2452
2453 // When the bounds are set, then there should be a valid motion range.
2454 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2455
2456 InputDeviceInfo info2;
2457 mapper->populateDeviceInfo(&info2);
2458
2459 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2460 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2461 1, 800 - 1, 0.0f, 0.0f));
2462 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2463 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2464 2, 480 - 1, 0.0f, 0.0f));
2465 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2466 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2467 0.0f, 1.0f, 0.0f, 0.0f));
2468}
2469
2470TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2471 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2472 addConfigurationProperty("cursor.mode", "navigation");
2473 addMapperAndConfigure(mapper);
2474
2475 InputDeviceInfo info;
2476 mapper->populateDeviceInfo(&info);
2477
2478 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2479 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2480 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2481 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2482 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2483 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2484 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2485 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2486 0.0f, 1.0f, 0.0f, 0.0f));
2487}
2488
2489TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2490 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2491 addConfigurationProperty("cursor.mode", "navigation");
2492 addMapperAndConfigure(mapper);
2493
2494 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2495
2496 NotifyMotionArgs args;
2497
2498 // Button press.
2499 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002500 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2501 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2503 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2504 ASSERT_EQ(DEVICE_ID, args.deviceId);
2505 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2506 ASSERT_EQ(uint32_t(0), args.policyFlags);
2507 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2508 ASSERT_EQ(0, args.flags);
2509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2510 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2511 ASSERT_EQ(0, args.edgeFlags);
2512 ASSERT_EQ(uint32_t(1), args.pointerCount);
2513 ASSERT_EQ(0, args.pointerProperties[0].id);
2514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2516 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2517 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2518 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2519 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2520
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2522 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2523 ASSERT_EQ(DEVICE_ID, args.deviceId);
2524 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2525 ASSERT_EQ(uint32_t(0), args.policyFlags);
2526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2527 ASSERT_EQ(0, args.flags);
2528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2529 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2530 ASSERT_EQ(0, args.edgeFlags);
2531 ASSERT_EQ(uint32_t(1), args.pointerCount);
2532 ASSERT_EQ(0, args.pointerProperties[0].id);
2533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2535 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2536 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2537 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2538 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2539
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002541 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2542 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2544 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2545 ASSERT_EQ(DEVICE_ID, args.deviceId);
2546 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2547 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002548 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2549 ASSERT_EQ(0, args.flags);
2550 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2551 ASSERT_EQ(0, args.buttonState);
2552 ASSERT_EQ(0, args.edgeFlags);
2553 ASSERT_EQ(uint32_t(1), args.pointerCount);
2554 ASSERT_EQ(0, args.pointerProperties[0].id);
2555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2557 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2558 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2559 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2560 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2561
2562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2563 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2564 ASSERT_EQ(DEVICE_ID, args.deviceId);
2565 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2566 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2568 ASSERT_EQ(0, args.flags);
2569 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2570 ASSERT_EQ(0, args.buttonState);
2571 ASSERT_EQ(0, args.edgeFlags);
2572 ASSERT_EQ(uint32_t(1), args.pointerCount);
2573 ASSERT_EQ(0, args.pointerProperties[0].id);
2574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2576 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2577 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2578 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2579 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2580}
2581
2582TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2583 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2584 addConfigurationProperty("cursor.mode", "navigation");
2585 addMapperAndConfigure(mapper);
2586
2587 NotifyMotionArgs args;
2588
2589 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002590 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2591 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2595 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2596
2597 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002598 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2599 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2603 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2604}
2605
2606TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2607 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2608 addConfigurationProperty("cursor.mode", "navigation");
2609 addMapperAndConfigure(mapper);
2610
2611 NotifyMotionArgs args;
2612
2613 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002614 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2615 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2619 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2620
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2622 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2624 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2625
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002627 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2628 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2632 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2633
2634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2637 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2638}
2639
2640TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2641 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2642 addConfigurationProperty("cursor.mode", "navigation");
2643 addMapperAndConfigure(mapper);
2644
2645 NotifyMotionArgs args;
2646
2647 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002648 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2649 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2650 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2651 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2653 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2655 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2656 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2657
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2659 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2661 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2662 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2663
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002665 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2666 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2667 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2671 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2672 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2673
2674 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002675 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2676 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2680 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2681
2682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2685 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2686}
2687
2688TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2689 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2690 addConfigurationProperty("cursor.mode", "navigation");
2691 addMapperAndConfigure(mapper);
2692
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002693 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2695 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2696 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2697 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2698 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2699 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2700 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2701 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2702}
2703
2704TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2705 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2706 addConfigurationProperty("cursor.mode", "navigation");
2707 addConfigurationProperty("cursor.orientationAware", "1");
2708 addMapperAndConfigure(mapper);
2709
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002710 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2712 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2713 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2714 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2715 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2716 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2717 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2718 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2719
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002720 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2722 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2723 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2724 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2725 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2726 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2727 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2728 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2729
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002730 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2732 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2733 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2734 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2735 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2736 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2737 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2738 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2739
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002740 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2742 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2743 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2744 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2745 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2746 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2747 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2748 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2749}
2750
2751TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2752 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2753 addConfigurationProperty("cursor.mode", "pointer");
2754 addMapperAndConfigure(mapper);
2755
2756 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2757 mFakePointerController->setPosition(100, 200);
2758 mFakePointerController->setButtonState(0);
2759
2760 NotifyMotionArgs motionArgs;
2761 NotifyKeyArgs keyArgs;
2762
2763 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002764 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2765 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2767 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2768 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2769 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2771 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2772
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2774 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2775 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2776 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2778 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2779
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002780 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2781 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 ASSERT_EQ(0, motionArgs.buttonState);
2785 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2787 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2788
2789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002790 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791 ASSERT_EQ(0, motionArgs.buttonState);
2792 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2794 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2795
2796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002798 ASSERT_EQ(0, motionArgs.buttonState);
2799 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2801 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2802
2803 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002804 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2805 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2806 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2808 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2809 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2810 motionArgs.buttonState);
2811 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2812 mFakePointerController->getButtonState());
2813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2814 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2815
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2817 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2818 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2819 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2820 mFakePointerController->getButtonState());
2821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2822 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2823
2824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2825 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2826 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2827 motionArgs.buttonState);
2828 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2829 mFakePointerController->getButtonState());
2830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2831 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2832
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002833 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
2834 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002836 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2838 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2840 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2841
2842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002844 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2845 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2847 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2848
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002849 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2850 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2853 ASSERT_EQ(0, motionArgs.buttonState);
2854 ASSERT_EQ(0, mFakePointerController->getButtonState());
2855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2856 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 -08002857 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2858 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002859
2860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861 ASSERT_EQ(0, motionArgs.buttonState);
2862 ASSERT_EQ(0, mFakePointerController->getButtonState());
2863 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2865 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 -08002866
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2868 ASSERT_EQ(0, motionArgs.buttonState);
2869 ASSERT_EQ(0, mFakePointerController->getButtonState());
2870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2872 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2873
2874 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002875 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
2876 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2878 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2879 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002880
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002882 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002883 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2884 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2886 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2887
2888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2890 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2891 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2893 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2894
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002895 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
2896 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002898 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002899 ASSERT_EQ(0, motionArgs.buttonState);
2900 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2902 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2903
2904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002906 ASSERT_EQ(0, motionArgs.buttonState);
2907 ASSERT_EQ(0, mFakePointerController->getButtonState());
2908
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2910 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2912 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2913 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2914
2915 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002916 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
2917 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2919 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2920 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002921
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002923 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2925 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2927 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2928
2929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2931 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2932 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2934 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2935
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002936 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
2937 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940 ASSERT_EQ(0, motionArgs.buttonState);
2941 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2943 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 -08002944
2945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2946 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2947 ASSERT_EQ(0, motionArgs.buttonState);
2948 ASSERT_EQ(0, mFakePointerController->getButtonState());
2949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2950 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2951
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2953 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2954 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2955
2956 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002957 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
2958 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2960 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2961 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002962
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002964 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2966 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2968 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2969
2970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2972 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2973 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2975 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2976
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002977 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
2978 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 ASSERT_EQ(0, motionArgs.buttonState);
2982 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2984 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 -08002985
2986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2987 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2988 ASSERT_EQ(0, motionArgs.buttonState);
2989 ASSERT_EQ(0, mFakePointerController->getButtonState());
2990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2991 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2992
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2994 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2995 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2996
2997 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002998 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
2999 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3001 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3002 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003003
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003005 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3007 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3009 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3010
3011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3013 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3014 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3016 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3017
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003018 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3019 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 ASSERT_EQ(0, motionArgs.buttonState);
3023 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3025 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 -08003026
3027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3028 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3029 ASSERT_EQ(0, motionArgs.buttonState);
3030 ASSERT_EQ(0, mFakePointerController->getButtonState());
3031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3032 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3033
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3035 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3036 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3037}
3038
3039TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3040 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3041 addConfigurationProperty("cursor.mode", "pointer");
3042 addMapperAndConfigure(mapper);
3043
3044 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3045 mFakePointerController->setPosition(100, 200);
3046 mFakePointerController->setButtonState(0);
3047
3048 NotifyMotionArgs args;
3049
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003050 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3051 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3052 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003054 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3055 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3057 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3058 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3059}
3060
3061TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3062 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3063 addConfigurationProperty("cursor.mode", "pointer");
3064 mFakePolicy->setPointerCapture(true);
3065 addMapperAndConfigure(mapper);
3066
3067 NotifyDeviceResetArgs resetArgs;
3068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3069 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3070 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3071
3072 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3073 mFakePointerController->setPosition(100, 200);
3074 mFakePointerController->setButtonState(0);
3075
3076 NotifyMotionArgs args;
3077
3078 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003079 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3080 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3081 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3083 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3086 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3087 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3088
3089 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003090 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3091 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3093 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3094 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3096 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3098 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3099 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3101 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3102
3103 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003104 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3105 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3107 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3110 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3112 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3113 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3115 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3116
3117 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003118 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3119 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3120 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3122 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3125 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3126 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3127
3128 // Disable pointer capture and check that the device generation got bumped
3129 // and events are generated the usual way.
3130 const uint32_t generation = mFakeContext->getGeneration();
3131 mFakePolicy->setPointerCapture(false);
3132 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3133 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3134
3135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3136 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3137 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3138
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003139 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3140 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3141 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3143 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3146 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3147 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3148}
3149
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003150TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3151 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3152 addMapperAndConfigure(mapper);
3153
3154 // Setup PointerController for second display.
3155 constexpr int32_t SECOND_DISPLAY_ID = 1;
3156 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3157 mFakePointerController->setPosition(100, 200);
3158 mFakePointerController->setButtonState(0);
3159 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3160
3161 NotifyMotionArgs args;
3162 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3163 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3164 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3166 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3167 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3169 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3170 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3171 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3172}
3173
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174
3175// --- TouchInputMapperTest ---
3176
3177class TouchInputMapperTest : public InputMapperTest {
3178protected:
3179 static const int32_t RAW_X_MIN;
3180 static const int32_t RAW_X_MAX;
3181 static const int32_t RAW_Y_MIN;
3182 static const int32_t RAW_Y_MAX;
3183 static const int32_t RAW_TOUCH_MIN;
3184 static const int32_t RAW_TOUCH_MAX;
3185 static const int32_t RAW_TOOL_MIN;
3186 static const int32_t RAW_TOOL_MAX;
3187 static const int32_t RAW_PRESSURE_MIN;
3188 static const int32_t RAW_PRESSURE_MAX;
3189 static const int32_t RAW_ORIENTATION_MIN;
3190 static const int32_t RAW_ORIENTATION_MAX;
3191 static const int32_t RAW_DISTANCE_MIN;
3192 static const int32_t RAW_DISTANCE_MAX;
3193 static const int32_t RAW_TILT_MIN;
3194 static const int32_t RAW_TILT_MAX;
3195 static const int32_t RAW_ID_MIN;
3196 static const int32_t RAW_ID_MAX;
3197 static const int32_t RAW_SLOT_MIN;
3198 static const int32_t RAW_SLOT_MAX;
3199 static const float X_PRECISION;
3200 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003201 static const float X_PRECISION_VIRTUAL;
3202 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203
3204 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003205 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206
3207 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3208
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003209 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003210 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003211
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212 enum Axes {
3213 POSITION = 1 << 0,
3214 TOUCH = 1 << 1,
3215 TOOL = 1 << 2,
3216 PRESSURE = 1 << 3,
3217 ORIENTATION = 1 << 4,
3218 MINOR = 1 << 5,
3219 ID = 1 << 6,
3220 DISTANCE = 1 << 7,
3221 TILT = 1 << 8,
3222 SLOT = 1 << 9,
3223 TOOL_TYPE = 1 << 10,
3224 };
3225
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003226 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3227 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003228 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003230 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231 int32_t toRawX(float displayX);
3232 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003233 float toCookedX(float rawX, float rawY);
3234 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003236 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003238 float toDisplayY(int32_t rawY, int32_t displayHeight);
3239
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240};
3241
3242const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3243const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3244const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3245const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3246const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3247const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3248const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3249const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003250const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3251const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3253const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3254const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3255const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3256const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3257const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3258const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3259const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3260const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3261const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3262const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3263const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003264const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3265 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3266const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3267 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003268const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3269 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
3271const float TouchInputMapperTest::GEOMETRIC_SCALE =
3272 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3273 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3274
3275const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3276 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3277 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3278};
3279
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003280void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003281 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003282 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3283}
3284
3285void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3286 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3287 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288}
3289
Santos Cordonfa5cf462017-04-05 10:37:00 -07003290void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003291 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3292 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003293 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003294}
3295
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296void TouchInputMapperTest::prepareVirtualKeys() {
3297 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3298 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3299 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3300 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3301}
3302
Jason Gerecke489fda82012-09-07 17:19:40 -07003303void TouchInputMapperTest::prepareLocationCalibration() {
3304 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3305}
3306
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307int32_t TouchInputMapperTest::toRawX(float displayX) {
3308 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3309}
3310
3311int32_t TouchInputMapperTest::toRawY(float displayY) {
3312 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3313}
3314
Jason Gerecke489fda82012-09-07 17:19:40 -07003315float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3316 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3317 return rawX;
3318}
3319
3320float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3321 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3322 return rawY;
3323}
3324
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003326 return toDisplayX(rawX, DISPLAY_WIDTH);
3327}
3328
3329float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3330 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331}
3332
3333float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003334 return toDisplayY(rawY, DISPLAY_HEIGHT);
3335}
3336
3337float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3338 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339}
3340
3341
3342// --- SingleTouchInputMapperTest ---
3343
3344class SingleTouchInputMapperTest : public TouchInputMapperTest {
3345protected:
3346 void prepareButtons();
3347 void prepareAxes(int axes);
3348
3349 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3350 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3351 void processUp(SingleTouchInputMapper* mappery);
3352 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3353 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3354 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3355 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3356 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3357 void processSync(SingleTouchInputMapper* mapper);
3358};
3359
3360void SingleTouchInputMapperTest::prepareButtons() {
3361 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3362}
3363
3364void SingleTouchInputMapperTest::prepareAxes(int axes) {
3365 if (axes & POSITION) {
3366 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3367 RAW_X_MIN, RAW_X_MAX, 0, 0);
3368 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3369 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3370 }
3371 if (axes & PRESSURE) {
3372 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3373 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3374 }
3375 if (axes & TOOL) {
3376 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3377 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3378 }
3379 if (axes & DISTANCE) {
3380 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3381 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3382 }
3383 if (axes & TILT) {
3384 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3385 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3386 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3387 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3388 }
3389}
3390
3391void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003392 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3393 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3394 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395}
3396
3397void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003398 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3399 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400}
3401
3402void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003403 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404}
3405
3406void SingleTouchInputMapperTest::processPressure(
3407 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003408 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409}
3410
3411void SingleTouchInputMapperTest::processToolMajor(
3412 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003413 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414}
3415
3416void SingleTouchInputMapperTest::processDistance(
3417 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003418 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419}
3420
3421void SingleTouchInputMapperTest::processTilt(
3422 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003423 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3424 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425}
3426
3427void SingleTouchInputMapperTest::processKey(
3428 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003429 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430}
3431
3432void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003433 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434}
3435
3436
3437TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3438 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3439 prepareButtons();
3440 prepareAxes(POSITION);
3441 addMapperAndConfigure(mapper);
3442
3443 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3444}
3445
3446TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3447 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3448 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3449 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3450 prepareButtons();
3451 prepareAxes(POSITION);
3452 addMapperAndConfigure(mapper);
3453
3454 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3455}
3456
3457TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3458 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3459 prepareButtons();
3460 prepareAxes(POSITION);
3461 addConfigurationProperty("touch.deviceType", "touchPad");
3462 addMapperAndConfigure(mapper);
3463
3464 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3465}
3466
3467TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3468 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3469 prepareButtons();
3470 prepareAxes(POSITION);
3471 addConfigurationProperty("touch.deviceType", "touchScreen");
3472 addMapperAndConfigure(mapper);
3473
3474 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3475}
3476
3477TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3478 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3479 addConfigurationProperty("touch.deviceType", "touchScreen");
3480 prepareDisplay(DISPLAY_ORIENTATION_0);
3481 prepareButtons();
3482 prepareAxes(POSITION);
3483 prepareVirtualKeys();
3484 addMapperAndConfigure(mapper);
3485
3486 // Unknown key.
3487 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3488
3489 // Virtual key is down.
3490 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3491 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3492 processDown(mapper, x, y);
3493 processSync(mapper);
3494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3495
3496 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3497
3498 // Virtual key is up.
3499 processUp(mapper);
3500 processSync(mapper);
3501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3502
3503 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3504}
3505
3506TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3507 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3508 addConfigurationProperty("touch.deviceType", "touchScreen");
3509 prepareDisplay(DISPLAY_ORIENTATION_0);
3510 prepareButtons();
3511 prepareAxes(POSITION);
3512 prepareVirtualKeys();
3513 addMapperAndConfigure(mapper);
3514
3515 // Unknown key.
3516 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3517
3518 // Virtual key is down.
3519 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3520 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3521 processDown(mapper, x, y);
3522 processSync(mapper);
3523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3524
3525 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3526
3527 // Virtual key is up.
3528 processUp(mapper);
3529 processSync(mapper);
3530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3531
3532 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3533}
3534
3535TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3536 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3537 addConfigurationProperty("touch.deviceType", "touchScreen");
3538 prepareDisplay(DISPLAY_ORIENTATION_0);
3539 prepareButtons();
3540 prepareAxes(POSITION);
3541 prepareVirtualKeys();
3542 addMapperAndConfigure(mapper);
3543
3544 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3545 uint8_t flags[2] = { 0, 0 };
3546 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3547 ASSERT_TRUE(flags[0]);
3548 ASSERT_FALSE(flags[1]);
3549}
3550
3551TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3552 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3553 addConfigurationProperty("touch.deviceType", "touchScreen");
3554 prepareDisplay(DISPLAY_ORIENTATION_0);
3555 prepareButtons();
3556 prepareAxes(POSITION);
3557 prepareVirtualKeys();
3558 addMapperAndConfigure(mapper);
3559
3560 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3561
3562 NotifyKeyArgs args;
3563
3564 // Press virtual key.
3565 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3566 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3567 processDown(mapper, x, y);
3568 processSync(mapper);
3569
3570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3571 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3572 ASSERT_EQ(DEVICE_ID, args.deviceId);
3573 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3574 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3575 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3576 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3577 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3578 ASSERT_EQ(KEY_HOME, args.scanCode);
3579 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3580 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3581
3582 // Release virtual key.
3583 processUp(mapper);
3584 processSync(mapper);
3585
3586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3587 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3588 ASSERT_EQ(DEVICE_ID, args.deviceId);
3589 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3590 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3591 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3592 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3593 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3594 ASSERT_EQ(KEY_HOME, args.scanCode);
3595 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3596 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3597
3598 // Should not have sent any motions.
3599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3600}
3601
3602TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3603 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3604 addConfigurationProperty("touch.deviceType", "touchScreen");
3605 prepareDisplay(DISPLAY_ORIENTATION_0);
3606 prepareButtons();
3607 prepareAxes(POSITION);
3608 prepareVirtualKeys();
3609 addMapperAndConfigure(mapper);
3610
3611 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3612
3613 NotifyKeyArgs keyArgs;
3614
3615 // Press virtual key.
3616 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3617 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3618 processDown(mapper, x, y);
3619 processSync(mapper);
3620
3621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3622 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3623 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3624 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3625 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3626 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3627 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3628 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3629 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3630 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3631 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3632
3633 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3634 // into the display area.
3635 y -= 100;
3636 processMove(mapper, x, y);
3637 processSync(mapper);
3638
3639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3640 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3641 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3642 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3643 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3644 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3645 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3646 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3647 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3648 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3649 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3650 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3651
3652 NotifyMotionArgs motionArgs;
3653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3655 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3656 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3657 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3658 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3659 ASSERT_EQ(0, motionArgs.flags);
3660 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3661 ASSERT_EQ(0, motionArgs.buttonState);
3662 ASSERT_EQ(0, motionArgs.edgeFlags);
3663 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3664 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3667 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3668 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3669 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3670 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3671
3672 // Keep moving out of bounds. Should generate a pointer move.
3673 y -= 50;
3674 processMove(mapper, x, y);
3675 processSync(mapper);
3676
3677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3678 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3679 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3680 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3681 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3683 ASSERT_EQ(0, motionArgs.flags);
3684 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3685 ASSERT_EQ(0, motionArgs.buttonState);
3686 ASSERT_EQ(0, motionArgs.edgeFlags);
3687 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3688 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3691 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3692 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3693 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3694 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3695
3696 // Release out of bounds. Should generate a pointer up.
3697 processUp(mapper);
3698 processSync(mapper);
3699
3700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3701 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3702 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3703 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3704 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3705 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3706 ASSERT_EQ(0, motionArgs.flags);
3707 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3708 ASSERT_EQ(0, motionArgs.buttonState);
3709 ASSERT_EQ(0, motionArgs.edgeFlags);
3710 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3711 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3714 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3715 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3716 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3717 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3718
3719 // Should not have sent any more keys or motions.
3720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3722}
3723
3724TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3725 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3726 addConfigurationProperty("touch.deviceType", "touchScreen");
3727 prepareDisplay(DISPLAY_ORIENTATION_0);
3728 prepareButtons();
3729 prepareAxes(POSITION);
3730 prepareVirtualKeys();
3731 addMapperAndConfigure(mapper);
3732
3733 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3734
3735 NotifyMotionArgs motionArgs;
3736
3737 // Initially go down out of bounds.
3738 int32_t x = -10;
3739 int32_t y = -10;
3740 processDown(mapper, x, y);
3741 processSync(mapper);
3742
3743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3744
3745 // Move into the display area. Should generate a pointer down.
3746 x = 50;
3747 y = 75;
3748 processMove(mapper, x, y);
3749 processSync(mapper);
3750
3751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3753 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3754 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3755 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3757 ASSERT_EQ(0, motionArgs.flags);
3758 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3759 ASSERT_EQ(0, motionArgs.buttonState);
3760 ASSERT_EQ(0, motionArgs.edgeFlags);
3761 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3762 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3765 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3766 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3767 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3768 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3769
3770 // Release. Should generate a pointer up.
3771 processUp(mapper);
3772 processSync(mapper);
3773
3774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3775 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3776 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3777 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3778 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3779 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3780 ASSERT_EQ(0, motionArgs.flags);
3781 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3782 ASSERT_EQ(0, motionArgs.buttonState);
3783 ASSERT_EQ(0, motionArgs.edgeFlags);
3784 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3785 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3788 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3789 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3790 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3791 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3792
3793 // Should not have sent any more keys or motions.
3794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3796}
3797
Santos Cordonfa5cf462017-04-05 10:37:00 -07003798TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3799 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3800 addConfigurationProperty("touch.deviceType", "touchScreen");
3801 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3802
3803 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3804 prepareButtons();
3805 prepareAxes(POSITION);
3806 prepareVirtualKeys();
3807 addMapperAndConfigure(mapper);
3808
3809 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3810
3811 NotifyMotionArgs motionArgs;
3812
3813 // Down.
3814 int32_t x = 100;
3815 int32_t y = 125;
3816 processDown(mapper, x, y);
3817 processSync(mapper);
3818
3819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3821 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3822 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3823 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3824 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3825 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3826 ASSERT_EQ(0, motionArgs.flags);
3827 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3828 ASSERT_EQ(0, motionArgs.buttonState);
3829 ASSERT_EQ(0, motionArgs.edgeFlags);
3830 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3831 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3834 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3835 1, 0, 0, 0, 0, 0, 0, 0));
3836 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3837 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3839
3840 // Move.
3841 x += 50;
3842 y += 75;
3843 processMove(mapper, x, y);
3844 processSync(mapper);
3845
3846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3847 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3848 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3849 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3850 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3851 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3853 ASSERT_EQ(0, motionArgs.flags);
3854 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3855 ASSERT_EQ(0, motionArgs.buttonState);
3856 ASSERT_EQ(0, motionArgs.edgeFlags);
3857 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3858 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3861 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3862 1, 0, 0, 0, 0, 0, 0, 0));
3863 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3864 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3865 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3866
3867 // Up.
3868 processUp(mapper);
3869 processSync(mapper);
3870
3871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3872 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3873 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3874 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3875 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3876 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3877 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3878 ASSERT_EQ(0, motionArgs.flags);
3879 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3880 ASSERT_EQ(0, motionArgs.buttonState);
3881 ASSERT_EQ(0, motionArgs.edgeFlags);
3882 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3883 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3886 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3887 1, 0, 0, 0, 0, 0, 0, 0));
3888 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3889 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3890 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3891
3892 // Should not have sent any more keys or motions.
3893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3895}
3896
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
3898 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3899 addConfigurationProperty("touch.deviceType", "touchScreen");
3900 prepareDisplay(DISPLAY_ORIENTATION_0);
3901 prepareButtons();
3902 prepareAxes(POSITION);
3903 prepareVirtualKeys();
3904 addMapperAndConfigure(mapper);
3905
3906 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3907
3908 NotifyMotionArgs motionArgs;
3909
3910 // Down.
3911 int32_t x = 100;
3912 int32_t y = 125;
3913 processDown(mapper, x, y);
3914 processSync(mapper);
3915
3916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3917 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3918 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3919 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3920 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3921 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3922 ASSERT_EQ(0, motionArgs.flags);
3923 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3924 ASSERT_EQ(0, motionArgs.buttonState);
3925 ASSERT_EQ(0, motionArgs.edgeFlags);
3926 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3927 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3930 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3931 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3932 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3933 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3934
3935 // Move.
3936 x += 50;
3937 y += 75;
3938 processMove(mapper, x, y);
3939 processSync(mapper);
3940
3941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3942 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3943 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3944 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3945 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3947 ASSERT_EQ(0, motionArgs.flags);
3948 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3949 ASSERT_EQ(0, motionArgs.buttonState);
3950 ASSERT_EQ(0, motionArgs.edgeFlags);
3951 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3952 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3955 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3956 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3957 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3958 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3959
3960 // Up.
3961 processUp(mapper);
3962 processSync(mapper);
3963
3964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3965 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3966 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3967 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3968 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3969 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3970 ASSERT_EQ(0, motionArgs.flags);
3971 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3972 ASSERT_EQ(0, motionArgs.buttonState);
3973 ASSERT_EQ(0, motionArgs.edgeFlags);
3974 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3975 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3978 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3979 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3980 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3981 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3982
3983 // Should not have sent any more keys or motions.
3984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3986}
3987
3988TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3989 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3990 addConfigurationProperty("touch.deviceType", "touchScreen");
3991 prepareButtons();
3992 prepareAxes(POSITION);
3993 addConfigurationProperty("touch.orientationAware", "0");
3994 addMapperAndConfigure(mapper);
3995
3996 NotifyMotionArgs args;
3997
3998 // Rotation 90.
3999 prepareDisplay(DISPLAY_ORIENTATION_90);
4000 processDown(mapper, toRawX(50), toRawY(75));
4001 processSync(mapper);
4002
4003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4004 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4005 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4006
4007 processUp(mapper);
4008 processSync(mapper);
4009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4010}
4011
4012TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4013 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4014 addConfigurationProperty("touch.deviceType", "touchScreen");
4015 prepareButtons();
4016 prepareAxes(POSITION);
4017 addMapperAndConfigure(mapper);
4018
4019 NotifyMotionArgs args;
4020
4021 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004022 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 prepareDisplay(DISPLAY_ORIENTATION_0);
4024 processDown(mapper, toRawX(50), toRawY(75));
4025 processSync(mapper);
4026
4027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4028 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4029 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4030
4031 processUp(mapper);
4032 processSync(mapper);
4033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4034
4035 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004036 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037 prepareDisplay(DISPLAY_ORIENTATION_90);
4038 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4039 processSync(mapper);
4040
4041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4042 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4043 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4044
4045 processUp(mapper);
4046 processSync(mapper);
4047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4048
4049 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004050 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051 prepareDisplay(DISPLAY_ORIENTATION_180);
4052 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4053 processSync(mapper);
4054
4055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4056 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4057 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4058
4059 processUp(mapper);
4060 processSync(mapper);
4061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4062
4063 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004064 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 prepareDisplay(DISPLAY_ORIENTATION_270);
4066 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4067 processSync(mapper);
4068
4069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4070 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4071 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4072
4073 processUp(mapper);
4074 processSync(mapper);
4075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4076}
4077
4078TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4079 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4080 addConfigurationProperty("touch.deviceType", "touchScreen");
4081 prepareDisplay(DISPLAY_ORIENTATION_0);
4082 prepareButtons();
4083 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4084 addMapperAndConfigure(mapper);
4085
4086 // These calculations are based on the input device calibration documentation.
4087 int32_t rawX = 100;
4088 int32_t rawY = 200;
4089 int32_t rawPressure = 10;
4090 int32_t rawToolMajor = 12;
4091 int32_t rawDistance = 2;
4092 int32_t rawTiltX = 30;
4093 int32_t rawTiltY = 110;
4094
4095 float x = toDisplayX(rawX);
4096 float y = toDisplayY(rawY);
4097 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4098 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4099 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4100 float distance = float(rawDistance);
4101
4102 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4103 float tiltScale = M_PI / 180;
4104 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4105 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4106 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4107 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4108
4109 processDown(mapper, rawX, rawY);
4110 processPressure(mapper, rawPressure);
4111 processToolMajor(mapper, rawToolMajor);
4112 processDistance(mapper, rawDistance);
4113 processTilt(mapper, rawTiltX, rawTiltY);
4114 processSync(mapper);
4115
4116 NotifyMotionArgs args;
4117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4119 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4120 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4121}
4122
Jason Gerecke489fda82012-09-07 17:19:40 -07004123TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4124 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4125 addConfigurationProperty("touch.deviceType", "touchScreen");
4126 prepareDisplay(DISPLAY_ORIENTATION_0);
4127 prepareLocationCalibration();
4128 prepareButtons();
4129 prepareAxes(POSITION);
4130 addMapperAndConfigure(mapper);
4131
4132 int32_t rawX = 100;
4133 int32_t rawY = 200;
4134
4135 float x = toDisplayX(toCookedX(rawX, rawY));
4136 float y = toDisplayY(toCookedY(rawX, rawY));
4137
4138 processDown(mapper, rawX, rawY);
4139 processSync(mapper);
4140
4141 NotifyMotionArgs args;
4142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4144 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4145}
4146
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4148 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4149 addConfigurationProperty("touch.deviceType", "touchScreen");
4150 prepareDisplay(DISPLAY_ORIENTATION_0);
4151 prepareButtons();
4152 prepareAxes(POSITION);
4153 addMapperAndConfigure(mapper);
4154
4155 NotifyMotionArgs motionArgs;
4156 NotifyKeyArgs keyArgs;
4157
4158 processDown(mapper, 100, 200);
4159 processSync(mapper);
4160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4161 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4162 ASSERT_EQ(0, motionArgs.buttonState);
4163
4164 // press BTN_LEFT, release BTN_LEFT
4165 processKey(mapper, BTN_LEFT, 1);
4166 processSync(mapper);
4167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4169 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4170
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4173 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4174
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 processKey(mapper, BTN_LEFT, 0);
4176 processSync(mapper);
4177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004180
4181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004183 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
4185 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4186 processKey(mapper, BTN_RIGHT, 1);
4187 processKey(mapper, BTN_MIDDLE, 1);
4188 processSync(mapper);
4189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4191 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4192 motionArgs.buttonState);
4193
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4196 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4197
4198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4200 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4201 motionArgs.buttonState);
4202
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 processKey(mapper, BTN_RIGHT, 0);
4204 processSync(mapper);
4205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004208
4209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212
4213 processKey(mapper, BTN_MIDDLE, 0);
4214 processSync(mapper);
4215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004221 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222
4223 // press BTN_BACK, release BTN_BACK
4224 processKey(mapper, BTN_BACK, 1);
4225 processSync(mapper);
4226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4227 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4228 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004232 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4233
4234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4236 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237
4238 processKey(mapper, BTN_BACK, 0);
4239 processSync(mapper);
4240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243
4244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246 ASSERT_EQ(0, motionArgs.buttonState);
4247
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4249 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4250 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4251
4252 // press BTN_SIDE, release BTN_SIDE
4253 processKey(mapper, BTN_SIDE, 1);
4254 processSync(mapper);
4255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4256 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4257 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004258
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4262
4263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4264 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4265 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
4267 processKey(mapper, BTN_SIDE, 0);
4268 processSync(mapper);
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004272
4273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004275 ASSERT_EQ(0, motionArgs.buttonState);
4276
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4278 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4279 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4280
4281 // press BTN_FORWARD, release BTN_FORWARD
4282 processKey(mapper, BTN_FORWARD, 1);
4283 processSync(mapper);
4284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4285 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4286 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004287
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004290 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4291
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4294 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
4296 processKey(mapper, BTN_FORWARD, 0);
4297 processSync(mapper);
4298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301
4302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004304 ASSERT_EQ(0, motionArgs.buttonState);
4305
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4307 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4308 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4309
4310 // press BTN_EXTRA, release BTN_EXTRA
4311 processKey(mapper, BTN_EXTRA, 1);
4312 processSync(mapper);
4313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4314 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4315 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004316
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004319 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4320
4321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4323 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324
4325 processKey(mapper, BTN_EXTRA, 0);
4326 processSync(mapper);
4327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004330
4331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333 ASSERT_EQ(0, motionArgs.buttonState);
4334
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4336 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4337 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4338
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4340
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 // press BTN_STYLUS, release BTN_STYLUS
4342 processKey(mapper, BTN_STYLUS, 1);
4343 processSync(mapper);
4344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004346 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4347
4348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4349 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4350 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351
4352 processKey(mapper, BTN_STYLUS, 0);
4353 processSync(mapper);
4354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357
4358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004360 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361
4362 // press BTN_STYLUS2, release BTN_STYLUS2
4363 processKey(mapper, BTN_STYLUS2, 1);
4364 processSync(mapper);
4365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004367 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4371 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372
4373 processKey(mapper, BTN_STYLUS2, 0);
4374 processSync(mapper);
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378
4379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004381 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
4383 // release touch
4384 processUp(mapper);
4385 processSync(mapper);
4386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4388 ASSERT_EQ(0, motionArgs.buttonState);
4389}
4390
4391TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4392 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4393 addConfigurationProperty("touch.deviceType", "touchScreen");
4394 prepareDisplay(DISPLAY_ORIENTATION_0);
4395 prepareButtons();
4396 prepareAxes(POSITION);
4397 addMapperAndConfigure(mapper);
4398
4399 NotifyMotionArgs motionArgs;
4400
4401 // default tool type is finger
4402 processDown(mapper, 100, 200);
4403 processSync(mapper);
4404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4407
4408 // eraser
4409 processKey(mapper, BTN_TOOL_RUBBER, 1);
4410 processSync(mapper);
4411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4414
4415 // stylus
4416 processKey(mapper, BTN_TOOL_RUBBER, 0);
4417 processKey(mapper, BTN_TOOL_PEN, 1);
4418 processSync(mapper);
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4422
4423 // brush
4424 processKey(mapper, BTN_TOOL_PEN, 0);
4425 processKey(mapper, BTN_TOOL_BRUSH, 1);
4426 processSync(mapper);
4427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4430
4431 // pencil
4432 processKey(mapper, BTN_TOOL_BRUSH, 0);
4433 processKey(mapper, BTN_TOOL_PENCIL, 1);
4434 processSync(mapper);
4435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4438
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004439 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 processKey(mapper, BTN_TOOL_PENCIL, 0);
4441 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4442 processSync(mapper);
4443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4446
4447 // mouse
4448 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4449 processKey(mapper, BTN_TOOL_MOUSE, 1);
4450 processSync(mapper);
4451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4454
4455 // lens
4456 processKey(mapper, BTN_TOOL_MOUSE, 0);
4457 processKey(mapper, BTN_TOOL_LENS, 1);
4458 processSync(mapper);
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4462
4463 // double-tap
4464 processKey(mapper, BTN_TOOL_LENS, 0);
4465 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
4470
4471 // triple-tap
4472 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4473 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4474 processSync(mapper);
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4478
4479 // quad-tap
4480 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4481 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4482 processSync(mapper);
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4486
4487 // finger
4488 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4489 processKey(mapper, BTN_TOOL_FINGER, 1);
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 // stylus trumps finger
4496 processKey(mapper, BTN_TOOL_PEN, 1);
4497 processSync(mapper);
4498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4501
4502 // eraser trumps stylus
4503 processKey(mapper, BTN_TOOL_RUBBER, 1);
4504 processSync(mapper);
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4508
4509 // mouse trumps eraser
4510 processKey(mapper, BTN_TOOL_MOUSE, 1);
4511 processSync(mapper);
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4515
4516 // back to default tool type
4517 processKey(mapper, BTN_TOOL_MOUSE, 0);
4518 processKey(mapper, BTN_TOOL_RUBBER, 0);
4519 processKey(mapper, BTN_TOOL_PEN, 0);
4520 processKey(mapper, BTN_TOOL_FINGER, 0);
4521 processSync(mapper);
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4525}
4526
4527TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4528 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4529 addConfigurationProperty("touch.deviceType", "touchScreen");
4530 prepareDisplay(DISPLAY_ORIENTATION_0);
4531 prepareButtons();
4532 prepareAxes(POSITION);
4533 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4534 addMapperAndConfigure(mapper);
4535
4536 NotifyMotionArgs motionArgs;
4537
4538 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4539 processKey(mapper, BTN_TOOL_FINGER, 1);
4540 processMove(mapper, 100, 200);
4541 processSync(mapper);
4542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4545 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4546
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4550 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4551
4552 // move a little
4553 processMove(mapper, 150, 250);
4554 processSync(mapper);
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 // down when BTN_TOUCH is pressed, pressure defaults to 1
4561 processKey(mapper, BTN_TOUCH, 1);
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4569 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4571 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4572
4573 // up when BTN_TOUCH is released, hover restored
4574 processKey(mapper, BTN_TOUCH, 0);
4575 processSync(mapper);
4576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4579 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4580
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4584 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4585
4586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4587 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4589 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4590
4591 // exit hover when pointer goes away
4592 processKey(mapper, BTN_TOOL_FINGER, 0);
4593 processSync(mapper);
4594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4597 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4598}
4599
4600TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4601 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4602 addConfigurationProperty("touch.deviceType", "touchScreen");
4603 prepareDisplay(DISPLAY_ORIENTATION_0);
4604 prepareButtons();
4605 prepareAxes(POSITION | PRESSURE);
4606 addMapperAndConfigure(mapper);
4607
4608 NotifyMotionArgs motionArgs;
4609
4610 // initially hovering because pressure is 0
4611 processDown(mapper, 100, 200);
4612 processPressure(mapper, 0);
4613 processSync(mapper);
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4617 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4618
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4620 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4622 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4623
4624 // move a little
4625 processMove(mapper, 150, 250);
4626 processSync(mapper);
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 // down when pressure is non-zero
4633 processPressure(mapper, RAW_PRESSURE_MAX);
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4641 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4643 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4644
4645 // up when pressure becomes 0, hover restored
4646 processPressure(mapper, 0);
4647 processSync(mapper);
4648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4649 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4651 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4652
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4654 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4656 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4657
4658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4661 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4662
4663 // exit hover when pointer goes away
4664 processUp(mapper);
4665 processSync(mapper);
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4669 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4670}
4671
Dan Harmsaca28402018-12-17 13:55:20 -08004672
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673// --- MultiTouchInputMapperTest ---
4674
4675class MultiTouchInputMapperTest : public TouchInputMapperTest {
4676protected:
4677 void prepareAxes(int axes);
4678
4679 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4680 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4681 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4682 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4683 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4684 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4685 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4686 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4687 void processId(MultiTouchInputMapper* mapper, int32_t id);
4688 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4689 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4690 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004691 void processTimestamp(MultiTouchInputMapper* mapper, uint32_t value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 void processMTSync(MultiTouchInputMapper* mapper);
4693 void processSync(MultiTouchInputMapper* mapper);
4694};
4695
4696void MultiTouchInputMapperTest::prepareAxes(int axes) {
4697 if (axes & POSITION) {
4698 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4699 RAW_X_MIN, RAW_X_MAX, 0, 0);
4700 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4701 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4702 }
4703 if (axes & TOUCH) {
4704 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4705 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4706 if (axes & MINOR) {
4707 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4708 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4709 }
4710 }
4711 if (axes & TOOL) {
4712 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4713 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4714 if (axes & MINOR) {
4715 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4716 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4717 }
4718 }
4719 if (axes & ORIENTATION) {
4720 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4721 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4722 }
4723 if (axes & PRESSURE) {
4724 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4725 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4726 }
4727 if (axes & DISTANCE) {
4728 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4729 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4730 }
4731 if (axes & ID) {
4732 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4733 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4734 }
4735 if (axes & SLOT) {
4736 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4737 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4738 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4739 }
4740 if (axes & TOOL_TYPE) {
4741 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4742 0, MT_TOOL_MAX, 0, 0);
4743 }
4744}
4745
4746void MultiTouchInputMapperTest::processPosition(
4747 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004748 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4749 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750}
4751
4752void MultiTouchInputMapperTest::processTouchMajor(
4753 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004754 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755}
4756
4757void MultiTouchInputMapperTest::processTouchMinor(
4758 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004759 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760}
4761
4762void MultiTouchInputMapperTest::processToolMajor(
4763 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004764 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765}
4766
4767void MultiTouchInputMapperTest::processToolMinor(
4768 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004769 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770}
4771
4772void MultiTouchInputMapperTest::processOrientation(
4773 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004774 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775}
4776
4777void MultiTouchInputMapperTest::processPressure(
4778 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004779 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780}
4781
4782void MultiTouchInputMapperTest::processDistance(
4783 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004784 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785}
4786
4787void MultiTouchInputMapperTest::processId(
4788 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004789 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790}
4791
4792void MultiTouchInputMapperTest::processSlot(
4793 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004794 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795}
4796
4797void MultiTouchInputMapperTest::processToolType(
4798 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004799 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800}
4801
4802void MultiTouchInputMapperTest::processKey(
4803 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004804 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805}
4806
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004807void MultiTouchInputMapperTest::processTimestamp(MultiTouchInputMapper* mapper, uint32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004808 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, value);
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004809}
4810
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004812 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813}
4814
4815void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004816 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817}
4818
4819
4820TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4821 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4822 addConfigurationProperty("touch.deviceType", "touchScreen");
4823 prepareDisplay(DISPLAY_ORIENTATION_0);
4824 prepareAxes(POSITION);
4825 prepareVirtualKeys();
4826 addMapperAndConfigure(mapper);
4827
4828 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4829
4830 NotifyMotionArgs motionArgs;
4831
4832 // Two fingers down at once.
4833 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4834 processPosition(mapper, x1, y1);
4835 processMTSync(mapper);
4836 processPosition(mapper, x2, y2);
4837 processMTSync(mapper);
4838 processSync(mapper);
4839
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4841 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4842 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4843 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4844 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4845 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4846 ASSERT_EQ(0, motionArgs.flags);
4847 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4848 ASSERT_EQ(0, motionArgs.buttonState);
4849 ASSERT_EQ(0, motionArgs.edgeFlags);
4850 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4854 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4855 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4856 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4858
4859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4860 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4861 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4862 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4863 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4864 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4865 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 // Move.
4884 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4885 processPosition(mapper, x1, y1);
4886 processMTSync(mapper);
4887 processPosition(mapper, x2, y2);
4888 processMTSync(mapper);
4889 processSync(mapper);
4890
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4892 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4893 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4894 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4895 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4897 ASSERT_EQ(0, motionArgs.flags);
4898 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4899 ASSERT_EQ(0, motionArgs.buttonState);
4900 ASSERT_EQ(0, motionArgs.edgeFlags);
4901 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4902 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4904 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4907 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4909 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4910 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4911 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4913
4914 // First finger up.
4915 x2 += 15; y2 -= 20;
4916 processPosition(mapper, x2, y2);
4917 processMTSync(mapper);
4918 processSync(mapper);
4919
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4922 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4923 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4924 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4925 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4926 motionArgs.action);
4927 ASSERT_EQ(0, motionArgs.flags);
4928 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4929 ASSERT_EQ(0, motionArgs.buttonState);
4930 ASSERT_EQ(0, motionArgs.edgeFlags);
4931 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4932 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4934 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4937 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4939 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4940 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4941 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4942 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4943
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4945 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4946 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4947 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4948 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4950 ASSERT_EQ(0, motionArgs.flags);
4951 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4952 ASSERT_EQ(0, motionArgs.buttonState);
4953 ASSERT_EQ(0, motionArgs.edgeFlags);
4954 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4955 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4958 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4959 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4960 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4961 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4962
4963 // Move.
4964 x2 += 20; y2 -= 25;
4965 processPosition(mapper, x2, y2);
4966 processMTSync(mapper);
4967 processSync(mapper);
4968
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4970 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4971 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4972 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4973 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4975 ASSERT_EQ(0, motionArgs.flags);
4976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4977 ASSERT_EQ(0, motionArgs.buttonState);
4978 ASSERT_EQ(0, motionArgs.edgeFlags);
4979 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4980 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4981 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4983 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4984 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4985 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4986 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4987
4988 // New finger down.
4989 int32_t x3 = 700, y3 = 300;
4990 processPosition(mapper, x2, y2);
4991 processMTSync(mapper);
4992 processPosition(mapper, x3, y3);
4993 processMTSync(mapper);
4994 processSync(mapper);
4995
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4997 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4998 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4999 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5000 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5001 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5002 motionArgs.action);
5003 ASSERT_EQ(0, motionArgs.flags);
5004 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5005 ASSERT_EQ(0, motionArgs.buttonState);
5006 ASSERT_EQ(0, motionArgs.edgeFlags);
5007 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5008 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5010 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5013 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5015 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5016 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5017 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5018 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5019
5020 // Second finger up.
5021 x3 += 30; y3 -= 20;
5022 processPosition(mapper, x3, y3);
5023 processMTSync(mapper);
5024 processSync(mapper);
5025
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5028 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5029 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5030 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5031 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5032 motionArgs.action);
5033 ASSERT_EQ(0, motionArgs.flags);
5034 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5035 ASSERT_EQ(0, motionArgs.buttonState);
5036 ASSERT_EQ(0, motionArgs.edgeFlags);
5037 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5038 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5039 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5040 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5043 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5045 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5046 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5047 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5049
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5051 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5052 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5053 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5054 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5056 ASSERT_EQ(0, motionArgs.flags);
5057 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5058 ASSERT_EQ(0, motionArgs.buttonState);
5059 ASSERT_EQ(0, motionArgs.edgeFlags);
5060 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5061 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5064 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5065 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5066 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5068
5069 // Last finger up.
5070 processMTSync(mapper);
5071 processSync(mapper);
5072
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5074 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5075 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5076 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5077 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5078 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5079 ASSERT_EQ(0, motionArgs.flags);
5080 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5081 ASSERT_EQ(0, motionArgs.buttonState);
5082 ASSERT_EQ(0, motionArgs.edgeFlags);
5083 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5084 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5087 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5088 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5089 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5090 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5091
5092 // Should not have sent any more keys or motions.
5093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5095}
5096
5097TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5098 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5099 addConfigurationProperty("touch.deviceType", "touchScreen");
5100 prepareDisplay(DISPLAY_ORIENTATION_0);
5101 prepareAxes(POSITION | ID);
5102 prepareVirtualKeys();
5103 addMapperAndConfigure(mapper);
5104
5105 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5106
5107 NotifyMotionArgs motionArgs;
5108
5109 // Two fingers down at once.
5110 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
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_DOWN, motionArgs.action);
5121 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5122 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5125 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5126
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5129 motionArgs.action);
5130 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5131 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5133 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5136 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5138 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5139
5140 // Move.
5141 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5142 processPosition(mapper, x1, y1);
5143 processId(mapper, 1);
5144 processMTSync(mapper);
5145 processPosition(mapper, x2, y2);
5146 processId(mapper, 2);
5147 processMTSync(mapper);
5148 processSync(mapper);
5149
5150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5152 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5153 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5155 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5158 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5160 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5161
5162 // First finger up.
5163 x2 += 15; y2 -= 20;
5164 processPosition(mapper, x2, y2);
5165 processId(mapper, 2);
5166 processMTSync(mapper);
5167 processSync(mapper);
5168
5169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5170 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5171 motionArgs.action);
5172 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5173 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5175 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5178 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5180 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5181
5182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5184 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5185 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5188 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5189
5190 // Move.
5191 x2 += 20; y2 -= 25;
5192 processPosition(mapper, x2, y2);
5193 processId(mapper, 2);
5194 processMTSync(mapper);
5195 processSync(mapper);
5196
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5199 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5200 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5203 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5204
5205 // New finger down.
5206 int32_t x3 = 700, y3 = 300;
5207 processPosition(mapper, x2, y2);
5208 processId(mapper, 2);
5209 processMTSync(mapper);
5210 processPosition(mapper, x3, y3);
5211 processId(mapper, 3);
5212 processMTSync(mapper);
5213 processSync(mapper);
5214
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5217 motionArgs.action);
5218 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5219 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5221 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5224 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5226 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5227
5228 // Second finger up.
5229 x3 += 30; y3 -= 20;
5230 processPosition(mapper, x3, y3);
5231 processId(mapper, 3);
5232 processMTSync(mapper);
5233 processSync(mapper);
5234
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5236 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5237 motionArgs.action);
5238 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5241 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5244 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5246 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5247
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5250 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5251 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5254 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5255
5256 // Last finger up.
5257 processMTSync(mapper);
5258 processSync(mapper);
5259
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5261 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5262 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5263 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5266 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5267
5268 // Should not have sent any more keys or motions.
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5271}
5272
5273TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5274 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5275 addConfigurationProperty("touch.deviceType", "touchScreen");
5276 prepareDisplay(DISPLAY_ORIENTATION_0);
5277 prepareAxes(POSITION | ID | SLOT);
5278 prepareVirtualKeys();
5279 addMapperAndConfigure(mapper);
5280
5281 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5282
5283 NotifyMotionArgs motionArgs;
5284
5285 // Two fingers down at once.
5286 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5287 processPosition(mapper, x1, y1);
5288 processId(mapper, 1);
5289 processSlot(mapper, 1);
5290 processPosition(mapper, x2, y2);
5291 processId(mapper, 2);
5292 processSync(mapper);
5293
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5296 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5297 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5300 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5301
5302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5303 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5304 motionArgs.action);
5305 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5306 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5308 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5311 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5313 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5314
5315 // Move.
5316 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5317 processSlot(mapper, 0);
5318 processPosition(mapper, x1, y1);
5319 processSlot(mapper, 1);
5320 processPosition(mapper, x2, y2);
5321 processSync(mapper);
5322
5323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5325 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5326 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5328 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5331 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5333 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5334
5335 // First finger up.
5336 x2 += 15; y2 -= 20;
5337 processSlot(mapper, 0);
5338 processId(mapper, -1);
5339 processSlot(mapper, 1);
5340 processPosition(mapper, x2, y2);
5341 processSync(mapper);
5342
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5344 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5345 motionArgs.action);
5346 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5347 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5349 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5352 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5354 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5355
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5358 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5359 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5362 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5363
5364 // Move.
5365 x2 += 20; y2 -= 25;
5366 processPosition(mapper, x2, y2);
5367 processSync(mapper);
5368
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5372 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5376
5377 // New finger down.
5378 int32_t x3 = 700, y3 = 300;
5379 processPosition(mapper, x2, y2);
5380 processSlot(mapper, 0);
5381 processId(mapper, 3);
5382 processPosition(mapper, x3, y3);
5383 processSync(mapper);
5384
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5386 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5387 motionArgs.action);
5388 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5389 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5391 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5394 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5396 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5397
5398 // Second finger up.
5399 x3 += 30; y3 -= 20;
5400 processSlot(mapper, 1);
5401 processId(mapper, -1);
5402 processSlot(mapper, 0);
5403 processPosition(mapper, x3, y3);
5404 processSync(mapper);
5405
5406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5407 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5408 motionArgs.action);
5409 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5412 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5415 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5417 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5418
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5421 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5422 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5425 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5426
5427 // Last finger up.
5428 processId(mapper, -1);
5429 processSync(mapper);
5430
5431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5432 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5433 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5434 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5437 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5438
5439 // Should not have sent any more keys or motions.
5440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5442}
5443
5444TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5445 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5446 addConfigurationProperty("touch.deviceType", "touchScreen");
5447 prepareDisplay(DISPLAY_ORIENTATION_0);
5448 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5449 addMapperAndConfigure(mapper);
5450
5451 // These calculations are based on the input device calibration documentation.
5452 int32_t rawX = 100;
5453 int32_t rawY = 200;
5454 int32_t rawTouchMajor = 7;
5455 int32_t rawTouchMinor = 6;
5456 int32_t rawToolMajor = 9;
5457 int32_t rawToolMinor = 8;
5458 int32_t rawPressure = 11;
5459 int32_t rawDistance = 0;
5460 int32_t rawOrientation = 3;
5461 int32_t id = 5;
5462
5463 float x = toDisplayX(rawX);
5464 float y = toDisplayY(rawY);
5465 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5466 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5467 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5468 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5469 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5470 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5471 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5472 float distance = float(rawDistance);
5473
5474 processPosition(mapper, rawX, rawY);
5475 processTouchMajor(mapper, rawTouchMajor);
5476 processTouchMinor(mapper, rawTouchMinor);
5477 processToolMajor(mapper, rawToolMajor);
5478 processToolMinor(mapper, rawToolMinor);
5479 processPressure(mapper, rawPressure);
5480 processOrientation(mapper, rawOrientation);
5481 processDistance(mapper, rawDistance);
5482 processId(mapper, id);
5483 processMTSync(mapper);
5484 processSync(mapper);
5485
5486 NotifyMotionArgs args;
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5488 ASSERT_EQ(0, args.pointerProperties[0].id);
5489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5490 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5491 orientation, distance));
5492}
5493
5494TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5495 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5496 addConfigurationProperty("touch.deviceType", "touchScreen");
5497 prepareDisplay(DISPLAY_ORIENTATION_0);
5498 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5499 addConfigurationProperty("touch.size.calibration", "geometric");
5500 addMapperAndConfigure(mapper);
5501
5502 // These calculations are based on the input device calibration documentation.
5503 int32_t rawX = 100;
5504 int32_t rawY = 200;
5505 int32_t rawTouchMajor = 140;
5506 int32_t rawTouchMinor = 120;
5507 int32_t rawToolMajor = 180;
5508 int32_t rawToolMinor = 160;
5509
5510 float x = toDisplayX(rawX);
5511 float y = toDisplayY(rawY);
5512 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5513 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5514 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5515 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5516 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5517
5518 processPosition(mapper, rawX, rawY);
5519 processTouchMajor(mapper, rawTouchMajor);
5520 processTouchMinor(mapper, rawTouchMinor);
5521 processToolMajor(mapper, rawToolMajor);
5522 processToolMinor(mapper, rawToolMinor);
5523 processMTSync(mapper);
5524 processSync(mapper);
5525
5526 NotifyMotionArgs args;
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5529 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5530}
5531
5532TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5533 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5534 addConfigurationProperty("touch.deviceType", "touchScreen");
5535 prepareDisplay(DISPLAY_ORIENTATION_0);
5536 prepareAxes(POSITION | TOUCH | TOOL);
5537 addConfigurationProperty("touch.size.calibration", "diameter");
5538 addConfigurationProperty("touch.size.scale", "10");
5539 addConfigurationProperty("touch.size.bias", "160");
5540 addConfigurationProperty("touch.size.isSummed", "1");
5541 addMapperAndConfigure(mapper);
5542
5543 // These calculations are based on the input device calibration documentation.
5544 // Note: We only provide a single common touch/tool value because the device is assumed
5545 // not to emit separate values for each pointer (isSummed = 1).
5546 int32_t rawX = 100;
5547 int32_t rawY = 200;
5548 int32_t rawX2 = 150;
5549 int32_t rawY2 = 250;
5550 int32_t rawTouchMajor = 5;
5551 int32_t rawToolMajor = 8;
5552
5553 float x = toDisplayX(rawX);
5554 float y = toDisplayY(rawY);
5555 float x2 = toDisplayX(rawX2);
5556 float y2 = toDisplayY(rawY2);
5557 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5558 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5559 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5560
5561 processPosition(mapper, rawX, rawY);
5562 processTouchMajor(mapper, rawTouchMajor);
5563 processToolMajor(mapper, rawToolMajor);
5564 processMTSync(mapper);
5565 processPosition(mapper, rawX2, rawY2);
5566 processTouchMajor(mapper, rawTouchMajor);
5567 processToolMajor(mapper, rawToolMajor);
5568 processMTSync(mapper);
5569 processSync(mapper);
5570
5571 NotifyMotionArgs args;
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5574
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5576 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5577 args.action);
5578 ASSERT_EQ(size_t(2), args.pointerCount);
5579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5580 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5582 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5583}
5584
5585TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5586 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5587 addConfigurationProperty("touch.deviceType", "touchScreen");
5588 prepareDisplay(DISPLAY_ORIENTATION_0);
5589 prepareAxes(POSITION | TOUCH | TOOL);
5590 addConfigurationProperty("touch.size.calibration", "area");
5591 addConfigurationProperty("touch.size.scale", "43");
5592 addConfigurationProperty("touch.size.bias", "3");
5593 addMapperAndConfigure(mapper);
5594
5595 // These calculations are based on the input device calibration documentation.
5596 int32_t rawX = 100;
5597 int32_t rawY = 200;
5598 int32_t rawTouchMajor = 5;
5599 int32_t rawToolMajor = 8;
5600
5601 float x = toDisplayX(rawX);
5602 float y = toDisplayY(rawY);
5603 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5604 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5605 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5606
5607 processPosition(mapper, rawX, rawY);
5608 processTouchMajor(mapper, rawTouchMajor);
5609 processToolMajor(mapper, rawToolMajor);
5610 processMTSync(mapper);
5611 processSync(mapper);
5612
5613 NotifyMotionArgs args;
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5616 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5617}
5618
5619TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5620 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5621 addConfigurationProperty("touch.deviceType", "touchScreen");
5622 prepareDisplay(DISPLAY_ORIENTATION_0);
5623 prepareAxes(POSITION | PRESSURE);
5624 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5625 addConfigurationProperty("touch.pressure.scale", "0.01");
5626 addMapperAndConfigure(mapper);
5627
Michael Wrightaa449c92017-12-13 21:21:43 +00005628 InputDeviceInfo info;
5629 mapper->populateDeviceInfo(&info);
5630 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5631 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5632 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5633
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 // These calculations are based on the input device calibration documentation.
5635 int32_t rawX = 100;
5636 int32_t rawY = 200;
5637 int32_t rawPressure = 60;
5638
5639 float x = toDisplayX(rawX);
5640 float y = toDisplayY(rawY);
5641 float pressure = float(rawPressure) * 0.01f;
5642
5643 processPosition(mapper, rawX, rawY);
5644 processPressure(mapper, rawPressure);
5645 processMTSync(mapper);
5646 processSync(mapper);
5647
5648 NotifyMotionArgs args;
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5651 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5652}
5653
5654TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5655 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5656 addConfigurationProperty("touch.deviceType", "touchScreen");
5657 prepareDisplay(DISPLAY_ORIENTATION_0);
5658 prepareAxes(POSITION | ID | SLOT);
5659 addMapperAndConfigure(mapper);
5660
5661 NotifyMotionArgs motionArgs;
5662 NotifyKeyArgs keyArgs;
5663
5664 processId(mapper, 1);
5665 processPosition(mapper, 100, 200);
5666 processSync(mapper);
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5669 ASSERT_EQ(0, motionArgs.buttonState);
5670
5671 // press BTN_LEFT, release BTN_LEFT
5672 processKey(mapper, BTN_LEFT, 1);
5673 processSync(mapper);
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5676 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5677
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5680 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5681
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 processKey(mapper, BTN_LEFT, 0);
5683 processSync(mapper);
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005687
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005690 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691
5692 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5693 processKey(mapper, BTN_RIGHT, 1);
5694 processKey(mapper, BTN_MIDDLE, 1);
5695 processSync(mapper);
5696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5698 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5699 motionArgs.buttonState);
5700
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5703 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5704
5705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5707 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5708 motionArgs.buttonState);
5709
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710 processKey(mapper, BTN_RIGHT, 0);
5711 processSync(mapper);
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005713 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005714 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005715
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005718 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005719
5720 processKey(mapper, BTN_MIDDLE, 0);
5721 processSync(mapper);
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005725
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005728 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729
5730 // press BTN_BACK, release BTN_BACK
5731 processKey(mapper, BTN_BACK, 1);
5732 processSync(mapper);
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5734 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5735 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005736
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005739 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5740
5741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5742 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5743 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744
5745 processKey(mapper, BTN_BACK, 0);
5746 processSync(mapper);
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005748 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005750
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005753 ASSERT_EQ(0, motionArgs.buttonState);
5754
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5756 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5757 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5758
5759 // press BTN_SIDE, release BTN_SIDE
5760 processKey(mapper, BTN_SIDE, 1);
5761 processSync(mapper);
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5763 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5764 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005765
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005768 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5769
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5772 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773
5774 processKey(mapper, BTN_SIDE, 0);
5775 processSync(mapper);
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005777 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005779
5780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005782 ASSERT_EQ(0, motionArgs.buttonState);
5783
Michael Wrightd02c5b62014-02-10 15:10:22 -08005784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5785 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5786 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5787
5788 // press BTN_FORWARD, release BTN_FORWARD
5789 processKey(mapper, BTN_FORWARD, 1);
5790 processSync(mapper);
5791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5792 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5793 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005794
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005797 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5798
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5801 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802
5803 processKey(mapper, BTN_FORWARD, 0);
5804 processSync(mapper);
5805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005808
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005811 ASSERT_EQ(0, motionArgs.buttonState);
5812
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5814 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5815 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5816
5817 // press BTN_EXTRA, release BTN_EXTRA
5818 processKey(mapper, BTN_EXTRA, 1);
5819 processSync(mapper);
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5821 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5822 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005823
Michael Wrightd02c5b62014-02-10 15:10:22 -08005824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005826 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5827
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5830 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831
5832 processKey(mapper, BTN_EXTRA, 0);
5833 processSync(mapper);
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005836 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005837
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005840 ASSERT_EQ(0, motionArgs.buttonState);
5841
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5843 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5844 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5845
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5847
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848 // press BTN_STYLUS, release BTN_STYLUS
5849 processKey(mapper, BTN_STYLUS, 1);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005853 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5854
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5856 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5857 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858
5859 processKey(mapper, BTN_STYLUS, 0);
5860 processSync(mapper);
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005863 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005867 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868
5869 // press BTN_STYLUS2, release BTN_STYLUS2
5870 processKey(mapper, BTN_STYLUS2, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005874 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5875
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5877 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5878 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 processKey(mapper, BTN_STYLUS2, 0);
5881 processSync(mapper);
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005885
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005888 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889
5890 // release touch
5891 processId(mapper, -1);
5892 processSync(mapper);
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5894 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5895 ASSERT_EQ(0, motionArgs.buttonState);
5896}
5897
5898TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
5899 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5900 addConfigurationProperty("touch.deviceType", "touchScreen");
5901 prepareDisplay(DISPLAY_ORIENTATION_0);
5902 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
5903 addMapperAndConfigure(mapper);
5904
5905 NotifyMotionArgs motionArgs;
5906
5907 // default tool type is finger
5908 processId(mapper, 1);
5909 processPosition(mapper, 100, 200);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5912 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5914
5915 // eraser
5916 processKey(mapper, BTN_TOOL_RUBBER, 1);
5917 processSync(mapper);
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5921
5922 // stylus
5923 processKey(mapper, BTN_TOOL_RUBBER, 0);
5924 processKey(mapper, BTN_TOOL_PEN, 1);
5925 processSync(mapper);
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5929
5930 // brush
5931 processKey(mapper, BTN_TOOL_PEN, 0);
5932 processKey(mapper, BTN_TOOL_BRUSH, 1);
5933 processSync(mapper);
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5937
5938 // pencil
5939 processKey(mapper, BTN_TOOL_BRUSH, 0);
5940 processKey(mapper, BTN_TOOL_PENCIL, 1);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5945
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005946 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947 processKey(mapper, BTN_TOOL_PENCIL, 0);
5948 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5953
5954 // mouse
5955 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5956 processKey(mapper, BTN_TOOL_MOUSE, 1);
5957 processSync(mapper);
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5961
5962 // lens
5963 processKey(mapper, BTN_TOOL_MOUSE, 0);
5964 processKey(mapper, BTN_TOOL_LENS, 1);
5965 processSync(mapper);
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5969
5970 // double-tap
5971 processKey(mapper, BTN_TOOL_LENS, 0);
5972 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
5977
5978 // triple-tap
5979 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5980 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5981 processSync(mapper);
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5985
5986 // quad-tap
5987 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5988 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5992 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5993
5994 // finger
5995 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5996 processKey(mapper, BTN_TOOL_FINGER, 1);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6001
6002 // stylus trumps finger
6003 processKey(mapper, BTN_TOOL_PEN, 1);
6004 processSync(mapper);
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6008
6009 // eraser trumps stylus
6010 processKey(mapper, BTN_TOOL_RUBBER, 1);
6011 processSync(mapper);
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6014 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6015
6016 // mouse trumps eraser
6017 processKey(mapper, BTN_TOOL_MOUSE, 1);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6022
6023 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6024 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6025 processSync(mapper);
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6029
6030 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6031 processToolType(mapper, MT_TOOL_PEN);
6032 processSync(mapper);
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6036
6037 // back to default tool type
6038 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6039 processKey(mapper, BTN_TOOL_MOUSE, 0);
6040 processKey(mapper, BTN_TOOL_RUBBER, 0);
6041 processKey(mapper, BTN_TOOL_PEN, 0);
6042 processKey(mapper, BTN_TOOL_FINGER, 0);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6047}
6048
6049TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6050 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6051 addConfigurationProperty("touch.deviceType", "touchScreen");
6052 prepareDisplay(DISPLAY_ORIENTATION_0);
6053 prepareAxes(POSITION | ID | SLOT);
6054 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6055 addMapperAndConfigure(mapper);
6056
6057 NotifyMotionArgs motionArgs;
6058
6059 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6060 processId(mapper, 1);
6061 processPosition(mapper, 100, 200);
6062 processSync(mapper);
6063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6064 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6066 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6067
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6071 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6072
6073 // move a little
6074 processPosition(mapper, 150, 250);
6075 processSync(mapper);
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 // down when BTN_TOUCH is pressed, pressure defaults to 1
6082 processKey(mapper, BTN_TOUCH, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6090 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6092 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6093
6094 // up when BTN_TOUCH is released, hover restored
6095 processKey(mapper, BTN_TOUCH, 0);
6096 processSync(mapper);
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6100 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6101
6102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6103 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6105 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6110 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6111
6112 // exit hover when pointer goes away
6113 processId(mapper, -1);
6114 processSync(mapper);
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6116 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6118 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6119}
6120
6121TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6122 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6123 addConfigurationProperty("touch.deviceType", "touchScreen");
6124 prepareDisplay(DISPLAY_ORIENTATION_0);
6125 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6126 addMapperAndConfigure(mapper);
6127
6128 NotifyMotionArgs motionArgs;
6129
6130 // initially hovering because pressure is 0
6131 processId(mapper, 1);
6132 processPosition(mapper, 100, 200);
6133 processPressure(mapper, 0);
6134 processSync(mapper);
6135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6136 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6138 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6139
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6141 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6143 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6144
6145 // move a little
6146 processPosition(mapper, 150, 250);
6147 processSync(mapper);
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 // down when pressure becomes non-zero
6154 processPressure(mapper, RAW_PRESSURE_MAX);
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6162 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6164 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6165
6166 // up when pressure becomes 0, hover restored
6167 processPressure(mapper, 0);
6168 processSync(mapper);
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6172 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6173
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6175 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6177 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6178
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6180 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6182 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6183
6184 // exit hover when pointer goes away
6185 processId(mapper, -1);
6186 processSync(mapper);
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6188 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6190 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6191}
6192
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08006193TEST_F(MultiTouchInputMapperTest, Process_HandlesTimestamp) {
6194 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6195
6196 addConfigurationProperty("touch.deviceType", "touchScreen");
6197 prepareDisplay(DISPLAY_ORIENTATION_0);
6198 prepareAxes(POSITION);
6199 addMapperAndConfigure(mapper);
6200 NotifyMotionArgs args;
6201
6202 // By default, deviceTimestamp should be zero
6203 processPosition(mapper, 100, 100);
6204 processMTSync(mapper);
6205 processSync(mapper);
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6207 ASSERT_EQ(0U, args.deviceTimestamp);
6208
6209 // Now the timestamp of 1000 is reported by evdev and should appear in MotionArgs
6210 processPosition(mapper, 0, 0);
6211 processTimestamp(mapper, 1000);
6212 processMTSync(mapper);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6215 ASSERT_EQ(1000U, args.deviceTimestamp);
6216}
6217
Siarhei Vishniakoueaf7acd2018-01-09 12:35:51 -08006218TEST_F(MultiTouchInputMapperTest, WhenMapperIsReset_TimestampIsCleared) {
6219 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6220
6221 addConfigurationProperty("touch.deviceType", "touchScreen");
6222 prepareDisplay(DISPLAY_ORIENTATION_0);
6223 prepareAxes(POSITION);
6224 addMapperAndConfigure(mapper);
6225 NotifyMotionArgs args;
6226
6227 // Send a touch event with a timestamp
6228 processPosition(mapper, 100, 100);
6229 processTimestamp(mapper, 1);
6230 processMTSync(mapper);
6231 processSync(mapper);
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6233 ASSERT_EQ(1U, args.deviceTimestamp);
6234
6235 // Since the data accumulates, and new timestamp has not arrived, deviceTimestamp won't change
6236 processPosition(mapper, 100, 200);
6237 processMTSync(mapper);
6238 processSync(mapper);
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6240 ASSERT_EQ(1U, args.deviceTimestamp);
6241
6242 mapper->reset(/* when */ 0);
6243 // After the mapper is reset, deviceTimestamp should become zero again
6244 processPosition(mapper, 100, 300);
6245 processMTSync(mapper);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6248 ASSERT_EQ(0U, args.deviceTimestamp);
6249}
6250
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006251/**
6252 * Set the input device port <--> display port associations, and check that the
6253 * events are routed to the display that matches the display port.
6254 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6255 */
6256TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6257 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6258 const std::string usb2 = "USB2";
6259 const uint8_t hdmi1 = 0;
6260 const uint8_t hdmi2 = 1;
6261 const std::string secondaryUniqueId = "uniqueId2";
6262 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6263
6264 addConfigurationProperty("touch.deviceType", "touchScreen");
6265 prepareAxes(POSITION);
6266 addMapperAndConfigure(mapper);
6267
6268 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6269 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6270
6271 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6272 // for this input device is specified, and the matching viewport is not present,
6273 // the input device should be disabled (at the mapper level).
6274
6275 // Add viewport for display 2 on hdmi2
6276 prepareSecondaryDisplay(type, hdmi2);
6277 // Send a touch event
6278 processPosition(mapper, 100, 100);
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6281
6282 // Add viewport for display 1 on hdmi1
6283 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6284 // Send a touch event again
6285 processPosition(mapper, 100, 100);
6286 processSync(mapper);
6287
6288 NotifyMotionArgs args;
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6290 ASSERT_EQ(DISPLAY_ID, args.displayId);
6291}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292
Arthur Hung41a712e2018-11-22 19:41:03 +08006293/**
6294 * Expect fallback to internal viewport if device is external and external viewport is not present.
6295 */
6296TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6297 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6298 prepareAxes(POSITION);
6299 addConfigurationProperty("touch.deviceType", "touchScreen");
6300 prepareDisplay(DISPLAY_ORIENTATION_0);
6301 mDevice->setExternal(true);
6302 addMapperAndConfigure(mapper);
6303
6304 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6305
6306 NotifyMotionArgs motionArgs;
6307
6308 // Expect the event to be sent to the internal viewport,
6309 // because an external viewport is not present.
6310 processPosition(mapper, 100, 100);
6311 processSync(mapper);
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6314
6315 // Expect the event to be sent to the external viewport if it is present.
6316 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6317 processPosition(mapper, 100, 100);
6318 processSync(mapper);
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6321}
6322
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006323TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6324 // Setup PointerController for second display.
6325 sp<FakePointerController> fakePointerController = new FakePointerController();
6326 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6327 fakePointerController->setPosition(100, 200);
6328 fakePointerController->setButtonState(0);
6329 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6330 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6331
6332 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6333 prepareDisplay(DISPLAY_ORIENTATION_0);
6334 prepareAxes(POSITION);
6335 addMapperAndConfigure(mapper);
6336
6337 // Check source is mouse that would obtain the PointerController.
6338 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6339
6340 NotifyMotionArgs motionArgs;
6341 processPosition(mapper, 100, 100);
6342 processSync(mapper);
6343
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6346 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6347}
6348
Arthur Hung7c645402019-01-25 17:45:42 +08006349TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6350 // Setup the first touch screen device.
6351 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6352 prepareAxes(POSITION | ID | SLOT);
6353 addConfigurationProperty("touch.deviceType", "touchScreen");
6354 addMapperAndConfigure(mapper);
6355
6356 // Create the second touch screen device, and enable multi fingers.
6357 const std::string USB2 = "USB2";
6358 const int32_t SECOND_DEVICE_ID = 2;
6359 InputDeviceIdentifier identifier;
6360 identifier.name = DEVICE_NAME;
6361 identifier.location = USB2;
6362 InputDevice* device2 = new InputDevice(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6363 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
6364 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6365 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6366 0 /*flat*/, 0 /*fuzz*/);
6367 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6368 0 /*flat*/, 0 /*fuzz*/);
6369 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6370 0 /*flat*/, 0 /*fuzz*/);
6371 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6372 0 /*flat*/, 0 /*fuzz*/);
6373 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6374 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6375 String8("touchScreen"));
6376
6377 // Setup the second touch screen device.
6378 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2);
6379 device2->addMapper(mapper2);
6380 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6381 device2->reset(ARBITRARY_TIME);
6382
6383 // Setup PointerController.
6384 sp<FakePointerController> fakePointerController = new FakePointerController();
6385 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6386 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6387
6388 // Setup policy for associated displays and show touches.
6389 const uint8_t hdmi1 = 0;
6390 const uint8_t hdmi2 = 1;
6391 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6392 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6393 mFakePolicy->setShowTouches(true);
6394
6395 // Create displays.
6396 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6397 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6398
6399 // Default device will reconfigure above, need additional reconfiguration for another device.
6400 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6401 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6402
6403 // Two fingers down at default display.
6404 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6405 processPosition(mapper, x1, y1);
6406 processId(mapper, 1);
6407 processSlot(mapper, 1);
6408 processPosition(mapper, x2, y2);
6409 processId(mapper, 2);
6410 processSync(mapper);
6411
6412 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6413 fakePointerController->getSpots().find(DISPLAY_ID);
6414 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6415 ASSERT_EQ(size_t(2), iter->second.size());
6416
6417 // Two fingers down at second display.
6418 processPosition(mapper2, x1, y1);
6419 processId(mapper2, 1);
6420 processSlot(mapper2, 1);
6421 processPosition(mapper2, x2, y2);
6422 processId(mapper2, 2);
6423 processSync(mapper2);
6424
6425 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6426 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6427 ASSERT_EQ(size_t(2), iter->second.size());
6428}
6429
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006430TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6431 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6432 prepareAxes(POSITION);
6433 addConfigurationProperty("touch.deviceType", "touchScreen");
6434 prepareDisplay(DISPLAY_ORIENTATION_0);
6435 addMapperAndConfigure(mapper);
6436
6437 NotifyMotionArgs motionArgs;
6438 // Unrotated video frame
6439 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6440 std::vector<TouchVideoFrame> frames{frame};
6441 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6442 processPosition(mapper, 100, 200);
6443 processSync(mapper);
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(frames, motionArgs.videoFrames);
6446
6447 // Subsequent touch events should not have any videoframes
6448 // This is implemented separately in FakeEventHub,
6449 // but that should match the behaviour of TouchVideoDevice.
6450 processPosition(mapper, 200, 200);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6454}
6455
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006456TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6457 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6458 prepareAxes(POSITION);
6459 addConfigurationProperty("touch.deviceType", "touchScreen");
6460 addMapperAndConfigure(mapper);
6461 // Unrotated video frame
6462 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6463 NotifyMotionArgs motionArgs;
6464
6465 // Test all 4 orientations
6466 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6467 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6468 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6469 clearViewports();
6470 prepareDisplay(orientation);
6471 std::vector<TouchVideoFrame> frames{frame};
6472 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6473 processPosition(mapper, 100, 200);
6474 processSync(mapper);
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 frames[0].rotate(orientation);
6477 ASSERT_EQ(frames, motionArgs.videoFrames);
6478 }
6479}
6480
6481TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6482 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6483 prepareAxes(POSITION);
6484 addConfigurationProperty("touch.deviceType", "touchScreen");
6485 addMapperAndConfigure(mapper);
6486 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6487 // so mix these.
6488 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6489 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6490 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6491 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6492 NotifyMotionArgs motionArgs;
6493
6494 prepareDisplay(DISPLAY_ORIENTATION_90);
6495 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6496 processPosition(mapper, 100, 200);
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 std::for_each(frames.begin(), frames.end(),
6500 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6501 ASSERT_EQ(frames, motionArgs.videoFrames);
6502}
6503
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504} // namespace android