blob: 09df2a70c779ad9b9921def3650975128ddf2e4e [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../InputReader.h"
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080018#include "TestInputListener.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Michael Wrightd02c5b62014-02-10 15:10:22 -080020#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080021#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <math.h>
23
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080024
Michael Wrightd02c5b62014-02-10 15:10:22 -080025namespace android {
26
27// An arbitrary time value.
28static const nsecs_t ARBITRARY_TIME = 1234;
29
30// Arbitrary display properties.
31static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070032static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080033static const int32_t DISPLAY_WIDTH = 480;
34static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070035static const int32_t VIRTUAL_DISPLAY_ID = 1;
36static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
37static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070038static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070039static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
41// Error tolerance for floating point assertions.
42static const float EPSILON = 0.001f;
43
44template<typename T>
45static inline T min(T a, T b) {
46 return a < b ? a : b;
47}
48
49static inline float avg(float x, float y) {
50 return (x + y) / 2;
51}
52
53
54// --- FakePointerController ---
55
56class FakePointerController : public PointerControllerInterface {
57 bool mHaveBounds;
58 float mMinX, mMinY, mMaxX, mMaxY;
59 float mX, mY;
60 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080061 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
63protected:
64 virtual ~FakePointerController() { }
65
66public:
67 FakePointerController() :
68 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080069 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080070 }
71
72 void setBounds(float minX, float minY, float maxX, float maxY) {
73 mHaveBounds = true;
74 mMinX = minX;
75 mMinY = minY;
76 mMaxX = maxX;
77 mMaxY = maxY;
78 }
79
Arthur Hungc7ad2d02018-12-18 17:41:29 +080080 void setDisplayId(int32_t displayId) {
81 mDisplayId = displayId;
82 }
83
Michael Wrightd02c5b62014-02-10 15:10:22 -080084 virtual void setPosition(float x, float y) {
85 mX = x;
86 mY = y;
87 }
88
89 virtual void setButtonState(int32_t buttonState) {
90 mButtonState = buttonState;
91 }
92
93 virtual int32_t getButtonState() const {
94 return mButtonState;
95 }
96
97 virtual void getPosition(float* outX, float* outY) const {
98 *outX = mX;
99 *outY = mY;
100 }
101
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800102 virtual int32_t getDisplayId() const {
103 return mDisplayId;
104 }
105
Arthur Hung7c645402019-01-25 17:45:42 +0800106 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
107 return mSpotsByDisplay;
108 }
109
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110private:
111 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
112 *outMinX = mMinX;
113 *outMinY = mMinY;
114 *outMaxX = mMaxX;
115 *outMaxY = mMaxY;
116 return mHaveBounds;
117 }
118
119 virtual void move(float deltaX, float deltaY) {
120 mX += deltaX;
121 if (mX < mMinX) mX = mMinX;
122 if (mX > mMaxX) mX = mMaxX;
123 mY += deltaY;
124 if (mY < mMinY) mY = mMinY;
125 if (mY > mMaxY) mY = mMaxY;
126 }
127
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100128 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129 }
130
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100131 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 }
133
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100134 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 }
136
Arthur Hung7c645402019-01-25 17:45:42 +0800137 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
138 int32_t displayId) {
139 std::vector<int32_t> newSpots;
140 // Add spots for fingers that are down.
141 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
142 uint32_t id = idBits.clearFirstMarkedBit();
143 newSpots.push_back(id);
144 }
145
146 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147 }
148
149 virtual void clearSpots() {
150 }
Arthur Hung7c645402019-01-25 17:45:42 +0800151
152 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153};
154
155
156// --- FakeInputReaderPolicy ---
157
158class FakeInputReaderPolicy : public InputReaderPolicyInterface {
159 InputReaderConfiguration mConfig;
160 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800161 std::vector<InputDeviceInfo> mInputDevices;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100162 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700163 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
165protected:
166 virtual ~FakeInputReaderPolicy() { }
167
168public:
169 FakeInputReaderPolicy() {
170 }
171
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700172 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100173 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100174 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700175 }
176
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700177 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
178 return mConfig.getDisplayViewportByUniqueId(uniqueId);
179 }
180 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
181 return mConfig.getDisplayViewportByType(type);
182 }
183
184 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
185 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700186 }
187
188 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700189 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
190 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700191 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700192 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700193 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100194 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 }
196
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100197 void addExcludedDeviceName(const std::string& deviceName) {
198 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199 }
200
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700201 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
202 mConfig.portAssociations.insert({inputPort, displayPort});
203 }
204
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000205 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700206
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000207 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700208
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
210 mPointerControllers.add(deviceId, controller);
211 }
212
213 const InputReaderConfiguration* getReaderConfiguration() const {
214 return &mConfig;
215 }
216
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800217 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 return mInputDevices;
219 }
220
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100221 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700222 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700223 return transform;
224 }
225
226 void setTouchAffineTransformation(const TouchAffineTransformation t) {
227 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800228 }
229
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800230 void setPointerCapture(bool enabled) {
231 mConfig.pointerCapture = enabled;
232 }
233
Arthur Hung7c645402019-01-25 17:45:42 +0800234 void setShowTouches(bool enabled) {
235 mConfig.showTouches = enabled;
236 }
237
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700239 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700240 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
241 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700242 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
243 || orientation == DISPLAY_ORIENTATION_270);
244 DisplayViewport v;
245 v.displayId = displayId;
246 v.orientation = orientation;
247 v.logicalLeft = 0;
248 v.logicalTop = 0;
249 v.logicalRight = isRotated ? height : width;
250 v.logicalBottom = isRotated ? width : height;
251 v.physicalLeft = 0;
252 v.physicalTop = 0;
253 v.physicalRight = isRotated ? height : width;
254 v.physicalBottom = isRotated ? width : height;
255 v.deviceWidth = isRotated ? height : width;
256 v.deviceHeight = isRotated ? width : height;
257 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700258 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100259 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700260 return v;
261 }
262
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
264 *outConfig = mConfig;
265 }
266
267 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
268 return mPointerControllers.valueFor(deviceId);
269 }
270
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800271 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 mInputDevices = inputDevices;
273 }
274
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100275 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700276 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277 }
278
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100279 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
280 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 }
282};
283
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284// --- FakeEventHub ---
285
286class FakeEventHub : public EventHubInterface {
287 struct KeyInfo {
288 int32_t keyCode;
289 uint32_t flags;
290 };
291
292 struct Device {
293 InputDeviceIdentifier identifier;
294 uint32_t classes;
295 PropertyMap configuration;
296 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
297 KeyedVector<int, bool> relativeAxes;
298 KeyedVector<int32_t, int32_t> keyCodeStates;
299 KeyedVector<int32_t, int32_t> scanCodeStates;
300 KeyedVector<int32_t, int32_t> switchStates;
301 KeyedVector<int32_t, int32_t> absoluteAxisValue;
302 KeyedVector<int32_t, KeyInfo> keysByScanCode;
303 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
304 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800305 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700306 bool enabled;
307
308 status_t enable() {
309 enabled = true;
310 return OK;
311 }
312
313 status_t disable() {
314 enabled = false;
315 return OK;
316 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700318 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700319 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 }
321 };
322
323 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100324 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 List<RawEvent> mEvents;
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600326 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700328public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 virtual ~FakeEventHub() {
330 for (size_t i = 0; i < mDevices.size(); i++) {
331 delete mDevices.valueAt(i);
332 }
333 }
334
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 FakeEventHub() { }
336
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100337 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 Device* device = new Device(classes);
339 device->identifier.name = name;
340 mDevices.add(deviceId, device);
341
342 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
343 }
344
345 void removeDevice(int32_t deviceId) {
346 delete mDevices.valueFor(deviceId);
347 mDevices.removeItem(deviceId);
348
349 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
350 }
351
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700352 bool isDeviceEnabled(int32_t deviceId) {
353 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700354 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700355 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
356 return false;
357 }
358 return device->enabled;
359 }
360
361 status_t enableDevice(int32_t deviceId) {
362 status_t result;
363 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700364 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700365 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
366 return BAD_VALUE;
367 }
368 if (device->enabled) {
369 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
370 return OK;
371 }
372 result = device->enable();
373 return result;
374 }
375
376 status_t disableDevice(int32_t deviceId) {
377 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700378 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700379 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
380 return BAD_VALUE;
381 }
382 if (!device->enabled) {
383 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
384 return OK;
385 }
386 return device->disable();
387 }
388
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 void finishDeviceScan() {
390 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
391 }
392
393 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
394 Device* device = getDevice(deviceId);
395 device->configuration.addProperty(key, value);
396 }
397
398 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
399 Device* device = getDevice(deviceId);
400 device->configuration.addAll(configuration);
401 }
402
403 void addAbsoluteAxis(int32_t deviceId, int axis,
404 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
405 Device* device = getDevice(deviceId);
406
407 RawAbsoluteAxisInfo info;
408 info.valid = true;
409 info.minValue = minValue;
410 info.maxValue = maxValue;
411 info.flat = flat;
412 info.fuzz = fuzz;
413 info.resolution = resolution;
414 device->absoluteAxes.add(axis, info);
415 }
416
417 void addRelativeAxis(int32_t deviceId, int32_t axis) {
418 Device* device = getDevice(deviceId);
419 device->relativeAxes.add(axis, true);
420 }
421
422 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
423 Device* device = getDevice(deviceId);
424 device->keyCodeStates.replaceValueFor(keyCode, state);
425 }
426
427 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
428 Device* device = getDevice(deviceId);
429 device->scanCodeStates.replaceValueFor(scanCode, state);
430 }
431
432 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
433 Device* device = getDevice(deviceId);
434 device->switchStates.replaceValueFor(switchCode, state);
435 }
436
437 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
438 Device* device = getDevice(deviceId);
439 device->absoluteAxisValue.replaceValueFor(axis, value);
440 }
441
442 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
443 int32_t keyCode, uint32_t flags) {
444 Device* device = getDevice(deviceId);
445 KeyInfo info;
446 info.keyCode = keyCode;
447 info.flags = flags;
448 if (scanCode) {
449 device->keysByScanCode.add(scanCode, info);
450 }
451 if (usageCode) {
452 device->keysByUsageCode.add(usageCode, info);
453 }
454 }
455
456 void addLed(int32_t deviceId, int32_t led, bool initialState) {
457 Device* device = getDevice(deviceId);
458 device->leds.add(led, initialState);
459 }
460
461 bool getLedState(int32_t deviceId, int32_t led) {
462 Device* device = getDevice(deviceId);
463 return device->leds.valueFor(led);
464 }
465
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100466 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467 return mExcludedDevices;
468 }
469
470 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
471 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800472 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473 }
474
475 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
476 int32_t code, int32_t value) {
477 RawEvent event;
478 event.when = when;
479 event.deviceId = deviceId;
480 event.type = type;
481 event.code = code;
482 event.value = value;
483 mEvents.push_back(event);
484
485 if (type == EV_ABS) {
486 setAbsoluteAxisValue(deviceId, code, value);
487 }
488 }
489
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600490 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
491 std::vector<TouchVideoFrame>> videoFrames) {
492 mVideoFrames = std::move(videoFrames);
493 }
494
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495 void assertQueueIsEmpty() {
496 ASSERT_EQ(size_t(0), mEvents.size())
497 << "Expected the event queue to be empty (fully consumed).";
498 }
499
500private:
501 Device* getDevice(int32_t deviceId) const {
502 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100503 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 }
505
506 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
507 Device* device = getDevice(deviceId);
508 return device ? device->classes : 0;
509 }
510
511 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
512 Device* device = getDevice(deviceId);
513 return device ? device->identifier : InputDeviceIdentifier();
514 }
515
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100516 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 return 0;
518 }
519
520 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
521 Device* device = getDevice(deviceId);
522 if (device) {
523 *outConfiguration = device->configuration;
524 }
525 }
526
527 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
528 RawAbsoluteAxisInfo* outAxisInfo) const {
529 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800530 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 ssize_t index = device->absoluteAxes.indexOfKey(axis);
532 if (index >= 0) {
533 *outAxisInfo = device->absoluteAxes.valueAt(index);
534 return OK;
535 }
536 }
537 outAxisInfo->clear();
538 return -1;
539 }
540
541 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
542 Device* device = getDevice(deviceId);
543 if (device) {
544 return device->relativeAxes.indexOfKey(axis) >= 0;
545 }
546 return false;
547 }
548
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100549 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 return false;
551 }
552
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700553 virtual status_t mapKey(int32_t deviceId,
554 int32_t scanCode, int32_t usageCode, int32_t metaState,
555 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556 Device* device = getDevice(deviceId);
557 if (device) {
558 const KeyInfo* key = getKey(device, scanCode, usageCode);
559 if (key) {
560 if (outKeycode) {
561 *outKeycode = key->keyCode;
562 }
563 if (outFlags) {
564 *outFlags = key->flags;
565 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700566 if (outMetaState) {
567 *outMetaState = metaState;
568 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569 return OK;
570 }
571 }
572 return NAME_NOT_FOUND;
573 }
574
575 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
576 if (usageCode) {
577 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
578 if (index >= 0) {
579 return &device->keysByUsageCode.valueAt(index);
580 }
581 }
582 if (scanCode) {
583 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
584 if (index >= 0) {
585 return &device->keysByScanCode.valueAt(index);
586 }
587 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700588 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100591 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 return NAME_NOT_FOUND;
593 }
594
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100595 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 mExcludedDevices = devices;
597 }
598
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100599 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 if (mEvents.empty()) {
601 return 0;
602 }
603
604 *buffer = *mEvents.begin();
605 mEvents.erase(mEvents.begin());
606 return 1;
607 }
608
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800609 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600610 auto it = mVideoFrames.find(deviceId);
611 if (it != mVideoFrames.end()) {
612 std::vector<TouchVideoFrame> frames = std::move(it->second);
613 mVideoFrames.erase(deviceId);
614 return frames;
615 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800616 return {};
617 }
618
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
620 Device* device = getDevice(deviceId);
621 if (device) {
622 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
623 if (index >= 0) {
624 return device->scanCodeStates.valueAt(index);
625 }
626 }
627 return AKEY_STATE_UNKNOWN;
628 }
629
630 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
631 Device* device = getDevice(deviceId);
632 if (device) {
633 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
634 if (index >= 0) {
635 return device->keyCodeStates.valueAt(index);
636 }
637 }
638 return AKEY_STATE_UNKNOWN;
639 }
640
641 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
642 Device* device = getDevice(deviceId);
643 if (device) {
644 ssize_t index = device->switchStates.indexOfKey(sw);
645 if (index >= 0) {
646 return device->switchStates.valueAt(index);
647 }
648 }
649 return AKEY_STATE_UNKNOWN;
650 }
651
652 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
653 int32_t* outValue) const {
654 Device* device = getDevice(deviceId);
655 if (device) {
656 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
657 if (index >= 0) {
658 *outValue = device->absoluteAxisValue.valueAt(index);
659 return OK;
660 }
661 }
662 *outValue = 0;
663 return -1;
664 }
665
666 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
667 uint8_t* outFlags) const {
668 bool result = false;
669 Device* device = getDevice(deviceId);
670 if (device) {
671 for (size_t i = 0; i < numCodes; i++) {
672 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
673 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
674 outFlags[i] = 1;
675 result = true;
676 }
677 }
678 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
679 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
680 outFlags[i] = 1;
681 result = true;
682 }
683 }
684 }
685 }
686 return result;
687 }
688
689 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
690 Device* device = getDevice(deviceId);
691 if (device) {
692 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
693 return index >= 0;
694 }
695 return false;
696 }
697
698 virtual bool hasLed(int32_t deviceId, int32_t led) const {
699 Device* device = getDevice(deviceId);
700 return device && device->leds.indexOfKey(led) >= 0;
701 }
702
703 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
704 Device* device = getDevice(deviceId);
705 if (device) {
706 ssize_t index = device->leds.indexOfKey(led);
707 if (index >= 0) {
708 device->leds.replaceValueAt(led, on);
709 } else {
710 ADD_FAILURE()
711 << "Attempted to set the state of an LED that the EventHub declared "
712 "was not present. led=" << led;
713 }
714 }
715 }
716
717 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800718 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 outVirtualKeys.clear();
720
721 Device* device = getDevice(deviceId);
722 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800723 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 }
725 }
726
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100727 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700728 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 }
730
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100731 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732 return false;
733 }
734
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100735 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800736 }
737
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100738 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 }
740
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100741 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 return false;
743 }
744
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800745 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746 }
747
748 virtual void monitor() {
749 }
750
751 virtual void requestReopenDevices() {
752 }
753
754 virtual void wake() {
755 }
756};
757
758
759// --- FakeInputReaderContext ---
760
761class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700762 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 sp<InputReaderPolicyInterface> mPolicy;
764 sp<InputListenerInterface> mListener;
765 int32_t mGlobalMetaState;
766 bool mUpdateGlobalMetaStateWasCalled;
767 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800768 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769
770public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700771 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
772 const sp<InputReaderPolicyInterface>& policy,
773 const sp<InputListenerInterface>& listener)
774 : mEventHub(eventHub),
775 mPolicy(policy),
776 mListener(listener),
777 mGlobalMetaState(0),
778 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779
780 virtual ~FakeInputReaderContext() { }
781
782 void assertUpdateGlobalMetaStateWasCalled() {
783 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
784 << "Expected updateGlobalMetaState() to have been called.";
785 mUpdateGlobalMetaStateWasCalled = false;
786 }
787
788 void setGlobalMetaState(int32_t state) {
789 mGlobalMetaState = state;
790 }
791
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800792 uint32_t getGeneration() {
793 return mGeneration;
794 }
795
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796private:
797 virtual void updateGlobalMetaState() {
798 mUpdateGlobalMetaStateWasCalled = true;
799 }
800
801 virtual int32_t getGlobalMetaState() {
802 return mGlobalMetaState;
803 }
804
805 virtual EventHubInterface* getEventHub() {
806 return mEventHub.get();
807 }
808
809 virtual InputReaderPolicyInterface* getPolicy() {
810 return mPolicy.get();
811 }
812
813 virtual InputListenerInterface* getListener() {
814 return mListener.get();
815 }
816
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100817 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 }
819
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100820 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 return false;
822 }
823
824 virtual void fadePointer() {
825 }
826
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100827 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 }
829
830 virtual int32_t bumpGeneration() {
831 return ++mGeneration;
832 }
Michael Wright842500e2015-03-13 17:32:02 -0700833
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800834 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700835
836 }
837
838 virtual void dispatchExternalStylusState(const StylusState&) {
839
840 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800841
842 virtual uint32_t getNextSequenceNum() {
843 return mNextSequenceNum++;
844 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845};
846
847
848// --- FakeInputMapper ---
849
850class FakeInputMapper : public InputMapper {
851 uint32_t mSources;
852 int32_t mKeyboardType;
853 int32_t mMetaState;
854 KeyedVector<int32_t, int32_t> mKeyCodeStates;
855 KeyedVector<int32_t, int32_t> mScanCodeStates;
856 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800857 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858 RawEvent mLastEvent;
859
860 bool mConfigureWasCalled;
861 bool mResetWasCalled;
862 bool mProcessWasCalled;
863
Arthur Hungc23540e2018-11-29 20:42:11 +0800864 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865public:
866 FakeInputMapper(InputDevice* device, uint32_t sources) :
867 InputMapper(device),
868 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
869 mMetaState(0),
870 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
871 }
872
873 virtual ~FakeInputMapper() { }
874
875 void setKeyboardType(int32_t keyboardType) {
876 mKeyboardType = keyboardType;
877 }
878
879 void setMetaState(int32_t metaState) {
880 mMetaState = metaState;
881 }
882
883 void assertConfigureWasCalled() {
884 ASSERT_TRUE(mConfigureWasCalled)
885 << "Expected configure() to have been called.";
886 mConfigureWasCalled = false;
887 }
888
889 void assertResetWasCalled() {
890 ASSERT_TRUE(mResetWasCalled)
891 << "Expected reset() to have been called.";
892 mResetWasCalled = false;
893 }
894
Yi Kong9b14ac62018-07-17 13:48:38 -0700895 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 ASSERT_TRUE(mProcessWasCalled)
897 << "Expected process() to have been called.";
898 if (outLastEvent) {
899 *outLastEvent = mLastEvent;
900 }
901 mProcessWasCalled = false;
902 }
903
904 void setKeyCodeState(int32_t keyCode, int32_t state) {
905 mKeyCodeStates.replaceValueFor(keyCode, state);
906 }
907
908 void setScanCodeState(int32_t scanCode, int32_t state) {
909 mScanCodeStates.replaceValueFor(scanCode, state);
910 }
911
912 void setSwitchState(int32_t switchCode, int32_t state) {
913 mSwitchStates.replaceValueFor(switchCode, state);
914 }
915
916 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800917 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 }
919
920private:
921 virtual uint32_t getSources() {
922 return mSources;
923 }
924
925 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
926 InputMapper::populateDeviceInfo(deviceInfo);
927
928 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
929 deviceInfo->setKeyboardType(mKeyboardType);
930 }
931 }
932
Arthur Hungc23540e2018-11-29 20:42:11 +0800933 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800935
936 // Find the associated viewport if exist.
937 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
938 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
939 mViewport = config->getDisplayViewportByPort(*displayPort);
940 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 }
942
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100943 virtual void reset(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 mResetWasCalled = true;
945 }
946
947 virtual void process(const RawEvent* rawEvent) {
948 mLastEvent = *rawEvent;
949 mProcessWasCalled = true;
950 }
951
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100952 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
954 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
955 }
956
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100957 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
959 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
960 }
961
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100962 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963 ssize_t index = mSwitchStates.indexOfKey(switchCode);
964 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
965 }
966
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100967 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 const int32_t* keyCodes, uint8_t* outFlags) {
969 bool result = false;
970 for (size_t i = 0; i < numCodes; i++) {
971 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
972 if (keyCodes[i] == mSupportedKeyCodes[j]) {
973 outFlags[i] = 1;
974 result = true;
975 }
976 }
977 }
978 return result;
979 }
980
981 virtual int32_t getMetaState() {
982 return mMetaState;
983 }
984
985 virtual void fadePointer() {
986 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800987
988 virtual std::optional<int32_t> getAssociatedDisplay() {
989 if (mViewport) {
990 return std::make_optional(mViewport->displayId);
991 }
992 return std::nullopt;
993 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994};
995
996
997// --- InstrumentedInputReader ---
998
999class InstrumentedInputReader : public InputReader {
1000 InputDevice* mNextDevice;
1001
1002public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001003 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1004 const sp<InputReaderPolicyInterface>& policy,
1005 const sp<InputListenerInterface>& listener)
1006 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007
1008 virtual ~InstrumentedInputReader() {
1009 if (mNextDevice) {
1010 delete mNextDevice;
1011 }
1012 }
1013
1014 void setNextDevice(InputDevice* device) {
1015 mNextDevice = device;
1016 }
1017
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001018 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Arthur Hungc23540e2018-11-29 20:42:11 +08001019 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020 InputDeviceIdentifier identifier;
1021 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001022 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 int32_t generation = deviceId + 1;
1024 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
1025 classes);
1026 }
1027
1028protected:
1029 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
1030 const InputDeviceIdentifier& identifier, uint32_t classes) {
1031 if (mNextDevice) {
1032 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001033 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 return device;
1035 }
1036 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1037 }
1038
1039 friend class InputReaderTest;
1040};
1041
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001042// --- InputReaderPolicyTest ---
1043class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001044protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001045 sp<FakeInputReaderPolicy> mFakePolicy;
1046
1047 virtual void SetUp() {
1048 mFakePolicy = new FakeInputReaderPolicy();
1049 }
1050 virtual void TearDown() {
1051 mFakePolicy.clear();
1052 }
1053};
1054
1055/**
1056 * Check that empty set of viewports is an acceptable configuration.
1057 * Also try to get internal viewport two different ways - by type and by uniqueId.
1058 *
1059 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1060 * Such configuration is not currently allowed.
1061 */
1062TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001063 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001064
1065 // We didn't add any viewports yet, so there shouldn't be any.
1066 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001067 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001068 ASSERT_FALSE(internalViewport);
1069
1070 // Add an internal viewport, then clear it
1071 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001072 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001073
1074 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001075 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001076 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001077 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001078
1079 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001080 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001081 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001082 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001083
1084 mFakePolicy->clearViewports();
1085 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001086 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001087 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001088 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001089 ASSERT_FALSE(internalViewport);
1090}
1091
1092TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1093 const std::string internalUniqueId = "local:0";
1094 const std::string externalUniqueId = "local:1";
1095 const std::string virtualUniqueId1 = "virtual:2";
1096 const std::string virtualUniqueId2 = "virtual:3";
1097 constexpr int32_t virtualDisplayId1 = 2;
1098 constexpr int32_t virtualDisplayId2 = 3;
1099
1100 // Add an internal viewport
1101 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001102 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001103 // Add an external viewport
1104 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001105 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001106 // Add an virtual viewport
1107 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001108 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001109 // Add another virtual viewport
1110 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001111 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001112
1113 // Check matching by type for internal
1114 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001115 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001116 ASSERT_TRUE(internalViewport);
1117 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1118
1119 // Check matching by type for external
1120 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001121 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001122 ASSERT_TRUE(externalViewport);
1123 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1124
1125 // Check matching by uniqueId for virtual viewport #1
1126 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001127 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001128 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001129 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001130 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1131 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1132
1133 // Check matching by uniqueId for virtual viewport #2
1134 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001135 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001136 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001137 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001138 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1139 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1140}
1141
1142
1143/**
1144 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1145 * that lookup works by checking display id.
1146 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1147 */
1148TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1149 const std::string uniqueId1 = "uniqueId1";
1150 const std::string uniqueId2 = "uniqueId2";
1151 constexpr int32_t displayId1 = 2;
1152 constexpr int32_t displayId2 = 3;
1153
1154 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1155 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1156 for (const ViewportType& type : types) {
1157 mFakePolicy->clearViewports();
1158 // Add a viewport
1159 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001160 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001161 // Add another viewport
1162 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001163 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001164
1165 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001166 std::optional<DisplayViewport> viewport1 =
1167 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168 ASSERT_TRUE(viewport1);
1169 ASSERT_EQ(displayId1, viewport1->displayId);
1170 ASSERT_EQ(type, viewport1->type);
1171
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001172 std::optional<DisplayViewport> viewport2 =
1173 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001174 ASSERT_TRUE(viewport2);
1175 ASSERT_EQ(displayId2, viewport2->displayId);
1176 ASSERT_EQ(type, viewport2->type);
1177
1178 // When there are multiple viewports of the same kind, and uniqueId is not specified
1179 // in the call to getDisplayViewport, then that situation is not supported.
1180 // The viewports can be stored in any order, so we cannot rely on the order, since that
1181 // is just implementation detail.
1182 // However, we can check that it still returns *a* viewport, we just cannot assert
1183 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001184 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185 ASSERT_TRUE(someViewport);
1186 }
1187}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001189/**
1190 * Check getDisplayViewportByPort
1191 */
1192TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1193 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1194 const std::string uniqueId1 = "uniqueId1";
1195 const std::string uniqueId2 = "uniqueId2";
1196 constexpr int32_t displayId1 = 1;
1197 constexpr int32_t displayId2 = 2;
1198 const uint8_t hdmi1 = 0;
1199 const uint8_t hdmi2 = 1;
1200 const uint8_t hdmi3 = 2;
1201
1202 mFakePolicy->clearViewports();
1203 // Add a viewport that's associated with some display port that's not of interest.
1204 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1205 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1206 // Add another viewport, connected to HDMI1 port
1207 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1208 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1209
1210 // Check that correct display viewport was returned by comparing the display ports.
1211 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1212 ASSERT_TRUE(hdmi1Viewport);
1213 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1214 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1215
1216 // Check that we can still get the same viewport using the uniqueId
1217 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1218 ASSERT_TRUE(hdmi1Viewport);
1219 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1220 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1221 ASSERT_EQ(type, hdmi1Viewport->type);
1222
1223 // Check that we cannot find a port with "HDMI2", because we never added one
1224 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1225 ASSERT_FALSE(hdmi2Viewport);
1226}
1227
Michael Wrightd02c5b62014-02-10 15:10:22 -08001228// --- InputReaderTest ---
1229
1230class InputReaderTest : public testing::Test {
1231protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001232 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001234 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 sp<InstrumentedInputReader> mReader;
1236
1237 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001238 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001240 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241
1242 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1243 }
1244
1245 virtual void TearDown() {
1246 mReader.clear();
1247
1248 mFakeListener.clear();
1249 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 }
1251
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001252 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 const PropertyMap* configuration) {
1254 mFakeEventHub->addDevice(deviceId, name, classes);
1255
1256 if (configuration) {
1257 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1258 }
1259 mFakeEventHub->finishDeviceScan();
1260 mReader->loopOnce();
1261 mReader->loopOnce();
1262 mFakeEventHub->assertQueueIsEmpty();
1263 }
1264
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001265 void disableDevice(int32_t deviceId, InputDevice* device) {
1266 mFakePolicy->addDisabledDevice(deviceId);
1267 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1268 }
1269
1270 void enableDevice(int32_t deviceId, InputDevice* device) {
1271 mFakePolicy->removeDisabledDevice(deviceId);
1272 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1273 }
1274
1275 void configureDevice(uint32_t changes, InputDevice* device) {
1276 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1277 }
1278
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001280 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 const PropertyMap* configuration) {
1282 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1283 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1284 device->addMapper(mapper);
1285 mReader->setNextDevice(device);
1286 addDevice(deviceId, name, classes, configuration);
1287 return mapper;
1288 }
1289};
1290
1291TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001292 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001293 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001294 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001295 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001296
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001297
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001298 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001299 mReader->getInputDevices(inputDevices);
1300
1301 ASSERT_EQ(1U, inputDevices.size());
1302 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001303 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001304 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1305 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1306 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1307
1308 // Should also have received a notification describing the new input devices.
1309 inputDevices = mFakePolicy->getInputDevices();
1310 ASSERT_EQ(1U, inputDevices.size());
1311 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001312 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1314 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1315 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1316}
1317
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001318TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1319 constexpr int32_t deviceId = 1;
1320 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001321 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001322 // Must add at least one mapper or the device will be ignored!
1323 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1324 device->addMapper(mapper);
1325 mReader->setNextDevice(device);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001326 addDevice(deviceId, "fake", deviceClass, nullptr);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001327
Yi Kong9b14ac62018-07-17 13:48:38 -07001328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001329
1330 NotifyDeviceResetArgs resetArgs;
1331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1332 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1333 ASSERT_EQ(deviceId, resetArgs.deviceId);
1334
1335 ASSERT_EQ(device->isEnabled(), true);
1336 disableDevice(deviceId, device);
1337 mReader->loopOnce();
1338
1339 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1340 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1341 ASSERT_EQ(deviceId, resetArgs.deviceId);
1342 ASSERT_EQ(device->isEnabled(), false);
1343
1344 disableDevice(deviceId, device);
1345 mReader->loopOnce();
1346 mFakeListener->assertNotifyDeviceResetWasNotCalled();
1347 mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1348 ASSERT_EQ(device->isEnabled(), false);
1349
1350 enableDevice(deviceId, device);
1351 mReader->loopOnce();
1352 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1353 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1354 ASSERT_EQ(deviceId, resetArgs.deviceId);
1355 ASSERT_EQ(device->isEnabled(), true);
1356}
1357
Michael Wrightd02c5b62014-02-10 15:10:22 -08001358TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001359 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001360 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001361 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001362 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1363
1364 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1365 AINPUT_SOURCE_ANY, AKEYCODE_A))
1366 << "Should return unknown when the device id is >= 0 but unknown.";
1367
1368 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1369 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1370 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1371
1372 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1373 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1374 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1375
1376 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1377 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1378 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1379
1380 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1381 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1382 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1383}
1384
1385TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001386 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001387 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001388 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001389 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1390
1391 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1392 AINPUT_SOURCE_ANY, KEY_A))
1393 << "Should return unknown when the device id is >= 0 but unknown.";
1394
1395 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1396 AINPUT_SOURCE_TRACKBALL, KEY_A))
1397 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1398
1399 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1400 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1401 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1402
1403 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1404 AINPUT_SOURCE_TRACKBALL, KEY_A))
1405 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1406
1407 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1408 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1409 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1410}
1411
1412TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001413 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001414 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001415 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1417
1418 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1419 AINPUT_SOURCE_ANY, SW_LID))
1420 << "Should return unknown when the device id is >= 0 but unknown.";
1421
1422 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1423 AINPUT_SOURCE_TRACKBALL, SW_LID))
1424 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1425
1426 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1427 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1428 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1429
1430 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1431 AINPUT_SOURCE_TRACKBALL, SW_LID))
1432 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1433
1434 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1435 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1436 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1437}
1438
1439TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001440 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001441 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001442 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001443
Michael Wrightd02c5b62014-02-10 15:10:22 -08001444 mapper->addSupportedKeyCode(AKEYCODE_A);
1445 mapper->addSupportedKeyCode(AKEYCODE_B);
1446
1447 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1448 uint8_t flags[4] = { 0, 0, 0, 1 };
1449
1450 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1451 << "Should return false when device id is >= 0 but unknown.";
1452 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1453
1454 flags[3] = 1;
1455 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1456 << "Should return false when device id is valid but the sources are not supported by the device.";
1457 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1458
1459 flags[3] = 1;
1460 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1461 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1462 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1463
1464 flags[3] = 1;
1465 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1466 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1467 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1468
1469 flags[3] = 1;
1470 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1471 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1472 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1473}
1474
1475TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001476 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001477
1478 NotifyConfigurationChangedArgs args;
1479
1480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1481 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1482}
1483
1484TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001485 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001486 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001487 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001488
1489 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1490 mReader->loopOnce();
1491 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1492
1493 RawEvent event;
1494 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1495 ASSERT_EQ(0, event.when);
1496 ASSERT_EQ(1, event.deviceId);
1497 ASSERT_EQ(EV_KEY, event.type);
1498 ASSERT_EQ(KEY_A, event.code);
1499 ASSERT_EQ(1, event.value);
1500}
1501
Prabir Pradhan42611e02018-11-27 14:04:02 -08001502TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1503 constexpr int32_t deviceId = 1;
1504 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001505 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001506 // Must add at least one mapper or the device will be ignored!
1507 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1508 device->addMapper(mapper);
1509 mReader->setNextDevice(device);
1510 addDevice(deviceId, "fake", deviceClass, nullptr);
1511
1512 NotifyDeviceResetArgs resetArgs;
1513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1514 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1515
1516 disableDevice(deviceId, device);
1517 mReader->loopOnce();
1518 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1519 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1520 prevSequenceNum = resetArgs.sequenceNum;
1521
1522 enableDevice(deviceId, device);
1523 mReader->loopOnce();
1524 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1525 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1526 prevSequenceNum = resetArgs.sequenceNum;
1527
1528 disableDevice(deviceId, device);
1529 mReader->loopOnce();
1530 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1531 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1532 prevSequenceNum = resetArgs.sequenceNum;
1533}
1534
Arthur Hungc23540e2018-11-29 20:42:11 +08001535TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1536 constexpr int32_t deviceId = 1;
1537 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1538 const char* DEVICE_LOCATION = "USB1";
1539 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1540 DEVICE_LOCATION);
1541 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1542 device->addMapper(mapper);
1543 mReader->setNextDevice(device);
1544 addDevice(deviceId, "fake", deviceClass, nullptr);
1545
1546 const uint8_t hdmi1 = 1;
1547
1548 // Associated touch screen with second display.
1549 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1550
1551 // Add default and second display.
1552 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1553 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1554 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1555 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1556 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1557 mReader->loopOnce();
1558
Arthur Hung2c9a3342019-07-23 14:18:59 +08001559 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001560 ASSERT_EQ(deviceId, device->getId());
1561 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1562 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001563
1564 // Can't dispatch event from a disabled device.
1565 disableDevice(deviceId, device);
1566 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001567}
1568
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569
1570// --- InputDeviceTest ---
1571
1572class InputDeviceTest : public testing::Test {
1573protected:
1574 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001575 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001576 static const int32_t DEVICE_ID;
1577 static const int32_t DEVICE_GENERATION;
1578 static const int32_t DEVICE_CONTROLLER_NUMBER;
1579 static const uint32_t DEVICE_CLASSES;
1580
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001581 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001583 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584 FakeInputReaderContext* mFakeContext;
1585
1586 InputDevice* mDevice;
1587
1588 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001589 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001591 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1593
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001594 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595 InputDeviceIdentifier identifier;
1596 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001597 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1599 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1600 }
1601
1602 virtual void TearDown() {
1603 delete mDevice;
1604
1605 delete mFakeContext;
1606 mFakeListener.clear();
1607 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608 }
1609};
1610
1611const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001612const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613const int32_t InputDeviceTest::DEVICE_ID = 1;
1614const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1615const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1616const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1617 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1618
1619TEST_F(InputDeviceTest, ImmutableProperties) {
1620 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001621 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1623}
1624
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001625TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1626 ASSERT_EQ(mDevice->isEnabled(), true);
1627}
1628
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1630 // Configuration.
1631 InputReaderConfiguration config;
1632 mDevice->configure(ARBITRARY_TIME, &config, 0);
1633
1634 // Reset.
1635 mDevice->reset(ARBITRARY_TIME);
1636
1637 NotifyDeviceResetArgs resetArgs;
1638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1639 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1640 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1641
1642 // Metadata.
1643 ASSERT_TRUE(mDevice->isIgnored());
1644 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1645
1646 InputDeviceInfo info;
1647 mDevice->getDeviceInfo(&info);
1648 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001649 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1651 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1652
1653 // State queries.
1654 ASSERT_EQ(0, mDevice->getMetaState());
1655
1656 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1657 << "Ignored device should return unknown key code state.";
1658 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1659 << "Ignored device should return unknown scan code state.";
1660 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1661 << "Ignored device should return unknown switch state.";
1662
1663 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1664 uint8_t flags[2] = { 0, 1 };
1665 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1666 << "Ignored device should never mark any key codes.";
1667 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1668 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1669}
1670
1671TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1672 // Configuration.
1673 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1674
1675 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1676 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1677 mapper1->setMetaState(AMETA_ALT_ON);
1678 mapper1->addSupportedKeyCode(AKEYCODE_A);
1679 mapper1->addSupportedKeyCode(AKEYCODE_B);
1680 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1681 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1682 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1683 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1684 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1685 mDevice->addMapper(mapper1);
1686
1687 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1688 mapper2->setMetaState(AMETA_SHIFT_ON);
1689 mDevice->addMapper(mapper2);
1690
1691 InputReaderConfiguration config;
1692 mDevice->configure(ARBITRARY_TIME, &config, 0);
1693
1694 String8 propertyValue;
1695 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1696 << "Device should have read configuration during configuration phase.";
1697 ASSERT_STREQ("value", propertyValue.string());
1698
1699 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1700 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1701
1702 // Reset
1703 mDevice->reset(ARBITRARY_TIME);
1704 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1705 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1706
1707 NotifyDeviceResetArgs resetArgs;
1708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1709 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1710 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1711
1712 // Metadata.
1713 ASSERT_FALSE(mDevice->isIgnored());
1714 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1715
1716 InputDeviceInfo info;
1717 mDevice->getDeviceInfo(&info);
1718 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001719 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001720 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1721 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1722
1723 // State queries.
1724 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1725 << "Should query mappers and combine meta states.";
1726
1727 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1728 << "Should return unknown key code state when source not supported.";
1729 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1730 << "Should return unknown scan code state when source not supported.";
1731 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1732 << "Should return unknown switch state when source not supported.";
1733
1734 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1735 << "Should query mapper when source is supported.";
1736 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1737 << "Should query mapper when source is supported.";
1738 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1739 << "Should query mapper when source is supported.";
1740
1741 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1742 uint8_t flags[4] = { 0, 0, 0, 1 };
1743 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1744 << "Should do nothing when source is unsupported.";
1745 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1746 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1747 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1748 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1749
1750 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1751 << "Should query mapper when source is supported.";
1752 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1753 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1754 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1755 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1756
1757 // Event handling.
1758 RawEvent event;
1759 mDevice->process(&event, 1);
1760
1761 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1762 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1763}
1764
Arthur Hung2c9a3342019-07-23 14:18:59 +08001765// A single input device is associated with a specific display. Check that:
1766// 1. Device is disabled if the viewport corresponding to the associated display is not found
1767// 2. Device is disabled when setEnabled API is called
1768TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
1769 FakeInputMapper* mapper = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1770 mDevice->addMapper(mapper);
1771
1772 // First Configuration.
1773 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1774
1775 // Device should be enabled by default.
1776 ASSERT_TRUE(mDevice->isEnabled());
1777
1778 // Prepare associated info.
1779 constexpr uint8_t hdmi = 1;
1780 const std::string UNIQUE_ID = "local:1";
1781
1782 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1783 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1784 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1785 // Device should be disabled because it is associated with a specific display via
1786 // input port <-> display port association, but the corresponding display is not found
1787 ASSERT_FALSE(mDevice->isEnabled());
1788
1789 // Prepare displays.
1790 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1791 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1792 ViewportType::VIEWPORT_INTERNAL);
1793 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1794 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1795 ASSERT_TRUE(mDevice->isEnabled());
1796
1797 // Device should be disabled after set disable.
1798 mFakePolicy->addDisabledDevice(mDevice->getId());
1799 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1800 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1801 ASSERT_FALSE(mDevice->isEnabled());
1802
1803 // Device should still be disabled even found the associated display.
1804 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1805 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1806 ASSERT_FALSE(mDevice->isEnabled());
1807}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001808
1809// --- InputMapperTest ---
1810
1811class InputMapperTest : public testing::Test {
1812protected:
1813 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001814 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001815 static const int32_t DEVICE_ID;
1816 static const int32_t DEVICE_GENERATION;
1817 static const int32_t DEVICE_CONTROLLER_NUMBER;
1818 static const uint32_t DEVICE_CLASSES;
1819
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001820 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001821 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001822 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001823 FakeInputReaderContext* mFakeContext;
1824 InputDevice* mDevice;
1825
1826 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001827 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001828 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001829 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001830 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1831 InputDeviceIdentifier identifier;
1832 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001833 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001834 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1835 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1836
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001837 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001838 }
1839
1840 virtual void TearDown() {
1841 delete mDevice;
1842 delete mFakeContext;
1843 mFakeListener.clear();
1844 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001845 }
1846
1847 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001848 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849 }
1850
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001851 void configureDevice(uint32_t changes) {
1852 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1853 }
1854
Michael Wrightd02c5b62014-02-10 15:10:22 -08001855 void addMapperAndConfigure(InputMapper* mapper) {
1856 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001857 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001858 mDevice->reset(ARBITRARY_TIME);
1859 }
1860
1861 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001862 int32_t orientation, const std::string& uniqueId,
1863 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001864 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001865 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001866 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1867 }
1868
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001869 void clearViewports() {
1870 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001871 }
1872
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001873 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001874 int32_t code, int32_t value) {
1875 RawEvent event;
1876 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001877 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001878 event.type = type;
1879 event.code = code;
1880 event.value = value;
1881 mapper->process(&event);
1882 }
1883
1884 static void assertMotionRange(const InputDeviceInfo& info,
1885 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1886 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001887 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001888 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1889 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1890 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1891 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1892 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1893 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1894 }
1895
1896 static void assertPointerCoords(const PointerCoords& coords,
1897 float x, float y, float pressure, float size,
1898 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1899 float orientation, float distance) {
1900 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1901 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1902 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1903 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1904 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1905 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1906 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1907 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1908 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1909 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1910 }
1911
1912 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1913 float actualX, actualY;
1914 controller->getPosition(&actualX, &actualY);
1915 ASSERT_NEAR(x, actualX, 1);
1916 ASSERT_NEAR(y, actualY, 1);
1917 }
1918};
1919
1920const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001921const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001922const int32_t InputMapperTest::DEVICE_ID = 1;
1923const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1924const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1925const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1926
1927
1928// --- SwitchInputMapperTest ---
1929
1930class SwitchInputMapperTest : public InputMapperTest {
1931protected:
1932};
1933
1934TEST_F(SwitchInputMapperTest, GetSources) {
1935 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1936 addMapperAndConfigure(mapper);
1937
1938 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1939}
1940
1941TEST_F(SwitchInputMapperTest, GetSwitchState) {
1942 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1943 addMapperAndConfigure(mapper);
1944
1945 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1946 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1947
1948 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1949 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1950}
1951
1952TEST_F(SwitchInputMapperTest, Process) {
1953 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1954 addMapperAndConfigure(mapper);
1955
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001956 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
1957 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1958 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
1959 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001960
1961 NotifySwitchArgs args;
1962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1963 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08001964 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1965 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001966 args.switchMask);
1967 ASSERT_EQ(uint32_t(0), args.policyFlags);
1968}
1969
1970
1971// --- KeyboardInputMapperTest ---
1972
1973class KeyboardInputMapperTest : public InputMapperTest {
1974protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001975 const std::string UNIQUE_ID = "local:0";
1976
1977 void prepareDisplay(int32_t orientation);
1978
Arthur Hung2c9a3342019-07-23 14:18:59 +08001979 void testDPadKeyRotation(KeyboardInputMapper* mapper, int32_t originalScanCode,
1980 int32_t originalKeyCode, int32_t rotatedKeyCode,
1981 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001982};
1983
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001984/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
1985 * orientation.
1986 */
1987void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
1988 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001989 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001990}
1991
Michael Wrightd02c5b62014-02-10 15:10:22 -08001992void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08001993 int32_t originalScanCode, int32_t originalKeyCode,
1994 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001995 NotifyKeyArgs args;
1996
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001997 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1999 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2000 ASSERT_EQ(originalScanCode, args.scanCode);
2001 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002002 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002003
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002004 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2006 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2007 ASSERT_EQ(originalScanCode, args.scanCode);
2008 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002009 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002010}
2011
Michael Wrightd02c5b62014-02-10 15:10:22 -08002012TEST_F(KeyboardInputMapperTest, GetSources) {
2013 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2014 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2015 addMapperAndConfigure(mapper);
2016
2017 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
2018}
2019
2020TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2021 const int32_t USAGE_A = 0x070004;
2022 const int32_t USAGE_UNKNOWN = 0x07ffff;
2023 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2024 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2025
2026 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2027 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2028 addMapperAndConfigure(mapper);
2029
2030 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002031 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032 NotifyKeyArgs args;
2033 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_HOME, args.keyCode);
2039 ASSERT_EQ(KEY_HOME, 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 scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002046 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2048 ASSERT_EQ(DEVICE_ID, args.deviceId);
2049 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2050 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2051 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2052 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2053 ASSERT_EQ(KEY_HOME, args.scanCode);
2054 ASSERT_EQ(AMETA_NONE, args.metaState);
2055 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2056 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2057 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2058
2059 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002060 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2061 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2063 ASSERT_EQ(DEVICE_ID, args.deviceId);
2064 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2065 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2066 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2067 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2068 ASSERT_EQ(0, args.scanCode);
2069 ASSERT_EQ(AMETA_NONE, args.metaState);
2070 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2071 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2072 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2073
2074 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002075 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2076 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2078 ASSERT_EQ(DEVICE_ID, args.deviceId);
2079 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2080 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2081 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2082 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2083 ASSERT_EQ(0, args.scanCode);
2084 ASSERT_EQ(AMETA_NONE, args.metaState);
2085 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2086 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2087 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2088
2089 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002090 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2091 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2093 ASSERT_EQ(DEVICE_ID, args.deviceId);
2094 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2095 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2096 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2097 ASSERT_EQ(0, args.keyCode);
2098 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2099 ASSERT_EQ(AMETA_NONE, args.metaState);
2100 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2101 ASSERT_EQ(0U, args.policyFlags);
2102 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2103
2104 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002105 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2106 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2108 ASSERT_EQ(DEVICE_ID, args.deviceId);
2109 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2110 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2111 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2112 ASSERT_EQ(0, args.keyCode);
2113 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2114 ASSERT_EQ(AMETA_NONE, args.metaState);
2115 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2116 ASSERT_EQ(0U, args.policyFlags);
2117 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2118}
2119
2120TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2121 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2122 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2123
2124 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2125 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2126 addMapperAndConfigure(mapper);
2127
2128 // Initial metastate.
2129 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2130
2131 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002132 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133 NotifyKeyArgs args;
2134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2135 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2136 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2137 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2138
2139 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002140 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2142 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2144
2145 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002146 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2148 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2150
2151 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002152 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2154 ASSERT_EQ(AMETA_NONE, args.metaState);
2155 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2156 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2157}
2158
2159TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2160 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2161 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2162 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2163 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2164
2165 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2166 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2167 addMapperAndConfigure(mapper);
2168
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002169 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002170 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2171 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2172 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2173 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2174 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2175 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2176 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2177 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2178}
2179
2180TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2181 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2182 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2183 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2184 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2185
2186 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2187 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2188 addConfigurationProperty("keyboard.orientationAware", "1");
2189 addMapperAndConfigure(mapper);
2190
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002191 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002192 ASSERT_NO_FATAL_FAILURE(
2193 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2194 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2195 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2196 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2197 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2198 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2199 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002200
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002201 clearViewports();
2202 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002203 ASSERT_NO_FATAL_FAILURE(
2204 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2205 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2206 AKEYCODE_DPAD_UP, DISPLAY_ID));
2207 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2208 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2209 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2210 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002212 clearViewports();
2213 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002214 ASSERT_NO_FATAL_FAILURE(
2215 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2216 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2217 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2218 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2219 AKEYCODE_DPAD_UP, DISPLAY_ID));
2220 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2221 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002222
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002223 clearViewports();
2224 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002225 ASSERT_NO_FATAL_FAILURE(
2226 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2227 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2228 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2229 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2230 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2231 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2232 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233
2234 // Special case: if orientation changes while key is down, we still emit the same keycode
2235 // in the key up as we did in the key down.
2236 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002237 clearViewports();
2238 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002239 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2241 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2242 ASSERT_EQ(KEY_UP, args.scanCode);
2243 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2244
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002245 clearViewports();
2246 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002247 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2249 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2250 ASSERT_EQ(KEY_UP, args.scanCode);
2251 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2252}
2253
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002254TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2255 // If the keyboard is not orientation aware,
2256 // key events should not be associated with a specific display id
2257 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2258
2259 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2260 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2261 addMapperAndConfigure(mapper);
2262 NotifyKeyArgs args;
2263
2264 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002265 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002267 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2269 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2270
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002271 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002272 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002274 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2276 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2277}
2278
2279TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2280 // If the keyboard is orientation aware,
2281 // key events should be associated with the internal viewport
2282 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2283
2284 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2285 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2286 addConfigurationProperty("keyboard.orientationAware", "1");
2287 addMapperAndConfigure(mapper);
2288 NotifyKeyArgs args;
2289
2290 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2291 // ^--- already checked by the previous test
2292
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002293 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002294 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002295 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002297 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2299 ASSERT_EQ(DISPLAY_ID, args.displayId);
2300
2301 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002302 clearViewports();
2303 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002304 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002305 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002307 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2309 ASSERT_EQ(newDisplayId, args.displayId);
2310}
2311
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2313 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2314 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2315 addMapperAndConfigure(mapper);
2316
2317 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2318 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2319
2320 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2321 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2322}
2323
2324TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2325 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2326 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2327 addMapperAndConfigure(mapper);
2328
2329 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2330 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2331
2332 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2333 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2334}
2335
2336TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2337 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2338 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2339 addMapperAndConfigure(mapper);
2340
2341 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2342
2343 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2344 uint8_t flags[2] = { 0, 0 };
2345 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2346 ASSERT_TRUE(flags[0]);
2347 ASSERT_FALSE(flags[1]);
2348}
2349
2350TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2351 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2352 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2353 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2354 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2355 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2356 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2357
2358 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2359 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2360 addMapperAndConfigure(mapper);
2361
2362 // Initialization should have turned all of the lights off.
2363 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2364 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2365 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2366
2367 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002368 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2369 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2371 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2372 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2373 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2374
2375 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002376 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2377 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2379 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2380 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2381 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2382
2383 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002384 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2385 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2387 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2388 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2389 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2390
2391 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002392 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2393 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2395 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2396 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2397 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2398
2399 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002400 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2401 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2403 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2404 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2405 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2406
2407 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002408 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2409 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2411 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2412 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2413 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2414}
2415
Arthur Hung2c9a3342019-07-23 14:18:59 +08002416TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2417 // keyboard 1.
2418 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2419 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2420 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2421 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2422
2423 // keyboard 2.
2424 const std::string USB2 = "USB2";
2425 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2426 InputDeviceIdentifier identifier;
2427 identifier.name = "KEYBOARD2";
2428 identifier.location = USB2;
2429 std::unique_ptr<InputDevice> device2 =
2430 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2431 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2432 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2433 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2434 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2435 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2436 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2437
2438 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2439 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2440 addMapperAndConfigure(mapper);
2441
2442 KeyboardInputMapper* mapper2 = new KeyboardInputMapper(device2.get(), AINPUT_SOURCE_KEYBOARD,
2443 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2444 device2->addMapper(mapper2);
2445 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2446 device2->reset(ARBITRARY_TIME);
2447
2448 // Prepared displays and associated info.
2449 constexpr uint8_t hdmi1 = 0;
2450 constexpr uint8_t hdmi2 = 1;
2451 const std::string SECONDARY_UNIQUE_ID = "local:1";
2452
2453 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2454 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2455
2456 // No associated display viewport found, should disable the device.
2457 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2458 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2459 ASSERT_FALSE(device2->isEnabled());
2460
2461 // Prepare second display.
2462 constexpr int32_t newDisplayId = 2;
2463 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2464 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2465 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2466 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2467 // Default device will reconfigure above, need additional reconfiguration for another device.
2468 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2469 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2470
2471 // Device should be enabled after the associated display is found.
2472 ASSERT_TRUE(mDevice->isEnabled());
2473 ASSERT_TRUE(device2->isEnabled());
2474
2475 // Test pad key events
2476 ASSERT_NO_FATAL_FAILURE(
2477 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2478 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2479 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2480 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2481 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2482 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2483 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2484
2485 ASSERT_NO_FATAL_FAILURE(
2486 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2487 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2488 AKEYCODE_DPAD_RIGHT, newDisplayId));
2489 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2490 AKEYCODE_DPAD_DOWN, newDisplayId));
2491 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2492 AKEYCODE_DPAD_LEFT, newDisplayId));
2493}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494
2495// --- CursorInputMapperTest ---
2496
2497class CursorInputMapperTest : public InputMapperTest {
2498protected:
2499 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2500
2501 sp<FakePointerController> mFakePointerController;
2502
2503 virtual void SetUp() {
2504 InputMapperTest::SetUp();
2505
2506 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002507 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508 }
2509
2510 void testMotionRotation(CursorInputMapper* mapper,
2511 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002512
2513 void prepareDisplay(int32_t orientation) {
2514 const std::string uniqueId = "local:0";
2515 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2516 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2517 orientation, uniqueId, NO_PORT, viewportType);
2518 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002519};
2520
2521const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2522
2523void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2524 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2525 NotifyMotionArgs args;
2526
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002527 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2528 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2529 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2533 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2534 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2535 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2536}
2537
2538TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2539 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2540 addConfigurationProperty("cursor.mode", "pointer");
2541 addMapperAndConfigure(mapper);
2542
2543 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2544}
2545
2546TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2547 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2548 addConfigurationProperty("cursor.mode", "navigation");
2549 addMapperAndConfigure(mapper);
2550
2551 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2552}
2553
2554TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2555 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2556 addConfigurationProperty("cursor.mode", "pointer");
2557 addMapperAndConfigure(mapper);
2558
2559 InputDeviceInfo info;
2560 mapper->populateDeviceInfo(&info);
2561
2562 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002563 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2564 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002565 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2566 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2567
2568 // When the bounds are set, then there should be a valid motion range.
2569 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2570
2571 InputDeviceInfo info2;
2572 mapper->populateDeviceInfo(&info2);
2573
2574 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2575 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2576 1, 800 - 1, 0.0f, 0.0f));
2577 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2578 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2579 2, 480 - 1, 0.0f, 0.0f));
2580 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2581 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2582 0.0f, 1.0f, 0.0f, 0.0f));
2583}
2584
2585TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2586 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2587 addConfigurationProperty("cursor.mode", "navigation");
2588 addMapperAndConfigure(mapper);
2589
2590 InputDeviceInfo info;
2591 mapper->populateDeviceInfo(&info);
2592
2593 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2594 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2595 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2596 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2597 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2598 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2599 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2600 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2601 0.0f, 1.0f, 0.0f, 0.0f));
2602}
2603
2604TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2605 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2606 addConfigurationProperty("cursor.mode", "navigation");
2607 addMapperAndConfigure(mapper);
2608
2609 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2610
2611 NotifyMotionArgs args;
2612
2613 // Button press.
2614 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002615 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2616 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2618 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2619 ASSERT_EQ(DEVICE_ID, args.deviceId);
2620 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2621 ASSERT_EQ(uint32_t(0), args.policyFlags);
2622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2623 ASSERT_EQ(0, args.flags);
2624 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2625 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2626 ASSERT_EQ(0, args.edgeFlags);
2627 ASSERT_EQ(uint32_t(1), args.pointerCount);
2628 ASSERT_EQ(0, args.pointerProperties[0].id);
2629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2631 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2632 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2633 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2634 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2635
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2637 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2638 ASSERT_EQ(DEVICE_ID, args.deviceId);
2639 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2640 ASSERT_EQ(uint32_t(0), args.policyFlags);
2641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2642 ASSERT_EQ(0, args.flags);
2643 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2644 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2645 ASSERT_EQ(0, args.edgeFlags);
2646 ASSERT_EQ(uint32_t(1), args.pointerCount);
2647 ASSERT_EQ(0, args.pointerProperties[0].id);
2648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2650 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2651 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2652 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2653 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2654
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002656 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2657 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2659 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2660 ASSERT_EQ(DEVICE_ID, args.deviceId);
2661 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2662 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2664 ASSERT_EQ(0, args.flags);
2665 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2666 ASSERT_EQ(0, args.buttonState);
2667 ASSERT_EQ(0, args.edgeFlags);
2668 ASSERT_EQ(uint32_t(1), args.pointerCount);
2669 ASSERT_EQ(0, args.pointerProperties[0].id);
2670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2672 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2673 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2674 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2675 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2676
2677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2678 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2679 ASSERT_EQ(DEVICE_ID, args.deviceId);
2680 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2681 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2683 ASSERT_EQ(0, args.flags);
2684 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2685 ASSERT_EQ(0, args.buttonState);
2686 ASSERT_EQ(0, args.edgeFlags);
2687 ASSERT_EQ(uint32_t(1), args.pointerCount);
2688 ASSERT_EQ(0, args.pointerProperties[0].id);
2689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2691 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2692 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2693 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2694 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2695}
2696
2697TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2698 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2699 addConfigurationProperty("cursor.mode", "navigation");
2700 addMapperAndConfigure(mapper);
2701
2702 NotifyMotionArgs args;
2703
2704 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002705 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2706 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2710 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2711
2712 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002713 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2714 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2718 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2719}
2720
2721TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2722 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2723 addConfigurationProperty("cursor.mode", "navigation");
2724 addMapperAndConfigure(mapper);
2725
2726 NotifyMotionArgs args;
2727
2728 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002729 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2730 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2732 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2734 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2735
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2737 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2739 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2740
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002742 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2743 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002745 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2747 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2748
2749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002750 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2752 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2753}
2754
2755TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2756 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2757 addConfigurationProperty("cursor.mode", "navigation");
2758 addMapperAndConfigure(mapper);
2759
2760 NotifyMotionArgs args;
2761
2762 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002763 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2764 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2765 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2766 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2768 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2770 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2771 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2772
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2774 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2776 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2777 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2778
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002780 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2781 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2782 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2786 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2787 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2788
2789 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002790 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2791 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002793 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2795 0.0f, 0.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(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2800 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2801}
2802
2803TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2804 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2805 addConfigurationProperty("cursor.mode", "navigation");
2806 addMapperAndConfigure(mapper);
2807
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002808 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2810 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2811 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2812 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2813 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2814 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2815 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2816 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2817}
2818
2819TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2820 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2821 addConfigurationProperty("cursor.mode", "navigation");
2822 addConfigurationProperty("cursor.orientationAware", "1");
2823 addMapperAndConfigure(mapper);
2824
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002825 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2827 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2828 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2829 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2830 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2831 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2832 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2833 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2834
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002835 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2837 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2838 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2839 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2840 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2841 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2842 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2843 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2844
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002845 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2847 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2848 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2849 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2850 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2851 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2852 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2853 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2854
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002855 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2857 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2858 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2859 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2860 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2861 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2862 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2863 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2864}
2865
2866TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2867 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2868 addConfigurationProperty("cursor.mode", "pointer");
2869 addMapperAndConfigure(mapper);
2870
2871 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2872 mFakePointerController->setPosition(100, 200);
2873 mFakePointerController->setButtonState(0);
2874
2875 NotifyMotionArgs motionArgs;
2876 NotifyKeyArgs keyArgs;
2877
2878 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002879 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2880 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2882 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2883 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2884 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2886 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2887
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2890 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2891 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2893 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2894
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002895 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2896 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002898 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002899 ASSERT_EQ(0, motionArgs.buttonState);
2900 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2902 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2903
2904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002905 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002906 ASSERT_EQ(0, motionArgs.buttonState);
2907 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2909 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2910
2911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002913 ASSERT_EQ(0, motionArgs.buttonState);
2914 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2916 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2917
2918 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002919 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2920 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2921 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2924 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2925 motionArgs.buttonState);
2926 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2927 mFakePointerController->getButtonState());
2928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2929 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2930
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2932 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2933 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2934 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2935 mFakePointerController->getButtonState());
2936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2937 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2938
2939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2941 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2942 motionArgs.buttonState);
2943 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2944 mFakePointerController->getButtonState());
2945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2946 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2947
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002948 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
2949 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2953 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2955 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2956
2957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002959 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2960 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2962 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2963
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002964 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2965 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002967 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2968 ASSERT_EQ(0, motionArgs.buttonState);
2969 ASSERT_EQ(0, mFakePointerController->getButtonState());
2970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2971 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 -08002972 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2973 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002974
2975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976 ASSERT_EQ(0, motionArgs.buttonState);
2977 ASSERT_EQ(0, mFakePointerController->getButtonState());
2978 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2980 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 -08002981
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2983 ASSERT_EQ(0, motionArgs.buttonState);
2984 ASSERT_EQ(0, mFakePointerController->getButtonState());
2985 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2987 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2988
2989 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002990 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
2991 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2993 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2994 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002995
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2999 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3001 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3002
3003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3005 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3006 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3008 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3009
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003010 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3011 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
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_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 ASSERT_EQ(0, motionArgs.buttonState);
3015 ASSERT_EQ(0, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003021 ASSERT_EQ(0, motionArgs.buttonState);
3022 ASSERT_EQ(0, mFakePointerController->getButtonState());
3023
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3025 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3027 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3028 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3029
3030 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003031 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3032 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3034 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3035 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003036
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3040 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3042 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3043
3044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3046 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3047 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3049 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3050
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003051 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3052 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_EQ(0, motionArgs.buttonState);
3056 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3058 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 -08003059
3060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3061 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3062 ASSERT_EQ(0, motionArgs.buttonState);
3063 ASSERT_EQ(0, mFakePointerController->getButtonState());
3064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3065 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3066
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3068 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3069 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3070
3071 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003072 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3073 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3075 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3076 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003077
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003079 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3081 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3083 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3084
3085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3087 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3088 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3090 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3091
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003092 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3093 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096 ASSERT_EQ(0, motionArgs.buttonState);
3097 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3099 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 -08003100
3101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3102 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3103 ASSERT_EQ(0, motionArgs.buttonState);
3104 ASSERT_EQ(0, mFakePointerController->getButtonState());
3105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3106 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3107
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3109 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3110 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3111
3112 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003113 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3114 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3116 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3117 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003118
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003120 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3122 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3124 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3125
3126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3128 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3129 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3131 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3132
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003133 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3134 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003136 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137 ASSERT_EQ(0, motionArgs.buttonState);
3138 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3140 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 -08003141
3142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3143 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3144 ASSERT_EQ(0, motionArgs.buttonState);
3145 ASSERT_EQ(0, mFakePointerController->getButtonState());
3146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3147 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3148
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3150 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3151 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3152}
3153
3154TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3155 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3156 addConfigurationProperty("cursor.mode", "pointer");
3157 addMapperAndConfigure(mapper);
3158
3159 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3160 mFakePointerController->setPosition(100, 200);
3161 mFakePointerController->setButtonState(0);
3162
3163 NotifyMotionArgs args;
3164
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003165 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3166 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3167 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003169 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3170 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3172 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3173 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3174}
3175
3176TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3177 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3178 addConfigurationProperty("cursor.mode", "pointer");
3179 mFakePolicy->setPointerCapture(true);
3180 addMapperAndConfigure(mapper);
3181
3182 NotifyDeviceResetArgs resetArgs;
3183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3184 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3185 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3186
3187 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3188 mFakePointerController->setPosition(100, 200);
3189 mFakePointerController->setButtonState(0);
3190
3191 NotifyMotionArgs args;
3192
3193 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003194 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3195 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3196 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3198 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3201 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3202 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3203
3204 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003205 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3206 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3208 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3209 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3211 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3213 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3216 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3217
3218 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003219 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3220 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3222 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3225 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3227 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3228 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3230 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3231
3232 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003233 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3234 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3235 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3237 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3240 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3241 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3242
3243 // Disable pointer capture and check that the device generation got bumped
3244 // and events are generated the usual way.
3245 const uint32_t generation = mFakeContext->getGeneration();
3246 mFakePolicy->setPointerCapture(false);
3247 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3248 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3249
3250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3251 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3252 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3253
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003254 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3255 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3256 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3258 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3261 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3262 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3263}
3264
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003265TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3266 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3267 addMapperAndConfigure(mapper);
3268
3269 // Setup PointerController for second display.
3270 constexpr int32_t SECOND_DISPLAY_ID = 1;
3271 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3272 mFakePointerController->setPosition(100, 200);
3273 mFakePointerController->setButtonState(0);
3274 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3275
3276 NotifyMotionArgs args;
3277 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3278 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3279 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3281 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3282 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3284 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3285 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3286 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3287}
3288
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289
3290// --- TouchInputMapperTest ---
3291
3292class TouchInputMapperTest : public InputMapperTest {
3293protected:
3294 static const int32_t RAW_X_MIN;
3295 static const int32_t RAW_X_MAX;
3296 static const int32_t RAW_Y_MIN;
3297 static const int32_t RAW_Y_MAX;
3298 static const int32_t RAW_TOUCH_MIN;
3299 static const int32_t RAW_TOUCH_MAX;
3300 static const int32_t RAW_TOOL_MIN;
3301 static const int32_t RAW_TOOL_MAX;
3302 static const int32_t RAW_PRESSURE_MIN;
3303 static const int32_t RAW_PRESSURE_MAX;
3304 static const int32_t RAW_ORIENTATION_MIN;
3305 static const int32_t RAW_ORIENTATION_MAX;
3306 static const int32_t RAW_DISTANCE_MIN;
3307 static const int32_t RAW_DISTANCE_MAX;
3308 static const int32_t RAW_TILT_MIN;
3309 static const int32_t RAW_TILT_MAX;
3310 static const int32_t RAW_ID_MIN;
3311 static const int32_t RAW_ID_MAX;
3312 static const int32_t RAW_SLOT_MIN;
3313 static const int32_t RAW_SLOT_MAX;
3314 static const float X_PRECISION;
3315 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003316 static const float X_PRECISION_VIRTUAL;
3317 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318
3319 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003320 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321
3322 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3323
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003324 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003325 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003326
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 enum Axes {
3328 POSITION = 1 << 0,
3329 TOUCH = 1 << 1,
3330 TOOL = 1 << 2,
3331 PRESSURE = 1 << 3,
3332 ORIENTATION = 1 << 4,
3333 MINOR = 1 << 5,
3334 ID = 1 << 6,
3335 DISTANCE = 1 << 7,
3336 TILT = 1 << 8,
3337 SLOT = 1 << 9,
3338 TOOL_TYPE = 1 << 10,
3339 };
3340
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003341 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3342 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003343 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003345 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 int32_t toRawX(float displayX);
3347 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003348 float toCookedX(float rawX, float rawY);
3349 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003351 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003353 float toDisplayY(int32_t rawY, int32_t displayHeight);
3354
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355};
3356
3357const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3358const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3359const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3360const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3361const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3362const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3363const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3364const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003365const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3366const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3368const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3369const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3370const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3371const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3372const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3373const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3374const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3375const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3376const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3377const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3378const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003379const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3380 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3381const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3382 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003383const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3384 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
3386const float TouchInputMapperTest::GEOMETRIC_SCALE =
3387 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3388 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3389
3390const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3391 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3392 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3393};
3394
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003395void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003396 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003397 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3398}
3399
3400void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3401 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3402 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403}
3404
Santos Cordonfa5cf462017-04-05 10:37:00 -07003405void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003406 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3407 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003408 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003409}
3410
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411void TouchInputMapperTest::prepareVirtualKeys() {
3412 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3413 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3414 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3415 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3416}
3417
Jason Gerecke489fda82012-09-07 17:19:40 -07003418void TouchInputMapperTest::prepareLocationCalibration() {
3419 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3420}
3421
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422int32_t TouchInputMapperTest::toRawX(float displayX) {
3423 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3424}
3425
3426int32_t TouchInputMapperTest::toRawY(float displayY) {
3427 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3428}
3429
Jason Gerecke489fda82012-09-07 17:19:40 -07003430float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3431 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3432 return rawX;
3433}
3434
3435float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3436 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3437 return rawY;
3438}
3439
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003441 return toDisplayX(rawX, DISPLAY_WIDTH);
3442}
3443
3444float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3445 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446}
3447
3448float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003449 return toDisplayY(rawY, DISPLAY_HEIGHT);
3450}
3451
3452float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3453 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454}
3455
3456
3457// --- SingleTouchInputMapperTest ---
3458
3459class SingleTouchInputMapperTest : public TouchInputMapperTest {
3460protected:
3461 void prepareButtons();
3462 void prepareAxes(int axes);
3463
3464 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3465 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3466 void processUp(SingleTouchInputMapper* mappery);
3467 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3468 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3469 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3470 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3471 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3472 void processSync(SingleTouchInputMapper* mapper);
3473};
3474
3475void SingleTouchInputMapperTest::prepareButtons() {
3476 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3477}
3478
3479void SingleTouchInputMapperTest::prepareAxes(int axes) {
3480 if (axes & POSITION) {
3481 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3482 RAW_X_MIN, RAW_X_MAX, 0, 0);
3483 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3484 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3485 }
3486 if (axes & PRESSURE) {
3487 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3488 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3489 }
3490 if (axes & TOOL) {
3491 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3492 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3493 }
3494 if (axes & DISTANCE) {
3495 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3496 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3497 }
3498 if (axes & TILT) {
3499 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3500 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3501 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3502 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3503 }
3504}
3505
3506void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003507 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3508 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3509 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510}
3511
3512void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003513 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3514 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515}
3516
3517void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003518 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519}
3520
3521void SingleTouchInputMapperTest::processPressure(
3522 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003523 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524}
3525
3526void SingleTouchInputMapperTest::processToolMajor(
3527 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003528 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529}
3530
3531void SingleTouchInputMapperTest::processDistance(
3532 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003533 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534}
3535
3536void SingleTouchInputMapperTest::processTilt(
3537 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003538 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3539 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540}
3541
3542void SingleTouchInputMapperTest::processKey(
3543 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003544 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545}
3546
3547void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003548 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549}
3550
3551
3552TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3553 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3554 prepareButtons();
3555 prepareAxes(POSITION);
3556 addMapperAndConfigure(mapper);
3557
3558 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3559}
3560
3561TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3562 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3563 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3564 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3565 prepareButtons();
3566 prepareAxes(POSITION);
3567 addMapperAndConfigure(mapper);
3568
3569 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3570}
3571
3572TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3573 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3574 prepareButtons();
3575 prepareAxes(POSITION);
3576 addConfigurationProperty("touch.deviceType", "touchPad");
3577 addMapperAndConfigure(mapper);
3578
3579 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3580}
3581
3582TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3583 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3584 prepareButtons();
3585 prepareAxes(POSITION);
3586 addConfigurationProperty("touch.deviceType", "touchScreen");
3587 addMapperAndConfigure(mapper);
3588
3589 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3590}
3591
3592TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3593 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3594 addConfigurationProperty("touch.deviceType", "touchScreen");
3595 prepareDisplay(DISPLAY_ORIENTATION_0);
3596 prepareButtons();
3597 prepareAxes(POSITION);
3598 prepareVirtualKeys();
3599 addMapperAndConfigure(mapper);
3600
3601 // Unknown key.
3602 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3603
3604 // Virtual key is down.
3605 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3606 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3607 processDown(mapper, x, y);
3608 processSync(mapper);
3609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3610
3611 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3612
3613 // Virtual key is up.
3614 processUp(mapper);
3615 processSync(mapper);
3616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3617
3618 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3619}
3620
3621TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3622 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3623 addConfigurationProperty("touch.deviceType", "touchScreen");
3624 prepareDisplay(DISPLAY_ORIENTATION_0);
3625 prepareButtons();
3626 prepareAxes(POSITION);
3627 prepareVirtualKeys();
3628 addMapperAndConfigure(mapper);
3629
3630 // Unknown key.
3631 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3632
3633 // Virtual key is down.
3634 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3635 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3636 processDown(mapper, x, y);
3637 processSync(mapper);
3638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3639
3640 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3641
3642 // Virtual key is up.
3643 processUp(mapper);
3644 processSync(mapper);
3645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3646
3647 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3648}
3649
3650TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3651 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3652 addConfigurationProperty("touch.deviceType", "touchScreen");
3653 prepareDisplay(DISPLAY_ORIENTATION_0);
3654 prepareButtons();
3655 prepareAxes(POSITION);
3656 prepareVirtualKeys();
3657 addMapperAndConfigure(mapper);
3658
3659 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3660 uint8_t flags[2] = { 0, 0 };
3661 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3662 ASSERT_TRUE(flags[0]);
3663 ASSERT_FALSE(flags[1]);
3664}
3665
3666TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3667 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3668 addConfigurationProperty("touch.deviceType", "touchScreen");
3669 prepareDisplay(DISPLAY_ORIENTATION_0);
3670 prepareButtons();
3671 prepareAxes(POSITION);
3672 prepareVirtualKeys();
3673 addMapperAndConfigure(mapper);
3674
3675 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3676
3677 NotifyKeyArgs args;
3678
3679 // Press virtual key.
3680 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3681 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3682 processDown(mapper, x, y);
3683 processSync(mapper);
3684
3685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3686 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3687 ASSERT_EQ(DEVICE_ID, args.deviceId);
3688 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3689 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3690 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3691 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3692 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3693 ASSERT_EQ(KEY_HOME, args.scanCode);
3694 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3695 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3696
3697 // Release virtual key.
3698 processUp(mapper);
3699 processSync(mapper);
3700
3701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3702 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3703 ASSERT_EQ(DEVICE_ID, args.deviceId);
3704 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3705 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3706 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3707 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3708 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3709 ASSERT_EQ(KEY_HOME, args.scanCode);
3710 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3711 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3712
3713 // Should not have sent any motions.
3714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3715}
3716
3717TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3718 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3719 addConfigurationProperty("touch.deviceType", "touchScreen");
3720 prepareDisplay(DISPLAY_ORIENTATION_0);
3721 prepareButtons();
3722 prepareAxes(POSITION);
3723 prepareVirtualKeys();
3724 addMapperAndConfigure(mapper);
3725
3726 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3727
3728 NotifyKeyArgs keyArgs;
3729
3730 // Press virtual key.
3731 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3732 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3733 processDown(mapper, x, y);
3734 processSync(mapper);
3735
3736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3737 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3738 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3739 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3740 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3741 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3742 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3743 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3744 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3746 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3747
3748 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3749 // into the display area.
3750 y -= 100;
3751 processMove(mapper, x, y);
3752 processSync(mapper);
3753
3754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3755 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3756 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3757 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3758 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3759 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3760 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3761 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3762 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3763 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3764 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3765 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3766
3767 NotifyMotionArgs motionArgs;
3768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3769 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3770 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3771 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3772 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3773 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3774 ASSERT_EQ(0, motionArgs.flags);
3775 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3776 ASSERT_EQ(0, motionArgs.buttonState);
3777 ASSERT_EQ(0, motionArgs.edgeFlags);
3778 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3782 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3783 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3784 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3785 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3786
3787 // Keep moving out of bounds. Should generate a pointer move.
3788 y -= 50;
3789 processMove(mapper, x, y);
3790 processSync(mapper);
3791
3792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3793 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3794 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3795 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3796 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3798 ASSERT_EQ(0, motionArgs.flags);
3799 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3800 ASSERT_EQ(0, motionArgs.buttonState);
3801 ASSERT_EQ(0, motionArgs.edgeFlags);
3802 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3803 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3806 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3807 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3808 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3810
3811 // Release out of bounds. Should generate a pointer up.
3812 processUp(mapper);
3813 processSync(mapper);
3814
3815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3817 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3818 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3819 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3820 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3821 ASSERT_EQ(0, motionArgs.flags);
3822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3823 ASSERT_EQ(0, motionArgs.buttonState);
3824 ASSERT_EQ(0, motionArgs.edgeFlags);
3825 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3826 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3829 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3830 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3831 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3832 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3833
3834 // Should not have sent any more keys or motions.
3835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3837}
3838
3839TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3840 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3841 addConfigurationProperty("touch.deviceType", "touchScreen");
3842 prepareDisplay(DISPLAY_ORIENTATION_0);
3843 prepareButtons();
3844 prepareAxes(POSITION);
3845 prepareVirtualKeys();
3846 addMapperAndConfigure(mapper);
3847
3848 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3849
3850 NotifyMotionArgs motionArgs;
3851
3852 // Initially go down out of bounds.
3853 int32_t x = -10;
3854 int32_t y = -10;
3855 processDown(mapper, x, y);
3856 processSync(mapper);
3857
3858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3859
3860 // Move into the display area. Should generate a pointer down.
3861 x = 50;
3862 y = 75;
3863 processMove(mapper, x, y);
3864 processSync(mapper);
3865
3866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3867 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3868 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3869 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3870 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3871 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3872 ASSERT_EQ(0, motionArgs.flags);
3873 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3874 ASSERT_EQ(0, motionArgs.buttonState);
3875 ASSERT_EQ(0, motionArgs.edgeFlags);
3876 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3877 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3880 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3881 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3882 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3883 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3884
3885 // Release. Should generate a pointer up.
3886 processUp(mapper);
3887 processSync(mapper);
3888
3889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3890 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3891 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3892 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3893 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3894 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3895 ASSERT_EQ(0, motionArgs.flags);
3896 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3897 ASSERT_EQ(0, motionArgs.buttonState);
3898 ASSERT_EQ(0, motionArgs.edgeFlags);
3899 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3900 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3903 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3904 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3905 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3906 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3907
3908 // Should not have sent any more keys or motions.
3909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3911}
3912
Santos Cordonfa5cf462017-04-05 10:37:00 -07003913TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3914 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3915 addConfigurationProperty("touch.deviceType", "touchScreen");
3916 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3917
3918 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3919 prepareButtons();
3920 prepareAxes(POSITION);
3921 prepareVirtualKeys();
3922 addMapperAndConfigure(mapper);
3923
3924 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3925
3926 NotifyMotionArgs motionArgs;
3927
3928 // Down.
3929 int32_t x = 100;
3930 int32_t y = 125;
3931 processDown(mapper, x, y);
3932 processSync(mapper);
3933
3934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3936 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3937 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3938 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3939 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3940 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3941 ASSERT_EQ(0, motionArgs.flags);
3942 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3943 ASSERT_EQ(0, motionArgs.buttonState);
3944 ASSERT_EQ(0, motionArgs.edgeFlags);
3945 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3946 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3949 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3950 1, 0, 0, 0, 0, 0, 0, 0));
3951 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3952 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3953 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3954
3955 // Move.
3956 x += 50;
3957 y += 75;
3958 processMove(mapper, x, y);
3959 processSync(mapper);
3960
3961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3962 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3963 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3964 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3965 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3966 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3968 ASSERT_EQ(0, motionArgs.flags);
3969 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3970 ASSERT_EQ(0, motionArgs.buttonState);
3971 ASSERT_EQ(0, motionArgs.edgeFlags);
3972 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3973 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3976 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3977 1, 0, 0, 0, 0, 0, 0, 0));
3978 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3979 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3980 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3981
3982 // Up.
3983 processUp(mapper);
3984 processSync(mapper);
3985
3986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3987 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3988 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3989 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3990 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3991 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3992 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3993 ASSERT_EQ(0, motionArgs.flags);
3994 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3995 ASSERT_EQ(0, motionArgs.buttonState);
3996 ASSERT_EQ(0, motionArgs.edgeFlags);
3997 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3998 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4001 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4002 1, 0, 0, 0, 0, 0, 0, 0));
4003 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4004 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4005 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4006
4007 // Should not have sent any more keys or motions.
4008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4010}
4011
Michael Wrightd02c5b62014-02-10 15:10:22 -08004012TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4013 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4014 addConfigurationProperty("touch.deviceType", "touchScreen");
4015 prepareDisplay(DISPLAY_ORIENTATION_0);
4016 prepareButtons();
4017 prepareAxes(POSITION);
4018 prepareVirtualKeys();
4019 addMapperAndConfigure(mapper);
4020
4021 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4022
4023 NotifyMotionArgs motionArgs;
4024
4025 // Down.
4026 int32_t x = 100;
4027 int32_t y = 125;
4028 processDown(mapper, x, y);
4029 processSync(mapper);
4030
4031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4032 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4033 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4034 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4035 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4036 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4037 ASSERT_EQ(0, motionArgs.flags);
4038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4039 ASSERT_EQ(0, motionArgs.buttonState);
4040 ASSERT_EQ(0, motionArgs.edgeFlags);
4041 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4042 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4045 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4046 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4047 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4049
4050 // Move.
4051 x += 50;
4052 y += 75;
4053 processMove(mapper, x, y);
4054 processSync(mapper);
4055
4056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4057 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4058 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4059 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4060 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4062 ASSERT_EQ(0, motionArgs.flags);
4063 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4064 ASSERT_EQ(0, motionArgs.buttonState);
4065 ASSERT_EQ(0, motionArgs.edgeFlags);
4066 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4067 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4070 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4071 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4072 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4073 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4074
4075 // Up.
4076 processUp(mapper);
4077 processSync(mapper);
4078
4079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4081 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4082 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4083 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4084 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4085 ASSERT_EQ(0, motionArgs.flags);
4086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4087 ASSERT_EQ(0, motionArgs.buttonState);
4088 ASSERT_EQ(0, motionArgs.edgeFlags);
4089 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4090 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4093 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4094 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4095 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4096 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4097
4098 // Should not have sent any more keys or motions.
4099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4101}
4102
4103TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4104 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4105 addConfigurationProperty("touch.deviceType", "touchScreen");
4106 prepareButtons();
4107 prepareAxes(POSITION);
4108 addConfigurationProperty("touch.orientationAware", "0");
4109 addMapperAndConfigure(mapper);
4110
4111 NotifyMotionArgs args;
4112
4113 // Rotation 90.
4114 prepareDisplay(DISPLAY_ORIENTATION_90);
4115 processDown(mapper, toRawX(50), toRawY(75));
4116 processSync(mapper);
4117
4118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4119 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4120 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4121
4122 processUp(mapper);
4123 processSync(mapper);
4124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4125}
4126
4127TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4128 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4129 addConfigurationProperty("touch.deviceType", "touchScreen");
4130 prepareButtons();
4131 prepareAxes(POSITION);
4132 addMapperAndConfigure(mapper);
4133
4134 NotifyMotionArgs args;
4135
4136 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004137 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 prepareDisplay(DISPLAY_ORIENTATION_0);
4139 processDown(mapper, toRawX(50), toRawY(75));
4140 processSync(mapper);
4141
4142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4143 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4144 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4145
4146 processUp(mapper);
4147 processSync(mapper);
4148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4149
4150 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004151 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 prepareDisplay(DISPLAY_ORIENTATION_90);
4153 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4154 processSync(mapper);
4155
4156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4157 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4158 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4159
4160 processUp(mapper);
4161 processSync(mapper);
4162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4163
4164 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004165 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 prepareDisplay(DISPLAY_ORIENTATION_180);
4167 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4168 processSync(mapper);
4169
4170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4171 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4172 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4173
4174 processUp(mapper);
4175 processSync(mapper);
4176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4177
4178 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004179 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 prepareDisplay(DISPLAY_ORIENTATION_270);
4181 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4182 processSync(mapper);
4183
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4185 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4186 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4187
4188 processUp(mapper);
4189 processSync(mapper);
4190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4191}
4192
4193TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4194 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4195 addConfigurationProperty("touch.deviceType", "touchScreen");
4196 prepareDisplay(DISPLAY_ORIENTATION_0);
4197 prepareButtons();
4198 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4199 addMapperAndConfigure(mapper);
4200
4201 // These calculations are based on the input device calibration documentation.
4202 int32_t rawX = 100;
4203 int32_t rawY = 200;
4204 int32_t rawPressure = 10;
4205 int32_t rawToolMajor = 12;
4206 int32_t rawDistance = 2;
4207 int32_t rawTiltX = 30;
4208 int32_t rawTiltY = 110;
4209
4210 float x = toDisplayX(rawX);
4211 float y = toDisplayY(rawY);
4212 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4213 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4214 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4215 float distance = float(rawDistance);
4216
4217 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4218 float tiltScale = M_PI / 180;
4219 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4220 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4221 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4222 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4223
4224 processDown(mapper, rawX, rawY);
4225 processPressure(mapper, rawPressure);
4226 processToolMajor(mapper, rawToolMajor);
4227 processDistance(mapper, rawDistance);
4228 processTilt(mapper, rawTiltX, rawTiltY);
4229 processSync(mapper);
4230
4231 NotifyMotionArgs args;
4232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4234 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4235 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4236}
4237
Jason Gerecke489fda82012-09-07 17:19:40 -07004238TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4239 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4240 addConfigurationProperty("touch.deviceType", "touchScreen");
4241 prepareDisplay(DISPLAY_ORIENTATION_0);
4242 prepareLocationCalibration();
4243 prepareButtons();
4244 prepareAxes(POSITION);
4245 addMapperAndConfigure(mapper);
4246
4247 int32_t rawX = 100;
4248 int32_t rawY = 200;
4249
4250 float x = toDisplayX(toCookedX(rawX, rawY));
4251 float y = toDisplayY(toCookedY(rawX, rawY));
4252
4253 processDown(mapper, rawX, rawY);
4254 processSync(mapper);
4255
4256 NotifyMotionArgs args;
4257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4259 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4260}
4261
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4263 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4264 addConfigurationProperty("touch.deviceType", "touchScreen");
4265 prepareDisplay(DISPLAY_ORIENTATION_0);
4266 prepareButtons();
4267 prepareAxes(POSITION);
4268 addMapperAndConfigure(mapper);
4269
4270 NotifyMotionArgs motionArgs;
4271 NotifyKeyArgs keyArgs;
4272
4273 processDown(mapper, 100, 200);
4274 processSync(mapper);
4275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4276 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4277 ASSERT_EQ(0, motionArgs.buttonState);
4278
4279 // press BTN_LEFT, release BTN_LEFT
4280 processKey(mapper, BTN_LEFT, 1);
4281 processSync(mapper);
4282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4284 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4285
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4288 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4289
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 processKey(mapper, BTN_LEFT, 0);
4291 processSync(mapper);
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295
4296 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299
4300 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4301 processKey(mapper, BTN_RIGHT, 1);
4302 processKey(mapper, BTN_MIDDLE, 1);
4303 processSync(mapper);
4304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4306 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4307 motionArgs.buttonState);
4308
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4311 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4312
4313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4315 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4316 motionArgs.buttonState);
4317
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 processKey(mapper, BTN_RIGHT, 0);
4319 processSync(mapper);
4320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
4324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327
4328 processKey(mapper, BTN_MIDDLE, 0);
4329 processSync(mapper);
4330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333
4334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337
4338 // press BTN_BACK, release BTN_BACK
4339 processKey(mapper, BTN_BACK, 1);
4340 processSync(mapper);
4341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4342 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4343 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4348
4349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4351 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352
4353 processKey(mapper, BTN_BACK, 0);
4354 processSync(mapper);
4355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361 ASSERT_EQ(0, motionArgs.buttonState);
4362
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4364 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4365 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4366
4367 // press BTN_SIDE, release BTN_SIDE
4368 processKey(mapper, BTN_SIDE, 1);
4369 processSync(mapper);
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4371 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4372 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004373
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004376 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4377
4378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4380 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381
4382 processKey(mapper, BTN_SIDE, 0);
4383 processSync(mapper);
4384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004387
4388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004390 ASSERT_EQ(0, motionArgs.buttonState);
4391
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4394 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4395
4396 // press BTN_FORWARD, release BTN_FORWARD
4397 processKey(mapper, BTN_FORWARD, 1);
4398 processSync(mapper);
4399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4400 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4401 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4406
4407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4409 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410
4411 processKey(mapper, BTN_FORWARD, 0);
4412 processSync(mapper);
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004414 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416
4417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419 ASSERT_EQ(0, motionArgs.buttonState);
4420
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4422 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4423 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4424
4425 // press BTN_EXTRA, release BTN_EXTRA
4426 processKey(mapper, BTN_EXTRA, 1);
4427 processSync(mapper);
4428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4429 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4430 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004431
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004434 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4435
4436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4438 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
4440 processKey(mapper, BTN_EXTRA, 0);
4441 processSync(mapper);
4442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004445
4446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004448 ASSERT_EQ(0, motionArgs.buttonState);
4449
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4451 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4452 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4453
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4455
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 // press BTN_STYLUS, release BTN_STYLUS
4457 processKey(mapper, BTN_STYLUS, 1);
4458 processSync(mapper);
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4462
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4465 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466
4467 processKey(mapper, BTN_STYLUS, 0);
4468 processSync(mapper);
4469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472
4473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004475 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476
4477 // press BTN_STYLUS2, release BTN_STYLUS2
4478 processKey(mapper, BTN_STYLUS2, 1);
4479 processSync(mapper);
4480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4483
4484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4485 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4486 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487
4488 processKey(mapper, BTN_STYLUS2, 0);
4489 processSync(mapper);
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493
4494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004496 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497
4498 // release touch
4499 processUp(mapper);
4500 processSync(mapper);
4501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4503 ASSERT_EQ(0, motionArgs.buttonState);
4504}
4505
4506TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4507 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4508 addConfigurationProperty("touch.deviceType", "touchScreen");
4509 prepareDisplay(DISPLAY_ORIENTATION_0);
4510 prepareButtons();
4511 prepareAxes(POSITION);
4512 addMapperAndConfigure(mapper);
4513
4514 NotifyMotionArgs motionArgs;
4515
4516 // default tool type is finger
4517 processDown(mapper, 100, 200);
4518 processSync(mapper);
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4520 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4522
4523 // eraser
4524 processKey(mapper, BTN_TOOL_RUBBER, 1);
4525 processSync(mapper);
4526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4529
4530 // stylus
4531 processKey(mapper, BTN_TOOL_RUBBER, 0);
4532 processKey(mapper, BTN_TOOL_PEN, 1);
4533 processSync(mapper);
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4537
4538 // brush
4539 processKey(mapper, BTN_TOOL_PEN, 0);
4540 processKey(mapper, BTN_TOOL_BRUSH, 1);
4541 processSync(mapper);
4542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4545
4546 // pencil
4547 processKey(mapper, BTN_TOOL_BRUSH, 0);
4548 processKey(mapper, BTN_TOOL_PENCIL, 1);
4549 processSync(mapper);
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4553
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004554 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 processKey(mapper, BTN_TOOL_PENCIL, 0);
4556 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4557 processSync(mapper);
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4561
4562 // mouse
4563 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4564 processKey(mapper, BTN_TOOL_MOUSE, 1);
4565 processSync(mapper);
4566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4569
4570 // lens
4571 processKey(mapper, BTN_TOOL_MOUSE, 0);
4572 processKey(mapper, BTN_TOOL_LENS, 1);
4573 processSync(mapper);
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4577
4578 // double-tap
4579 processKey(mapper, BTN_TOOL_LENS, 0);
4580 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4581 processSync(mapper);
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4585
4586 // triple-tap
4587 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4588 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4589 processSync(mapper);
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4593
4594 // quad-tap
4595 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4596 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4597 processSync(mapper);
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4601
4602 // finger
4603 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4604 processKey(mapper, BTN_TOOL_FINGER, 1);
4605 processSync(mapper);
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4609
4610 // stylus trumps finger
4611 processKey(mapper, BTN_TOOL_PEN, 1);
4612 processSync(mapper);
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4616
4617 // eraser trumps stylus
4618 processKey(mapper, BTN_TOOL_RUBBER, 1);
4619 processSync(mapper);
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4623
4624 // mouse trumps eraser
4625 processKey(mapper, BTN_TOOL_MOUSE, 1);
4626 processSync(mapper);
4627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4630
4631 // back to default tool type
4632 processKey(mapper, BTN_TOOL_MOUSE, 0);
4633 processKey(mapper, BTN_TOOL_RUBBER, 0);
4634 processKey(mapper, BTN_TOOL_PEN, 0);
4635 processKey(mapper, BTN_TOOL_FINGER, 0);
4636 processSync(mapper);
4637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4640}
4641
4642TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4643 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4644 addConfigurationProperty("touch.deviceType", "touchScreen");
4645 prepareDisplay(DISPLAY_ORIENTATION_0);
4646 prepareButtons();
4647 prepareAxes(POSITION);
4648 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4649 addMapperAndConfigure(mapper);
4650
4651 NotifyMotionArgs motionArgs;
4652
4653 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4654 processKey(mapper, BTN_TOOL_FINGER, 1);
4655 processMove(mapper, 100, 200);
4656 processSync(mapper);
4657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4660 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4661
4662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4665 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4666
4667 // move a little
4668 processMove(mapper, 150, 250);
4669 processSync(mapper);
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4673 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4674
4675 // down when BTN_TOUCH is pressed, pressure defaults to 1
4676 processKey(mapper, BTN_TOUCH, 1);
4677 processSync(mapper);
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4681 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4682
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4686 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4687
4688 // up when BTN_TOUCH is released, hover restored
4689 processKey(mapper, BTN_TOUCH, 0);
4690 processSync(mapper);
4691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4692 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4694 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4695
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4699 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4700
4701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4702 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4704 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4705
4706 // exit hover when pointer goes away
4707 processKey(mapper, BTN_TOOL_FINGER, 0);
4708 processSync(mapper);
4709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4710 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4712 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4713}
4714
4715TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4716 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4717 addConfigurationProperty("touch.deviceType", "touchScreen");
4718 prepareDisplay(DISPLAY_ORIENTATION_0);
4719 prepareButtons();
4720 prepareAxes(POSITION | PRESSURE);
4721 addMapperAndConfigure(mapper);
4722
4723 NotifyMotionArgs motionArgs;
4724
4725 // initially hovering because pressure is 0
4726 processDown(mapper, 100, 200);
4727 processPressure(mapper, 0);
4728 processSync(mapper);
4729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4730 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4732 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4733
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4737 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4738
4739 // move a little
4740 processMove(mapper, 150, 250);
4741 processSync(mapper);
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4745 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4746
4747 // down when pressure is non-zero
4748 processPressure(mapper, RAW_PRESSURE_MAX);
4749 processSync(mapper);
4750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4753 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4754
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4758 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4759
4760 // up when pressure becomes 0, hover restored
4761 processPressure(mapper, 0);
4762 processSync(mapper);
4763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4764 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4766 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4767
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4769 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4771 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4772
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4776 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4777
4778 // exit hover when pointer goes away
4779 processUp(mapper);
4780 processSync(mapper);
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4784 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4785}
4786
Dan Harmsaca28402018-12-17 13:55:20 -08004787
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788// --- MultiTouchInputMapperTest ---
4789
4790class MultiTouchInputMapperTest : public TouchInputMapperTest {
4791protected:
4792 void prepareAxes(int axes);
4793
4794 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4795 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4796 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4797 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4798 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4799 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4800 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4801 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4802 void processId(MultiTouchInputMapper* mapper, int32_t id);
4803 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4804 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4805 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4806 void processMTSync(MultiTouchInputMapper* mapper);
4807 void processSync(MultiTouchInputMapper* mapper);
4808};
4809
4810void MultiTouchInputMapperTest::prepareAxes(int axes) {
4811 if (axes & POSITION) {
4812 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4813 RAW_X_MIN, RAW_X_MAX, 0, 0);
4814 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4815 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4816 }
4817 if (axes & TOUCH) {
4818 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4819 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4820 if (axes & MINOR) {
4821 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4822 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4823 }
4824 }
4825 if (axes & TOOL) {
4826 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4827 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4828 if (axes & MINOR) {
4829 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4830 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4831 }
4832 }
4833 if (axes & ORIENTATION) {
4834 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4835 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4836 }
4837 if (axes & PRESSURE) {
4838 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4839 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4840 }
4841 if (axes & DISTANCE) {
4842 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4843 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4844 }
4845 if (axes & ID) {
4846 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4847 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4848 }
4849 if (axes & SLOT) {
4850 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4851 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4852 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4853 }
4854 if (axes & TOOL_TYPE) {
4855 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4856 0, MT_TOOL_MAX, 0, 0);
4857 }
4858}
4859
4860void MultiTouchInputMapperTest::processPosition(
4861 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004862 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4863 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864}
4865
4866void MultiTouchInputMapperTest::processTouchMajor(
4867 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004868 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869}
4870
4871void MultiTouchInputMapperTest::processTouchMinor(
4872 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004873 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874}
4875
4876void MultiTouchInputMapperTest::processToolMajor(
4877 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004878 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879}
4880
4881void MultiTouchInputMapperTest::processToolMinor(
4882 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004883 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884}
4885
4886void MultiTouchInputMapperTest::processOrientation(
4887 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004888 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889}
4890
4891void MultiTouchInputMapperTest::processPressure(
4892 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004893 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894}
4895
4896void MultiTouchInputMapperTest::processDistance(
4897 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004898 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899}
4900
4901void MultiTouchInputMapperTest::processId(
4902 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004903 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904}
4905
4906void MultiTouchInputMapperTest::processSlot(
4907 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004908 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909}
4910
4911void MultiTouchInputMapperTest::processToolType(
4912 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004913 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914}
4915
4916void MultiTouchInputMapperTest::processKey(
4917 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004918 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919}
4920
4921void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004922 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923}
4924
4925void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004926 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927}
4928
4929
4930TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4931 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4932 addConfigurationProperty("touch.deviceType", "touchScreen");
4933 prepareDisplay(DISPLAY_ORIENTATION_0);
4934 prepareAxes(POSITION);
4935 prepareVirtualKeys();
4936 addMapperAndConfigure(mapper);
4937
4938 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4939
4940 NotifyMotionArgs motionArgs;
4941
4942 // Two fingers down at once.
4943 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4944 processPosition(mapper, x1, y1);
4945 processMTSync(mapper);
4946 processPosition(mapper, x2, y2);
4947 processMTSync(mapper);
4948 processSync(mapper);
4949
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4951 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4952 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4953 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4954 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4955 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4956 ASSERT_EQ(0, motionArgs.flags);
4957 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4958 ASSERT_EQ(0, motionArgs.buttonState);
4959 ASSERT_EQ(0, motionArgs.edgeFlags);
4960 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4961 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4962 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4964 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4965 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4966 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4967 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4968
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4970 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4971 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4972 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4973 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4974 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4975 motionArgs.action);
4976 ASSERT_EQ(0, motionArgs.flags);
4977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4978 ASSERT_EQ(0, motionArgs.buttonState);
4979 ASSERT_EQ(0, motionArgs.edgeFlags);
4980 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4981 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4983 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4986 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4988 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4989 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4990 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4991 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4992
4993 // Move.
4994 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4995 processPosition(mapper, x1, y1);
4996 processMTSync(mapper);
4997 processPosition(mapper, x2, y2);
4998 processMTSync(mapper);
4999 processSync(mapper);
5000
5001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5002 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5003 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5004 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5005 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5007 ASSERT_EQ(0, motionArgs.flags);
5008 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5009 ASSERT_EQ(0, motionArgs.buttonState);
5010 ASSERT_EQ(0, motionArgs.edgeFlags);
5011 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5012 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5014 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5017 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5019 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5020 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5021 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5022 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5023
5024 // First finger up.
5025 x2 += 15; y2 -= 20;
5026 processPosition(mapper, x2, y2);
5027 processMTSync(mapper);
5028 processSync(mapper);
5029
5030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5031 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5032 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5033 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5034 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5035 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5036 motionArgs.action);
5037 ASSERT_EQ(0, motionArgs.flags);
5038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5039 ASSERT_EQ(0, motionArgs.buttonState);
5040 ASSERT_EQ(0, motionArgs.edgeFlags);
5041 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5042 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5044 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5047 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5049 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5050 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5051 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5052 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5053
5054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5055 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5056 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5057 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5058 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5060 ASSERT_EQ(0, motionArgs.flags);
5061 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5062 ASSERT_EQ(0, motionArgs.buttonState);
5063 ASSERT_EQ(0, motionArgs.edgeFlags);
5064 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5065 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5068 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5069 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5070 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5071 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5072
5073 // Move.
5074 x2 += 20; y2 -= 25;
5075 processPosition(mapper, x2, y2);
5076 processMTSync(mapper);
5077 processSync(mapper);
5078
5079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5081 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5082 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5083 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5085 ASSERT_EQ(0, motionArgs.flags);
5086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5087 ASSERT_EQ(0, motionArgs.buttonState);
5088 ASSERT_EQ(0, motionArgs.edgeFlags);
5089 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5090 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5093 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5094 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5095 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5096 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5097
5098 // New finger down.
5099 int32_t x3 = 700, y3 = 300;
5100 processPosition(mapper, x2, y2);
5101 processMTSync(mapper);
5102 processPosition(mapper, x3, y3);
5103 processMTSync(mapper);
5104 processSync(mapper);
5105
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5107 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5108 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5109 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5110 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5111 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5112 motionArgs.action);
5113 ASSERT_EQ(0, motionArgs.flags);
5114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5115 ASSERT_EQ(0, motionArgs.buttonState);
5116 ASSERT_EQ(0, motionArgs.edgeFlags);
5117 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5118 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5120 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5123 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5125 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5126 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5127 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5128 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5129
5130 // Second finger up.
5131 x3 += 30; y3 -= 20;
5132 processPosition(mapper, x3, y3);
5133 processMTSync(mapper);
5134 processSync(mapper);
5135
5136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5138 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5139 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5140 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5141 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5142 motionArgs.action);
5143 ASSERT_EQ(0, motionArgs.flags);
5144 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5145 ASSERT_EQ(0, motionArgs.buttonState);
5146 ASSERT_EQ(0, motionArgs.edgeFlags);
5147 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5148 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5150 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5153 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5155 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5156 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5157 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5158 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5159
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5161 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5162 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5163 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5164 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5166 ASSERT_EQ(0, motionArgs.flags);
5167 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5168 ASSERT_EQ(0, motionArgs.buttonState);
5169 ASSERT_EQ(0, motionArgs.edgeFlags);
5170 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5171 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5174 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5175 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5176 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5177 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5178
5179 // Last finger up.
5180 processMTSync(mapper);
5181 processSync(mapper);
5182
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5185 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5186 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5187 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5188 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5189 ASSERT_EQ(0, motionArgs.flags);
5190 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5191 ASSERT_EQ(0, motionArgs.buttonState);
5192 ASSERT_EQ(0, motionArgs.edgeFlags);
5193 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5194 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5195 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5197 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5198 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5199 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5200 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5201
5202 // Should not have sent any more keys or motions.
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5205}
5206
5207TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5208 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5209 addConfigurationProperty("touch.deviceType", "touchScreen");
5210 prepareDisplay(DISPLAY_ORIENTATION_0);
5211 prepareAxes(POSITION | ID);
5212 prepareVirtualKeys();
5213 addMapperAndConfigure(mapper);
5214
5215 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5216
5217 NotifyMotionArgs motionArgs;
5218
5219 // Two fingers down at once.
5220 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5221 processPosition(mapper, x1, y1);
5222 processId(mapper, 1);
5223 processMTSync(mapper);
5224 processPosition(mapper, x2, y2);
5225 processId(mapper, 2);
5226 processMTSync(mapper);
5227 processSync(mapper);
5228
5229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5230 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5231 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5232 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5235 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5236
5237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5238 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5239 motionArgs.action);
5240 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5241 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5243 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5246 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5248 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5249
5250 // Move.
5251 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5252 processPosition(mapper, x1, y1);
5253 processId(mapper, 1);
5254 processMTSync(mapper);
5255 processPosition(mapper, x2, y2);
5256 processId(mapper, 2);
5257 processMTSync(mapper);
5258 processSync(mapper);
5259
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5262 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5263 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5265 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5268 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5270 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5271
5272 // First finger up.
5273 x2 += 15; y2 -= 20;
5274 processPosition(mapper, x2, y2);
5275 processId(mapper, 2);
5276 processMTSync(mapper);
5277 processSync(mapper);
5278
5279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5280 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5281 motionArgs.action);
5282 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5285 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5288 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5290 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5291
5292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5294 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5295 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5298 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5299
5300 // Move.
5301 x2 += 20; y2 -= 25;
5302 processPosition(mapper, x2, y2);
5303 processId(mapper, 2);
5304 processMTSync(mapper);
5305 processSync(mapper);
5306
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5309 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5310 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5313 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5314
5315 // New finger down.
5316 int32_t x3 = 700, y3 = 300;
5317 processPosition(mapper, x2, y2);
5318 processId(mapper, 2);
5319 processMTSync(mapper);
5320 processPosition(mapper, x3, y3);
5321 processId(mapper, 3);
5322 processMTSync(mapper);
5323 processSync(mapper);
5324
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5326 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5327 motionArgs.action);
5328 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5329 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5331 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5334 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5336 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5337
5338 // Second finger up.
5339 x3 += 30; y3 -= 20;
5340 processPosition(mapper, x3, y3);
5341 processId(mapper, 3);
5342 processMTSync(mapper);
5343 processSync(mapper);
5344
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5346 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5347 motionArgs.action);
5348 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5349 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5351 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5354 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5356 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5357
5358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5360 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5361 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5364 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5365
5366 // Last finger up.
5367 processMTSync(mapper);
5368 processSync(mapper);
5369
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5371 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5376 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5377
5378 // Should not have sent any more keys or motions.
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5381}
5382
5383TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5384 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5385 addConfigurationProperty("touch.deviceType", "touchScreen");
5386 prepareDisplay(DISPLAY_ORIENTATION_0);
5387 prepareAxes(POSITION | ID | SLOT);
5388 prepareVirtualKeys();
5389 addMapperAndConfigure(mapper);
5390
5391 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5392
5393 NotifyMotionArgs motionArgs;
5394
5395 // Two fingers down at once.
5396 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5397 processPosition(mapper, x1, y1);
5398 processId(mapper, 1);
5399 processSlot(mapper, 1);
5400 processPosition(mapper, x2, y2);
5401 processId(mapper, 2);
5402 processSync(mapper);
5403
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5405 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5406 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5407 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5410 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5411
5412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5414 motionArgs.action);
5415 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5418 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5421 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5423 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5424
5425 // Move.
5426 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5427 processSlot(mapper, 0);
5428 processPosition(mapper, x1, y1);
5429 processSlot(mapper, 1);
5430 processPosition(mapper, x2, y2);
5431 processSync(mapper);
5432
5433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5435 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5436 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5438 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5441 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5443 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5444
5445 // First finger up.
5446 x2 += 15; y2 -= 20;
5447 processSlot(mapper, 0);
5448 processId(mapper, -1);
5449 processSlot(mapper, 1);
5450 processPosition(mapper, x2, y2);
5451 processSync(mapper);
5452
5453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5454 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5455 motionArgs.action);
5456 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5457 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5459 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5462 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5464 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5468 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5469 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5472 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5473
5474 // Move.
5475 x2 += 20; y2 -= 25;
5476 processPosition(mapper, x2, y2);
5477 processSync(mapper);
5478
5479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5481 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5482 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5485 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5486
5487 // New finger down.
5488 int32_t x3 = 700, y3 = 300;
5489 processPosition(mapper, x2, y2);
5490 processSlot(mapper, 0);
5491 processId(mapper, 3);
5492 processPosition(mapper, x3, y3);
5493 processSync(mapper);
5494
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5496 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5497 motionArgs.action);
5498 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5499 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5501 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5504 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5506 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5507
5508 // Second finger up.
5509 x3 += 30; y3 -= 20;
5510 processSlot(mapper, 1);
5511 processId(mapper, -1);
5512 processSlot(mapper, 0);
5513 processPosition(mapper, x3, y3);
5514 processSync(mapper);
5515
5516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5517 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5518 motionArgs.action);
5519 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5522 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5525 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5527 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5528
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5531 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5535 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5536
5537 // Last finger up.
5538 processId(mapper, -1);
5539 processSync(mapper);
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5542 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5543 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5547 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5548
5549 // Should not have sent any more keys or motions.
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5552}
5553
5554TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5555 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5556 addConfigurationProperty("touch.deviceType", "touchScreen");
5557 prepareDisplay(DISPLAY_ORIENTATION_0);
5558 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5559 addMapperAndConfigure(mapper);
5560
5561 // These calculations are based on the input device calibration documentation.
5562 int32_t rawX = 100;
5563 int32_t rawY = 200;
5564 int32_t rawTouchMajor = 7;
5565 int32_t rawTouchMinor = 6;
5566 int32_t rawToolMajor = 9;
5567 int32_t rawToolMinor = 8;
5568 int32_t rawPressure = 11;
5569 int32_t rawDistance = 0;
5570 int32_t rawOrientation = 3;
5571 int32_t id = 5;
5572
5573 float x = toDisplayX(rawX);
5574 float y = toDisplayY(rawY);
5575 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5576 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5577 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5578 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5579 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5580 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5581 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5582 float distance = float(rawDistance);
5583
5584 processPosition(mapper, rawX, rawY);
5585 processTouchMajor(mapper, rawTouchMajor);
5586 processTouchMinor(mapper, rawTouchMinor);
5587 processToolMajor(mapper, rawToolMajor);
5588 processToolMinor(mapper, rawToolMinor);
5589 processPressure(mapper, rawPressure);
5590 processOrientation(mapper, rawOrientation);
5591 processDistance(mapper, rawDistance);
5592 processId(mapper, id);
5593 processMTSync(mapper);
5594 processSync(mapper);
5595
5596 NotifyMotionArgs args;
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5598 ASSERT_EQ(0, args.pointerProperties[0].id);
5599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5600 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5601 orientation, distance));
5602}
5603
5604TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5605 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5606 addConfigurationProperty("touch.deviceType", "touchScreen");
5607 prepareDisplay(DISPLAY_ORIENTATION_0);
5608 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5609 addConfigurationProperty("touch.size.calibration", "geometric");
5610 addMapperAndConfigure(mapper);
5611
5612 // These calculations are based on the input device calibration documentation.
5613 int32_t rawX = 100;
5614 int32_t rawY = 200;
5615 int32_t rawTouchMajor = 140;
5616 int32_t rawTouchMinor = 120;
5617 int32_t rawToolMajor = 180;
5618 int32_t rawToolMinor = 160;
5619
5620 float x = toDisplayX(rawX);
5621 float y = toDisplayY(rawY);
5622 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5623 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5624 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5625 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5626 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5627
5628 processPosition(mapper, rawX, rawY);
5629 processTouchMajor(mapper, rawTouchMajor);
5630 processTouchMinor(mapper, rawTouchMinor);
5631 processToolMajor(mapper, rawToolMajor);
5632 processToolMinor(mapper, rawToolMinor);
5633 processMTSync(mapper);
5634 processSync(mapper);
5635
5636 NotifyMotionArgs args;
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5639 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5640}
5641
5642TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5643 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5644 addConfigurationProperty("touch.deviceType", "touchScreen");
5645 prepareDisplay(DISPLAY_ORIENTATION_0);
5646 prepareAxes(POSITION | TOUCH | TOOL);
5647 addConfigurationProperty("touch.size.calibration", "diameter");
5648 addConfigurationProperty("touch.size.scale", "10");
5649 addConfigurationProperty("touch.size.bias", "160");
5650 addConfigurationProperty("touch.size.isSummed", "1");
5651 addMapperAndConfigure(mapper);
5652
5653 // These calculations are based on the input device calibration documentation.
5654 // Note: We only provide a single common touch/tool value because the device is assumed
5655 // not to emit separate values for each pointer (isSummed = 1).
5656 int32_t rawX = 100;
5657 int32_t rawY = 200;
5658 int32_t rawX2 = 150;
5659 int32_t rawY2 = 250;
5660 int32_t rawTouchMajor = 5;
5661 int32_t rawToolMajor = 8;
5662
5663 float x = toDisplayX(rawX);
5664 float y = toDisplayY(rawY);
5665 float x2 = toDisplayX(rawX2);
5666 float y2 = toDisplayY(rawY2);
5667 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5668 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5669 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5670
5671 processPosition(mapper, rawX, rawY);
5672 processTouchMajor(mapper, rawTouchMajor);
5673 processToolMajor(mapper, rawToolMajor);
5674 processMTSync(mapper);
5675 processPosition(mapper, rawX2, rawY2);
5676 processTouchMajor(mapper, rawTouchMajor);
5677 processToolMajor(mapper, rawToolMajor);
5678 processMTSync(mapper);
5679 processSync(mapper);
5680
5681 NotifyMotionArgs args;
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5683 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5684
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5686 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5687 args.action);
5688 ASSERT_EQ(size_t(2), args.pointerCount);
5689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5690 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5692 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5693}
5694
5695TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5696 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5697 addConfigurationProperty("touch.deviceType", "touchScreen");
5698 prepareDisplay(DISPLAY_ORIENTATION_0);
5699 prepareAxes(POSITION | TOUCH | TOOL);
5700 addConfigurationProperty("touch.size.calibration", "area");
5701 addConfigurationProperty("touch.size.scale", "43");
5702 addConfigurationProperty("touch.size.bias", "3");
5703 addMapperAndConfigure(mapper);
5704
5705 // These calculations are based on the input device calibration documentation.
5706 int32_t rawX = 100;
5707 int32_t rawY = 200;
5708 int32_t rawTouchMajor = 5;
5709 int32_t rawToolMajor = 8;
5710
5711 float x = toDisplayX(rawX);
5712 float y = toDisplayY(rawY);
5713 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5714 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5715 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5716
5717 processPosition(mapper, rawX, rawY);
5718 processTouchMajor(mapper, rawTouchMajor);
5719 processToolMajor(mapper, rawToolMajor);
5720 processMTSync(mapper);
5721 processSync(mapper);
5722
5723 NotifyMotionArgs args;
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5726 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5727}
5728
5729TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5730 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5731 addConfigurationProperty("touch.deviceType", "touchScreen");
5732 prepareDisplay(DISPLAY_ORIENTATION_0);
5733 prepareAxes(POSITION | PRESSURE);
5734 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5735 addConfigurationProperty("touch.pressure.scale", "0.01");
5736 addMapperAndConfigure(mapper);
5737
Michael Wrightaa449c92017-12-13 21:21:43 +00005738 InputDeviceInfo info;
5739 mapper->populateDeviceInfo(&info);
5740 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5741 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5742 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5743
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744 // These calculations are based on the input device calibration documentation.
5745 int32_t rawX = 100;
5746 int32_t rawY = 200;
5747 int32_t rawPressure = 60;
5748
5749 float x = toDisplayX(rawX);
5750 float y = toDisplayY(rawY);
5751 float pressure = float(rawPressure) * 0.01f;
5752
5753 processPosition(mapper, rawX, rawY);
5754 processPressure(mapper, rawPressure);
5755 processMTSync(mapper);
5756 processSync(mapper);
5757
5758 NotifyMotionArgs args;
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5761 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5762}
5763
5764TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5765 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5766 addConfigurationProperty("touch.deviceType", "touchScreen");
5767 prepareDisplay(DISPLAY_ORIENTATION_0);
5768 prepareAxes(POSITION | ID | SLOT);
5769 addMapperAndConfigure(mapper);
5770
5771 NotifyMotionArgs motionArgs;
5772 NotifyKeyArgs keyArgs;
5773
5774 processId(mapper, 1);
5775 processPosition(mapper, 100, 200);
5776 processSync(mapper);
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5779 ASSERT_EQ(0, motionArgs.buttonState);
5780
5781 // press BTN_LEFT, release BTN_LEFT
5782 processKey(mapper, BTN_LEFT, 1);
5783 processSync(mapper);
5784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5786 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5787
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5790 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5791
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 processKey(mapper, BTN_LEFT, 0);
5793 processSync(mapper);
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005797
5798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005800 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005801
5802 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5803 processKey(mapper, BTN_RIGHT, 1);
5804 processKey(mapper, BTN_MIDDLE, 1);
5805 processSync(mapper);
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5808 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5809 motionArgs.buttonState);
5810
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5813 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5814
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5817 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5818 motionArgs.buttonState);
5819
Michael Wrightd02c5b62014-02-10 15:10:22 -08005820 processKey(mapper, BTN_RIGHT, 0);
5821 processSync(mapper);
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005824 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005825
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005828 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829
5830 processKey(mapper, BTN_MIDDLE, 0);
5831 processSync(mapper);
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005838 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839
5840 // press BTN_BACK, release BTN_BACK
5841 processKey(mapper, BTN_BACK, 1);
5842 processSync(mapper);
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5844 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5845 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005849 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5850
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5853 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854
5855 processKey(mapper, BTN_BACK, 0);
5856 processSync(mapper);
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005863 ASSERT_EQ(0, motionArgs.buttonState);
5864
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5866 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5867 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5868
5869 // press BTN_SIDE, release BTN_SIDE
5870 processKey(mapper, BTN_SIDE, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5873 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5874 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005875
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005878 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5879
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5882 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883
5884 processKey(mapper, BTN_SIDE, 0);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005889
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005892 ASSERT_EQ(0, motionArgs.buttonState);
5893
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5895 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5896 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5897
5898 // press BTN_FORWARD, release BTN_FORWARD
5899 processKey(mapper, BTN_FORWARD, 1);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5902 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5903 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005904
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005907 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5908
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5911 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912
5913 processKey(mapper, BTN_FORWARD, 0);
5914 processSync(mapper);
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005921 ASSERT_EQ(0, motionArgs.buttonState);
5922
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5924 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5925 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5926
5927 // press BTN_EXTRA, release BTN_EXTRA
5928 processKey(mapper, BTN_EXTRA, 1);
5929 processSync(mapper);
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5931 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5932 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005933
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005936 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5937
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5940 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941
5942 processKey(mapper, BTN_EXTRA, 0);
5943 processSync(mapper);
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005947
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005950 ASSERT_EQ(0, motionArgs.buttonState);
5951
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5953 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5954 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5955
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5957
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958 // press BTN_STYLUS, release BTN_STYLUS
5959 processKey(mapper, BTN_STYLUS, 1);
5960 processSync(mapper);
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5964
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5967 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968
5969 processKey(mapper, BTN_STYLUS, 0);
5970 processSync(mapper);
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005973 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005974
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005977 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978
5979 // press BTN_STYLUS2, release BTN_STYLUS2
5980 processKey(mapper, BTN_STYLUS2, 1);
5981 processSync(mapper);
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005984 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5985
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5988 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989
5990 processKey(mapper, BTN_STYLUS2, 0);
5991 processSync(mapper);
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005998 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999
6000 // release touch
6001 processId(mapper, -1);
6002 processSync(mapper);
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6004 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6005 ASSERT_EQ(0, motionArgs.buttonState);
6006}
6007
6008TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6009 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6010 addConfigurationProperty("touch.deviceType", "touchScreen");
6011 prepareDisplay(DISPLAY_ORIENTATION_0);
6012 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6013 addMapperAndConfigure(mapper);
6014
6015 NotifyMotionArgs motionArgs;
6016
6017 // default tool type is finger
6018 processId(mapper, 1);
6019 processPosition(mapper, 100, 200);
6020 processSync(mapper);
6021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6022 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6024
6025 // eraser
6026 processKey(mapper, BTN_TOOL_RUBBER, 1);
6027 processSync(mapper);
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6031
6032 // stylus
6033 processKey(mapper, BTN_TOOL_RUBBER, 0);
6034 processKey(mapper, BTN_TOOL_PEN, 1);
6035 processSync(mapper);
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6039
6040 // brush
6041 processKey(mapper, BTN_TOOL_PEN, 0);
6042 processKey(mapper, BTN_TOOL_BRUSH, 1);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6047
6048 // pencil
6049 processKey(mapper, BTN_TOOL_BRUSH, 0);
6050 processKey(mapper, BTN_TOOL_PENCIL, 1);
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_STYLUS, motionArgs.pointerProperties[0].toolType);
6055
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006056 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 processKey(mapper, BTN_TOOL_PENCIL, 0);
6058 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6059 processSync(mapper);
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6063
6064 // mouse
6065 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6066 processKey(mapper, BTN_TOOL_MOUSE, 1);
6067 processSync(mapper);
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6071
6072 // lens
6073 processKey(mapper, BTN_TOOL_MOUSE, 0);
6074 processKey(mapper, BTN_TOOL_LENS, 1);
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6079
6080 // double-tap
6081 processKey(mapper, BTN_TOOL_LENS, 0);
6082 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6083 processSync(mapper);
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6087
6088 // triple-tap
6089 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6090 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6091 processSync(mapper);
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6095
6096 // quad-tap
6097 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6098 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6103
6104 // finger
6105 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6106 processKey(mapper, BTN_TOOL_FINGER, 1);
6107 processSync(mapper);
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6111
6112 // stylus trumps finger
6113 processKey(mapper, BTN_TOOL_PEN, 1);
6114 processSync(mapper);
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6118
6119 // eraser trumps stylus
6120 processKey(mapper, BTN_TOOL_RUBBER, 1);
6121 processSync(mapper);
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6125
6126 // mouse trumps eraser
6127 processKey(mapper, BTN_TOOL_MOUSE, 1);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6132
6133 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6134 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6135 processSync(mapper);
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6139
6140 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6141 processToolType(mapper, MT_TOOL_PEN);
6142 processSync(mapper);
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6146
6147 // back to default tool type
6148 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6149 processKey(mapper, BTN_TOOL_MOUSE, 0);
6150 processKey(mapper, BTN_TOOL_RUBBER, 0);
6151 processKey(mapper, BTN_TOOL_PEN, 0);
6152 processKey(mapper, BTN_TOOL_FINGER, 0);
6153 processSync(mapper);
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6157}
6158
6159TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6160 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6161 addConfigurationProperty("touch.deviceType", "touchScreen");
6162 prepareDisplay(DISPLAY_ORIENTATION_0);
6163 prepareAxes(POSITION | ID | SLOT);
6164 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6165 addMapperAndConfigure(mapper);
6166
6167 NotifyMotionArgs motionArgs;
6168
6169 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6170 processId(mapper, 1);
6171 processPosition(mapper, 100, 200);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6176 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6177
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6179 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6181 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6182
6183 // move a little
6184 processPosition(mapper, 150, 250);
6185 processSync(mapper);
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6187 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6189 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6190
6191 // down when BTN_TOUCH is pressed, pressure defaults to 1
6192 processKey(mapper, BTN_TOUCH, 1);
6193 processSync(mapper);
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6195 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6197 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6198
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6202 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6203
6204 // up when BTN_TOUCH is released, hover restored
6205 processKey(mapper, BTN_TOUCH, 0);
6206 processSync(mapper);
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6210 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6211
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6215 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6216
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6220 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6221
6222 // exit hover when pointer goes away
6223 processId(mapper, -1);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6228 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6229}
6230
6231TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6232 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6233 addConfigurationProperty("touch.deviceType", "touchScreen");
6234 prepareDisplay(DISPLAY_ORIENTATION_0);
6235 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6236 addMapperAndConfigure(mapper);
6237
6238 NotifyMotionArgs motionArgs;
6239
6240 // initially hovering because pressure is 0
6241 processId(mapper, 1);
6242 processPosition(mapper, 100, 200);
6243 processPressure(mapper, 0);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6246 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6248 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6249
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6253 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6254
6255 // move a little
6256 processPosition(mapper, 150, 250);
6257 processSync(mapper);
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6259 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6261 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6262
6263 // down when pressure becomes non-zero
6264 processPressure(mapper, RAW_PRESSURE_MAX);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6269 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6270
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6274 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6275
6276 // up when pressure becomes 0, hover restored
6277 processPressure(mapper, 0);
6278 processSync(mapper);
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6282 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6283
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6287 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6292 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6293
6294 // exit hover when pointer goes away
6295 processId(mapper, -1);
6296 processSync(mapper);
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6300 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6301}
6302
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006303/**
6304 * Set the input device port <--> display port associations, and check that the
6305 * events are routed to the display that matches the display port.
6306 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6307 */
6308TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6309 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6310 const std::string usb2 = "USB2";
6311 const uint8_t hdmi1 = 0;
6312 const uint8_t hdmi2 = 1;
6313 const std::string secondaryUniqueId = "uniqueId2";
6314 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6315
6316 addConfigurationProperty("touch.deviceType", "touchScreen");
6317 prepareAxes(POSITION);
6318 addMapperAndConfigure(mapper);
6319
6320 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6321 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6322
6323 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6324 // for this input device is specified, and the matching viewport is not present,
6325 // the input device should be disabled (at the mapper level).
6326
6327 // Add viewport for display 2 on hdmi2
6328 prepareSecondaryDisplay(type, hdmi2);
6329 // Send a touch event
6330 processPosition(mapper, 100, 100);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6333
6334 // Add viewport for display 1 on hdmi1
6335 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6336 // Send a touch event again
6337 processPosition(mapper, 100, 100);
6338 processSync(mapper);
6339
6340 NotifyMotionArgs args;
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6342 ASSERT_EQ(DISPLAY_ID, args.displayId);
6343}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344
Arthur Hung41a712e2018-11-22 19:41:03 +08006345/**
6346 * Expect fallback to internal viewport if device is external and external viewport is not present.
6347 */
6348TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6349 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6350 prepareAxes(POSITION);
6351 addConfigurationProperty("touch.deviceType", "touchScreen");
6352 prepareDisplay(DISPLAY_ORIENTATION_0);
6353 mDevice->setExternal(true);
6354 addMapperAndConfigure(mapper);
6355
6356 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6357
6358 NotifyMotionArgs motionArgs;
6359
6360 // Expect the event to be sent to the internal viewport,
6361 // because an external viewport is not present.
6362 processPosition(mapper, 100, 100);
6363 processSync(mapper);
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6366
6367 // Expect the event to be sent to the external viewport if it is present.
6368 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6369 processPosition(mapper, 100, 100);
6370 processSync(mapper);
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6372 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6373}
6374
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006375TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6376 // Setup PointerController for second display.
6377 sp<FakePointerController> fakePointerController = new FakePointerController();
6378 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6379 fakePointerController->setPosition(100, 200);
6380 fakePointerController->setButtonState(0);
6381 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6382 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6383
6384 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6385 prepareDisplay(DISPLAY_ORIENTATION_0);
6386 prepareAxes(POSITION);
6387 addMapperAndConfigure(mapper);
6388
6389 // Check source is mouse that would obtain the PointerController.
6390 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6391
6392 NotifyMotionArgs motionArgs;
6393 processPosition(mapper, 100, 100);
6394 processSync(mapper);
6395
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6398 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6399}
6400
Arthur Hung7c645402019-01-25 17:45:42 +08006401TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6402 // Setup the first touch screen device.
6403 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6404 prepareAxes(POSITION | ID | SLOT);
6405 addConfigurationProperty("touch.deviceType", "touchScreen");
6406 addMapperAndConfigure(mapper);
6407
6408 // Create the second touch screen device, and enable multi fingers.
6409 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006410 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006411 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006412 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006413 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006414 std::unique_ptr<InputDevice> device2 =
6415 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6416 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006417 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6418 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6419 0 /*flat*/, 0 /*fuzz*/);
6420 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6421 0 /*flat*/, 0 /*fuzz*/);
6422 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6423 0 /*flat*/, 0 /*fuzz*/);
6424 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6425 0 /*flat*/, 0 /*fuzz*/);
6426 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6427 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6428 String8("touchScreen"));
6429
6430 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006431 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006432 device2->addMapper(mapper2);
6433 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6434 device2->reset(ARBITRARY_TIME);
6435
6436 // Setup PointerController.
6437 sp<FakePointerController> fakePointerController = new FakePointerController();
6438 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6439 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6440
6441 // Setup policy for associated displays and show touches.
6442 const uint8_t hdmi1 = 0;
6443 const uint8_t hdmi2 = 1;
6444 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6445 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6446 mFakePolicy->setShowTouches(true);
6447
6448 // Create displays.
6449 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6450 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6451
6452 // Default device will reconfigure above, need additional reconfiguration for another device.
6453 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6454 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6455
6456 // Two fingers down at default display.
6457 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6458 processPosition(mapper, x1, y1);
6459 processId(mapper, 1);
6460 processSlot(mapper, 1);
6461 processPosition(mapper, x2, y2);
6462 processId(mapper, 2);
6463 processSync(mapper);
6464
6465 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6466 fakePointerController->getSpots().find(DISPLAY_ID);
6467 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6468 ASSERT_EQ(size_t(2), iter->second.size());
6469
6470 // Two fingers down at second display.
6471 processPosition(mapper2, x1, y1);
6472 processId(mapper2, 1);
6473 processSlot(mapper2, 1);
6474 processPosition(mapper2, x2, y2);
6475 processId(mapper2, 2);
6476 processSync(mapper2);
6477
6478 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6479 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6480 ASSERT_EQ(size_t(2), iter->second.size());
6481}
6482
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006483TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6484 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6485 prepareAxes(POSITION);
6486 addConfigurationProperty("touch.deviceType", "touchScreen");
6487 prepareDisplay(DISPLAY_ORIENTATION_0);
6488 addMapperAndConfigure(mapper);
6489
6490 NotifyMotionArgs motionArgs;
6491 // Unrotated video frame
6492 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6493 std::vector<TouchVideoFrame> frames{frame};
6494 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6495 processPosition(mapper, 100, 200);
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(frames, motionArgs.videoFrames);
6499
6500 // Subsequent touch events should not have any videoframes
6501 // This is implemented separately in FakeEventHub,
6502 // but that should match the behaviour of TouchVideoDevice.
6503 processPosition(mapper, 200, 200);
6504 processSync(mapper);
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6507}
6508
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006509TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6510 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6511 prepareAxes(POSITION);
6512 addConfigurationProperty("touch.deviceType", "touchScreen");
6513 addMapperAndConfigure(mapper);
6514 // Unrotated video frame
6515 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6516 NotifyMotionArgs motionArgs;
6517
6518 // Test all 4 orientations
6519 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6520 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6521 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6522 clearViewports();
6523 prepareDisplay(orientation);
6524 std::vector<TouchVideoFrame> frames{frame};
6525 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6526 processPosition(mapper, 100, 200);
6527 processSync(mapper);
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6529 frames[0].rotate(orientation);
6530 ASSERT_EQ(frames, motionArgs.videoFrames);
6531 }
6532}
6533
6534TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6535 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6536 prepareAxes(POSITION);
6537 addConfigurationProperty("touch.deviceType", "touchScreen");
6538 addMapperAndConfigure(mapper);
6539 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6540 // so mix these.
6541 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6542 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6543 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6544 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6545 NotifyMotionArgs motionArgs;
6546
6547 prepareDisplay(DISPLAY_ORIENTATION_90);
6548 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6549 processPosition(mapper, 100, 200);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 std::for_each(frames.begin(), frames.end(),
6553 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6554 ASSERT_EQ(frames, motionArgs.videoFrames);
6555}
6556
Arthur Hung9da14732019-09-02 16:16:58 +08006557/**
6558 * If we had defined port associations, but the viewport is not ready, the touch device would be
6559 * expected to be disabled, and it should be enabled after the viewport has found.
6560 */
6561TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6562 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6563 constexpr uint8_t hdmi2 = 1;
6564 const std::string secondaryUniqueId = "uniqueId2";
6565 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6566
6567 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6568
6569 addConfigurationProperty("touch.deviceType", "touchScreen");
6570 prepareAxes(POSITION);
6571 addMapperAndConfigure(mapper);
6572
6573 ASSERT_EQ(mDevice->isEnabled(), false);
6574
6575 // Add display on hdmi2, the device should be enabled and can receive touch event.
6576 prepareSecondaryDisplay(type, hdmi2);
6577 ASSERT_EQ(mDevice->isEnabled(), true);
6578
6579 // Send a touch event.
6580 processPosition(mapper, 100, 100);
6581 processSync(mapper);
6582
6583 NotifyMotionArgs args;
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6585 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6586}
6587
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588} // namespace android