blob: aeb4ad62a8b8db95d47648bc413685d7bc3ee745 [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
Prabir Pradhan394eed02019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
21#include <KeyboardInputMapper.h>
22#include <MultiTouchInputMapper.h>
23#include <SingleTouchInputMapper.h>
24#include <SwitchInputMapper.h>
25#include <TestInputListener.h>
26#include <TouchInputMapper.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080029#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <math.h>
31
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080032
Michael Wrightd02c5b62014-02-10 15:10:22 -080033namespace android {
34
35// An arbitrary time value.
36static const nsecs_t ARBITRARY_TIME = 1234;
37
38// Arbitrary display properties.
39static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070040static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080041static const int32_t DISPLAY_WIDTH = 480;
42static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070043static const int32_t VIRTUAL_DISPLAY_ID = 1;
44static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
45static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070046static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070047static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080048
49// Error tolerance for floating point assertions.
50static const float EPSILON = 0.001f;
51
52template<typename T>
53static inline T min(T a, T b) {
54 return a < b ? a : b;
55}
56
57static inline float avg(float x, float y) {
58 return (x + y) / 2;
59}
60
61
62// --- FakePointerController ---
63
64class FakePointerController : public PointerControllerInterface {
65 bool mHaveBounds;
66 float mMinX, mMinY, mMaxX, mMaxY;
67 float mX, mY;
68 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080069 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080070
71protected:
72 virtual ~FakePointerController() { }
73
74public:
75 FakePointerController() :
76 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080077 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080078 }
79
80 void setBounds(float minX, float minY, float maxX, float maxY) {
81 mHaveBounds = true;
82 mMinX = minX;
83 mMinY = minY;
84 mMaxX = maxX;
85 mMaxY = maxY;
86 }
87
Arthur Hungc7ad2d02018-12-18 17:41:29 +080088 void setDisplayId(int32_t displayId) {
89 mDisplayId = displayId;
90 }
91
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 virtual void setPosition(float x, float y) {
93 mX = x;
94 mY = y;
95 }
96
97 virtual void setButtonState(int32_t buttonState) {
98 mButtonState = buttonState;
99 }
100
101 virtual int32_t getButtonState() const {
102 return mButtonState;
103 }
104
105 virtual void getPosition(float* outX, float* outY) const {
106 *outX = mX;
107 *outY = mY;
108 }
109
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800110 virtual int32_t getDisplayId() const {
111 return mDisplayId;
112 }
113
Arthur Hung7c645402019-01-25 17:45:42 +0800114 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
115 return mSpotsByDisplay;
116 }
117
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118private:
119 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
120 *outMinX = mMinX;
121 *outMinY = mMinY;
122 *outMaxX = mMaxX;
123 *outMaxY = mMaxY;
124 return mHaveBounds;
125 }
126
127 virtual void move(float deltaX, float deltaY) {
128 mX += deltaX;
129 if (mX < mMinX) mX = mMinX;
130 if (mX > mMaxX) mX = mMaxX;
131 mY += deltaY;
132 if (mY < mMinY) mY = mMinY;
133 if (mY > mMaxY) mY = mMaxY;
134 }
135
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100136 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137 }
138
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100139 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 }
141
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100142 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 }
144
Arthur Hung7c645402019-01-25 17:45:42 +0800145 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
146 int32_t displayId) {
147 std::vector<int32_t> newSpots;
148 // Add spots for fingers that are down.
149 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
150 uint32_t id = idBits.clearFirstMarkedBit();
151 newSpots.push_back(id);
152 }
153
154 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155 }
156
157 virtual void clearSpots() {
158 }
Arthur Hung7c645402019-01-25 17:45:42 +0800159
160 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161};
162
163
164// --- FakeInputReaderPolicy ---
165
166class FakeInputReaderPolicy : public InputReaderPolicyInterface {
167 InputReaderConfiguration mConfig;
168 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800169 std::vector<InputDeviceInfo> mInputDevices;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100170 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700171 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
173protected:
174 virtual ~FakeInputReaderPolicy() { }
175
176public:
177 FakeInputReaderPolicy() {
178 }
179
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700180 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100181 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100182 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700183 }
184
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700185 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
186 return mConfig.getDisplayViewportByUniqueId(uniqueId);
187 }
188 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
189 return mConfig.getDisplayViewportByType(type);
190 }
191
192 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
193 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700194 }
195
196 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700197 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
198 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700199 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700200 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700201 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100202 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 }
204
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100205 void addExcludedDeviceName(const std::string& deviceName) {
206 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 }
208
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700209 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
210 mConfig.portAssociations.insert({inputPort, displayPort});
211 }
212
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700213 void addDisabledDevice(int32_t deviceId) {
214 ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
215 bool currentlyEnabled = index < 0;
216 if (currentlyEnabled) {
217 mConfig.disabledDevices.add(deviceId);
218 }
219 }
220
221 void removeDisabledDevice(int32_t deviceId) {
222 ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
223 bool currentlyEnabled = index < 0;
224 if (!currentlyEnabled) {
225 mConfig.disabledDevices.remove(deviceId);
226 }
227 }
228
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
230 mPointerControllers.add(deviceId, controller);
231 }
232
233 const InputReaderConfiguration* getReaderConfiguration() const {
234 return &mConfig;
235 }
236
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800237 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 return mInputDevices;
239 }
240
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100241 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700242 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700243 return transform;
244 }
245
246 void setTouchAffineTransformation(const TouchAffineTransformation t) {
247 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800248 }
249
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800250 void setPointerCapture(bool enabled) {
251 mConfig.pointerCapture = enabled;
252 }
253
Arthur Hung7c645402019-01-25 17:45:42 +0800254 void setShowTouches(bool enabled) {
255 mConfig.showTouches = enabled;
256 }
257
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700259 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700260 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
261 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700262 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
263 || orientation == DISPLAY_ORIENTATION_270);
264 DisplayViewport v;
265 v.displayId = displayId;
266 v.orientation = orientation;
267 v.logicalLeft = 0;
268 v.logicalTop = 0;
269 v.logicalRight = isRotated ? height : width;
270 v.logicalBottom = isRotated ? width : height;
271 v.physicalLeft = 0;
272 v.physicalTop = 0;
273 v.physicalRight = isRotated ? height : width;
274 v.physicalBottom = isRotated ? width : height;
275 v.deviceWidth = isRotated ? height : width;
276 v.deviceHeight = isRotated ? width : height;
277 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700278 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100279 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700280 return v;
281 }
282
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
284 *outConfig = mConfig;
285 }
286
287 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
288 return mPointerControllers.valueFor(deviceId);
289 }
290
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800291 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292 mInputDevices = inputDevices;
293 }
294
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100295 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700296 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297 }
298
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100299 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
300 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301 }
302};
303
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304// --- FakeEventHub ---
305
306class FakeEventHub : public EventHubInterface {
307 struct KeyInfo {
308 int32_t keyCode;
309 uint32_t flags;
310 };
311
312 struct Device {
313 InputDeviceIdentifier identifier;
314 uint32_t classes;
315 PropertyMap configuration;
316 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
317 KeyedVector<int, bool> relativeAxes;
318 KeyedVector<int32_t, int32_t> keyCodeStates;
319 KeyedVector<int32_t, int32_t> scanCodeStates;
320 KeyedVector<int32_t, int32_t> switchStates;
321 KeyedVector<int32_t, int32_t> absoluteAxisValue;
322 KeyedVector<int32_t, KeyInfo> keysByScanCode;
323 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
324 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800325 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700326 bool enabled;
327
328 status_t enable() {
329 enabled = true;
330 return OK;
331 }
332
333 status_t disable() {
334 enabled = false;
335 return OK;
336 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700338 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 }
341 };
342
343 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100344 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 List<RawEvent> mEvents;
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600346 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347
348protected:
349 virtual ~FakeEventHub() {
350 for (size_t i = 0; i < mDevices.size(); i++) {
351 delete mDevices.valueAt(i);
352 }
353 }
354
355public:
356 FakeEventHub() { }
357
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100358 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359 Device* device = new Device(classes);
360 device->identifier.name = name;
361 mDevices.add(deviceId, device);
362
363 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
364 }
365
366 void removeDevice(int32_t deviceId) {
367 delete mDevices.valueFor(deviceId);
368 mDevices.removeItem(deviceId);
369
370 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
371 }
372
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700373 bool isDeviceEnabled(int32_t deviceId) {
374 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700375 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700376 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
377 return false;
378 }
379 return device->enabled;
380 }
381
382 status_t enableDevice(int32_t deviceId) {
383 status_t result;
384 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700385 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700386 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
387 return BAD_VALUE;
388 }
389 if (device->enabled) {
390 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
391 return OK;
392 }
393 result = device->enable();
394 return result;
395 }
396
397 status_t disableDevice(int32_t deviceId) {
398 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700399 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700400 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
401 return BAD_VALUE;
402 }
403 if (!device->enabled) {
404 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
405 return OK;
406 }
407 return device->disable();
408 }
409
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 void finishDeviceScan() {
411 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
412 }
413
414 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
415 Device* device = getDevice(deviceId);
416 device->configuration.addProperty(key, value);
417 }
418
419 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
420 Device* device = getDevice(deviceId);
421 device->configuration.addAll(configuration);
422 }
423
424 void addAbsoluteAxis(int32_t deviceId, int axis,
425 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
426 Device* device = getDevice(deviceId);
427
428 RawAbsoluteAxisInfo info;
429 info.valid = true;
430 info.minValue = minValue;
431 info.maxValue = maxValue;
432 info.flat = flat;
433 info.fuzz = fuzz;
434 info.resolution = resolution;
435 device->absoluteAxes.add(axis, info);
436 }
437
438 void addRelativeAxis(int32_t deviceId, int32_t axis) {
439 Device* device = getDevice(deviceId);
440 device->relativeAxes.add(axis, true);
441 }
442
443 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
444 Device* device = getDevice(deviceId);
445 device->keyCodeStates.replaceValueFor(keyCode, state);
446 }
447
448 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
449 Device* device = getDevice(deviceId);
450 device->scanCodeStates.replaceValueFor(scanCode, state);
451 }
452
453 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
454 Device* device = getDevice(deviceId);
455 device->switchStates.replaceValueFor(switchCode, state);
456 }
457
458 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
459 Device* device = getDevice(deviceId);
460 device->absoluteAxisValue.replaceValueFor(axis, value);
461 }
462
463 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
464 int32_t keyCode, uint32_t flags) {
465 Device* device = getDevice(deviceId);
466 KeyInfo info;
467 info.keyCode = keyCode;
468 info.flags = flags;
469 if (scanCode) {
470 device->keysByScanCode.add(scanCode, info);
471 }
472 if (usageCode) {
473 device->keysByUsageCode.add(usageCode, info);
474 }
475 }
476
477 void addLed(int32_t deviceId, int32_t led, bool initialState) {
478 Device* device = getDevice(deviceId);
479 device->leds.add(led, initialState);
480 }
481
482 bool getLedState(int32_t deviceId, int32_t led) {
483 Device* device = getDevice(deviceId);
484 return device->leds.valueFor(led);
485 }
486
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100487 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 return mExcludedDevices;
489 }
490
491 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
492 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800493 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 }
495
496 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
497 int32_t code, int32_t value) {
498 RawEvent event;
499 event.when = when;
500 event.deviceId = deviceId;
501 event.type = type;
502 event.code = code;
503 event.value = value;
504 mEvents.push_back(event);
505
506 if (type == EV_ABS) {
507 setAbsoluteAxisValue(deviceId, code, value);
508 }
509 }
510
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600511 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
512 std::vector<TouchVideoFrame>> videoFrames) {
513 mVideoFrames = std::move(videoFrames);
514 }
515
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516 void assertQueueIsEmpty() {
517 ASSERT_EQ(size_t(0), mEvents.size())
518 << "Expected the event queue to be empty (fully consumed).";
519 }
520
521private:
522 Device* getDevice(int32_t deviceId) const {
523 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100524 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 }
526
527 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
528 Device* device = getDevice(deviceId);
529 return device ? device->classes : 0;
530 }
531
532 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
533 Device* device = getDevice(deviceId);
534 return device ? device->identifier : InputDeviceIdentifier();
535 }
536
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100537 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 return 0;
539 }
540
541 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
542 Device* device = getDevice(deviceId);
543 if (device) {
544 *outConfiguration = device->configuration;
545 }
546 }
547
548 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
549 RawAbsoluteAxisInfo* outAxisInfo) const {
550 Device* device = getDevice(deviceId);
551 if (device) {
552 ssize_t index = device->absoluteAxes.indexOfKey(axis);
553 if (index >= 0) {
554 *outAxisInfo = device->absoluteAxes.valueAt(index);
555 return OK;
556 }
557 }
558 outAxisInfo->clear();
559 return -1;
560 }
561
562 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
563 Device* device = getDevice(deviceId);
564 if (device) {
565 return device->relativeAxes.indexOfKey(axis) >= 0;
566 }
567 return false;
568 }
569
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100570 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 return false;
572 }
573
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700574 virtual status_t mapKey(int32_t deviceId,
575 int32_t scanCode, int32_t usageCode, int32_t metaState,
576 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 Device* device = getDevice(deviceId);
578 if (device) {
579 const KeyInfo* key = getKey(device, scanCode, usageCode);
580 if (key) {
581 if (outKeycode) {
582 *outKeycode = key->keyCode;
583 }
584 if (outFlags) {
585 *outFlags = key->flags;
586 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700587 if (outMetaState) {
588 *outMetaState = metaState;
589 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 return OK;
591 }
592 }
593 return NAME_NOT_FOUND;
594 }
595
596 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
597 if (usageCode) {
598 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
599 if (index >= 0) {
600 return &device->keysByUsageCode.valueAt(index);
601 }
602 }
603 if (scanCode) {
604 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
605 if (index >= 0) {
606 return &device->keysByScanCode.valueAt(index);
607 }
608 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700609 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 }
611
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100612 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 return NAME_NOT_FOUND;
614 }
615
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100616 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 mExcludedDevices = devices;
618 }
619
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100620 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 if (mEvents.empty()) {
622 return 0;
623 }
624
625 *buffer = *mEvents.begin();
626 mEvents.erase(mEvents.begin());
627 return 1;
628 }
629
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800630 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600631 auto it = mVideoFrames.find(deviceId);
632 if (it != mVideoFrames.end()) {
633 std::vector<TouchVideoFrame> frames = std::move(it->second);
634 mVideoFrames.erase(deviceId);
635 return frames;
636 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800637 return {};
638 }
639
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
641 Device* device = getDevice(deviceId);
642 if (device) {
643 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
644 if (index >= 0) {
645 return device->scanCodeStates.valueAt(index);
646 }
647 }
648 return AKEY_STATE_UNKNOWN;
649 }
650
651 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
652 Device* device = getDevice(deviceId);
653 if (device) {
654 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
655 if (index >= 0) {
656 return device->keyCodeStates.valueAt(index);
657 }
658 }
659 return AKEY_STATE_UNKNOWN;
660 }
661
662 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
663 Device* device = getDevice(deviceId);
664 if (device) {
665 ssize_t index = device->switchStates.indexOfKey(sw);
666 if (index >= 0) {
667 return device->switchStates.valueAt(index);
668 }
669 }
670 return AKEY_STATE_UNKNOWN;
671 }
672
673 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
674 int32_t* outValue) const {
675 Device* device = getDevice(deviceId);
676 if (device) {
677 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
678 if (index >= 0) {
679 *outValue = device->absoluteAxisValue.valueAt(index);
680 return OK;
681 }
682 }
683 *outValue = 0;
684 return -1;
685 }
686
687 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
688 uint8_t* outFlags) const {
689 bool result = false;
690 Device* device = getDevice(deviceId);
691 if (device) {
692 for (size_t i = 0; i < numCodes; i++) {
693 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
694 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
695 outFlags[i] = 1;
696 result = true;
697 }
698 }
699 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
700 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
701 outFlags[i] = 1;
702 result = true;
703 }
704 }
705 }
706 }
707 return result;
708 }
709
710 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
711 Device* device = getDevice(deviceId);
712 if (device) {
713 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
714 return index >= 0;
715 }
716 return false;
717 }
718
719 virtual bool hasLed(int32_t deviceId, int32_t led) const {
720 Device* device = getDevice(deviceId);
721 return device && device->leds.indexOfKey(led) >= 0;
722 }
723
724 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
725 Device* device = getDevice(deviceId);
726 if (device) {
727 ssize_t index = device->leds.indexOfKey(led);
728 if (index >= 0) {
729 device->leds.replaceValueAt(led, on);
730 } else {
731 ADD_FAILURE()
732 << "Attempted to set the state of an LED that the EventHub declared "
733 "was not present. led=" << led;
734 }
735 }
736 }
737
738 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800739 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 outVirtualKeys.clear();
741
742 Device* device = getDevice(deviceId);
743 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800744 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 }
746 }
747
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100748 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700749 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 }
751
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100752 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 return false;
754 }
755
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100756 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757 }
758
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100759 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760 }
761
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100762 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 return false;
764 }
765
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800766 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800767 }
768
769 virtual void monitor() {
770 }
771
772 virtual void requestReopenDevices() {
773 }
774
775 virtual void wake() {
776 }
777};
778
779
780// --- FakeInputReaderContext ---
781
782class FakeInputReaderContext : public InputReaderContext {
783 sp<EventHubInterface> mEventHub;
784 sp<InputReaderPolicyInterface> mPolicy;
785 sp<InputListenerInterface> mListener;
786 int32_t mGlobalMetaState;
787 bool mUpdateGlobalMetaStateWasCalled;
788 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800789 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790
791public:
792 FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
793 const sp<InputReaderPolicyInterface>& policy,
794 const sp<InputListenerInterface>& listener) :
795 mEventHub(eventHub), mPolicy(policy), mListener(listener),
Prabir Pradhan42611e02018-11-27 14:04:02 -0800796 mGlobalMetaState(0), mNextSequenceNum(1) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797 }
798
799 virtual ~FakeInputReaderContext() { }
800
801 void assertUpdateGlobalMetaStateWasCalled() {
802 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
803 << "Expected updateGlobalMetaState() to have been called.";
804 mUpdateGlobalMetaStateWasCalled = false;
805 }
806
807 void setGlobalMetaState(int32_t state) {
808 mGlobalMetaState = state;
809 }
810
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800811 uint32_t getGeneration() {
812 return mGeneration;
813 }
814
Michael Wrightd02c5b62014-02-10 15:10:22 -0800815private:
816 virtual void updateGlobalMetaState() {
817 mUpdateGlobalMetaStateWasCalled = true;
818 }
819
820 virtual int32_t getGlobalMetaState() {
821 return mGlobalMetaState;
822 }
823
824 virtual EventHubInterface* getEventHub() {
825 return mEventHub.get();
826 }
827
828 virtual InputReaderPolicyInterface* getPolicy() {
829 return mPolicy.get();
830 }
831
832 virtual InputListenerInterface* getListener() {
833 return mListener.get();
834 }
835
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100836 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837 }
838
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100839 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840 return false;
841 }
842
843 virtual void fadePointer() {
844 }
845
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100846 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847 }
848
849 virtual int32_t bumpGeneration() {
850 return ++mGeneration;
851 }
Michael Wright842500e2015-03-13 17:32:02 -0700852
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800853 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700854
855 }
856
857 virtual void dispatchExternalStylusState(const StylusState&) {
858
859 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800860
861 virtual uint32_t getNextSequenceNum() {
862 return mNextSequenceNum++;
863 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864};
865
866
867// --- FakeInputMapper ---
868
869class FakeInputMapper : public InputMapper {
870 uint32_t mSources;
871 int32_t mKeyboardType;
872 int32_t mMetaState;
873 KeyedVector<int32_t, int32_t> mKeyCodeStates;
874 KeyedVector<int32_t, int32_t> mScanCodeStates;
875 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800876 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877 RawEvent mLastEvent;
878
879 bool mConfigureWasCalled;
880 bool mResetWasCalled;
881 bool mProcessWasCalled;
882
Arthur Hungc23540e2018-11-29 20:42:11 +0800883 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884public:
885 FakeInputMapper(InputDevice* device, uint32_t sources) :
886 InputMapper(device),
887 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
888 mMetaState(0),
889 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
890 }
891
892 virtual ~FakeInputMapper() { }
893
894 void setKeyboardType(int32_t keyboardType) {
895 mKeyboardType = keyboardType;
896 }
897
898 void setMetaState(int32_t metaState) {
899 mMetaState = metaState;
900 }
901
902 void assertConfigureWasCalled() {
903 ASSERT_TRUE(mConfigureWasCalled)
904 << "Expected configure() to have been called.";
905 mConfigureWasCalled = false;
906 }
907
908 void assertResetWasCalled() {
909 ASSERT_TRUE(mResetWasCalled)
910 << "Expected reset() to have been called.";
911 mResetWasCalled = false;
912 }
913
Yi Kong9b14ac62018-07-17 13:48:38 -0700914 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915 ASSERT_TRUE(mProcessWasCalled)
916 << "Expected process() to have been called.";
917 if (outLastEvent) {
918 *outLastEvent = mLastEvent;
919 }
920 mProcessWasCalled = false;
921 }
922
923 void setKeyCodeState(int32_t keyCode, int32_t state) {
924 mKeyCodeStates.replaceValueFor(keyCode, state);
925 }
926
927 void setScanCodeState(int32_t scanCode, int32_t state) {
928 mScanCodeStates.replaceValueFor(scanCode, state);
929 }
930
931 void setSwitchState(int32_t switchCode, int32_t state) {
932 mSwitchStates.replaceValueFor(switchCode, state);
933 }
934
935 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800936 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 }
938
939private:
940 virtual uint32_t getSources() {
941 return mSources;
942 }
943
944 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
945 InputMapper::populateDeviceInfo(deviceInfo);
946
947 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
948 deviceInfo->setKeyboardType(mKeyboardType);
949 }
950 }
951
Arthur Hungc23540e2018-11-29 20:42:11 +0800952 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800954
955 // Find the associated viewport if exist.
956 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
957 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
958 mViewport = config->getDisplayViewportByPort(*displayPort);
959 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 }
961
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100962 virtual void reset(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963 mResetWasCalled = true;
964 }
965
966 virtual void process(const RawEvent* rawEvent) {
967 mLastEvent = *rawEvent;
968 mProcessWasCalled = true;
969 }
970
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100971 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
973 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
974 }
975
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100976 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
978 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
979 }
980
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100981 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 ssize_t index = mSwitchStates.indexOfKey(switchCode);
983 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
984 }
985
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100986 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987 const int32_t* keyCodes, uint8_t* outFlags) {
988 bool result = false;
989 for (size_t i = 0; i < numCodes; i++) {
990 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
991 if (keyCodes[i] == mSupportedKeyCodes[j]) {
992 outFlags[i] = 1;
993 result = true;
994 }
995 }
996 }
997 return result;
998 }
999
1000 virtual int32_t getMetaState() {
1001 return mMetaState;
1002 }
1003
1004 virtual void fadePointer() {
1005 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001006
1007 virtual std::optional<int32_t> getAssociatedDisplay() {
1008 if (mViewport) {
1009 return std::make_optional(mViewport->displayId);
1010 }
1011 return std::nullopt;
1012 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013};
1014
1015
1016// --- InstrumentedInputReader ---
1017
1018class InstrumentedInputReader : public InputReader {
1019 InputDevice* mNextDevice;
1020
1021public:
1022 InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
1023 const sp<InputReaderPolicyInterface>& policy,
1024 const sp<InputListenerInterface>& listener) :
1025 InputReader(eventHub, policy, listener),
Yi Kong9b14ac62018-07-17 13:48:38 -07001026 mNextDevice(nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001027 }
1028
1029 virtual ~InstrumentedInputReader() {
1030 if (mNextDevice) {
1031 delete mNextDevice;
1032 }
1033 }
1034
1035 void setNextDevice(InputDevice* device) {
1036 mNextDevice = device;
1037 }
1038
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001039 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Arthur Hungc23540e2018-11-29 20:42:11 +08001040 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041 InputDeviceIdentifier identifier;
1042 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044 int32_t generation = deviceId + 1;
1045 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
1046 classes);
1047 }
1048
1049protected:
1050 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
1051 const InputDeviceIdentifier& identifier, uint32_t classes) {
1052 if (mNextDevice) {
1053 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001054 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055 return device;
1056 }
1057 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1058 }
1059
1060 friend class InputReaderTest;
1061};
1062
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001063// --- InputReaderPolicyTest ---
1064class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001065protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001066 sp<FakeInputReaderPolicy> mFakePolicy;
1067
1068 virtual void SetUp() {
1069 mFakePolicy = new FakeInputReaderPolicy();
1070 }
1071 virtual void TearDown() {
1072 mFakePolicy.clear();
1073 }
1074};
1075
1076/**
1077 * Check that empty set of viewports is an acceptable configuration.
1078 * Also try to get internal viewport two different ways - by type and by uniqueId.
1079 *
1080 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1081 * Such configuration is not currently allowed.
1082 */
1083TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001084 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001085
1086 // We didn't add any viewports yet, so there shouldn't be any.
1087 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001088 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001089 ASSERT_FALSE(internalViewport);
1090
1091 // Add an internal viewport, then clear it
1092 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001093 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001094
1095 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001096 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001097 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001098 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001099
1100 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001101 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001102 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001103 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001104
1105 mFakePolicy->clearViewports();
1106 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001107 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001108 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001109 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001110 ASSERT_FALSE(internalViewport);
1111}
1112
1113TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1114 const std::string internalUniqueId = "local:0";
1115 const std::string externalUniqueId = "local:1";
1116 const std::string virtualUniqueId1 = "virtual:2";
1117 const std::string virtualUniqueId2 = "virtual:3";
1118 constexpr int32_t virtualDisplayId1 = 2;
1119 constexpr int32_t virtualDisplayId2 = 3;
1120
1121 // Add an internal viewport
1122 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001123 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001124 // Add an external viewport
1125 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001126 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001127 // Add an virtual viewport
1128 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001129 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001130 // Add another virtual viewport
1131 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001132 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001133
1134 // Check matching by type for internal
1135 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001136 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001137 ASSERT_TRUE(internalViewport);
1138 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1139
1140 // Check matching by type for external
1141 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001142 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001143 ASSERT_TRUE(externalViewport);
1144 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1145
1146 // Check matching by uniqueId for virtual viewport #1
1147 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001148 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001149 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001150 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001151 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1152 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1153
1154 // Check matching by uniqueId for virtual viewport #2
1155 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001156 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001157 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001158 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001159 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1160 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1161}
1162
1163
1164/**
1165 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1166 * that lookup works by checking display id.
1167 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1168 */
1169TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1170 const std::string uniqueId1 = "uniqueId1";
1171 const std::string uniqueId2 = "uniqueId2";
1172 constexpr int32_t displayId1 = 2;
1173 constexpr int32_t displayId2 = 3;
1174
1175 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1176 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1177 for (const ViewportType& type : types) {
1178 mFakePolicy->clearViewports();
1179 // Add a viewport
1180 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001181 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001182 // Add another viewport
1183 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001184 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185
1186 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001187 std::optional<DisplayViewport> viewport1 =
1188 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001189 ASSERT_TRUE(viewport1);
1190 ASSERT_EQ(displayId1, viewport1->displayId);
1191 ASSERT_EQ(type, viewport1->type);
1192
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001193 std::optional<DisplayViewport> viewport2 =
1194 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001195 ASSERT_TRUE(viewport2);
1196 ASSERT_EQ(displayId2, viewport2->displayId);
1197 ASSERT_EQ(type, viewport2->type);
1198
1199 // When there are multiple viewports of the same kind, and uniqueId is not specified
1200 // in the call to getDisplayViewport, then that situation is not supported.
1201 // The viewports can be stored in any order, so we cannot rely on the order, since that
1202 // is just implementation detail.
1203 // However, we can check that it still returns *a* viewport, we just cannot assert
1204 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001205 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001206 ASSERT_TRUE(someViewport);
1207 }
1208}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001210/**
1211 * Check getDisplayViewportByPort
1212 */
1213TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1214 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1215 const std::string uniqueId1 = "uniqueId1";
1216 const std::string uniqueId2 = "uniqueId2";
1217 constexpr int32_t displayId1 = 1;
1218 constexpr int32_t displayId2 = 2;
1219 const uint8_t hdmi1 = 0;
1220 const uint8_t hdmi2 = 1;
1221 const uint8_t hdmi3 = 2;
1222
1223 mFakePolicy->clearViewports();
1224 // Add a viewport that's associated with some display port that's not of interest.
1225 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1226 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1227 // Add another viewport, connected to HDMI1 port
1228 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1229 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1230
1231 // Check that correct display viewport was returned by comparing the display ports.
1232 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1233 ASSERT_TRUE(hdmi1Viewport);
1234 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1235 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1236
1237 // Check that we can still get the same viewport using the uniqueId
1238 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1239 ASSERT_TRUE(hdmi1Viewport);
1240 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1241 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1242 ASSERT_EQ(type, hdmi1Viewport->type);
1243
1244 // Check that we cannot find a port with "HDMI2", because we never added one
1245 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1246 ASSERT_FALSE(hdmi2Viewport);
1247}
1248
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249// --- InputReaderTest ---
1250
1251class InputReaderTest : public testing::Test {
1252protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001253 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254 sp<FakeInputReaderPolicy> mFakePolicy;
1255 sp<FakeEventHub> mFakeEventHub;
1256 sp<InstrumentedInputReader> mReader;
1257
1258 virtual void SetUp() {
1259 mFakeEventHub = new FakeEventHub();
1260 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001261 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262
1263 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1264 }
1265
1266 virtual void TearDown() {
1267 mReader.clear();
1268
1269 mFakeListener.clear();
1270 mFakePolicy.clear();
1271 mFakeEventHub.clear();
1272 }
1273
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001274 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275 const PropertyMap* configuration) {
1276 mFakeEventHub->addDevice(deviceId, name, classes);
1277
1278 if (configuration) {
1279 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1280 }
1281 mFakeEventHub->finishDeviceScan();
1282 mReader->loopOnce();
1283 mReader->loopOnce();
1284 mFakeEventHub->assertQueueIsEmpty();
1285 }
1286
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001287 void disableDevice(int32_t deviceId, InputDevice* device) {
1288 mFakePolicy->addDisabledDevice(deviceId);
1289 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1290 }
1291
1292 void enableDevice(int32_t deviceId, InputDevice* device) {
1293 mFakePolicy->removeDisabledDevice(deviceId);
1294 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1295 }
1296
1297 void configureDevice(uint32_t changes, InputDevice* device) {
1298 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1299 }
1300
Michael Wrightd02c5b62014-02-10 15:10:22 -08001301 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001302 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001303 const PropertyMap* configuration) {
1304 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1305 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1306 device->addMapper(mapper);
1307 mReader->setNextDevice(device);
1308 addDevice(deviceId, name, classes, configuration);
1309 return mapper;
1310 }
1311};
1312
1313TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001314 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001315 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001316 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001317 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001319
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001320 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321 mReader->getInputDevices(inputDevices);
1322
1323 ASSERT_EQ(1U, inputDevices.size());
1324 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001325 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1327 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1328 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1329
1330 // Should also have received a notification describing the new input devices.
1331 inputDevices = mFakePolicy->getInputDevices();
1332 ASSERT_EQ(1U, inputDevices.size());
1333 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001334 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1336 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1337 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1338}
1339
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001340TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1341 constexpr int32_t deviceId = 1;
1342 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001343 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001344 // Must add at least one mapper or the device will be ignored!
1345 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1346 device->addMapper(mapper);
1347 mReader->setNextDevice(device);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001348 addDevice(deviceId, "fake", deviceClass, nullptr);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001349
Yi Kong9b14ac62018-07-17 13:48:38 -07001350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001351
1352 NotifyDeviceResetArgs resetArgs;
1353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1354 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1355 ASSERT_EQ(deviceId, resetArgs.deviceId);
1356
1357 ASSERT_EQ(device->isEnabled(), true);
1358 disableDevice(deviceId, device);
1359 mReader->loopOnce();
1360
1361 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1362 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1363 ASSERT_EQ(deviceId, resetArgs.deviceId);
1364 ASSERT_EQ(device->isEnabled(), false);
1365
1366 disableDevice(deviceId, device);
1367 mReader->loopOnce();
1368 mFakeListener->assertNotifyDeviceResetWasNotCalled();
1369 mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1370 ASSERT_EQ(device->isEnabled(), false);
1371
1372 enableDevice(deviceId, device);
1373 mReader->loopOnce();
1374 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1375 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1376 ASSERT_EQ(deviceId, resetArgs.deviceId);
1377 ASSERT_EQ(device->isEnabled(), true);
1378}
1379
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380TEST_F(InputReaderTest, GetKeyCodeState_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->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1385
1386 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1387 AINPUT_SOURCE_ANY, AKEYCODE_A))
1388 << "Should return unknown when the device id is >= 0 but unknown.";
1389
1390 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1391 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1392 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1393
1394 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1395 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1396 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1397
1398 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1399 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
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->getKeyCodeState(-1,
1403 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
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, GetScanCodeState_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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001411 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1412
1413 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1414 AINPUT_SOURCE_ANY, KEY_A))
1415 << "Should return unknown when the device id is >= 0 but unknown.";
1416
1417 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1418 AINPUT_SOURCE_TRACKBALL, KEY_A))
1419 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1420
1421 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1422 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1423 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1424
1425 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1426 AINPUT_SOURCE_TRACKBALL, KEY_A))
1427 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1428
1429 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1430 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1431 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1432}
1433
1434TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001435 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001436 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001437 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001438 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1439
1440 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1441 AINPUT_SOURCE_ANY, SW_LID))
1442 << "Should return unknown when the device id is >= 0 but unknown.";
1443
1444 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1445 AINPUT_SOURCE_TRACKBALL, SW_LID))
1446 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1447
1448 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1449 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1450 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1451
1452 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1453 AINPUT_SOURCE_TRACKBALL, SW_LID))
1454 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1455
1456 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1457 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1458 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1459}
1460
1461TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001462 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001463 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001464 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001465
Michael Wrightd02c5b62014-02-10 15:10:22 -08001466 mapper->addSupportedKeyCode(AKEYCODE_A);
1467 mapper->addSupportedKeyCode(AKEYCODE_B);
1468
1469 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1470 uint8_t flags[4] = { 0, 0, 0, 1 };
1471
1472 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1473 << "Should return false when device id is >= 0 but unknown.";
1474 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1475
1476 flags[3] = 1;
1477 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1478 << "Should return false when device id is valid but the sources are not supported by the device.";
1479 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1480
1481 flags[3] = 1;
1482 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1483 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1484 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1485
1486 flags[3] = 1;
1487 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1488 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1489 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1490
1491 flags[3] = 1;
1492 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1493 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1494 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1495}
1496
1497TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001498 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001499
1500 NotifyConfigurationChangedArgs args;
1501
1502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1503 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1504}
1505
1506TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001507 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001508 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001509 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001510
1511 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1512 mReader->loopOnce();
1513 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1514
1515 RawEvent event;
1516 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1517 ASSERT_EQ(0, event.when);
1518 ASSERT_EQ(1, event.deviceId);
1519 ASSERT_EQ(EV_KEY, event.type);
1520 ASSERT_EQ(KEY_A, event.code);
1521 ASSERT_EQ(1, event.value);
1522}
1523
Prabir Pradhan42611e02018-11-27 14:04:02 -08001524TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1525 constexpr int32_t deviceId = 1;
1526 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001527 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001528 // Must add at least one mapper or the device will be ignored!
1529 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1530 device->addMapper(mapper);
1531 mReader->setNextDevice(device);
1532 addDevice(deviceId, "fake", deviceClass, nullptr);
1533
1534 NotifyDeviceResetArgs resetArgs;
1535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1536 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1537
1538 disableDevice(deviceId, device);
1539 mReader->loopOnce();
1540 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1541 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1542 prevSequenceNum = resetArgs.sequenceNum;
1543
1544 enableDevice(deviceId, device);
1545 mReader->loopOnce();
1546 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1547 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1548 prevSequenceNum = resetArgs.sequenceNum;
1549
1550 disableDevice(deviceId, device);
1551 mReader->loopOnce();
1552 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1553 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1554 prevSequenceNum = resetArgs.sequenceNum;
1555}
1556
Arthur Hungc23540e2018-11-29 20:42:11 +08001557TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1558 constexpr int32_t deviceId = 1;
1559 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1560 const char* DEVICE_LOCATION = "USB1";
1561 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1562 DEVICE_LOCATION);
1563 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1564 device->addMapper(mapper);
1565 mReader->setNextDevice(device);
1566 addDevice(deviceId, "fake", deviceClass, nullptr);
1567
1568 const uint8_t hdmi1 = 1;
1569
1570 // Associated touch screen with second display.
1571 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1572
1573 // Add default and second display.
1574 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1575 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1576 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1577 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1578 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1579 mReader->loopOnce();
1580
1581 // Check device.
1582 ASSERT_EQ(deviceId, device->getId());
1583 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1584 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
1585}
1586
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587
1588// --- InputDeviceTest ---
1589
1590class InputDeviceTest : public testing::Test {
1591protected:
1592 static const char* DEVICE_NAME;
1593 static const int32_t DEVICE_ID;
1594 static const int32_t DEVICE_GENERATION;
1595 static const int32_t DEVICE_CONTROLLER_NUMBER;
1596 static const uint32_t DEVICE_CLASSES;
1597
1598 sp<FakeEventHub> mFakeEventHub;
1599 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001600 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 FakeInputReaderContext* mFakeContext;
1602
1603 InputDevice* mDevice;
1604
1605 virtual void SetUp() {
1606 mFakeEventHub = new FakeEventHub();
1607 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001608 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001609 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1610
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001611 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001612 InputDeviceIdentifier identifier;
1613 identifier.name = DEVICE_NAME;
1614 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1615 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1616 }
1617
1618 virtual void TearDown() {
1619 delete mDevice;
1620
1621 delete mFakeContext;
1622 mFakeListener.clear();
1623 mFakePolicy.clear();
1624 mFakeEventHub.clear();
1625 }
1626};
1627
1628const char* InputDeviceTest::DEVICE_NAME = "device";
1629const int32_t InputDeviceTest::DEVICE_ID = 1;
1630const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1631const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1632const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1633 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1634
1635TEST_F(InputDeviceTest, ImmutableProperties) {
1636 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001637 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001638 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1639}
1640
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001641TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1642 ASSERT_EQ(mDevice->isEnabled(), true);
1643}
1644
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1646 // Configuration.
1647 InputReaderConfiguration config;
1648 mDevice->configure(ARBITRARY_TIME, &config, 0);
1649
1650 // Reset.
1651 mDevice->reset(ARBITRARY_TIME);
1652
1653 NotifyDeviceResetArgs resetArgs;
1654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1655 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1656 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1657
1658 // Metadata.
1659 ASSERT_TRUE(mDevice->isIgnored());
1660 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1661
1662 InputDeviceInfo info;
1663 mDevice->getDeviceInfo(&info);
1664 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001665 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001666 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1667 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1668
1669 // State queries.
1670 ASSERT_EQ(0, mDevice->getMetaState());
1671
1672 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1673 << "Ignored device should return unknown key code state.";
1674 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1675 << "Ignored device should return unknown scan code state.";
1676 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1677 << "Ignored device should return unknown switch state.";
1678
1679 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1680 uint8_t flags[2] = { 0, 1 };
1681 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1682 << "Ignored device should never mark any key codes.";
1683 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1684 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1685}
1686
1687TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1688 // Configuration.
1689 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1690
1691 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1692 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1693 mapper1->setMetaState(AMETA_ALT_ON);
1694 mapper1->addSupportedKeyCode(AKEYCODE_A);
1695 mapper1->addSupportedKeyCode(AKEYCODE_B);
1696 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1697 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1698 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1699 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1700 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1701 mDevice->addMapper(mapper1);
1702
1703 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1704 mapper2->setMetaState(AMETA_SHIFT_ON);
1705 mDevice->addMapper(mapper2);
1706
1707 InputReaderConfiguration config;
1708 mDevice->configure(ARBITRARY_TIME, &config, 0);
1709
1710 String8 propertyValue;
1711 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1712 << "Device should have read configuration during configuration phase.";
1713 ASSERT_STREQ("value", propertyValue.string());
1714
1715 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1716 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1717
1718 // Reset
1719 mDevice->reset(ARBITRARY_TIME);
1720 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1721 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1722
1723 NotifyDeviceResetArgs resetArgs;
1724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1725 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1726 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1727
1728 // Metadata.
1729 ASSERT_FALSE(mDevice->isIgnored());
1730 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1731
1732 InputDeviceInfo info;
1733 mDevice->getDeviceInfo(&info);
1734 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001735 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001736 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1737 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1738
1739 // State queries.
1740 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1741 << "Should query mappers and combine meta states.";
1742
1743 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1744 << "Should return unknown key code state when source not supported.";
1745 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1746 << "Should return unknown scan code state when source not supported.";
1747 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1748 << "Should return unknown switch state when source not supported.";
1749
1750 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1751 << "Should query mapper when source is supported.";
1752 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1753 << "Should query mapper when source is supported.";
1754 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1755 << "Should query mapper when source is supported.";
1756
1757 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1758 uint8_t flags[4] = { 0, 0, 0, 1 };
1759 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1760 << "Should do nothing when source is unsupported.";
1761 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1762 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1763 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1764 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1765
1766 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1767 << "Should query mapper when source is supported.";
1768 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1769 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1770 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1771 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1772
1773 // Event handling.
1774 RawEvent event;
1775 mDevice->process(&event, 1);
1776
1777 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1778 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1779}
1780
1781
1782// --- InputMapperTest ---
1783
1784class InputMapperTest : public testing::Test {
1785protected:
1786 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001787 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001788 static const int32_t DEVICE_ID;
1789 static const int32_t DEVICE_GENERATION;
1790 static const int32_t DEVICE_CONTROLLER_NUMBER;
1791 static const uint32_t DEVICE_CLASSES;
1792
1793 sp<FakeEventHub> mFakeEventHub;
1794 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001795 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001796 FakeInputReaderContext* mFakeContext;
1797 InputDevice* mDevice;
1798
1799 virtual void SetUp() {
1800 mFakeEventHub = new FakeEventHub();
1801 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001802 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1804 InputDeviceIdentifier identifier;
1805 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001806 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001807 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1808 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1809
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001810 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811 }
1812
1813 virtual void TearDown() {
1814 delete mDevice;
1815 delete mFakeContext;
1816 mFakeListener.clear();
1817 mFakePolicy.clear();
1818 mFakeEventHub.clear();
1819 }
1820
1821 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001822 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001823 }
1824
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001825 void configureDevice(uint32_t changes) {
1826 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1827 }
1828
Michael Wrightd02c5b62014-02-10 15:10:22 -08001829 void addMapperAndConfigure(InputMapper* mapper) {
1830 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001831 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832 mDevice->reset(ARBITRARY_TIME);
1833 }
1834
1835 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001836 int32_t orientation, const std::string& uniqueId,
1837 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001838 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001839 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001840 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1841 }
1842
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001843 void clearViewports() {
1844 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001845 }
1846
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001847 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001848 int32_t code, int32_t value) {
1849 RawEvent event;
1850 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001851 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852 event.type = type;
1853 event.code = code;
1854 event.value = value;
1855 mapper->process(&event);
1856 }
1857
1858 static void assertMotionRange(const InputDeviceInfo& info,
1859 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1860 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001861 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001862 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1863 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1864 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1865 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1866 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1867 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1868 }
1869
1870 static void assertPointerCoords(const PointerCoords& coords,
1871 float x, float y, float pressure, float size,
1872 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1873 float orientation, float distance) {
1874 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1875 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1876 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1877 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1878 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1879 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1880 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1881 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1882 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1883 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1884 }
1885
1886 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1887 float actualX, actualY;
1888 controller->getPosition(&actualX, &actualY);
1889 ASSERT_NEAR(x, actualX, 1);
1890 ASSERT_NEAR(y, actualY, 1);
1891 }
1892};
1893
1894const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001895const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896const int32_t InputMapperTest::DEVICE_ID = 1;
1897const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1898const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1899const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1900
1901
1902// --- SwitchInputMapperTest ---
1903
1904class SwitchInputMapperTest : public InputMapperTest {
1905protected:
1906};
1907
1908TEST_F(SwitchInputMapperTest, GetSources) {
1909 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1910 addMapperAndConfigure(mapper);
1911
1912 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1913}
1914
1915TEST_F(SwitchInputMapperTest, GetSwitchState) {
1916 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1917 addMapperAndConfigure(mapper);
1918
1919 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1920 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1921
1922 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1923 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1924}
1925
1926TEST_F(SwitchInputMapperTest, Process) {
1927 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1928 addMapperAndConfigure(mapper);
1929
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001930 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
1931 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1932 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
1933 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001934
1935 NotifySwitchArgs args;
1936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1937 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08001938 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1939 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001940 args.switchMask);
1941 ASSERT_EQ(uint32_t(0), args.policyFlags);
1942}
1943
1944
1945// --- KeyboardInputMapperTest ---
1946
1947class KeyboardInputMapperTest : public InputMapperTest {
1948protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001949 const std::string UNIQUE_ID = "local:0";
1950
1951 void prepareDisplay(int32_t orientation);
1952
Michael Wrightd02c5b62014-02-10 15:10:22 -08001953 void testDPadKeyRotation(KeyboardInputMapper* mapper,
1954 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1955};
1956
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001957/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
1958 * orientation.
1959 */
1960void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
1961 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001962 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001963}
1964
Michael Wrightd02c5b62014-02-10 15:10:22 -08001965void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001966 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001967 NotifyKeyArgs args;
1968
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001969 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1971 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1972 ASSERT_EQ(originalScanCode, args.scanCode);
1973 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1974
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001975 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1977 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1978 ASSERT_EQ(originalScanCode, args.scanCode);
1979 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1980}
1981
1982
1983TEST_F(KeyboardInputMapperTest, GetSources) {
1984 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1985 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1986 addMapperAndConfigure(mapper);
1987
1988 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1989}
1990
1991TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
1992 const int32_t USAGE_A = 0x070004;
1993 const int32_t USAGE_UNKNOWN = 0x07ffff;
1994 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
1995 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
1996
1997 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1998 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1999 addMapperAndConfigure(mapper);
2000
2001 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002002 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002003 NotifyKeyArgs args;
2004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2005 ASSERT_EQ(DEVICE_ID, args.deviceId);
2006 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2007 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2008 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2009 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2010 ASSERT_EQ(KEY_HOME, args.scanCode);
2011 ASSERT_EQ(AMETA_NONE, args.metaState);
2012 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2013 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2014 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2015
2016 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002017 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2019 ASSERT_EQ(DEVICE_ID, args.deviceId);
2020 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2021 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2022 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2023 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2024 ASSERT_EQ(KEY_HOME, args.scanCode);
2025 ASSERT_EQ(AMETA_NONE, args.metaState);
2026 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2027 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2028 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2029
2030 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002031 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2032 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2034 ASSERT_EQ(DEVICE_ID, args.deviceId);
2035 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2036 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2037 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2038 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2039 ASSERT_EQ(0, args.scanCode);
2040 ASSERT_EQ(AMETA_NONE, args.metaState);
2041 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2042 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2043 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2044
2045 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002046 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2047 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2049 ASSERT_EQ(DEVICE_ID, args.deviceId);
2050 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2051 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2052 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2053 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2054 ASSERT_EQ(0, args.scanCode);
2055 ASSERT_EQ(AMETA_NONE, args.metaState);
2056 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2057 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2058 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2059
2060 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002061 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2062 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2064 ASSERT_EQ(DEVICE_ID, args.deviceId);
2065 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2066 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2067 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2068 ASSERT_EQ(0, args.keyCode);
2069 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2070 ASSERT_EQ(AMETA_NONE, args.metaState);
2071 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2072 ASSERT_EQ(0U, args.policyFlags);
2073 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2074
2075 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002076 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2077 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2079 ASSERT_EQ(DEVICE_ID, args.deviceId);
2080 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2081 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2082 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2083 ASSERT_EQ(0, args.keyCode);
2084 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2085 ASSERT_EQ(AMETA_NONE, args.metaState);
2086 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2087 ASSERT_EQ(0U, args.policyFlags);
2088 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2089}
2090
2091TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2092 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2093 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2094
2095 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2096 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2097 addMapperAndConfigure(mapper);
2098
2099 // Initial metastate.
2100 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2101
2102 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002103 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002104 NotifyKeyArgs args;
2105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2106 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2107 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2108 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2109
2110 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002111 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2113 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2115
2116 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002117 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2119 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2120 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2121
2122 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002123 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2125 ASSERT_EQ(AMETA_NONE, args.metaState);
2126 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2127 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2128}
2129
2130TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2131 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2132 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2133 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2134 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2135
2136 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2137 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2138 addMapperAndConfigure(mapper);
2139
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002140 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002141 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2142 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2143 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2144 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2145 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2146 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2147 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2148 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2149}
2150
2151TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2152 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2153 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2154 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2155 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2156
2157 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2158 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2159 addConfigurationProperty("keyboard.orientationAware", "1");
2160 addMapperAndConfigure(mapper);
2161
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002162 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002163 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2164 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2165 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2166 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2167 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2168 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2169 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2170 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2171
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002172 clearViewports();
2173 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002174 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2175 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
2176 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2177 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
2178 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2179 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
2180 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2181 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
2182
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002183 clearViewports();
2184 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002185 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2186 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
2187 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2188 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
2189 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2190 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
2191 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2192 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
2193
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002194 clearViewports();
2195 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002196 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2197 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
2198 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2199 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
2200 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2201 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
2202 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2203 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
2204
2205 // Special case: if orientation changes while key is down, we still emit the same keycode
2206 // in the key up as we did in the key down.
2207 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002208 clearViewports();
2209 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002210 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2212 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2213 ASSERT_EQ(KEY_UP, args.scanCode);
2214 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2215
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002216 clearViewports();
2217 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002218 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2220 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2221 ASSERT_EQ(KEY_UP, args.scanCode);
2222 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2223}
2224
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002225TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2226 // If the keyboard is not orientation aware,
2227 // key events should not be associated with a specific display id
2228 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2229
2230 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2231 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2232 addMapperAndConfigure(mapper);
2233 NotifyKeyArgs args;
2234
2235 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002236 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002238 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2240 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2241
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002242 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002243 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002245 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2247 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2248}
2249
2250TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2251 // If the keyboard is orientation aware,
2252 // key events should be associated with the internal viewport
2253 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2254
2255 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2256 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2257 addConfigurationProperty("keyboard.orientationAware", "1");
2258 addMapperAndConfigure(mapper);
2259 NotifyKeyArgs args;
2260
2261 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2262 // ^--- already checked by the previous test
2263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002264 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002265 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002266 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002268 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2270 ASSERT_EQ(DISPLAY_ID, args.displayId);
2271
2272 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002273 clearViewports();
2274 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002275 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002276 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002278 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2280 ASSERT_EQ(newDisplayId, args.displayId);
2281}
2282
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2284 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2285 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2286 addMapperAndConfigure(mapper);
2287
2288 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2289 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2290
2291 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2292 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2293}
2294
2295TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2296 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2297 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2298 addMapperAndConfigure(mapper);
2299
2300 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2301 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2302
2303 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2304 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2305}
2306
2307TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2308 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2309 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2310 addMapperAndConfigure(mapper);
2311
2312 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2313
2314 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2315 uint8_t flags[2] = { 0, 0 };
2316 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2317 ASSERT_TRUE(flags[0]);
2318 ASSERT_FALSE(flags[1]);
2319}
2320
2321TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2322 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2323 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2324 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2325 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2326 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2327 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2328
2329 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2330 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2331 addMapperAndConfigure(mapper);
2332
2333 // Initialization should have turned all of the lights off.
2334 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2335 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2336 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2337
2338 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002339 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2340 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2342 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2343 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2344 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2345
2346 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002347 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2348 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002349 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2350 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2351 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2352 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2353
2354 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002355 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2356 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002357 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2358 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2359 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2360 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2361
2362 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002363 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2364 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2366 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2367 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2368 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2369
2370 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002371 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2372 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002373 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2374 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2375 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2376 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2377
2378 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002379 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2380 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2382 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2383 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2384 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2385}
2386
2387
2388// --- CursorInputMapperTest ---
2389
2390class CursorInputMapperTest : public InputMapperTest {
2391protected:
2392 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2393
2394 sp<FakePointerController> mFakePointerController;
2395
2396 virtual void SetUp() {
2397 InputMapperTest::SetUp();
2398
2399 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002400 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 }
2402
2403 void testMotionRotation(CursorInputMapper* mapper,
2404 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002405
2406 void prepareDisplay(int32_t orientation) {
2407 const std::string uniqueId = "local:0";
2408 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2409 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2410 orientation, uniqueId, NO_PORT, viewportType);
2411 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412};
2413
2414const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2415
2416void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2417 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2418 NotifyMotionArgs args;
2419
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002420 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2421 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2422 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2426 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2427 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2428 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2429}
2430
2431TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2432 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2433 addConfigurationProperty("cursor.mode", "pointer");
2434 addMapperAndConfigure(mapper);
2435
2436 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2437}
2438
2439TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2440 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2441 addConfigurationProperty("cursor.mode", "navigation");
2442 addMapperAndConfigure(mapper);
2443
2444 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2445}
2446
2447TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2448 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2449 addConfigurationProperty("cursor.mode", "pointer");
2450 addMapperAndConfigure(mapper);
2451
2452 InputDeviceInfo info;
2453 mapper->populateDeviceInfo(&info);
2454
2455 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002456 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2457 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2459 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2460
2461 // When the bounds are set, then there should be a valid motion range.
2462 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2463
2464 InputDeviceInfo info2;
2465 mapper->populateDeviceInfo(&info2);
2466
2467 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2468 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2469 1, 800 - 1, 0.0f, 0.0f));
2470 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2471 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2472 2, 480 - 1, 0.0f, 0.0f));
2473 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2474 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2475 0.0f, 1.0f, 0.0f, 0.0f));
2476}
2477
2478TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2479 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2480 addConfigurationProperty("cursor.mode", "navigation");
2481 addMapperAndConfigure(mapper);
2482
2483 InputDeviceInfo info;
2484 mapper->populateDeviceInfo(&info);
2485
2486 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2487 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2488 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2489 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2490 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2491 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2492 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2493 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2494 0.0f, 1.0f, 0.0f, 0.0f));
2495}
2496
2497TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2498 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2499 addConfigurationProperty("cursor.mode", "navigation");
2500 addMapperAndConfigure(mapper);
2501
2502 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2503
2504 NotifyMotionArgs args;
2505
2506 // Button press.
2507 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002508 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2509 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2511 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2512 ASSERT_EQ(DEVICE_ID, args.deviceId);
2513 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2514 ASSERT_EQ(uint32_t(0), args.policyFlags);
2515 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2516 ASSERT_EQ(0, args.flags);
2517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2518 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2519 ASSERT_EQ(0, args.edgeFlags);
2520 ASSERT_EQ(uint32_t(1), args.pointerCount);
2521 ASSERT_EQ(0, args.pointerProperties[0].id);
2522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2524 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2525 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2526 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2527 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2528
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2530 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2531 ASSERT_EQ(DEVICE_ID, args.deviceId);
2532 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2533 ASSERT_EQ(uint32_t(0), args.policyFlags);
2534 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2535 ASSERT_EQ(0, args.flags);
2536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2537 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2538 ASSERT_EQ(0, args.edgeFlags);
2539 ASSERT_EQ(uint32_t(1), args.pointerCount);
2540 ASSERT_EQ(0, args.pointerProperties[0].id);
2541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
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 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2545 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2546 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2547
Michael Wrightd02c5b62014-02-10 15:10:22 -08002548 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002549 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2550 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2552 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2553 ASSERT_EQ(DEVICE_ID, args.deviceId);
2554 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2555 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2557 ASSERT_EQ(0, args.flags);
2558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2559 ASSERT_EQ(0, args.buttonState);
2560 ASSERT_EQ(0, args.edgeFlags);
2561 ASSERT_EQ(uint32_t(1), args.pointerCount);
2562 ASSERT_EQ(0, args.pointerProperties[0].id);
2563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2565 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2566 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2567 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2568 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2569
2570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2571 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2572 ASSERT_EQ(DEVICE_ID, args.deviceId);
2573 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2574 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2576 ASSERT_EQ(0, args.flags);
2577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2578 ASSERT_EQ(0, args.buttonState);
2579 ASSERT_EQ(0, args.edgeFlags);
2580 ASSERT_EQ(uint32_t(1), args.pointerCount);
2581 ASSERT_EQ(0, args.pointerProperties[0].id);
2582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2584 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2585 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2586 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2587 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2588}
2589
2590TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2591 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2592 addConfigurationProperty("cursor.mode", "navigation");
2593 addMapperAndConfigure(mapper);
2594
2595 NotifyMotionArgs args;
2596
2597 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002598 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2599 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2603 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2604
2605 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002606 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2607 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2611 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2612}
2613
2614TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2615 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2616 addConfigurationProperty("cursor.mode", "navigation");
2617 addMapperAndConfigure(mapper);
2618
2619 NotifyMotionArgs args;
2620
2621 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002622 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2623 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2625 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2627 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2628
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2632 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2633
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002635 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2636 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2640 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2641
2642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2645 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2646}
2647
2648TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2649 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2650 addConfigurationProperty("cursor.mode", "navigation");
2651 addMapperAndConfigure(mapper);
2652
2653 NotifyMotionArgs args;
2654
2655 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002656 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2657 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2658 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2659 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2661 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2663 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2664 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2665
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2669 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2670 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2671
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002673 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2674 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2675 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2679 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2680 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2681
2682 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002683 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2684 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002686 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2688 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2689
2690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2693 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2694}
2695
2696TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2697 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2698 addConfigurationProperty("cursor.mode", "navigation");
2699 addMapperAndConfigure(mapper);
2700
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002701 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2703 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2704 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2705 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2706 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2707 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2708 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2709 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2710}
2711
2712TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2713 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2714 addConfigurationProperty("cursor.mode", "navigation");
2715 addConfigurationProperty("cursor.orientationAware", "1");
2716 addMapperAndConfigure(mapper);
2717
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002718 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2720 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2721 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2722 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2723 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2724 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2725 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2726 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2727
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002728 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2730 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2731 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2732 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2733 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2734 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2735 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2736 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2737
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002738 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2740 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2741 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2742 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2743 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2744 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2745 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2746 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2747
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002748 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2750 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2751 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2752 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2753 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2754 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2755 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2756 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2757}
2758
2759TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2760 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2761 addConfigurationProperty("cursor.mode", "pointer");
2762 addMapperAndConfigure(mapper);
2763
2764 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2765 mFakePointerController->setPosition(100, 200);
2766 mFakePointerController->setButtonState(0);
2767
2768 NotifyMotionArgs motionArgs;
2769 NotifyKeyArgs keyArgs;
2770
2771 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002772 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2773 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2776 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2777 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2779 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2780
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2782 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2783 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2784 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2786 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2787
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002788 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2789 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002791 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792 ASSERT_EQ(0, motionArgs.buttonState);
2793 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2795 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2796
2797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002798 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 ASSERT_EQ(0, motionArgs.buttonState);
2800 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2802 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2803
2804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002806 ASSERT_EQ(0, motionArgs.buttonState);
2807 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2809 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2810
2811 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002812 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2813 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2814 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2816 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2817 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2818 motionArgs.buttonState);
2819 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2820 mFakePointerController->getButtonState());
2821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2822 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2823
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2825 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2826 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2827 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2828 mFakePointerController->getButtonState());
2829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2830 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2831
2832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2834 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2835 motionArgs.buttonState);
2836 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2837 mFakePointerController->getButtonState());
2838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2839 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2840
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002841 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
2842 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002844 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2846 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2848 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2849
2850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002852 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2853 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2855 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2856
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002857 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2858 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2861 ASSERT_EQ(0, motionArgs.buttonState);
2862 ASSERT_EQ(0, mFakePointerController->getButtonState());
2863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2864 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 -08002865 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2866 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002867
2868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869 ASSERT_EQ(0, motionArgs.buttonState);
2870 ASSERT_EQ(0, mFakePointerController->getButtonState());
2871 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2873 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 -08002874
Michael Wrightd02c5b62014-02-10 15:10:22 -08002875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2876 ASSERT_EQ(0, motionArgs.buttonState);
2877 ASSERT_EQ(0, mFakePointerController->getButtonState());
2878 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2880 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2881
2882 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002883 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
2884 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2886 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2887 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002888
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002890 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2892 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2894 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2895
2896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2898 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2899 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2901 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2902
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002903 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
2904 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907 ASSERT_EQ(0, motionArgs.buttonState);
2908 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2910 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2911
2912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002914 ASSERT_EQ(0, motionArgs.buttonState);
2915 ASSERT_EQ(0, mFakePointerController->getButtonState());
2916
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2918 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2920 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2921 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2922
2923 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002924 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
2925 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2927 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2928 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002929
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2933 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2935 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2936
2937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2938 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2939 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2940 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2942 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2943
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002944 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
2945 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 ASSERT_EQ(0, motionArgs.buttonState);
2949 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2951 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 -08002952
2953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2954 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2955 ASSERT_EQ(0, motionArgs.buttonState);
2956 ASSERT_EQ(0, mFakePointerController->getButtonState());
2957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2958 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2959
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2961 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2962 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2963
2964 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002965 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
2966 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2968 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2969 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002970
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002972 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2974 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2976 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2977
2978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2980 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2981 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2983 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2984
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002985 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
2986 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 ASSERT_EQ(0, motionArgs.buttonState);
2990 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2992 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 -08002993
2994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2995 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2996 ASSERT_EQ(0, motionArgs.buttonState);
2997 ASSERT_EQ(0, mFakePointerController->getButtonState());
2998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2999 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3000
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3002 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3003 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3004
3005 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003006 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3007 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3009 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3010 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003011
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003013 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3015 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3017 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3018
3019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3021 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3022 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3024 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3025
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003026 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3027 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003030 ASSERT_EQ(0, motionArgs.buttonState);
3031 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3033 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 -08003034
3035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3037 ASSERT_EQ(0, motionArgs.buttonState);
3038 ASSERT_EQ(0, mFakePointerController->getButtonState());
3039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3040 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3041
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3043 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3044 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3045}
3046
3047TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3048 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3049 addConfigurationProperty("cursor.mode", "pointer");
3050 addMapperAndConfigure(mapper);
3051
3052 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3053 mFakePointerController->setPosition(100, 200);
3054 mFakePointerController->setButtonState(0);
3055
3056 NotifyMotionArgs args;
3057
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003058 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3059 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3060 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003062 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3063 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3065 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3066 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3067}
3068
3069TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3070 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3071 addConfigurationProperty("cursor.mode", "pointer");
3072 mFakePolicy->setPointerCapture(true);
3073 addMapperAndConfigure(mapper);
3074
3075 NotifyDeviceResetArgs resetArgs;
3076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3077 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3078 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3079
3080 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3081 mFakePointerController->setPosition(100, 200);
3082 mFakePointerController->setButtonState(0);
3083
3084 NotifyMotionArgs args;
3085
3086 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003087 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3088 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3089 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3091 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3094 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3095 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3096
3097 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003098 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3099 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3101 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3102 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3104 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3106 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3109 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3110
3111 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003112 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3113 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3115 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3118 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3120 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3121 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3123 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3124
3125 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003126 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3127 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3128 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3130 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3133 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3134 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3135
3136 // Disable pointer capture and check that the device generation got bumped
3137 // and events are generated the usual way.
3138 const uint32_t generation = mFakeContext->getGeneration();
3139 mFakePolicy->setPointerCapture(false);
3140 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3141 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3142
3143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3144 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3145 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3146
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003147 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3148 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3149 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3151 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3154 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3155 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3156}
3157
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003158TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3159 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3160 addMapperAndConfigure(mapper);
3161
3162 // Setup PointerController for second display.
3163 constexpr int32_t SECOND_DISPLAY_ID = 1;
3164 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3165 mFakePointerController->setPosition(100, 200);
3166 mFakePointerController->setButtonState(0);
3167 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3168
3169 NotifyMotionArgs args;
3170 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3171 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3172 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3174 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3175 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3177 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3178 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3179 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3180}
3181
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182
3183// --- TouchInputMapperTest ---
3184
3185class TouchInputMapperTest : public InputMapperTest {
3186protected:
3187 static const int32_t RAW_X_MIN;
3188 static const int32_t RAW_X_MAX;
3189 static const int32_t RAW_Y_MIN;
3190 static const int32_t RAW_Y_MAX;
3191 static const int32_t RAW_TOUCH_MIN;
3192 static const int32_t RAW_TOUCH_MAX;
3193 static const int32_t RAW_TOOL_MIN;
3194 static const int32_t RAW_TOOL_MAX;
3195 static const int32_t RAW_PRESSURE_MIN;
3196 static const int32_t RAW_PRESSURE_MAX;
3197 static const int32_t RAW_ORIENTATION_MIN;
3198 static const int32_t RAW_ORIENTATION_MAX;
3199 static const int32_t RAW_DISTANCE_MIN;
3200 static const int32_t RAW_DISTANCE_MAX;
3201 static const int32_t RAW_TILT_MIN;
3202 static const int32_t RAW_TILT_MAX;
3203 static const int32_t RAW_ID_MIN;
3204 static const int32_t RAW_ID_MAX;
3205 static const int32_t RAW_SLOT_MIN;
3206 static const int32_t RAW_SLOT_MAX;
3207 static const float X_PRECISION;
3208 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003209 static const float X_PRECISION_VIRTUAL;
3210 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003211
3212 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003213 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214
3215 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3216
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003217 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003218 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003219
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220 enum Axes {
3221 POSITION = 1 << 0,
3222 TOUCH = 1 << 1,
3223 TOOL = 1 << 2,
3224 PRESSURE = 1 << 3,
3225 ORIENTATION = 1 << 4,
3226 MINOR = 1 << 5,
3227 ID = 1 << 6,
3228 DISTANCE = 1 << 7,
3229 TILT = 1 << 8,
3230 SLOT = 1 << 9,
3231 TOOL_TYPE = 1 << 10,
3232 };
3233
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003234 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3235 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003236 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003238 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239 int32_t toRawX(float displayX);
3240 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003241 float toCookedX(float rawX, float rawY);
3242 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003244 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003246 float toDisplayY(int32_t rawY, int32_t displayHeight);
3247
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248};
3249
3250const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3251const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3252const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3253const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3254const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3255const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3256const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3257const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003258const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3259const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3261const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3262const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3263const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3264const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3265const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3266const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3267const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3268const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3269const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3270const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3271const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003272const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3273 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3274const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3275 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003276const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3277 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
3279const float TouchInputMapperTest::GEOMETRIC_SCALE =
3280 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3281 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3282
3283const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3284 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3285 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3286};
3287
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003288void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003289 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003290 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3291}
3292
3293void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3294 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3295 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296}
3297
Santos Cordonfa5cf462017-04-05 10:37:00 -07003298void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003299 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3300 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003301 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003302}
3303
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304void TouchInputMapperTest::prepareVirtualKeys() {
3305 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3306 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3307 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3308 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3309}
3310
Jason Gerecke489fda82012-09-07 17:19:40 -07003311void TouchInputMapperTest::prepareLocationCalibration() {
3312 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3313}
3314
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315int32_t TouchInputMapperTest::toRawX(float displayX) {
3316 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3317}
3318
3319int32_t TouchInputMapperTest::toRawY(float displayY) {
3320 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3321}
3322
Jason Gerecke489fda82012-09-07 17:19:40 -07003323float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3324 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3325 return rawX;
3326}
3327
3328float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3329 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3330 return rawY;
3331}
3332
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003334 return toDisplayX(rawX, DISPLAY_WIDTH);
3335}
3336
3337float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3338 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339}
3340
3341float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003342 return toDisplayY(rawY, DISPLAY_HEIGHT);
3343}
3344
3345float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3346 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347}
3348
3349
3350// --- SingleTouchInputMapperTest ---
3351
3352class SingleTouchInputMapperTest : public TouchInputMapperTest {
3353protected:
3354 void prepareButtons();
3355 void prepareAxes(int axes);
3356
3357 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3358 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3359 void processUp(SingleTouchInputMapper* mappery);
3360 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3361 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3362 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3363 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3364 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3365 void processSync(SingleTouchInputMapper* mapper);
3366};
3367
3368void SingleTouchInputMapperTest::prepareButtons() {
3369 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3370}
3371
3372void SingleTouchInputMapperTest::prepareAxes(int axes) {
3373 if (axes & POSITION) {
3374 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3375 RAW_X_MIN, RAW_X_MAX, 0, 0);
3376 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3377 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3378 }
3379 if (axes & PRESSURE) {
3380 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3381 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3382 }
3383 if (axes & TOOL) {
3384 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3385 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3386 }
3387 if (axes & DISTANCE) {
3388 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3389 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3390 }
3391 if (axes & TILT) {
3392 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3393 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3394 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3395 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3396 }
3397}
3398
3399void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003400 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3401 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3402 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403}
3404
3405void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003406 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3407 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408}
3409
3410void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003411 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412}
3413
3414void SingleTouchInputMapperTest::processPressure(
3415 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003416 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417}
3418
3419void SingleTouchInputMapperTest::processToolMajor(
3420 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003421 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422}
3423
3424void SingleTouchInputMapperTest::processDistance(
3425 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003426 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427}
3428
3429void SingleTouchInputMapperTest::processTilt(
3430 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003431 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3432 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433}
3434
3435void SingleTouchInputMapperTest::processKey(
3436 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003437 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438}
3439
3440void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003441 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442}
3443
3444
3445TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3446 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3447 prepareButtons();
3448 prepareAxes(POSITION);
3449 addMapperAndConfigure(mapper);
3450
3451 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3452}
3453
3454TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3455 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3456 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3457 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3458 prepareButtons();
3459 prepareAxes(POSITION);
3460 addMapperAndConfigure(mapper);
3461
3462 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3463}
3464
3465TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3466 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3467 prepareButtons();
3468 prepareAxes(POSITION);
3469 addConfigurationProperty("touch.deviceType", "touchPad");
3470 addMapperAndConfigure(mapper);
3471
3472 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3473}
3474
3475TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3476 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3477 prepareButtons();
3478 prepareAxes(POSITION);
3479 addConfigurationProperty("touch.deviceType", "touchScreen");
3480 addMapperAndConfigure(mapper);
3481
3482 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3483}
3484
3485TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3486 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3487 addConfigurationProperty("touch.deviceType", "touchScreen");
3488 prepareDisplay(DISPLAY_ORIENTATION_0);
3489 prepareButtons();
3490 prepareAxes(POSITION);
3491 prepareVirtualKeys();
3492 addMapperAndConfigure(mapper);
3493
3494 // Unknown key.
3495 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3496
3497 // Virtual key is down.
3498 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3499 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3500 processDown(mapper, x, y);
3501 processSync(mapper);
3502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3503
3504 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3505
3506 // Virtual key is up.
3507 processUp(mapper);
3508 processSync(mapper);
3509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3510
3511 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3512}
3513
3514TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3515 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3516 addConfigurationProperty("touch.deviceType", "touchScreen");
3517 prepareDisplay(DISPLAY_ORIENTATION_0);
3518 prepareButtons();
3519 prepareAxes(POSITION);
3520 prepareVirtualKeys();
3521 addMapperAndConfigure(mapper);
3522
3523 // Unknown key.
3524 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3525
3526 // Virtual key is down.
3527 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3528 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3529 processDown(mapper, x, y);
3530 processSync(mapper);
3531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3532
3533 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3534
3535 // Virtual key is up.
3536 processUp(mapper);
3537 processSync(mapper);
3538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3539
3540 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3541}
3542
3543TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3544 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3545 addConfigurationProperty("touch.deviceType", "touchScreen");
3546 prepareDisplay(DISPLAY_ORIENTATION_0);
3547 prepareButtons();
3548 prepareAxes(POSITION);
3549 prepareVirtualKeys();
3550 addMapperAndConfigure(mapper);
3551
3552 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3553 uint8_t flags[2] = { 0, 0 };
3554 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3555 ASSERT_TRUE(flags[0]);
3556 ASSERT_FALSE(flags[1]);
3557}
3558
3559TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3560 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3561 addConfigurationProperty("touch.deviceType", "touchScreen");
3562 prepareDisplay(DISPLAY_ORIENTATION_0);
3563 prepareButtons();
3564 prepareAxes(POSITION);
3565 prepareVirtualKeys();
3566 addMapperAndConfigure(mapper);
3567
3568 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3569
3570 NotifyKeyArgs args;
3571
3572 // Press virtual key.
3573 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3574 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3575 processDown(mapper, x, y);
3576 processSync(mapper);
3577
3578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3579 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3580 ASSERT_EQ(DEVICE_ID, args.deviceId);
3581 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3582 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3583 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3584 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3585 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3586 ASSERT_EQ(KEY_HOME, args.scanCode);
3587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3588 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3589
3590 // Release virtual key.
3591 processUp(mapper);
3592 processSync(mapper);
3593
3594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3595 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3596 ASSERT_EQ(DEVICE_ID, args.deviceId);
3597 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3598 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3599 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3600 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3601 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3602 ASSERT_EQ(KEY_HOME, args.scanCode);
3603 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3604 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3605
3606 // Should not have sent any motions.
3607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3608}
3609
3610TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3611 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3612 addConfigurationProperty("touch.deviceType", "touchScreen");
3613 prepareDisplay(DISPLAY_ORIENTATION_0);
3614 prepareButtons();
3615 prepareAxes(POSITION);
3616 prepareVirtualKeys();
3617 addMapperAndConfigure(mapper);
3618
3619 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3620
3621 NotifyKeyArgs keyArgs;
3622
3623 // Press virtual key.
3624 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3625 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3626 processDown(mapper, x, y);
3627 processSync(mapper);
3628
3629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3630 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3631 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3632 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3633 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3634 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3635 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3636 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3637 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3638 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3639 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3640
3641 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3642 // into the display area.
3643 y -= 100;
3644 processMove(mapper, x, y);
3645 processSync(mapper);
3646
3647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3648 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3649 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3650 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3651 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3652 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3653 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3654 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3655 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3656 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3657 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3658 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3659
3660 NotifyMotionArgs motionArgs;
3661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3662 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3663 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3664 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3665 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3666 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3667 ASSERT_EQ(0, motionArgs.flags);
3668 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3669 ASSERT_EQ(0, motionArgs.buttonState);
3670 ASSERT_EQ(0, motionArgs.edgeFlags);
3671 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3672 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3675 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3676 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3677 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3678 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3679
3680 // Keep moving out of bounds. Should generate a pointer move.
3681 y -= 50;
3682 processMove(mapper, x, y);
3683 processSync(mapper);
3684
3685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3686 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3687 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3688 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3689 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3691 ASSERT_EQ(0, motionArgs.flags);
3692 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3693 ASSERT_EQ(0, motionArgs.buttonState);
3694 ASSERT_EQ(0, motionArgs.edgeFlags);
3695 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3696 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3699 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3700 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3701 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3702 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3703
3704 // Release out of bounds. Should generate a pointer up.
3705 processUp(mapper);
3706 processSync(mapper);
3707
3708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3709 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3710 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3711 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3712 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3713 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3714 ASSERT_EQ(0, motionArgs.flags);
3715 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3716 ASSERT_EQ(0, motionArgs.buttonState);
3717 ASSERT_EQ(0, motionArgs.edgeFlags);
3718 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3719 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3722 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3723 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3724 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3725 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3726
3727 // Should not have sent any more keys or motions.
3728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3730}
3731
3732TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3733 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3734 addConfigurationProperty("touch.deviceType", "touchScreen");
3735 prepareDisplay(DISPLAY_ORIENTATION_0);
3736 prepareButtons();
3737 prepareAxes(POSITION);
3738 prepareVirtualKeys();
3739 addMapperAndConfigure(mapper);
3740
3741 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3742
3743 NotifyMotionArgs motionArgs;
3744
3745 // Initially go down out of bounds.
3746 int32_t x = -10;
3747 int32_t y = -10;
3748 processDown(mapper, x, y);
3749 processSync(mapper);
3750
3751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3752
3753 // Move into the display area. Should generate a pointer down.
3754 x = 50;
3755 y = 75;
3756 processMove(mapper, x, y);
3757 processSync(mapper);
3758
3759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3761 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3762 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3763 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3765 ASSERT_EQ(0, motionArgs.flags);
3766 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3767 ASSERT_EQ(0, motionArgs.buttonState);
3768 ASSERT_EQ(0, motionArgs.edgeFlags);
3769 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3770 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3773 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3774 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3775 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3776 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3777
3778 // Release. Should generate a pointer up.
3779 processUp(mapper);
3780 processSync(mapper);
3781
3782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3783 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3784 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3785 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3786 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3788 ASSERT_EQ(0, motionArgs.flags);
3789 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3790 ASSERT_EQ(0, motionArgs.buttonState);
3791 ASSERT_EQ(0, motionArgs.edgeFlags);
3792 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3793 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3796 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3797 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3798 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3799 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3800
3801 // Should not have sent any more keys or motions.
3802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3804}
3805
Santos Cordonfa5cf462017-04-05 10:37:00 -07003806TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3807 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3808 addConfigurationProperty("touch.deviceType", "touchScreen");
3809 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3810
3811 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3812 prepareButtons();
3813 prepareAxes(POSITION);
3814 prepareVirtualKeys();
3815 addMapperAndConfigure(mapper);
3816
3817 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3818
3819 NotifyMotionArgs motionArgs;
3820
3821 // Down.
3822 int32_t x = 100;
3823 int32_t y = 125;
3824 processDown(mapper, x, y);
3825 processSync(mapper);
3826
3827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3828 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3829 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3830 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3831 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3832 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3833 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3834 ASSERT_EQ(0, motionArgs.flags);
3835 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3836 ASSERT_EQ(0, motionArgs.buttonState);
3837 ASSERT_EQ(0, motionArgs.edgeFlags);
3838 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3842 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3843 1, 0, 0, 0, 0, 0, 0, 0));
3844 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3845 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3847
3848 // Move.
3849 x += 50;
3850 y += 75;
3851 processMove(mapper, x, y);
3852 processSync(mapper);
3853
3854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3855 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3856 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3857 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3858 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3859 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3861 ASSERT_EQ(0, motionArgs.flags);
3862 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3863 ASSERT_EQ(0, motionArgs.buttonState);
3864 ASSERT_EQ(0, motionArgs.edgeFlags);
3865 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3866 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3869 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3870 1, 0, 0, 0, 0, 0, 0, 0));
3871 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3872 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3874
3875 // Up.
3876 processUp(mapper);
3877 processSync(mapper);
3878
3879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3880 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3881 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3882 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3883 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3884 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3885 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3886 ASSERT_EQ(0, motionArgs.flags);
3887 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3888 ASSERT_EQ(0, motionArgs.buttonState);
3889 ASSERT_EQ(0, motionArgs.edgeFlags);
3890 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3891 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3894 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3895 1, 0, 0, 0, 0, 0, 0, 0));
3896 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3897 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3898 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3899
3900 // Should not have sent any more keys or motions.
3901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3903}
3904
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
3906 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3907 addConfigurationProperty("touch.deviceType", "touchScreen");
3908 prepareDisplay(DISPLAY_ORIENTATION_0);
3909 prepareButtons();
3910 prepareAxes(POSITION);
3911 prepareVirtualKeys();
3912 addMapperAndConfigure(mapper);
3913
3914 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3915
3916 NotifyMotionArgs motionArgs;
3917
3918 // Down.
3919 int32_t x = 100;
3920 int32_t y = 125;
3921 processDown(mapper, x, y);
3922 processSync(mapper);
3923
3924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3925 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3926 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3927 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3928 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3929 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3930 ASSERT_EQ(0, motionArgs.flags);
3931 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3932 ASSERT_EQ(0, motionArgs.buttonState);
3933 ASSERT_EQ(0, motionArgs.edgeFlags);
3934 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3935 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3938 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3939 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3940 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3941 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3942
3943 // Move.
3944 x += 50;
3945 y += 75;
3946 processMove(mapper, x, y);
3947 processSync(mapper);
3948
3949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3950 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3951 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3952 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3953 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3955 ASSERT_EQ(0, motionArgs.flags);
3956 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3957 ASSERT_EQ(0, motionArgs.buttonState);
3958 ASSERT_EQ(0, motionArgs.edgeFlags);
3959 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3960 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3963 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3964 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3965 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3966 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3967
3968 // Up.
3969 processUp(mapper);
3970 processSync(mapper);
3971
3972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3973 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3974 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3975 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3976 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3977 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3978 ASSERT_EQ(0, motionArgs.flags);
3979 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3980 ASSERT_EQ(0, motionArgs.buttonState);
3981 ASSERT_EQ(0, motionArgs.edgeFlags);
3982 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3983 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3986 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3987 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3988 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3989 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3990
3991 // Should not have sent any more keys or motions.
3992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3994}
3995
3996TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3997 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3998 addConfigurationProperty("touch.deviceType", "touchScreen");
3999 prepareButtons();
4000 prepareAxes(POSITION);
4001 addConfigurationProperty("touch.orientationAware", "0");
4002 addMapperAndConfigure(mapper);
4003
4004 NotifyMotionArgs args;
4005
4006 // Rotation 90.
4007 prepareDisplay(DISPLAY_ORIENTATION_90);
4008 processDown(mapper, toRawX(50), toRawY(75));
4009 processSync(mapper);
4010
4011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4012 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4013 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4014
4015 processUp(mapper);
4016 processSync(mapper);
4017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4018}
4019
4020TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4021 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4022 addConfigurationProperty("touch.deviceType", "touchScreen");
4023 prepareButtons();
4024 prepareAxes(POSITION);
4025 addMapperAndConfigure(mapper);
4026
4027 NotifyMotionArgs args;
4028
4029 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004030 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 prepareDisplay(DISPLAY_ORIENTATION_0);
4032 processDown(mapper, toRawX(50), toRawY(75));
4033 processSync(mapper);
4034
4035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4036 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4037 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4038
4039 processUp(mapper);
4040 processSync(mapper);
4041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4042
4043 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004044 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045 prepareDisplay(DISPLAY_ORIENTATION_90);
4046 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4047 processSync(mapper);
4048
4049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4050 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4051 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4052
4053 processUp(mapper);
4054 processSync(mapper);
4055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4056
4057 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004058 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059 prepareDisplay(DISPLAY_ORIENTATION_180);
4060 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4061 processSync(mapper);
4062
4063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4064 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4065 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4066
4067 processUp(mapper);
4068 processSync(mapper);
4069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4070
4071 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004072 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073 prepareDisplay(DISPLAY_ORIENTATION_270);
4074 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4075 processSync(mapper);
4076
4077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4078 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4079 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4080
4081 processUp(mapper);
4082 processSync(mapper);
4083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4084}
4085
4086TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4087 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4088 addConfigurationProperty("touch.deviceType", "touchScreen");
4089 prepareDisplay(DISPLAY_ORIENTATION_0);
4090 prepareButtons();
4091 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4092 addMapperAndConfigure(mapper);
4093
4094 // These calculations are based on the input device calibration documentation.
4095 int32_t rawX = 100;
4096 int32_t rawY = 200;
4097 int32_t rawPressure = 10;
4098 int32_t rawToolMajor = 12;
4099 int32_t rawDistance = 2;
4100 int32_t rawTiltX = 30;
4101 int32_t rawTiltY = 110;
4102
4103 float x = toDisplayX(rawX);
4104 float y = toDisplayY(rawY);
4105 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4106 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4107 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4108 float distance = float(rawDistance);
4109
4110 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4111 float tiltScale = M_PI / 180;
4112 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4113 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4114 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4115 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4116
4117 processDown(mapper, rawX, rawY);
4118 processPressure(mapper, rawPressure);
4119 processToolMajor(mapper, rawToolMajor);
4120 processDistance(mapper, rawDistance);
4121 processTilt(mapper, rawTiltX, rawTiltY);
4122 processSync(mapper);
4123
4124 NotifyMotionArgs args;
4125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4127 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4128 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4129}
4130
Jason Gerecke489fda82012-09-07 17:19:40 -07004131TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4132 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4133 addConfigurationProperty("touch.deviceType", "touchScreen");
4134 prepareDisplay(DISPLAY_ORIENTATION_0);
4135 prepareLocationCalibration();
4136 prepareButtons();
4137 prepareAxes(POSITION);
4138 addMapperAndConfigure(mapper);
4139
4140 int32_t rawX = 100;
4141 int32_t rawY = 200;
4142
4143 float x = toDisplayX(toCookedX(rawX, rawY));
4144 float y = toDisplayY(toCookedY(rawX, rawY));
4145
4146 processDown(mapper, rawX, rawY);
4147 processSync(mapper);
4148
4149 NotifyMotionArgs args;
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4152 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4153}
4154
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4156 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4157 addConfigurationProperty("touch.deviceType", "touchScreen");
4158 prepareDisplay(DISPLAY_ORIENTATION_0);
4159 prepareButtons();
4160 prepareAxes(POSITION);
4161 addMapperAndConfigure(mapper);
4162
4163 NotifyMotionArgs motionArgs;
4164 NotifyKeyArgs keyArgs;
4165
4166 processDown(mapper, 100, 200);
4167 processSync(mapper);
4168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4170 ASSERT_EQ(0, motionArgs.buttonState);
4171
4172 // press BTN_LEFT, release BTN_LEFT
4173 processKey(mapper, BTN_LEFT, 1);
4174 processSync(mapper);
4175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4177 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4178
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4181 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4182
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 processKey(mapper, BTN_LEFT, 0);
4184 processSync(mapper);
4185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004188
4189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004191 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192
4193 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4194 processKey(mapper, BTN_RIGHT, 1);
4195 processKey(mapper, BTN_MIDDLE, 1);
4196 processSync(mapper);
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4199 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4200 motionArgs.buttonState);
4201
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4203 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4204 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4205
4206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4208 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4209 motionArgs.buttonState);
4210
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 processKey(mapper, BTN_RIGHT, 0);
4212 processSync(mapper);
4213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004216
4217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004219 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220
4221 processKey(mapper, BTN_MIDDLE, 0);
4222 processSync(mapper);
4223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004226
4227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230
4231 // press BTN_BACK, release BTN_BACK
4232 processKey(mapper, BTN_BACK, 1);
4233 processSync(mapper);
4234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4235 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4236 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004237
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4241
4242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4244 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
4246 processKey(mapper, BTN_BACK, 0);
4247 processSync(mapper);
4248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004249 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004251
4252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254 ASSERT_EQ(0, motionArgs.buttonState);
4255
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4257 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4258 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4259
4260 // press BTN_SIDE, release BTN_SIDE
4261 processKey(mapper, BTN_SIDE, 1);
4262 processSync(mapper);
4263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4264 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4265 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004266
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004269 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4270
4271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4273 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
4275 processKey(mapper, BTN_SIDE, 0);
4276 processSync(mapper);
4277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004280
4281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004283 ASSERT_EQ(0, motionArgs.buttonState);
4284
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4286 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4287 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4288
4289 // press BTN_FORWARD, release BTN_FORWARD
4290 processKey(mapper, BTN_FORWARD, 1);
4291 processSync(mapper);
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4293 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4294 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004298 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4299
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4302 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303
4304 processKey(mapper, BTN_FORWARD, 0);
4305 processSync(mapper);
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309
4310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312 ASSERT_EQ(0, motionArgs.buttonState);
4313
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4315 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4316 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4317
4318 // press BTN_EXTRA, release BTN_EXTRA
4319 processKey(mapper, BTN_EXTRA, 1);
4320 processSync(mapper);
4321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4322 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4323 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004324
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4328
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4331 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333 processKey(mapper, BTN_EXTRA, 0);
4334 processSync(mapper);
4335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338
4339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341 ASSERT_EQ(0, motionArgs.buttonState);
4342
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4344 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4345 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4346
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4348
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 // press BTN_STYLUS, release BTN_STYLUS
4350 processKey(mapper, BTN_STYLUS, 1);
4351 processSync(mapper);
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4355
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4358 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359
4360 processKey(mapper, BTN_STYLUS, 0);
4361 processSync(mapper);
4362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365
4366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369
4370 // press BTN_STYLUS2, release BTN_STYLUS2
4371 processKey(mapper, BTN_STYLUS2, 1);
4372 processSync(mapper);
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4376
4377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4378 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4379 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380
4381 processKey(mapper, BTN_STYLUS2, 0);
4382 processSync(mapper);
4383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004386
4387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390
4391 // release touch
4392 processUp(mapper);
4393 processSync(mapper);
4394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4395 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4396 ASSERT_EQ(0, motionArgs.buttonState);
4397}
4398
4399TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4400 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4401 addConfigurationProperty("touch.deviceType", "touchScreen");
4402 prepareDisplay(DISPLAY_ORIENTATION_0);
4403 prepareButtons();
4404 prepareAxes(POSITION);
4405 addMapperAndConfigure(mapper);
4406
4407 NotifyMotionArgs motionArgs;
4408
4409 // default tool type is finger
4410 processDown(mapper, 100, 200);
4411 processSync(mapper);
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4415
4416 // eraser
4417 processKey(mapper, BTN_TOOL_RUBBER, 1);
4418 processSync(mapper);
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4422
4423 // stylus
4424 processKey(mapper, BTN_TOOL_RUBBER, 0);
4425 processKey(mapper, BTN_TOOL_PEN, 1);
4426 processSync(mapper);
4427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4430
4431 // brush
4432 processKey(mapper, BTN_TOOL_PEN, 0);
4433 processKey(mapper, BTN_TOOL_BRUSH, 1);
4434 processSync(mapper);
4435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4438
4439 // pencil
4440 processKey(mapper, BTN_TOOL_BRUSH, 0);
4441 processKey(mapper, BTN_TOOL_PENCIL, 1);
4442 processSync(mapper);
4443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4446
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004447 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 processKey(mapper, BTN_TOOL_PENCIL, 0);
4449 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4450 processSync(mapper);
4451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4454
4455 // mouse
4456 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4457 processKey(mapper, BTN_TOOL_MOUSE, 1);
4458 processSync(mapper);
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4462
4463 // lens
4464 processKey(mapper, BTN_TOOL_MOUSE, 0);
4465 processKey(mapper, BTN_TOOL_LENS, 1);
4466 processSync(mapper);
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4470
4471 // double-tap
4472 processKey(mapper, BTN_TOOL_LENS, 0);
4473 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4474 processSync(mapper);
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4478
4479 // triple-tap
4480 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4481 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4482 processSync(mapper);
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4486
4487 // quad-tap
4488 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4489 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4490 processSync(mapper);
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4494
4495 // finger
4496 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4497 processKey(mapper, BTN_TOOL_FINGER, 1);
4498 processSync(mapper);
4499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4502
4503 // stylus trumps finger
4504 processKey(mapper, BTN_TOOL_PEN, 1);
4505 processSync(mapper);
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4509
4510 // eraser trumps stylus
4511 processKey(mapper, BTN_TOOL_RUBBER, 1);
4512 processSync(mapper);
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4516
4517 // mouse trumps eraser
4518 processKey(mapper, BTN_TOOL_MOUSE, 1);
4519 processSync(mapper);
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4523
4524 // back to default tool type
4525 processKey(mapper, BTN_TOOL_MOUSE, 0);
4526 processKey(mapper, BTN_TOOL_RUBBER, 0);
4527 processKey(mapper, BTN_TOOL_PEN, 0);
4528 processKey(mapper, BTN_TOOL_FINGER, 0);
4529 processSync(mapper);
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4533}
4534
4535TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4536 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4537 addConfigurationProperty("touch.deviceType", "touchScreen");
4538 prepareDisplay(DISPLAY_ORIENTATION_0);
4539 prepareButtons();
4540 prepareAxes(POSITION);
4541 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4542 addMapperAndConfigure(mapper);
4543
4544 NotifyMotionArgs motionArgs;
4545
4546 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4547 processKey(mapper, BTN_TOOL_FINGER, 1);
4548 processMove(mapper, 100, 200);
4549 processSync(mapper);
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4553 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4554
4555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4558 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4559
4560 // move a little
4561 processMove(mapper, 150, 250);
4562 processSync(mapper);
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4566 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4567
4568 // down when BTN_TOUCH is pressed, pressure defaults to 1
4569 processKey(mapper, BTN_TOUCH, 1);
4570 processSync(mapper);
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4574 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4575
4576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4579 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4580
4581 // up when BTN_TOUCH is released, hover restored
4582 processKey(mapper, BTN_TOUCH, 0);
4583 processSync(mapper);
4584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4585 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4587 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4588
4589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4593
4594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4597 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4598
4599 // exit hover when pointer goes away
4600 processKey(mapper, BTN_TOOL_FINGER, 0);
4601 processSync(mapper);
4602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4605 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4606}
4607
4608TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4609 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4610 addConfigurationProperty("touch.deviceType", "touchScreen");
4611 prepareDisplay(DISPLAY_ORIENTATION_0);
4612 prepareButtons();
4613 prepareAxes(POSITION | PRESSURE);
4614 addMapperAndConfigure(mapper);
4615
4616 NotifyMotionArgs motionArgs;
4617
4618 // initially hovering because pressure is 0
4619 processDown(mapper, 100, 200);
4620 processPressure(mapper, 0);
4621 processSync(mapper);
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4625 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4626
4627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4628 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4630 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4631
4632 // move a little
4633 processMove(mapper, 150, 250);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4638 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4639
4640 // down when pressure is non-zero
4641 processPressure(mapper, RAW_PRESSURE_MAX);
4642 processSync(mapper);
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4644 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4646 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4647
4648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4649 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4651 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4652
4653 // up when pressure becomes 0, hover restored
4654 processPressure(mapper, 0);
4655 processSync(mapper);
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4657 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4659 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4660
4661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4662 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4664 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4665
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4669 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4670
4671 // exit hover when pointer goes away
4672 processUp(mapper);
4673 processSync(mapper);
4674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4675 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4677 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4678}
4679
Dan Harmsaca28402018-12-17 13:55:20 -08004680
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681// --- MultiTouchInputMapperTest ---
4682
4683class MultiTouchInputMapperTest : public TouchInputMapperTest {
4684protected:
4685 void prepareAxes(int axes);
4686
4687 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4688 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4689 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4690 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4691 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4692 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4693 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4694 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4695 void processId(MultiTouchInputMapper* mapper, int32_t id);
4696 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4697 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4698 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004699 void processTimestamp(MultiTouchInputMapper* mapper, uint32_t value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 void processMTSync(MultiTouchInputMapper* mapper);
4701 void processSync(MultiTouchInputMapper* mapper);
4702};
4703
4704void MultiTouchInputMapperTest::prepareAxes(int axes) {
4705 if (axes & POSITION) {
4706 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4707 RAW_X_MIN, RAW_X_MAX, 0, 0);
4708 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4709 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4710 }
4711 if (axes & TOUCH) {
4712 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4713 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4714 if (axes & MINOR) {
4715 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4716 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4717 }
4718 }
4719 if (axes & TOOL) {
4720 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4721 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4722 if (axes & MINOR) {
4723 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4724 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4725 }
4726 }
4727 if (axes & ORIENTATION) {
4728 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4729 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4730 }
4731 if (axes & PRESSURE) {
4732 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4733 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4734 }
4735 if (axes & DISTANCE) {
4736 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4737 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4738 }
4739 if (axes & ID) {
4740 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4741 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4742 }
4743 if (axes & SLOT) {
4744 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4745 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4746 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4747 }
4748 if (axes & TOOL_TYPE) {
4749 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4750 0, MT_TOOL_MAX, 0, 0);
4751 }
4752}
4753
4754void MultiTouchInputMapperTest::processPosition(
4755 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004756 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4757 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758}
4759
4760void MultiTouchInputMapperTest::processTouchMajor(
4761 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004762 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763}
4764
4765void MultiTouchInputMapperTest::processTouchMinor(
4766 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004767 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768}
4769
4770void MultiTouchInputMapperTest::processToolMajor(
4771 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004772 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773}
4774
4775void MultiTouchInputMapperTest::processToolMinor(
4776 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004777 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778}
4779
4780void MultiTouchInputMapperTest::processOrientation(
4781 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004782 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783}
4784
4785void MultiTouchInputMapperTest::processPressure(
4786 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004787 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788}
4789
4790void MultiTouchInputMapperTest::processDistance(
4791 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004792 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793}
4794
4795void MultiTouchInputMapperTest::processId(
4796 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004797 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798}
4799
4800void MultiTouchInputMapperTest::processSlot(
4801 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004802 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803}
4804
4805void MultiTouchInputMapperTest::processToolType(
4806 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004807 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808}
4809
4810void MultiTouchInputMapperTest::processKey(
4811 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004812 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813}
4814
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004815void MultiTouchInputMapperTest::processTimestamp(MultiTouchInputMapper* mapper, uint32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004816 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, value);
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08004817}
4818
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004820 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821}
4822
4823void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004824 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825}
4826
4827
4828TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4829 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4830 addConfigurationProperty("touch.deviceType", "touchScreen");
4831 prepareDisplay(DISPLAY_ORIENTATION_0);
4832 prepareAxes(POSITION);
4833 prepareVirtualKeys();
4834 addMapperAndConfigure(mapper);
4835
4836 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4837
4838 NotifyMotionArgs motionArgs;
4839
4840 // Two fingers down at once.
4841 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4842 processPosition(mapper, x1, y1);
4843 processMTSync(mapper);
4844 processPosition(mapper, x2, y2);
4845 processMTSync(mapper);
4846 processSync(mapper);
4847
4848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4849 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4850 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4851 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4852 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4853 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4854 ASSERT_EQ(0, motionArgs.flags);
4855 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4856 ASSERT_EQ(0, motionArgs.buttonState);
4857 ASSERT_EQ(0, motionArgs.edgeFlags);
4858 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4859 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4860 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4862 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4863 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4864 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4865 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4866
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4868 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4869 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4870 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4871 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4872 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4873 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(2), motionArgs.pointerCount);
4879 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4881 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4884 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4886 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4887 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4888 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4890
4891 // Move.
4892 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4893 processPosition(mapper, x1, y1);
4894 processMTSync(mapper);
4895 processPosition(mapper, x2, y2);
4896 processMTSync(mapper);
4897 processSync(mapper);
4898
4899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4900 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4901 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4902 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4903 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4905 ASSERT_EQ(0, motionArgs.flags);
4906 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4907 ASSERT_EQ(0, motionArgs.buttonState);
4908 ASSERT_EQ(0, motionArgs.edgeFlags);
4909 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4910 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4912 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4915 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4917 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4918 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4919 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4920 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4921
4922 // First finger up.
4923 x2 += 15; y2 -= 20;
4924 processPosition(mapper, x2, y2);
4925 processMTSync(mapper);
4926 processSync(mapper);
4927
4928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4929 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4930 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4931 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4932 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4933 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4934 motionArgs.action);
4935 ASSERT_EQ(0, motionArgs.flags);
4936 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4937 ASSERT_EQ(0, motionArgs.buttonState);
4938 ASSERT_EQ(0, motionArgs.edgeFlags);
4939 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4940 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4942 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4945 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4947 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4948 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4949 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4950 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4951
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4953 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4954 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4955 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4956 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4958 ASSERT_EQ(0, motionArgs.flags);
4959 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4960 ASSERT_EQ(0, motionArgs.buttonState);
4961 ASSERT_EQ(0, motionArgs.edgeFlags);
4962 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4963 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4966 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4967 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4968 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4970
4971 // Move.
4972 x2 += 20; y2 -= 25;
4973 processPosition(mapper, x2, y2);
4974 processMTSync(mapper);
4975 processSync(mapper);
4976
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4979 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4980 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4981 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4983 ASSERT_EQ(0, motionArgs.flags);
4984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4985 ASSERT_EQ(0, motionArgs.buttonState);
4986 ASSERT_EQ(0, motionArgs.edgeFlags);
4987 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4988 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4991 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4992 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4993 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4995
4996 // New finger down.
4997 int32_t x3 = 700, y3 = 300;
4998 processPosition(mapper, x2, y2);
4999 processMTSync(mapper);
5000 processPosition(mapper, x3, y3);
5001 processMTSync(mapper);
5002 processSync(mapper);
5003
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5005 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5006 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5007 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5008 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5009 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5010 motionArgs.action);
5011 ASSERT_EQ(0, motionArgs.flags);
5012 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5013 ASSERT_EQ(0, motionArgs.buttonState);
5014 ASSERT_EQ(0, motionArgs.edgeFlags);
5015 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5016 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5018 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5021 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5023 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5024 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5025 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5026 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5027
5028 // Second finger up.
5029 x3 += 30; y3 -= 20;
5030 processPosition(mapper, x3, y3);
5031 processMTSync(mapper);
5032 processSync(mapper);
5033
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5035 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5036 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5037 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5038 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5039 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5040 motionArgs.action);
5041 ASSERT_EQ(0, motionArgs.flags);
5042 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5043 ASSERT_EQ(0, motionArgs.buttonState);
5044 ASSERT_EQ(0, motionArgs.edgeFlags);
5045 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5046 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5048 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5051 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5053 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5054 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5055 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5056 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5057
5058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5060 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5061 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5062 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5064 ASSERT_EQ(0, motionArgs.flags);
5065 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5066 ASSERT_EQ(0, motionArgs.buttonState);
5067 ASSERT_EQ(0, motionArgs.edgeFlags);
5068 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5069 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5072 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5073 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5074 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5075 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5076
5077 // Last finger up.
5078 processMTSync(mapper);
5079 processSync(mapper);
5080
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5083 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5084 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5085 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5086 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5087 ASSERT_EQ(0, motionArgs.flags);
5088 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5089 ASSERT_EQ(0, motionArgs.buttonState);
5090 ASSERT_EQ(0, motionArgs.edgeFlags);
5091 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5092 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5095 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5096 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5097 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5098 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5099
5100 // Should not have sent any more keys or motions.
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5103}
5104
5105TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5106 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5107 addConfigurationProperty("touch.deviceType", "touchScreen");
5108 prepareDisplay(DISPLAY_ORIENTATION_0);
5109 prepareAxes(POSITION | ID);
5110 prepareVirtualKeys();
5111 addMapperAndConfigure(mapper);
5112
5113 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5114
5115 NotifyMotionArgs motionArgs;
5116
5117 // Two fingers down at once.
5118 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5119 processPosition(mapper, x1, y1);
5120 processId(mapper, 1);
5121 processMTSync(mapper);
5122 processPosition(mapper, x2, y2);
5123 processId(mapper, 2);
5124 processMTSync(mapper);
5125 processSync(mapper);
5126
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5129 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5130 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5133 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5134
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5136 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5137 motionArgs.action);
5138 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5139 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5141 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5144 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5146 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5147
5148 // Move.
5149 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5150 processPosition(mapper, x1, y1);
5151 processId(mapper, 1);
5152 processMTSync(mapper);
5153 processPosition(mapper, x2, y2);
5154 processId(mapper, 2);
5155 processMTSync(mapper);
5156 processSync(mapper);
5157
5158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5160 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5161 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5163 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5166 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5168 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5169
5170 // First finger up.
5171 x2 += 15; y2 -= 20;
5172 processPosition(mapper, x2, y2);
5173 processId(mapper, 2);
5174 processMTSync(mapper);
5175 processSync(mapper);
5176
5177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5178 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5179 motionArgs.action);
5180 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5181 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5183 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5186 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5188 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5189
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5192 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5193 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5196 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5197
5198 // Move.
5199 x2 += 20; y2 -= 25;
5200 processPosition(mapper, x2, y2);
5201 processId(mapper, 2);
5202 processMTSync(mapper);
5203 processSync(mapper);
5204
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5207 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5208 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5211 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5212
5213 // New finger down.
5214 int32_t x3 = 700, y3 = 300;
5215 processPosition(mapper, x2, y2);
5216 processId(mapper, 2);
5217 processMTSync(mapper);
5218 processPosition(mapper, x3, y3);
5219 processId(mapper, 3);
5220 processMTSync(mapper);
5221 processSync(mapper);
5222
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5224 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5225 motionArgs.action);
5226 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5229 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5232 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5235
5236 // Second finger up.
5237 x3 += 30; y3 -= 20;
5238 processPosition(mapper, x3, y3);
5239 processId(mapper, 3);
5240 processMTSync(mapper);
5241 processSync(mapper);
5242
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5244 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5245 motionArgs.action);
5246 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5249 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5252 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5254 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5255
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5258 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5262 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5263
5264 // Last finger up.
5265 processMTSync(mapper);
5266 processSync(mapper);
5267
5268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5269 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5270 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5271 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5274 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5275
5276 // Should not have sent any more keys or motions.
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5279}
5280
5281TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5282 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5283 addConfigurationProperty("touch.deviceType", "touchScreen");
5284 prepareDisplay(DISPLAY_ORIENTATION_0);
5285 prepareAxes(POSITION | ID | SLOT);
5286 prepareVirtualKeys();
5287 addMapperAndConfigure(mapper);
5288
5289 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5290
5291 NotifyMotionArgs motionArgs;
5292
5293 // Two fingers down at once.
5294 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5295 processPosition(mapper, x1, y1);
5296 processId(mapper, 1);
5297 processSlot(mapper, 1);
5298 processPosition(mapper, x2, y2);
5299 processId(mapper, 2);
5300 processSync(mapper);
5301
5302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5303 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5304 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5305 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5308 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5309
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5311 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5312 motionArgs.action);
5313 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5314 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5316 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5319 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5321 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5322
5323 // Move.
5324 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5325 processSlot(mapper, 0);
5326 processPosition(mapper, x1, y1);
5327 processSlot(mapper, 1);
5328 processPosition(mapper, x2, y2);
5329 processSync(mapper);
5330
5331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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(x1), toDisplayY(y1), 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 // First finger up.
5344 x2 += 15; y2 -= 20;
5345 processSlot(mapper, 0);
5346 processId(mapper, -1);
5347 processSlot(mapper, 1);
5348 processPosition(mapper, x2, y2);
5349 processSync(mapper);
5350
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5352 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5353 motionArgs.action);
5354 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5355 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5357 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5360 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5362 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5363
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5366 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5367 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5370 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5371
5372 // Move.
5373 x2 += 20; y2 -= 25;
5374 processPosition(mapper, x2, y2);
5375 processSync(mapper);
5376
5377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5379 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5380 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5383 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5384
5385 // New finger down.
5386 int32_t x3 = 700, y3 = 300;
5387 processPosition(mapper, x2, y2);
5388 processSlot(mapper, 0);
5389 processId(mapper, 3);
5390 processPosition(mapper, x3, y3);
5391 processSync(mapper);
5392
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5394 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5395 motionArgs.action);
5396 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5397 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5399 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5402 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5404 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5405
5406 // Second finger up.
5407 x3 += 30; y3 -= 20;
5408 processSlot(mapper, 1);
5409 processId(mapper, -1);
5410 processSlot(mapper, 0);
5411 processPosition(mapper, x3, y3);
5412 processSync(mapper);
5413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5415 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5416 motionArgs.action);
5417 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5418 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5420 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5423 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5425 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5426
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5429 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5433 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5434
5435 // Last finger up.
5436 processId(mapper, -1);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5441 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5442 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5445 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5446
5447 // Should not have sent any more keys or motions.
5448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5450}
5451
5452TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5453 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5454 addConfigurationProperty("touch.deviceType", "touchScreen");
5455 prepareDisplay(DISPLAY_ORIENTATION_0);
5456 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5457 addMapperAndConfigure(mapper);
5458
5459 // These calculations are based on the input device calibration documentation.
5460 int32_t rawX = 100;
5461 int32_t rawY = 200;
5462 int32_t rawTouchMajor = 7;
5463 int32_t rawTouchMinor = 6;
5464 int32_t rawToolMajor = 9;
5465 int32_t rawToolMinor = 8;
5466 int32_t rawPressure = 11;
5467 int32_t rawDistance = 0;
5468 int32_t rawOrientation = 3;
5469 int32_t id = 5;
5470
5471 float x = toDisplayX(rawX);
5472 float y = toDisplayY(rawY);
5473 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5474 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5475 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5476 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5477 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5478 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5479 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5480 float distance = float(rawDistance);
5481
5482 processPosition(mapper, rawX, rawY);
5483 processTouchMajor(mapper, rawTouchMajor);
5484 processTouchMinor(mapper, rawTouchMinor);
5485 processToolMajor(mapper, rawToolMajor);
5486 processToolMinor(mapper, rawToolMinor);
5487 processPressure(mapper, rawPressure);
5488 processOrientation(mapper, rawOrientation);
5489 processDistance(mapper, rawDistance);
5490 processId(mapper, id);
5491 processMTSync(mapper);
5492 processSync(mapper);
5493
5494 NotifyMotionArgs args;
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5496 ASSERT_EQ(0, args.pointerProperties[0].id);
5497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5498 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5499 orientation, distance));
5500}
5501
5502TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5503 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5504 addConfigurationProperty("touch.deviceType", "touchScreen");
5505 prepareDisplay(DISPLAY_ORIENTATION_0);
5506 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5507 addConfigurationProperty("touch.size.calibration", "geometric");
5508 addMapperAndConfigure(mapper);
5509
5510 // These calculations are based on the input device calibration documentation.
5511 int32_t rawX = 100;
5512 int32_t rawY = 200;
5513 int32_t rawTouchMajor = 140;
5514 int32_t rawTouchMinor = 120;
5515 int32_t rawToolMajor = 180;
5516 int32_t rawToolMinor = 160;
5517
5518 float x = toDisplayX(rawX);
5519 float y = toDisplayY(rawY);
5520 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5521 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5522 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5523 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5524 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5525
5526 processPosition(mapper, rawX, rawY);
5527 processTouchMajor(mapper, rawTouchMajor);
5528 processTouchMinor(mapper, rawTouchMinor);
5529 processToolMajor(mapper, rawToolMajor);
5530 processToolMinor(mapper, rawToolMinor);
5531 processMTSync(mapper);
5532 processSync(mapper);
5533
5534 NotifyMotionArgs args;
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5537 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5538}
5539
5540TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5541 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5542 addConfigurationProperty("touch.deviceType", "touchScreen");
5543 prepareDisplay(DISPLAY_ORIENTATION_0);
5544 prepareAxes(POSITION | TOUCH | TOOL);
5545 addConfigurationProperty("touch.size.calibration", "diameter");
5546 addConfigurationProperty("touch.size.scale", "10");
5547 addConfigurationProperty("touch.size.bias", "160");
5548 addConfigurationProperty("touch.size.isSummed", "1");
5549 addMapperAndConfigure(mapper);
5550
5551 // These calculations are based on the input device calibration documentation.
5552 // Note: We only provide a single common touch/tool value because the device is assumed
5553 // not to emit separate values for each pointer (isSummed = 1).
5554 int32_t rawX = 100;
5555 int32_t rawY = 200;
5556 int32_t rawX2 = 150;
5557 int32_t rawY2 = 250;
5558 int32_t rawTouchMajor = 5;
5559 int32_t rawToolMajor = 8;
5560
5561 float x = toDisplayX(rawX);
5562 float y = toDisplayY(rawY);
5563 float x2 = toDisplayX(rawX2);
5564 float y2 = toDisplayY(rawY2);
5565 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5566 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5567 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5568
5569 processPosition(mapper, rawX, rawY);
5570 processTouchMajor(mapper, rawTouchMajor);
5571 processToolMajor(mapper, rawToolMajor);
5572 processMTSync(mapper);
5573 processPosition(mapper, rawX2, rawY2);
5574 processTouchMajor(mapper, rawTouchMajor);
5575 processToolMajor(mapper, rawToolMajor);
5576 processMTSync(mapper);
5577 processSync(mapper);
5578
5579 NotifyMotionArgs args;
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5581 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5584 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5585 args.action);
5586 ASSERT_EQ(size_t(2), args.pointerCount);
5587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5588 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5590 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5591}
5592
5593TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5594 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5595 addConfigurationProperty("touch.deviceType", "touchScreen");
5596 prepareDisplay(DISPLAY_ORIENTATION_0);
5597 prepareAxes(POSITION | TOUCH | TOOL);
5598 addConfigurationProperty("touch.size.calibration", "area");
5599 addConfigurationProperty("touch.size.scale", "43");
5600 addConfigurationProperty("touch.size.bias", "3");
5601 addMapperAndConfigure(mapper);
5602
5603 // These calculations are based on the input device calibration documentation.
5604 int32_t rawX = 100;
5605 int32_t rawY = 200;
5606 int32_t rawTouchMajor = 5;
5607 int32_t rawToolMajor = 8;
5608
5609 float x = toDisplayX(rawX);
5610 float y = toDisplayY(rawY);
5611 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5612 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5613 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5614
5615 processPosition(mapper, rawX, rawY);
5616 processTouchMajor(mapper, rawTouchMajor);
5617 processToolMajor(mapper, rawToolMajor);
5618 processMTSync(mapper);
5619 processSync(mapper);
5620
5621 NotifyMotionArgs args;
5622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5624 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5625}
5626
5627TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5628 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5629 addConfigurationProperty("touch.deviceType", "touchScreen");
5630 prepareDisplay(DISPLAY_ORIENTATION_0);
5631 prepareAxes(POSITION | PRESSURE);
5632 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5633 addConfigurationProperty("touch.pressure.scale", "0.01");
5634 addMapperAndConfigure(mapper);
5635
Michael Wrightaa449c92017-12-13 21:21:43 +00005636 InputDeviceInfo info;
5637 mapper->populateDeviceInfo(&info);
5638 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5639 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5640 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5641
Michael Wrightd02c5b62014-02-10 15:10:22 -08005642 // These calculations are based on the input device calibration documentation.
5643 int32_t rawX = 100;
5644 int32_t rawY = 200;
5645 int32_t rawPressure = 60;
5646
5647 float x = toDisplayX(rawX);
5648 float y = toDisplayY(rawY);
5649 float pressure = float(rawPressure) * 0.01f;
5650
5651 processPosition(mapper, rawX, rawY);
5652 processPressure(mapper, rawPressure);
5653 processMTSync(mapper);
5654 processSync(mapper);
5655
5656 NotifyMotionArgs args;
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5659 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5660}
5661
5662TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5663 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5664 addConfigurationProperty("touch.deviceType", "touchScreen");
5665 prepareDisplay(DISPLAY_ORIENTATION_0);
5666 prepareAxes(POSITION | ID | SLOT);
5667 addMapperAndConfigure(mapper);
5668
5669 NotifyMotionArgs motionArgs;
5670 NotifyKeyArgs keyArgs;
5671
5672 processId(mapper, 1);
5673 processPosition(mapper, 100, 200);
5674 processSync(mapper);
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5676 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5677 ASSERT_EQ(0, motionArgs.buttonState);
5678
5679 // press BTN_LEFT, release BTN_LEFT
5680 processKey(mapper, BTN_LEFT, 1);
5681 processSync(mapper);
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5684 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5685
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5688 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5689
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690 processKey(mapper, BTN_LEFT, 0);
5691 processSync(mapper);
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005693 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005694 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005695
5696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005698 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699
5700 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5701 processKey(mapper, BTN_RIGHT, 1);
5702 processKey(mapper, BTN_MIDDLE, 1);
5703 processSync(mapper);
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5706 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5707 motionArgs.buttonState);
5708
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5710 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5711 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5712
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5715 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5716 motionArgs.buttonState);
5717
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 processKey(mapper, BTN_RIGHT, 0);
5719 processSync(mapper);
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005723
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005726 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727
5728 processKey(mapper, BTN_MIDDLE, 0);
5729 processSync(mapper);
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005733
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005736 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737
5738 // press BTN_BACK, release BTN_BACK
5739 processKey(mapper, BTN_BACK, 1);
5740 processSync(mapper);
5741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5742 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5743 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005744
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005747 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5751 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752
5753 processKey(mapper, BTN_BACK, 0);
5754 processSync(mapper);
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005758
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005761 ASSERT_EQ(0, motionArgs.buttonState);
5762
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5764 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5765 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5766
5767 // press BTN_SIDE, release BTN_SIDE
5768 processKey(mapper, BTN_SIDE, 1);
5769 processSync(mapper);
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5771 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5772 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005773
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005776 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5777
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5780 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781
5782 processKey(mapper, BTN_SIDE, 0);
5783 processSync(mapper);
5784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005785 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005787
5788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005790 ASSERT_EQ(0, motionArgs.buttonState);
5791
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5793 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5794 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5795
5796 // press BTN_FORWARD, release BTN_FORWARD
5797 processKey(mapper, BTN_FORWARD, 1);
5798 processSync(mapper);
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5800 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5801 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005802
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005805 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5806
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5808 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5809 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810
5811 processKey(mapper, BTN_FORWARD, 0);
5812 processSync(mapper);
5813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005814 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005816
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005819 ASSERT_EQ(0, motionArgs.buttonState);
5820
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5822 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5823 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5824
5825 // press BTN_EXTRA, release BTN_EXTRA
5826 processKey(mapper, BTN_EXTRA, 1);
5827 processSync(mapper);
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5829 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5830 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005831
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005834 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5838 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839
5840 processKey(mapper, BTN_EXTRA, 0);
5841 processSync(mapper);
5842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005845
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005848 ASSERT_EQ(0, motionArgs.buttonState);
5849
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5851 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5852 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5853
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5855
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 // press BTN_STYLUS, release BTN_STYLUS
5857 processKey(mapper, BTN_STYLUS, 1);
5858 processSync(mapper);
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005861 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5862
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5865 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866
5867 processKey(mapper, BTN_STYLUS, 0);
5868 processSync(mapper);
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005870 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005872
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005875 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876
5877 // press BTN_STYLUS2, release BTN_STYLUS2
5878 processKey(mapper, BTN_STYLUS2, 1);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005882 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5883
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5886 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887
5888 processKey(mapper, BTN_STYLUS2, 0);
5889 processSync(mapper);
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005891 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005893
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005896 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897
5898 // release touch
5899 processId(mapper, -1);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5903 ASSERT_EQ(0, motionArgs.buttonState);
5904}
5905
5906TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
5907 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5908 addConfigurationProperty("touch.deviceType", "touchScreen");
5909 prepareDisplay(DISPLAY_ORIENTATION_0);
5910 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
5911 addMapperAndConfigure(mapper);
5912
5913 NotifyMotionArgs motionArgs;
5914
5915 // default tool type is finger
5916 processId(mapper, 1);
5917 processPosition(mapper, 100, 200);
5918 processSync(mapper);
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5921 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5922
5923 // eraser
5924 processKey(mapper, BTN_TOOL_RUBBER, 1);
5925 processSync(mapper);
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5929
5930 // stylus
5931 processKey(mapper, BTN_TOOL_RUBBER, 0);
5932 processKey(mapper, BTN_TOOL_PEN, 1);
5933 processSync(mapper);
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5937
5938 // brush
5939 processKey(mapper, BTN_TOOL_PEN, 0);
5940 processKey(mapper, BTN_TOOL_BRUSH, 1);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5945
5946 // pencil
5947 processKey(mapper, BTN_TOOL_BRUSH, 0);
5948 processKey(mapper, BTN_TOOL_PENCIL, 1);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5953
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005954 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005955 processKey(mapper, BTN_TOOL_PENCIL, 0);
5956 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5957 processSync(mapper);
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5961
5962 // mouse
5963 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5964 processKey(mapper, BTN_TOOL_MOUSE, 1);
5965 processSync(mapper);
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5969
5970 // lens
5971 processKey(mapper, BTN_TOOL_MOUSE, 0);
5972 processKey(mapper, BTN_TOOL_LENS, 1);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5977
5978 // double-tap
5979 processKey(mapper, BTN_TOOL_LENS, 0);
5980 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5981 processSync(mapper);
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5985
5986 // triple-tap
5987 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5988 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5992 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5993
5994 // quad-tap
5995 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5996 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6001
6002 // finger
6003 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6004 processKey(mapper, BTN_TOOL_FINGER, 1);
6005 processSync(mapper);
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6009
6010 // stylus trumps finger
6011 processKey(mapper, BTN_TOOL_PEN, 1);
6012 processSync(mapper);
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6016
6017 // eraser trumps stylus
6018 processKey(mapper, BTN_TOOL_RUBBER, 1);
6019 processSync(mapper);
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6023
6024 // mouse trumps eraser
6025 processKey(mapper, BTN_TOOL_MOUSE, 1);
6026 processSync(mapper);
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6030
6031 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6032 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6033 processSync(mapper);
6034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6037
6038 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6039 processToolType(mapper, MT_TOOL_PEN);
6040 processSync(mapper);
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6044
6045 // back to default tool type
6046 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6047 processKey(mapper, BTN_TOOL_MOUSE, 0);
6048 processKey(mapper, BTN_TOOL_RUBBER, 0);
6049 processKey(mapper, BTN_TOOL_PEN, 0);
6050 processKey(mapper, BTN_TOOL_FINGER, 0);
6051 processSync(mapper);
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6055}
6056
6057TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6058 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6059 addConfigurationProperty("touch.deviceType", "touchScreen");
6060 prepareDisplay(DISPLAY_ORIENTATION_0);
6061 prepareAxes(POSITION | ID | SLOT);
6062 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6063 addMapperAndConfigure(mapper);
6064
6065 NotifyMotionArgs motionArgs;
6066
6067 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6068 processId(mapper, 1);
6069 processPosition(mapper, 100, 200);
6070 processSync(mapper);
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6072 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6074 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6075
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6077 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6079 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6080
6081 // move a little
6082 processPosition(mapper, 150, 250);
6083 processSync(mapper);
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6087 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6088
6089 // down when BTN_TOUCH is pressed, pressure defaults to 1
6090 processKey(mapper, BTN_TOUCH, 1);
6091 processSync(mapper);
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6093 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6095 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6096
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6100 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6101
6102 // up when BTN_TOUCH is released, hover restored
6103 processKey(mapper, BTN_TOUCH, 0);
6104 processSync(mapper);
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6106 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6108 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6109
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6111 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6113 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6114
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6116 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6118 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6119
6120 // exit hover when pointer goes away
6121 processId(mapper, -1);
6122 processSync(mapper);
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6124 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6126 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6127}
6128
6129TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6130 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6131 addConfigurationProperty("touch.deviceType", "touchScreen");
6132 prepareDisplay(DISPLAY_ORIENTATION_0);
6133 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6134 addMapperAndConfigure(mapper);
6135
6136 NotifyMotionArgs motionArgs;
6137
6138 // initially hovering because pressure is 0
6139 processId(mapper, 1);
6140 processPosition(mapper, 100, 200);
6141 processPressure(mapper, 0);
6142 processSync(mapper);
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6144 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6146 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6147
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6149 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6151 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6152
6153 // move a little
6154 processPosition(mapper, 150, 250);
6155 processSync(mapper);
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6157 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6159 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6160
6161 // down when pressure becomes non-zero
6162 processPressure(mapper, RAW_PRESSURE_MAX);
6163 processSync(mapper);
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6167 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6168
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6172 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6173
6174 // up when pressure becomes 0, hover restored
6175 processPressure(mapper, 0);
6176 processSync(mapper);
6177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6178 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6180 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6181
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6183 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6185 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6186
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6188 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6190 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6191
6192 // exit hover when pointer goes away
6193 processId(mapper, -1);
6194 processSync(mapper);
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6198 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6199}
6200
Siarhei Vishniakou16f90692017-12-27 14:29:55 -08006201TEST_F(MultiTouchInputMapperTest, Process_HandlesTimestamp) {
6202 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6203
6204 addConfigurationProperty("touch.deviceType", "touchScreen");
6205 prepareDisplay(DISPLAY_ORIENTATION_0);
6206 prepareAxes(POSITION);
6207 addMapperAndConfigure(mapper);
6208 NotifyMotionArgs args;
6209
6210 // By default, deviceTimestamp should be zero
6211 processPosition(mapper, 100, 100);
6212 processMTSync(mapper);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6215 ASSERT_EQ(0U, args.deviceTimestamp);
6216
6217 // Now the timestamp of 1000 is reported by evdev and should appear in MotionArgs
6218 processPosition(mapper, 0, 0);
6219 processTimestamp(mapper, 1000);
6220 processMTSync(mapper);
6221 processSync(mapper);
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6223 ASSERT_EQ(1000U, args.deviceTimestamp);
6224}
6225
Siarhei Vishniakoueaf7acd2018-01-09 12:35:51 -08006226TEST_F(MultiTouchInputMapperTest, WhenMapperIsReset_TimestampIsCleared) {
6227 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6228
6229 addConfigurationProperty("touch.deviceType", "touchScreen");
6230 prepareDisplay(DISPLAY_ORIENTATION_0);
6231 prepareAxes(POSITION);
6232 addMapperAndConfigure(mapper);
6233 NotifyMotionArgs args;
6234
6235 // Send a touch event with a timestamp
6236 processPosition(mapper, 100, 100);
6237 processTimestamp(mapper, 1);
6238 processMTSync(mapper);
6239 processSync(mapper);
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6241 ASSERT_EQ(1U, args.deviceTimestamp);
6242
6243 // Since the data accumulates, and new timestamp has not arrived, deviceTimestamp won't change
6244 processPosition(mapper, 100, 200);
6245 processMTSync(mapper);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6248 ASSERT_EQ(1U, args.deviceTimestamp);
6249
6250 mapper->reset(/* when */ 0);
6251 // After the mapper is reset, deviceTimestamp should become zero again
6252 processPosition(mapper, 100, 300);
6253 processMTSync(mapper);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6256 ASSERT_EQ(0U, args.deviceTimestamp);
6257}
6258
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006259/**
6260 * Set the input device port <--> display port associations, and check that the
6261 * events are routed to the display that matches the display port.
6262 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6263 */
6264TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6265 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6266 const std::string usb2 = "USB2";
6267 const uint8_t hdmi1 = 0;
6268 const uint8_t hdmi2 = 1;
6269 const std::string secondaryUniqueId = "uniqueId2";
6270 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6271
6272 addConfigurationProperty("touch.deviceType", "touchScreen");
6273 prepareAxes(POSITION);
6274 addMapperAndConfigure(mapper);
6275
6276 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6277 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6278
6279 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6280 // for this input device is specified, and the matching viewport is not present,
6281 // the input device should be disabled (at the mapper level).
6282
6283 // Add viewport for display 2 on hdmi2
6284 prepareSecondaryDisplay(type, hdmi2);
6285 // Send a touch event
6286 processPosition(mapper, 100, 100);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6289
6290 // Add viewport for display 1 on hdmi1
6291 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6292 // Send a touch event again
6293 processPosition(mapper, 100, 100);
6294 processSync(mapper);
6295
6296 NotifyMotionArgs args;
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6298 ASSERT_EQ(DISPLAY_ID, args.displayId);
6299}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300
Arthur Hung41a712e2018-11-22 19:41:03 +08006301/**
6302 * Expect fallback to internal viewport if device is external and external viewport is not present.
6303 */
6304TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6305 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6306 prepareAxes(POSITION);
6307 addConfigurationProperty("touch.deviceType", "touchScreen");
6308 prepareDisplay(DISPLAY_ORIENTATION_0);
6309 mDevice->setExternal(true);
6310 addMapperAndConfigure(mapper);
6311
6312 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6313
6314 NotifyMotionArgs motionArgs;
6315
6316 // Expect the event to be sent to the internal viewport,
6317 // because an external viewport is not present.
6318 processPosition(mapper, 100, 100);
6319 processSync(mapper);
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6321 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6322
6323 // Expect the event to be sent to the external viewport if it is present.
6324 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6325 processPosition(mapper, 100, 100);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6329}
6330
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006331TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6332 // Setup PointerController for second display.
6333 sp<FakePointerController> fakePointerController = new FakePointerController();
6334 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6335 fakePointerController->setPosition(100, 200);
6336 fakePointerController->setButtonState(0);
6337 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6338 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6339
6340 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6341 prepareDisplay(DISPLAY_ORIENTATION_0);
6342 prepareAxes(POSITION);
6343 addMapperAndConfigure(mapper);
6344
6345 // Check source is mouse that would obtain the PointerController.
6346 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6347
6348 NotifyMotionArgs motionArgs;
6349 processPosition(mapper, 100, 100);
6350 processSync(mapper);
6351
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6354 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6355}
6356
Arthur Hung7c645402019-01-25 17:45:42 +08006357TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6358 // Setup the first touch screen device.
6359 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6360 prepareAxes(POSITION | ID | SLOT);
6361 addConfigurationProperty("touch.deviceType", "touchScreen");
6362 addMapperAndConfigure(mapper);
6363
6364 // Create the second touch screen device, and enable multi fingers.
6365 const std::string USB2 = "USB2";
6366 const int32_t SECOND_DEVICE_ID = 2;
6367 InputDeviceIdentifier identifier;
6368 identifier.name = DEVICE_NAME;
6369 identifier.location = USB2;
6370 InputDevice* device2 = new InputDevice(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6371 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
6372 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6373 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6374 0 /*flat*/, 0 /*fuzz*/);
6375 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6376 0 /*flat*/, 0 /*fuzz*/);
6377 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6378 0 /*flat*/, 0 /*fuzz*/);
6379 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6380 0 /*flat*/, 0 /*fuzz*/);
6381 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6382 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6383 String8("touchScreen"));
6384
6385 // Setup the second touch screen device.
6386 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2);
6387 device2->addMapper(mapper2);
6388 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6389 device2->reset(ARBITRARY_TIME);
6390
6391 // Setup PointerController.
6392 sp<FakePointerController> fakePointerController = new FakePointerController();
6393 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6394 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6395
6396 // Setup policy for associated displays and show touches.
6397 const uint8_t hdmi1 = 0;
6398 const uint8_t hdmi2 = 1;
6399 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6400 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6401 mFakePolicy->setShowTouches(true);
6402
6403 // Create displays.
6404 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6405 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6406
6407 // Default device will reconfigure above, need additional reconfiguration for another device.
6408 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6409 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6410
6411 // Two fingers down at default display.
6412 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6413 processPosition(mapper, x1, y1);
6414 processId(mapper, 1);
6415 processSlot(mapper, 1);
6416 processPosition(mapper, x2, y2);
6417 processId(mapper, 2);
6418 processSync(mapper);
6419
6420 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6421 fakePointerController->getSpots().find(DISPLAY_ID);
6422 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6423 ASSERT_EQ(size_t(2), iter->second.size());
6424
6425 // Two fingers down at second display.
6426 processPosition(mapper2, x1, y1);
6427 processId(mapper2, 1);
6428 processSlot(mapper2, 1);
6429 processPosition(mapper2, x2, y2);
6430 processId(mapper2, 2);
6431 processSync(mapper2);
6432
6433 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6434 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6435 ASSERT_EQ(size_t(2), iter->second.size());
6436}
6437
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006438TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6439 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6440 prepareAxes(POSITION);
6441 addConfigurationProperty("touch.deviceType", "touchScreen");
6442 prepareDisplay(DISPLAY_ORIENTATION_0);
6443 addMapperAndConfigure(mapper);
6444
6445 NotifyMotionArgs motionArgs;
6446 // Unrotated video frame
6447 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6448 std::vector<TouchVideoFrame> frames{frame};
6449 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6450 processPosition(mapper, 100, 200);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(frames, motionArgs.videoFrames);
6454
6455 // Subsequent touch events should not have any videoframes
6456 // This is implemented separately in FakeEventHub,
6457 // but that should match the behaviour of TouchVideoDevice.
6458 processPosition(mapper, 200, 200);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6462}
6463
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006464TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6465 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6466 prepareAxes(POSITION);
6467 addConfigurationProperty("touch.deviceType", "touchScreen");
6468 addMapperAndConfigure(mapper);
6469 // Unrotated video frame
6470 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6471 NotifyMotionArgs motionArgs;
6472
6473 // Test all 4 orientations
6474 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6475 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6476 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6477 clearViewports();
6478 prepareDisplay(orientation);
6479 std::vector<TouchVideoFrame> frames{frame};
6480 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6481 processPosition(mapper, 100, 200);
6482 processSync(mapper);
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 frames[0].rotate(orientation);
6485 ASSERT_EQ(frames, motionArgs.videoFrames);
6486 }
6487}
6488
6489TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6490 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6491 prepareAxes(POSITION);
6492 addConfigurationProperty("touch.deviceType", "touchScreen");
6493 addMapperAndConfigure(mapper);
6494 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6495 // so mix these.
6496 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6497 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6498 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6499 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6500 NotifyMotionArgs motionArgs;
6501
6502 prepareDisplay(DISPLAY_ORIENTATION_90);
6503 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6504 processPosition(mapper, 100, 200);
6505 processSync(mapper);
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 std::for_each(frames.begin(), frames.end(),
6508 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6509 ASSERT_EQ(frames, motionArgs.videoFrames);
6510}
6511
Michael Wrightd02c5b62014-02-10 15:10:22 -08006512} // namespace android