blob: 541de99623791aa83ad3fb305a7beac6cc75977c [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
328protected:
329 virtual ~FakeEventHub() {
330 for (size_t i = 0; i < mDevices.size(); i++) {
331 delete mDevices.valueAt(i);
332 }
333 }
334
335public:
336 FakeEventHub() { }
337
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100338 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339 Device* device = new Device(classes);
340 device->identifier.name = name;
341 mDevices.add(deviceId, device);
342
343 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
344 }
345
346 void removeDevice(int32_t deviceId) {
347 delete mDevices.valueFor(deviceId);
348 mDevices.removeItem(deviceId);
349
350 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
351 }
352
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700353 bool isDeviceEnabled(int32_t deviceId) {
354 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700355 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700356 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
357 return false;
358 }
359 return device->enabled;
360 }
361
362 status_t enableDevice(int32_t deviceId) {
363 status_t result;
364 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700365 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700366 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
367 return BAD_VALUE;
368 }
369 if (device->enabled) {
370 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
371 return OK;
372 }
373 result = device->enable();
374 return result;
375 }
376
377 status_t disableDevice(int32_t deviceId) {
378 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700379 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700380 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
381 return BAD_VALUE;
382 }
383 if (!device->enabled) {
384 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
385 return OK;
386 }
387 return device->disable();
388 }
389
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390 void finishDeviceScan() {
391 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
392 }
393
394 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
395 Device* device = getDevice(deviceId);
396 device->configuration.addProperty(key, value);
397 }
398
399 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
400 Device* device = getDevice(deviceId);
401 device->configuration.addAll(configuration);
402 }
403
404 void addAbsoluteAxis(int32_t deviceId, int axis,
405 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
406 Device* device = getDevice(deviceId);
407
408 RawAbsoluteAxisInfo info;
409 info.valid = true;
410 info.minValue = minValue;
411 info.maxValue = maxValue;
412 info.flat = flat;
413 info.fuzz = fuzz;
414 info.resolution = resolution;
415 device->absoluteAxes.add(axis, info);
416 }
417
418 void addRelativeAxis(int32_t deviceId, int32_t axis) {
419 Device* device = getDevice(deviceId);
420 device->relativeAxes.add(axis, true);
421 }
422
423 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
424 Device* device = getDevice(deviceId);
425 device->keyCodeStates.replaceValueFor(keyCode, state);
426 }
427
428 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
429 Device* device = getDevice(deviceId);
430 device->scanCodeStates.replaceValueFor(scanCode, state);
431 }
432
433 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
434 Device* device = getDevice(deviceId);
435 device->switchStates.replaceValueFor(switchCode, state);
436 }
437
438 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
439 Device* device = getDevice(deviceId);
440 device->absoluteAxisValue.replaceValueFor(axis, value);
441 }
442
443 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
444 int32_t keyCode, uint32_t flags) {
445 Device* device = getDevice(deviceId);
446 KeyInfo info;
447 info.keyCode = keyCode;
448 info.flags = flags;
449 if (scanCode) {
450 device->keysByScanCode.add(scanCode, info);
451 }
452 if (usageCode) {
453 device->keysByUsageCode.add(usageCode, info);
454 }
455 }
456
457 void addLed(int32_t deviceId, int32_t led, bool initialState) {
458 Device* device = getDevice(deviceId);
459 device->leds.add(led, initialState);
460 }
461
462 bool getLedState(int32_t deviceId, int32_t led) {
463 Device* device = getDevice(deviceId);
464 return device->leds.valueFor(led);
465 }
466
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100467 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468 return mExcludedDevices;
469 }
470
471 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
472 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800473 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474 }
475
476 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
477 int32_t code, int32_t value) {
478 RawEvent event;
479 event.when = when;
480 event.deviceId = deviceId;
481 event.type = type;
482 event.code = code;
483 event.value = value;
484 mEvents.push_back(event);
485
486 if (type == EV_ABS) {
487 setAbsoluteAxisValue(deviceId, code, value);
488 }
489 }
490
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600491 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
492 std::vector<TouchVideoFrame>> videoFrames) {
493 mVideoFrames = std::move(videoFrames);
494 }
495
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 void assertQueueIsEmpty() {
497 ASSERT_EQ(size_t(0), mEvents.size())
498 << "Expected the event queue to be empty (fully consumed).";
499 }
500
501private:
502 Device* getDevice(int32_t deviceId) const {
503 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100504 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505 }
506
507 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
508 Device* device = getDevice(deviceId);
509 return device ? device->classes : 0;
510 }
511
512 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
513 Device* device = getDevice(deviceId);
514 return device ? device->identifier : InputDeviceIdentifier();
515 }
516
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100517 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518 return 0;
519 }
520
521 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
522 Device* device = getDevice(deviceId);
523 if (device) {
524 *outConfiguration = device->configuration;
525 }
526 }
527
528 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
529 RawAbsoluteAxisInfo* outAxisInfo) const {
530 Device* device = getDevice(deviceId);
531 if (device) {
532 ssize_t index = device->absoluteAxes.indexOfKey(axis);
533 if (index >= 0) {
534 *outAxisInfo = device->absoluteAxes.valueAt(index);
535 return OK;
536 }
537 }
538 outAxisInfo->clear();
539 return -1;
540 }
541
542 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
543 Device* device = getDevice(deviceId);
544 if (device) {
545 return device->relativeAxes.indexOfKey(axis) >= 0;
546 }
547 return false;
548 }
549
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100550 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551 return false;
552 }
553
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700554 virtual status_t mapKey(int32_t deviceId,
555 int32_t scanCode, int32_t usageCode, int32_t metaState,
556 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 Device* device = getDevice(deviceId);
558 if (device) {
559 const KeyInfo* key = getKey(device, scanCode, usageCode);
560 if (key) {
561 if (outKeycode) {
562 *outKeycode = key->keyCode;
563 }
564 if (outFlags) {
565 *outFlags = key->flags;
566 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700567 if (outMetaState) {
568 *outMetaState = metaState;
569 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570 return OK;
571 }
572 }
573 return NAME_NOT_FOUND;
574 }
575
576 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
577 if (usageCode) {
578 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
579 if (index >= 0) {
580 return &device->keysByUsageCode.valueAt(index);
581 }
582 }
583 if (scanCode) {
584 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
585 if (index >= 0) {
586 return &device->keysByScanCode.valueAt(index);
587 }
588 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700589 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 }
591
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100592 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 return NAME_NOT_FOUND;
594 }
595
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100596 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 mExcludedDevices = devices;
598 }
599
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100600 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 if (mEvents.empty()) {
602 return 0;
603 }
604
605 *buffer = *mEvents.begin();
606 mEvents.erase(mEvents.begin());
607 return 1;
608 }
609
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800610 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600611 auto it = mVideoFrames.find(deviceId);
612 if (it != mVideoFrames.end()) {
613 std::vector<TouchVideoFrame> frames = std::move(it->second);
614 mVideoFrames.erase(deviceId);
615 return frames;
616 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800617 return {};
618 }
619
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
621 Device* device = getDevice(deviceId);
622 if (device) {
623 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
624 if (index >= 0) {
625 return device->scanCodeStates.valueAt(index);
626 }
627 }
628 return AKEY_STATE_UNKNOWN;
629 }
630
631 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
632 Device* device = getDevice(deviceId);
633 if (device) {
634 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
635 if (index >= 0) {
636 return device->keyCodeStates.valueAt(index);
637 }
638 }
639 return AKEY_STATE_UNKNOWN;
640 }
641
642 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
643 Device* device = getDevice(deviceId);
644 if (device) {
645 ssize_t index = device->switchStates.indexOfKey(sw);
646 if (index >= 0) {
647 return device->switchStates.valueAt(index);
648 }
649 }
650 return AKEY_STATE_UNKNOWN;
651 }
652
653 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
654 int32_t* outValue) const {
655 Device* device = getDevice(deviceId);
656 if (device) {
657 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
658 if (index >= 0) {
659 *outValue = device->absoluteAxisValue.valueAt(index);
660 return OK;
661 }
662 }
663 *outValue = 0;
664 return -1;
665 }
666
667 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
668 uint8_t* outFlags) const {
669 bool result = false;
670 Device* device = getDevice(deviceId);
671 if (device) {
672 for (size_t i = 0; i < numCodes; i++) {
673 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
674 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
675 outFlags[i] = 1;
676 result = true;
677 }
678 }
679 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
680 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
681 outFlags[i] = 1;
682 result = true;
683 }
684 }
685 }
686 }
687 return result;
688 }
689
690 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
691 Device* device = getDevice(deviceId);
692 if (device) {
693 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
694 return index >= 0;
695 }
696 return false;
697 }
698
699 virtual bool hasLed(int32_t deviceId, int32_t led) const {
700 Device* device = getDevice(deviceId);
701 return device && device->leds.indexOfKey(led) >= 0;
702 }
703
704 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
705 Device* device = getDevice(deviceId);
706 if (device) {
707 ssize_t index = device->leds.indexOfKey(led);
708 if (index >= 0) {
709 device->leds.replaceValueAt(led, on);
710 } else {
711 ADD_FAILURE()
712 << "Attempted to set the state of an LED that the EventHub declared "
713 "was not present. led=" << led;
714 }
715 }
716 }
717
718 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800719 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720 outVirtualKeys.clear();
721
722 Device* device = getDevice(deviceId);
723 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800724 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 }
726 }
727
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100728 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700729 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 }
731
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100732 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 return false;
734 }
735
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100736 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 }
738
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100739 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 }
741
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100742 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743 return false;
744 }
745
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800746 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 }
748
749 virtual void monitor() {
750 }
751
752 virtual void requestReopenDevices() {
753 }
754
755 virtual void wake() {
756 }
757};
758
759
760// --- FakeInputReaderContext ---
761
762class FakeInputReaderContext : public InputReaderContext {
763 sp<EventHubInterface> mEventHub;
764 sp<InputReaderPolicyInterface> mPolicy;
765 sp<InputListenerInterface> mListener;
766 int32_t mGlobalMetaState;
767 bool mUpdateGlobalMetaStateWasCalled;
768 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800769 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770
771public:
772 FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
773 const sp<InputReaderPolicyInterface>& policy,
774 const sp<InputListenerInterface>& listener) :
775 mEventHub(eventHub), mPolicy(policy), mListener(listener),
Prabir Pradhan42611e02018-11-27 14:04:02 -0800776 mGlobalMetaState(0), mNextSequenceNum(1) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 }
778
779 virtual ~FakeInputReaderContext() { }
780
781 void assertUpdateGlobalMetaStateWasCalled() {
782 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
783 << "Expected updateGlobalMetaState() to have been called.";
784 mUpdateGlobalMetaStateWasCalled = false;
785 }
786
787 void setGlobalMetaState(int32_t state) {
788 mGlobalMetaState = state;
789 }
790
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800791 uint32_t getGeneration() {
792 return mGeneration;
793 }
794
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795private:
796 virtual void updateGlobalMetaState() {
797 mUpdateGlobalMetaStateWasCalled = true;
798 }
799
800 virtual int32_t getGlobalMetaState() {
801 return mGlobalMetaState;
802 }
803
804 virtual EventHubInterface* getEventHub() {
805 return mEventHub.get();
806 }
807
808 virtual InputReaderPolicyInterface* getPolicy() {
809 return mPolicy.get();
810 }
811
812 virtual InputListenerInterface* getListener() {
813 return mListener.get();
814 }
815
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100816 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 }
818
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100819 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 return false;
821 }
822
823 virtual void fadePointer() {
824 }
825
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100826 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
828
829 virtual int32_t bumpGeneration() {
830 return ++mGeneration;
831 }
Michael Wright842500e2015-03-13 17:32:02 -0700832
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800833 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700834
835 }
836
837 virtual void dispatchExternalStylusState(const StylusState&) {
838
839 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800840
841 virtual uint32_t getNextSequenceNum() {
842 return mNextSequenceNum++;
843 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844};
845
846
847// --- FakeInputMapper ---
848
849class FakeInputMapper : public InputMapper {
850 uint32_t mSources;
851 int32_t mKeyboardType;
852 int32_t mMetaState;
853 KeyedVector<int32_t, int32_t> mKeyCodeStates;
854 KeyedVector<int32_t, int32_t> mScanCodeStates;
855 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800856 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857 RawEvent mLastEvent;
858
859 bool mConfigureWasCalled;
860 bool mResetWasCalled;
861 bool mProcessWasCalled;
862
Arthur Hungc23540e2018-11-29 20:42:11 +0800863 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864public:
865 FakeInputMapper(InputDevice* device, uint32_t sources) :
866 InputMapper(device),
867 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
868 mMetaState(0),
869 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
870 }
871
872 virtual ~FakeInputMapper() { }
873
874 void setKeyboardType(int32_t keyboardType) {
875 mKeyboardType = keyboardType;
876 }
877
878 void setMetaState(int32_t metaState) {
879 mMetaState = metaState;
880 }
881
882 void assertConfigureWasCalled() {
883 ASSERT_TRUE(mConfigureWasCalled)
884 << "Expected configure() to have been called.";
885 mConfigureWasCalled = false;
886 }
887
888 void assertResetWasCalled() {
889 ASSERT_TRUE(mResetWasCalled)
890 << "Expected reset() to have been called.";
891 mResetWasCalled = false;
892 }
893
Yi Kong9b14ac62018-07-17 13:48:38 -0700894 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 ASSERT_TRUE(mProcessWasCalled)
896 << "Expected process() to have been called.";
897 if (outLastEvent) {
898 *outLastEvent = mLastEvent;
899 }
900 mProcessWasCalled = false;
901 }
902
903 void setKeyCodeState(int32_t keyCode, int32_t state) {
904 mKeyCodeStates.replaceValueFor(keyCode, state);
905 }
906
907 void setScanCodeState(int32_t scanCode, int32_t state) {
908 mScanCodeStates.replaceValueFor(scanCode, state);
909 }
910
911 void setSwitchState(int32_t switchCode, int32_t state) {
912 mSwitchStates.replaceValueFor(switchCode, state);
913 }
914
915 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800916 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 }
918
919private:
920 virtual uint32_t getSources() {
921 return mSources;
922 }
923
924 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
925 InputMapper::populateDeviceInfo(deviceInfo);
926
927 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
928 deviceInfo->setKeyboardType(mKeyboardType);
929 }
930 }
931
Arthur Hungc23540e2018-11-29 20:42:11 +0800932 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800934
935 // Find the associated viewport if exist.
936 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
937 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
938 mViewport = config->getDisplayViewportByPort(*displayPort);
939 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 }
941
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100942 virtual void reset(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943 mResetWasCalled = true;
944 }
945
946 virtual void process(const RawEvent* rawEvent) {
947 mLastEvent = *rawEvent;
948 mProcessWasCalled = true;
949 }
950
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100951 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
953 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
954 }
955
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100956 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
958 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
959 }
960
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100961 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800962 ssize_t index = mSwitchStates.indexOfKey(switchCode);
963 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
964 }
965
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100966 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967 const int32_t* keyCodes, uint8_t* outFlags) {
968 bool result = false;
969 for (size_t i = 0; i < numCodes; i++) {
970 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
971 if (keyCodes[i] == mSupportedKeyCodes[j]) {
972 outFlags[i] = 1;
973 result = true;
974 }
975 }
976 }
977 return result;
978 }
979
980 virtual int32_t getMetaState() {
981 return mMetaState;
982 }
983
984 virtual void fadePointer() {
985 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800986
987 virtual std::optional<int32_t> getAssociatedDisplay() {
988 if (mViewport) {
989 return std::make_optional(mViewport->displayId);
990 }
991 return std::nullopt;
992 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993};
994
995
996// --- InstrumentedInputReader ---
997
998class InstrumentedInputReader : public InputReader {
999 InputDevice* mNextDevice;
1000
1001public:
1002 InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
1003 const sp<InputReaderPolicyInterface>& policy,
1004 const sp<InputListenerInterface>& listener) :
1005 InputReader(eventHub, policy, listener),
Yi Kong9b14ac62018-07-17 13:48:38 -07001006 mNextDevice(nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007 }
1008
1009 virtual ~InstrumentedInputReader() {
1010 if (mNextDevice) {
1011 delete mNextDevice;
1012 }
1013 }
1014
1015 void setNextDevice(InputDevice* device) {
1016 mNextDevice = device;
1017 }
1018
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001019 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Arthur Hungc23540e2018-11-29 20:42:11 +08001020 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021 InputDeviceIdentifier identifier;
1022 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001023 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024 int32_t generation = deviceId + 1;
1025 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
1026 classes);
1027 }
1028
1029protected:
1030 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
1031 const InputDeviceIdentifier& identifier, uint32_t classes) {
1032 if (mNextDevice) {
1033 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001034 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001035 return device;
1036 }
1037 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1038 }
1039
1040 friend class InputReaderTest;
1041};
1042
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001043// --- InputReaderPolicyTest ---
1044class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001045protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001046 sp<FakeInputReaderPolicy> mFakePolicy;
1047
1048 virtual void SetUp() {
1049 mFakePolicy = new FakeInputReaderPolicy();
1050 }
1051 virtual void TearDown() {
1052 mFakePolicy.clear();
1053 }
1054};
1055
1056/**
1057 * Check that empty set of viewports is an acceptable configuration.
1058 * Also try to get internal viewport two different ways - by type and by uniqueId.
1059 *
1060 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1061 * Such configuration is not currently allowed.
1062 */
1063TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001064 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001065
1066 // We didn't add any viewports yet, so there shouldn't be any.
1067 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001068 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001069 ASSERT_FALSE(internalViewport);
1070
1071 // Add an internal viewport, then clear it
1072 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001073 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001074
1075 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001076 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001077 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001078 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001079
1080 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001081 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001082 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001083 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001084
1085 mFakePolicy->clearViewports();
1086 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001087 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001088 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001089 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001090 ASSERT_FALSE(internalViewport);
1091}
1092
1093TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1094 const std::string internalUniqueId = "local:0";
1095 const std::string externalUniqueId = "local:1";
1096 const std::string virtualUniqueId1 = "virtual:2";
1097 const std::string virtualUniqueId2 = "virtual:3";
1098 constexpr int32_t virtualDisplayId1 = 2;
1099 constexpr int32_t virtualDisplayId2 = 3;
1100
1101 // Add an internal viewport
1102 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001103 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001104 // Add an external viewport
1105 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001106 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001107 // Add an virtual viewport
1108 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001109 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001110 // Add another virtual viewport
1111 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001112 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001113
1114 // Check matching by type for internal
1115 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001116 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001117 ASSERT_TRUE(internalViewport);
1118 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1119
1120 // Check matching by type for external
1121 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001122 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001123 ASSERT_TRUE(externalViewport);
1124 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1125
1126 // Check matching by uniqueId for virtual viewport #1
1127 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001128 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001129 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001130 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001131 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1132 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1133
1134 // Check matching by uniqueId for virtual viewport #2
1135 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001136 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001137 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001138 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001139 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1140 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1141}
1142
1143
1144/**
1145 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1146 * that lookup works by checking display id.
1147 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1148 */
1149TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1150 const std::string uniqueId1 = "uniqueId1";
1151 const std::string uniqueId2 = "uniqueId2";
1152 constexpr int32_t displayId1 = 2;
1153 constexpr int32_t displayId2 = 3;
1154
1155 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1156 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1157 for (const ViewportType& type : types) {
1158 mFakePolicy->clearViewports();
1159 // Add a viewport
1160 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001161 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001162 // Add another viewport
1163 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001164 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001165
1166 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001167 std::optional<DisplayViewport> viewport1 =
1168 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001169 ASSERT_TRUE(viewport1);
1170 ASSERT_EQ(displayId1, viewport1->displayId);
1171 ASSERT_EQ(type, viewport1->type);
1172
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001173 std::optional<DisplayViewport> viewport2 =
1174 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001175 ASSERT_TRUE(viewport2);
1176 ASSERT_EQ(displayId2, viewport2->displayId);
1177 ASSERT_EQ(type, viewport2->type);
1178
1179 // When there are multiple viewports of the same kind, and uniqueId is not specified
1180 // in the call to getDisplayViewport, then that situation is not supported.
1181 // The viewports can be stored in any order, so we cannot rely on the order, since that
1182 // is just implementation detail.
1183 // However, we can check that it still returns *a* viewport, we just cannot assert
1184 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001185 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001186 ASSERT_TRUE(someViewport);
1187 }
1188}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001190/**
1191 * Check getDisplayViewportByPort
1192 */
1193TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1194 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1195 const std::string uniqueId1 = "uniqueId1";
1196 const std::string uniqueId2 = "uniqueId2";
1197 constexpr int32_t displayId1 = 1;
1198 constexpr int32_t displayId2 = 2;
1199 const uint8_t hdmi1 = 0;
1200 const uint8_t hdmi2 = 1;
1201 const uint8_t hdmi3 = 2;
1202
1203 mFakePolicy->clearViewports();
1204 // Add a viewport that's associated with some display port that's not of interest.
1205 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1206 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1207 // Add another viewport, connected to HDMI1 port
1208 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1209 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1210
1211 // Check that correct display viewport was returned by comparing the display ports.
1212 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1213 ASSERT_TRUE(hdmi1Viewport);
1214 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1215 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1216
1217 // Check that we can still get the same viewport using the uniqueId
1218 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1219 ASSERT_TRUE(hdmi1Viewport);
1220 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1221 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1222 ASSERT_EQ(type, hdmi1Viewport->type);
1223
1224 // Check that we cannot find a port with "HDMI2", because we never added one
1225 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1226 ASSERT_FALSE(hdmi2Viewport);
1227}
1228
Michael Wrightd02c5b62014-02-10 15:10:22 -08001229// --- InputReaderTest ---
1230
1231class InputReaderTest : public testing::Test {
1232protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001233 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234 sp<FakeInputReaderPolicy> mFakePolicy;
1235 sp<FakeEventHub> mFakeEventHub;
1236 sp<InstrumentedInputReader> mReader;
1237
1238 virtual void SetUp() {
1239 mFakeEventHub = new FakeEventHub();
1240 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001241 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242
1243 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1244 }
1245
1246 virtual void TearDown() {
1247 mReader.clear();
1248
1249 mFakeListener.clear();
1250 mFakePolicy.clear();
1251 mFakeEventHub.clear();
1252 }
1253
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001254 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255 const PropertyMap* configuration) {
1256 mFakeEventHub->addDevice(deviceId, name, classes);
1257
1258 if (configuration) {
1259 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1260 }
1261 mFakeEventHub->finishDeviceScan();
1262 mReader->loopOnce();
1263 mReader->loopOnce();
1264 mFakeEventHub->assertQueueIsEmpty();
1265 }
1266
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001267 void disableDevice(int32_t deviceId, InputDevice* device) {
1268 mFakePolicy->addDisabledDevice(deviceId);
1269 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1270 }
1271
1272 void enableDevice(int32_t deviceId, InputDevice* device) {
1273 mFakePolicy->removeDisabledDevice(deviceId);
1274 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1275 }
1276
1277 void configureDevice(uint32_t changes, InputDevice* device) {
1278 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1279 }
1280
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001282 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 const PropertyMap* configuration) {
1284 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1285 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1286 device->addMapper(mapper);
1287 mReader->setNextDevice(device);
1288 addDevice(deviceId, name, classes, configuration);
1289 return mapper;
1290 }
1291};
1292
1293TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001294 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001295 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001296 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001297 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001298
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001299
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001300 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001301 mReader->getInputDevices(inputDevices);
1302
1303 ASSERT_EQ(1U, inputDevices.size());
1304 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001305 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1307 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1308 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1309
1310 // Should also have received a notification describing the new input devices.
1311 inputDevices = mFakePolicy->getInputDevices();
1312 ASSERT_EQ(1U, inputDevices.size());
1313 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001314 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001315 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1316 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1317 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1318}
1319
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001320TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1321 constexpr int32_t deviceId = 1;
1322 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001323 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001324 // Must add at least one mapper or the device will be ignored!
1325 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1326 device->addMapper(mapper);
1327 mReader->setNextDevice(device);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001328 addDevice(deviceId, "fake", deviceClass, nullptr);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001329
Yi Kong9b14ac62018-07-17 13:48:38 -07001330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001331
1332 NotifyDeviceResetArgs resetArgs;
1333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1334 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1335 ASSERT_EQ(deviceId, resetArgs.deviceId);
1336
1337 ASSERT_EQ(device->isEnabled(), true);
1338 disableDevice(deviceId, device);
1339 mReader->loopOnce();
1340
1341 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1342 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1343 ASSERT_EQ(deviceId, resetArgs.deviceId);
1344 ASSERT_EQ(device->isEnabled(), false);
1345
1346 disableDevice(deviceId, device);
1347 mReader->loopOnce();
1348 mFakeListener->assertNotifyDeviceResetWasNotCalled();
1349 mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1350 ASSERT_EQ(device->isEnabled(), false);
1351
1352 enableDevice(deviceId, device);
1353 mReader->loopOnce();
1354 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1355 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1356 ASSERT_EQ(deviceId, resetArgs.deviceId);
1357 ASSERT_EQ(device->isEnabled(), true);
1358}
1359
Michael Wrightd02c5b62014-02-10 15:10:22 -08001360TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001361 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001362 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001363 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001364 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1365
1366 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1367 AINPUT_SOURCE_ANY, AKEYCODE_A))
1368 << "Should return unknown when the device id is >= 0 but unknown.";
1369
1370 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1371 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1372 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1373
1374 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1375 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1376 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1377
1378 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1379 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1380 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1381
1382 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1383 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1384 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1385}
1386
1387TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001388 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001389 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001390 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001391 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1392
1393 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1394 AINPUT_SOURCE_ANY, KEY_A))
1395 << "Should return unknown when the device id is >= 0 but unknown.";
1396
1397 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1398 AINPUT_SOURCE_TRACKBALL, KEY_A))
1399 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1400
1401 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1402 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1403 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1404
1405 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1406 AINPUT_SOURCE_TRACKBALL, KEY_A))
1407 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1408
1409 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1410 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1411 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1412}
1413
1414TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001415 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001416 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001417 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1419
1420 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1421 AINPUT_SOURCE_ANY, SW_LID))
1422 << "Should return unknown when the device id is >= 0 but unknown.";
1423
1424 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1425 AINPUT_SOURCE_TRACKBALL, SW_LID))
1426 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1427
1428 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1429 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1430 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1431
1432 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1433 AINPUT_SOURCE_TRACKBALL, SW_LID))
1434 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1435
1436 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1437 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1438 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1439}
1440
1441TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001442 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001443 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001444 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001445
Michael Wrightd02c5b62014-02-10 15:10:22 -08001446 mapper->addSupportedKeyCode(AKEYCODE_A);
1447 mapper->addSupportedKeyCode(AKEYCODE_B);
1448
1449 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1450 uint8_t flags[4] = { 0, 0, 0, 1 };
1451
1452 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1453 << "Should return false when device id is >= 0 but unknown.";
1454 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1455
1456 flags[3] = 1;
1457 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1458 << "Should return false when device id is valid but the sources are not supported by the device.";
1459 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1460
1461 flags[3] = 1;
1462 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1463 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1464 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1465
1466 flags[3] = 1;
1467 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1468 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1469 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1470
1471 flags[3] = 1;
1472 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1473 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1474 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1475}
1476
1477TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001478 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001479
1480 NotifyConfigurationChangedArgs args;
1481
1482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1483 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1484}
1485
1486TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001487 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001488 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001489 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001490
1491 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1492 mReader->loopOnce();
1493 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1494
1495 RawEvent event;
1496 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1497 ASSERT_EQ(0, event.when);
1498 ASSERT_EQ(1, event.deviceId);
1499 ASSERT_EQ(EV_KEY, event.type);
1500 ASSERT_EQ(KEY_A, event.code);
1501 ASSERT_EQ(1, event.value);
1502}
1503
Prabir Pradhan42611e02018-11-27 14:04:02 -08001504TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1505 constexpr int32_t deviceId = 1;
1506 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001507 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001508 // Must add at least one mapper or the device will be ignored!
1509 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1510 device->addMapper(mapper);
1511 mReader->setNextDevice(device);
1512 addDevice(deviceId, "fake", deviceClass, nullptr);
1513
1514 NotifyDeviceResetArgs resetArgs;
1515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1516 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1517
1518 disableDevice(deviceId, device);
1519 mReader->loopOnce();
1520 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1521 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1522 prevSequenceNum = resetArgs.sequenceNum;
1523
1524 enableDevice(deviceId, device);
1525 mReader->loopOnce();
1526 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1527 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1528 prevSequenceNum = resetArgs.sequenceNum;
1529
1530 disableDevice(deviceId, device);
1531 mReader->loopOnce();
1532 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1533 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1534 prevSequenceNum = resetArgs.sequenceNum;
1535}
1536
Arthur Hungc23540e2018-11-29 20:42:11 +08001537TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1538 constexpr int32_t deviceId = 1;
1539 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1540 const char* DEVICE_LOCATION = "USB1";
1541 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1542 DEVICE_LOCATION);
1543 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1544 device->addMapper(mapper);
1545 mReader->setNextDevice(device);
1546 addDevice(deviceId, "fake", deviceClass, nullptr);
1547
1548 const uint8_t hdmi1 = 1;
1549
1550 // Associated touch screen with second display.
1551 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1552
1553 // Add default and second display.
1554 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1555 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1556 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1557 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1558 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1559 mReader->loopOnce();
1560
1561 // Check device.
1562 ASSERT_EQ(deviceId, device->getId());
1563 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1564 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
1565}
1566
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567
1568// --- InputDeviceTest ---
1569
1570class InputDeviceTest : public testing::Test {
1571protected:
1572 static const char* DEVICE_NAME;
1573 static const int32_t DEVICE_ID;
1574 static const int32_t DEVICE_GENERATION;
1575 static const int32_t DEVICE_CONTROLLER_NUMBER;
1576 static const uint32_t DEVICE_CLASSES;
1577
1578 sp<FakeEventHub> mFakeEventHub;
1579 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001580 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001581 FakeInputReaderContext* mFakeContext;
1582
1583 InputDevice* mDevice;
1584
1585 virtual void SetUp() {
1586 mFakeEventHub = new FakeEventHub();
1587 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001588 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1590
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001591 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 InputDeviceIdentifier identifier;
1593 identifier.name = DEVICE_NAME;
1594 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1595 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1596 }
1597
1598 virtual void TearDown() {
1599 delete mDevice;
1600
1601 delete mFakeContext;
1602 mFakeListener.clear();
1603 mFakePolicy.clear();
1604 mFakeEventHub.clear();
1605 }
1606};
1607
1608const char* InputDeviceTest::DEVICE_NAME = "device";
1609const int32_t InputDeviceTest::DEVICE_ID = 1;
1610const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1611const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1612const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1613 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1614
1615TEST_F(InputDeviceTest, ImmutableProperties) {
1616 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001617 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001618 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1619}
1620
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1622 ASSERT_EQ(mDevice->isEnabled(), true);
1623}
1624
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1626 // Configuration.
1627 InputReaderConfiguration config;
1628 mDevice->configure(ARBITRARY_TIME, &config, 0);
1629
1630 // Reset.
1631 mDevice->reset(ARBITRARY_TIME);
1632
1633 NotifyDeviceResetArgs resetArgs;
1634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1635 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1636 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1637
1638 // Metadata.
1639 ASSERT_TRUE(mDevice->isIgnored());
1640 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1641
1642 InputDeviceInfo info;
1643 mDevice->getDeviceInfo(&info);
1644 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001645 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1647 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1648
1649 // State queries.
1650 ASSERT_EQ(0, mDevice->getMetaState());
1651
1652 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1653 << "Ignored device should return unknown key code state.";
1654 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1655 << "Ignored device should return unknown scan code state.";
1656 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1657 << "Ignored device should return unknown switch state.";
1658
1659 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1660 uint8_t flags[2] = { 0, 1 };
1661 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1662 << "Ignored device should never mark any key codes.";
1663 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1664 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1665}
1666
1667TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1668 // Configuration.
1669 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1670
1671 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1672 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1673 mapper1->setMetaState(AMETA_ALT_ON);
1674 mapper1->addSupportedKeyCode(AKEYCODE_A);
1675 mapper1->addSupportedKeyCode(AKEYCODE_B);
1676 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1677 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1678 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1679 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1680 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1681 mDevice->addMapper(mapper1);
1682
1683 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1684 mapper2->setMetaState(AMETA_SHIFT_ON);
1685 mDevice->addMapper(mapper2);
1686
1687 InputReaderConfiguration config;
1688 mDevice->configure(ARBITRARY_TIME, &config, 0);
1689
1690 String8 propertyValue;
1691 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1692 << "Device should have read configuration during configuration phase.";
1693 ASSERT_STREQ("value", propertyValue.string());
1694
1695 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1696 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1697
1698 // Reset
1699 mDevice->reset(ARBITRARY_TIME);
1700 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1701 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1702
1703 NotifyDeviceResetArgs resetArgs;
1704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1705 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1706 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1707
1708 // Metadata.
1709 ASSERT_FALSE(mDevice->isIgnored());
1710 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1711
1712 InputDeviceInfo info;
1713 mDevice->getDeviceInfo(&info);
1714 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001715 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001716 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1717 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1718
1719 // State queries.
1720 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1721 << "Should query mappers and combine meta states.";
1722
1723 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1724 << "Should return unknown key code state when source not supported.";
1725 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1726 << "Should return unknown scan code state when source not supported.";
1727 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1728 << "Should return unknown switch state when source not supported.";
1729
1730 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1731 << "Should query mapper when source is supported.";
1732 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1733 << "Should query mapper when source is supported.";
1734 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1735 << "Should query mapper when source is supported.";
1736
1737 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1738 uint8_t flags[4] = { 0, 0, 0, 1 };
1739 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1740 << "Should do nothing when source is unsupported.";
1741 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1742 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1743 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1744 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1745
1746 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1747 << "Should query mapper when source is supported.";
1748 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1749 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1750 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1751 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1752
1753 // Event handling.
1754 RawEvent event;
1755 mDevice->process(&event, 1);
1756
1757 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1758 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1759}
1760
1761
1762// --- InputMapperTest ---
1763
1764class InputMapperTest : public testing::Test {
1765protected:
1766 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001767 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768 static const int32_t DEVICE_ID;
1769 static const int32_t DEVICE_GENERATION;
1770 static const int32_t DEVICE_CONTROLLER_NUMBER;
1771 static const uint32_t DEVICE_CLASSES;
1772
1773 sp<FakeEventHub> mFakeEventHub;
1774 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001775 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001776 FakeInputReaderContext* mFakeContext;
1777 InputDevice* mDevice;
1778
1779 virtual void SetUp() {
1780 mFakeEventHub = new FakeEventHub();
1781 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001782 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001783 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1784 InputDeviceIdentifier identifier;
1785 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001786 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001787 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1788 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1789
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001790 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001791 }
1792
1793 virtual void TearDown() {
1794 delete mDevice;
1795 delete mFakeContext;
1796 mFakeListener.clear();
1797 mFakePolicy.clear();
1798 mFakeEventHub.clear();
1799 }
1800
1801 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001802 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803 }
1804
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001805 void configureDevice(uint32_t changes) {
1806 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1807 }
1808
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809 void addMapperAndConfigure(InputMapper* mapper) {
1810 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001811 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001812 mDevice->reset(ARBITRARY_TIME);
1813 }
1814
1815 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001816 int32_t orientation, const std::string& uniqueId,
1817 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001818 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001819 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001820 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1821 }
1822
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001823 void clearViewports() {
1824 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001825 }
1826
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001827 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001828 int32_t code, int32_t value) {
1829 RawEvent event;
1830 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001831 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832 event.type = type;
1833 event.code = code;
1834 event.value = value;
1835 mapper->process(&event);
1836 }
1837
1838 static void assertMotionRange(const InputDeviceInfo& info,
1839 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1840 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001841 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1843 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1844 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1845 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1846 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1847 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1848 }
1849
1850 static void assertPointerCoords(const PointerCoords& coords,
1851 float x, float y, float pressure, float size,
1852 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1853 float orientation, float distance) {
1854 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1855 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1856 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1857 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1858 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1859 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1860 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1861 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1862 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1863 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1864 }
1865
1866 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1867 float actualX, actualY;
1868 controller->getPosition(&actualX, &actualY);
1869 ASSERT_NEAR(x, actualX, 1);
1870 ASSERT_NEAR(y, actualY, 1);
1871 }
1872};
1873
1874const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001875const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001876const int32_t InputMapperTest::DEVICE_ID = 1;
1877const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1878const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1879const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1880
1881
1882// --- SwitchInputMapperTest ---
1883
1884class SwitchInputMapperTest : public InputMapperTest {
1885protected:
1886};
1887
1888TEST_F(SwitchInputMapperTest, GetSources) {
1889 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1890 addMapperAndConfigure(mapper);
1891
1892 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1893}
1894
1895TEST_F(SwitchInputMapperTest, GetSwitchState) {
1896 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1897 addMapperAndConfigure(mapper);
1898
1899 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1900 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1901
1902 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1903 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1904}
1905
1906TEST_F(SwitchInputMapperTest, Process) {
1907 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1908 addMapperAndConfigure(mapper);
1909
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001910 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
1911 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1912 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
1913 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914
1915 NotifySwitchArgs args;
1916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1917 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08001918 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1919 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001920 args.switchMask);
1921 ASSERT_EQ(uint32_t(0), args.policyFlags);
1922}
1923
1924
1925// --- KeyboardInputMapperTest ---
1926
1927class KeyboardInputMapperTest : public InputMapperTest {
1928protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001929 const std::string UNIQUE_ID = "local:0";
1930
1931 void prepareDisplay(int32_t orientation);
1932
Michael Wrightd02c5b62014-02-10 15:10:22 -08001933 void testDPadKeyRotation(KeyboardInputMapper* mapper,
1934 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1935};
1936
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001937/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
1938 * orientation.
1939 */
1940void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
1941 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001942 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001943}
1944
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001946 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001947 NotifyKeyArgs args;
1948
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001949 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1951 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1952 ASSERT_EQ(originalScanCode, args.scanCode);
1953 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1954
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001955 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1957 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1958 ASSERT_EQ(originalScanCode, args.scanCode);
1959 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1960}
1961
1962
1963TEST_F(KeyboardInputMapperTest, GetSources) {
1964 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1965 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1966 addMapperAndConfigure(mapper);
1967
1968 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1969}
1970
1971TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
1972 const int32_t USAGE_A = 0x070004;
1973 const int32_t USAGE_UNKNOWN = 0x07ffff;
1974 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
1975 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
1976
1977 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1978 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1979 addMapperAndConfigure(mapper);
1980
1981 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001982 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001983 NotifyKeyArgs args;
1984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1985 ASSERT_EQ(DEVICE_ID, args.deviceId);
1986 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1987 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1988 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1989 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1990 ASSERT_EQ(KEY_HOME, args.scanCode);
1991 ASSERT_EQ(AMETA_NONE, args.metaState);
1992 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1993 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1994 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1995
1996 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001997 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1999 ASSERT_EQ(DEVICE_ID, args.deviceId);
2000 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2001 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2002 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2003 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2004 ASSERT_EQ(KEY_HOME, args.scanCode);
2005 ASSERT_EQ(AMETA_NONE, args.metaState);
2006 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2007 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2008 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2009
2010 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002011 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2012 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2014 ASSERT_EQ(DEVICE_ID, args.deviceId);
2015 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2016 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2017 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2018 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2019 ASSERT_EQ(0, args.scanCode);
2020 ASSERT_EQ(AMETA_NONE, args.metaState);
2021 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2022 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2023 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2024
2025 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002026 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2027 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2029 ASSERT_EQ(DEVICE_ID, args.deviceId);
2030 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2031 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2032 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2033 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2034 ASSERT_EQ(0, args.scanCode);
2035 ASSERT_EQ(AMETA_NONE, args.metaState);
2036 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2037 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2038 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2039
2040 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002041 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2042 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2044 ASSERT_EQ(DEVICE_ID, args.deviceId);
2045 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2046 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2047 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2048 ASSERT_EQ(0, args.keyCode);
2049 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2050 ASSERT_EQ(AMETA_NONE, args.metaState);
2051 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2052 ASSERT_EQ(0U, args.policyFlags);
2053 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2054
2055 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002056 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2057 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2059 ASSERT_EQ(DEVICE_ID, args.deviceId);
2060 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2061 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2062 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2063 ASSERT_EQ(0, args.keyCode);
2064 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2065 ASSERT_EQ(AMETA_NONE, args.metaState);
2066 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2067 ASSERT_EQ(0U, args.policyFlags);
2068 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2069}
2070
2071TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2072 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2073 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2074
2075 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2076 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2077 addMapperAndConfigure(mapper);
2078
2079 // Initial metastate.
2080 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2081
2082 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002083 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002084 NotifyKeyArgs args;
2085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2087 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2088 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2089
2090 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002091 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2093 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2094 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2095
2096 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002097 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2100 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2101
2102 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002103 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2105 ASSERT_EQ(AMETA_NONE, args.metaState);
2106 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2107 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2108}
2109
2110TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2111 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2112 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2113 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2114 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2115
2116 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2117 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2118 addMapperAndConfigure(mapper);
2119
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002120 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002121 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2122 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2123 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2124 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2125 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2126 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2127 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2128 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2129}
2130
2131TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2132 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2133 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2134 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2135 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2136
2137 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2138 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2139 addConfigurationProperty("keyboard.orientationAware", "1");
2140 addMapperAndConfigure(mapper);
2141
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002142 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002143 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2144 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2145 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2146 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2147 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2148 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2149 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2150 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2151
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002152 clearViewports();
2153 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2155 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
2156 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2157 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
2158 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2159 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
2160 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2161 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
2162
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002163 clearViewports();
2164 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002165 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2166 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
2167 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2168 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
2169 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2170 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
2171 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2172 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
2173
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002174 clearViewports();
2175 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002176 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2177 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
2178 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2179 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
2180 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2181 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
2182 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2183 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
2184
2185 // Special case: if orientation changes while key is down, we still emit the same keycode
2186 // in the key up as we did in the key down.
2187 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002188 clearViewports();
2189 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002190 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2192 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2193 ASSERT_EQ(KEY_UP, args.scanCode);
2194 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2195
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002196 clearViewports();
2197 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002198 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2200 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2201 ASSERT_EQ(KEY_UP, args.scanCode);
2202 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2203}
2204
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002205TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2206 // If the keyboard is not orientation aware,
2207 // key events should not be associated with a specific display id
2208 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2209
2210 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2211 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2212 addMapperAndConfigure(mapper);
2213 NotifyKeyArgs args;
2214
2215 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002216 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002218 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2220 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2221
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002222 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002223 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002225 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2227 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2228}
2229
2230TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2231 // If the keyboard is orientation aware,
2232 // key events should be associated with the internal viewport
2233 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2234
2235 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2236 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2237 addConfigurationProperty("keyboard.orientationAware", "1");
2238 addMapperAndConfigure(mapper);
2239 NotifyKeyArgs args;
2240
2241 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2242 // ^--- already checked by the previous test
2243
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002244 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002245 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002246 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002248 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2250 ASSERT_EQ(DISPLAY_ID, args.displayId);
2251
2252 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002253 clearViewports();
2254 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002255 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002256 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002258 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2260 ASSERT_EQ(newDisplayId, args.displayId);
2261}
2262
Michael Wrightd02c5b62014-02-10 15:10:22 -08002263TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2264 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2265 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2266 addMapperAndConfigure(mapper);
2267
2268 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2269 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2270
2271 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2272 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2273}
2274
2275TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2276 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2277 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2278 addMapperAndConfigure(mapper);
2279
2280 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2281 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2282
2283 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2284 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2285}
2286
2287TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2288 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2289 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2290 addMapperAndConfigure(mapper);
2291
2292 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2293
2294 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2295 uint8_t flags[2] = { 0, 0 };
2296 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2297 ASSERT_TRUE(flags[0]);
2298 ASSERT_FALSE(flags[1]);
2299}
2300
2301TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2302 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2303 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2304 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2305 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2306 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2307 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2308
2309 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2310 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2311 addMapperAndConfigure(mapper);
2312
2313 // Initialization should have turned all of the lights off.
2314 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2315 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2316 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2317
2318 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002319 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2320 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002321 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2322 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2323 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2324 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2325
2326 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002327 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2328 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2330 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2331 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2332 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2333
2334 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002335 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2336 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2338 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2339 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2340 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2341
2342 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002343 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2344 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2346 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2347 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2348 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2349
2350 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002351 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2352 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2354 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2355 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2356 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2357
2358 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002359 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2360 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2362 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2363 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2364 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2365}
2366
2367
2368// --- CursorInputMapperTest ---
2369
2370class CursorInputMapperTest : public InputMapperTest {
2371protected:
2372 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2373
2374 sp<FakePointerController> mFakePointerController;
2375
2376 virtual void SetUp() {
2377 InputMapperTest::SetUp();
2378
2379 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002380 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 }
2382
2383 void testMotionRotation(CursorInputMapper* mapper,
2384 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002385
2386 void prepareDisplay(int32_t orientation) {
2387 const std::string uniqueId = "local:0";
2388 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2389 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2390 orientation, uniqueId, NO_PORT, viewportType);
2391 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392};
2393
2394const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2395
2396void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2397 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2398 NotifyMotionArgs args;
2399
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002400 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2401 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2402 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2406 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2407 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2408 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2409}
2410
2411TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2412 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2413 addConfigurationProperty("cursor.mode", "pointer");
2414 addMapperAndConfigure(mapper);
2415
2416 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2417}
2418
2419TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2420 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2421 addConfigurationProperty("cursor.mode", "navigation");
2422 addMapperAndConfigure(mapper);
2423
2424 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2425}
2426
2427TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2428 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2429 addConfigurationProperty("cursor.mode", "pointer");
2430 addMapperAndConfigure(mapper);
2431
2432 InputDeviceInfo info;
2433 mapper->populateDeviceInfo(&info);
2434
2435 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002436 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2437 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2439 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2440
2441 // When the bounds are set, then there should be a valid motion range.
2442 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2443
2444 InputDeviceInfo info2;
2445 mapper->populateDeviceInfo(&info2);
2446
2447 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2448 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2449 1, 800 - 1, 0.0f, 0.0f));
2450 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2451 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2452 2, 480 - 1, 0.0f, 0.0f));
2453 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2454 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2455 0.0f, 1.0f, 0.0f, 0.0f));
2456}
2457
2458TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2459 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2460 addConfigurationProperty("cursor.mode", "navigation");
2461 addMapperAndConfigure(mapper);
2462
2463 InputDeviceInfo info;
2464 mapper->populateDeviceInfo(&info);
2465
2466 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2467 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2468 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2469 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2470 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2471 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2472 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2473 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2474 0.0f, 1.0f, 0.0f, 0.0f));
2475}
2476
2477TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2478 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2479 addConfigurationProperty("cursor.mode", "navigation");
2480 addMapperAndConfigure(mapper);
2481
2482 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2483
2484 NotifyMotionArgs args;
2485
2486 // Button press.
2487 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002488 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2489 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2491 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2492 ASSERT_EQ(DEVICE_ID, args.deviceId);
2493 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2494 ASSERT_EQ(uint32_t(0), args.policyFlags);
2495 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2496 ASSERT_EQ(0, args.flags);
2497 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2498 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2499 ASSERT_EQ(0, args.edgeFlags);
2500 ASSERT_EQ(uint32_t(1), args.pointerCount);
2501 ASSERT_EQ(0, args.pointerProperties[0].id);
2502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2504 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2505 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2506 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2507 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2508
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2510 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2511 ASSERT_EQ(DEVICE_ID, args.deviceId);
2512 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2513 ASSERT_EQ(uint32_t(0), args.policyFlags);
2514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2515 ASSERT_EQ(0, args.flags);
2516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2517 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2518 ASSERT_EQ(0, args.edgeFlags);
2519 ASSERT_EQ(uint32_t(1), args.pointerCount);
2520 ASSERT_EQ(0, args.pointerProperties[0].id);
2521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2523 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2524 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2525 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2526 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2527
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002529 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2530 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2532 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2533 ASSERT_EQ(DEVICE_ID, args.deviceId);
2534 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2535 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2537 ASSERT_EQ(0, args.flags);
2538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2539 ASSERT_EQ(0, args.buttonState);
2540 ASSERT_EQ(0, args.edgeFlags);
2541 ASSERT_EQ(uint32_t(1), args.pointerCount);
2542 ASSERT_EQ(0, args.pointerProperties[0].id);
2543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2545 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2546 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2547 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2548 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2549
2550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2551 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2552 ASSERT_EQ(DEVICE_ID, args.deviceId);
2553 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2554 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2556 ASSERT_EQ(0, args.flags);
2557 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2558 ASSERT_EQ(0, args.buttonState);
2559 ASSERT_EQ(0, args.edgeFlags);
2560 ASSERT_EQ(uint32_t(1), args.pointerCount);
2561 ASSERT_EQ(0, args.pointerProperties[0].id);
2562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2564 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2565 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2566 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2567 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2568}
2569
2570TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2571 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2572 addConfigurationProperty("cursor.mode", "navigation");
2573 addMapperAndConfigure(mapper);
2574
2575 NotifyMotionArgs args;
2576
2577 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002578 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2579 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2583 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2584
2585 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002586 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2587 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2591 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2592}
2593
2594TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2595 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2596 addConfigurationProperty("cursor.mode", "navigation");
2597 addMapperAndConfigure(mapper);
2598
2599 NotifyMotionArgs args;
2600
2601 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002602 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2603 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2607 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2608
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2610 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2612 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2613
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002615 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2620 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2621
2622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002623 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2625 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2626}
2627
2628TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2629 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2630 addConfigurationProperty("cursor.mode", "navigation");
2631 addMapperAndConfigure(mapper);
2632
2633 NotifyMotionArgs args;
2634
2635 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002636 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2637 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2638 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2639 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2641 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2643 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2644 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2645
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2649 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2650 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2651
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002653 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2654 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2655 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2659 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2660 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2661
2662 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002663 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2664 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2668 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2669
2670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2673 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2674}
2675
2676TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2677 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2678 addConfigurationProperty("cursor.mode", "navigation");
2679 addMapperAndConfigure(mapper);
2680
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002681 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2683 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2684 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2685 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2686 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2687 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2688 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2689 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2690}
2691
2692TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2693 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2694 addConfigurationProperty("cursor.mode", "navigation");
2695 addConfigurationProperty("cursor.orientationAware", "1");
2696 addMapperAndConfigure(mapper);
2697
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002698 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002699 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2700 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2701 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2702 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2703 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2704 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2705 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2706 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2707
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002708 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2710 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2711 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2712 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2713 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2714 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2715 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2716 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2717
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002718 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2720 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2721 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2722 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2723 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2724 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2725 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2726 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2727
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002728 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2730 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2731 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2732 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2733 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2734 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2735 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2736 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2737}
2738
2739TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2740 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2741 addConfigurationProperty("cursor.mode", "pointer");
2742 addMapperAndConfigure(mapper);
2743
2744 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2745 mFakePointerController->setPosition(100, 200);
2746 mFakePointerController->setButtonState(0);
2747
2748 NotifyMotionArgs motionArgs;
2749 NotifyKeyArgs keyArgs;
2750
2751 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002752 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2753 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2755 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2756 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2757 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2759 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2760
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2762 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2763 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2764 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2766 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2767
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002768 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2769 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772 ASSERT_EQ(0, motionArgs.buttonState);
2773 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2775 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2776
2777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002778 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 ASSERT_EQ(0, motionArgs.buttonState);
2780 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2782 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2783
2784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002786 ASSERT_EQ(0, motionArgs.buttonState);
2787 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2789 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2790
2791 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002792 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2793 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2794 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2796 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2797 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2798 motionArgs.buttonState);
2799 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2800 mFakePointerController->getButtonState());
2801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2802 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2803
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2805 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2806 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2807 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2808 mFakePointerController->getButtonState());
2809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2810 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2811
2812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2813 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2814 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2815 motionArgs.buttonState);
2816 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2817 mFakePointerController->getButtonState());
2818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2819 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2820
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002821 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
2822 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2826 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2828 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2829
2830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002832 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2833 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2835 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2836
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002837 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2838 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002840 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2841 ASSERT_EQ(0, motionArgs.buttonState);
2842 ASSERT_EQ(0, mFakePointerController->getButtonState());
2843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2844 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 -08002845 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2846 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002847
2848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849 ASSERT_EQ(0, motionArgs.buttonState);
2850 ASSERT_EQ(0, mFakePointerController->getButtonState());
2851 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2853 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 -08002854
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2856 ASSERT_EQ(0, motionArgs.buttonState);
2857 ASSERT_EQ(0, mFakePointerController->getButtonState());
2858 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2860 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2861
2862 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002863 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
2864 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2866 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2867 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002868
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002871 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2872 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2874 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2875
2876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2877 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2878 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2879 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2881 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2882
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002883 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
2884 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_EQ(0, motionArgs.buttonState);
2888 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2890 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2891
2892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002894 ASSERT_EQ(0, motionArgs.buttonState);
2895 ASSERT_EQ(0, mFakePointerController->getButtonState());
2896
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2898 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2900 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2901 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2902
2903 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002904 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
2905 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2907 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2908 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002909
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2913 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2915 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2916
2917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2919 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2920 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2922 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2923
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002924 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
2925 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002927 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928 ASSERT_EQ(0, motionArgs.buttonState);
2929 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2931 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 -08002932
2933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2934 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2935 ASSERT_EQ(0, motionArgs.buttonState);
2936 ASSERT_EQ(0, mFakePointerController->getButtonState());
2937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2938 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2939
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2941 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2942 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2943
2944 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002945 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
2946 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2948 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2949 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002950
Michael Wrightd02c5b62014-02-10 15:10:22 -08002951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002952 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002953 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2954 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2956 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2957
2958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2960 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2961 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2963 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2964
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002965 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
2966 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969 ASSERT_EQ(0, motionArgs.buttonState);
2970 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2972 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 -08002973
2974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2975 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2976 ASSERT_EQ(0, motionArgs.buttonState);
2977 ASSERT_EQ(0, mFakePointerController->getButtonState());
2978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2979 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2980
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2982 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2983 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2984
2985 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002986 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
2987 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2989 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2990 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002991
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002993 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2995 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2997 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2998
2999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3001 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3002 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3004 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3005
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003006 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3007 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 ASSERT_EQ(0, motionArgs.buttonState);
3011 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3013 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 -08003014
3015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3016 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3017 ASSERT_EQ(0, motionArgs.buttonState);
3018 ASSERT_EQ(0, mFakePointerController->getButtonState());
3019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3020 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3021
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3023 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3024 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3025}
3026
3027TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3028 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3029 addConfigurationProperty("cursor.mode", "pointer");
3030 addMapperAndConfigure(mapper);
3031
3032 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3033 mFakePointerController->setPosition(100, 200);
3034 mFakePointerController->setButtonState(0);
3035
3036 NotifyMotionArgs args;
3037
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003038 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3039 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3040 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003042 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3043 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3045 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3046 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3047}
3048
3049TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3050 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3051 addConfigurationProperty("cursor.mode", "pointer");
3052 mFakePolicy->setPointerCapture(true);
3053 addMapperAndConfigure(mapper);
3054
3055 NotifyDeviceResetArgs resetArgs;
3056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3057 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3058 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3059
3060 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3061 mFakePointerController->setPosition(100, 200);
3062 mFakePointerController->setButtonState(0);
3063
3064 NotifyMotionArgs args;
3065
3066 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003067 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3068 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3069 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3071 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3074 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3075 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3076
3077 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003078 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3079 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3081 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3082 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3084 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3086 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3089 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3090
3091 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003092 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3093 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3095 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3098 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3100 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3101 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3103 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3104
3105 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003106 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3107 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3108 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3110 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3113 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3114 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3115
3116 // Disable pointer capture and check that the device generation got bumped
3117 // and events are generated the usual way.
3118 const uint32_t generation = mFakeContext->getGeneration();
3119 mFakePolicy->setPointerCapture(false);
3120 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3121 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3122
3123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3124 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3125 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3126
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003127 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3128 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3129 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3131 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3134 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3135 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3136}
3137
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003138TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3139 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3140 addMapperAndConfigure(mapper);
3141
3142 // Setup PointerController for second display.
3143 constexpr int32_t SECOND_DISPLAY_ID = 1;
3144 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3145 mFakePointerController->setPosition(100, 200);
3146 mFakePointerController->setButtonState(0);
3147 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3148
3149 NotifyMotionArgs args;
3150 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3151 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3152 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3154 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3155 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3157 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3158 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3159 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3160}
3161
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162
3163// --- TouchInputMapperTest ---
3164
3165class TouchInputMapperTest : public InputMapperTest {
3166protected:
3167 static const int32_t RAW_X_MIN;
3168 static const int32_t RAW_X_MAX;
3169 static const int32_t RAW_Y_MIN;
3170 static const int32_t RAW_Y_MAX;
3171 static const int32_t RAW_TOUCH_MIN;
3172 static const int32_t RAW_TOUCH_MAX;
3173 static const int32_t RAW_TOOL_MIN;
3174 static const int32_t RAW_TOOL_MAX;
3175 static const int32_t RAW_PRESSURE_MIN;
3176 static const int32_t RAW_PRESSURE_MAX;
3177 static const int32_t RAW_ORIENTATION_MIN;
3178 static const int32_t RAW_ORIENTATION_MAX;
3179 static const int32_t RAW_DISTANCE_MIN;
3180 static const int32_t RAW_DISTANCE_MAX;
3181 static const int32_t RAW_TILT_MIN;
3182 static const int32_t RAW_TILT_MAX;
3183 static const int32_t RAW_ID_MIN;
3184 static const int32_t RAW_ID_MAX;
3185 static const int32_t RAW_SLOT_MIN;
3186 static const int32_t RAW_SLOT_MAX;
3187 static const float X_PRECISION;
3188 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003189 static const float X_PRECISION_VIRTUAL;
3190 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003191
3192 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003193 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
3195 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3196
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003197 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003198 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003199
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 enum Axes {
3201 POSITION = 1 << 0,
3202 TOUCH = 1 << 1,
3203 TOOL = 1 << 2,
3204 PRESSURE = 1 << 3,
3205 ORIENTATION = 1 << 4,
3206 MINOR = 1 << 5,
3207 ID = 1 << 6,
3208 DISTANCE = 1 << 7,
3209 TILT = 1 << 8,
3210 SLOT = 1 << 9,
3211 TOOL_TYPE = 1 << 10,
3212 };
3213
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003214 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3215 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003216 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003217 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003218 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219 int32_t toRawX(float displayX);
3220 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003221 float toCookedX(float rawX, float rawY);
3222 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003224 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003226 float toDisplayY(int32_t rawY, int32_t displayHeight);
3227
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228};
3229
3230const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3231const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3232const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3233const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3234const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3235const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3236const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3237const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003238const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3239const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3241const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3242const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3243const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3244const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3245const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3246const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3247const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3248const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3249const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3250const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3251const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003252const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3253 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3254const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3255 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003256const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3257 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258
3259const float TouchInputMapperTest::GEOMETRIC_SCALE =
3260 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3261 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3262
3263const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3264 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3265 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3266};
3267
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003268void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003269 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003270 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3271}
3272
3273void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3274 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3275 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276}
3277
Santos Cordonfa5cf462017-04-05 10:37:00 -07003278void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003279 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3280 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003281 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003282}
3283
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284void TouchInputMapperTest::prepareVirtualKeys() {
3285 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3286 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3287 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3288 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3289}
3290
Jason Gerecke489fda82012-09-07 17:19:40 -07003291void TouchInputMapperTest::prepareLocationCalibration() {
3292 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3293}
3294
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295int32_t TouchInputMapperTest::toRawX(float displayX) {
3296 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3297}
3298
3299int32_t TouchInputMapperTest::toRawY(float displayY) {
3300 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3301}
3302
Jason Gerecke489fda82012-09-07 17:19:40 -07003303float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3304 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3305 return rawX;
3306}
3307
3308float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3309 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3310 return rawY;
3311}
3312
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003314 return toDisplayX(rawX, DISPLAY_WIDTH);
3315}
3316
3317float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3318 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319}
3320
3321float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003322 return toDisplayY(rawY, DISPLAY_HEIGHT);
3323}
3324
3325float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3326 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327}
3328
3329
3330// --- SingleTouchInputMapperTest ---
3331
3332class SingleTouchInputMapperTest : public TouchInputMapperTest {
3333protected:
3334 void prepareButtons();
3335 void prepareAxes(int axes);
3336
3337 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3338 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3339 void processUp(SingleTouchInputMapper* mappery);
3340 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3341 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3342 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3343 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3344 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3345 void processSync(SingleTouchInputMapper* mapper);
3346};
3347
3348void SingleTouchInputMapperTest::prepareButtons() {
3349 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3350}
3351
3352void SingleTouchInputMapperTest::prepareAxes(int axes) {
3353 if (axes & POSITION) {
3354 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3355 RAW_X_MIN, RAW_X_MAX, 0, 0);
3356 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3357 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3358 }
3359 if (axes & PRESSURE) {
3360 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3361 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3362 }
3363 if (axes & TOOL) {
3364 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3365 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3366 }
3367 if (axes & DISTANCE) {
3368 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3369 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3370 }
3371 if (axes & TILT) {
3372 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3373 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3374 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3375 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3376 }
3377}
3378
3379void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003380 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3381 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3382 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383}
3384
3385void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003386 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3387 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388}
3389
3390void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003391 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392}
3393
3394void SingleTouchInputMapperTest::processPressure(
3395 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003396 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397}
3398
3399void SingleTouchInputMapperTest::processToolMajor(
3400 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003401 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402}
3403
3404void SingleTouchInputMapperTest::processDistance(
3405 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003406 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407}
3408
3409void SingleTouchInputMapperTest::processTilt(
3410 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003411 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3412 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413}
3414
3415void SingleTouchInputMapperTest::processKey(
3416 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003417 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418}
3419
3420void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003421 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422}
3423
3424
3425TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3426 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3427 prepareButtons();
3428 prepareAxes(POSITION);
3429 addMapperAndConfigure(mapper);
3430
3431 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3432}
3433
3434TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3435 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3436 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3437 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3438 prepareButtons();
3439 prepareAxes(POSITION);
3440 addMapperAndConfigure(mapper);
3441
3442 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3443}
3444
3445TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3446 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3447 prepareButtons();
3448 prepareAxes(POSITION);
3449 addConfigurationProperty("touch.deviceType", "touchPad");
3450 addMapperAndConfigure(mapper);
3451
3452 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3453}
3454
3455TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3456 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3457 prepareButtons();
3458 prepareAxes(POSITION);
3459 addConfigurationProperty("touch.deviceType", "touchScreen");
3460 addMapperAndConfigure(mapper);
3461
3462 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3463}
3464
3465TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3466 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3467 addConfigurationProperty("touch.deviceType", "touchScreen");
3468 prepareDisplay(DISPLAY_ORIENTATION_0);
3469 prepareButtons();
3470 prepareAxes(POSITION);
3471 prepareVirtualKeys();
3472 addMapperAndConfigure(mapper);
3473
3474 // Unknown key.
3475 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3476
3477 // Virtual key is down.
3478 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3479 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3480 processDown(mapper, x, y);
3481 processSync(mapper);
3482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3483
3484 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3485
3486 // Virtual key is up.
3487 processUp(mapper);
3488 processSync(mapper);
3489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3490
3491 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3492}
3493
3494TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3495 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3496 addConfigurationProperty("touch.deviceType", "touchScreen");
3497 prepareDisplay(DISPLAY_ORIENTATION_0);
3498 prepareButtons();
3499 prepareAxes(POSITION);
3500 prepareVirtualKeys();
3501 addMapperAndConfigure(mapper);
3502
3503 // Unknown key.
3504 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3505
3506 // Virtual key is down.
3507 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3508 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3509 processDown(mapper, x, y);
3510 processSync(mapper);
3511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3512
3513 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3514
3515 // Virtual key is up.
3516 processUp(mapper);
3517 processSync(mapper);
3518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3519
3520 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3521}
3522
3523TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3524 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3525 addConfigurationProperty("touch.deviceType", "touchScreen");
3526 prepareDisplay(DISPLAY_ORIENTATION_0);
3527 prepareButtons();
3528 prepareAxes(POSITION);
3529 prepareVirtualKeys();
3530 addMapperAndConfigure(mapper);
3531
3532 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3533 uint8_t flags[2] = { 0, 0 };
3534 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3535 ASSERT_TRUE(flags[0]);
3536 ASSERT_FALSE(flags[1]);
3537}
3538
3539TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3540 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3541 addConfigurationProperty("touch.deviceType", "touchScreen");
3542 prepareDisplay(DISPLAY_ORIENTATION_0);
3543 prepareButtons();
3544 prepareAxes(POSITION);
3545 prepareVirtualKeys();
3546 addMapperAndConfigure(mapper);
3547
3548 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3549
3550 NotifyKeyArgs args;
3551
3552 // Press virtual key.
3553 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3554 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3555 processDown(mapper, x, y);
3556 processSync(mapper);
3557
3558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3559 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3560 ASSERT_EQ(DEVICE_ID, args.deviceId);
3561 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3562 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3563 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3564 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3565 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3566 ASSERT_EQ(KEY_HOME, args.scanCode);
3567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3568 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3569
3570 // Release virtual key.
3571 processUp(mapper);
3572 processSync(mapper);
3573
3574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3575 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3576 ASSERT_EQ(DEVICE_ID, args.deviceId);
3577 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3578 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3579 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3580 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3581 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3582 ASSERT_EQ(KEY_HOME, args.scanCode);
3583 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3584 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3585
3586 // Should not have sent any motions.
3587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3588}
3589
3590TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3591 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3592 addConfigurationProperty("touch.deviceType", "touchScreen");
3593 prepareDisplay(DISPLAY_ORIENTATION_0);
3594 prepareButtons();
3595 prepareAxes(POSITION);
3596 prepareVirtualKeys();
3597 addMapperAndConfigure(mapper);
3598
3599 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3600
3601 NotifyKeyArgs keyArgs;
3602
3603 // Press virtual key.
3604 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3605 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3606 processDown(mapper, x, y);
3607 processSync(mapper);
3608
3609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3610 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3611 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3612 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3613 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3614 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3615 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3616 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3617 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3618 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3619 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3620
3621 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3622 // into the display area.
3623 y -= 100;
3624 processMove(mapper, x, y);
3625 processSync(mapper);
3626
3627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3628 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3629 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3630 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3631 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3632 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3633 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3634 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3635 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3636 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3637 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3638 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3639
3640 NotifyMotionArgs motionArgs;
3641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3644 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3645 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3646 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3647 ASSERT_EQ(0, motionArgs.flags);
3648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3649 ASSERT_EQ(0, motionArgs.buttonState);
3650 ASSERT_EQ(0, motionArgs.edgeFlags);
3651 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3655 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3656 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3657 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3659
3660 // Keep moving out of bounds. Should generate a pointer move.
3661 y -= 50;
3662 processMove(mapper, x, y);
3663 processSync(mapper);
3664
3665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3666 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3667 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3668 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3669 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3671 ASSERT_EQ(0, motionArgs.flags);
3672 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3673 ASSERT_EQ(0, motionArgs.buttonState);
3674 ASSERT_EQ(0, motionArgs.edgeFlags);
3675 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3679 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3680 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3681 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3682 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3683
3684 // Release out of bounds. Should generate a pointer up.
3685 processUp(mapper);
3686 processSync(mapper);
3687
3688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3689 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3690 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3691 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3692 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3693 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3694 ASSERT_EQ(0, motionArgs.flags);
3695 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3696 ASSERT_EQ(0, motionArgs.buttonState);
3697 ASSERT_EQ(0, motionArgs.edgeFlags);
3698 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3702 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3703 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3704 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3705 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3706
3707 // Should not have sent any more keys or motions.
3708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3710}
3711
3712TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3713 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3714 addConfigurationProperty("touch.deviceType", "touchScreen");
3715 prepareDisplay(DISPLAY_ORIENTATION_0);
3716 prepareButtons();
3717 prepareAxes(POSITION);
3718 prepareVirtualKeys();
3719 addMapperAndConfigure(mapper);
3720
3721 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3722
3723 NotifyMotionArgs motionArgs;
3724
3725 // Initially go down out of bounds.
3726 int32_t x = -10;
3727 int32_t y = -10;
3728 processDown(mapper, x, y);
3729 processSync(mapper);
3730
3731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3732
3733 // Move into the display area. Should generate a pointer down.
3734 x = 50;
3735 y = 75;
3736 processMove(mapper, x, y);
3737 processSync(mapper);
3738
3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3741 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3742 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3743 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3744 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3745 ASSERT_EQ(0, motionArgs.flags);
3746 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3747 ASSERT_EQ(0, motionArgs.buttonState);
3748 ASSERT_EQ(0, motionArgs.edgeFlags);
3749 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3750 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3753 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3754 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3755 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3756 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3757
3758 // Release. Should generate a pointer up.
3759 processUp(mapper);
3760 processSync(mapper);
3761
3762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3763 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3764 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3765 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3766 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3768 ASSERT_EQ(0, motionArgs.flags);
3769 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3770 ASSERT_EQ(0, motionArgs.buttonState);
3771 ASSERT_EQ(0, motionArgs.edgeFlags);
3772 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3776 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3777 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3778 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3779 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3780
3781 // Should not have sent any more keys or motions.
3782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3784}
3785
Santos Cordonfa5cf462017-04-05 10:37:00 -07003786TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3787 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3788 addConfigurationProperty("touch.deviceType", "touchScreen");
3789 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3790
3791 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3792 prepareButtons();
3793 prepareAxes(POSITION);
3794 prepareVirtualKeys();
3795 addMapperAndConfigure(mapper);
3796
3797 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3798
3799 NotifyMotionArgs motionArgs;
3800
3801 // Down.
3802 int32_t x = 100;
3803 int32_t y = 125;
3804 processDown(mapper, x, y);
3805 processSync(mapper);
3806
3807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3808 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3809 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3810 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3811 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3812 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3813 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3814 ASSERT_EQ(0, motionArgs.flags);
3815 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3816 ASSERT_EQ(0, motionArgs.buttonState);
3817 ASSERT_EQ(0, motionArgs.edgeFlags);
3818 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3819 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3822 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3823 1, 0, 0, 0, 0, 0, 0, 0));
3824 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3825 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3826 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3827
3828 // Move.
3829 x += 50;
3830 y += 75;
3831 processMove(mapper, x, y);
3832 processSync(mapper);
3833
3834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3836 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3837 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3838 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3839 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3841 ASSERT_EQ(0, motionArgs.flags);
3842 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3843 ASSERT_EQ(0, motionArgs.buttonState);
3844 ASSERT_EQ(0, motionArgs.edgeFlags);
3845 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3846 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3849 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3850 1, 0, 0, 0, 0, 0, 0, 0));
3851 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3852 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3853 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3854
3855 // Up.
3856 processUp(mapper);
3857 processSync(mapper);
3858
3859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3860 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3861 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3862 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3863 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3864 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3865 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3866 ASSERT_EQ(0, motionArgs.flags);
3867 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3868 ASSERT_EQ(0, motionArgs.buttonState);
3869 ASSERT_EQ(0, motionArgs.edgeFlags);
3870 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3871 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3874 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3875 1, 0, 0, 0, 0, 0, 0, 0));
3876 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3877 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3878 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3879
3880 // Should not have sent any more keys or motions.
3881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3883}
3884
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
3886 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3887 addConfigurationProperty("touch.deviceType", "touchScreen");
3888 prepareDisplay(DISPLAY_ORIENTATION_0);
3889 prepareButtons();
3890 prepareAxes(POSITION);
3891 prepareVirtualKeys();
3892 addMapperAndConfigure(mapper);
3893
3894 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3895
3896 NotifyMotionArgs motionArgs;
3897
3898 // Down.
3899 int32_t x = 100;
3900 int32_t y = 125;
3901 processDown(mapper, x, y);
3902 processSync(mapper);
3903
3904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3906 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3907 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3908 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3909 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3910 ASSERT_EQ(0, motionArgs.flags);
3911 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3912 ASSERT_EQ(0, motionArgs.buttonState);
3913 ASSERT_EQ(0, motionArgs.edgeFlags);
3914 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3915 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3918 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3919 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3920 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3922
3923 // Move.
3924 x += 50;
3925 y += 75;
3926 processMove(mapper, x, y);
3927 processSync(mapper);
3928
3929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3930 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3931 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3932 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3933 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3935 ASSERT_EQ(0, motionArgs.flags);
3936 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3937 ASSERT_EQ(0, motionArgs.buttonState);
3938 ASSERT_EQ(0, motionArgs.edgeFlags);
3939 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3940 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3943 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3944 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3945 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3947
3948 // Up.
3949 processUp(mapper);
3950 processSync(mapper);
3951
3952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3953 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3954 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3955 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3956 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3957 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3958 ASSERT_EQ(0, motionArgs.flags);
3959 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3960 ASSERT_EQ(0, motionArgs.buttonState);
3961 ASSERT_EQ(0, motionArgs.edgeFlags);
3962 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3963 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3966 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3967 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3968 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3970
3971 // Should not have sent any more keys or motions.
3972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3974}
3975
3976TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3977 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3978 addConfigurationProperty("touch.deviceType", "touchScreen");
3979 prepareButtons();
3980 prepareAxes(POSITION);
3981 addConfigurationProperty("touch.orientationAware", "0");
3982 addMapperAndConfigure(mapper);
3983
3984 NotifyMotionArgs args;
3985
3986 // Rotation 90.
3987 prepareDisplay(DISPLAY_ORIENTATION_90);
3988 processDown(mapper, toRawX(50), toRawY(75));
3989 processSync(mapper);
3990
3991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3992 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3993 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3994
3995 processUp(mapper);
3996 processSync(mapper);
3997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3998}
3999
4000TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4001 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4002 addConfigurationProperty("touch.deviceType", "touchScreen");
4003 prepareButtons();
4004 prepareAxes(POSITION);
4005 addMapperAndConfigure(mapper);
4006
4007 NotifyMotionArgs args;
4008
4009 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004010 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 prepareDisplay(DISPLAY_ORIENTATION_0);
4012 processDown(mapper, toRawX(50), toRawY(75));
4013 processSync(mapper);
4014
4015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4016 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4017 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4018
4019 processUp(mapper);
4020 processSync(mapper);
4021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4022
4023 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004024 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 prepareDisplay(DISPLAY_ORIENTATION_90);
4026 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4027 processSync(mapper);
4028
4029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4030 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4031 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4032
4033 processUp(mapper);
4034 processSync(mapper);
4035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4036
4037 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004038 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 prepareDisplay(DISPLAY_ORIENTATION_180);
4040 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4041 processSync(mapper);
4042
4043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4044 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4045 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4046
4047 processUp(mapper);
4048 processSync(mapper);
4049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4050
4051 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004052 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053 prepareDisplay(DISPLAY_ORIENTATION_270);
4054 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4055 processSync(mapper);
4056
4057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4058 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4059 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4060
4061 processUp(mapper);
4062 processSync(mapper);
4063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4064}
4065
4066TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4067 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4068 addConfigurationProperty("touch.deviceType", "touchScreen");
4069 prepareDisplay(DISPLAY_ORIENTATION_0);
4070 prepareButtons();
4071 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4072 addMapperAndConfigure(mapper);
4073
4074 // These calculations are based on the input device calibration documentation.
4075 int32_t rawX = 100;
4076 int32_t rawY = 200;
4077 int32_t rawPressure = 10;
4078 int32_t rawToolMajor = 12;
4079 int32_t rawDistance = 2;
4080 int32_t rawTiltX = 30;
4081 int32_t rawTiltY = 110;
4082
4083 float x = toDisplayX(rawX);
4084 float y = toDisplayY(rawY);
4085 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4086 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4087 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4088 float distance = float(rawDistance);
4089
4090 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4091 float tiltScale = M_PI / 180;
4092 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4093 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4094 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4095 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4096
4097 processDown(mapper, rawX, rawY);
4098 processPressure(mapper, rawPressure);
4099 processToolMajor(mapper, rawToolMajor);
4100 processDistance(mapper, rawDistance);
4101 processTilt(mapper, rawTiltX, rawTiltY);
4102 processSync(mapper);
4103
4104 NotifyMotionArgs args;
4105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4107 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4108 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4109}
4110
Jason Gerecke489fda82012-09-07 17:19:40 -07004111TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4112 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4113 addConfigurationProperty("touch.deviceType", "touchScreen");
4114 prepareDisplay(DISPLAY_ORIENTATION_0);
4115 prepareLocationCalibration();
4116 prepareButtons();
4117 prepareAxes(POSITION);
4118 addMapperAndConfigure(mapper);
4119
4120 int32_t rawX = 100;
4121 int32_t rawY = 200;
4122
4123 float x = toDisplayX(toCookedX(rawX, rawY));
4124 float y = toDisplayY(toCookedY(rawX, rawY));
4125
4126 processDown(mapper, rawX, rawY);
4127 processSync(mapper);
4128
4129 NotifyMotionArgs args;
4130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4132 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4133}
4134
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4136 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4137 addConfigurationProperty("touch.deviceType", "touchScreen");
4138 prepareDisplay(DISPLAY_ORIENTATION_0);
4139 prepareButtons();
4140 prepareAxes(POSITION);
4141 addMapperAndConfigure(mapper);
4142
4143 NotifyMotionArgs motionArgs;
4144 NotifyKeyArgs keyArgs;
4145
4146 processDown(mapper, 100, 200);
4147 processSync(mapper);
4148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4149 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4150 ASSERT_EQ(0, motionArgs.buttonState);
4151
4152 // press BTN_LEFT, release BTN_LEFT
4153 processKey(mapper, BTN_LEFT, 1);
4154 processSync(mapper);
4155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4157 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4158
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4161 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4162
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163 processKey(mapper, BTN_LEFT, 0);
4164 processSync(mapper);
4165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004168
4169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004171 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172
4173 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4174 processKey(mapper, BTN_RIGHT, 1);
4175 processKey(mapper, BTN_MIDDLE, 1);
4176 processSync(mapper);
4177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4179 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4180 motionArgs.buttonState);
4181
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4184 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4185
4186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4188 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4189 motionArgs.buttonState);
4190
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 processKey(mapper, BTN_RIGHT, 0);
4192 processSync(mapper);
4193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004196
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004199 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200
4201 processKey(mapper, BTN_MIDDLE, 0);
4202 processSync(mapper);
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004206
4207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004209 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210
4211 // press BTN_BACK, release BTN_BACK
4212 processKey(mapper, BTN_BACK, 1);
4213 processSync(mapper);
4214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4215 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4216 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004217
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004220 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4221
4222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4224 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225
4226 processKey(mapper, BTN_BACK, 0);
4227 processSync(mapper);
4228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004231
4232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234 ASSERT_EQ(0, motionArgs.buttonState);
4235
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4237 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4238 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4239
4240 // press BTN_SIDE, release BTN_SIDE
4241 processKey(mapper, BTN_SIDE, 1);
4242 processSync(mapper);
4243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4244 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4245 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004249 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4250
4251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4253 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254
4255 processKey(mapper, BTN_SIDE, 0);
4256 processSync(mapper);
4257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004260
4261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004263 ASSERT_EQ(0, motionArgs.buttonState);
4264
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4266 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4267 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4268
4269 // press BTN_FORWARD, release BTN_FORWARD
4270 processKey(mapper, BTN_FORWARD, 1);
4271 processSync(mapper);
4272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4273 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4274 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004275
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004278 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4279
4280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4282 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283
4284 processKey(mapper, BTN_FORWARD, 0);
4285 processSync(mapper);
4286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004289
4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004292 ASSERT_EQ(0, motionArgs.buttonState);
4293
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4295 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4296 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4297
4298 // press BTN_EXTRA, release BTN_EXTRA
4299 processKey(mapper, BTN_EXTRA, 1);
4300 processSync(mapper);
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4302 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4303 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004304
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4308
4309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4311 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312
4313 processKey(mapper, BTN_EXTRA, 0);
4314 processSync(mapper);
4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318
4319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321 ASSERT_EQ(0, motionArgs.buttonState);
4322
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4324 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4325 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4326
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4328
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 // press BTN_STYLUS, release BTN_STYLUS
4330 processKey(mapper, BTN_STYLUS, 1);
4331 processSync(mapper);
4332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4335
4336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4338 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339
4340 processKey(mapper, BTN_STYLUS, 0);
4341 processSync(mapper);
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004345
4346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349
4350 // press BTN_STYLUS2, release BTN_STYLUS2
4351 processKey(mapper, BTN_STYLUS2, 1);
4352 processSync(mapper);
4353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4356
4357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4359 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360
4361 processKey(mapper, BTN_STYLUS2, 0);
4362 processSync(mapper);
4363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004366
4367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370
4371 // release touch
4372 processUp(mapper);
4373 processSync(mapper);
4374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4375 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4376 ASSERT_EQ(0, motionArgs.buttonState);
4377}
4378
4379TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4380 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4381 addConfigurationProperty("touch.deviceType", "touchScreen");
4382 prepareDisplay(DISPLAY_ORIENTATION_0);
4383 prepareButtons();
4384 prepareAxes(POSITION);
4385 addMapperAndConfigure(mapper);
4386
4387 NotifyMotionArgs motionArgs;
4388
4389 // default tool type is finger
4390 processDown(mapper, 100, 200);
4391 processSync(mapper);
4392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4393 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4395
4396 // eraser
4397 processKey(mapper, BTN_TOOL_RUBBER, 1);
4398 processSync(mapper);
4399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4402
4403 // stylus
4404 processKey(mapper, BTN_TOOL_RUBBER, 0);
4405 processKey(mapper, BTN_TOOL_PEN, 1);
4406 processSync(mapper);
4407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4410
4411 // brush
4412 processKey(mapper, BTN_TOOL_PEN, 0);
4413 processKey(mapper, BTN_TOOL_BRUSH, 1);
4414 processSync(mapper);
4415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4418
4419 // pencil
4420 processKey(mapper, BTN_TOOL_BRUSH, 0);
4421 processKey(mapper, BTN_TOOL_PENCIL, 1);
4422 processSync(mapper);
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4426
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004427 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 processKey(mapper, BTN_TOOL_PENCIL, 0);
4429 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4430 processSync(mapper);
4431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4434
4435 // mouse
4436 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4437 processKey(mapper, BTN_TOOL_MOUSE, 1);
4438 processSync(mapper);
4439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4442
4443 // lens
4444 processKey(mapper, BTN_TOOL_MOUSE, 0);
4445 processKey(mapper, BTN_TOOL_LENS, 1);
4446 processSync(mapper);
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4450
4451 // double-tap
4452 processKey(mapper, BTN_TOOL_LENS, 0);
4453 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4454 processSync(mapper);
4455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4458
4459 // triple-tap
4460 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4461 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4462 processSync(mapper);
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4466
4467 // quad-tap
4468 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4469 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4470 processSync(mapper);
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4474
4475 // finger
4476 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4477 processKey(mapper, BTN_TOOL_FINGER, 1);
4478 processSync(mapper);
4479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4481 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4482
4483 // stylus trumps finger
4484 processKey(mapper, BTN_TOOL_PEN, 1);
4485 processSync(mapper);
4486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4489
4490 // eraser trumps stylus
4491 processKey(mapper, BTN_TOOL_RUBBER, 1);
4492 processSync(mapper);
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4496
4497 // mouse trumps eraser
4498 processKey(mapper, BTN_TOOL_MOUSE, 1);
4499 processSync(mapper);
4500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4503
4504 // back to default tool type
4505 processKey(mapper, BTN_TOOL_MOUSE, 0);
4506 processKey(mapper, BTN_TOOL_RUBBER, 0);
4507 processKey(mapper, BTN_TOOL_PEN, 0);
4508 processKey(mapper, BTN_TOOL_FINGER, 0);
4509 processSync(mapper);
4510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4513}
4514
4515TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4516 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4517 addConfigurationProperty("touch.deviceType", "touchScreen");
4518 prepareDisplay(DISPLAY_ORIENTATION_0);
4519 prepareButtons();
4520 prepareAxes(POSITION);
4521 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4522 addMapperAndConfigure(mapper);
4523
4524 NotifyMotionArgs motionArgs;
4525
4526 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4527 processKey(mapper, BTN_TOOL_FINGER, 1);
4528 processMove(mapper, 100, 200);
4529 processSync(mapper);
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4531 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4533 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4534
4535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4538 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4539
4540 // move a little
4541 processMove(mapper, 150, 250);
4542 processSync(mapper);
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4546 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4547
4548 // down when BTN_TOUCH is pressed, pressure defaults to 1
4549 processKey(mapper, BTN_TOUCH, 1);
4550 processSync(mapper);
4551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4554 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4555
4556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4557 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4559 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4560
4561 // up when BTN_TOUCH is released, hover restored
4562 processKey(mapper, BTN_TOUCH, 0);
4563 processSync(mapper);
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4565 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4567 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4568
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4572 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4573
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4577 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4578
4579 // exit hover when pointer goes away
4580 processKey(mapper, BTN_TOOL_FINGER, 0);
4581 processSync(mapper);
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4585 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4586}
4587
4588TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4589 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4590 addConfigurationProperty("touch.deviceType", "touchScreen");
4591 prepareDisplay(DISPLAY_ORIENTATION_0);
4592 prepareButtons();
4593 prepareAxes(POSITION | PRESSURE);
4594 addMapperAndConfigure(mapper);
4595
4596 NotifyMotionArgs motionArgs;
4597
4598 // initially hovering because pressure is 0
4599 processDown(mapper, 100, 200);
4600 processPressure(mapper, 0);
4601 processSync(mapper);
4602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4605 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4606
4607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4608 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4610 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4611
4612 // move a little
4613 processMove(mapper, 150, 250);
4614 processSync(mapper);
4615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4616 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4618 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4619
4620 // down when pressure is non-zero
4621 processPressure(mapper, RAW_PRESSURE_MAX);
4622 processSync(mapper);
4623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4624 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4626 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4627
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4629 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4631 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4632
4633 // up when pressure becomes 0, hover restored
4634 processPressure(mapper, 0);
4635 processSync(mapper);
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4637 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4639 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4640
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4644 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4645
4646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4649 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4650
4651 // exit hover when pointer goes away
4652 processUp(mapper);
4653 processSync(mapper);
4654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4657 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4658}
4659
Dan Harmsaca28402018-12-17 13:55:20 -08004660
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661// --- MultiTouchInputMapperTest ---
4662
4663class MultiTouchInputMapperTest : public TouchInputMapperTest {
4664protected:
4665 void prepareAxes(int axes);
4666
4667 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4668 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4669 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4670 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4671 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4672 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4673 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4674 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4675 void processId(MultiTouchInputMapper* mapper, int32_t id);
4676 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4677 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4678 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4679 void processMTSync(MultiTouchInputMapper* mapper);
4680 void processSync(MultiTouchInputMapper* mapper);
4681};
4682
4683void MultiTouchInputMapperTest::prepareAxes(int axes) {
4684 if (axes & POSITION) {
4685 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4686 RAW_X_MIN, RAW_X_MAX, 0, 0);
4687 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4688 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4689 }
4690 if (axes & TOUCH) {
4691 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4692 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4693 if (axes & MINOR) {
4694 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4695 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4696 }
4697 }
4698 if (axes & TOOL) {
4699 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4700 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4701 if (axes & MINOR) {
4702 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4703 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4704 }
4705 }
4706 if (axes & ORIENTATION) {
4707 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4708 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4709 }
4710 if (axes & PRESSURE) {
4711 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4712 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4713 }
4714 if (axes & DISTANCE) {
4715 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4716 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4717 }
4718 if (axes & ID) {
4719 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4720 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4721 }
4722 if (axes & SLOT) {
4723 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4724 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4725 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4726 }
4727 if (axes & TOOL_TYPE) {
4728 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4729 0, MT_TOOL_MAX, 0, 0);
4730 }
4731}
4732
4733void MultiTouchInputMapperTest::processPosition(
4734 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004735 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4736 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737}
4738
4739void MultiTouchInputMapperTest::processTouchMajor(
4740 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004741 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742}
4743
4744void MultiTouchInputMapperTest::processTouchMinor(
4745 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004746 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747}
4748
4749void MultiTouchInputMapperTest::processToolMajor(
4750 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004751 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752}
4753
4754void MultiTouchInputMapperTest::processToolMinor(
4755 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004756 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757}
4758
4759void MultiTouchInputMapperTest::processOrientation(
4760 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004761 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762}
4763
4764void MultiTouchInputMapperTest::processPressure(
4765 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004766 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767}
4768
4769void MultiTouchInputMapperTest::processDistance(
4770 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004771 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772}
4773
4774void MultiTouchInputMapperTest::processId(
4775 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004776 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777}
4778
4779void MultiTouchInputMapperTest::processSlot(
4780 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004781 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782}
4783
4784void MultiTouchInputMapperTest::processToolType(
4785 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004786 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787}
4788
4789void MultiTouchInputMapperTest::processKey(
4790 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004791 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792}
4793
4794void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004795 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796}
4797
4798void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004799 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800}
4801
4802
4803TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4804 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4805 addConfigurationProperty("touch.deviceType", "touchScreen");
4806 prepareDisplay(DISPLAY_ORIENTATION_0);
4807 prepareAxes(POSITION);
4808 prepareVirtualKeys();
4809 addMapperAndConfigure(mapper);
4810
4811 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4812
4813 NotifyMotionArgs motionArgs;
4814
4815 // Two fingers down at once.
4816 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4817 processPosition(mapper, x1, y1);
4818 processMTSync(mapper);
4819 processPosition(mapper, x2, y2);
4820 processMTSync(mapper);
4821 processSync(mapper);
4822
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4824 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4825 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4826 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4827 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4828 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4829 ASSERT_EQ(0, motionArgs.flags);
4830 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4831 ASSERT_EQ(0, motionArgs.buttonState);
4832 ASSERT_EQ(0, motionArgs.edgeFlags);
4833 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4834 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4837 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4838 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4839 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4840 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4841
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4843 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4844 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4845 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4846 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4847 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4848 motionArgs.action);
4849 ASSERT_EQ(0, motionArgs.flags);
4850 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4851 ASSERT_EQ(0, motionArgs.buttonState);
4852 ASSERT_EQ(0, motionArgs.edgeFlags);
4853 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4854 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4856 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4859 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4861 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4862 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4863 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4864 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4865
4866 // Move.
4867 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4868 processPosition(mapper, x1, y1);
4869 processMTSync(mapper);
4870 processPosition(mapper, x2, y2);
4871 processMTSync(mapper);
4872 processSync(mapper);
4873
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4875 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4876 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4877 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4878 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4880 ASSERT_EQ(0, motionArgs.flags);
4881 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4882 ASSERT_EQ(0, motionArgs.buttonState);
4883 ASSERT_EQ(0, motionArgs.edgeFlags);
4884 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4885 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4887 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4890 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4892 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4893 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4894 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4895 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4896
4897 // First finger up.
4898 x2 += 15; y2 -= 20;
4899 processPosition(mapper, x2, y2);
4900 processMTSync(mapper);
4901 processSync(mapper);
4902
4903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4904 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4905 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4906 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4907 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4908 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4909 motionArgs.action);
4910 ASSERT_EQ(0, motionArgs.flags);
4911 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4912 ASSERT_EQ(0, motionArgs.buttonState);
4913 ASSERT_EQ(0, motionArgs.edgeFlags);
4914 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4915 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4917 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4920 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4922 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4923 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4924 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4925 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4926
4927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4928 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4929 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4930 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4931 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4933 ASSERT_EQ(0, motionArgs.flags);
4934 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4935 ASSERT_EQ(0, motionArgs.buttonState);
4936 ASSERT_EQ(0, motionArgs.edgeFlags);
4937 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4938 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4939 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4941 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4942 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4943 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4944 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4945
4946 // Move.
4947 x2 += 20; y2 -= 25;
4948 processPosition(mapper, x2, y2);
4949 processMTSync(mapper);
4950 processSync(mapper);
4951
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4953 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4954 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4955 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4956 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4958 ASSERT_EQ(0, motionArgs.flags);
4959 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4960 ASSERT_EQ(0, motionArgs.buttonState);
4961 ASSERT_EQ(0, motionArgs.edgeFlags);
4962 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4963 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4966 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4967 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4968 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4970
4971 // New finger down.
4972 int32_t x3 = 700, y3 = 300;
4973 processPosition(mapper, x2, y2);
4974 processMTSync(mapper);
4975 processPosition(mapper, x3, y3);
4976 processMTSync(mapper);
4977 processSync(mapper);
4978
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4980 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4981 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4982 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4983 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4984 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4985 motionArgs.action);
4986 ASSERT_EQ(0, motionArgs.flags);
4987 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4988 ASSERT_EQ(0, motionArgs.buttonState);
4989 ASSERT_EQ(0, motionArgs.edgeFlags);
4990 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4991 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4992 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4993 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4996 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4998 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4999 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5000 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5001 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5002
5003 // Second finger up.
5004 x3 += 30; y3 -= 20;
5005 processPosition(mapper, x3, y3);
5006 processMTSync(mapper);
5007 processSync(mapper);
5008
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5011 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5012 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5013 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5014 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5015 motionArgs.action);
5016 ASSERT_EQ(0, motionArgs.flags);
5017 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5018 ASSERT_EQ(0, motionArgs.buttonState);
5019 ASSERT_EQ(0, motionArgs.edgeFlags);
5020 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5023 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5026 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5028 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5029 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5030 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5031 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5032
5033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5034 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5035 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5036 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5037 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5039 ASSERT_EQ(0, motionArgs.flags);
5040 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5041 ASSERT_EQ(0, motionArgs.buttonState);
5042 ASSERT_EQ(0, motionArgs.edgeFlags);
5043 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5044 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5047 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5048 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5049 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5050 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5051
5052 // Last finger up.
5053 processMTSync(mapper);
5054 processSync(mapper);
5055
5056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5057 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5058 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5059 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5060 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5061 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5062 ASSERT_EQ(0, motionArgs.flags);
5063 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5064 ASSERT_EQ(0, motionArgs.buttonState);
5065 ASSERT_EQ(0, motionArgs.edgeFlags);
5066 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5067 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5070 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5071 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5072 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5073 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5074
5075 // Should not have sent any more keys or motions.
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5078}
5079
5080TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5081 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5082 addConfigurationProperty("touch.deviceType", "touchScreen");
5083 prepareDisplay(DISPLAY_ORIENTATION_0);
5084 prepareAxes(POSITION | ID);
5085 prepareVirtualKeys();
5086 addMapperAndConfigure(mapper);
5087
5088 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5089
5090 NotifyMotionArgs motionArgs;
5091
5092 // Two fingers down at once.
5093 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5094 processPosition(mapper, x1, y1);
5095 processId(mapper, 1);
5096 processMTSync(mapper);
5097 processPosition(mapper, x2, y2);
5098 processId(mapper, 2);
5099 processMTSync(mapper);
5100 processSync(mapper);
5101
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5103 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5104 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5105 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5108 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5109
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5111 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5112 motionArgs.action);
5113 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5116 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5119 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5121 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5122
5123 // Move.
5124 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5125 processPosition(mapper, x1, y1);
5126 processId(mapper, 1);
5127 processMTSync(mapper);
5128 processPosition(mapper, x2, y2);
5129 processId(mapper, 2);
5130 processMTSync(mapper);
5131 processSync(mapper);
5132
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5135 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5136 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5138 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5141 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5143 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5144
5145 // First finger up.
5146 x2 += 15; y2 -= 20;
5147 processPosition(mapper, x2, y2);
5148 processId(mapper, 2);
5149 processMTSync(mapper);
5150 processSync(mapper);
5151
5152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5153 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5154 motionArgs.action);
5155 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5156 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5158 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5161 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5163 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5164
5165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5167 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5168 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5171 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5172
5173 // Move.
5174 x2 += 20; y2 -= 25;
5175 processPosition(mapper, x2, y2);
5176 processId(mapper, 2);
5177 processMTSync(mapper);
5178 processSync(mapper);
5179
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5182 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5183 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5186 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5187
5188 // New finger down.
5189 int32_t x3 = 700, y3 = 300;
5190 processPosition(mapper, x2, y2);
5191 processId(mapper, 2);
5192 processMTSync(mapper);
5193 processPosition(mapper, x3, y3);
5194 processId(mapper, 3);
5195 processMTSync(mapper);
5196 processSync(mapper);
5197
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5199 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5200 motionArgs.action);
5201 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5204 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5207 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5209 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5210
5211 // Second finger up.
5212 x3 += 30; y3 -= 20;
5213 processPosition(mapper, x3, y3);
5214 processId(mapper, 3);
5215 processMTSync(mapper);
5216 processSync(mapper);
5217
5218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5219 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5220 motionArgs.action);
5221 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5222 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5224 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5227 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5229 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5230
5231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5233 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5234 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5237 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5238
5239 // Last finger up.
5240 processMTSync(mapper);
5241 processSync(mapper);
5242
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5244 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5245 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5246 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5249 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5250
5251 // Should not have sent any more keys or motions.
5252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5254}
5255
5256TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5257 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5258 addConfigurationProperty("touch.deviceType", "touchScreen");
5259 prepareDisplay(DISPLAY_ORIENTATION_0);
5260 prepareAxes(POSITION | ID | SLOT);
5261 prepareVirtualKeys();
5262 addMapperAndConfigure(mapper);
5263
5264 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5265
5266 NotifyMotionArgs motionArgs;
5267
5268 // Two fingers down at once.
5269 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5270 processPosition(mapper, x1, y1);
5271 processId(mapper, 1);
5272 processSlot(mapper, 1);
5273 processPosition(mapper, x2, y2);
5274 processId(mapper, 2);
5275 processSync(mapper);
5276
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5278 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5279 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5283 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5284
5285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5286 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5287 motionArgs.action);
5288 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5289 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5291 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5294 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5296 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5297
5298 // Move.
5299 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5300 processSlot(mapper, 0);
5301 processPosition(mapper, x1, y1);
5302 processSlot(mapper, 1);
5303 processPosition(mapper, x2, y2);
5304 processSync(mapper);
5305
5306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5308 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5309 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5311 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5314 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5316 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5317
5318 // First finger up.
5319 x2 += 15; y2 -= 20;
5320 processSlot(mapper, 0);
5321 processId(mapper, -1);
5322 processSlot(mapper, 1);
5323 processPosition(mapper, x2, y2);
5324 processSync(mapper);
5325
5326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5327 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5328 motionArgs.action);
5329 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5332 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5335 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5337 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5338
5339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5341 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5342 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5345 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5346
5347 // Move.
5348 x2 += 20; y2 -= 25;
5349 processPosition(mapper, x2, y2);
5350 processSync(mapper);
5351
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5354 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5355 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5358 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5359
5360 // New finger down.
5361 int32_t x3 = 700, y3 = 300;
5362 processPosition(mapper, x2, y2);
5363 processSlot(mapper, 0);
5364 processId(mapper, 3);
5365 processPosition(mapper, x3, y3);
5366 processSync(mapper);
5367
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5369 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5370 motionArgs.action);
5371 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5372 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5374 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5377 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5379 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5380
5381 // Second finger up.
5382 x3 += 30; y3 -= 20;
5383 processSlot(mapper, 1);
5384 processId(mapper, -1);
5385 processSlot(mapper, 0);
5386 processPosition(mapper, x3, y3);
5387 processSync(mapper);
5388
5389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5390 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5391 motionArgs.action);
5392 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5393 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5395 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5398 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5400 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5401
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5404 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5405 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5408 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5409
5410 // Last finger up.
5411 processId(mapper, -1);
5412 processSync(mapper);
5413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5415 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5416 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5420 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5421
5422 // Should not have sent any more keys or motions.
5423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5425}
5426
5427TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5428 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5429 addConfigurationProperty("touch.deviceType", "touchScreen");
5430 prepareDisplay(DISPLAY_ORIENTATION_0);
5431 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5432 addMapperAndConfigure(mapper);
5433
5434 // These calculations are based on the input device calibration documentation.
5435 int32_t rawX = 100;
5436 int32_t rawY = 200;
5437 int32_t rawTouchMajor = 7;
5438 int32_t rawTouchMinor = 6;
5439 int32_t rawToolMajor = 9;
5440 int32_t rawToolMinor = 8;
5441 int32_t rawPressure = 11;
5442 int32_t rawDistance = 0;
5443 int32_t rawOrientation = 3;
5444 int32_t id = 5;
5445
5446 float x = toDisplayX(rawX);
5447 float y = toDisplayY(rawY);
5448 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5449 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5450 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5451 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5452 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5453 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5454 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5455 float distance = float(rawDistance);
5456
5457 processPosition(mapper, rawX, rawY);
5458 processTouchMajor(mapper, rawTouchMajor);
5459 processTouchMinor(mapper, rawTouchMinor);
5460 processToolMajor(mapper, rawToolMajor);
5461 processToolMinor(mapper, rawToolMinor);
5462 processPressure(mapper, rawPressure);
5463 processOrientation(mapper, rawOrientation);
5464 processDistance(mapper, rawDistance);
5465 processId(mapper, id);
5466 processMTSync(mapper);
5467 processSync(mapper);
5468
5469 NotifyMotionArgs args;
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5471 ASSERT_EQ(0, args.pointerProperties[0].id);
5472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5473 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5474 orientation, distance));
5475}
5476
5477TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5478 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5479 addConfigurationProperty("touch.deviceType", "touchScreen");
5480 prepareDisplay(DISPLAY_ORIENTATION_0);
5481 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5482 addConfigurationProperty("touch.size.calibration", "geometric");
5483 addMapperAndConfigure(mapper);
5484
5485 // These calculations are based on the input device calibration documentation.
5486 int32_t rawX = 100;
5487 int32_t rawY = 200;
5488 int32_t rawTouchMajor = 140;
5489 int32_t rawTouchMinor = 120;
5490 int32_t rawToolMajor = 180;
5491 int32_t rawToolMinor = 160;
5492
5493 float x = toDisplayX(rawX);
5494 float y = toDisplayY(rawY);
5495 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5496 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5497 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5498 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5499 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5500
5501 processPosition(mapper, rawX, rawY);
5502 processTouchMajor(mapper, rawTouchMajor);
5503 processTouchMinor(mapper, rawTouchMinor);
5504 processToolMajor(mapper, rawToolMajor);
5505 processToolMinor(mapper, rawToolMinor);
5506 processMTSync(mapper);
5507 processSync(mapper);
5508
5509 NotifyMotionArgs args;
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5512 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5513}
5514
5515TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5516 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5517 addConfigurationProperty("touch.deviceType", "touchScreen");
5518 prepareDisplay(DISPLAY_ORIENTATION_0);
5519 prepareAxes(POSITION | TOUCH | TOOL);
5520 addConfigurationProperty("touch.size.calibration", "diameter");
5521 addConfigurationProperty("touch.size.scale", "10");
5522 addConfigurationProperty("touch.size.bias", "160");
5523 addConfigurationProperty("touch.size.isSummed", "1");
5524 addMapperAndConfigure(mapper);
5525
5526 // These calculations are based on the input device calibration documentation.
5527 // Note: We only provide a single common touch/tool value because the device is assumed
5528 // not to emit separate values for each pointer (isSummed = 1).
5529 int32_t rawX = 100;
5530 int32_t rawY = 200;
5531 int32_t rawX2 = 150;
5532 int32_t rawY2 = 250;
5533 int32_t rawTouchMajor = 5;
5534 int32_t rawToolMajor = 8;
5535
5536 float x = toDisplayX(rawX);
5537 float y = toDisplayY(rawY);
5538 float x2 = toDisplayX(rawX2);
5539 float y2 = toDisplayY(rawY2);
5540 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5541 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5542 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5543
5544 processPosition(mapper, rawX, rawY);
5545 processTouchMajor(mapper, rawTouchMajor);
5546 processToolMajor(mapper, rawToolMajor);
5547 processMTSync(mapper);
5548 processPosition(mapper, rawX2, rawY2);
5549 processTouchMajor(mapper, rawTouchMajor);
5550 processToolMajor(mapper, rawToolMajor);
5551 processMTSync(mapper);
5552 processSync(mapper);
5553
5554 NotifyMotionArgs args;
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5556 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5559 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5560 args.action);
5561 ASSERT_EQ(size_t(2), args.pointerCount);
5562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5563 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5565 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5566}
5567
5568TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5569 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5570 addConfigurationProperty("touch.deviceType", "touchScreen");
5571 prepareDisplay(DISPLAY_ORIENTATION_0);
5572 prepareAxes(POSITION | TOUCH | TOOL);
5573 addConfigurationProperty("touch.size.calibration", "area");
5574 addConfigurationProperty("touch.size.scale", "43");
5575 addConfigurationProperty("touch.size.bias", "3");
5576 addMapperAndConfigure(mapper);
5577
5578 // These calculations are based on the input device calibration documentation.
5579 int32_t rawX = 100;
5580 int32_t rawY = 200;
5581 int32_t rawTouchMajor = 5;
5582 int32_t rawToolMajor = 8;
5583
5584 float x = toDisplayX(rawX);
5585 float y = toDisplayY(rawY);
5586 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5587 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5588 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5589
5590 processPosition(mapper, rawX, rawY);
5591 processTouchMajor(mapper, rawTouchMajor);
5592 processToolMajor(mapper, rawToolMajor);
5593 processMTSync(mapper);
5594 processSync(mapper);
5595
5596 NotifyMotionArgs args;
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5599 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5600}
5601
5602TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5603 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5604 addConfigurationProperty("touch.deviceType", "touchScreen");
5605 prepareDisplay(DISPLAY_ORIENTATION_0);
5606 prepareAxes(POSITION | PRESSURE);
5607 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5608 addConfigurationProperty("touch.pressure.scale", "0.01");
5609 addMapperAndConfigure(mapper);
5610
Michael Wrightaa449c92017-12-13 21:21:43 +00005611 InputDeviceInfo info;
5612 mapper->populateDeviceInfo(&info);
5613 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5614 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5615 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5616
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 // These calculations are based on the input device calibration documentation.
5618 int32_t rawX = 100;
5619 int32_t rawY = 200;
5620 int32_t rawPressure = 60;
5621
5622 float x = toDisplayX(rawX);
5623 float y = toDisplayY(rawY);
5624 float pressure = float(rawPressure) * 0.01f;
5625
5626 processPosition(mapper, rawX, rawY);
5627 processPressure(mapper, rawPressure);
5628 processMTSync(mapper);
5629 processSync(mapper);
5630
5631 NotifyMotionArgs args;
5632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5634 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5635}
5636
5637TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5638 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5639 addConfigurationProperty("touch.deviceType", "touchScreen");
5640 prepareDisplay(DISPLAY_ORIENTATION_0);
5641 prepareAxes(POSITION | ID | SLOT);
5642 addMapperAndConfigure(mapper);
5643
5644 NotifyMotionArgs motionArgs;
5645 NotifyKeyArgs keyArgs;
5646
5647 processId(mapper, 1);
5648 processPosition(mapper, 100, 200);
5649 processSync(mapper);
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5651 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5652 ASSERT_EQ(0, motionArgs.buttonState);
5653
5654 // press BTN_LEFT, release BTN_LEFT
5655 processKey(mapper, BTN_LEFT, 1);
5656 processSync(mapper);
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5659 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5660
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5663 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5664
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665 processKey(mapper, BTN_LEFT, 0);
5666 processSync(mapper);
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005668 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005670
5671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005673 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674
5675 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5676 processKey(mapper, BTN_RIGHT, 1);
5677 processKey(mapper, BTN_MIDDLE, 1);
5678 processSync(mapper);
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5681 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5682 motionArgs.buttonState);
5683
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5686 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5687
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5690 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5691 motionArgs.buttonState);
5692
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 processKey(mapper, BTN_RIGHT, 0);
5694 processSync(mapper);
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005696 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005698
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005701 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702
5703 processKey(mapper, BTN_MIDDLE, 0);
5704 processSync(mapper);
5705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005708
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005711 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712
5713 // press BTN_BACK, release BTN_BACK
5714 processKey(mapper, BTN_BACK, 1);
5715 processSync(mapper);
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5717 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5718 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005719
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005722 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5723
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5726 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727
5728 processKey(mapper, BTN_BACK, 0);
5729 processSync(mapper);
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005733
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005736 ASSERT_EQ(0, motionArgs.buttonState);
5737
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5739 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5740 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5741
5742 // press BTN_SIDE, release BTN_SIDE
5743 processKey(mapper, BTN_SIDE, 1);
5744 processSync(mapper);
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5746 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5747 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005748
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005751 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5752
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5754 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5755 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756
5757 processKey(mapper, BTN_SIDE, 0);
5758 processSync(mapper);
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005761 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005762
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005765 ASSERT_EQ(0, motionArgs.buttonState);
5766
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5768 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5769 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5770
5771 // press BTN_FORWARD, release BTN_FORWARD
5772 processKey(mapper, BTN_FORWARD, 1);
5773 processSync(mapper);
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5775 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5776 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005777
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005780 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5781
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5784 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005785
5786 processKey(mapper, BTN_FORWARD, 0);
5787 processSync(mapper);
5788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005790 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005791
5792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005794 ASSERT_EQ(0, motionArgs.buttonState);
5795
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5797 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5798 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5799
5800 // press BTN_EXTRA, release BTN_EXTRA
5801 processKey(mapper, BTN_EXTRA, 1);
5802 processSync(mapper);
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5804 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5805 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005806
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005809 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5810
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5813 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814
5815 processKey(mapper, BTN_EXTRA, 0);
5816 processSync(mapper);
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005820
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005823 ASSERT_EQ(0, motionArgs.buttonState);
5824
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5826 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5827 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5828
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5830
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831 // press BTN_STYLUS, release BTN_STYLUS
5832 processKey(mapper, BTN_STYLUS, 1);
5833 processSync(mapper);
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005836 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5837
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5839 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5840 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005841
5842 processKey(mapper, BTN_STYLUS, 0);
5843 processSync(mapper);
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005845 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005850 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851
5852 // press BTN_STYLUS2, release BTN_STYLUS2
5853 processKey(mapper, BTN_STYLUS2, 1);
5854 processSync(mapper);
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005857 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5858
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5861 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862
5863 processKey(mapper, BTN_STYLUS2, 0);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005868
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005871 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872
5873 // release touch
5874 processId(mapper, -1);
5875 processSync(mapper);
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5877 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5878 ASSERT_EQ(0, motionArgs.buttonState);
5879}
5880
5881TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
5882 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5883 addConfigurationProperty("touch.deviceType", "touchScreen");
5884 prepareDisplay(DISPLAY_ORIENTATION_0);
5885 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
5886 addMapperAndConfigure(mapper);
5887
5888 NotifyMotionArgs motionArgs;
5889
5890 // default tool type is finger
5891 processId(mapper, 1);
5892 processPosition(mapper, 100, 200);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5897
5898 // eraser
5899 processKey(mapper, BTN_TOOL_RUBBER, 1);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5904
5905 // stylus
5906 processKey(mapper, BTN_TOOL_RUBBER, 0);
5907 processKey(mapper, BTN_TOOL_PEN, 1);
5908 processSync(mapper);
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5912
5913 // brush
5914 processKey(mapper, BTN_TOOL_PEN, 0);
5915 processKey(mapper, BTN_TOOL_BRUSH, 1);
5916 processSync(mapper);
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5920
5921 // pencil
5922 processKey(mapper, BTN_TOOL_BRUSH, 0);
5923 processKey(mapper, BTN_TOOL_PENCIL, 1);
5924 processSync(mapper);
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5928
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005929 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930 processKey(mapper, BTN_TOOL_PENCIL, 0);
5931 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5932 processSync(mapper);
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5936
5937 // mouse
5938 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5939 processKey(mapper, BTN_TOOL_MOUSE, 1);
5940 processSync(mapper);
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5944
5945 // lens
5946 processKey(mapper, BTN_TOOL_MOUSE, 0);
5947 processKey(mapper, BTN_TOOL_LENS, 1);
5948 processSync(mapper);
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5951 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5952
5953 // double-tap
5954 processKey(mapper, BTN_TOOL_LENS, 0);
5955 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5956 processSync(mapper);
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5960
5961 // triple-tap
5962 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5963 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5964 processSync(mapper);
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5968
5969 // quad-tap
5970 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5971 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5972 processSync(mapper);
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5976
5977 // finger
5978 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5979 processKey(mapper, BTN_TOOL_FINGER, 1);
5980 processSync(mapper);
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5984
5985 // stylus trumps finger
5986 processKey(mapper, BTN_TOOL_PEN, 1);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5991
5992 // eraser trumps stylus
5993 processKey(mapper, BTN_TOOL_RUBBER, 1);
5994 processSync(mapper);
5995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5998
5999 // mouse trumps eraser
6000 processKey(mapper, BTN_TOOL_MOUSE, 1);
6001 processSync(mapper);
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6005
6006 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6007 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6008 processSync(mapper);
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6012
6013 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6014 processToolType(mapper, MT_TOOL_PEN);
6015 processSync(mapper);
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6019
6020 // back to default tool type
6021 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6022 processKey(mapper, BTN_TOOL_MOUSE, 0);
6023 processKey(mapper, BTN_TOOL_RUBBER, 0);
6024 processKey(mapper, BTN_TOOL_PEN, 0);
6025 processKey(mapper, BTN_TOOL_FINGER, 0);
6026 processSync(mapper);
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6030}
6031
6032TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6033 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6034 addConfigurationProperty("touch.deviceType", "touchScreen");
6035 prepareDisplay(DISPLAY_ORIENTATION_0);
6036 prepareAxes(POSITION | ID | SLOT);
6037 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6038 addMapperAndConfigure(mapper);
6039
6040 NotifyMotionArgs motionArgs;
6041
6042 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6043 processId(mapper, 1);
6044 processPosition(mapper, 100, 200);
6045 processSync(mapper);
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6047 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6049 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6050
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6054 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6055
6056 // move a little
6057 processPosition(mapper, 150, 250);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6062 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6063
6064 // down when BTN_TOUCH is pressed, pressure defaults to 1
6065 processKey(mapper, BTN_TOUCH, 1);
6066 processSync(mapper);
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6070 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6071
6072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6073 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6075 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6076
6077 // up when BTN_TOUCH is released, hover restored
6078 processKey(mapper, BTN_TOUCH, 0);
6079 processSync(mapper);
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6081 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6083 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6084
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6086 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6088 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6089
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6091 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6093 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6094
6095 // exit hover when pointer goes away
6096 processId(mapper, -1);
6097 processSync(mapper);
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6099 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6101 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6102}
6103
6104TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6105 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6106 addConfigurationProperty("touch.deviceType", "touchScreen");
6107 prepareDisplay(DISPLAY_ORIENTATION_0);
6108 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6109 addMapperAndConfigure(mapper);
6110
6111 NotifyMotionArgs motionArgs;
6112
6113 // initially hovering because pressure is 0
6114 processId(mapper, 1);
6115 processPosition(mapper, 100, 200);
6116 processPressure(mapper, 0);
6117 processSync(mapper);
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6119 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6121 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6122
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6124 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6126 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6127
6128 // move a little
6129 processPosition(mapper, 150, 250);
6130 processSync(mapper);
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6132 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6134 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6135
6136 // down when pressure becomes non-zero
6137 processPressure(mapper, RAW_PRESSURE_MAX);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6142 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6143
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6147 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6148
6149 // up when pressure becomes 0, hover restored
6150 processPressure(mapper, 0);
6151 processSync(mapper);
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6153 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6155 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6156
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6158 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6160 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6163 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6165 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6166
6167 // exit hover when pointer goes away
6168 processId(mapper, -1);
6169 processSync(mapper);
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6171 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6173 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6174}
6175
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006176/**
6177 * Set the input device port <--> display port associations, and check that the
6178 * events are routed to the display that matches the display port.
6179 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6180 */
6181TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6182 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6183 const std::string usb2 = "USB2";
6184 const uint8_t hdmi1 = 0;
6185 const uint8_t hdmi2 = 1;
6186 const std::string secondaryUniqueId = "uniqueId2";
6187 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6188
6189 addConfigurationProperty("touch.deviceType", "touchScreen");
6190 prepareAxes(POSITION);
6191 addMapperAndConfigure(mapper);
6192
6193 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6194 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6195
6196 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6197 // for this input device is specified, and the matching viewport is not present,
6198 // the input device should be disabled (at the mapper level).
6199
6200 // Add viewport for display 2 on hdmi2
6201 prepareSecondaryDisplay(type, hdmi2);
6202 // Send a touch event
6203 processPosition(mapper, 100, 100);
6204 processSync(mapper);
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6206
6207 // Add viewport for display 1 on hdmi1
6208 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6209 // Send a touch event again
6210 processPosition(mapper, 100, 100);
6211 processSync(mapper);
6212
6213 NotifyMotionArgs args;
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6215 ASSERT_EQ(DISPLAY_ID, args.displayId);
6216}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217
Arthur Hung41a712e2018-11-22 19:41:03 +08006218/**
6219 * Expect fallback to internal viewport if device is external and external viewport is not present.
6220 */
6221TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6222 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6223 prepareAxes(POSITION);
6224 addConfigurationProperty("touch.deviceType", "touchScreen");
6225 prepareDisplay(DISPLAY_ORIENTATION_0);
6226 mDevice->setExternal(true);
6227 addMapperAndConfigure(mapper);
6228
6229 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6230
6231 NotifyMotionArgs motionArgs;
6232
6233 // Expect the event to be sent to the internal viewport,
6234 // because an external viewport is not present.
6235 processPosition(mapper, 100, 100);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6238 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6239
6240 // Expect the event to be sent to the external viewport if it is present.
6241 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6242 processPosition(mapper, 100, 100);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6245 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6246}
6247
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006248TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6249 // Setup PointerController for second display.
6250 sp<FakePointerController> fakePointerController = new FakePointerController();
6251 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6252 fakePointerController->setPosition(100, 200);
6253 fakePointerController->setButtonState(0);
6254 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6255 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6256
6257 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6258 prepareDisplay(DISPLAY_ORIENTATION_0);
6259 prepareAxes(POSITION);
6260 addMapperAndConfigure(mapper);
6261
6262 // Check source is mouse that would obtain the PointerController.
6263 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6264
6265 NotifyMotionArgs motionArgs;
6266 processPosition(mapper, 100, 100);
6267 processSync(mapper);
6268
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6271 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6272}
6273
Arthur Hung7c645402019-01-25 17:45:42 +08006274TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6275 // Setup the first touch screen device.
6276 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6277 prepareAxes(POSITION | ID | SLOT);
6278 addConfigurationProperty("touch.deviceType", "touchScreen");
6279 addMapperAndConfigure(mapper);
6280
6281 // Create the second touch screen device, and enable multi fingers.
6282 const std::string USB2 = "USB2";
6283 const int32_t SECOND_DEVICE_ID = 2;
6284 InputDeviceIdentifier identifier;
6285 identifier.name = DEVICE_NAME;
6286 identifier.location = USB2;
6287 InputDevice* device2 = new InputDevice(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6288 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
6289 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6290 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6291 0 /*flat*/, 0 /*fuzz*/);
6292 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6293 0 /*flat*/, 0 /*fuzz*/);
6294 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6295 0 /*flat*/, 0 /*fuzz*/);
6296 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6297 0 /*flat*/, 0 /*fuzz*/);
6298 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6299 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6300 String8("touchScreen"));
6301
6302 // Setup the second touch screen device.
6303 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2);
6304 device2->addMapper(mapper2);
6305 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6306 device2->reset(ARBITRARY_TIME);
6307
6308 // Setup PointerController.
6309 sp<FakePointerController> fakePointerController = new FakePointerController();
6310 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6311 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6312
6313 // Setup policy for associated displays and show touches.
6314 const uint8_t hdmi1 = 0;
6315 const uint8_t hdmi2 = 1;
6316 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6317 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6318 mFakePolicy->setShowTouches(true);
6319
6320 // Create displays.
6321 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6322 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6323
6324 // Default device will reconfigure above, need additional reconfiguration for another device.
6325 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6326 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6327
6328 // Two fingers down at default display.
6329 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6330 processPosition(mapper, x1, y1);
6331 processId(mapper, 1);
6332 processSlot(mapper, 1);
6333 processPosition(mapper, x2, y2);
6334 processId(mapper, 2);
6335 processSync(mapper);
6336
6337 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6338 fakePointerController->getSpots().find(DISPLAY_ID);
6339 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6340 ASSERT_EQ(size_t(2), iter->second.size());
6341
6342 // Two fingers down at second display.
6343 processPosition(mapper2, x1, y1);
6344 processId(mapper2, 1);
6345 processSlot(mapper2, 1);
6346 processPosition(mapper2, x2, y2);
6347 processId(mapper2, 2);
6348 processSync(mapper2);
6349
6350 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6351 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6352 ASSERT_EQ(size_t(2), iter->second.size());
6353}
6354
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006355TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6356 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6357 prepareAxes(POSITION);
6358 addConfigurationProperty("touch.deviceType", "touchScreen");
6359 prepareDisplay(DISPLAY_ORIENTATION_0);
6360 addMapperAndConfigure(mapper);
6361
6362 NotifyMotionArgs motionArgs;
6363 // Unrotated video frame
6364 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6365 std::vector<TouchVideoFrame> frames{frame};
6366 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6367 processPosition(mapper, 100, 200);
6368 processSync(mapper);
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(frames, motionArgs.videoFrames);
6371
6372 // Subsequent touch events should not have any videoframes
6373 // This is implemented separately in FakeEventHub,
6374 // but that should match the behaviour of TouchVideoDevice.
6375 processPosition(mapper, 200, 200);
6376 processSync(mapper);
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6379}
6380
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006381TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6382 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6383 prepareAxes(POSITION);
6384 addConfigurationProperty("touch.deviceType", "touchScreen");
6385 addMapperAndConfigure(mapper);
6386 // Unrotated video frame
6387 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6388 NotifyMotionArgs motionArgs;
6389
6390 // Test all 4 orientations
6391 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6392 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6393 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6394 clearViewports();
6395 prepareDisplay(orientation);
6396 std::vector<TouchVideoFrame> frames{frame};
6397 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6398 processPosition(mapper, 100, 200);
6399 processSync(mapper);
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 frames[0].rotate(orientation);
6402 ASSERT_EQ(frames, motionArgs.videoFrames);
6403 }
6404}
6405
6406TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6407 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6408 prepareAxes(POSITION);
6409 addConfigurationProperty("touch.deviceType", "touchScreen");
6410 addMapperAndConfigure(mapper);
6411 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6412 // so mix these.
6413 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6414 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6415 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6416 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6417 NotifyMotionArgs motionArgs;
6418
6419 prepareDisplay(DISPLAY_ORIENTATION_90);
6420 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6421 processPosition(mapper, 100, 200);
6422 processSync(mapper);
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 std::for_each(frames.begin(), frames.end(),
6425 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6426 ASSERT_EQ(frames, motionArgs.videoFrames);
6427}
6428
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429} // namespace android