blob: 0b86555fb33f2e9641ceca91776fb11f9e1911f4 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../InputReader.h"
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080018#include "TestInputListener.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Michael Wrightd02c5b62014-02-10 15:10:22 -080020#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080021#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <math.h>
23
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080024
Michael Wrightd02c5b62014-02-10 15:10:22 -080025namespace android {
26
27// An arbitrary time value.
28static const nsecs_t ARBITRARY_TIME = 1234;
29
30// Arbitrary display properties.
31static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070032static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080033static const int32_t DISPLAY_WIDTH = 480;
34static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070035static const int32_t VIRTUAL_DISPLAY_ID = 1;
36static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
37static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070038static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070039static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
41// Error tolerance for floating point assertions.
42static const float EPSILON = 0.001f;
43
44template<typename T>
45static inline T min(T a, T b) {
46 return a < b ? a : b;
47}
48
49static inline float avg(float x, float y) {
50 return (x + y) / 2;
51}
52
53
54// --- FakePointerController ---
55
56class FakePointerController : public PointerControllerInterface {
57 bool mHaveBounds;
58 float mMinX, mMinY, mMaxX, mMaxY;
59 float mX, mY;
60 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080061 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
63protected:
64 virtual ~FakePointerController() { }
65
66public:
67 FakePointerController() :
68 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080069 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080070 }
71
72 void setBounds(float minX, float minY, float maxX, float maxY) {
73 mHaveBounds = true;
74 mMinX = minX;
75 mMinY = minY;
76 mMaxX = maxX;
77 mMaxY = maxY;
78 }
79
Arthur Hungc7ad2d02018-12-18 17:41:29 +080080 void setDisplayId(int32_t displayId) {
81 mDisplayId = displayId;
82 }
83
Michael Wrightd02c5b62014-02-10 15:10:22 -080084 virtual void setPosition(float x, float y) {
85 mX = x;
86 mY = y;
87 }
88
89 virtual void setButtonState(int32_t buttonState) {
90 mButtonState = buttonState;
91 }
92
93 virtual int32_t getButtonState() const {
94 return mButtonState;
95 }
96
97 virtual void getPosition(float* outX, float* outY) const {
98 *outX = mX;
99 *outY = mY;
100 }
101
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800102 virtual int32_t getDisplayId() const {
103 return mDisplayId;
104 }
105
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106private:
107 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
108 *outMinX = mMinX;
109 *outMinY = mMinY;
110 *outMaxX = mMaxX;
111 *outMaxY = mMaxY;
112 return mHaveBounds;
113 }
114
115 virtual void move(float deltaX, float deltaY) {
116 mX += deltaX;
117 if (mX < mMinX) mX = mMinX;
118 if (mX > mMaxX) mX = mMaxX;
119 mY += deltaY;
120 if (mY < mMinY) mY = mMinY;
121 if (mY > mMaxY) mY = mMaxY;
122 }
123
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100124 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125 }
126
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100127 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 }
129
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100130 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 }
132
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100133 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134 }
135
136 virtual void clearSpots() {
137 }
138};
139
140
141// --- FakeInputReaderPolicy ---
142
143class FakeInputReaderPolicy : public InputReaderPolicyInterface {
144 InputReaderConfiguration mConfig;
145 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
146 Vector<InputDeviceInfo> mInputDevices;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100147 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700148 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149
150protected:
151 virtual ~FakeInputReaderPolicy() { }
152
153public:
154 FakeInputReaderPolicy() {
155 }
156
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700157 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100158 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100159 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700160 }
161
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700162 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
163 return mConfig.getDisplayViewportByUniqueId(uniqueId);
164 }
165 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
166 return mConfig.getDisplayViewportByType(type);
167 }
168
169 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
170 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700171 }
172
173 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700174 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
175 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700176 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700177 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700178 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100179 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 }
181
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100182 void addExcludedDeviceName(const std::string& deviceName) {
183 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 }
185
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700186 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
187 mConfig.portAssociations.insert({inputPort, displayPort});
188 }
189
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700190 void addDisabledDevice(int32_t deviceId) {
191 ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
192 bool currentlyEnabled = index < 0;
193 if (currentlyEnabled) {
194 mConfig.disabledDevices.add(deviceId);
195 }
196 }
197
198 void removeDisabledDevice(int32_t deviceId) {
199 ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
200 bool currentlyEnabled = index < 0;
201 if (!currentlyEnabled) {
202 mConfig.disabledDevices.remove(deviceId);
203 }
204 }
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
207 mPointerControllers.add(deviceId, controller);
208 }
209
210 const InputReaderConfiguration* getReaderConfiguration() const {
211 return &mConfig;
212 }
213
214 const Vector<InputDeviceInfo>& getInputDevices() const {
215 return mInputDevices;
216 }
217
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100218 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700219 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700220 return transform;
221 }
222
223 void setTouchAffineTransformation(const TouchAffineTransformation t) {
224 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800225 }
226
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800227 void setPointerCapture(bool enabled) {
228 mConfig.pointerCapture = enabled;
229 }
230
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700232 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700233 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
234 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700235 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
236 || orientation == DISPLAY_ORIENTATION_270);
237 DisplayViewport v;
238 v.displayId = displayId;
239 v.orientation = orientation;
240 v.logicalLeft = 0;
241 v.logicalTop = 0;
242 v.logicalRight = isRotated ? height : width;
243 v.logicalBottom = isRotated ? width : height;
244 v.physicalLeft = 0;
245 v.physicalTop = 0;
246 v.physicalRight = isRotated ? height : width;
247 v.physicalBottom = isRotated ? width : height;
248 v.deviceWidth = isRotated ? height : width;
249 v.deviceHeight = isRotated ? width : height;
250 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700251 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100252 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700253 return v;
254 }
255
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
257 *outConfig = mConfig;
258 }
259
260 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
261 return mPointerControllers.valueFor(deviceId);
262 }
263
264 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
265 mInputDevices = inputDevices;
266 }
267
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100268 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700269 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 }
271
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100272 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
273 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274 }
275};
276
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277// --- FakeEventHub ---
278
279class FakeEventHub : public EventHubInterface {
280 struct KeyInfo {
281 int32_t keyCode;
282 uint32_t flags;
283 };
284
285 struct Device {
286 InputDeviceIdentifier identifier;
287 uint32_t classes;
288 PropertyMap configuration;
289 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
290 KeyedVector<int, bool> relativeAxes;
291 KeyedVector<int32_t, int32_t> keyCodeStates;
292 KeyedVector<int32_t, int32_t> scanCodeStates;
293 KeyedVector<int32_t, int32_t> switchStates;
294 KeyedVector<int32_t, int32_t> absoluteAxisValue;
295 KeyedVector<int32_t, KeyInfo> keysByScanCode;
296 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
297 KeyedVector<int32_t, bool> leds;
298 Vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700299 bool enabled;
300
301 status_t enable() {
302 enabled = true;
303 return OK;
304 }
305
306 status_t disable() {
307 enabled = false;
308 return OK;
309 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700311 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700312 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 }
314 };
315
316 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100317 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 List<RawEvent> mEvents;
319
320protected:
321 virtual ~FakeEventHub() {
322 for (size_t i = 0; i < mDevices.size(); i++) {
323 delete mDevices.valueAt(i);
324 }
325 }
326
327public:
328 FakeEventHub() { }
329
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100330 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 Device* device = new Device(classes);
332 device->identifier.name = name;
333 mDevices.add(deviceId, device);
334
335 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
336 }
337
338 void removeDevice(int32_t deviceId) {
339 delete mDevices.valueFor(deviceId);
340 mDevices.removeItem(deviceId);
341
342 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
343 }
344
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700345 bool isDeviceEnabled(int32_t deviceId) {
346 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700347 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700348 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
349 return false;
350 }
351 return device->enabled;
352 }
353
354 status_t enableDevice(int32_t deviceId) {
355 status_t result;
356 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700357 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700358 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
359 return BAD_VALUE;
360 }
361 if (device->enabled) {
362 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
363 return OK;
364 }
365 result = device->enable();
366 return result;
367 }
368
369 status_t disableDevice(int32_t deviceId) {
370 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700371 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700372 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
373 return BAD_VALUE;
374 }
375 if (!device->enabled) {
376 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
377 return OK;
378 }
379 return device->disable();
380 }
381
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 void finishDeviceScan() {
383 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
384 }
385
386 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
387 Device* device = getDevice(deviceId);
388 device->configuration.addProperty(key, value);
389 }
390
391 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
392 Device* device = getDevice(deviceId);
393 device->configuration.addAll(configuration);
394 }
395
396 void addAbsoluteAxis(int32_t deviceId, int axis,
397 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
398 Device* device = getDevice(deviceId);
399
400 RawAbsoluteAxisInfo info;
401 info.valid = true;
402 info.minValue = minValue;
403 info.maxValue = maxValue;
404 info.flat = flat;
405 info.fuzz = fuzz;
406 info.resolution = resolution;
407 device->absoluteAxes.add(axis, info);
408 }
409
410 void addRelativeAxis(int32_t deviceId, int32_t axis) {
411 Device* device = getDevice(deviceId);
412 device->relativeAxes.add(axis, true);
413 }
414
415 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
416 Device* device = getDevice(deviceId);
417 device->keyCodeStates.replaceValueFor(keyCode, state);
418 }
419
420 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
421 Device* device = getDevice(deviceId);
422 device->scanCodeStates.replaceValueFor(scanCode, state);
423 }
424
425 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
426 Device* device = getDevice(deviceId);
427 device->switchStates.replaceValueFor(switchCode, state);
428 }
429
430 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
431 Device* device = getDevice(deviceId);
432 device->absoluteAxisValue.replaceValueFor(axis, value);
433 }
434
435 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
436 int32_t keyCode, uint32_t flags) {
437 Device* device = getDevice(deviceId);
438 KeyInfo info;
439 info.keyCode = keyCode;
440 info.flags = flags;
441 if (scanCode) {
442 device->keysByScanCode.add(scanCode, info);
443 }
444 if (usageCode) {
445 device->keysByUsageCode.add(usageCode, info);
446 }
447 }
448
449 void addLed(int32_t deviceId, int32_t led, bool initialState) {
450 Device* device = getDevice(deviceId);
451 device->leds.add(led, initialState);
452 }
453
454 bool getLedState(int32_t deviceId, int32_t led) {
455 Device* device = getDevice(deviceId);
456 return device->leds.valueFor(led);
457 }
458
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100459 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 return mExcludedDevices;
461 }
462
463 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
464 Device* device = getDevice(deviceId);
465 device->virtualKeys.push(definition);
466 }
467
468 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
469 int32_t code, int32_t value) {
470 RawEvent event;
471 event.when = when;
472 event.deviceId = deviceId;
473 event.type = type;
474 event.code = code;
475 event.value = value;
476 mEvents.push_back(event);
477
478 if (type == EV_ABS) {
479 setAbsoluteAxisValue(deviceId, code, value);
480 }
481 }
482
483 void assertQueueIsEmpty() {
484 ASSERT_EQ(size_t(0), mEvents.size())
485 << "Expected the event queue to be empty (fully consumed).";
486 }
487
488private:
489 Device* getDevice(int32_t deviceId) const {
490 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100491 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492 }
493
494 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
495 Device* device = getDevice(deviceId);
496 return device ? device->classes : 0;
497 }
498
499 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
500 Device* device = getDevice(deviceId);
501 return device ? device->identifier : InputDeviceIdentifier();
502 }
503
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100504 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505 return 0;
506 }
507
508 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
509 Device* device = getDevice(deviceId);
510 if (device) {
511 *outConfiguration = device->configuration;
512 }
513 }
514
515 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
516 RawAbsoluteAxisInfo* outAxisInfo) const {
517 Device* device = getDevice(deviceId);
518 if (device) {
519 ssize_t index = device->absoluteAxes.indexOfKey(axis);
520 if (index >= 0) {
521 *outAxisInfo = device->absoluteAxes.valueAt(index);
522 return OK;
523 }
524 }
525 outAxisInfo->clear();
526 return -1;
527 }
528
529 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
530 Device* device = getDevice(deviceId);
531 if (device) {
532 return device->relativeAxes.indexOfKey(axis) >= 0;
533 }
534 return false;
535 }
536
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100537 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 return false;
539 }
540
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700541 virtual status_t mapKey(int32_t deviceId,
542 int32_t scanCode, int32_t usageCode, int32_t metaState,
543 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544 Device* device = getDevice(deviceId);
545 if (device) {
546 const KeyInfo* key = getKey(device, scanCode, usageCode);
547 if (key) {
548 if (outKeycode) {
549 *outKeycode = key->keyCode;
550 }
551 if (outFlags) {
552 *outFlags = key->flags;
553 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700554 if (outMetaState) {
555 *outMetaState = metaState;
556 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 return OK;
558 }
559 }
560 return NAME_NOT_FOUND;
561 }
562
563 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
564 if (usageCode) {
565 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
566 if (index >= 0) {
567 return &device->keysByUsageCode.valueAt(index);
568 }
569 }
570 if (scanCode) {
571 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
572 if (index >= 0) {
573 return &device->keysByScanCode.valueAt(index);
574 }
575 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700576 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100579 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 return NAME_NOT_FOUND;
581 }
582
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100583 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 mExcludedDevices = devices;
585 }
586
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100587 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 if (mEvents.empty()) {
589 return 0;
590 }
591
592 *buffer = *mEvents.begin();
593 mEvents.erase(mEvents.begin());
594 return 1;
595 }
596
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800597 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
598 return {};
599 }
600
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
602 Device* device = getDevice(deviceId);
603 if (device) {
604 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
605 if (index >= 0) {
606 return device->scanCodeStates.valueAt(index);
607 }
608 }
609 return AKEY_STATE_UNKNOWN;
610 }
611
612 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
613 Device* device = getDevice(deviceId);
614 if (device) {
615 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
616 if (index >= 0) {
617 return device->keyCodeStates.valueAt(index);
618 }
619 }
620 return AKEY_STATE_UNKNOWN;
621 }
622
623 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
624 Device* device = getDevice(deviceId);
625 if (device) {
626 ssize_t index = device->switchStates.indexOfKey(sw);
627 if (index >= 0) {
628 return device->switchStates.valueAt(index);
629 }
630 }
631 return AKEY_STATE_UNKNOWN;
632 }
633
634 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
635 int32_t* outValue) const {
636 Device* device = getDevice(deviceId);
637 if (device) {
638 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
639 if (index >= 0) {
640 *outValue = device->absoluteAxisValue.valueAt(index);
641 return OK;
642 }
643 }
644 *outValue = 0;
645 return -1;
646 }
647
648 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
649 uint8_t* outFlags) const {
650 bool result = false;
651 Device* device = getDevice(deviceId);
652 if (device) {
653 for (size_t i = 0; i < numCodes; i++) {
654 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
655 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
656 outFlags[i] = 1;
657 result = true;
658 }
659 }
660 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
661 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
662 outFlags[i] = 1;
663 result = true;
664 }
665 }
666 }
667 }
668 return result;
669 }
670
671 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
672 Device* device = getDevice(deviceId);
673 if (device) {
674 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
675 return index >= 0;
676 }
677 return false;
678 }
679
680 virtual bool hasLed(int32_t deviceId, int32_t led) const {
681 Device* device = getDevice(deviceId);
682 return device && device->leds.indexOfKey(led) >= 0;
683 }
684
685 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
686 Device* device = getDevice(deviceId);
687 if (device) {
688 ssize_t index = device->leds.indexOfKey(led);
689 if (index >= 0) {
690 device->leds.replaceValueAt(led, on);
691 } else {
692 ADD_FAILURE()
693 << "Attempted to set the state of an LED that the EventHub declared "
694 "was not present. led=" << led;
695 }
696 }
697 }
698
699 virtual void getVirtualKeyDefinitions(int32_t deviceId,
700 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
701 outVirtualKeys.clear();
702
703 Device* device = getDevice(deviceId);
704 if (device) {
705 outVirtualKeys.appendVector(device->virtualKeys);
706 }
707 }
708
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100709 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700710 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 }
712
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100713 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 return false;
715 }
716
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100717 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 }
719
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100720 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 }
722
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100723 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 return false;
725 }
726
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800727 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 }
729
730 virtual void monitor() {
731 }
732
733 virtual void requestReopenDevices() {
734 }
735
736 virtual void wake() {
737 }
738};
739
740
741// --- FakeInputReaderContext ---
742
743class FakeInputReaderContext : public InputReaderContext {
744 sp<EventHubInterface> mEventHub;
745 sp<InputReaderPolicyInterface> mPolicy;
746 sp<InputListenerInterface> mListener;
747 int32_t mGlobalMetaState;
748 bool mUpdateGlobalMetaStateWasCalled;
749 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800750 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751
752public:
753 FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
754 const sp<InputReaderPolicyInterface>& policy,
755 const sp<InputListenerInterface>& listener) :
756 mEventHub(eventHub), mPolicy(policy), mListener(listener),
Prabir Pradhan42611e02018-11-27 14:04:02 -0800757 mGlobalMetaState(0), mNextSequenceNum(1) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 }
759
760 virtual ~FakeInputReaderContext() { }
761
762 void assertUpdateGlobalMetaStateWasCalled() {
763 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
764 << "Expected updateGlobalMetaState() to have been called.";
765 mUpdateGlobalMetaStateWasCalled = false;
766 }
767
768 void setGlobalMetaState(int32_t state) {
769 mGlobalMetaState = state;
770 }
771
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800772 uint32_t getGeneration() {
773 return mGeneration;
774 }
775
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776private:
777 virtual void updateGlobalMetaState() {
778 mUpdateGlobalMetaStateWasCalled = true;
779 }
780
781 virtual int32_t getGlobalMetaState() {
782 return mGlobalMetaState;
783 }
784
785 virtual EventHubInterface* getEventHub() {
786 return mEventHub.get();
787 }
788
789 virtual InputReaderPolicyInterface* getPolicy() {
790 return mPolicy.get();
791 }
792
793 virtual InputListenerInterface* getListener() {
794 return mListener.get();
795 }
796
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100797 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 }
799
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100800 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801 return false;
802 }
803
804 virtual void fadePointer() {
805 }
806
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100807 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 }
809
810 virtual int32_t bumpGeneration() {
811 return ++mGeneration;
812 }
Michael Wright842500e2015-03-13 17:32:02 -0700813
814 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) {
815
816 }
817
818 virtual void dispatchExternalStylusState(const StylusState&) {
819
820 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800821
822 virtual uint32_t getNextSequenceNum() {
823 return mNextSequenceNum++;
824 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825};
826
827
828// --- FakeInputMapper ---
829
830class FakeInputMapper : public InputMapper {
831 uint32_t mSources;
832 int32_t mKeyboardType;
833 int32_t mMetaState;
834 KeyedVector<int32_t, int32_t> mKeyCodeStates;
835 KeyedVector<int32_t, int32_t> mScanCodeStates;
836 KeyedVector<int32_t, int32_t> mSwitchStates;
837 Vector<int32_t> mSupportedKeyCodes;
838 RawEvent mLastEvent;
839
840 bool mConfigureWasCalled;
841 bool mResetWasCalled;
842 bool mProcessWasCalled;
843
844public:
845 FakeInputMapper(InputDevice* device, uint32_t sources) :
846 InputMapper(device),
847 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
848 mMetaState(0),
849 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
850 }
851
852 virtual ~FakeInputMapper() { }
853
854 void setKeyboardType(int32_t keyboardType) {
855 mKeyboardType = keyboardType;
856 }
857
858 void setMetaState(int32_t metaState) {
859 mMetaState = metaState;
860 }
861
862 void assertConfigureWasCalled() {
863 ASSERT_TRUE(mConfigureWasCalled)
864 << "Expected configure() to have been called.";
865 mConfigureWasCalled = false;
866 }
867
868 void assertResetWasCalled() {
869 ASSERT_TRUE(mResetWasCalled)
870 << "Expected reset() to have been called.";
871 mResetWasCalled = false;
872 }
873
Yi Kong9b14ac62018-07-17 13:48:38 -0700874 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 ASSERT_TRUE(mProcessWasCalled)
876 << "Expected process() to have been called.";
877 if (outLastEvent) {
878 *outLastEvent = mLastEvent;
879 }
880 mProcessWasCalled = false;
881 }
882
883 void setKeyCodeState(int32_t keyCode, int32_t state) {
884 mKeyCodeStates.replaceValueFor(keyCode, state);
885 }
886
887 void setScanCodeState(int32_t scanCode, int32_t state) {
888 mScanCodeStates.replaceValueFor(scanCode, state);
889 }
890
891 void setSwitchState(int32_t switchCode, int32_t state) {
892 mSwitchStates.replaceValueFor(switchCode, state);
893 }
894
895 void addSupportedKeyCode(int32_t keyCode) {
896 mSupportedKeyCodes.add(keyCode);
897 }
898
899private:
900 virtual uint32_t getSources() {
901 return mSources;
902 }
903
904 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
905 InputMapper::populateDeviceInfo(deviceInfo);
906
907 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
908 deviceInfo->setKeyboardType(mKeyboardType);
909 }
910 }
911
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100912 virtual void configure(nsecs_t, const InputReaderConfiguration*, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 mConfigureWasCalled = true;
914 }
915
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100916 virtual void reset(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 mResetWasCalled = true;
918 }
919
920 virtual void process(const RawEvent* rawEvent) {
921 mLastEvent = *rawEvent;
922 mProcessWasCalled = true;
923 }
924
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100925 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
927 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
928 }
929
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100930 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
932 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
933 }
934
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100935 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 ssize_t index = mSwitchStates.indexOfKey(switchCode);
937 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
938 }
939
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100940 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 const int32_t* keyCodes, uint8_t* outFlags) {
942 bool result = false;
943 for (size_t i = 0; i < numCodes; i++) {
944 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
945 if (keyCodes[i] == mSupportedKeyCodes[j]) {
946 outFlags[i] = 1;
947 result = true;
948 }
949 }
950 }
951 return result;
952 }
953
954 virtual int32_t getMetaState() {
955 return mMetaState;
956 }
957
958 virtual void fadePointer() {
959 }
960};
961
962
963// --- InstrumentedInputReader ---
964
965class InstrumentedInputReader : public InputReader {
966 InputDevice* mNextDevice;
967
968public:
969 InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
970 const sp<InputReaderPolicyInterface>& policy,
971 const sp<InputListenerInterface>& listener) :
972 InputReader(eventHub, policy, listener),
Yi Kong9b14ac62018-07-17 13:48:38 -0700973 mNextDevice(nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 }
975
976 virtual ~InstrumentedInputReader() {
977 if (mNextDevice) {
978 delete mNextDevice;
979 }
980 }
981
982 void setNextDevice(InputDevice* device) {
983 mNextDevice = device;
984 }
985
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100986 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987 uint32_t classes) {
988 InputDeviceIdentifier identifier;
989 identifier.name = name;
990 int32_t generation = deviceId + 1;
991 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
992 classes);
993 }
994
995protected:
996 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
997 const InputDeviceIdentifier& identifier, uint32_t classes) {
998 if (mNextDevice) {
999 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001000 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001 return device;
1002 }
1003 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1004 }
1005
1006 friend class InputReaderTest;
1007};
1008
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001009// --- InputReaderPolicyTest ---
1010class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001011protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001012 sp<FakeInputReaderPolicy> mFakePolicy;
1013
1014 virtual void SetUp() {
1015 mFakePolicy = new FakeInputReaderPolicy();
1016 }
1017 virtual void TearDown() {
1018 mFakePolicy.clear();
1019 }
1020};
1021
1022/**
1023 * Check that empty set of viewports is an acceptable configuration.
1024 * Also try to get internal viewport two different ways - by type and by uniqueId.
1025 *
1026 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1027 * Such configuration is not currently allowed.
1028 */
1029TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001030 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001031
1032 // We didn't add any viewports yet, so there shouldn't be any.
1033 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001034 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001035 ASSERT_FALSE(internalViewport);
1036
1037 // Add an internal viewport, then clear it
1038 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001039 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001040
1041 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001042 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001043 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001044 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001045
1046 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001047 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001048 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001049 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001050
1051 mFakePolicy->clearViewports();
1052 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001053 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001054 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001055 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001056 ASSERT_FALSE(internalViewport);
1057}
1058
1059TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1060 const std::string internalUniqueId = "local:0";
1061 const std::string externalUniqueId = "local:1";
1062 const std::string virtualUniqueId1 = "virtual:2";
1063 const std::string virtualUniqueId2 = "virtual:3";
1064 constexpr int32_t virtualDisplayId1 = 2;
1065 constexpr int32_t virtualDisplayId2 = 3;
1066
1067 // Add an internal viewport
1068 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001069 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001070 // Add an external viewport
1071 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001072 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001073 // Add an virtual viewport
1074 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001075 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001076 // Add another virtual viewport
1077 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001078 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001079
1080 // Check matching by type for internal
1081 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001082 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001083 ASSERT_TRUE(internalViewport);
1084 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1085
1086 // Check matching by type for external
1087 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001088 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001089 ASSERT_TRUE(externalViewport);
1090 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1091
1092 // Check matching by uniqueId for virtual viewport #1
1093 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001094 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001095 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001096 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001097 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1098 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1099
1100 // Check matching by uniqueId for virtual viewport #2
1101 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001102 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001103 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001104 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001105 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1106 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1107}
1108
1109
1110/**
1111 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1112 * that lookup works by checking display id.
1113 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1114 */
1115TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1116 const std::string uniqueId1 = "uniqueId1";
1117 const std::string uniqueId2 = "uniqueId2";
1118 constexpr int32_t displayId1 = 2;
1119 constexpr int32_t displayId2 = 3;
1120
1121 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1122 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1123 for (const ViewportType& type : types) {
1124 mFakePolicy->clearViewports();
1125 // Add a viewport
1126 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001127 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001128 // Add another viewport
1129 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001130 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001131
1132 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001133 std::optional<DisplayViewport> viewport1 =
1134 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001135 ASSERT_TRUE(viewport1);
1136 ASSERT_EQ(displayId1, viewport1->displayId);
1137 ASSERT_EQ(type, viewport1->type);
1138
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001139 std::optional<DisplayViewport> viewport2 =
1140 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001141 ASSERT_TRUE(viewport2);
1142 ASSERT_EQ(displayId2, viewport2->displayId);
1143 ASSERT_EQ(type, viewport2->type);
1144
1145 // When there are multiple viewports of the same kind, and uniqueId is not specified
1146 // in the call to getDisplayViewport, then that situation is not supported.
1147 // The viewports can be stored in any order, so we cannot rely on the order, since that
1148 // is just implementation detail.
1149 // However, we can check that it still returns *a* viewport, we just cannot assert
1150 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001151 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001152 ASSERT_TRUE(someViewport);
1153 }
1154}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001156/**
1157 * Check getDisplayViewportByPort
1158 */
1159TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1160 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1161 const std::string uniqueId1 = "uniqueId1";
1162 const std::string uniqueId2 = "uniqueId2";
1163 constexpr int32_t displayId1 = 1;
1164 constexpr int32_t displayId2 = 2;
1165 const uint8_t hdmi1 = 0;
1166 const uint8_t hdmi2 = 1;
1167 const uint8_t hdmi3 = 2;
1168
1169 mFakePolicy->clearViewports();
1170 // Add a viewport that's associated with some display port that's not of interest.
1171 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1172 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1173 // Add another viewport, connected to HDMI1 port
1174 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1175 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1176
1177 // Check that correct display viewport was returned by comparing the display ports.
1178 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1179 ASSERT_TRUE(hdmi1Viewport);
1180 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1181 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1182
1183 // Check that we can still get the same viewport using the uniqueId
1184 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1185 ASSERT_TRUE(hdmi1Viewport);
1186 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1187 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1188 ASSERT_EQ(type, hdmi1Viewport->type);
1189
1190 // Check that we cannot find a port with "HDMI2", because we never added one
1191 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1192 ASSERT_FALSE(hdmi2Viewport);
1193}
1194
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195// --- InputReaderTest ---
1196
1197class InputReaderTest : public testing::Test {
1198protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001199 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 sp<FakeInputReaderPolicy> mFakePolicy;
1201 sp<FakeEventHub> mFakeEventHub;
1202 sp<InstrumentedInputReader> mReader;
1203
1204 virtual void SetUp() {
1205 mFakeEventHub = new FakeEventHub();
1206 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001207 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208
1209 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1210 }
1211
1212 virtual void TearDown() {
1213 mReader.clear();
1214
1215 mFakeListener.clear();
1216 mFakePolicy.clear();
1217 mFakeEventHub.clear();
1218 }
1219
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001220 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 const PropertyMap* configuration) {
1222 mFakeEventHub->addDevice(deviceId, name, classes);
1223
1224 if (configuration) {
1225 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1226 }
1227 mFakeEventHub->finishDeviceScan();
1228 mReader->loopOnce();
1229 mReader->loopOnce();
1230 mFakeEventHub->assertQueueIsEmpty();
1231 }
1232
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001233 void disableDevice(int32_t deviceId, InputDevice* device) {
1234 mFakePolicy->addDisabledDevice(deviceId);
1235 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1236 }
1237
1238 void enableDevice(int32_t deviceId, InputDevice* device) {
1239 mFakePolicy->removeDisabledDevice(deviceId);
1240 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1241 }
1242
1243 void configureDevice(uint32_t changes, InputDevice* device) {
1244 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1245 }
1246
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001248 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249 const PropertyMap* configuration) {
1250 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1251 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1252 device->addMapper(mapper);
1253 mReader->setNextDevice(device);
1254 addDevice(deviceId, name, classes, configuration);
1255 return mapper;
1256 }
1257};
1258
1259TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001260 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001261 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001262 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001263 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001264
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001265
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266 Vector<InputDeviceInfo> inputDevices;
1267 mReader->getInputDevices(inputDevices);
1268
1269 ASSERT_EQ(1U, inputDevices.size());
1270 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001271 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1273 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1274 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1275
1276 // Should also have received a notification describing the new input devices.
1277 inputDevices = mFakePolicy->getInputDevices();
1278 ASSERT_EQ(1U, inputDevices.size());
1279 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001280 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1282 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1283 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1284}
1285
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001286TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1287 constexpr int32_t deviceId = 1;
1288 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001289 InputDevice* device = mReader->newDevice(deviceId, 0, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001290 // Must add at least one mapper or the device will be ignored!
1291 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1292 device->addMapper(mapper);
1293 mReader->setNextDevice(device);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001294 addDevice(deviceId, "fake", deviceClass, nullptr);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001295
Yi Kong9b14ac62018-07-17 13:48:38 -07001296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001297
1298 NotifyDeviceResetArgs resetArgs;
1299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1300 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1301 ASSERT_EQ(deviceId, resetArgs.deviceId);
1302
1303 ASSERT_EQ(device->isEnabled(), true);
1304 disableDevice(deviceId, device);
1305 mReader->loopOnce();
1306
1307 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1308 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1309 ASSERT_EQ(deviceId, resetArgs.deviceId);
1310 ASSERT_EQ(device->isEnabled(), false);
1311
1312 disableDevice(deviceId, device);
1313 mReader->loopOnce();
1314 mFakeListener->assertNotifyDeviceResetWasNotCalled();
1315 mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1316 ASSERT_EQ(device->isEnabled(), false);
1317
1318 enableDevice(deviceId, device);
1319 mReader->loopOnce();
1320 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1321 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1322 ASSERT_EQ(deviceId, resetArgs.deviceId);
1323 ASSERT_EQ(device->isEnabled(), true);
1324}
1325
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001327 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001328 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001329 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1331
1332 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1333 AINPUT_SOURCE_ANY, AKEYCODE_A))
1334 << "Should return unknown when the device id is >= 0 but unknown.";
1335
1336 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1337 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1338 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1339
1340 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1341 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1342 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1343
1344 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1345 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1346 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1347
1348 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1349 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1350 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1351}
1352
1353TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001354 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001355 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001356 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001357 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1358
1359 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1360 AINPUT_SOURCE_ANY, KEY_A))
1361 << "Should return unknown when the device id is >= 0 but unknown.";
1362
1363 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1364 AINPUT_SOURCE_TRACKBALL, KEY_A))
1365 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1366
1367 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1368 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1369 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1370
1371 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1372 AINPUT_SOURCE_TRACKBALL, KEY_A))
1373 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1374
1375 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1376 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1377 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1378}
1379
1380TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001381 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001382 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001383 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1385
1386 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1387 AINPUT_SOURCE_ANY, SW_LID))
1388 << "Should return unknown when the device id is >= 0 but unknown.";
1389
1390 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1391 AINPUT_SOURCE_TRACKBALL, SW_LID))
1392 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1393
1394 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1395 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1396 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1397
1398 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1399 AINPUT_SOURCE_TRACKBALL, SW_LID))
1400 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1401
1402 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1403 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1404 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1405}
1406
1407TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001408 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001409 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001410 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001411
Michael Wrightd02c5b62014-02-10 15:10:22 -08001412 mapper->addSupportedKeyCode(AKEYCODE_A);
1413 mapper->addSupportedKeyCode(AKEYCODE_B);
1414
1415 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1416 uint8_t flags[4] = { 0, 0, 0, 1 };
1417
1418 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1419 << "Should return false when device id is >= 0 but unknown.";
1420 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1421
1422 flags[3] = 1;
1423 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1424 << "Should return false when device id is valid but the sources are not supported by the device.";
1425 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1426
1427 flags[3] = 1;
1428 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1429 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1430 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1431
1432 flags[3] = 1;
1433 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1434 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1435 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1436
1437 flags[3] = 1;
1438 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1439 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1440 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1441}
1442
1443TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001444 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001445
1446 NotifyConfigurationChangedArgs args;
1447
1448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1449 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1450}
1451
1452TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001453 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001454 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001455 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001456
1457 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1458 mReader->loopOnce();
1459 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1460
1461 RawEvent event;
1462 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1463 ASSERT_EQ(0, event.when);
1464 ASSERT_EQ(1, event.deviceId);
1465 ASSERT_EQ(EV_KEY, event.type);
1466 ASSERT_EQ(KEY_A, event.code);
1467 ASSERT_EQ(1, event.value);
1468}
1469
Prabir Pradhan42611e02018-11-27 14:04:02 -08001470TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1471 constexpr int32_t deviceId = 1;
1472 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1473 InputDevice* device = mReader->newDevice(deviceId, 0, "fake", deviceClass);
1474 // Must add at least one mapper or the device will be ignored!
1475 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1476 device->addMapper(mapper);
1477 mReader->setNextDevice(device);
1478 addDevice(deviceId, "fake", deviceClass, nullptr);
1479
1480 NotifyDeviceResetArgs resetArgs;
1481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1482 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1483
1484 disableDevice(deviceId, device);
1485 mReader->loopOnce();
1486 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1487 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1488 prevSequenceNum = resetArgs.sequenceNum;
1489
1490 enableDevice(deviceId, device);
1491 mReader->loopOnce();
1492 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1493 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1494 prevSequenceNum = resetArgs.sequenceNum;
1495
1496 disableDevice(deviceId, device);
1497 mReader->loopOnce();
1498 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1499 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1500 prevSequenceNum = resetArgs.sequenceNum;
1501}
1502
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503
1504// --- InputDeviceTest ---
1505
1506class InputDeviceTest : public testing::Test {
1507protected:
1508 static const char* DEVICE_NAME;
1509 static const int32_t DEVICE_ID;
1510 static const int32_t DEVICE_GENERATION;
1511 static const int32_t DEVICE_CONTROLLER_NUMBER;
1512 static const uint32_t DEVICE_CLASSES;
1513
1514 sp<FakeEventHub> mFakeEventHub;
1515 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001516 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001517 FakeInputReaderContext* mFakeContext;
1518
1519 InputDevice* mDevice;
1520
1521 virtual void SetUp() {
1522 mFakeEventHub = new FakeEventHub();
1523 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001524 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001525 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1526
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001527 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528 InputDeviceIdentifier identifier;
1529 identifier.name = DEVICE_NAME;
1530 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1531 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1532 }
1533
1534 virtual void TearDown() {
1535 delete mDevice;
1536
1537 delete mFakeContext;
1538 mFakeListener.clear();
1539 mFakePolicy.clear();
1540 mFakeEventHub.clear();
1541 }
1542};
1543
1544const char* InputDeviceTest::DEVICE_NAME = "device";
1545const int32_t InputDeviceTest::DEVICE_ID = 1;
1546const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1547const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1548const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1549 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1550
1551TEST_F(InputDeviceTest, ImmutableProperties) {
1552 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001553 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001554 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1555}
1556
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001557TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1558 ASSERT_EQ(mDevice->isEnabled(), true);
1559}
1560
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1562 // Configuration.
1563 InputReaderConfiguration config;
1564 mDevice->configure(ARBITRARY_TIME, &config, 0);
1565
1566 // Reset.
1567 mDevice->reset(ARBITRARY_TIME);
1568
1569 NotifyDeviceResetArgs resetArgs;
1570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1571 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1572 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1573
1574 // Metadata.
1575 ASSERT_TRUE(mDevice->isIgnored());
1576 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1577
1578 InputDeviceInfo info;
1579 mDevice->getDeviceInfo(&info);
1580 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001581 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1583 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1584
1585 // State queries.
1586 ASSERT_EQ(0, mDevice->getMetaState());
1587
1588 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1589 << "Ignored device should return unknown key code state.";
1590 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1591 << "Ignored device should return unknown scan code state.";
1592 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1593 << "Ignored device should return unknown switch state.";
1594
1595 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1596 uint8_t flags[2] = { 0, 1 };
1597 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1598 << "Ignored device should never mark any key codes.";
1599 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1600 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1601}
1602
1603TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1604 // Configuration.
1605 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1606
1607 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1608 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1609 mapper1->setMetaState(AMETA_ALT_ON);
1610 mapper1->addSupportedKeyCode(AKEYCODE_A);
1611 mapper1->addSupportedKeyCode(AKEYCODE_B);
1612 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1613 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1614 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1615 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1616 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1617 mDevice->addMapper(mapper1);
1618
1619 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1620 mapper2->setMetaState(AMETA_SHIFT_ON);
1621 mDevice->addMapper(mapper2);
1622
1623 InputReaderConfiguration config;
1624 mDevice->configure(ARBITRARY_TIME, &config, 0);
1625
1626 String8 propertyValue;
1627 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1628 << "Device should have read configuration during configuration phase.";
1629 ASSERT_STREQ("value", propertyValue.string());
1630
1631 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1632 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1633
1634 // Reset
1635 mDevice->reset(ARBITRARY_TIME);
1636 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1637 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1638
1639 NotifyDeviceResetArgs resetArgs;
1640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1641 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1642 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1643
1644 // Metadata.
1645 ASSERT_FALSE(mDevice->isIgnored());
1646 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1647
1648 InputDeviceInfo info;
1649 mDevice->getDeviceInfo(&info);
1650 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001651 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1653 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1654
1655 // State queries.
1656 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1657 << "Should query mappers and combine meta states.";
1658
1659 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1660 << "Should return unknown key code state when source not supported.";
1661 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1662 << "Should return unknown scan code state when source not supported.";
1663 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1664 << "Should return unknown switch state when source not supported.";
1665
1666 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1667 << "Should query mapper when source is supported.";
1668 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1669 << "Should query mapper when source is supported.";
1670 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1671 << "Should query mapper when source is supported.";
1672
1673 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1674 uint8_t flags[4] = { 0, 0, 0, 1 };
1675 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1676 << "Should do nothing when source is unsupported.";
1677 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1678 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1679 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1680 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1681
1682 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1683 << "Should query mapper when source is supported.";
1684 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1685 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1686 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1687 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1688
1689 // Event handling.
1690 RawEvent event;
1691 mDevice->process(&event, 1);
1692
1693 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1694 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1695}
1696
1697
1698// --- InputMapperTest ---
1699
1700class InputMapperTest : public testing::Test {
1701protected:
1702 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001703 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001704 static const int32_t DEVICE_ID;
1705 static const int32_t DEVICE_GENERATION;
1706 static const int32_t DEVICE_CONTROLLER_NUMBER;
1707 static const uint32_t DEVICE_CLASSES;
1708
1709 sp<FakeEventHub> mFakeEventHub;
1710 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001711 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712 FakeInputReaderContext* mFakeContext;
1713 InputDevice* mDevice;
1714
1715 virtual void SetUp() {
1716 mFakeEventHub = new FakeEventHub();
1717 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001718 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001719 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1720 InputDeviceIdentifier identifier;
1721 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001722 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001723 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1724 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1725
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001726 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001727 }
1728
1729 virtual void TearDown() {
1730 delete mDevice;
1731 delete mFakeContext;
1732 mFakeListener.clear();
1733 mFakePolicy.clear();
1734 mFakeEventHub.clear();
1735 }
1736
1737 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001738 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001739 }
1740
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001741 void configureDevice(uint32_t changes) {
1742 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1743 }
1744
Michael Wrightd02c5b62014-02-10 15:10:22 -08001745 void addMapperAndConfigure(InputMapper* mapper) {
1746 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001747 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001748 mDevice->reset(ARBITRARY_TIME);
1749 }
1750
1751 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001752 int32_t orientation, const std::string& uniqueId,
1753 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001754 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001755 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001756 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1757 }
1758
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001759 void clearViewports() {
1760 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001761 }
1762
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001763 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001764 int32_t code, int32_t value) {
1765 RawEvent event;
1766 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001767 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768 event.type = type;
1769 event.code = code;
1770 event.value = value;
1771 mapper->process(&event);
1772 }
1773
1774 static void assertMotionRange(const InputDeviceInfo& info,
1775 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1776 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001777 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001778 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1779 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1780 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1781 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1782 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1783 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1784 }
1785
1786 static void assertPointerCoords(const PointerCoords& coords,
1787 float x, float y, float pressure, float size,
1788 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1789 float orientation, float distance) {
1790 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1791 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1792 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1793 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1794 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1795 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1796 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1797 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1798 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1799 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1800 }
1801
1802 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1803 float actualX, actualY;
1804 controller->getPosition(&actualX, &actualY);
1805 ASSERT_NEAR(x, actualX, 1);
1806 ASSERT_NEAR(y, actualY, 1);
1807 }
1808};
1809
1810const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001811const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001812const int32_t InputMapperTest::DEVICE_ID = 1;
1813const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1814const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1815const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1816
1817
1818// --- SwitchInputMapperTest ---
1819
1820class SwitchInputMapperTest : public InputMapperTest {
1821protected:
1822};
1823
1824TEST_F(SwitchInputMapperTest, GetSources) {
1825 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1826 addMapperAndConfigure(mapper);
1827
1828 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1829}
1830
1831TEST_F(SwitchInputMapperTest, GetSwitchState) {
1832 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1833 addMapperAndConfigure(mapper);
1834
1835 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1836 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1837
1838 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1839 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1840}
1841
1842TEST_F(SwitchInputMapperTest, Process) {
1843 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1844 addMapperAndConfigure(mapper);
1845
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001846 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
1847 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1848 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
1849 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850
1851 NotifySwitchArgs args;
1852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1853 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08001854 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1855 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001856 args.switchMask);
1857 ASSERT_EQ(uint32_t(0), args.policyFlags);
1858}
1859
1860
1861// --- KeyboardInputMapperTest ---
1862
1863class KeyboardInputMapperTest : public InputMapperTest {
1864protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001865 const std::string UNIQUE_ID = "local:0";
1866
1867 void prepareDisplay(int32_t orientation);
1868
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 void testDPadKeyRotation(KeyboardInputMapper* mapper,
1870 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1871};
1872
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001873/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
1874 * orientation.
1875 */
1876void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
1877 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001878 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001879}
1880
Michael Wrightd02c5b62014-02-10 15:10:22 -08001881void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001882 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001883 NotifyKeyArgs args;
1884
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001885 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1887 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1888 ASSERT_EQ(originalScanCode, args.scanCode);
1889 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1890
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001891 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1893 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1894 ASSERT_EQ(originalScanCode, args.scanCode);
1895 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1896}
1897
1898
1899TEST_F(KeyboardInputMapperTest, GetSources) {
1900 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1901 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1902 addMapperAndConfigure(mapper);
1903
1904 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1905}
1906
1907TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
1908 const int32_t USAGE_A = 0x070004;
1909 const int32_t USAGE_UNKNOWN = 0x07ffff;
1910 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
1911 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
1912
1913 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1914 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1915 addMapperAndConfigure(mapper);
1916
1917 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001918 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001919 NotifyKeyArgs args;
1920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1921 ASSERT_EQ(DEVICE_ID, args.deviceId);
1922 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1923 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1924 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1925 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1926 ASSERT_EQ(KEY_HOME, args.scanCode);
1927 ASSERT_EQ(AMETA_NONE, args.metaState);
1928 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1929 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1930 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1931
1932 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001933 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1935 ASSERT_EQ(DEVICE_ID, args.deviceId);
1936 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1937 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1938 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1939 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1940 ASSERT_EQ(KEY_HOME, args.scanCode);
1941 ASSERT_EQ(AMETA_NONE, args.metaState);
1942 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1943 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1944 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1945
1946 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001947 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
1948 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1950 ASSERT_EQ(DEVICE_ID, args.deviceId);
1951 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1952 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1953 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1954 ASSERT_EQ(AKEYCODE_A, args.keyCode);
1955 ASSERT_EQ(0, args.scanCode);
1956 ASSERT_EQ(AMETA_NONE, args.metaState);
1957 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1958 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1959 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1960
1961 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001962 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
1963 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1965 ASSERT_EQ(DEVICE_ID, args.deviceId);
1966 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1967 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1968 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1969 ASSERT_EQ(AKEYCODE_A, args.keyCode);
1970 ASSERT_EQ(0, args.scanCode);
1971 ASSERT_EQ(AMETA_NONE, args.metaState);
1972 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1973 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1974 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1975
1976 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001977 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
1978 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1980 ASSERT_EQ(DEVICE_ID, args.deviceId);
1981 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1982 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1983 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1984 ASSERT_EQ(0, args.keyCode);
1985 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
1986 ASSERT_EQ(AMETA_NONE, args.metaState);
1987 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1988 ASSERT_EQ(0U, args.policyFlags);
1989 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1990
1991 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001992 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
1993 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1995 ASSERT_EQ(DEVICE_ID, args.deviceId);
1996 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1997 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1998 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1999 ASSERT_EQ(0, args.keyCode);
2000 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2001 ASSERT_EQ(AMETA_NONE, args.metaState);
2002 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2003 ASSERT_EQ(0U, args.policyFlags);
2004 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2005}
2006
2007TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2008 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2009 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2010
2011 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2012 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2013 addMapperAndConfigure(mapper);
2014
2015 // Initial metastate.
2016 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2017
2018 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002019 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002020 NotifyKeyArgs args;
2021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2022 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2023 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2024 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2025
2026 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002027 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2029 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2030 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2031
2032 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002033 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2036 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2037
2038 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002039 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2041 ASSERT_EQ(AMETA_NONE, args.metaState);
2042 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2043 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2044}
2045
2046TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2047 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2048 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2049 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2050 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2051
2052 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2053 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2054 addMapperAndConfigure(mapper);
2055
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002056 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002057 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2058 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2059 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2060 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2061 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2062 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2063 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2064 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2065}
2066
2067TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2068 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2069 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2070 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2071 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2072
2073 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2074 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2075 addConfigurationProperty("keyboard.orientationAware", "1");
2076 addMapperAndConfigure(mapper);
2077
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002078 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002079 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2080 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2081 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2082 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2083 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2084 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2085 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2086 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2087
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002088 clearViewports();
2089 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002090 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2091 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
2092 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2093 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
2094 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2095 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
2096 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2097 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
2098
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002099 clearViewports();
2100 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002101 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2102 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
2103 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2104 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
2105 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2106 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
2107 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2108 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
2109
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002110 clearViewports();
2111 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002112 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2113 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
2114 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2115 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
2116 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2117 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
2118 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2119 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
2120
2121 // Special case: if orientation changes while key is down, we still emit the same keycode
2122 // in the key up as we did in the key down.
2123 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002124 clearViewports();
2125 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002126 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2128 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2129 ASSERT_EQ(KEY_UP, args.scanCode);
2130 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2131
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002132 clearViewports();
2133 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002134 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2136 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2137 ASSERT_EQ(KEY_UP, args.scanCode);
2138 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2139}
2140
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002141TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2142 // If the keyboard is not orientation aware,
2143 // key events should not be associated with a specific display id
2144 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2145
2146 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2147 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2148 addMapperAndConfigure(mapper);
2149 NotifyKeyArgs args;
2150
2151 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002152 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002154 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2156 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2157
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002158 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002159 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002161 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2163 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2164}
2165
2166TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2167 // If the keyboard is orientation aware,
2168 // key events should be associated with the internal viewport
2169 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2170
2171 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2172 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2173 addConfigurationProperty("keyboard.orientationAware", "1");
2174 addMapperAndConfigure(mapper);
2175 NotifyKeyArgs args;
2176
2177 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2178 // ^--- already checked by the previous test
2179
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002180 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002181 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002182 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002184 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2186 ASSERT_EQ(DISPLAY_ID, args.displayId);
2187
2188 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002189 clearViewports();
2190 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002191 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002192 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002194 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2196 ASSERT_EQ(newDisplayId, args.displayId);
2197}
2198
Michael Wrightd02c5b62014-02-10 15:10:22 -08002199TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2200 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2201 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2202 addMapperAndConfigure(mapper);
2203
2204 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2205 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2206
2207 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2208 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2209}
2210
2211TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2212 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2213 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2214 addMapperAndConfigure(mapper);
2215
2216 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2217 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2218
2219 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2220 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2221}
2222
2223TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2224 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2225 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2226 addMapperAndConfigure(mapper);
2227
2228 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2229
2230 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2231 uint8_t flags[2] = { 0, 0 };
2232 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2233 ASSERT_TRUE(flags[0]);
2234 ASSERT_FALSE(flags[1]);
2235}
2236
2237TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2238 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2239 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2240 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2241 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2242 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2243 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2244
2245 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2246 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2247 addMapperAndConfigure(mapper);
2248
2249 // Initialization should have turned all of the lights off.
2250 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2251 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2252 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2253
2254 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002255 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2256 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2258 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2259 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2260 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2261
2262 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002263 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2264 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002265 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2266 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2267 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2268 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2269
2270 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002271 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2272 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002273 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2274 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2275 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2276 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2277
2278 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002279 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2280 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2282 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2283 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2284 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2285
2286 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002287 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2288 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2290 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2291 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2292 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2293
2294 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002295 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2296 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002297 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2298 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2299 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2300 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2301}
2302
2303
2304// --- CursorInputMapperTest ---
2305
2306class CursorInputMapperTest : public InputMapperTest {
2307protected:
2308 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2309
2310 sp<FakePointerController> mFakePointerController;
2311
2312 virtual void SetUp() {
2313 InputMapperTest::SetUp();
2314
2315 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002316 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002317 }
2318
2319 void testMotionRotation(CursorInputMapper* mapper,
2320 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002321
2322 void prepareDisplay(int32_t orientation) {
2323 const std::string uniqueId = "local:0";
2324 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2325 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2326 orientation, uniqueId, NO_PORT, viewportType);
2327 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002328};
2329
2330const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2331
2332void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2333 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2334 NotifyMotionArgs args;
2335
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002336 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2337 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2338 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2342 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2343 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2344 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2345}
2346
2347TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2348 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2349 addConfigurationProperty("cursor.mode", "pointer");
2350 addMapperAndConfigure(mapper);
2351
2352 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2353}
2354
2355TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2356 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2357 addConfigurationProperty("cursor.mode", "navigation");
2358 addMapperAndConfigure(mapper);
2359
2360 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2361}
2362
2363TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2364 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2365 addConfigurationProperty("cursor.mode", "pointer");
2366 addMapperAndConfigure(mapper);
2367
2368 InputDeviceInfo info;
2369 mapper->populateDeviceInfo(&info);
2370
2371 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002372 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2373 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002374 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2375 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2376
2377 // When the bounds are set, then there should be a valid motion range.
2378 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2379
2380 InputDeviceInfo info2;
2381 mapper->populateDeviceInfo(&info2);
2382
2383 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2384 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2385 1, 800 - 1, 0.0f, 0.0f));
2386 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2387 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2388 2, 480 - 1, 0.0f, 0.0f));
2389 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2390 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2391 0.0f, 1.0f, 0.0f, 0.0f));
2392}
2393
2394TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2395 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2396 addConfigurationProperty("cursor.mode", "navigation");
2397 addMapperAndConfigure(mapper);
2398
2399 InputDeviceInfo info;
2400 mapper->populateDeviceInfo(&info);
2401
2402 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2403 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2404 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2405 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2406 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2407 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2408 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2409 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2410 0.0f, 1.0f, 0.0f, 0.0f));
2411}
2412
2413TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2414 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2415 addConfigurationProperty("cursor.mode", "navigation");
2416 addMapperAndConfigure(mapper);
2417
2418 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2419
2420 NotifyMotionArgs args;
2421
2422 // Button press.
2423 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002424 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2425 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2427 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2428 ASSERT_EQ(DEVICE_ID, args.deviceId);
2429 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2430 ASSERT_EQ(uint32_t(0), args.policyFlags);
2431 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2432 ASSERT_EQ(0, args.flags);
2433 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2434 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2435 ASSERT_EQ(0, args.edgeFlags);
2436 ASSERT_EQ(uint32_t(1), args.pointerCount);
2437 ASSERT_EQ(0, args.pointerProperties[0].id);
2438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2440 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2441 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2442 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2443 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2444
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2446 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2447 ASSERT_EQ(DEVICE_ID, args.deviceId);
2448 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2449 ASSERT_EQ(uint32_t(0), args.policyFlags);
2450 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2451 ASSERT_EQ(0, args.flags);
2452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2453 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2454 ASSERT_EQ(0, args.edgeFlags);
2455 ASSERT_EQ(uint32_t(1), args.pointerCount);
2456 ASSERT_EQ(0, args.pointerProperties[0].id);
2457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2459 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2460 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2461 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2462 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2463
Michael Wrightd02c5b62014-02-10 15:10:22 -08002464 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002465 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2466 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2468 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2469 ASSERT_EQ(DEVICE_ID, args.deviceId);
2470 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2471 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002472 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2473 ASSERT_EQ(0, args.flags);
2474 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2475 ASSERT_EQ(0, args.buttonState);
2476 ASSERT_EQ(0, args.edgeFlags);
2477 ASSERT_EQ(uint32_t(1), args.pointerCount);
2478 ASSERT_EQ(0, args.pointerProperties[0].id);
2479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2481 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2482 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2483 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2484 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2485
2486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2487 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2488 ASSERT_EQ(DEVICE_ID, args.deviceId);
2489 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2490 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2492 ASSERT_EQ(0, args.flags);
2493 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2494 ASSERT_EQ(0, args.buttonState);
2495 ASSERT_EQ(0, args.edgeFlags);
2496 ASSERT_EQ(uint32_t(1), args.pointerCount);
2497 ASSERT_EQ(0, args.pointerProperties[0].id);
2498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2500 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2501 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2502 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2503 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2504}
2505
2506TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2507 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2508 addConfigurationProperty("cursor.mode", "navigation");
2509 addMapperAndConfigure(mapper);
2510
2511 NotifyMotionArgs args;
2512
2513 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002514 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2515 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2519 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2520
2521 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002522 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2523 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2527 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2528}
2529
2530TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2531 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2532 addConfigurationProperty("cursor.mode", "navigation");
2533 addMapperAndConfigure(mapper);
2534
2535 NotifyMotionArgs args;
2536
2537 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002538 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2539 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2541 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2543 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2544
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2546 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2548 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2549
Michael Wrightd02c5b62014-02-10 15:10:22 -08002550 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002551 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2552 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002554 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2556 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2557
2558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2561 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2562}
2563
2564TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2565 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2566 addConfigurationProperty("cursor.mode", "navigation");
2567 addMapperAndConfigure(mapper);
2568
2569 NotifyMotionArgs args;
2570
2571 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002572 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2573 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2574 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2575 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2579 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2580 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2581
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2583 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2585 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2586 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2587
Michael Wrightd02c5b62014-02-10 15:10:22 -08002588 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002589 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2590 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 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 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2596 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2597
2598 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002599 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2600 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002602 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2604 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2605
2606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2609 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2610}
2611
2612TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2613 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2614 addConfigurationProperty("cursor.mode", "navigation");
2615 addMapperAndConfigure(mapper);
2616
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002617 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2619 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2620 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2621 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2622 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2623 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2624 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2625 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2626}
2627
2628TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2629 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2630 addConfigurationProperty("cursor.mode", "navigation");
2631 addConfigurationProperty("cursor.orientationAware", "1");
2632 addMapperAndConfigure(mapper);
2633
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002634 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2636 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2637 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2638 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2639 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2640 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2641 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2642 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2643
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002644 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2646 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2647 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2648 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2649 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2650 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2651 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2652 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2653
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002654 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2656 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2657 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2658 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2659 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2660 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2661 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2662 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2663
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002664 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2666 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2667 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2668 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2669 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2670 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2671 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2672 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2673}
2674
2675TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2676 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2677 addConfigurationProperty("cursor.mode", "pointer");
2678 addMapperAndConfigure(mapper);
2679
2680 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2681 mFakePointerController->setPosition(100, 200);
2682 mFakePointerController->setButtonState(0);
2683
2684 NotifyMotionArgs motionArgs;
2685 NotifyKeyArgs keyArgs;
2686
2687 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002688 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2689 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2691 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2692 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2693 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2695 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2696
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2699 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2700 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2702 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2703
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002704 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2705 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002707 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 ASSERT_EQ(0, motionArgs.buttonState);
2709 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2711 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2712
2713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002714 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_EQ(0, motionArgs.buttonState);
2716 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2718 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2719
2720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002722 ASSERT_EQ(0, motionArgs.buttonState);
2723 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2725 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2726
2727 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002728 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2729 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2730 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2732 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2733 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2734 motionArgs.buttonState);
2735 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2736 mFakePointerController->getButtonState());
2737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2738 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2739
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2741 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2742 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2743 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2744 mFakePointerController->getButtonState());
2745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2746 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2747
2748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2750 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2751 motionArgs.buttonState);
2752 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2753 mFakePointerController->getButtonState());
2754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2755 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2756
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002757 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
2758 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2762 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2764 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2765
2766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002768 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2769 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770 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
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002773 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2774 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002776 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2777 ASSERT_EQ(0, motionArgs.buttonState);
2778 ASSERT_EQ(0, mFakePointerController->getButtonState());
2779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2780 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 -08002781 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2782 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002783
2784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785 ASSERT_EQ(0, motionArgs.buttonState);
2786 ASSERT_EQ(0, mFakePointerController->getButtonState());
2787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2789 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 -08002790
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2792 ASSERT_EQ(0, motionArgs.buttonState);
2793 ASSERT_EQ(0, mFakePointerController->getButtonState());
2794 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2796 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2797
2798 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002799 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
2800 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2802 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2803 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002804
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002806 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2808 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2810 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2811
2812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2813 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2814 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2815 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2817 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2818
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002819 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
2820 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002823 ASSERT_EQ(0, motionArgs.buttonState);
2824 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2826 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2827
2828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002830 ASSERT_EQ(0, motionArgs.buttonState);
2831 ASSERT_EQ(0, mFakePointerController->getButtonState());
2832
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2834 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2836 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2837 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2838
2839 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002840 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
2841 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2843 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2844 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002845
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2849 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2851 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2852
2853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2854 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2855 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2856 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2858 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2859
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002860 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
2861 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002863 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864 ASSERT_EQ(0, motionArgs.buttonState);
2865 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2867 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 -08002868
2869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2871 ASSERT_EQ(0, motionArgs.buttonState);
2872 ASSERT_EQ(0, mFakePointerController->getButtonState());
2873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2874 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2875
Michael Wrightd02c5b62014-02-10 15:10:22 -08002876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2877 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2878 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2879
2880 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002881 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
2882 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2884 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2885 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002886
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002888 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2890 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2892 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2893
2894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2896 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2897 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2899 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2900
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002901 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
2902 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905 ASSERT_EQ(0, motionArgs.buttonState);
2906 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2908 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 -08002909
2910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2912 ASSERT_EQ(0, motionArgs.buttonState);
2913 ASSERT_EQ(0, mFakePointerController->getButtonState());
2914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2915 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2916
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2918 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2919 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2920
2921 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002922 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
2923 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2925 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2926 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002927
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002929 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2931 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2933 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2934
2935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2937 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2938 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2940 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2941
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002942 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
2943 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002946 ASSERT_EQ(0, motionArgs.buttonState);
2947 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2949 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 -08002950
2951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2952 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2953 ASSERT_EQ(0, motionArgs.buttonState);
2954 ASSERT_EQ(0, mFakePointerController->getButtonState());
2955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2956 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2957
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2959 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2960 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2961}
2962
2963TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
2964 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2965 addConfigurationProperty("cursor.mode", "pointer");
2966 addMapperAndConfigure(mapper);
2967
2968 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2969 mFakePointerController->setPosition(100, 200);
2970 mFakePointerController->setButtonState(0);
2971
2972 NotifyMotionArgs args;
2973
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002974 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
2975 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
2976 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002978 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
2979 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
2980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2981 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2982 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
2983}
2984
2985TEST_F(CursorInputMapperTest, Process_PointerCapture) {
2986 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2987 addConfigurationProperty("cursor.mode", "pointer");
2988 mFakePolicy->setPointerCapture(true);
2989 addMapperAndConfigure(mapper);
2990
2991 NotifyDeviceResetArgs resetArgs;
2992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2993 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2994 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2995
2996 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2997 mFakePointerController->setPosition(100, 200);
2998 mFakePointerController->setButtonState(0);
2999
3000 NotifyMotionArgs args;
3001
3002 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003003 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3004 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3005 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3007 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3010 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3011 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3012
3013 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003014 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3015 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3017 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3020 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3022 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3023 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3025 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3026
3027 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003028 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3029 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3031 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3034 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3036 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3037 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3039 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3040
3041 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003042 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3043 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3044 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3046 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3049 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3050 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3051
3052 // Disable pointer capture and check that the device generation got bumped
3053 // and events are generated the usual way.
3054 const uint32_t generation = mFakeContext->getGeneration();
3055 mFakePolicy->setPointerCapture(false);
3056 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3057 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3058
3059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3060 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3061 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3062
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003063 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3064 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3065 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3067 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3070 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3071 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3072}
3073
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003074TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3075 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3076 addMapperAndConfigure(mapper);
3077
3078 // Setup PointerController for second display.
3079 constexpr int32_t SECOND_DISPLAY_ID = 1;
3080 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3081 mFakePointerController->setPosition(100, 200);
3082 mFakePointerController->setButtonState(0);
3083 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3084
3085 NotifyMotionArgs args;
3086 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3087 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3088 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3090 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3091 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3093 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3094 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3095 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3096}
3097
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
3099// --- TouchInputMapperTest ---
3100
3101class TouchInputMapperTest : public InputMapperTest {
3102protected:
3103 static const int32_t RAW_X_MIN;
3104 static const int32_t RAW_X_MAX;
3105 static const int32_t RAW_Y_MIN;
3106 static const int32_t RAW_Y_MAX;
3107 static const int32_t RAW_TOUCH_MIN;
3108 static const int32_t RAW_TOUCH_MAX;
3109 static const int32_t RAW_TOOL_MIN;
3110 static const int32_t RAW_TOOL_MAX;
3111 static const int32_t RAW_PRESSURE_MIN;
3112 static const int32_t RAW_PRESSURE_MAX;
3113 static const int32_t RAW_ORIENTATION_MIN;
3114 static const int32_t RAW_ORIENTATION_MAX;
3115 static const int32_t RAW_DISTANCE_MIN;
3116 static const int32_t RAW_DISTANCE_MAX;
3117 static const int32_t RAW_TILT_MIN;
3118 static const int32_t RAW_TILT_MAX;
3119 static const int32_t RAW_ID_MIN;
3120 static const int32_t RAW_ID_MAX;
3121 static const int32_t RAW_SLOT_MIN;
3122 static const int32_t RAW_SLOT_MAX;
3123 static const float X_PRECISION;
3124 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003125 static const float X_PRECISION_VIRTUAL;
3126 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127
3128 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003129 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130
3131 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3132
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003133 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003134 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003135
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 enum Axes {
3137 POSITION = 1 << 0,
3138 TOUCH = 1 << 1,
3139 TOOL = 1 << 2,
3140 PRESSURE = 1 << 3,
3141 ORIENTATION = 1 << 4,
3142 MINOR = 1 << 5,
3143 ID = 1 << 6,
3144 DISTANCE = 1 << 7,
3145 TILT = 1 << 8,
3146 SLOT = 1 << 9,
3147 TOOL_TYPE = 1 << 10,
3148 };
3149
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003150 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3151 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003152 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003154 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 int32_t toRawX(float displayX);
3156 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003157 float toCookedX(float rawX, float rawY);
3158 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003160 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003161 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003162 float toDisplayY(int32_t rawY, int32_t displayHeight);
3163
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164};
3165
3166const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3167const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3168const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3169const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3170const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3171const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3172const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3173const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003174const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3175const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3177const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3178const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3179const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3180const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3181const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3182const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3183const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3184const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3185const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3186const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3187const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003188const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3189 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3190const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3191 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003192const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3193 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
3195const float TouchInputMapperTest::GEOMETRIC_SCALE =
3196 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3197 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3198
3199const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3200 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3201 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3202};
3203
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003204void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003205 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003206 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3207}
3208
3209void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3210 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3211 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212}
3213
Santos Cordonfa5cf462017-04-05 10:37:00 -07003214void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003215 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3216 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003217 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003218}
3219
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220void TouchInputMapperTest::prepareVirtualKeys() {
3221 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3222 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3223 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3224 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3225}
3226
Jason Gerecke489fda82012-09-07 17:19:40 -07003227void TouchInputMapperTest::prepareLocationCalibration() {
3228 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3229}
3230
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231int32_t TouchInputMapperTest::toRawX(float displayX) {
3232 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3233}
3234
3235int32_t TouchInputMapperTest::toRawY(float displayY) {
3236 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3237}
3238
Jason Gerecke489fda82012-09-07 17:19:40 -07003239float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3240 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3241 return rawX;
3242}
3243
3244float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3245 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3246 return rawY;
3247}
3248
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003250 return toDisplayX(rawX, DISPLAY_WIDTH);
3251}
3252
3253float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3254 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255}
3256
3257float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003258 return toDisplayY(rawY, DISPLAY_HEIGHT);
3259}
3260
3261float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3262 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263}
3264
3265
3266// --- SingleTouchInputMapperTest ---
3267
3268class SingleTouchInputMapperTest : public TouchInputMapperTest {
3269protected:
3270 void prepareButtons();
3271 void prepareAxes(int axes);
3272
3273 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3274 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3275 void processUp(SingleTouchInputMapper* mappery);
3276 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3277 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3278 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3279 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3280 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3281 void processSync(SingleTouchInputMapper* mapper);
3282};
3283
3284void SingleTouchInputMapperTest::prepareButtons() {
3285 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3286}
3287
3288void SingleTouchInputMapperTest::prepareAxes(int axes) {
3289 if (axes & POSITION) {
3290 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3291 RAW_X_MIN, RAW_X_MAX, 0, 0);
3292 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3293 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3294 }
3295 if (axes & PRESSURE) {
3296 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3297 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3298 }
3299 if (axes & TOOL) {
3300 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3301 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3302 }
3303 if (axes & DISTANCE) {
3304 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3305 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3306 }
3307 if (axes & TILT) {
3308 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3309 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3310 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3311 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3312 }
3313}
3314
3315void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003316 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3317 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3318 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319}
3320
3321void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003322 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3323 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324}
3325
3326void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003327 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328}
3329
3330void SingleTouchInputMapperTest::processPressure(
3331 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003332 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333}
3334
3335void SingleTouchInputMapperTest::processToolMajor(
3336 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003337 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338}
3339
3340void SingleTouchInputMapperTest::processDistance(
3341 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003342 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343}
3344
3345void SingleTouchInputMapperTest::processTilt(
3346 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003347 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3348 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349}
3350
3351void SingleTouchInputMapperTest::processKey(
3352 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003353 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354}
3355
3356void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003357 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358}
3359
3360
3361TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3362 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3363 prepareButtons();
3364 prepareAxes(POSITION);
3365 addMapperAndConfigure(mapper);
3366
3367 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3368}
3369
3370TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3371 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3372 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3373 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3374 prepareButtons();
3375 prepareAxes(POSITION);
3376 addMapperAndConfigure(mapper);
3377
3378 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3379}
3380
3381TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3382 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3383 prepareButtons();
3384 prepareAxes(POSITION);
3385 addConfigurationProperty("touch.deviceType", "touchPad");
3386 addMapperAndConfigure(mapper);
3387
3388 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3389}
3390
3391TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3392 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3393 prepareButtons();
3394 prepareAxes(POSITION);
3395 addConfigurationProperty("touch.deviceType", "touchScreen");
3396 addMapperAndConfigure(mapper);
3397
3398 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3399}
3400
3401TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3402 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3403 addConfigurationProperty("touch.deviceType", "touchScreen");
3404 prepareDisplay(DISPLAY_ORIENTATION_0);
3405 prepareButtons();
3406 prepareAxes(POSITION);
3407 prepareVirtualKeys();
3408 addMapperAndConfigure(mapper);
3409
3410 // Unknown key.
3411 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3412
3413 // Virtual key is down.
3414 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3415 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3416 processDown(mapper, x, y);
3417 processSync(mapper);
3418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3419
3420 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3421
3422 // Virtual key is up.
3423 processUp(mapper);
3424 processSync(mapper);
3425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3426
3427 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3428}
3429
3430TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3431 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3432 addConfigurationProperty("touch.deviceType", "touchScreen");
3433 prepareDisplay(DISPLAY_ORIENTATION_0);
3434 prepareButtons();
3435 prepareAxes(POSITION);
3436 prepareVirtualKeys();
3437 addMapperAndConfigure(mapper);
3438
3439 // Unknown key.
3440 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3441
3442 // Virtual key is down.
3443 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3444 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3445 processDown(mapper, x, y);
3446 processSync(mapper);
3447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3448
3449 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3450
3451 // Virtual key is up.
3452 processUp(mapper);
3453 processSync(mapper);
3454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3455
3456 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3457}
3458
3459TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3460 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3461 addConfigurationProperty("touch.deviceType", "touchScreen");
3462 prepareDisplay(DISPLAY_ORIENTATION_0);
3463 prepareButtons();
3464 prepareAxes(POSITION);
3465 prepareVirtualKeys();
3466 addMapperAndConfigure(mapper);
3467
3468 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3469 uint8_t flags[2] = { 0, 0 };
3470 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3471 ASSERT_TRUE(flags[0]);
3472 ASSERT_FALSE(flags[1]);
3473}
3474
3475TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3476 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3477 addConfigurationProperty("touch.deviceType", "touchScreen");
3478 prepareDisplay(DISPLAY_ORIENTATION_0);
3479 prepareButtons();
3480 prepareAxes(POSITION);
3481 prepareVirtualKeys();
3482 addMapperAndConfigure(mapper);
3483
3484 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3485
3486 NotifyKeyArgs args;
3487
3488 // Press virtual key.
3489 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3490 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3491 processDown(mapper, x, y);
3492 processSync(mapper);
3493
3494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3495 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3496 ASSERT_EQ(DEVICE_ID, args.deviceId);
3497 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3498 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3499 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3500 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3501 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3502 ASSERT_EQ(KEY_HOME, args.scanCode);
3503 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3504 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3505
3506 // Release virtual key.
3507 processUp(mapper);
3508 processSync(mapper);
3509
3510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3511 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3512 ASSERT_EQ(DEVICE_ID, args.deviceId);
3513 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3514 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3515 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3516 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3517 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3518 ASSERT_EQ(KEY_HOME, args.scanCode);
3519 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3520 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3521
3522 // Should not have sent any motions.
3523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3524}
3525
3526TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3527 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3528 addConfigurationProperty("touch.deviceType", "touchScreen");
3529 prepareDisplay(DISPLAY_ORIENTATION_0);
3530 prepareButtons();
3531 prepareAxes(POSITION);
3532 prepareVirtualKeys();
3533 addMapperAndConfigure(mapper);
3534
3535 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3536
3537 NotifyKeyArgs keyArgs;
3538
3539 // Press virtual key.
3540 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3541 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3542 processDown(mapper, x, y);
3543 processSync(mapper);
3544
3545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3546 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3547 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3548 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3549 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3550 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3551 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3552 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3553 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3554 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3555 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3556
3557 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3558 // into the display area.
3559 y -= 100;
3560 processMove(mapper, x, y);
3561 processSync(mapper);
3562
3563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3564 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3565 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3566 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3567 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3568 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3569 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3570 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3571 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3572 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3573 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3574 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3575
3576 NotifyMotionArgs motionArgs;
3577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3578 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3579 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3580 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3581 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3583 ASSERT_EQ(0, motionArgs.flags);
3584 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3585 ASSERT_EQ(0, motionArgs.buttonState);
3586 ASSERT_EQ(0, motionArgs.edgeFlags);
3587 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3591 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3592 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3593 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3594 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3595
3596 // Keep moving out of bounds. Should generate a pointer move.
3597 y -= 50;
3598 processMove(mapper, x, y);
3599 processSync(mapper);
3600
3601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3602 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3603 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3604 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3605 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3607 ASSERT_EQ(0, motionArgs.flags);
3608 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3609 ASSERT_EQ(0, motionArgs.buttonState);
3610 ASSERT_EQ(0, motionArgs.edgeFlags);
3611 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3615 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3616 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3617 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3618 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3619
3620 // Release out of bounds. Should generate a pointer up.
3621 processUp(mapper);
3622 processSync(mapper);
3623
3624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3625 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3626 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3627 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3628 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3629 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3630 ASSERT_EQ(0, motionArgs.flags);
3631 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3632 ASSERT_EQ(0, motionArgs.buttonState);
3633 ASSERT_EQ(0, motionArgs.edgeFlags);
3634 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3638 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3639 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3640 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3641 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3642
3643 // Should not have sent any more keys or motions.
3644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3646}
3647
3648TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3649 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3650 addConfigurationProperty("touch.deviceType", "touchScreen");
3651 prepareDisplay(DISPLAY_ORIENTATION_0);
3652 prepareButtons();
3653 prepareAxes(POSITION);
3654 prepareVirtualKeys();
3655 addMapperAndConfigure(mapper);
3656
3657 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3658
3659 NotifyMotionArgs motionArgs;
3660
3661 // Initially go down out of bounds.
3662 int32_t x = -10;
3663 int32_t y = -10;
3664 processDown(mapper, x, y);
3665 processSync(mapper);
3666
3667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3668
3669 // Move into the display area. Should generate a pointer down.
3670 x = 50;
3671 y = 75;
3672 processMove(mapper, x, y);
3673 processSync(mapper);
3674
3675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3676 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3677 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3678 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3679 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3681 ASSERT_EQ(0, motionArgs.flags);
3682 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3683 ASSERT_EQ(0, motionArgs.buttonState);
3684 ASSERT_EQ(0, motionArgs.edgeFlags);
3685 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3686 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3689 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3690 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3691 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3693
3694 // Release. Should generate a pointer up.
3695 processUp(mapper);
3696 processSync(mapper);
3697
3698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3699 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3700 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3701 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3702 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3703 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3704 ASSERT_EQ(0, motionArgs.flags);
3705 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3706 ASSERT_EQ(0, motionArgs.buttonState);
3707 ASSERT_EQ(0, motionArgs.edgeFlags);
3708 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3709 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3712 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3713 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3714 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3715 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3716
3717 // Should not have sent any more keys or motions.
3718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3720}
3721
Santos Cordonfa5cf462017-04-05 10:37:00 -07003722TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3723 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3724 addConfigurationProperty("touch.deviceType", "touchScreen");
3725 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3726
3727 prepareVirtualDisplay(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 // Down.
3738 int32_t x = 100;
3739 int32_t y = 125;
3740 processDown(mapper, x, y);
3741 processSync(mapper);
3742
3743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3746 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3747 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3748 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3749 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3750 ASSERT_EQ(0, motionArgs.flags);
3751 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3752 ASSERT_EQ(0, motionArgs.buttonState);
3753 ASSERT_EQ(0, motionArgs.edgeFlags);
3754 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3755 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3758 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3759 1, 0, 0, 0, 0, 0, 0, 0));
3760 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3761 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3762 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3763
3764 // Move.
3765 x += 50;
3766 y += 75;
3767 processMove(mapper, x, y);
3768 processSync(mapper);
3769
3770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3771 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3772 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3773 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3774 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3775 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3777 ASSERT_EQ(0, motionArgs.flags);
3778 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3779 ASSERT_EQ(0, motionArgs.buttonState);
3780 ASSERT_EQ(0, motionArgs.edgeFlags);
3781 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3782 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3785 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3786 1, 0, 0, 0, 0, 0, 0, 0));
3787 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3788 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3789 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3790
3791 // Up.
3792 processUp(mapper);
3793 processSync(mapper);
3794
3795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3796 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3797 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3798 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3799 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3800 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3801 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3802 ASSERT_EQ(0, motionArgs.flags);
3803 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3804 ASSERT_EQ(0, motionArgs.buttonState);
3805 ASSERT_EQ(0, motionArgs.edgeFlags);
3806 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3810 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3811 1, 0, 0, 0, 0, 0, 0, 0));
3812 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3813 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3814 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3815
3816 // Should not have sent any more keys or motions.
3817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3819}
3820
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
3822 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3823 addConfigurationProperty("touch.deviceType", "touchScreen");
3824 prepareDisplay(DISPLAY_ORIENTATION_0);
3825 prepareButtons();
3826 prepareAxes(POSITION);
3827 prepareVirtualKeys();
3828 addMapperAndConfigure(mapper);
3829
3830 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3831
3832 NotifyMotionArgs motionArgs;
3833
3834 // Down.
3835 int32_t x = 100;
3836 int32_t y = 125;
3837 processDown(mapper, x, y);
3838 processSync(mapper);
3839
3840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3841 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3842 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3843 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3844 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3845 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3846 ASSERT_EQ(0, motionArgs.flags);
3847 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3848 ASSERT_EQ(0, motionArgs.buttonState);
3849 ASSERT_EQ(0, motionArgs.edgeFlags);
3850 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3854 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3855 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3856 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3858
3859 // Move.
3860 x += 50;
3861 y += 75;
3862 processMove(mapper, x, y);
3863 processSync(mapper);
3864
3865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3866 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3867 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3868 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3869 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3871 ASSERT_EQ(0, motionArgs.flags);
3872 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3873 ASSERT_EQ(0, motionArgs.buttonState);
3874 ASSERT_EQ(0, motionArgs.edgeFlags);
3875 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3876 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3879 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3880 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3881 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3882 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3883
3884 // Up.
3885 processUp(mapper);
3886 processSync(mapper);
3887
3888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3890 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3891 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3892 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3893 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3894 ASSERT_EQ(0, motionArgs.flags);
3895 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3896 ASSERT_EQ(0, motionArgs.buttonState);
3897 ASSERT_EQ(0, motionArgs.edgeFlags);
3898 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3899 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3902 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3903 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3904 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3906
3907 // Should not have sent any more keys or motions.
3908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3910}
3911
3912TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3913 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3914 addConfigurationProperty("touch.deviceType", "touchScreen");
3915 prepareButtons();
3916 prepareAxes(POSITION);
3917 addConfigurationProperty("touch.orientationAware", "0");
3918 addMapperAndConfigure(mapper);
3919
3920 NotifyMotionArgs args;
3921
3922 // Rotation 90.
3923 prepareDisplay(DISPLAY_ORIENTATION_90);
3924 processDown(mapper, toRawX(50), toRawY(75));
3925 processSync(mapper);
3926
3927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3928 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3929 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3930
3931 processUp(mapper);
3932 processSync(mapper);
3933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3934}
3935
3936TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
3937 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3938 addConfigurationProperty("touch.deviceType", "touchScreen");
3939 prepareButtons();
3940 prepareAxes(POSITION);
3941 addMapperAndConfigure(mapper);
3942
3943 NotifyMotionArgs args;
3944
3945 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003946 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947 prepareDisplay(DISPLAY_ORIENTATION_0);
3948 processDown(mapper, toRawX(50), toRawY(75));
3949 processSync(mapper);
3950
3951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3952 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3953 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3954
3955 processUp(mapper);
3956 processSync(mapper);
3957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3958
3959 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003960 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961 prepareDisplay(DISPLAY_ORIENTATION_90);
3962 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
3963 processSync(mapper);
3964
3965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3966 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3967 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3968
3969 processUp(mapper);
3970 processSync(mapper);
3971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3972
3973 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003974 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975 prepareDisplay(DISPLAY_ORIENTATION_180);
3976 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
3977 processSync(mapper);
3978
3979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3980 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3981 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3982
3983 processUp(mapper);
3984 processSync(mapper);
3985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3986
3987 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003988 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003989 prepareDisplay(DISPLAY_ORIENTATION_270);
3990 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
3991 processSync(mapper);
3992
3993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3994 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3995 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3996
3997 processUp(mapper);
3998 processSync(mapper);
3999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4000}
4001
4002TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4003 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4004 addConfigurationProperty("touch.deviceType", "touchScreen");
4005 prepareDisplay(DISPLAY_ORIENTATION_0);
4006 prepareButtons();
4007 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4008 addMapperAndConfigure(mapper);
4009
4010 // These calculations are based on the input device calibration documentation.
4011 int32_t rawX = 100;
4012 int32_t rawY = 200;
4013 int32_t rawPressure = 10;
4014 int32_t rawToolMajor = 12;
4015 int32_t rawDistance = 2;
4016 int32_t rawTiltX = 30;
4017 int32_t rawTiltY = 110;
4018
4019 float x = toDisplayX(rawX);
4020 float y = toDisplayY(rawY);
4021 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4022 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4023 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4024 float distance = float(rawDistance);
4025
4026 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4027 float tiltScale = M_PI / 180;
4028 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4029 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4030 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4031 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4032
4033 processDown(mapper, rawX, rawY);
4034 processPressure(mapper, rawPressure);
4035 processToolMajor(mapper, rawToolMajor);
4036 processDistance(mapper, rawDistance);
4037 processTilt(mapper, rawTiltX, rawTiltY);
4038 processSync(mapper);
4039
4040 NotifyMotionArgs args;
4041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4043 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4044 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4045}
4046
Jason Gerecke489fda82012-09-07 17:19:40 -07004047TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4048 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4049 addConfigurationProperty("touch.deviceType", "touchScreen");
4050 prepareDisplay(DISPLAY_ORIENTATION_0);
4051 prepareLocationCalibration();
4052 prepareButtons();
4053 prepareAxes(POSITION);
4054 addMapperAndConfigure(mapper);
4055
4056 int32_t rawX = 100;
4057 int32_t rawY = 200;
4058
4059 float x = toDisplayX(toCookedX(rawX, rawY));
4060 float y = toDisplayY(toCookedY(rawX, rawY));
4061
4062 processDown(mapper, rawX, rawY);
4063 processSync(mapper);
4064
4065 NotifyMotionArgs args;
4066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4068 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4069}
4070
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4072 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4073 addConfigurationProperty("touch.deviceType", "touchScreen");
4074 prepareDisplay(DISPLAY_ORIENTATION_0);
4075 prepareButtons();
4076 prepareAxes(POSITION);
4077 addMapperAndConfigure(mapper);
4078
4079 NotifyMotionArgs motionArgs;
4080 NotifyKeyArgs keyArgs;
4081
4082 processDown(mapper, 100, 200);
4083 processSync(mapper);
4084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4086 ASSERT_EQ(0, motionArgs.buttonState);
4087
4088 // press BTN_LEFT, release BTN_LEFT
4089 processKey(mapper, BTN_LEFT, 1);
4090 processSync(mapper);
4091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4093 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4094
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4097 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4098
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 processKey(mapper, BTN_LEFT, 0);
4100 processSync(mapper);
4101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004104
4105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004107 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108
4109 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4110 processKey(mapper, BTN_RIGHT, 1);
4111 processKey(mapper, BTN_MIDDLE, 1);
4112 processSync(mapper);
4113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4115 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4116 motionArgs.buttonState);
4117
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4120 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4121
4122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4123 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4124 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4125 motionArgs.buttonState);
4126
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127 processKey(mapper, BTN_RIGHT, 0);
4128 processSync(mapper);
4129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004130 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004132
4133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004135 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136
4137 processKey(mapper, BTN_MIDDLE, 0);
4138 processSync(mapper);
4139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004142
4143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004145 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146
4147 // press BTN_BACK, release BTN_BACK
4148 processKey(mapper, BTN_BACK, 1);
4149 processSync(mapper);
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4151 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4152 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004153
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4157
4158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4160 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161
4162 processKey(mapper, BTN_BACK, 0);
4163 processSync(mapper);
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004167
4168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004170 ASSERT_EQ(0, motionArgs.buttonState);
4171
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4173 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4174 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4175
4176 // press BTN_SIDE, release BTN_SIDE
4177 processKey(mapper, BTN_SIDE, 1);
4178 processSync(mapper);
4179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4180 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4181 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004182
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004185 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4186
4187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4189 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190
4191 processKey(mapper, BTN_SIDE, 0);
4192 processSync(mapper);
4193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004196
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004199 ASSERT_EQ(0, motionArgs.buttonState);
4200
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4202 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4203 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4204
4205 // press BTN_FORWARD, release BTN_FORWARD
4206 processKey(mapper, BTN_FORWARD, 1);
4207 processSync(mapper);
4208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4209 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4210 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004214 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4215
4216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4218 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219
4220 processKey(mapper, BTN_FORWARD, 0);
4221 processSync(mapper);
4222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225
4226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228 ASSERT_EQ(0, motionArgs.buttonState);
4229
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4231 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4232 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4233
4234 // press BTN_EXTRA, release BTN_EXTRA
4235 processKey(mapper, BTN_EXTRA, 1);
4236 processSync(mapper);
4237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4238 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4239 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4244
4245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4247 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248
4249 processKey(mapper, BTN_EXTRA, 0);
4250 processSync(mapper);
4251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254
4255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004257 ASSERT_EQ(0, motionArgs.buttonState);
4258
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4260 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4261 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4262
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4264
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 // press BTN_STYLUS, release BTN_STYLUS
4266 processKey(mapper, BTN_STYLUS, 1);
4267 processSync(mapper);
4268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4271
4272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4274 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
4276 processKey(mapper, BTN_STYLUS, 0);
4277 processSync(mapper);
4278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004281
4282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004284 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285
4286 // press BTN_STYLUS2, release BTN_STYLUS2
4287 processKey(mapper, BTN_STYLUS2, 1);
4288 processSync(mapper);
4289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004291 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4292
4293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4295 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296
4297 processKey(mapper, BTN_STYLUS2, 0);
4298 processSync(mapper);
4299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302
4303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306
4307 // release touch
4308 processUp(mapper);
4309 processSync(mapper);
4310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4311 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4312 ASSERT_EQ(0, motionArgs.buttonState);
4313}
4314
4315TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4316 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4317 addConfigurationProperty("touch.deviceType", "touchScreen");
4318 prepareDisplay(DISPLAY_ORIENTATION_0);
4319 prepareButtons();
4320 prepareAxes(POSITION);
4321 addMapperAndConfigure(mapper);
4322
4323 NotifyMotionArgs motionArgs;
4324
4325 // default tool type is finger
4326 processDown(mapper, 100, 200);
4327 processSync(mapper);
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4329 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4331
4332 // eraser
4333 processKey(mapper, BTN_TOOL_RUBBER, 1);
4334 processSync(mapper);
4335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4338
4339 // stylus
4340 processKey(mapper, BTN_TOOL_RUBBER, 0);
4341 processKey(mapper, BTN_TOOL_PEN, 1);
4342 processSync(mapper);
4343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4346
4347 // brush
4348 processKey(mapper, BTN_TOOL_PEN, 0);
4349 processKey(mapper, BTN_TOOL_BRUSH, 1);
4350 processSync(mapper);
4351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4354
4355 // pencil
4356 processKey(mapper, BTN_TOOL_BRUSH, 0);
4357 processKey(mapper, BTN_TOOL_PENCIL, 1);
4358 processSync(mapper);
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4362
4363 // airbrush
4364 processKey(mapper, BTN_TOOL_PENCIL, 0);
4365 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4366 processSync(mapper);
4367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4370
4371 // mouse
4372 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4373 processKey(mapper, BTN_TOOL_MOUSE, 1);
4374 processSync(mapper);
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4378
4379 // lens
4380 processKey(mapper, BTN_TOOL_MOUSE, 0);
4381 processKey(mapper, BTN_TOOL_LENS, 1);
4382 processSync(mapper);
4383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4386
4387 // double-tap
4388 processKey(mapper, BTN_TOOL_LENS, 0);
4389 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4390 processSync(mapper);
4391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4394
4395 // triple-tap
4396 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4397 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4398 processSync(mapper);
4399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4402
4403 // quad-tap
4404 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4405 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4406 processSync(mapper);
4407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4410
4411 // finger
4412 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4413 processKey(mapper, BTN_TOOL_FINGER, 1);
4414 processSync(mapper);
4415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4418
4419 // stylus trumps finger
4420 processKey(mapper, BTN_TOOL_PEN, 1);
4421 processSync(mapper);
4422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4425
4426 // eraser trumps stylus
4427 processKey(mapper, BTN_TOOL_RUBBER, 1);
4428 processSync(mapper);
4429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4432
4433 // mouse trumps eraser
4434 processKey(mapper, BTN_TOOL_MOUSE, 1);
4435 processSync(mapper);
4436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4439
4440 // back to default tool type
4441 processKey(mapper, BTN_TOOL_MOUSE, 0);
4442 processKey(mapper, BTN_TOOL_RUBBER, 0);
4443 processKey(mapper, BTN_TOOL_PEN, 0);
4444 processKey(mapper, BTN_TOOL_FINGER, 0);
4445 processSync(mapper);
4446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4449}
4450
4451TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4452 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4453 addConfigurationProperty("touch.deviceType", "touchScreen");
4454 prepareDisplay(DISPLAY_ORIENTATION_0);
4455 prepareButtons();
4456 prepareAxes(POSITION);
4457 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4458 addMapperAndConfigure(mapper);
4459
4460 NotifyMotionArgs motionArgs;
4461
4462 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4463 processKey(mapper, BTN_TOOL_FINGER, 1);
4464 processMove(mapper, 100, 200);
4465 processSync(mapper);
4466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4467 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4469 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4470
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4472 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4474 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4475
4476 // move a little
4477 processMove(mapper, 150, 250);
4478 processSync(mapper);
4479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4482 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4483
4484 // down when BTN_TOUCH is pressed, pressure defaults to 1
4485 processKey(mapper, BTN_TOUCH, 1);
4486 processSync(mapper);
4487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4490 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4491
4492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4495 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4496
4497 // up when BTN_TOUCH is released, hover restored
4498 processKey(mapper, BTN_TOUCH, 0);
4499 processSync(mapper);
4500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4501 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4503 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4504
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4508 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4509
4510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4513 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4514
4515 // exit hover when pointer goes away
4516 processKey(mapper, BTN_TOOL_FINGER, 0);
4517 processSync(mapper);
4518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4521 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4522}
4523
4524TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4525 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4526 addConfigurationProperty("touch.deviceType", "touchScreen");
4527 prepareDisplay(DISPLAY_ORIENTATION_0);
4528 prepareButtons();
4529 prepareAxes(POSITION | PRESSURE);
4530 addMapperAndConfigure(mapper);
4531
4532 NotifyMotionArgs motionArgs;
4533
4534 // initially hovering because pressure is 0
4535 processDown(mapper, 100, 200);
4536 processPressure(mapper, 0);
4537 processSync(mapper);
4538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4539 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4541 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4542
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4546 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4547
4548 // move a little
4549 processMove(mapper, 150, 250);
4550 processSync(mapper);
4551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4554 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4555
4556 // down when pressure is non-zero
4557 processPressure(mapper, RAW_PRESSURE_MAX);
4558 processSync(mapper);
4559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4562 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4563
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4567 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4568
4569 // up when pressure becomes 0, hover restored
4570 processPressure(mapper, 0);
4571 processSync(mapper);
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4575 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4576
4577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4578 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4580 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4581
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4585 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4586
4587 // exit hover when pointer goes away
4588 processUp(mapper);
4589 processSync(mapper);
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4593 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4594}
4595
Dan Harmsaca28402018-12-17 13:55:20 -08004596
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597// --- MultiTouchInputMapperTest ---
4598
4599class MultiTouchInputMapperTest : public TouchInputMapperTest {
4600protected:
4601 void prepareAxes(int axes);
4602
4603 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4604 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4605 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4606 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4607 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4608 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4609 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4610 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4611 void processId(MultiTouchInputMapper* mapper, int32_t id);
4612 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4613 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4614 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004615 void processTimestamp(MultiTouchInputMapper* mapper, uint32_t value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 void processMTSync(MultiTouchInputMapper* mapper);
4617 void processSync(MultiTouchInputMapper* mapper);
4618};
4619
4620void MultiTouchInputMapperTest::prepareAxes(int axes) {
4621 if (axes & POSITION) {
4622 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4623 RAW_X_MIN, RAW_X_MAX, 0, 0);
4624 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4625 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4626 }
4627 if (axes & TOUCH) {
4628 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4629 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4630 if (axes & MINOR) {
4631 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4632 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4633 }
4634 }
4635 if (axes & TOOL) {
4636 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4637 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4638 if (axes & MINOR) {
4639 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4640 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4641 }
4642 }
4643 if (axes & ORIENTATION) {
4644 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4645 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4646 }
4647 if (axes & PRESSURE) {
4648 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4649 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4650 }
4651 if (axes & DISTANCE) {
4652 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4653 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4654 }
4655 if (axes & ID) {
4656 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4657 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4658 }
4659 if (axes & SLOT) {
4660 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4661 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4662 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4663 }
4664 if (axes & TOOL_TYPE) {
4665 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4666 0, MT_TOOL_MAX, 0, 0);
4667 }
4668}
4669
4670void MultiTouchInputMapperTest::processPosition(
4671 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004672 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4673 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674}
4675
4676void MultiTouchInputMapperTest::processTouchMajor(
4677 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004678 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679}
4680
4681void MultiTouchInputMapperTest::processTouchMinor(
4682 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004683 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684}
4685
4686void MultiTouchInputMapperTest::processToolMajor(
4687 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004688 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689}
4690
4691void MultiTouchInputMapperTest::processToolMinor(
4692 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004693 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694}
4695
4696void MultiTouchInputMapperTest::processOrientation(
4697 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004698 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699}
4700
4701void MultiTouchInputMapperTest::processPressure(
4702 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004703 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704}
4705
4706void MultiTouchInputMapperTest::processDistance(
4707 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004708 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709}
4710
4711void MultiTouchInputMapperTest::processId(
4712 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004713 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714}
4715
4716void MultiTouchInputMapperTest::processSlot(
4717 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004718 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719}
4720
4721void MultiTouchInputMapperTest::processToolType(
4722 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004723 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724}
4725
4726void MultiTouchInputMapperTest::processKey(
4727 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004728 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729}
4730
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004731void MultiTouchInputMapperTest::processTimestamp(MultiTouchInputMapper* mapper, uint32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004732 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, value);
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004733}
4734
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004736 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737}
4738
4739void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004740 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741}
4742
4743
4744TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4745 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4746 addConfigurationProperty("touch.deviceType", "touchScreen");
4747 prepareDisplay(DISPLAY_ORIENTATION_0);
4748 prepareAxes(POSITION);
4749 prepareVirtualKeys();
4750 addMapperAndConfigure(mapper);
4751
4752 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4753
4754 NotifyMotionArgs motionArgs;
4755
4756 // Two fingers down at once.
4757 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4758 processPosition(mapper, x1, y1);
4759 processMTSync(mapper);
4760 processPosition(mapper, x2, y2);
4761 processMTSync(mapper);
4762 processSync(mapper);
4763
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4765 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4766 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4767 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4768 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4769 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4770 ASSERT_EQ(0, motionArgs.flags);
4771 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4772 ASSERT_EQ(0, motionArgs.buttonState);
4773 ASSERT_EQ(0, motionArgs.edgeFlags);
4774 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4778 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4779 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4780 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4782
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4784 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4785 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4786 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4787 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4788 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4789 motionArgs.action);
4790 ASSERT_EQ(0, motionArgs.flags);
4791 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4792 ASSERT_EQ(0, motionArgs.buttonState);
4793 ASSERT_EQ(0, motionArgs.edgeFlags);
4794 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4795 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4796 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4797 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4800 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4802 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4803 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4804 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4805 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4806
4807 // Move.
4808 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4809 processPosition(mapper, x1, y1);
4810 processMTSync(mapper);
4811 processPosition(mapper, x2, y2);
4812 processMTSync(mapper);
4813 processSync(mapper);
4814
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4817 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4818 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4819 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4821 ASSERT_EQ(0, motionArgs.flags);
4822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4823 ASSERT_EQ(0, motionArgs.buttonState);
4824 ASSERT_EQ(0, motionArgs.edgeFlags);
4825 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4826 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4828 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4831 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4833 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4834 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4835 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4837
4838 // First finger up.
4839 x2 += 15; y2 -= 20;
4840 processPosition(mapper, x2, y2);
4841 processMTSync(mapper);
4842 processSync(mapper);
4843
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4846 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4847 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4848 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4849 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4850 motionArgs.action);
4851 ASSERT_EQ(0, motionArgs.flags);
4852 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4853 ASSERT_EQ(0, motionArgs.buttonState);
4854 ASSERT_EQ(0, motionArgs.edgeFlags);
4855 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4856 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4858 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4861 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4863 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4864 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4865 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4866 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4867
4868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4869 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4870 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4871 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4872 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4874 ASSERT_EQ(0, motionArgs.flags);
4875 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4876 ASSERT_EQ(0, motionArgs.buttonState);
4877 ASSERT_EQ(0, motionArgs.edgeFlags);
4878 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4879 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4882 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4883 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4884 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4885 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4886
4887 // Move.
4888 x2 += 20; y2 -= 25;
4889 processPosition(mapper, x2, y2);
4890 processMTSync(mapper);
4891 processSync(mapper);
4892
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4895 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4896 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4897 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4899 ASSERT_EQ(0, motionArgs.flags);
4900 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4901 ASSERT_EQ(0, motionArgs.buttonState);
4902 ASSERT_EQ(0, motionArgs.edgeFlags);
4903 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4904 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4907 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4908 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4909 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4910 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4911
4912 // New finger down.
4913 int32_t x3 = 700, y3 = 300;
4914 processPosition(mapper, x2, y2);
4915 processMTSync(mapper);
4916 processPosition(mapper, x3, y3);
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_DOWN | (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(x3), toDisplayY(y3), 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 // Second finger up.
4945 x3 += 30; y3 -= 20;
4946 processPosition(mapper, x3, y3);
4947 processMTSync(mapper);
4948 processSync(mapper);
4949
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4951 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4952 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4953 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4954 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4955 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4956 motionArgs.action);
4957 ASSERT_EQ(0, motionArgs.flags);
4958 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4959 ASSERT_EQ(0, motionArgs.buttonState);
4960 ASSERT_EQ(0, motionArgs.edgeFlags);
4961 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4962 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4964 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4967 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4969 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4970 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4971 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4972 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4973
4974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4975 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4976 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4977 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4978 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4980 ASSERT_EQ(0, motionArgs.flags);
4981 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4982 ASSERT_EQ(0, motionArgs.buttonState);
4983 ASSERT_EQ(0, motionArgs.edgeFlags);
4984 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4985 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4986 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4988 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4989 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4990 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4991 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4992
4993 // Last finger up.
4994 processMTSync(mapper);
4995 processSync(mapper);
4996
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4998 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4999 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5000 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5001 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5002 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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(1), motionArgs.pointerCount);
5008 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5011 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5012 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5013 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5014 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5015
5016 // Should not have sent any more keys or motions.
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5019}
5020
5021TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5022 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5023 addConfigurationProperty("touch.deviceType", "touchScreen");
5024 prepareDisplay(DISPLAY_ORIENTATION_0);
5025 prepareAxes(POSITION | ID);
5026 prepareVirtualKeys();
5027 addMapperAndConfigure(mapper);
5028
5029 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5030
5031 NotifyMotionArgs motionArgs;
5032
5033 // Two fingers down at once.
5034 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5035 processPosition(mapper, x1, y1);
5036 processId(mapper, 1);
5037 processMTSync(mapper);
5038 processPosition(mapper, x2, y2);
5039 processId(mapper, 2);
5040 processMTSync(mapper);
5041 processSync(mapper);
5042
5043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5045 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5046 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5049 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5050
5051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5052 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5053 motionArgs.action);
5054 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5055 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5057 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5059 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5060 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5062 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5063
5064 // Move.
5065 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5066 processPosition(mapper, x1, y1);
5067 processId(mapper, 1);
5068 processMTSync(mapper);
5069 processPosition(mapper, x2, y2);
5070 processId(mapper, 2);
5071 processMTSync(mapper);
5072 processSync(mapper);
5073
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5076 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5079 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5082 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5084 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5085
5086 // First finger up.
5087 x2 += 15; y2 -= 20;
5088 processPosition(mapper, x2, y2);
5089 processId(mapper, 2);
5090 processMTSync(mapper);
5091 processSync(mapper);
5092
5093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5094 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5095 motionArgs.action);
5096 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5097 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5099 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5102 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5104 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5105
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5108 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5109 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5112 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5113
5114 // Move.
5115 x2 += 20; y2 -= 25;
5116 processPosition(mapper, x2, y2);
5117 processId(mapper, 2);
5118 processMTSync(mapper);
5119 processSync(mapper);
5120
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5123 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5124 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5127 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5128
5129 // New finger down.
5130 int32_t x3 = 700, y3 = 300;
5131 processPosition(mapper, x2, y2);
5132 processId(mapper, 2);
5133 processMTSync(mapper);
5134 processPosition(mapper, x3, y3);
5135 processId(mapper, 3);
5136 processMTSync(mapper);
5137 processSync(mapper);
5138
5139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5140 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5141 motionArgs.action);
5142 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5143 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5145 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5148 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5150 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5151
5152 // Second finger up.
5153 x3 += 30; y3 -= 20;
5154 processPosition(mapper, x3, y3);
5155 processId(mapper, 3);
5156 processMTSync(mapper);
5157 processSync(mapper);
5158
5159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5160 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5161 motionArgs.action);
5162 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5163 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5165 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5168 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5170 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5171
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5174 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5178 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5179
5180 // Last finger up.
5181 processMTSync(mapper);
5182 processSync(mapper);
5183
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5185 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5186 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5187 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5190 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5191
5192 // Should not have sent any more keys or motions.
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5195}
5196
5197TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5198 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5199 addConfigurationProperty("touch.deviceType", "touchScreen");
5200 prepareDisplay(DISPLAY_ORIENTATION_0);
5201 prepareAxes(POSITION | ID | SLOT);
5202 prepareVirtualKeys();
5203 addMapperAndConfigure(mapper);
5204
5205 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5206
5207 NotifyMotionArgs motionArgs;
5208
5209 // Two fingers down at once.
5210 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5211 processPosition(mapper, x1, y1);
5212 processId(mapper, 1);
5213 processSlot(mapper, 1);
5214 processPosition(mapper, x2, y2);
5215 processId(mapper, 2);
5216 processSync(mapper);
5217
5218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5220 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5221 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5224 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5225
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5227 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5228 motionArgs.action);
5229 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5230 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5232 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5235 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5237 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5238
5239 // Move.
5240 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5241 processSlot(mapper, 0);
5242 processPosition(mapper, x1, y1);
5243 processSlot(mapper, 1);
5244 processPosition(mapper, x2, y2);
5245 processSync(mapper);
5246
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5249 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5250 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5252 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5255 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5257 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5258
5259 // First finger up.
5260 x2 += 15; y2 -= 20;
5261 processSlot(mapper, 0);
5262 processId(mapper, -1);
5263 processSlot(mapper, 1);
5264 processPosition(mapper, x2, y2);
5265 processSync(mapper);
5266
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5268 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5269 motionArgs.action);
5270 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5271 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5273 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5276 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5278 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5279
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5283 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5286 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5287
5288 // Move.
5289 x2 += 20; y2 -= 25;
5290 processPosition(mapper, x2, y2);
5291 processSync(mapper);
5292
5293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5295 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5296 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5299 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5300
5301 // New finger down.
5302 int32_t x3 = 700, y3 = 300;
5303 processPosition(mapper, x2, y2);
5304 processSlot(mapper, 0);
5305 processId(mapper, 3);
5306 processPosition(mapper, x3, y3);
5307 processSync(mapper);
5308
5309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5310 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5311 motionArgs.action);
5312 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5313 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5315 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5318 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5320 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5321
5322 // Second finger up.
5323 x3 += 30; y3 -= 20;
5324 processSlot(mapper, 1);
5325 processId(mapper, -1);
5326 processSlot(mapper, 0);
5327 processPosition(mapper, x3, y3);
5328 processSync(mapper);
5329
5330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5331 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5332 motionArgs.action);
5333 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5334 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5336 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5339 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5341 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5342
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5345 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5346 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5349 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5350
5351 // Last finger up.
5352 processId(mapper, -1);
5353 processSync(mapper);
5354
5355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5357 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5358 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5361 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5362
5363 // Should not have sent any more keys or motions.
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5366}
5367
5368TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5369 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5370 addConfigurationProperty("touch.deviceType", "touchScreen");
5371 prepareDisplay(DISPLAY_ORIENTATION_0);
5372 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5373 addMapperAndConfigure(mapper);
5374
5375 // These calculations are based on the input device calibration documentation.
5376 int32_t rawX = 100;
5377 int32_t rawY = 200;
5378 int32_t rawTouchMajor = 7;
5379 int32_t rawTouchMinor = 6;
5380 int32_t rawToolMajor = 9;
5381 int32_t rawToolMinor = 8;
5382 int32_t rawPressure = 11;
5383 int32_t rawDistance = 0;
5384 int32_t rawOrientation = 3;
5385 int32_t id = 5;
5386
5387 float x = toDisplayX(rawX);
5388 float y = toDisplayY(rawY);
5389 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5390 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5391 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5392 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5393 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5394 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5395 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5396 float distance = float(rawDistance);
5397
5398 processPosition(mapper, rawX, rawY);
5399 processTouchMajor(mapper, rawTouchMajor);
5400 processTouchMinor(mapper, rawTouchMinor);
5401 processToolMajor(mapper, rawToolMajor);
5402 processToolMinor(mapper, rawToolMinor);
5403 processPressure(mapper, rawPressure);
5404 processOrientation(mapper, rawOrientation);
5405 processDistance(mapper, rawDistance);
5406 processId(mapper, id);
5407 processMTSync(mapper);
5408 processSync(mapper);
5409
5410 NotifyMotionArgs args;
5411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5412 ASSERT_EQ(0, args.pointerProperties[0].id);
5413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5414 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5415 orientation, distance));
5416}
5417
5418TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5419 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5420 addConfigurationProperty("touch.deviceType", "touchScreen");
5421 prepareDisplay(DISPLAY_ORIENTATION_0);
5422 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5423 addConfigurationProperty("touch.size.calibration", "geometric");
5424 addMapperAndConfigure(mapper);
5425
5426 // These calculations are based on the input device calibration documentation.
5427 int32_t rawX = 100;
5428 int32_t rawY = 200;
5429 int32_t rawTouchMajor = 140;
5430 int32_t rawTouchMinor = 120;
5431 int32_t rawToolMajor = 180;
5432 int32_t rawToolMinor = 160;
5433
5434 float x = toDisplayX(rawX);
5435 float y = toDisplayY(rawY);
5436 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5437 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5438 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5439 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5440 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5441
5442 processPosition(mapper, rawX, rawY);
5443 processTouchMajor(mapper, rawTouchMajor);
5444 processTouchMinor(mapper, rawTouchMinor);
5445 processToolMajor(mapper, rawToolMajor);
5446 processToolMinor(mapper, rawToolMinor);
5447 processMTSync(mapper);
5448 processSync(mapper);
5449
5450 NotifyMotionArgs args;
5451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5453 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5454}
5455
5456TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5457 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5458 addConfigurationProperty("touch.deviceType", "touchScreen");
5459 prepareDisplay(DISPLAY_ORIENTATION_0);
5460 prepareAxes(POSITION | TOUCH | TOOL);
5461 addConfigurationProperty("touch.size.calibration", "diameter");
5462 addConfigurationProperty("touch.size.scale", "10");
5463 addConfigurationProperty("touch.size.bias", "160");
5464 addConfigurationProperty("touch.size.isSummed", "1");
5465 addMapperAndConfigure(mapper);
5466
5467 // These calculations are based on the input device calibration documentation.
5468 // Note: We only provide a single common touch/tool value because the device is assumed
5469 // not to emit separate values for each pointer (isSummed = 1).
5470 int32_t rawX = 100;
5471 int32_t rawY = 200;
5472 int32_t rawX2 = 150;
5473 int32_t rawY2 = 250;
5474 int32_t rawTouchMajor = 5;
5475 int32_t rawToolMajor = 8;
5476
5477 float x = toDisplayX(rawX);
5478 float y = toDisplayY(rawY);
5479 float x2 = toDisplayX(rawX2);
5480 float y2 = toDisplayY(rawY2);
5481 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5482 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5483 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5484
5485 processPosition(mapper, rawX, rawY);
5486 processTouchMajor(mapper, rawTouchMajor);
5487 processToolMajor(mapper, rawToolMajor);
5488 processMTSync(mapper);
5489 processPosition(mapper, rawX2, rawY2);
5490 processTouchMajor(mapper, rawTouchMajor);
5491 processToolMajor(mapper, rawToolMajor);
5492 processMTSync(mapper);
5493 processSync(mapper);
5494
5495 NotifyMotionArgs args;
5496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5497 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5498
5499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5500 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5501 args.action);
5502 ASSERT_EQ(size_t(2), args.pointerCount);
5503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5504 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5506 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5507}
5508
5509TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5510 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5511 addConfigurationProperty("touch.deviceType", "touchScreen");
5512 prepareDisplay(DISPLAY_ORIENTATION_0);
5513 prepareAxes(POSITION | TOUCH | TOOL);
5514 addConfigurationProperty("touch.size.calibration", "area");
5515 addConfigurationProperty("touch.size.scale", "43");
5516 addConfigurationProperty("touch.size.bias", "3");
5517 addMapperAndConfigure(mapper);
5518
5519 // These calculations are based on the input device calibration documentation.
5520 int32_t rawX = 100;
5521 int32_t rawY = 200;
5522 int32_t rawTouchMajor = 5;
5523 int32_t rawToolMajor = 8;
5524
5525 float x = toDisplayX(rawX);
5526 float y = toDisplayY(rawY);
5527 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5528 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5529 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5530
5531 processPosition(mapper, rawX, rawY);
5532 processTouchMajor(mapper, rawTouchMajor);
5533 processToolMajor(mapper, rawToolMajor);
5534 processMTSync(mapper);
5535 processSync(mapper);
5536
5537 NotifyMotionArgs args;
5538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5540 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5541}
5542
5543TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5544 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5545 addConfigurationProperty("touch.deviceType", "touchScreen");
5546 prepareDisplay(DISPLAY_ORIENTATION_0);
5547 prepareAxes(POSITION | PRESSURE);
5548 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5549 addConfigurationProperty("touch.pressure.scale", "0.01");
5550 addMapperAndConfigure(mapper);
5551
Michael Wrightaa449c92017-12-13 21:21:43 +00005552 InputDeviceInfo info;
5553 mapper->populateDeviceInfo(&info);
5554 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5555 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5556 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5557
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558 // These calculations are based on the input device calibration documentation.
5559 int32_t rawX = 100;
5560 int32_t rawY = 200;
5561 int32_t rawPressure = 60;
5562
5563 float x = toDisplayX(rawX);
5564 float y = toDisplayY(rawY);
5565 float pressure = float(rawPressure) * 0.01f;
5566
5567 processPosition(mapper, rawX, rawY);
5568 processPressure(mapper, rawPressure);
5569 processMTSync(mapper);
5570 processSync(mapper);
5571
5572 NotifyMotionArgs args;
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5575 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5576}
5577
5578TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5579 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5580 addConfigurationProperty("touch.deviceType", "touchScreen");
5581 prepareDisplay(DISPLAY_ORIENTATION_0);
5582 prepareAxes(POSITION | ID | SLOT);
5583 addMapperAndConfigure(mapper);
5584
5585 NotifyMotionArgs motionArgs;
5586 NotifyKeyArgs keyArgs;
5587
5588 processId(mapper, 1);
5589 processPosition(mapper, 100, 200);
5590 processSync(mapper);
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5592 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5593 ASSERT_EQ(0, motionArgs.buttonState);
5594
5595 // press BTN_LEFT, release BTN_LEFT
5596 processKey(mapper, BTN_LEFT, 1);
5597 processSync(mapper);
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5600 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5601
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5604 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5605
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 processKey(mapper, BTN_LEFT, 0);
5607 processSync(mapper);
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005614 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005615
5616 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5617 processKey(mapper, BTN_RIGHT, 1);
5618 processKey(mapper, BTN_MIDDLE, 1);
5619 processSync(mapper);
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5622 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5623 motionArgs.buttonState);
5624
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5626 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5627 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5628
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5631 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5632 motionArgs.buttonState);
5633
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 processKey(mapper, BTN_RIGHT, 0);
5635 processSync(mapper);
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005637 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005639
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005642 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005643
5644 processKey(mapper, BTN_MIDDLE, 0);
5645 processSync(mapper);
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005649
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005652 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005653
5654 // press BTN_BACK, release BTN_BACK
5655 processKey(mapper, BTN_BACK, 1);
5656 processSync(mapper);
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5658 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5659 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005660
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005663 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5664
5665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5667 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668
5669 processKey(mapper, BTN_BACK, 0);
5670 processSync(mapper);
5671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005672 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005674
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005677 ASSERT_EQ(0, motionArgs.buttonState);
5678
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5680 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5681 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5682
5683 // press BTN_SIDE, release BTN_SIDE
5684 processKey(mapper, BTN_SIDE, 1);
5685 processSync(mapper);
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5687 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5688 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005689
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005692 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5693
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5695 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5696 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697
5698 processKey(mapper, BTN_SIDE, 0);
5699 processSync(mapper);
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005701 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005703
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005706 ASSERT_EQ(0, motionArgs.buttonState);
5707
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5709 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5710 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5711
5712 // press BTN_FORWARD, release BTN_FORWARD
5713 processKey(mapper, BTN_FORWARD, 1);
5714 processSync(mapper);
5715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5716 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5717 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005718
Michael Wrightd02c5b62014-02-10 15:10:22 -08005719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005721 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5722
5723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5725 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726
5727 processKey(mapper, BTN_FORWARD, 0);
5728 processSync(mapper);
5729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005735 ASSERT_EQ(0, motionArgs.buttonState);
5736
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5738 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5739 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5740
5741 // press BTN_EXTRA, release BTN_EXTRA
5742 processKey(mapper, BTN_EXTRA, 1);
5743 processSync(mapper);
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5745 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5746 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005747
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005750 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5751
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5754 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755
5756 processKey(mapper, BTN_EXTRA, 0);
5757 processSync(mapper);
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005759 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005761
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005764 ASSERT_EQ(0, motionArgs.buttonState);
5765
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5767 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5768 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5769
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5771
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772 // press BTN_STYLUS, release BTN_STYLUS
5773 processKey(mapper, BTN_STYLUS, 1);
5774 processSync(mapper);
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005777 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5778
5779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5781 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782
5783 processKey(mapper, BTN_STYLUS, 0);
5784 processSync(mapper);
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005786 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005788
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005791 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792
5793 // press BTN_STYLUS2, release BTN_STYLUS2
5794 processKey(mapper, BTN_STYLUS2, 1);
5795 processSync(mapper);
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005798 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5799
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5802 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803
5804 processKey(mapper, BTN_STYLUS2, 0);
5805 processSync(mapper);
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005807 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005808 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005809
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005812 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813
5814 // release touch
5815 processId(mapper, -1);
5816 processSync(mapper);
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5818 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5819 ASSERT_EQ(0, motionArgs.buttonState);
5820}
5821
5822TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
5823 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5824 addConfigurationProperty("touch.deviceType", "touchScreen");
5825 prepareDisplay(DISPLAY_ORIENTATION_0);
5826 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
5827 addMapperAndConfigure(mapper);
5828
5829 NotifyMotionArgs motionArgs;
5830
5831 // default tool type is finger
5832 processId(mapper, 1);
5833 processPosition(mapper, 100, 200);
5834 processSync(mapper);
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5838
5839 // eraser
5840 processKey(mapper, BTN_TOOL_RUBBER, 1);
5841 processSync(mapper);
5842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5845
5846 // stylus
5847 processKey(mapper, BTN_TOOL_RUBBER, 0);
5848 processKey(mapper, BTN_TOOL_PEN, 1);
5849 processSync(mapper);
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5853
5854 // brush
5855 processKey(mapper, BTN_TOOL_PEN, 0);
5856 processKey(mapper, BTN_TOOL_BRUSH, 1);
5857 processSync(mapper);
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5860 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5861
5862 // pencil
5863 processKey(mapper, BTN_TOOL_BRUSH, 0);
5864 processKey(mapper, BTN_TOOL_PENCIL, 1);
5865 processSync(mapper);
5866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5869
5870 // airbrush
5871 processKey(mapper, BTN_TOOL_PENCIL, 0);
5872 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5873 processSync(mapper);
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5876 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5877
5878 // mouse
5879 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5880 processKey(mapper, BTN_TOOL_MOUSE, 1);
5881 processSync(mapper);
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5885
5886 // lens
5887 processKey(mapper, BTN_TOOL_MOUSE, 0);
5888 processKey(mapper, BTN_TOOL_LENS, 1);
5889 processSync(mapper);
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5893
5894 // double-tap
5895 processKey(mapper, BTN_TOOL_LENS, 0);
5896 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5897 processSync(mapper);
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5901
5902 // triple-tap
5903 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5904 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5905 processSync(mapper);
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5909
5910 // quad-tap
5911 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5912 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5913 processSync(mapper);
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5917
5918 // finger
5919 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5920 processKey(mapper, BTN_TOOL_FINGER, 1);
5921 processSync(mapper);
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5925
5926 // stylus trumps finger
5927 processKey(mapper, BTN_TOOL_PEN, 1);
5928 processSync(mapper);
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5932
5933 // eraser trumps stylus
5934 processKey(mapper, BTN_TOOL_RUBBER, 1);
5935 processSync(mapper);
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5939
5940 // mouse trumps eraser
5941 processKey(mapper, BTN_TOOL_MOUSE, 1);
5942 processSync(mapper);
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5946
5947 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
5948 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
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_FINGER, motionArgs.pointerProperties[0].toolType);
5953
5954 // MT tool type trumps BTN tool types: MT_TOOL_PEN
5955 processToolType(mapper, MT_TOOL_PEN);
5956 processSync(mapper);
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5960
5961 // back to default tool type
5962 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
5963 processKey(mapper, BTN_TOOL_MOUSE, 0);
5964 processKey(mapper, BTN_TOOL_RUBBER, 0);
5965 processKey(mapper, BTN_TOOL_PEN, 0);
5966 processKey(mapper, BTN_TOOL_FINGER, 0);
5967 processSync(mapper);
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5971}
5972
5973TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
5974 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5975 addConfigurationProperty("touch.deviceType", "touchScreen");
5976 prepareDisplay(DISPLAY_ORIENTATION_0);
5977 prepareAxes(POSITION | ID | SLOT);
5978 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
5979 addMapperAndConfigure(mapper);
5980
5981 NotifyMotionArgs motionArgs;
5982
5983 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5984 processId(mapper, 1);
5985 processPosition(mapper, 100, 200);
5986 processSync(mapper);
5987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5988 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5990 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5991
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5993 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5995 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5996
5997 // move a little
5998 processPosition(mapper, 150, 250);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6001 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6003 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6004
6005 // down when BTN_TOUCH is pressed, pressure defaults to 1
6006 processKey(mapper, BTN_TOUCH, 1);
6007 processSync(mapper);
6008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6009 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6011 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6012
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6016 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6017
6018 // up when BTN_TOUCH is released, hover restored
6019 processKey(mapper, BTN_TOUCH, 0);
6020 processSync(mapper);
6021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6022 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6024 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6029 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6030
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6032 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6034 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6035
6036 // exit hover when pointer goes away
6037 processId(mapper, -1);
6038 processSync(mapper);
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6040 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6042 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6043}
6044
6045TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6046 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6047 addConfigurationProperty("touch.deviceType", "touchScreen");
6048 prepareDisplay(DISPLAY_ORIENTATION_0);
6049 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6050 addMapperAndConfigure(mapper);
6051
6052 NotifyMotionArgs motionArgs;
6053
6054 // initially hovering because pressure is 0
6055 processId(mapper, 1);
6056 processPosition(mapper, 100, 200);
6057 processPressure(mapper, 0);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6062 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6063
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6065 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6067 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6068
6069 // move a little
6070 processPosition(mapper, 150, 250);
6071 processSync(mapper);
6072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6073 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6075 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6076
6077 // down when pressure becomes non-zero
6078 processPressure(mapper, RAW_PRESSURE_MAX);
6079 processSync(mapper);
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6081 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6083 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6084
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6086 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6088 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6089
6090 // up when pressure becomes 0, hover restored
6091 processPressure(mapper, 0);
6092 processSync(mapper);
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6094 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6096 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6097
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6099 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6101 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6102
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6104 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6106 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6107
6108 // exit hover when pointer goes away
6109 processId(mapper, -1);
6110 processSync(mapper);
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6112 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6114 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6115}
6116
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08006117TEST_F(MultiTouchInputMapperTest, Process_HandlesTimestamp) {
6118 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6119
6120 addConfigurationProperty("touch.deviceType", "touchScreen");
6121 prepareDisplay(DISPLAY_ORIENTATION_0);
6122 prepareAxes(POSITION);
6123 addMapperAndConfigure(mapper);
6124 NotifyMotionArgs args;
6125
6126 // By default, deviceTimestamp should be zero
6127 processPosition(mapper, 100, 100);
6128 processMTSync(mapper);
6129 processSync(mapper);
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6131 ASSERT_EQ(0U, args.deviceTimestamp);
6132
6133 // Now the timestamp of 1000 is reported by evdev and should appear in MotionArgs
6134 processPosition(mapper, 0, 0);
6135 processTimestamp(mapper, 1000);
6136 processMTSync(mapper);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6139 ASSERT_EQ(1000U, args.deviceTimestamp);
6140}
6141
Siarhei Vishniakoueaf7acd2018-01-09 12:35:51 -08006142TEST_F(MultiTouchInputMapperTest, WhenMapperIsReset_TimestampIsCleared) {
6143 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6144
6145 addConfigurationProperty("touch.deviceType", "touchScreen");
6146 prepareDisplay(DISPLAY_ORIENTATION_0);
6147 prepareAxes(POSITION);
6148 addMapperAndConfigure(mapper);
6149 NotifyMotionArgs args;
6150
6151 // Send a touch event with a timestamp
6152 processPosition(mapper, 100, 100);
6153 processTimestamp(mapper, 1);
6154 processMTSync(mapper);
6155 processSync(mapper);
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6157 ASSERT_EQ(1U, args.deviceTimestamp);
6158
6159 // Since the data accumulates, and new timestamp has not arrived, deviceTimestamp won't change
6160 processPosition(mapper, 100, 200);
6161 processMTSync(mapper);
6162 processSync(mapper);
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6164 ASSERT_EQ(1U, args.deviceTimestamp);
6165
6166 mapper->reset(/* when */ 0);
6167 // After the mapper is reset, deviceTimestamp should become zero again
6168 processPosition(mapper, 100, 300);
6169 processMTSync(mapper);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6172 ASSERT_EQ(0U, args.deviceTimestamp);
6173}
6174
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006175/**
6176 * Set the input device port <--> display port associations, and check that the
6177 * events are routed to the display that matches the display port.
6178 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6179 */
6180TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6181 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6182 const std::string usb2 = "USB2";
6183 const uint8_t hdmi1 = 0;
6184 const uint8_t hdmi2 = 1;
6185 const std::string secondaryUniqueId = "uniqueId2";
6186 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6187
6188 addConfigurationProperty("touch.deviceType", "touchScreen");
6189 prepareAxes(POSITION);
6190 addMapperAndConfigure(mapper);
6191
6192 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6193 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6194
6195 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6196 // for this input device is specified, and the matching viewport is not present,
6197 // the input device should be disabled (at the mapper level).
6198
6199 // Add viewport for display 2 on hdmi2
6200 prepareSecondaryDisplay(type, hdmi2);
6201 // Send a touch event
6202 processPosition(mapper, 100, 100);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6205
6206 // Add viewport for display 1 on hdmi1
6207 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6208 // Send a touch event again
6209 processPosition(mapper, 100, 100);
6210 processSync(mapper);
6211
6212 NotifyMotionArgs args;
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6214 ASSERT_EQ(DISPLAY_ID, args.displayId);
6215}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216
Arthur Hung41a712e2018-11-22 19:41:03 +08006217/**
6218 * Expect fallback to internal viewport if device is external and external viewport is not present.
6219 */
6220TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6221 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6222 prepareAxes(POSITION);
6223 addConfigurationProperty("touch.deviceType", "touchScreen");
6224 prepareDisplay(DISPLAY_ORIENTATION_0);
6225 mDevice->setExternal(true);
6226 addMapperAndConfigure(mapper);
6227
6228 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6229
6230 NotifyMotionArgs motionArgs;
6231
6232 // Expect the event to be sent to the internal viewport,
6233 // because an external viewport is not present.
6234 processPosition(mapper, 100, 100);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6238
6239 // Expect the event to be sent to the external viewport if it is present.
6240 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6241 processPosition(mapper, 100, 100);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6245}
6246
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006247TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6248 // Setup PointerController for second display.
6249 sp<FakePointerController> fakePointerController = new FakePointerController();
6250 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6251 fakePointerController->setPosition(100, 200);
6252 fakePointerController->setButtonState(0);
6253 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6254 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6255
6256 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6257 prepareDisplay(DISPLAY_ORIENTATION_0);
6258 prepareAxes(POSITION);
6259 addMapperAndConfigure(mapper);
6260
6261 // Check source is mouse that would obtain the PointerController.
6262 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6263
6264 NotifyMotionArgs motionArgs;
6265 processPosition(mapper, 100, 100);
6266 processSync(mapper);
6267
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6270 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6271}
6272
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273} // namespace android