blob: d95ac961245584b93716f7cf4f8746cf01b3fd81 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../InputReader.h"
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080018#include "TestInputListener.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Michael Wrightd02c5b62014-02-10 15:10:22 -080020#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080021#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <math.h>
23
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080024
Michael Wrightd02c5b62014-02-10 15:10:22 -080025namespace android {
26
27// An arbitrary time value.
28static const nsecs_t ARBITRARY_TIME = 1234;
29
30// Arbitrary display properties.
31static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070032static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080033static const int32_t DISPLAY_WIDTH = 480;
34static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070035static const int32_t VIRTUAL_DISPLAY_ID = 1;
36static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
37static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070038static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070039static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
41// Error tolerance for floating point assertions.
42static const float EPSILON = 0.001f;
43
44template<typename T>
45static inline T min(T a, T b) {
46 return a < b ? a : b;
47}
48
49static inline float avg(float x, float y) {
50 return (x + y) / 2;
51}
52
53
54// --- FakePointerController ---
55
56class FakePointerController : public PointerControllerInterface {
57 bool mHaveBounds;
58 float mMinX, mMinY, mMaxX, mMaxY;
59 float mX, mY;
60 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080061 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
63protected:
64 virtual ~FakePointerController() { }
65
66public:
67 FakePointerController() :
68 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080069 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080070 }
71
72 void setBounds(float minX, float minY, float maxX, float maxY) {
73 mHaveBounds = true;
74 mMinX = minX;
75 mMinY = minY;
76 mMaxX = maxX;
77 mMaxY = maxY;
78 }
79
Arthur Hungc7ad2d02018-12-18 17:41:29 +080080 void setDisplayId(int32_t displayId) {
81 mDisplayId = displayId;
82 }
83
Michael Wrightd02c5b62014-02-10 15:10:22 -080084 virtual void setPosition(float x, float y) {
85 mX = x;
86 mY = y;
87 }
88
89 virtual void setButtonState(int32_t buttonState) {
90 mButtonState = buttonState;
91 }
92
93 virtual int32_t getButtonState() const {
94 return mButtonState;
95 }
96
97 virtual void getPosition(float* outX, float* outY) const {
98 *outX = mX;
99 *outY = mY;
100 }
101
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800102 virtual int32_t getDisplayId() const {
103 return mDisplayId;
104 }
105
Arthur Hung7c645402019-01-25 17:45:42 +0800106 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
107 return mSpotsByDisplay;
108 }
109
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110private:
111 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
112 *outMinX = mMinX;
113 *outMinY = mMinY;
114 *outMaxX = mMaxX;
115 *outMaxY = mMaxY;
116 return mHaveBounds;
117 }
118
119 virtual void move(float deltaX, float deltaY) {
120 mX += deltaX;
121 if (mX < mMinX) mX = mMinX;
122 if (mX > mMaxX) mX = mMaxX;
123 mY += deltaY;
124 if (mY < mMinY) mY = mMinY;
125 if (mY > mMaxY) mY = mMaxY;
126 }
127
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100128 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129 }
130
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100131 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 }
133
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100134 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 }
136
Arthur Hung7c645402019-01-25 17:45:42 +0800137 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
138 int32_t displayId) {
139 std::vector<int32_t> newSpots;
140 // Add spots for fingers that are down.
141 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
142 uint32_t id = idBits.clearFirstMarkedBit();
143 newSpots.push_back(id);
144 }
145
146 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147 }
148
149 virtual void clearSpots() {
150 }
Arthur Hung7c645402019-01-25 17:45:42 +0800151
152 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153};
154
155
156// --- FakeInputReaderPolicy ---
157
158class FakeInputReaderPolicy : public InputReaderPolicyInterface {
159 InputReaderConfiguration mConfig;
160 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800161 std::vector<InputDeviceInfo> mInputDevices;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100162 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700163 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
165protected:
166 virtual ~FakeInputReaderPolicy() { }
167
168public:
169 FakeInputReaderPolicy() {
170 }
171
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700172 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100173 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100174 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700175 }
176
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700177 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
178 return mConfig.getDisplayViewportByUniqueId(uniqueId);
179 }
180 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
181 return mConfig.getDisplayViewportByType(type);
182 }
183
184 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
185 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700186 }
187
188 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700189 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
190 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700191 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700192 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700193 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100194 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 }
196
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100197 void addExcludedDeviceName(const std::string& deviceName) {
198 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199 }
200
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700201 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
202 mConfig.portAssociations.insert({inputPort, displayPort});
203 }
204
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000205 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700206
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000207 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700208
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
210 mPointerControllers.add(deviceId, controller);
211 }
212
213 const InputReaderConfiguration* getReaderConfiguration() const {
214 return &mConfig;
215 }
216
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800217 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 return mInputDevices;
219 }
220
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100221 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700222 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700223 return transform;
224 }
225
226 void setTouchAffineTransformation(const TouchAffineTransformation t) {
227 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800228 }
229
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800230 void setPointerCapture(bool enabled) {
231 mConfig.pointerCapture = enabled;
232 }
233
Arthur Hung7c645402019-01-25 17:45:42 +0800234 void setShowTouches(bool enabled) {
235 mConfig.showTouches = enabled;
236 }
237
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700239 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700240 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
241 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700242 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
243 || orientation == DISPLAY_ORIENTATION_270);
244 DisplayViewport v;
245 v.displayId = displayId;
246 v.orientation = orientation;
247 v.logicalLeft = 0;
248 v.logicalTop = 0;
249 v.logicalRight = isRotated ? height : width;
250 v.logicalBottom = isRotated ? width : height;
251 v.physicalLeft = 0;
252 v.physicalTop = 0;
253 v.physicalRight = isRotated ? height : width;
254 v.physicalBottom = isRotated ? width : height;
255 v.deviceWidth = isRotated ? height : width;
256 v.deviceHeight = isRotated ? width : height;
257 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700258 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100259 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700260 return v;
261 }
262
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
264 *outConfig = mConfig;
265 }
266
267 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
268 return mPointerControllers.valueFor(deviceId);
269 }
270
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800271 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 mInputDevices = inputDevices;
273 }
274
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100275 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700276 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277 }
278
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100279 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
280 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 }
282};
283
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284// --- FakeEventHub ---
285
286class FakeEventHub : public EventHubInterface {
287 struct KeyInfo {
288 int32_t keyCode;
289 uint32_t flags;
290 };
291
292 struct Device {
293 InputDeviceIdentifier identifier;
294 uint32_t classes;
295 PropertyMap configuration;
296 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
297 KeyedVector<int, bool> relativeAxes;
298 KeyedVector<int32_t, int32_t> keyCodeStates;
299 KeyedVector<int32_t, int32_t> scanCodeStates;
300 KeyedVector<int32_t, int32_t> switchStates;
301 KeyedVector<int32_t, int32_t> absoluteAxisValue;
302 KeyedVector<int32_t, KeyInfo> keysByScanCode;
303 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
304 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800305 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700306 bool enabled;
307
308 status_t enable() {
309 enabled = true;
310 return OK;
311 }
312
313 status_t disable() {
314 enabled = false;
315 return OK;
316 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700318 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700319 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 }
321 };
322
323 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100324 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 List<RawEvent> mEvents;
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600326 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700328public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 virtual ~FakeEventHub() {
330 for (size_t i = 0; i < mDevices.size(); i++) {
331 delete mDevices.valueAt(i);
332 }
333 }
334
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 FakeEventHub() { }
336
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100337 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 Device* device = new Device(classes);
339 device->identifier.name = name;
340 mDevices.add(deviceId, device);
341
342 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
343 }
344
345 void removeDevice(int32_t deviceId) {
346 delete mDevices.valueFor(deviceId);
347 mDevices.removeItem(deviceId);
348
349 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
350 }
351
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700352 bool isDeviceEnabled(int32_t deviceId) {
353 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700354 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700355 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
356 return false;
357 }
358 return device->enabled;
359 }
360
361 status_t enableDevice(int32_t deviceId) {
362 status_t result;
363 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700364 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700365 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
366 return BAD_VALUE;
367 }
368 if (device->enabled) {
369 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
370 return OK;
371 }
372 result = device->enable();
373 return result;
374 }
375
376 status_t disableDevice(int32_t deviceId) {
377 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700378 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700379 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
380 return BAD_VALUE;
381 }
382 if (!device->enabled) {
383 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
384 return OK;
385 }
386 return device->disable();
387 }
388
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 void finishDeviceScan() {
390 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
391 }
392
393 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
394 Device* device = getDevice(deviceId);
395 device->configuration.addProperty(key, value);
396 }
397
398 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
399 Device* device = getDevice(deviceId);
400 device->configuration.addAll(configuration);
401 }
402
403 void addAbsoluteAxis(int32_t deviceId, int axis,
404 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
405 Device* device = getDevice(deviceId);
406
407 RawAbsoluteAxisInfo info;
408 info.valid = true;
409 info.minValue = minValue;
410 info.maxValue = maxValue;
411 info.flat = flat;
412 info.fuzz = fuzz;
413 info.resolution = resolution;
414 device->absoluteAxes.add(axis, info);
415 }
416
417 void addRelativeAxis(int32_t deviceId, int32_t axis) {
418 Device* device = getDevice(deviceId);
419 device->relativeAxes.add(axis, true);
420 }
421
422 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
423 Device* device = getDevice(deviceId);
424 device->keyCodeStates.replaceValueFor(keyCode, state);
425 }
426
427 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
428 Device* device = getDevice(deviceId);
429 device->scanCodeStates.replaceValueFor(scanCode, state);
430 }
431
432 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
433 Device* device = getDevice(deviceId);
434 device->switchStates.replaceValueFor(switchCode, state);
435 }
436
437 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
438 Device* device = getDevice(deviceId);
439 device->absoluteAxisValue.replaceValueFor(axis, value);
440 }
441
442 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
443 int32_t keyCode, uint32_t flags) {
444 Device* device = getDevice(deviceId);
445 KeyInfo info;
446 info.keyCode = keyCode;
447 info.flags = flags;
448 if (scanCode) {
449 device->keysByScanCode.add(scanCode, info);
450 }
451 if (usageCode) {
452 device->keysByUsageCode.add(usageCode, info);
453 }
454 }
455
456 void addLed(int32_t deviceId, int32_t led, bool initialState) {
457 Device* device = getDevice(deviceId);
458 device->leds.add(led, initialState);
459 }
460
461 bool getLedState(int32_t deviceId, int32_t led) {
462 Device* device = getDevice(deviceId);
463 return device->leds.valueFor(led);
464 }
465
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100466 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467 return mExcludedDevices;
468 }
469
470 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
471 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800472 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473 }
474
475 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
476 int32_t code, int32_t value) {
477 RawEvent event;
478 event.when = when;
479 event.deviceId = deviceId;
480 event.type = type;
481 event.code = code;
482 event.value = value;
483 mEvents.push_back(event);
484
485 if (type == EV_ABS) {
486 setAbsoluteAxisValue(deviceId, code, value);
487 }
488 }
489
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600490 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
491 std::vector<TouchVideoFrame>> videoFrames) {
492 mVideoFrames = std::move(videoFrames);
493 }
494
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495 void assertQueueIsEmpty() {
496 ASSERT_EQ(size_t(0), mEvents.size())
497 << "Expected the event queue to be empty (fully consumed).";
498 }
499
500private:
501 Device* getDevice(int32_t deviceId) const {
502 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100503 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 }
505
506 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
507 Device* device = getDevice(deviceId);
508 return device ? device->classes : 0;
509 }
510
511 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
512 Device* device = getDevice(deviceId);
513 return device ? device->identifier : InputDeviceIdentifier();
514 }
515
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100516 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 return 0;
518 }
519
520 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
521 Device* device = getDevice(deviceId);
522 if (device) {
523 *outConfiguration = device->configuration;
524 }
525 }
526
527 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
528 RawAbsoluteAxisInfo* outAxisInfo) const {
529 Device* device = getDevice(deviceId);
530 if (device) {
531 ssize_t index = device->absoluteAxes.indexOfKey(axis);
532 if (index >= 0) {
533 *outAxisInfo = device->absoluteAxes.valueAt(index);
534 return OK;
535 }
536 }
537 outAxisInfo->clear();
538 return -1;
539 }
540
541 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
542 Device* device = getDevice(deviceId);
543 if (device) {
544 return device->relativeAxes.indexOfKey(axis) >= 0;
545 }
546 return false;
547 }
548
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100549 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 return false;
551 }
552
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700553 virtual status_t mapKey(int32_t deviceId,
554 int32_t scanCode, int32_t usageCode, int32_t metaState,
555 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556 Device* device = getDevice(deviceId);
557 if (device) {
558 const KeyInfo* key = getKey(device, scanCode, usageCode);
559 if (key) {
560 if (outKeycode) {
561 *outKeycode = key->keyCode;
562 }
563 if (outFlags) {
564 *outFlags = key->flags;
565 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700566 if (outMetaState) {
567 *outMetaState = metaState;
568 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569 return OK;
570 }
571 }
572 return NAME_NOT_FOUND;
573 }
574
575 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
576 if (usageCode) {
577 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
578 if (index >= 0) {
579 return &device->keysByUsageCode.valueAt(index);
580 }
581 }
582 if (scanCode) {
583 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
584 if (index >= 0) {
585 return &device->keysByScanCode.valueAt(index);
586 }
587 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700588 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100591 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 return NAME_NOT_FOUND;
593 }
594
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100595 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 mExcludedDevices = devices;
597 }
598
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100599 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 if (mEvents.empty()) {
601 return 0;
602 }
603
604 *buffer = *mEvents.begin();
605 mEvents.erase(mEvents.begin());
606 return 1;
607 }
608
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800609 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600610 auto it = mVideoFrames.find(deviceId);
611 if (it != mVideoFrames.end()) {
612 std::vector<TouchVideoFrame> frames = std::move(it->second);
613 mVideoFrames.erase(deviceId);
614 return frames;
615 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800616 return {};
617 }
618
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
620 Device* device = getDevice(deviceId);
621 if (device) {
622 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
623 if (index >= 0) {
624 return device->scanCodeStates.valueAt(index);
625 }
626 }
627 return AKEY_STATE_UNKNOWN;
628 }
629
630 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
631 Device* device = getDevice(deviceId);
632 if (device) {
633 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
634 if (index >= 0) {
635 return device->keyCodeStates.valueAt(index);
636 }
637 }
638 return AKEY_STATE_UNKNOWN;
639 }
640
641 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
642 Device* device = getDevice(deviceId);
643 if (device) {
644 ssize_t index = device->switchStates.indexOfKey(sw);
645 if (index >= 0) {
646 return device->switchStates.valueAt(index);
647 }
648 }
649 return AKEY_STATE_UNKNOWN;
650 }
651
652 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
653 int32_t* outValue) const {
654 Device* device = getDevice(deviceId);
655 if (device) {
656 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
657 if (index >= 0) {
658 *outValue = device->absoluteAxisValue.valueAt(index);
659 return OK;
660 }
661 }
662 *outValue = 0;
663 return -1;
664 }
665
666 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
667 uint8_t* outFlags) const {
668 bool result = false;
669 Device* device = getDevice(deviceId);
670 if (device) {
671 for (size_t i = 0; i < numCodes; i++) {
672 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
673 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
674 outFlags[i] = 1;
675 result = true;
676 }
677 }
678 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
679 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
680 outFlags[i] = 1;
681 result = true;
682 }
683 }
684 }
685 }
686 return result;
687 }
688
689 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
690 Device* device = getDevice(deviceId);
691 if (device) {
692 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
693 return index >= 0;
694 }
695 return false;
696 }
697
698 virtual bool hasLed(int32_t deviceId, int32_t led) const {
699 Device* device = getDevice(deviceId);
700 return device && device->leds.indexOfKey(led) >= 0;
701 }
702
703 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
704 Device* device = getDevice(deviceId);
705 if (device) {
706 ssize_t index = device->leds.indexOfKey(led);
707 if (index >= 0) {
708 device->leds.replaceValueAt(led, on);
709 } else {
710 ADD_FAILURE()
711 << "Attempted to set the state of an LED that the EventHub declared "
712 "was not present. led=" << led;
713 }
714 }
715 }
716
717 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800718 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 outVirtualKeys.clear();
720
721 Device* device = getDevice(deviceId);
722 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800723 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 }
725 }
726
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100727 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700728 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 }
730
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100731 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732 return false;
733 }
734
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100735 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800736 }
737
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100738 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 }
740
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100741 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 return false;
743 }
744
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800745 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746 }
747
748 virtual void monitor() {
749 }
750
751 virtual void requestReopenDevices() {
752 }
753
754 virtual void wake() {
755 }
756};
757
758
759// --- FakeInputReaderContext ---
760
761class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700762 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 sp<InputReaderPolicyInterface> mPolicy;
764 sp<InputListenerInterface> mListener;
765 int32_t mGlobalMetaState;
766 bool mUpdateGlobalMetaStateWasCalled;
767 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800768 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769
770public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700771 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
772 const sp<InputReaderPolicyInterface>& policy,
773 const sp<InputListenerInterface>& listener)
774 : mEventHub(eventHub),
775 mPolicy(policy),
776 mListener(listener),
777 mGlobalMetaState(0),
778 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779
780 virtual ~FakeInputReaderContext() { }
781
782 void assertUpdateGlobalMetaStateWasCalled() {
783 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
784 << "Expected updateGlobalMetaState() to have been called.";
785 mUpdateGlobalMetaStateWasCalled = false;
786 }
787
788 void setGlobalMetaState(int32_t state) {
789 mGlobalMetaState = state;
790 }
791
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800792 uint32_t getGeneration() {
793 return mGeneration;
794 }
795
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796private:
797 virtual void updateGlobalMetaState() {
798 mUpdateGlobalMetaStateWasCalled = true;
799 }
800
801 virtual int32_t getGlobalMetaState() {
802 return mGlobalMetaState;
803 }
804
805 virtual EventHubInterface* getEventHub() {
806 return mEventHub.get();
807 }
808
809 virtual InputReaderPolicyInterface* getPolicy() {
810 return mPolicy.get();
811 }
812
813 virtual InputListenerInterface* getListener() {
814 return mListener.get();
815 }
816
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100817 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 }
819
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100820 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 return false;
822 }
823
824 virtual void fadePointer() {
825 }
826
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100827 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 }
829
830 virtual int32_t bumpGeneration() {
831 return ++mGeneration;
832 }
Michael Wright842500e2015-03-13 17:32:02 -0700833
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800834 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700835
836 }
837
838 virtual void dispatchExternalStylusState(const StylusState&) {
839
840 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800841
842 virtual uint32_t getNextSequenceNum() {
843 return mNextSequenceNum++;
844 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845};
846
847
848// --- FakeInputMapper ---
849
850class FakeInputMapper : public InputMapper {
851 uint32_t mSources;
852 int32_t mKeyboardType;
853 int32_t mMetaState;
854 KeyedVector<int32_t, int32_t> mKeyCodeStates;
855 KeyedVector<int32_t, int32_t> mScanCodeStates;
856 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800857 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858 RawEvent mLastEvent;
859
860 bool mConfigureWasCalled;
861 bool mResetWasCalled;
862 bool mProcessWasCalled;
863
Arthur Hungc23540e2018-11-29 20:42:11 +0800864 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865public:
866 FakeInputMapper(InputDevice* device, uint32_t sources) :
867 InputMapper(device),
868 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
869 mMetaState(0),
870 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
871 }
872
873 virtual ~FakeInputMapper() { }
874
875 void setKeyboardType(int32_t keyboardType) {
876 mKeyboardType = keyboardType;
877 }
878
879 void setMetaState(int32_t metaState) {
880 mMetaState = metaState;
881 }
882
883 void assertConfigureWasCalled() {
884 ASSERT_TRUE(mConfigureWasCalled)
885 << "Expected configure() to have been called.";
886 mConfigureWasCalled = false;
887 }
888
889 void assertResetWasCalled() {
890 ASSERT_TRUE(mResetWasCalled)
891 << "Expected reset() to have been called.";
892 mResetWasCalled = false;
893 }
894
Yi Kong9b14ac62018-07-17 13:48:38 -0700895 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 ASSERT_TRUE(mProcessWasCalled)
897 << "Expected process() to have been called.";
898 if (outLastEvent) {
899 *outLastEvent = mLastEvent;
900 }
901 mProcessWasCalled = false;
902 }
903
904 void setKeyCodeState(int32_t keyCode, int32_t state) {
905 mKeyCodeStates.replaceValueFor(keyCode, state);
906 }
907
908 void setScanCodeState(int32_t scanCode, int32_t state) {
909 mScanCodeStates.replaceValueFor(scanCode, state);
910 }
911
912 void setSwitchState(int32_t switchCode, int32_t state) {
913 mSwitchStates.replaceValueFor(switchCode, state);
914 }
915
916 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800917 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 }
919
920private:
921 virtual uint32_t getSources() {
922 return mSources;
923 }
924
925 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
926 InputMapper::populateDeviceInfo(deviceInfo);
927
928 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
929 deviceInfo->setKeyboardType(mKeyboardType);
930 }
931 }
932
Arthur Hungc23540e2018-11-29 20:42:11 +0800933 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800935
936 // Find the associated viewport if exist.
937 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
938 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
939 mViewport = config->getDisplayViewportByPort(*displayPort);
940 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 }
942
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100943 virtual void reset(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 mResetWasCalled = true;
945 }
946
947 virtual void process(const RawEvent* rawEvent) {
948 mLastEvent = *rawEvent;
949 mProcessWasCalled = true;
950 }
951
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100952 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
954 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
955 }
956
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100957 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
959 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
960 }
961
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100962 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963 ssize_t index = mSwitchStates.indexOfKey(switchCode);
964 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
965 }
966
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100967 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 const int32_t* keyCodes, uint8_t* outFlags) {
969 bool result = false;
970 for (size_t i = 0; i < numCodes; i++) {
971 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
972 if (keyCodes[i] == mSupportedKeyCodes[j]) {
973 outFlags[i] = 1;
974 result = true;
975 }
976 }
977 }
978 return result;
979 }
980
981 virtual int32_t getMetaState() {
982 return mMetaState;
983 }
984
985 virtual void fadePointer() {
986 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800987
988 virtual std::optional<int32_t> getAssociatedDisplay() {
989 if (mViewport) {
990 return std::make_optional(mViewport->displayId);
991 }
992 return std::nullopt;
993 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994};
995
996
997// --- InstrumentedInputReader ---
998
999class InstrumentedInputReader : public InputReader {
1000 InputDevice* mNextDevice;
1001
1002public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001003 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1004 const sp<InputReaderPolicyInterface>& policy,
1005 const sp<InputListenerInterface>& listener)
1006 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007
1008 virtual ~InstrumentedInputReader() {
1009 if (mNextDevice) {
1010 delete mNextDevice;
1011 }
1012 }
1013
1014 void setNextDevice(InputDevice* device) {
1015 mNextDevice = device;
1016 }
1017
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001018 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Arthur Hungc23540e2018-11-29 20:42:11 +08001019 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020 InputDeviceIdentifier identifier;
1021 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001022 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 int32_t generation = deviceId + 1;
1024 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
1025 classes);
1026 }
1027
1028protected:
1029 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
1030 const InputDeviceIdentifier& identifier, uint32_t classes) {
1031 if (mNextDevice) {
1032 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001033 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 return device;
1035 }
1036 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1037 }
1038
1039 friend class InputReaderTest;
1040};
1041
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001042// --- InputReaderPolicyTest ---
1043class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001044protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001045 sp<FakeInputReaderPolicy> mFakePolicy;
1046
1047 virtual void SetUp() {
1048 mFakePolicy = new FakeInputReaderPolicy();
1049 }
1050 virtual void TearDown() {
1051 mFakePolicy.clear();
1052 }
1053};
1054
1055/**
1056 * Check that empty set of viewports is an acceptable configuration.
1057 * Also try to get internal viewport two different ways - by type and by uniqueId.
1058 *
1059 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1060 * Such configuration is not currently allowed.
1061 */
1062TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001063 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001064
1065 // We didn't add any viewports yet, so there shouldn't be any.
1066 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001067 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001068 ASSERT_FALSE(internalViewport);
1069
1070 // Add an internal viewport, then clear it
1071 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001072 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001073
1074 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001075 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001076 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001077 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001078
1079 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001080 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001081 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001082 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001083
1084 mFakePolicy->clearViewports();
1085 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001086 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001087 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001088 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001089 ASSERT_FALSE(internalViewport);
1090}
1091
1092TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1093 const std::string internalUniqueId = "local:0";
1094 const std::string externalUniqueId = "local:1";
1095 const std::string virtualUniqueId1 = "virtual:2";
1096 const std::string virtualUniqueId2 = "virtual:3";
1097 constexpr int32_t virtualDisplayId1 = 2;
1098 constexpr int32_t virtualDisplayId2 = 3;
1099
1100 // Add an internal viewport
1101 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001102 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001103 // Add an external viewport
1104 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001105 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001106 // Add an virtual viewport
1107 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001108 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001109 // Add another virtual viewport
1110 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001111 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001112
1113 // Check matching by type for internal
1114 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001115 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001116 ASSERT_TRUE(internalViewport);
1117 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1118
1119 // Check matching by type for external
1120 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001121 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001122 ASSERT_TRUE(externalViewport);
1123 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1124
1125 // Check matching by uniqueId for virtual viewport #1
1126 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001127 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001128 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001129 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001130 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1131 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1132
1133 // Check matching by uniqueId for virtual viewport #2
1134 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001135 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001136 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001137 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001138 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1139 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1140}
1141
1142
1143/**
1144 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1145 * that lookup works by checking display id.
1146 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1147 */
1148TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1149 const std::string uniqueId1 = "uniqueId1";
1150 const std::string uniqueId2 = "uniqueId2";
1151 constexpr int32_t displayId1 = 2;
1152 constexpr int32_t displayId2 = 3;
1153
1154 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1155 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1156 for (const ViewportType& type : types) {
1157 mFakePolicy->clearViewports();
1158 // Add a viewport
1159 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001160 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001161 // Add another viewport
1162 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001163 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001164
1165 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001166 std::optional<DisplayViewport> viewport1 =
1167 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168 ASSERT_TRUE(viewport1);
1169 ASSERT_EQ(displayId1, viewport1->displayId);
1170 ASSERT_EQ(type, viewport1->type);
1171
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001172 std::optional<DisplayViewport> viewport2 =
1173 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001174 ASSERT_TRUE(viewport2);
1175 ASSERT_EQ(displayId2, viewport2->displayId);
1176 ASSERT_EQ(type, viewport2->type);
1177
1178 // When there are multiple viewports of the same kind, and uniqueId is not specified
1179 // in the call to getDisplayViewport, then that situation is not supported.
1180 // The viewports can be stored in any order, so we cannot rely on the order, since that
1181 // is just implementation detail.
1182 // However, we can check that it still returns *a* viewport, we just cannot assert
1183 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001184 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185 ASSERT_TRUE(someViewport);
1186 }
1187}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001189/**
1190 * Check getDisplayViewportByPort
1191 */
1192TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1193 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1194 const std::string uniqueId1 = "uniqueId1";
1195 const std::string uniqueId2 = "uniqueId2";
1196 constexpr int32_t displayId1 = 1;
1197 constexpr int32_t displayId2 = 2;
1198 const uint8_t hdmi1 = 0;
1199 const uint8_t hdmi2 = 1;
1200 const uint8_t hdmi3 = 2;
1201
1202 mFakePolicy->clearViewports();
1203 // Add a viewport that's associated with some display port that's not of interest.
1204 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1205 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1206 // Add another viewport, connected to HDMI1 port
1207 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1208 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1209
1210 // Check that correct display viewport was returned by comparing the display ports.
1211 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1212 ASSERT_TRUE(hdmi1Viewport);
1213 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1214 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1215
1216 // Check that we can still get the same viewport using the uniqueId
1217 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1218 ASSERT_TRUE(hdmi1Viewport);
1219 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1220 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1221 ASSERT_EQ(type, hdmi1Viewport->type);
1222
1223 // Check that we cannot find a port with "HDMI2", because we never added one
1224 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1225 ASSERT_FALSE(hdmi2Viewport);
1226}
1227
Michael Wrightd02c5b62014-02-10 15:10:22 -08001228// --- InputReaderTest ---
1229
1230class InputReaderTest : public testing::Test {
1231protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001232 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001234 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 sp<InstrumentedInputReader> mReader;
1236
1237 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001238 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001240 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241
1242 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1243 }
1244
1245 virtual void TearDown() {
1246 mReader.clear();
1247
1248 mFakeListener.clear();
1249 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 }
1251
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001252 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 const PropertyMap* configuration) {
1254 mFakeEventHub->addDevice(deviceId, name, classes);
1255
1256 if (configuration) {
1257 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1258 }
1259 mFakeEventHub->finishDeviceScan();
1260 mReader->loopOnce();
1261 mReader->loopOnce();
1262 mFakeEventHub->assertQueueIsEmpty();
1263 }
1264
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001265 void disableDevice(int32_t deviceId, InputDevice* device) {
1266 mFakePolicy->addDisabledDevice(deviceId);
1267 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1268 }
1269
1270 void enableDevice(int32_t deviceId, InputDevice* device) {
1271 mFakePolicy->removeDisabledDevice(deviceId);
1272 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1273 }
1274
1275 void configureDevice(uint32_t changes, InputDevice* device) {
1276 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1277 }
1278
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001280 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 const PropertyMap* configuration) {
1282 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1283 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1284 device->addMapper(mapper);
1285 mReader->setNextDevice(device);
1286 addDevice(deviceId, name, classes, configuration);
1287 return mapper;
1288 }
1289};
1290
1291TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001292 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001293 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001294 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001295 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001296
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001297
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001298 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001299 mReader->getInputDevices(inputDevices);
1300
1301 ASSERT_EQ(1U, inputDevices.size());
1302 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001303 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001304 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1305 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1306 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1307
1308 // Should also have received a notification describing the new input devices.
1309 inputDevices = mFakePolicy->getInputDevices();
1310 ASSERT_EQ(1U, inputDevices.size());
1311 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001312 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1314 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1315 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1316}
1317
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001318TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1319 constexpr int32_t deviceId = 1;
1320 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001321 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001322 // Must add at least one mapper or the device will be ignored!
1323 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1324 device->addMapper(mapper);
1325 mReader->setNextDevice(device);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001326 addDevice(deviceId, "fake", deviceClass, nullptr);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001327
Yi Kong9b14ac62018-07-17 13:48:38 -07001328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001329
1330 NotifyDeviceResetArgs resetArgs;
1331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1332 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1333 ASSERT_EQ(deviceId, resetArgs.deviceId);
1334
1335 ASSERT_EQ(device->isEnabled(), true);
1336 disableDevice(deviceId, device);
1337 mReader->loopOnce();
1338
1339 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1340 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1341 ASSERT_EQ(deviceId, resetArgs.deviceId);
1342 ASSERT_EQ(device->isEnabled(), false);
1343
1344 disableDevice(deviceId, device);
1345 mReader->loopOnce();
1346 mFakeListener->assertNotifyDeviceResetWasNotCalled();
1347 mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1348 ASSERT_EQ(device->isEnabled(), false);
1349
1350 enableDevice(deviceId, device);
1351 mReader->loopOnce();
1352 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1353 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1354 ASSERT_EQ(deviceId, resetArgs.deviceId);
1355 ASSERT_EQ(device->isEnabled(), true);
1356}
1357
Michael Wrightd02c5b62014-02-10 15:10:22 -08001358TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001359 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001360 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001361 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001362 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1363
1364 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1365 AINPUT_SOURCE_ANY, AKEYCODE_A))
1366 << "Should return unknown when the device id is >= 0 but unknown.";
1367
1368 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1369 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1370 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1371
1372 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1373 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1374 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1375
1376 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1377 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1378 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1379
1380 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1381 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1382 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1383}
1384
1385TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001386 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001387 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001388 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001389 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1390
1391 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1392 AINPUT_SOURCE_ANY, KEY_A))
1393 << "Should return unknown when the device id is >= 0 but unknown.";
1394
1395 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1396 AINPUT_SOURCE_TRACKBALL, KEY_A))
1397 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1398
1399 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1400 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1401 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1402
1403 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1404 AINPUT_SOURCE_TRACKBALL, KEY_A))
1405 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1406
1407 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1408 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1409 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1410}
1411
1412TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001413 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001414 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001415 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1417
1418 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1419 AINPUT_SOURCE_ANY, SW_LID))
1420 << "Should return unknown when the device id is >= 0 but unknown.";
1421
1422 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1423 AINPUT_SOURCE_TRACKBALL, SW_LID))
1424 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1425
1426 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1427 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1428 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1429
1430 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1431 AINPUT_SOURCE_TRACKBALL, SW_LID))
1432 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1433
1434 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1435 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1436 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1437}
1438
1439TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001440 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001441 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001442 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001443
Michael Wrightd02c5b62014-02-10 15:10:22 -08001444 mapper->addSupportedKeyCode(AKEYCODE_A);
1445 mapper->addSupportedKeyCode(AKEYCODE_B);
1446
1447 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1448 uint8_t flags[4] = { 0, 0, 0, 1 };
1449
1450 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1451 << "Should return false when device id is >= 0 but unknown.";
1452 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1453
1454 flags[3] = 1;
1455 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1456 << "Should return false when device id is valid but the sources are not supported by the device.";
1457 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1458
1459 flags[3] = 1;
1460 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1461 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1462 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1463
1464 flags[3] = 1;
1465 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1466 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1467 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1468
1469 flags[3] = 1;
1470 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1471 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1472 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1473}
1474
1475TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001476 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001477
1478 NotifyConfigurationChangedArgs args;
1479
1480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1481 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1482}
1483
1484TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001485 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001486 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001487 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001488
1489 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1490 mReader->loopOnce();
1491 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1492
1493 RawEvent event;
1494 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1495 ASSERT_EQ(0, event.when);
1496 ASSERT_EQ(1, event.deviceId);
1497 ASSERT_EQ(EV_KEY, event.type);
1498 ASSERT_EQ(KEY_A, event.code);
1499 ASSERT_EQ(1, event.value);
1500}
1501
Prabir Pradhan42611e02018-11-27 14:04:02 -08001502TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1503 constexpr int32_t deviceId = 1;
1504 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001505 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001506 // Must add at least one mapper or the device will be ignored!
1507 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1508 device->addMapper(mapper);
1509 mReader->setNextDevice(device);
1510 addDevice(deviceId, "fake", deviceClass, nullptr);
1511
1512 NotifyDeviceResetArgs resetArgs;
1513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1514 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1515
1516 disableDevice(deviceId, device);
1517 mReader->loopOnce();
1518 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1519 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1520 prevSequenceNum = resetArgs.sequenceNum;
1521
1522 enableDevice(deviceId, device);
1523 mReader->loopOnce();
1524 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1525 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1526 prevSequenceNum = resetArgs.sequenceNum;
1527
1528 disableDevice(deviceId, device);
1529 mReader->loopOnce();
1530 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1531 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1532 prevSequenceNum = resetArgs.sequenceNum;
1533}
1534
Arthur Hungc23540e2018-11-29 20:42:11 +08001535TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1536 constexpr int32_t deviceId = 1;
1537 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1538 const char* DEVICE_LOCATION = "USB1";
1539 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1540 DEVICE_LOCATION);
1541 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1542 device->addMapper(mapper);
1543 mReader->setNextDevice(device);
1544 addDevice(deviceId, "fake", deviceClass, nullptr);
1545
1546 const uint8_t hdmi1 = 1;
1547
1548 // Associated touch screen with second display.
1549 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1550
1551 // Add default and second display.
1552 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1553 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1554 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1555 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1556 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1557 mReader->loopOnce();
1558
1559 // Check device.
1560 ASSERT_EQ(deviceId, device->getId());
1561 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1562 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
1563}
1564
Michael Wrightd02c5b62014-02-10 15:10:22 -08001565
1566// --- InputDeviceTest ---
1567
1568class InputDeviceTest : public testing::Test {
1569protected:
1570 static const char* DEVICE_NAME;
1571 static const int32_t DEVICE_ID;
1572 static const int32_t DEVICE_GENERATION;
1573 static const int32_t DEVICE_CONTROLLER_NUMBER;
1574 static const uint32_t DEVICE_CLASSES;
1575
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001576 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001578 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 FakeInputReaderContext* mFakeContext;
1580
1581 InputDevice* mDevice;
1582
1583 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001584 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001586 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1588
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001589 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 InputDeviceIdentifier identifier;
1591 identifier.name = DEVICE_NAME;
1592 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1593 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1594 }
1595
1596 virtual void TearDown() {
1597 delete mDevice;
1598
1599 delete mFakeContext;
1600 mFakeListener.clear();
1601 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001602 }
1603};
1604
1605const char* InputDeviceTest::DEVICE_NAME = "device";
1606const int32_t InputDeviceTest::DEVICE_ID = 1;
1607const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1608const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1609const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1610 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1611
1612TEST_F(InputDeviceTest, ImmutableProperties) {
1613 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001614 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1616}
1617
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001618TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1619 ASSERT_EQ(mDevice->isEnabled(), true);
1620}
1621
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1623 // Configuration.
1624 InputReaderConfiguration config;
1625 mDevice->configure(ARBITRARY_TIME, &config, 0);
1626
1627 // Reset.
1628 mDevice->reset(ARBITRARY_TIME);
1629
1630 NotifyDeviceResetArgs resetArgs;
1631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1632 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1633 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1634
1635 // Metadata.
1636 ASSERT_TRUE(mDevice->isIgnored());
1637 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1638
1639 InputDeviceInfo info;
1640 mDevice->getDeviceInfo(&info);
1641 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001642 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1644 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1645
1646 // State queries.
1647 ASSERT_EQ(0, mDevice->getMetaState());
1648
1649 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1650 << "Ignored device should return unknown key code state.";
1651 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1652 << "Ignored device should return unknown scan code state.";
1653 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1654 << "Ignored device should return unknown switch state.";
1655
1656 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1657 uint8_t flags[2] = { 0, 1 };
1658 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1659 << "Ignored device should never mark any key codes.";
1660 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1661 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1662}
1663
1664TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1665 // Configuration.
1666 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1667
1668 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1669 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1670 mapper1->setMetaState(AMETA_ALT_ON);
1671 mapper1->addSupportedKeyCode(AKEYCODE_A);
1672 mapper1->addSupportedKeyCode(AKEYCODE_B);
1673 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1674 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1675 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1676 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1677 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1678 mDevice->addMapper(mapper1);
1679
1680 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1681 mapper2->setMetaState(AMETA_SHIFT_ON);
1682 mDevice->addMapper(mapper2);
1683
1684 InputReaderConfiguration config;
1685 mDevice->configure(ARBITRARY_TIME, &config, 0);
1686
1687 String8 propertyValue;
1688 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1689 << "Device should have read configuration during configuration phase.";
1690 ASSERT_STREQ("value", propertyValue.string());
1691
1692 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1693 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1694
1695 // Reset
1696 mDevice->reset(ARBITRARY_TIME);
1697 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1698 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1699
1700 NotifyDeviceResetArgs resetArgs;
1701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1702 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1703 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1704
1705 // Metadata.
1706 ASSERT_FALSE(mDevice->isIgnored());
1707 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1708
1709 InputDeviceInfo info;
1710 mDevice->getDeviceInfo(&info);
1711 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001712 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001713 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1714 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1715
1716 // State queries.
1717 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1718 << "Should query mappers and combine meta states.";
1719
1720 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1721 << "Should return unknown key code state when source not supported.";
1722 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1723 << "Should return unknown scan code state when source not supported.";
1724 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1725 << "Should return unknown switch state when source not supported.";
1726
1727 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1728 << "Should query mapper when source is supported.";
1729 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1730 << "Should query mapper when source is supported.";
1731 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1732 << "Should query mapper when source is supported.";
1733
1734 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1735 uint8_t flags[4] = { 0, 0, 0, 1 };
1736 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1737 << "Should do nothing when source is unsupported.";
1738 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1739 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1740 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1741 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1742
1743 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1744 << "Should query mapper when source is supported.";
1745 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1746 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1747 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1748 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1749
1750 // Event handling.
1751 RawEvent event;
1752 mDevice->process(&event, 1);
1753
1754 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1755 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1756}
1757
1758
1759// --- InputMapperTest ---
1760
1761class InputMapperTest : public testing::Test {
1762protected:
1763 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001764 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001765 static const int32_t DEVICE_ID;
1766 static const int32_t DEVICE_GENERATION;
1767 static const int32_t DEVICE_CONTROLLER_NUMBER;
1768 static const uint32_t DEVICE_CLASSES;
1769
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001770 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001771 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001772 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001773 FakeInputReaderContext* mFakeContext;
1774 InputDevice* mDevice;
1775
1776 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001777 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001778 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001779 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001780 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1781 InputDeviceIdentifier identifier;
1782 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001783 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1785 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1786
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001787 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001788 }
1789
1790 virtual void TearDown() {
1791 delete mDevice;
1792 delete mFakeContext;
1793 mFakeListener.clear();
1794 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001795 }
1796
1797 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001798 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799 }
1800
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001801 void configureDevice(uint32_t changes) {
1802 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1803 }
1804
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805 void addMapperAndConfigure(InputMapper* mapper) {
1806 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001807 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001808 mDevice->reset(ARBITRARY_TIME);
1809 }
1810
1811 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001812 int32_t orientation, const std::string& uniqueId,
1813 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001814 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001815 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001816 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1817 }
1818
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001819 void clearViewports() {
1820 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001821 }
1822
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001823 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824 int32_t code, int32_t value) {
1825 RawEvent event;
1826 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001827 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001828 event.type = type;
1829 event.code = code;
1830 event.value = value;
1831 mapper->process(&event);
1832 }
1833
1834 static void assertMotionRange(const InputDeviceInfo& info,
1835 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1836 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001837 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001838 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1839 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1840 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1841 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1842 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1843 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1844 }
1845
1846 static void assertPointerCoords(const PointerCoords& coords,
1847 float x, float y, float pressure, float size,
1848 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1849 float orientation, float distance) {
1850 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1851 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1852 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1853 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1854 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1855 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1856 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1857 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1858 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1859 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1860 }
1861
1862 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1863 float actualX, actualY;
1864 controller->getPosition(&actualX, &actualY);
1865 ASSERT_NEAR(x, actualX, 1);
1866 ASSERT_NEAR(y, actualY, 1);
1867 }
1868};
1869
1870const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001871const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001872const int32_t InputMapperTest::DEVICE_ID = 1;
1873const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1874const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1875const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1876
1877
1878// --- SwitchInputMapperTest ---
1879
1880class SwitchInputMapperTest : public InputMapperTest {
1881protected:
1882};
1883
1884TEST_F(SwitchInputMapperTest, GetSources) {
1885 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1886 addMapperAndConfigure(mapper);
1887
1888 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1889}
1890
1891TEST_F(SwitchInputMapperTest, GetSwitchState) {
1892 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1893 addMapperAndConfigure(mapper);
1894
1895 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1896 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1897
1898 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1899 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1900}
1901
1902TEST_F(SwitchInputMapperTest, Process) {
1903 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1904 addMapperAndConfigure(mapper);
1905
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001906 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
1907 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1908 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
1909 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001910
1911 NotifySwitchArgs args;
1912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1913 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08001914 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1915 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001916 args.switchMask);
1917 ASSERT_EQ(uint32_t(0), args.policyFlags);
1918}
1919
1920
1921// --- KeyboardInputMapperTest ---
1922
1923class KeyboardInputMapperTest : public InputMapperTest {
1924protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001925 const std::string UNIQUE_ID = "local:0";
1926
1927 void prepareDisplay(int32_t orientation);
1928
Michael Wrightd02c5b62014-02-10 15:10:22 -08001929 void testDPadKeyRotation(KeyboardInputMapper* mapper,
1930 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1931};
1932
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001933/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
1934 * orientation.
1935 */
1936void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
1937 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001938 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001939}
1940
Michael Wrightd02c5b62014-02-10 15:10:22 -08001941void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001942 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943 NotifyKeyArgs args;
1944
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001945 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1947 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1948 ASSERT_EQ(originalScanCode, args.scanCode);
1949 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1950
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001951 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1953 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1954 ASSERT_EQ(originalScanCode, args.scanCode);
1955 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1956}
1957
1958
1959TEST_F(KeyboardInputMapperTest, GetSources) {
1960 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1961 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1962 addMapperAndConfigure(mapper);
1963
1964 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1965}
1966
1967TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
1968 const int32_t USAGE_A = 0x070004;
1969 const int32_t USAGE_UNKNOWN = 0x07ffff;
1970 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
1971 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
1972
1973 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1974 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1975 addMapperAndConfigure(mapper);
1976
1977 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001978 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001979 NotifyKeyArgs args;
1980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1981 ASSERT_EQ(DEVICE_ID, args.deviceId);
1982 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1983 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1984 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1985 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1986 ASSERT_EQ(KEY_HOME, args.scanCode);
1987 ASSERT_EQ(AMETA_NONE, args.metaState);
1988 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1989 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1990 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1991
1992 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001993 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1995 ASSERT_EQ(DEVICE_ID, args.deviceId);
1996 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1997 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1998 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1999 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2000 ASSERT_EQ(KEY_HOME, args.scanCode);
2001 ASSERT_EQ(AMETA_NONE, args.metaState);
2002 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2003 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2004 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2005
2006 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002007 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2008 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2010 ASSERT_EQ(DEVICE_ID, args.deviceId);
2011 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2012 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2013 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2014 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2015 ASSERT_EQ(0, args.scanCode);
2016 ASSERT_EQ(AMETA_NONE, args.metaState);
2017 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2018 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2019 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2020
2021 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002022 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2023 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2025 ASSERT_EQ(DEVICE_ID, args.deviceId);
2026 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2027 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2028 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2029 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2030 ASSERT_EQ(0, args.scanCode);
2031 ASSERT_EQ(AMETA_NONE, args.metaState);
2032 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2033 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2034 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2035
2036 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002037 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2038 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2040 ASSERT_EQ(DEVICE_ID, args.deviceId);
2041 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2042 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2043 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2044 ASSERT_EQ(0, args.keyCode);
2045 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2046 ASSERT_EQ(AMETA_NONE, args.metaState);
2047 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2048 ASSERT_EQ(0U, args.policyFlags);
2049 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2050
2051 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002052 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2053 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2055 ASSERT_EQ(DEVICE_ID, args.deviceId);
2056 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2057 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2058 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2059 ASSERT_EQ(0, args.keyCode);
2060 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2061 ASSERT_EQ(AMETA_NONE, args.metaState);
2062 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2063 ASSERT_EQ(0U, args.policyFlags);
2064 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2065}
2066
2067TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2068 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2069 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2070
2071 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2072 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2073 addMapperAndConfigure(mapper);
2074
2075 // Initial metastate.
2076 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2077
2078 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002079 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080 NotifyKeyArgs args;
2081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2082 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2084 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2085
2086 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002087 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2089 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2090 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2091
2092 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002093 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2095 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2097
2098 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002099 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2101 ASSERT_EQ(AMETA_NONE, args.metaState);
2102 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2103 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2104}
2105
2106TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2107 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2108 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2109 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2110 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2111
2112 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2113 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2114 addMapperAndConfigure(mapper);
2115
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002116 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002117 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2118 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2119 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2120 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2121 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2122 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2123 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2124 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2125}
2126
2127TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2128 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2129 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2130 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2131 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2132
2133 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2134 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2135 addConfigurationProperty("keyboard.orientationAware", "1");
2136 addMapperAndConfigure(mapper);
2137
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002138 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002139 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2140 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2141 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2142 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2143 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2144 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2145 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2146 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2147
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002148 clearViewports();
2149 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002150 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2151 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
2152 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2153 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
2154 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2155 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
2156 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2157 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
2158
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002159 clearViewports();
2160 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002161 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2162 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
2163 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2164 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
2165 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2166 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
2167 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2168 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
2169
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002170 clearViewports();
2171 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002172 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2173 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
2174 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2175 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
2176 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2177 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
2178 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2179 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
2180
2181 // Special case: if orientation changes while key is down, we still emit the same keycode
2182 // in the key up as we did in the key down.
2183 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002184 clearViewports();
2185 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002186 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2188 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2189 ASSERT_EQ(KEY_UP, args.scanCode);
2190 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2191
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002192 clearViewports();
2193 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002194 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2196 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2197 ASSERT_EQ(KEY_UP, args.scanCode);
2198 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2199}
2200
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002201TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2202 // If the keyboard is not orientation aware,
2203 // key events should not be associated with a specific display id
2204 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2205
2206 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2207 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2208 addMapperAndConfigure(mapper);
2209 NotifyKeyArgs args;
2210
2211 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002212 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002214 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2216 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2217
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002218 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002219 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002221 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2223 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2224}
2225
2226TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2227 // If the keyboard is orientation aware,
2228 // key events should be associated with the internal viewport
2229 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2230
2231 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2232 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2233 addConfigurationProperty("keyboard.orientationAware", "1");
2234 addMapperAndConfigure(mapper);
2235 NotifyKeyArgs args;
2236
2237 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2238 // ^--- already checked by the previous test
2239
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002240 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002241 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002242 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002244 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2246 ASSERT_EQ(DISPLAY_ID, args.displayId);
2247
2248 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002249 clearViewports();
2250 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002251 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002252 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002254 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2256 ASSERT_EQ(newDisplayId, args.displayId);
2257}
2258
Michael Wrightd02c5b62014-02-10 15:10:22 -08002259TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2260 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2261 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2262 addMapperAndConfigure(mapper);
2263
2264 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2265 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2266
2267 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2268 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2269}
2270
2271TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2272 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2273 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2274 addMapperAndConfigure(mapper);
2275
2276 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2277 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2278
2279 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2280 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2281}
2282
2283TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2284 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2285 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2286 addMapperAndConfigure(mapper);
2287
2288 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2289
2290 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2291 uint8_t flags[2] = { 0, 0 };
2292 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2293 ASSERT_TRUE(flags[0]);
2294 ASSERT_FALSE(flags[1]);
2295}
2296
2297TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2298 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2299 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2300 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2301 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2302 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2303 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2304
2305 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2306 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2307 addMapperAndConfigure(mapper);
2308
2309 // Initialization should have turned all of the lights off.
2310 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2311 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2312 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2313
2314 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002315 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2316 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002317 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2318 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2319 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2320 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2321
2322 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002323 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2324 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2326 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2327 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2328 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2329
2330 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002331 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2332 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002333 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2334 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2335 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2336 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2337
2338 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002339 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2340 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2342 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2343 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2344 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2345
2346 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002347 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2348 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002349 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2350 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2351 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2352 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2353
2354 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002355 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2356 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002357 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2358 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2359 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2360 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2361}
2362
2363
2364// --- CursorInputMapperTest ---
2365
2366class CursorInputMapperTest : public InputMapperTest {
2367protected:
2368 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2369
2370 sp<FakePointerController> mFakePointerController;
2371
2372 virtual void SetUp() {
2373 InputMapperTest::SetUp();
2374
2375 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002376 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002377 }
2378
2379 void testMotionRotation(CursorInputMapper* mapper,
2380 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002381
2382 void prepareDisplay(int32_t orientation) {
2383 const std::string uniqueId = "local:0";
2384 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2385 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2386 orientation, uniqueId, NO_PORT, viewportType);
2387 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002388};
2389
2390const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2391
2392void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2393 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2394 NotifyMotionArgs args;
2395
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002396 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2397 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2398 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2402 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2403 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2404 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2405}
2406
2407TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2408 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2409 addConfigurationProperty("cursor.mode", "pointer");
2410 addMapperAndConfigure(mapper);
2411
2412 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2413}
2414
2415TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2416 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2417 addConfigurationProperty("cursor.mode", "navigation");
2418 addMapperAndConfigure(mapper);
2419
2420 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2421}
2422
2423TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2424 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2425 addConfigurationProperty("cursor.mode", "pointer");
2426 addMapperAndConfigure(mapper);
2427
2428 InputDeviceInfo info;
2429 mapper->populateDeviceInfo(&info);
2430
2431 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002432 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2433 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2435 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2436
2437 // When the bounds are set, then there should be a valid motion range.
2438 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2439
2440 InputDeviceInfo info2;
2441 mapper->populateDeviceInfo(&info2);
2442
2443 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2444 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2445 1, 800 - 1, 0.0f, 0.0f));
2446 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2447 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2448 2, 480 - 1, 0.0f, 0.0f));
2449 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2450 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2451 0.0f, 1.0f, 0.0f, 0.0f));
2452}
2453
2454TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2455 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2456 addConfigurationProperty("cursor.mode", "navigation");
2457 addMapperAndConfigure(mapper);
2458
2459 InputDeviceInfo info;
2460 mapper->populateDeviceInfo(&info);
2461
2462 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2463 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2464 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2465 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2466 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2467 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2468 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2469 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2470 0.0f, 1.0f, 0.0f, 0.0f));
2471}
2472
2473TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2474 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2475 addConfigurationProperty("cursor.mode", "navigation");
2476 addMapperAndConfigure(mapper);
2477
2478 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2479
2480 NotifyMotionArgs args;
2481
2482 // Button press.
2483 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002484 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2485 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2487 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2488 ASSERT_EQ(DEVICE_ID, args.deviceId);
2489 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2490 ASSERT_EQ(uint32_t(0), args.policyFlags);
2491 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2492 ASSERT_EQ(0, args.flags);
2493 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2494 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2495 ASSERT_EQ(0, args.edgeFlags);
2496 ASSERT_EQ(uint32_t(1), args.pointerCount);
2497 ASSERT_EQ(0, args.pointerProperties[0].id);
2498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2500 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2501 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2502 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2503 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2504
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2506 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2507 ASSERT_EQ(DEVICE_ID, args.deviceId);
2508 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2509 ASSERT_EQ(uint32_t(0), args.policyFlags);
2510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2511 ASSERT_EQ(0, args.flags);
2512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2513 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2514 ASSERT_EQ(0, args.edgeFlags);
2515 ASSERT_EQ(uint32_t(1), args.pointerCount);
2516 ASSERT_EQ(0, args.pointerProperties[0].id);
2517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2519 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2520 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2521 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2522 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2523
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002525 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2526 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2528 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2529 ASSERT_EQ(DEVICE_ID, args.deviceId);
2530 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2531 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002532 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2533 ASSERT_EQ(0, args.flags);
2534 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2535 ASSERT_EQ(0, args.buttonState);
2536 ASSERT_EQ(0, args.edgeFlags);
2537 ASSERT_EQ(uint32_t(1), args.pointerCount);
2538 ASSERT_EQ(0, args.pointerProperties[0].id);
2539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2541 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2542 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2543 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2544 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2545
2546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2547 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2548 ASSERT_EQ(DEVICE_ID, args.deviceId);
2549 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2550 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2552 ASSERT_EQ(0, args.flags);
2553 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2554 ASSERT_EQ(0, args.buttonState);
2555 ASSERT_EQ(0, args.edgeFlags);
2556 ASSERT_EQ(uint32_t(1), args.pointerCount);
2557 ASSERT_EQ(0, args.pointerProperties[0].id);
2558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2560 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2561 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2562 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2563 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2564}
2565
2566TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2567 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2568 addConfigurationProperty("cursor.mode", "navigation");
2569 addMapperAndConfigure(mapper);
2570
2571 NotifyMotionArgs args;
2572
2573 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002574 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2575 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2579 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2580
2581 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002582 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2583 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2587 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2588}
2589
2590TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2591 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2592 addConfigurationProperty("cursor.mode", "navigation");
2593 addMapperAndConfigure(mapper);
2594
2595 NotifyMotionArgs args;
2596
2597 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002598 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2599 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2601 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2603 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2604
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2608 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2609
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002611 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2612 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2616 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2617
2618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2621 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2622}
2623
2624TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2625 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2626 addConfigurationProperty("cursor.mode", "navigation");
2627 addMapperAndConfigure(mapper);
2628
2629 NotifyMotionArgs args;
2630
2631 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002632 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2633 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2634 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2635 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2637 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2639 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2640 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2641
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2645 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2646 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2647
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002649 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2650 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2651 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2655 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2656 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2657
2658 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002659 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2660 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002662 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2664 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2665
2666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2669 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2670}
2671
2672TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2673 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2674 addConfigurationProperty("cursor.mode", "navigation");
2675 addMapperAndConfigure(mapper);
2676
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002677 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2679 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2680 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2681 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2682 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}
2687
2688TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2689 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2690 addConfigurationProperty("cursor.mode", "navigation");
2691 addConfigurationProperty("cursor.orientationAware", "1");
2692 addMapperAndConfigure(mapper);
2693
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002694 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2696 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2697 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2698 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2699 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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002704 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2706 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2707 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2708 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2709 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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002714 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2716 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2717 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2718 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2719 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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002724 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2726 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2727 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2728 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2729 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}
2734
2735TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2736 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2737 addConfigurationProperty("cursor.mode", "pointer");
2738 addMapperAndConfigure(mapper);
2739
2740 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2741 mFakePointerController->setPosition(100, 200);
2742 mFakePointerController->setButtonState(0);
2743
2744 NotifyMotionArgs motionArgs;
2745 NotifyKeyArgs keyArgs;
2746
2747 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002748 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2749 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2752 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2753 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2755 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2756
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2758 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2759 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2760 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2762 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2763
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002764 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2765 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002767 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768 ASSERT_EQ(0, motionArgs.buttonState);
2769 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2771 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2772
2773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002774 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 ASSERT_EQ(0, motionArgs.buttonState);
2776 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2778 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2779
2780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002782 ASSERT_EQ(0, motionArgs.buttonState);
2783 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2785 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2786
2787 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002788 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2789 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2790 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2792 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2793 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2794 motionArgs.buttonState);
2795 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2796 mFakePointerController->getButtonState());
2797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2798 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2799
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2802 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2803 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2804 mFakePointerController->getButtonState());
2805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2806 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2807
2808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2810 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2811 motionArgs.buttonState);
2812 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2813 mFakePointerController->getButtonState());
2814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2815 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2816
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002817 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
2818 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2822 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2824 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2825
2826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002828 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2829 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2831 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2832
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002833 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2834 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002836 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2837 ASSERT_EQ(0, motionArgs.buttonState);
2838 ASSERT_EQ(0, mFakePointerController->getButtonState());
2839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2840 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 -08002841 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2842 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002843
2844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845 ASSERT_EQ(0, motionArgs.buttonState);
2846 ASSERT_EQ(0, mFakePointerController->getButtonState());
2847 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2849 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 -08002850
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2852 ASSERT_EQ(0, motionArgs.buttonState);
2853 ASSERT_EQ(0, mFakePointerController->getButtonState());
2854 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2856 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2857
2858 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002859 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
2860 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2862 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2863 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002864
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002866 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2868 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2870 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2871
2872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2873 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2874 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2875 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2877 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2878
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002879 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
2880 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002882 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002883 ASSERT_EQ(0, motionArgs.buttonState);
2884 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2886 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2887
2888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002890 ASSERT_EQ(0, motionArgs.buttonState);
2891 ASSERT_EQ(0, mFakePointerController->getButtonState());
2892
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2894 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2896 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2897 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2898
2899 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002900 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
2901 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2903 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2904 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002905
Michael Wrightd02c5b62014-02-10 15:10:22 -08002906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002907 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002908 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2909 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2911 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2912
2913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2915 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2916 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2918 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2919
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002920 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
2921 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002923 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 ASSERT_EQ(0, motionArgs.buttonState);
2925 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2927 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002928
2929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2930 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2931 ASSERT_EQ(0, motionArgs.buttonState);
2932 ASSERT_EQ(0, mFakePointerController->getButtonState());
2933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2934 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2935
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2937 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2938 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2939
2940 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002941 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
2942 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2944 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2945 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002946
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002948 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2950 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2952 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2953
2954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2956 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2957 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2959 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2960
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002961 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
2962 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002964 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965 ASSERT_EQ(0, motionArgs.buttonState);
2966 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2968 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002969
2970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2972 ASSERT_EQ(0, motionArgs.buttonState);
2973 ASSERT_EQ(0, mFakePointerController->getButtonState());
2974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2975 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2976
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2978 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2979 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2980
2981 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002982 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
2983 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2985 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2986 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002987
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002989 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2991 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2993 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2994
2995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2996 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2997 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2998 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3000 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3001
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003002 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3003 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 ASSERT_EQ(0, motionArgs.buttonState);
3007 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3009 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003010
3011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3012 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3013 ASSERT_EQ(0, motionArgs.buttonState);
3014 ASSERT_EQ(0, mFakePointerController->getButtonState());
3015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3016 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3017
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3019 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3020 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3021}
3022
3023TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3024 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3025 addConfigurationProperty("cursor.mode", "pointer");
3026 addMapperAndConfigure(mapper);
3027
3028 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3029 mFakePointerController->setPosition(100, 200);
3030 mFakePointerController->setButtonState(0);
3031
3032 NotifyMotionArgs args;
3033
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003034 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3035 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3036 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003038 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3039 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3041 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3042 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3043}
3044
3045TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3046 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3047 addConfigurationProperty("cursor.mode", "pointer");
3048 mFakePolicy->setPointerCapture(true);
3049 addMapperAndConfigure(mapper);
3050
3051 NotifyDeviceResetArgs resetArgs;
3052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3053 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3054 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3055
3056 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3057 mFakePointerController->setPosition(100, 200);
3058 mFakePointerController->setButtonState(0);
3059
3060 NotifyMotionArgs args;
3061
3062 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003063 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3064 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3065 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3067 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3070 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3071 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3072
3073 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003074 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3075 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3077 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3078 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3080 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3082 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3085 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3086
3087 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003088 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3089 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3091 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3094 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3096 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3097 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3099 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3100
3101 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003102 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3103 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3104 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3106 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3109 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3110 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3111
3112 // Disable pointer capture and check that the device generation got bumped
3113 // and events are generated the usual way.
3114 const uint32_t generation = mFakeContext->getGeneration();
3115 mFakePolicy->setPointerCapture(false);
3116 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3117 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3118
3119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3120 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3121 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3122
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003123 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3124 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3125 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3127 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3130 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3131 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3132}
3133
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003134TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3135 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3136 addMapperAndConfigure(mapper);
3137
3138 // Setup PointerController for second display.
3139 constexpr int32_t SECOND_DISPLAY_ID = 1;
3140 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3141 mFakePointerController->setPosition(100, 200);
3142 mFakePointerController->setButtonState(0);
3143 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3144
3145 NotifyMotionArgs args;
3146 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3147 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3148 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3150 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3151 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3153 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3154 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3155 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3156}
3157
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158
3159// --- TouchInputMapperTest ---
3160
3161class TouchInputMapperTest : public InputMapperTest {
3162protected:
3163 static const int32_t RAW_X_MIN;
3164 static const int32_t RAW_X_MAX;
3165 static const int32_t RAW_Y_MIN;
3166 static const int32_t RAW_Y_MAX;
3167 static const int32_t RAW_TOUCH_MIN;
3168 static const int32_t RAW_TOUCH_MAX;
3169 static const int32_t RAW_TOOL_MIN;
3170 static const int32_t RAW_TOOL_MAX;
3171 static const int32_t RAW_PRESSURE_MIN;
3172 static const int32_t RAW_PRESSURE_MAX;
3173 static const int32_t RAW_ORIENTATION_MIN;
3174 static const int32_t RAW_ORIENTATION_MAX;
3175 static const int32_t RAW_DISTANCE_MIN;
3176 static const int32_t RAW_DISTANCE_MAX;
3177 static const int32_t RAW_TILT_MIN;
3178 static const int32_t RAW_TILT_MAX;
3179 static const int32_t RAW_ID_MIN;
3180 static const int32_t RAW_ID_MAX;
3181 static const int32_t RAW_SLOT_MIN;
3182 static const int32_t RAW_SLOT_MAX;
3183 static const float X_PRECISION;
3184 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003185 static const float X_PRECISION_VIRTUAL;
3186 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187
3188 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003189 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190
3191 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3192
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003193 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003194 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003195
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196 enum Axes {
3197 POSITION = 1 << 0,
3198 TOUCH = 1 << 1,
3199 TOOL = 1 << 2,
3200 PRESSURE = 1 << 3,
3201 ORIENTATION = 1 << 4,
3202 MINOR = 1 << 5,
3203 ID = 1 << 6,
3204 DISTANCE = 1 << 7,
3205 TILT = 1 << 8,
3206 SLOT = 1 << 9,
3207 TOOL_TYPE = 1 << 10,
3208 };
3209
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003210 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3211 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003212 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003214 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215 int32_t toRawX(float displayX);
3216 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003217 float toCookedX(float rawX, float rawY);
3218 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003220 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003222 float toDisplayY(int32_t rawY, int32_t displayHeight);
3223
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224};
3225
3226const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3227const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3228const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3229const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3230const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3231const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3232const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3233const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003234const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3235const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3237const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3238const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3239const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3240const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3241const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3242const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3243const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3244const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3245const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3246const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3247const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003248const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3249 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3250const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3251 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003252const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3253 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254
3255const float TouchInputMapperTest::GEOMETRIC_SCALE =
3256 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3257 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3258
3259const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3260 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3261 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3262};
3263
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003264void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003265 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003266 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3267}
3268
3269void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3270 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3271 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272}
3273
Santos Cordonfa5cf462017-04-05 10:37:00 -07003274void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003275 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3276 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003277 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003278}
3279
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280void TouchInputMapperTest::prepareVirtualKeys() {
3281 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3282 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3283 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3284 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3285}
3286
Jason Gerecke489fda82012-09-07 17:19:40 -07003287void TouchInputMapperTest::prepareLocationCalibration() {
3288 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3289}
3290
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291int32_t TouchInputMapperTest::toRawX(float displayX) {
3292 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3293}
3294
3295int32_t TouchInputMapperTest::toRawY(float displayY) {
3296 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3297}
3298
Jason Gerecke489fda82012-09-07 17:19:40 -07003299float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3300 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3301 return rawX;
3302}
3303
3304float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3305 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3306 return rawY;
3307}
3308
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003310 return toDisplayX(rawX, DISPLAY_WIDTH);
3311}
3312
3313float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3314 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315}
3316
3317float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003318 return toDisplayY(rawY, DISPLAY_HEIGHT);
3319}
3320
3321float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3322 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003323}
3324
3325
3326// --- SingleTouchInputMapperTest ---
3327
3328class SingleTouchInputMapperTest : public TouchInputMapperTest {
3329protected:
3330 void prepareButtons();
3331 void prepareAxes(int axes);
3332
3333 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3334 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3335 void processUp(SingleTouchInputMapper* mappery);
3336 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3337 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3338 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3339 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3340 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3341 void processSync(SingleTouchInputMapper* mapper);
3342};
3343
3344void SingleTouchInputMapperTest::prepareButtons() {
3345 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3346}
3347
3348void SingleTouchInputMapperTest::prepareAxes(int axes) {
3349 if (axes & POSITION) {
3350 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3351 RAW_X_MIN, RAW_X_MAX, 0, 0);
3352 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3353 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3354 }
3355 if (axes & PRESSURE) {
3356 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3357 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3358 }
3359 if (axes & TOOL) {
3360 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3361 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3362 }
3363 if (axes & DISTANCE) {
3364 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3365 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3366 }
3367 if (axes & TILT) {
3368 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3369 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3370 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3371 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3372 }
3373}
3374
3375void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003376 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3377 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3378 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379}
3380
3381void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003382 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3383 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384}
3385
3386void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003387 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388}
3389
3390void SingleTouchInputMapperTest::processPressure(
3391 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003392 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393}
3394
3395void SingleTouchInputMapperTest::processToolMajor(
3396 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003397 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398}
3399
3400void SingleTouchInputMapperTest::processDistance(
3401 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003402 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403}
3404
3405void SingleTouchInputMapperTest::processTilt(
3406 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003407 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3408 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409}
3410
3411void SingleTouchInputMapperTest::processKey(
3412 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003413 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414}
3415
3416void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003417 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418}
3419
3420
3421TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3422 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3423 prepareButtons();
3424 prepareAxes(POSITION);
3425 addMapperAndConfigure(mapper);
3426
3427 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3428}
3429
3430TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3431 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3432 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3433 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3434 prepareButtons();
3435 prepareAxes(POSITION);
3436 addMapperAndConfigure(mapper);
3437
3438 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3439}
3440
3441TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3442 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3443 prepareButtons();
3444 prepareAxes(POSITION);
3445 addConfigurationProperty("touch.deviceType", "touchPad");
3446 addMapperAndConfigure(mapper);
3447
3448 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3449}
3450
3451TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3452 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3453 prepareButtons();
3454 prepareAxes(POSITION);
3455 addConfigurationProperty("touch.deviceType", "touchScreen");
3456 addMapperAndConfigure(mapper);
3457
3458 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3459}
3460
3461TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3462 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3463 addConfigurationProperty("touch.deviceType", "touchScreen");
3464 prepareDisplay(DISPLAY_ORIENTATION_0);
3465 prepareButtons();
3466 prepareAxes(POSITION);
3467 prepareVirtualKeys();
3468 addMapperAndConfigure(mapper);
3469
3470 // Unknown key.
3471 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3472
3473 // Virtual key is down.
3474 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3475 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3476 processDown(mapper, x, y);
3477 processSync(mapper);
3478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3479
3480 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3481
3482 // Virtual key is up.
3483 processUp(mapper);
3484 processSync(mapper);
3485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3486
3487 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3488}
3489
3490TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3491 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3492 addConfigurationProperty("touch.deviceType", "touchScreen");
3493 prepareDisplay(DISPLAY_ORIENTATION_0);
3494 prepareButtons();
3495 prepareAxes(POSITION);
3496 prepareVirtualKeys();
3497 addMapperAndConfigure(mapper);
3498
3499 // Unknown key.
3500 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3501
3502 // Virtual key is down.
3503 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3504 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3505 processDown(mapper, x, y);
3506 processSync(mapper);
3507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3508
3509 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3510
3511 // Virtual key is up.
3512 processUp(mapper);
3513 processSync(mapper);
3514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3515
3516 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3517}
3518
3519TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3520 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3521 addConfigurationProperty("touch.deviceType", "touchScreen");
3522 prepareDisplay(DISPLAY_ORIENTATION_0);
3523 prepareButtons();
3524 prepareAxes(POSITION);
3525 prepareVirtualKeys();
3526 addMapperAndConfigure(mapper);
3527
3528 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3529 uint8_t flags[2] = { 0, 0 };
3530 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3531 ASSERT_TRUE(flags[0]);
3532 ASSERT_FALSE(flags[1]);
3533}
3534
3535TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3536 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3537 addConfigurationProperty("touch.deviceType", "touchScreen");
3538 prepareDisplay(DISPLAY_ORIENTATION_0);
3539 prepareButtons();
3540 prepareAxes(POSITION);
3541 prepareVirtualKeys();
3542 addMapperAndConfigure(mapper);
3543
3544 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3545
3546 NotifyKeyArgs args;
3547
3548 // Press virtual key.
3549 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3550 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3551 processDown(mapper, x, y);
3552 processSync(mapper);
3553
3554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3555 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3556 ASSERT_EQ(DEVICE_ID, args.deviceId);
3557 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3558 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3559 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3560 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3561 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3562 ASSERT_EQ(KEY_HOME, args.scanCode);
3563 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3564 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3565
3566 // Release virtual key.
3567 processUp(mapper);
3568 processSync(mapper);
3569
3570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3571 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3572 ASSERT_EQ(DEVICE_ID, args.deviceId);
3573 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3574 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3575 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3576 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3577 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3578 ASSERT_EQ(KEY_HOME, args.scanCode);
3579 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3580 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3581
3582 // Should not have sent any motions.
3583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3584}
3585
3586TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3587 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3588 addConfigurationProperty("touch.deviceType", "touchScreen");
3589 prepareDisplay(DISPLAY_ORIENTATION_0);
3590 prepareButtons();
3591 prepareAxes(POSITION);
3592 prepareVirtualKeys();
3593 addMapperAndConfigure(mapper);
3594
3595 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3596
3597 NotifyKeyArgs keyArgs;
3598
3599 // Press virtual key.
3600 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3601 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3602 processDown(mapper, x, y);
3603 processSync(mapper);
3604
3605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3606 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3607 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3608 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3609 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3610 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3611 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3612 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3613 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3614 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3615 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3616
3617 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3618 // into the display area.
3619 y -= 100;
3620 processMove(mapper, x, y);
3621 processSync(mapper);
3622
3623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3624 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3625 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3626 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3627 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3628 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3629 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3630 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3631 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3632 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3633 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3634 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3635
3636 NotifyMotionArgs motionArgs;
3637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3640 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3641 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3643 ASSERT_EQ(0, motionArgs.flags);
3644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3645 ASSERT_EQ(0, motionArgs.buttonState);
3646 ASSERT_EQ(0, motionArgs.edgeFlags);
3647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3651 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3652 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3653 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3655
3656 // Keep moving out of bounds. Should generate a pointer move.
3657 y -= 50;
3658 processMove(mapper, x, y);
3659 processSync(mapper);
3660
3661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3662 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3663 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3664 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3665 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3667 ASSERT_EQ(0, motionArgs.flags);
3668 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3669 ASSERT_EQ(0, motionArgs.buttonState);
3670 ASSERT_EQ(0, motionArgs.edgeFlags);
3671 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3672 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3675 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3676 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3677 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3678 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3679
3680 // Release out of bounds. Should generate a pointer up.
3681 processUp(mapper);
3682 processSync(mapper);
3683
3684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3686 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3687 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3688 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3689 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3690 ASSERT_EQ(0, motionArgs.flags);
3691 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3692 ASSERT_EQ(0, motionArgs.buttonState);
3693 ASSERT_EQ(0, motionArgs.edgeFlags);
3694 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3695 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3698 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3699 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3700 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3701 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3702
3703 // Should not have sent any more keys or motions.
3704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3706}
3707
3708TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3709 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3710 addConfigurationProperty("touch.deviceType", "touchScreen");
3711 prepareDisplay(DISPLAY_ORIENTATION_0);
3712 prepareButtons();
3713 prepareAxes(POSITION);
3714 prepareVirtualKeys();
3715 addMapperAndConfigure(mapper);
3716
3717 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3718
3719 NotifyMotionArgs motionArgs;
3720
3721 // Initially go down out of bounds.
3722 int32_t x = -10;
3723 int32_t y = -10;
3724 processDown(mapper, x, y);
3725 processSync(mapper);
3726
3727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3728
3729 // Move into the display area. Should generate a pointer down.
3730 x = 50;
3731 y = 75;
3732 processMove(mapper, x, y);
3733 processSync(mapper);
3734
3735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3737 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3738 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3739 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3740 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3741 ASSERT_EQ(0, motionArgs.flags);
3742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3743 ASSERT_EQ(0, motionArgs.buttonState);
3744 ASSERT_EQ(0, motionArgs.edgeFlags);
3745 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3746 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3749 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3750 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3751 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3753
3754 // Release. Should generate a pointer up.
3755 processUp(mapper);
3756 processSync(mapper);
3757
3758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3759 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3760 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3761 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3762 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3763 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3764 ASSERT_EQ(0, motionArgs.flags);
3765 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3766 ASSERT_EQ(0, motionArgs.buttonState);
3767 ASSERT_EQ(0, motionArgs.edgeFlags);
3768 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3769 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3772 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3773 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3774 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3775 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3776
3777 // Should not have sent any more keys or motions.
3778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3780}
3781
Santos Cordonfa5cf462017-04-05 10:37:00 -07003782TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3783 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3784 addConfigurationProperty("touch.deviceType", "touchScreen");
3785 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3786
3787 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3788 prepareButtons();
3789 prepareAxes(POSITION);
3790 prepareVirtualKeys();
3791 addMapperAndConfigure(mapper);
3792
3793 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3794
3795 NotifyMotionArgs motionArgs;
3796
3797 // Down.
3798 int32_t x = 100;
3799 int32_t y = 125;
3800 processDown(mapper, x, y);
3801 processSync(mapper);
3802
3803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3805 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3806 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3807 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3808 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3810 ASSERT_EQ(0, motionArgs.flags);
3811 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3812 ASSERT_EQ(0, motionArgs.buttonState);
3813 ASSERT_EQ(0, motionArgs.edgeFlags);
3814 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3815 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3818 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3819 1, 0, 0, 0, 0, 0, 0, 0));
3820 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3821 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3822 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3823
3824 // Move.
3825 x += 50;
3826 y += 75;
3827 processMove(mapper, x, y);
3828 processSync(mapper);
3829
3830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3831 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3832 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3833 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3834 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3835 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3837 ASSERT_EQ(0, motionArgs.flags);
3838 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3839 ASSERT_EQ(0, motionArgs.buttonState);
3840 ASSERT_EQ(0, motionArgs.edgeFlags);
3841 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3842 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3845 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3846 1, 0, 0, 0, 0, 0, 0, 0));
3847 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3848 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3849 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3850
3851 // Up.
3852 processUp(mapper);
3853 processSync(mapper);
3854
3855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3856 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3857 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3858 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3860 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3861 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3862 ASSERT_EQ(0, motionArgs.flags);
3863 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3864 ASSERT_EQ(0, motionArgs.buttonState);
3865 ASSERT_EQ(0, motionArgs.edgeFlags);
3866 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3867 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3870 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3871 1, 0, 0, 0, 0, 0, 0, 0));
3872 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3873 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3874 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3875
3876 // Should not have sent any more keys or motions.
3877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3879}
3880
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
3882 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3883 addConfigurationProperty("touch.deviceType", "touchScreen");
3884 prepareDisplay(DISPLAY_ORIENTATION_0);
3885 prepareButtons();
3886 prepareAxes(POSITION);
3887 prepareVirtualKeys();
3888 addMapperAndConfigure(mapper);
3889
3890 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3891
3892 NotifyMotionArgs motionArgs;
3893
3894 // Down.
3895 int32_t x = 100;
3896 int32_t y = 125;
3897 processDown(mapper, x, y);
3898 processSync(mapper);
3899
3900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3901 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3902 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3903 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3904 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3905 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3906 ASSERT_EQ(0, motionArgs.flags);
3907 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3908 ASSERT_EQ(0, motionArgs.buttonState);
3909 ASSERT_EQ(0, motionArgs.edgeFlags);
3910 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3911 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3914 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3915 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3916 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3917 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3918
3919 // Move.
3920 x += 50;
3921 y += 75;
3922 processMove(mapper, x, y);
3923 processSync(mapper);
3924
3925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3926 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3927 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3928 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3929 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3931 ASSERT_EQ(0, motionArgs.flags);
3932 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3933 ASSERT_EQ(0, motionArgs.buttonState);
3934 ASSERT_EQ(0, motionArgs.edgeFlags);
3935 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3936 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3939 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3940 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3941 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3942 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3943
3944 // Up.
3945 processUp(mapper);
3946 processSync(mapper);
3947
3948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3949 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3950 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3951 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3952 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3953 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3954 ASSERT_EQ(0, motionArgs.flags);
3955 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3956 ASSERT_EQ(0, motionArgs.buttonState);
3957 ASSERT_EQ(0, motionArgs.edgeFlags);
3958 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3959 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3962 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3963 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3964 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3965 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3966
3967 // Should not have sent any more keys or motions.
3968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3970}
3971
3972TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3973 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3974 addConfigurationProperty("touch.deviceType", "touchScreen");
3975 prepareButtons();
3976 prepareAxes(POSITION);
3977 addConfigurationProperty("touch.orientationAware", "0");
3978 addMapperAndConfigure(mapper);
3979
3980 NotifyMotionArgs args;
3981
3982 // Rotation 90.
3983 prepareDisplay(DISPLAY_ORIENTATION_90);
3984 processDown(mapper, toRawX(50), toRawY(75));
3985 processSync(mapper);
3986
3987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3988 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3989 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3990
3991 processUp(mapper);
3992 processSync(mapper);
3993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3994}
3995
3996TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
3997 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3998 addConfigurationProperty("touch.deviceType", "touchScreen");
3999 prepareButtons();
4000 prepareAxes(POSITION);
4001 addMapperAndConfigure(mapper);
4002
4003 NotifyMotionArgs args;
4004
4005 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004006 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 prepareDisplay(DISPLAY_ORIENTATION_0);
4008 processDown(mapper, toRawX(50), toRawY(75));
4009 processSync(mapper);
4010
4011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4012 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4013 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4014
4015 processUp(mapper);
4016 processSync(mapper);
4017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4018
4019 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004020 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 prepareDisplay(DISPLAY_ORIENTATION_90);
4022 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4023 processSync(mapper);
4024
4025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4026 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4027 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4028
4029 processUp(mapper);
4030 processSync(mapper);
4031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4032
4033 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004034 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035 prepareDisplay(DISPLAY_ORIENTATION_180);
4036 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4037 processSync(mapper);
4038
4039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4040 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4041 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4042
4043 processUp(mapper);
4044 processSync(mapper);
4045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4046
4047 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004048 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 prepareDisplay(DISPLAY_ORIENTATION_270);
4050 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4051 processSync(mapper);
4052
4053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4054 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4055 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4056
4057 processUp(mapper);
4058 processSync(mapper);
4059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4060}
4061
4062TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4063 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4064 addConfigurationProperty("touch.deviceType", "touchScreen");
4065 prepareDisplay(DISPLAY_ORIENTATION_0);
4066 prepareButtons();
4067 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4068 addMapperAndConfigure(mapper);
4069
4070 // These calculations are based on the input device calibration documentation.
4071 int32_t rawX = 100;
4072 int32_t rawY = 200;
4073 int32_t rawPressure = 10;
4074 int32_t rawToolMajor = 12;
4075 int32_t rawDistance = 2;
4076 int32_t rawTiltX = 30;
4077 int32_t rawTiltY = 110;
4078
4079 float x = toDisplayX(rawX);
4080 float y = toDisplayY(rawY);
4081 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4082 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4083 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4084 float distance = float(rawDistance);
4085
4086 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4087 float tiltScale = M_PI / 180;
4088 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4089 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4090 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4091 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4092
4093 processDown(mapper, rawX, rawY);
4094 processPressure(mapper, rawPressure);
4095 processToolMajor(mapper, rawToolMajor);
4096 processDistance(mapper, rawDistance);
4097 processTilt(mapper, rawTiltX, rawTiltY);
4098 processSync(mapper);
4099
4100 NotifyMotionArgs args;
4101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4103 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4104 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4105}
4106
Jason Gerecke489fda82012-09-07 17:19:40 -07004107TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4108 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4109 addConfigurationProperty("touch.deviceType", "touchScreen");
4110 prepareDisplay(DISPLAY_ORIENTATION_0);
4111 prepareLocationCalibration();
4112 prepareButtons();
4113 prepareAxes(POSITION);
4114 addMapperAndConfigure(mapper);
4115
4116 int32_t rawX = 100;
4117 int32_t rawY = 200;
4118
4119 float x = toDisplayX(toCookedX(rawX, rawY));
4120 float y = toDisplayY(toCookedY(rawX, rawY));
4121
4122 processDown(mapper, rawX, rawY);
4123 processSync(mapper);
4124
4125 NotifyMotionArgs args;
4126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4128 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4129}
4130
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4132 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4133 addConfigurationProperty("touch.deviceType", "touchScreen");
4134 prepareDisplay(DISPLAY_ORIENTATION_0);
4135 prepareButtons();
4136 prepareAxes(POSITION);
4137 addMapperAndConfigure(mapper);
4138
4139 NotifyMotionArgs motionArgs;
4140 NotifyKeyArgs keyArgs;
4141
4142 processDown(mapper, 100, 200);
4143 processSync(mapper);
4144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4145 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4146 ASSERT_EQ(0, motionArgs.buttonState);
4147
4148 // press BTN_LEFT, release BTN_LEFT
4149 processKey(mapper, BTN_LEFT, 1);
4150 processSync(mapper);
4151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4153 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4154
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4157 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4158
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159 processKey(mapper, BTN_LEFT, 0);
4160 processSync(mapper);
4161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004164
4165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004167 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168
4169 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4170 processKey(mapper, BTN_RIGHT, 1);
4171 processKey(mapper, BTN_MIDDLE, 1);
4172 processSync(mapper);
4173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4175 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4176 motionArgs.buttonState);
4177
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4180 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4181
4182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4184 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4185 motionArgs.buttonState);
4186
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 processKey(mapper, BTN_RIGHT, 0);
4188 processSync(mapper);
4189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004192
4193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004195 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196
4197 processKey(mapper, BTN_MIDDLE, 0);
4198 processSync(mapper);
4199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004200 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004202
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004205 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206
4207 // press BTN_BACK, release BTN_BACK
4208 processKey(mapper, BTN_BACK, 1);
4209 processSync(mapper);
4210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4211 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4212 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004213
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004216 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4217
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4220 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
4222 processKey(mapper, BTN_BACK, 0);
4223 processSync(mapper);
4224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004227
4228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004230 ASSERT_EQ(0, motionArgs.buttonState);
4231
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4233 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4234 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4235
4236 // press BTN_SIDE, release BTN_SIDE
4237 processKey(mapper, BTN_SIDE, 1);
4238 processSync(mapper);
4239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4240 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4241 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004242
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004245 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4246
4247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4249 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250
4251 processKey(mapper, BTN_SIDE, 0);
4252 processSync(mapper);
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256
4257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259 ASSERT_EQ(0, motionArgs.buttonState);
4260
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4262 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4263 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4264
4265 // press BTN_FORWARD, release BTN_FORWARD
4266 processKey(mapper, BTN_FORWARD, 1);
4267 processSync(mapper);
4268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4269 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4270 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004274 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4275
4276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4278 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
4280 processKey(mapper, BTN_FORWARD, 0);
4281 processSync(mapper);
4282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004283 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285
4286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004288 ASSERT_EQ(0, motionArgs.buttonState);
4289
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4291 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4292 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4293
4294 // press BTN_EXTRA, release BTN_EXTRA
4295 processKey(mapper, BTN_EXTRA, 1);
4296 processSync(mapper);
4297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4298 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4299 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4304
4305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4307 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308
4309 processKey(mapper, BTN_EXTRA, 0);
4310 processSync(mapper);
4311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314
4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317 ASSERT_EQ(0, motionArgs.buttonState);
4318
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4320 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4321 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4322
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4324
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 // press BTN_STYLUS, release BTN_STYLUS
4326 processKey(mapper, BTN_STYLUS, 1);
4327 processSync(mapper);
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004330 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4331
4332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4334 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335
4336 processKey(mapper, BTN_STYLUS, 0);
4337 processSync(mapper);
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345
4346 // press BTN_STYLUS2, release BTN_STYLUS2
4347 processKey(mapper, BTN_STYLUS2, 1);
4348 processSync(mapper);
4349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4352
4353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4355 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356
4357 processKey(mapper, BTN_STYLUS2, 0);
4358 processSync(mapper);
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362
4363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366
4367 // release touch
4368 processUp(mapper);
4369 processSync(mapper);
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4371 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4372 ASSERT_EQ(0, motionArgs.buttonState);
4373}
4374
4375TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4376 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4377 addConfigurationProperty("touch.deviceType", "touchScreen");
4378 prepareDisplay(DISPLAY_ORIENTATION_0);
4379 prepareButtons();
4380 prepareAxes(POSITION);
4381 addMapperAndConfigure(mapper);
4382
4383 NotifyMotionArgs motionArgs;
4384
4385 // default tool type is finger
4386 processDown(mapper, 100, 200);
4387 processSync(mapper);
4388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4391
4392 // eraser
4393 processKey(mapper, BTN_TOOL_RUBBER, 1);
4394 processSync(mapper);
4395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4398
4399 // stylus
4400 processKey(mapper, BTN_TOOL_RUBBER, 0);
4401 processKey(mapper, BTN_TOOL_PEN, 1);
4402 processSync(mapper);
4403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4406
4407 // brush
4408 processKey(mapper, BTN_TOOL_PEN, 0);
4409 processKey(mapper, BTN_TOOL_BRUSH, 1);
4410 processSync(mapper);
4411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4414
4415 // pencil
4416 processKey(mapper, BTN_TOOL_BRUSH, 0);
4417 processKey(mapper, BTN_TOOL_PENCIL, 1);
4418 processSync(mapper);
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4422
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004423 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 processKey(mapper, BTN_TOOL_PENCIL, 0);
4425 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4426 processSync(mapper);
4427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4430
4431 // mouse
4432 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4433 processKey(mapper, BTN_TOOL_MOUSE, 1);
4434 processSync(mapper);
4435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4438
4439 // lens
4440 processKey(mapper, BTN_TOOL_MOUSE, 0);
4441 processKey(mapper, BTN_TOOL_LENS, 1);
4442 processSync(mapper);
4443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4446
4447 // double-tap
4448 processKey(mapper, BTN_TOOL_LENS, 0);
4449 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4450 processSync(mapper);
4451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4454
4455 // triple-tap
4456 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4457 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4458 processSync(mapper);
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4462
4463 // quad-tap
4464 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4465 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4466 processSync(mapper);
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4470
4471 // finger
4472 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4473 processKey(mapper, BTN_TOOL_FINGER, 1);
4474 processSync(mapper);
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4478
4479 // stylus trumps finger
4480 processKey(mapper, BTN_TOOL_PEN, 1);
4481 processSync(mapper);
4482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4485
4486 // eraser trumps stylus
4487 processKey(mapper, BTN_TOOL_RUBBER, 1);
4488 processSync(mapper);
4489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4492
4493 // mouse trumps eraser
4494 processKey(mapper, BTN_TOOL_MOUSE, 1);
4495 processSync(mapper);
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4499
4500 // back to default tool type
4501 processKey(mapper, BTN_TOOL_MOUSE, 0);
4502 processKey(mapper, BTN_TOOL_RUBBER, 0);
4503 processKey(mapper, BTN_TOOL_PEN, 0);
4504 processKey(mapper, BTN_TOOL_FINGER, 0);
4505 processSync(mapper);
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4509}
4510
4511TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4512 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4513 addConfigurationProperty("touch.deviceType", "touchScreen");
4514 prepareDisplay(DISPLAY_ORIENTATION_0);
4515 prepareButtons();
4516 prepareAxes(POSITION);
4517 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4518 addMapperAndConfigure(mapper);
4519
4520 NotifyMotionArgs motionArgs;
4521
4522 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4523 processKey(mapper, BTN_TOOL_FINGER, 1);
4524 processMove(mapper, 100, 200);
4525 processSync(mapper);
4526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4529 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4530
4531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4534 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4535
4536 // move a little
4537 processMove(mapper, 150, 250);
4538 processSync(mapper);
4539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4540 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4542 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4543
4544 // down when BTN_TOUCH is pressed, pressure defaults to 1
4545 processKey(mapper, BTN_TOUCH, 1);
4546 processSync(mapper);
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4550 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4551
4552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4553 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4555 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4556
4557 // up when BTN_TOUCH is released, hover restored
4558 processKey(mapper, BTN_TOUCH, 0);
4559 processSync(mapper);
4560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4561 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4563 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4564
4565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4566 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4568 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4569
4570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4573 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4574
4575 // exit hover when pointer goes away
4576 processKey(mapper, BTN_TOOL_FINGER, 0);
4577 processSync(mapper);
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4581 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4582}
4583
4584TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4585 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4586 addConfigurationProperty("touch.deviceType", "touchScreen");
4587 prepareDisplay(DISPLAY_ORIENTATION_0);
4588 prepareButtons();
4589 prepareAxes(POSITION | PRESSURE);
4590 addMapperAndConfigure(mapper);
4591
4592 NotifyMotionArgs motionArgs;
4593
4594 // initially hovering because pressure is 0
4595 processDown(mapper, 100, 200);
4596 processPressure(mapper, 0);
4597 processSync(mapper);
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4601 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4602
4603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4606 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4607
4608 // move a little
4609 processMove(mapper, 150, 250);
4610 processSync(mapper);
4611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4614 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4615
4616 // down when pressure is non-zero
4617 processPressure(mapper, RAW_PRESSURE_MAX);
4618 processSync(mapper);
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4620 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4622 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4623
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4625 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4627 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4628
4629 // up when pressure becomes 0, hover restored
4630 processPressure(mapper, 0);
4631 processSync(mapper);
4632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4633 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4635 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4636
4637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4640 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4641
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4645 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4646
4647 // exit hover when pointer goes away
4648 processUp(mapper);
4649 processSync(mapper);
4650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4653 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4654}
4655
Dan Harmsaca28402018-12-17 13:55:20 -08004656
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657// --- MultiTouchInputMapperTest ---
4658
4659class MultiTouchInputMapperTest : public TouchInputMapperTest {
4660protected:
4661 void prepareAxes(int axes);
4662
4663 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4664 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4665 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4666 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4667 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4668 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4669 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4670 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4671 void processId(MultiTouchInputMapper* mapper, int32_t id);
4672 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4673 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4674 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4675 void processMTSync(MultiTouchInputMapper* mapper);
4676 void processSync(MultiTouchInputMapper* mapper);
4677};
4678
4679void MultiTouchInputMapperTest::prepareAxes(int axes) {
4680 if (axes & POSITION) {
4681 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4682 RAW_X_MIN, RAW_X_MAX, 0, 0);
4683 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4684 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4685 }
4686 if (axes & TOUCH) {
4687 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4688 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4689 if (axes & MINOR) {
4690 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4691 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4692 }
4693 }
4694 if (axes & TOOL) {
4695 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4696 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4697 if (axes & MINOR) {
4698 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4699 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4700 }
4701 }
4702 if (axes & ORIENTATION) {
4703 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4704 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4705 }
4706 if (axes & PRESSURE) {
4707 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4708 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4709 }
4710 if (axes & DISTANCE) {
4711 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4712 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4713 }
4714 if (axes & ID) {
4715 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4716 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4717 }
4718 if (axes & SLOT) {
4719 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4720 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4721 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4722 }
4723 if (axes & TOOL_TYPE) {
4724 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4725 0, MT_TOOL_MAX, 0, 0);
4726 }
4727}
4728
4729void MultiTouchInputMapperTest::processPosition(
4730 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004731 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4732 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733}
4734
4735void MultiTouchInputMapperTest::processTouchMajor(
4736 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004737 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738}
4739
4740void MultiTouchInputMapperTest::processTouchMinor(
4741 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004742 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743}
4744
4745void MultiTouchInputMapperTest::processToolMajor(
4746 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004747 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748}
4749
4750void MultiTouchInputMapperTest::processToolMinor(
4751 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004752 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004753}
4754
4755void MultiTouchInputMapperTest::processOrientation(
4756 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004757 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758}
4759
4760void MultiTouchInputMapperTest::processPressure(
4761 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004762 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763}
4764
4765void MultiTouchInputMapperTest::processDistance(
4766 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004767 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768}
4769
4770void MultiTouchInputMapperTest::processId(
4771 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004772 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773}
4774
4775void MultiTouchInputMapperTest::processSlot(
4776 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004777 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778}
4779
4780void MultiTouchInputMapperTest::processToolType(
4781 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004782 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783}
4784
4785void MultiTouchInputMapperTest::processKey(
4786 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004787 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788}
4789
4790void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004791 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792}
4793
4794void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004795 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796}
4797
4798
4799TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4800 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4801 addConfigurationProperty("touch.deviceType", "touchScreen");
4802 prepareDisplay(DISPLAY_ORIENTATION_0);
4803 prepareAxes(POSITION);
4804 prepareVirtualKeys();
4805 addMapperAndConfigure(mapper);
4806
4807 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4808
4809 NotifyMotionArgs motionArgs;
4810
4811 // Two fingers down at once.
4812 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4813 processPosition(mapper, x1, y1);
4814 processMTSync(mapper);
4815 processPosition(mapper, x2, y2);
4816 processMTSync(mapper);
4817 processSync(mapper);
4818
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4821 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4822 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4823 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4824 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4825 ASSERT_EQ(0, motionArgs.flags);
4826 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4827 ASSERT_EQ(0, motionArgs.buttonState);
4828 ASSERT_EQ(0, motionArgs.edgeFlags);
4829 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4830 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4833 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4834 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4835 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4837
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4839 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4840 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4841 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4842 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4844 motionArgs.action);
4845 ASSERT_EQ(0, motionArgs.flags);
4846 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4847 ASSERT_EQ(0, motionArgs.buttonState);
4848 ASSERT_EQ(0, motionArgs.edgeFlags);
4849 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4850 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4852 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4855 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4857 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4858 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4859 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4860 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4861
4862 // Move.
4863 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4864 processPosition(mapper, x1, y1);
4865 processMTSync(mapper);
4866 processPosition(mapper, x2, y2);
4867 processMTSync(mapper);
4868 processSync(mapper);
4869
4870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4871 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4872 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4873 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4874 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4876 ASSERT_EQ(0, motionArgs.flags);
4877 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4878 ASSERT_EQ(0, motionArgs.buttonState);
4879 ASSERT_EQ(0, motionArgs.edgeFlags);
4880 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4881 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4883 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4886 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4888 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4889 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4890 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4891 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4892
4893 // First finger up.
4894 x2 += 15; y2 -= 20;
4895 processPosition(mapper, x2, y2);
4896 processMTSync(mapper);
4897 processSync(mapper);
4898
4899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4900 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4901 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4902 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4903 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4904 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4905 motionArgs.action);
4906 ASSERT_EQ(0, motionArgs.flags);
4907 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4908 ASSERT_EQ(0, motionArgs.buttonState);
4909 ASSERT_EQ(0, motionArgs.edgeFlags);
4910 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4911 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4913 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4916 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4918 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4919 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4920 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4922
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4924 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4925 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4926 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4927 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4929 ASSERT_EQ(0, motionArgs.flags);
4930 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4931 ASSERT_EQ(0, motionArgs.buttonState);
4932 ASSERT_EQ(0, motionArgs.edgeFlags);
4933 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4934 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4937 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4938 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4939 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4940 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4941
4942 // Move.
4943 x2 += 20; y2 -= 25;
4944 processPosition(mapper, x2, y2);
4945 processMTSync(mapper);
4946 processSync(mapper);
4947
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4949 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4950 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4951 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4952 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4954 ASSERT_EQ(0, motionArgs.flags);
4955 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4956 ASSERT_EQ(0, motionArgs.buttonState);
4957 ASSERT_EQ(0, motionArgs.edgeFlags);
4958 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4959 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4962 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4963 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4964 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4965 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4966
4967 // New finger down.
4968 int32_t x3 = 700, y3 = 300;
4969 processPosition(mapper, x2, y2);
4970 processMTSync(mapper);
4971 processPosition(mapper, x3, y3);
4972 processMTSync(mapper);
4973 processSync(mapper);
4974
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4976 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4977 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4978 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4979 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4980 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4981 motionArgs.action);
4982 ASSERT_EQ(0, motionArgs.flags);
4983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4984 ASSERT_EQ(0, motionArgs.buttonState);
4985 ASSERT_EQ(0, motionArgs.edgeFlags);
4986 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4989 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4992 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4994 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4995 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4996 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4997 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4998
4999 // Second finger up.
5000 x3 += 30; y3 -= 20;
5001 processPosition(mapper, x3, y3);
5002 processMTSync(mapper);
5003 processSync(mapper);
5004
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5006 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5007 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5008 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5009 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5010 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5011 motionArgs.action);
5012 ASSERT_EQ(0, motionArgs.flags);
5013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5014 ASSERT_EQ(0, motionArgs.buttonState);
5015 ASSERT_EQ(0, motionArgs.edgeFlags);
5016 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5019 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5022 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5024 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5025 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5026 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5028
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5030 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5031 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5032 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5033 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5035 ASSERT_EQ(0, motionArgs.flags);
5036 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5037 ASSERT_EQ(0, motionArgs.buttonState);
5038 ASSERT_EQ(0, motionArgs.edgeFlags);
5039 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5040 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5043 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5044 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5045 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5046 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5047
5048 // Last finger up.
5049 processMTSync(mapper);
5050 processSync(mapper);
5051
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5053 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5054 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5055 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5056 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5057 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5058 ASSERT_EQ(0, motionArgs.flags);
5059 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5060 ASSERT_EQ(0, motionArgs.buttonState);
5061 ASSERT_EQ(0, motionArgs.edgeFlags);
5062 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5063 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5066 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5067 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5068 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5069 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5070
5071 // Should not have sent any more keys or motions.
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5074}
5075
5076TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5077 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5078 addConfigurationProperty("touch.deviceType", "touchScreen");
5079 prepareDisplay(DISPLAY_ORIENTATION_0);
5080 prepareAxes(POSITION | ID);
5081 prepareVirtualKeys();
5082 addMapperAndConfigure(mapper);
5083
5084 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5085
5086 NotifyMotionArgs motionArgs;
5087
5088 // Two fingers down at once.
5089 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5090 processPosition(mapper, x1, y1);
5091 processId(mapper, 1);
5092 processMTSync(mapper);
5093 processPosition(mapper, x2, y2);
5094 processId(mapper, 2);
5095 processMTSync(mapper);
5096 processSync(mapper);
5097
5098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5099 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5100 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5101 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5104 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5105
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5107 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5108 motionArgs.action);
5109 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5110 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5112 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5115 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5117 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5118
5119 // Move.
5120 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5121 processPosition(mapper, x1, y1);
5122 processId(mapper, 1);
5123 processMTSync(mapper);
5124 processPosition(mapper, x2, y2);
5125 processId(mapper, 2);
5126 processMTSync(mapper);
5127 processSync(mapper);
5128
5129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5131 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5132 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5134 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5137 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5139 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5140
5141 // First finger up.
5142 x2 += 15; y2 -= 20;
5143 processPosition(mapper, x2, y2);
5144 processId(mapper, 2);
5145 processMTSync(mapper);
5146 processSync(mapper);
5147
5148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5149 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5150 motionArgs.action);
5151 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5152 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5154 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5157 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5159 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5160
5161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5163 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5164 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5167 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5168
5169 // Move.
5170 x2 += 20; y2 -= 25;
5171 processPosition(mapper, x2, y2);
5172 processId(mapper, 2);
5173 processMTSync(mapper);
5174 processSync(mapper);
5175
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5178 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5179 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5182 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5183
5184 // New finger down.
5185 int32_t x3 = 700, y3 = 300;
5186 processPosition(mapper, x2, y2);
5187 processId(mapper, 2);
5188 processMTSync(mapper);
5189 processPosition(mapper, x3, y3);
5190 processId(mapper, 3);
5191 processMTSync(mapper);
5192 processSync(mapper);
5193
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5195 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5196 motionArgs.action);
5197 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5198 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5200 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5203 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5205 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5206
5207 // Second finger up.
5208 x3 += 30; y3 -= 20;
5209 processPosition(mapper, x3, y3);
5210 processId(mapper, 3);
5211 processMTSync(mapper);
5212 processSync(mapper);
5213
5214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5215 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5216 motionArgs.action);
5217 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5218 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5220 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5223 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5225 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5226
5227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5229 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5230 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5233 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5234
5235 // Last finger up.
5236 processMTSync(mapper);
5237 processSync(mapper);
5238
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5240 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5241 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5242 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5245 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5246
5247 // Should not have sent any more keys or motions.
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5250}
5251
5252TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5253 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5254 addConfigurationProperty("touch.deviceType", "touchScreen");
5255 prepareDisplay(DISPLAY_ORIENTATION_0);
5256 prepareAxes(POSITION | ID | SLOT);
5257 prepareVirtualKeys();
5258 addMapperAndConfigure(mapper);
5259
5260 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5261
5262 NotifyMotionArgs motionArgs;
5263
5264 // Two fingers down at once.
5265 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5266 processPosition(mapper, x1, y1);
5267 processId(mapper, 1);
5268 processSlot(mapper, 1);
5269 processPosition(mapper, x2, y2);
5270 processId(mapper, 2);
5271 processSync(mapper);
5272
5273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5274 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5275 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5276 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5279 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5280
5281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5282 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5283 motionArgs.action);
5284 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5285 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5287 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5290 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5292 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5293
5294 // Move.
5295 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5296 processSlot(mapper, 0);
5297 processPosition(mapper, x1, y1);
5298 processSlot(mapper, 1);
5299 processPosition(mapper, x2, y2);
5300 processSync(mapper);
5301
5302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5304 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5305 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5307 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5310 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5312 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5313
5314 // First finger up.
5315 x2 += 15; y2 -= 20;
5316 processSlot(mapper, 0);
5317 processId(mapper, -1);
5318 processSlot(mapper, 1);
5319 processPosition(mapper, x2, y2);
5320 processSync(mapper);
5321
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5323 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5324 motionArgs.action);
5325 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5326 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5328 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5331 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5333 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5334
5335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5337 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5338 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5341 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5342
5343 // Move.
5344 x2 += 20; y2 -= 25;
5345 processPosition(mapper, x2, y2);
5346 processSync(mapper);
5347
5348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5350 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5351 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5354 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5355
5356 // New finger down.
5357 int32_t x3 = 700, y3 = 300;
5358 processPosition(mapper, x2, y2);
5359 processSlot(mapper, 0);
5360 processId(mapper, 3);
5361 processPosition(mapper, x3, y3);
5362 processSync(mapper);
5363
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5365 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5366 motionArgs.action);
5367 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5368 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5370 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5373 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5376
5377 // Second finger up.
5378 x3 += 30; y3 -= 20;
5379 processSlot(mapper, 1);
5380 processId(mapper, -1);
5381 processSlot(mapper, 0);
5382 processPosition(mapper, x3, y3);
5383 processSync(mapper);
5384
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5386 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5387 motionArgs.action);
5388 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5389 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5391 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5394 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5396 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5397
5398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5400 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5401 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5404 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5405
5406 // Last finger up.
5407 processId(mapper, -1);
5408 processSync(mapper);
5409
5410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5411 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5413 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5416 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5417
5418 // Should not have sent any more keys or motions.
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5421}
5422
5423TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5424 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5425 addConfigurationProperty("touch.deviceType", "touchScreen");
5426 prepareDisplay(DISPLAY_ORIENTATION_0);
5427 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5428 addMapperAndConfigure(mapper);
5429
5430 // These calculations are based on the input device calibration documentation.
5431 int32_t rawX = 100;
5432 int32_t rawY = 200;
5433 int32_t rawTouchMajor = 7;
5434 int32_t rawTouchMinor = 6;
5435 int32_t rawToolMajor = 9;
5436 int32_t rawToolMinor = 8;
5437 int32_t rawPressure = 11;
5438 int32_t rawDistance = 0;
5439 int32_t rawOrientation = 3;
5440 int32_t id = 5;
5441
5442 float x = toDisplayX(rawX);
5443 float y = toDisplayY(rawY);
5444 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5445 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5446 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5447 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5448 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5449 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5450 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5451 float distance = float(rawDistance);
5452
5453 processPosition(mapper, rawX, rawY);
5454 processTouchMajor(mapper, rawTouchMajor);
5455 processTouchMinor(mapper, rawTouchMinor);
5456 processToolMajor(mapper, rawToolMajor);
5457 processToolMinor(mapper, rawToolMinor);
5458 processPressure(mapper, rawPressure);
5459 processOrientation(mapper, rawOrientation);
5460 processDistance(mapper, rawDistance);
5461 processId(mapper, id);
5462 processMTSync(mapper);
5463 processSync(mapper);
5464
5465 NotifyMotionArgs args;
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5467 ASSERT_EQ(0, args.pointerProperties[0].id);
5468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5469 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5470 orientation, distance));
5471}
5472
5473TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5474 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5475 addConfigurationProperty("touch.deviceType", "touchScreen");
5476 prepareDisplay(DISPLAY_ORIENTATION_0);
5477 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5478 addConfigurationProperty("touch.size.calibration", "geometric");
5479 addMapperAndConfigure(mapper);
5480
5481 // These calculations are based on the input device calibration documentation.
5482 int32_t rawX = 100;
5483 int32_t rawY = 200;
5484 int32_t rawTouchMajor = 140;
5485 int32_t rawTouchMinor = 120;
5486 int32_t rawToolMajor = 180;
5487 int32_t rawToolMinor = 160;
5488
5489 float x = toDisplayX(rawX);
5490 float y = toDisplayY(rawY);
5491 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5492 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5493 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5494 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5495 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5496
5497 processPosition(mapper, rawX, rawY);
5498 processTouchMajor(mapper, rawTouchMajor);
5499 processTouchMinor(mapper, rawTouchMinor);
5500 processToolMajor(mapper, rawToolMajor);
5501 processToolMinor(mapper, rawToolMinor);
5502 processMTSync(mapper);
5503 processSync(mapper);
5504
5505 NotifyMotionArgs args;
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5508 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5509}
5510
5511TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5512 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5513 addConfigurationProperty("touch.deviceType", "touchScreen");
5514 prepareDisplay(DISPLAY_ORIENTATION_0);
5515 prepareAxes(POSITION | TOUCH | TOOL);
5516 addConfigurationProperty("touch.size.calibration", "diameter");
5517 addConfigurationProperty("touch.size.scale", "10");
5518 addConfigurationProperty("touch.size.bias", "160");
5519 addConfigurationProperty("touch.size.isSummed", "1");
5520 addMapperAndConfigure(mapper);
5521
5522 // These calculations are based on the input device calibration documentation.
5523 // Note: We only provide a single common touch/tool value because the device is assumed
5524 // not to emit separate values for each pointer (isSummed = 1).
5525 int32_t rawX = 100;
5526 int32_t rawY = 200;
5527 int32_t rawX2 = 150;
5528 int32_t rawY2 = 250;
5529 int32_t rawTouchMajor = 5;
5530 int32_t rawToolMajor = 8;
5531
5532 float x = toDisplayX(rawX);
5533 float y = toDisplayY(rawY);
5534 float x2 = toDisplayX(rawX2);
5535 float y2 = toDisplayY(rawY2);
5536 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5537 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5538 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5539
5540 processPosition(mapper, rawX, rawY);
5541 processTouchMajor(mapper, rawTouchMajor);
5542 processToolMajor(mapper, rawToolMajor);
5543 processMTSync(mapper);
5544 processPosition(mapper, rawX2, rawY2);
5545 processTouchMajor(mapper, rawTouchMajor);
5546 processToolMajor(mapper, rawToolMajor);
5547 processMTSync(mapper);
5548 processSync(mapper);
5549
5550 NotifyMotionArgs args;
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5552 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5553
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5555 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5556 args.action);
5557 ASSERT_EQ(size_t(2), args.pointerCount);
5558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5559 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5561 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5562}
5563
5564TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5565 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5566 addConfigurationProperty("touch.deviceType", "touchScreen");
5567 prepareDisplay(DISPLAY_ORIENTATION_0);
5568 prepareAxes(POSITION | TOUCH | TOOL);
5569 addConfigurationProperty("touch.size.calibration", "area");
5570 addConfigurationProperty("touch.size.scale", "43");
5571 addConfigurationProperty("touch.size.bias", "3");
5572 addMapperAndConfigure(mapper);
5573
5574 // These calculations are based on the input device calibration documentation.
5575 int32_t rawX = 100;
5576 int32_t rawY = 200;
5577 int32_t rawTouchMajor = 5;
5578 int32_t rawToolMajor = 8;
5579
5580 float x = toDisplayX(rawX);
5581 float y = toDisplayY(rawY);
5582 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5583 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5584 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5585
5586 processPosition(mapper, rawX, rawY);
5587 processTouchMajor(mapper, rawTouchMajor);
5588 processToolMajor(mapper, rawToolMajor);
5589 processMTSync(mapper);
5590 processSync(mapper);
5591
5592 NotifyMotionArgs args;
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5595 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5596}
5597
5598TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5599 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5600 addConfigurationProperty("touch.deviceType", "touchScreen");
5601 prepareDisplay(DISPLAY_ORIENTATION_0);
5602 prepareAxes(POSITION | PRESSURE);
5603 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5604 addConfigurationProperty("touch.pressure.scale", "0.01");
5605 addMapperAndConfigure(mapper);
5606
Michael Wrightaa449c92017-12-13 21:21:43 +00005607 InputDeviceInfo info;
5608 mapper->populateDeviceInfo(&info);
5609 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5610 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5611 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5612
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613 // These calculations are based on the input device calibration documentation.
5614 int32_t rawX = 100;
5615 int32_t rawY = 200;
5616 int32_t rawPressure = 60;
5617
5618 float x = toDisplayX(rawX);
5619 float y = toDisplayY(rawY);
5620 float pressure = float(rawPressure) * 0.01f;
5621
5622 processPosition(mapper, rawX, rawY);
5623 processPressure(mapper, rawPressure);
5624 processMTSync(mapper);
5625 processSync(mapper);
5626
5627 NotifyMotionArgs args;
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5630 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5631}
5632
5633TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5634 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5635 addConfigurationProperty("touch.deviceType", "touchScreen");
5636 prepareDisplay(DISPLAY_ORIENTATION_0);
5637 prepareAxes(POSITION | ID | SLOT);
5638 addMapperAndConfigure(mapper);
5639
5640 NotifyMotionArgs motionArgs;
5641 NotifyKeyArgs keyArgs;
5642
5643 processId(mapper, 1);
5644 processPosition(mapper, 100, 200);
5645 processSync(mapper);
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5647 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5648 ASSERT_EQ(0, motionArgs.buttonState);
5649
5650 // press BTN_LEFT, release BTN_LEFT
5651 processKey(mapper, BTN_LEFT, 1);
5652 processSync(mapper);
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5655 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5656
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5659 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5660
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661 processKey(mapper, BTN_LEFT, 0);
5662 processSync(mapper);
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005664 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005666
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005669 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670
5671 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5672 processKey(mapper, BTN_RIGHT, 1);
5673 processKey(mapper, BTN_MIDDLE, 1);
5674 processSync(mapper);
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5677 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5678 motionArgs.buttonState);
5679
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5682 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5683
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5686 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5687 motionArgs.buttonState);
5688
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689 processKey(mapper, BTN_RIGHT, 0);
5690 processSync(mapper);
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005697 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698
5699 processKey(mapper, BTN_MIDDLE, 0);
5700 processSync(mapper);
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005704
5705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005707 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708
5709 // press BTN_BACK, release BTN_BACK
5710 processKey(mapper, BTN_BACK, 1);
5711 processSync(mapper);
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5713 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5714 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005715
Michael Wrightd02c5b62014-02-10 15:10:22 -08005716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005718 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5719
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5722 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005723
5724 processKey(mapper, BTN_BACK, 0);
5725 processSync(mapper);
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005729
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005732 ASSERT_EQ(0, motionArgs.buttonState);
5733
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5735 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5736 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5737
5738 // press BTN_SIDE, release BTN_SIDE
5739 processKey(mapper, BTN_SIDE, 1);
5740 processSync(mapper);
5741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5742 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5743 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005744
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005747 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5751 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752
5753 processKey(mapper, BTN_SIDE, 0);
5754 processSync(mapper);
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005758
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005761 ASSERT_EQ(0, motionArgs.buttonState);
5762
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5764 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5765 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5766
5767 // press BTN_FORWARD, release BTN_FORWARD
5768 processKey(mapper, BTN_FORWARD, 1);
5769 processSync(mapper);
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5771 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5772 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005773
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005776 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5777
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5780 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781
5782 processKey(mapper, BTN_FORWARD, 0);
5783 processSync(mapper);
5784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005785 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005787
5788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005790 ASSERT_EQ(0, motionArgs.buttonState);
5791
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5793 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5794 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5795
5796 // press BTN_EXTRA, release BTN_EXTRA
5797 processKey(mapper, BTN_EXTRA, 1);
5798 processSync(mapper);
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5800 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5801 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005802
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005805 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5806
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5808 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5809 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810
5811 processKey(mapper, BTN_EXTRA, 0);
5812 processSync(mapper);
5813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005814 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005816
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005819 ASSERT_EQ(0, motionArgs.buttonState);
5820
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5822 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5823 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5824
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5826
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827 // press BTN_STYLUS, release BTN_STYLUS
5828 processKey(mapper, BTN_STYLUS, 1);
5829 processSync(mapper);
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005832 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5833
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5836 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837
5838 processKey(mapper, BTN_STYLUS, 0);
5839 processSync(mapper);
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005843
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847
5848 // press BTN_STYLUS2, release BTN_STYLUS2
5849 processKey(mapper, BTN_STYLUS2, 1);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005853 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5854
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5856 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5857 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858
5859 processKey(mapper, BTN_STYLUS2, 0);
5860 processSync(mapper);
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005863 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005867 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868
5869 // release touch
5870 processId(mapper, -1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5874 ASSERT_EQ(0, motionArgs.buttonState);
5875}
5876
5877TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
5878 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5879 addConfigurationProperty("touch.deviceType", "touchScreen");
5880 prepareDisplay(DISPLAY_ORIENTATION_0);
5881 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
5882 addMapperAndConfigure(mapper);
5883
5884 NotifyMotionArgs motionArgs;
5885
5886 // default tool type is finger
5887 processId(mapper, 1);
5888 processPosition(mapper, 100, 200);
5889 processSync(mapper);
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5891 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5893
5894 // eraser
5895 processKey(mapper, BTN_TOOL_RUBBER, 1);
5896 processSync(mapper);
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5900
5901 // stylus
5902 processKey(mapper, BTN_TOOL_RUBBER, 0);
5903 processKey(mapper, BTN_TOOL_PEN, 1);
5904 processSync(mapper);
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5908
5909 // brush
5910 processKey(mapper, BTN_TOOL_PEN, 0);
5911 processKey(mapper, BTN_TOOL_BRUSH, 1);
5912 processSync(mapper);
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5915 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5916
5917 // pencil
5918 processKey(mapper, BTN_TOOL_BRUSH, 0);
5919 processKey(mapper, BTN_TOOL_PENCIL, 1);
5920 processSync(mapper);
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5924
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005925 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 processKey(mapper, BTN_TOOL_PENCIL, 0);
5927 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5928 processSync(mapper);
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5932
5933 // mouse
5934 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5935 processKey(mapper, BTN_TOOL_MOUSE, 1);
5936 processSync(mapper);
5937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5939 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5940
5941 // lens
5942 processKey(mapper, BTN_TOOL_MOUSE, 0);
5943 processKey(mapper, BTN_TOOL_LENS, 1);
5944 processSync(mapper);
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5948
5949 // double-tap
5950 processKey(mapper, BTN_TOOL_LENS, 0);
5951 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5952 processSync(mapper);
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5956
5957 // triple-tap
5958 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5959 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5960 processSync(mapper);
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5964
5965 // quad-tap
5966 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5967 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5968 processSync(mapper);
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5972
5973 // finger
5974 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5975 processKey(mapper, BTN_TOOL_FINGER, 1);
5976 processSync(mapper);
5977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5980
5981 // stylus trumps finger
5982 processKey(mapper, BTN_TOOL_PEN, 1);
5983 processSync(mapper);
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5986 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5987
5988 // eraser trumps stylus
5989 processKey(mapper, BTN_TOOL_RUBBER, 1);
5990 processSync(mapper);
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5994
5995 // mouse trumps eraser
5996 processKey(mapper, BTN_TOOL_MOUSE, 1);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6001
6002 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6003 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6004 processSync(mapper);
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6008
6009 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6010 processToolType(mapper, MT_TOOL_PEN);
6011 processSync(mapper);
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6014 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6015
6016 // back to default tool type
6017 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6018 processKey(mapper, BTN_TOOL_MOUSE, 0);
6019 processKey(mapper, BTN_TOOL_RUBBER, 0);
6020 processKey(mapper, BTN_TOOL_PEN, 0);
6021 processKey(mapper, BTN_TOOL_FINGER, 0);
6022 processSync(mapper);
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6025 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6026}
6027
6028TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6029 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6030 addConfigurationProperty("touch.deviceType", "touchScreen");
6031 prepareDisplay(DISPLAY_ORIENTATION_0);
6032 prepareAxes(POSITION | ID | SLOT);
6033 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6034 addMapperAndConfigure(mapper);
6035
6036 NotifyMotionArgs motionArgs;
6037
6038 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6039 processId(mapper, 1);
6040 processPosition(mapper, 100, 200);
6041 processSync(mapper);
6042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6043 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6045 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6046
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6048 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6050 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6051
6052 // move a little
6053 processPosition(mapper, 150, 250);
6054 processSync(mapper);
6055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6056 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6058 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6059
6060 // down when BTN_TOUCH is pressed, pressure defaults to 1
6061 processKey(mapper, BTN_TOUCH, 1);
6062 processSync(mapper);
6063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6064 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6066 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6067
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6071 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6072
6073 // up when BTN_TOUCH is released, hover restored
6074 processKey(mapper, BTN_TOUCH, 0);
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6077 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6079 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6080
6081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6082 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6084 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6085
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6087 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6089 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6090
6091 // exit hover when pointer goes away
6092 processId(mapper, -1);
6093 processSync(mapper);
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6095 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6097 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6098}
6099
6100TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6101 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6102 addConfigurationProperty("touch.deviceType", "touchScreen");
6103 prepareDisplay(DISPLAY_ORIENTATION_0);
6104 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6105 addMapperAndConfigure(mapper);
6106
6107 NotifyMotionArgs motionArgs;
6108
6109 // initially hovering because pressure is 0
6110 processId(mapper, 1);
6111 processPosition(mapper, 100, 200);
6112 processPressure(mapper, 0);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6117 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6118
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6120 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6122 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6123
6124 // move a little
6125 processPosition(mapper, 150, 250);
6126 processSync(mapper);
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6128 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6130 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6131
6132 // down when pressure becomes non-zero
6133 processPressure(mapper, RAW_PRESSURE_MAX);
6134 processSync(mapper);
6135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6136 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6138 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6139
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6143 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6144
6145 // up when pressure becomes 0, hover restored
6146 processPressure(mapper, 0);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6149 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6151 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6152
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6156 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6157
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6159 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6161 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6162
6163 // exit hover when pointer goes away
6164 processId(mapper, -1);
6165 processSync(mapper);
6166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6167 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6169 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6170}
6171
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006172/**
6173 * Set the input device port <--> display port associations, and check that the
6174 * events are routed to the display that matches the display port.
6175 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6176 */
6177TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6178 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6179 const std::string usb2 = "USB2";
6180 const uint8_t hdmi1 = 0;
6181 const uint8_t hdmi2 = 1;
6182 const std::string secondaryUniqueId = "uniqueId2";
6183 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6184
6185 addConfigurationProperty("touch.deviceType", "touchScreen");
6186 prepareAxes(POSITION);
6187 addMapperAndConfigure(mapper);
6188
6189 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6190 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6191
6192 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6193 // for this input device is specified, and the matching viewport is not present,
6194 // the input device should be disabled (at the mapper level).
6195
6196 // Add viewport for display 2 on hdmi2
6197 prepareSecondaryDisplay(type, hdmi2);
6198 // Send a touch event
6199 processPosition(mapper, 100, 100);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6202
6203 // Add viewport for display 1 on hdmi1
6204 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6205 // Send a touch event again
6206 processPosition(mapper, 100, 100);
6207 processSync(mapper);
6208
6209 NotifyMotionArgs args;
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6211 ASSERT_EQ(DISPLAY_ID, args.displayId);
6212}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213
Arthur Hung41a712e2018-11-22 19:41:03 +08006214/**
6215 * Expect fallback to internal viewport if device is external and external viewport is not present.
6216 */
6217TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6218 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6219 prepareAxes(POSITION);
6220 addConfigurationProperty("touch.deviceType", "touchScreen");
6221 prepareDisplay(DISPLAY_ORIENTATION_0);
6222 mDevice->setExternal(true);
6223 addMapperAndConfigure(mapper);
6224
6225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6226
6227 NotifyMotionArgs motionArgs;
6228
6229 // Expect the event to be sent to the internal viewport,
6230 // because an external viewport is not present.
6231 processPosition(mapper, 100, 100);
6232 processSync(mapper);
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6234 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6235
6236 // Expect the event to be sent to the external viewport if it is present.
6237 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6238 processPosition(mapper, 100, 100);
6239 processSync(mapper);
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6241 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6242}
6243
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006244TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6245 // Setup PointerController for second display.
6246 sp<FakePointerController> fakePointerController = new FakePointerController();
6247 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6248 fakePointerController->setPosition(100, 200);
6249 fakePointerController->setButtonState(0);
6250 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6251 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6252
6253 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6254 prepareDisplay(DISPLAY_ORIENTATION_0);
6255 prepareAxes(POSITION);
6256 addMapperAndConfigure(mapper);
6257
6258 // Check source is mouse that would obtain the PointerController.
6259 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6260
6261 NotifyMotionArgs motionArgs;
6262 processPosition(mapper, 100, 100);
6263 processSync(mapper);
6264
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6267 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6268}
6269
Arthur Hung7c645402019-01-25 17:45:42 +08006270TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6271 // Setup the first touch screen device.
6272 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6273 prepareAxes(POSITION | ID | SLOT);
6274 addConfigurationProperty("touch.deviceType", "touchScreen");
6275 addMapperAndConfigure(mapper);
6276
6277 // Create the second touch screen device, and enable multi fingers.
6278 const std::string USB2 = "USB2";
6279 const int32_t SECOND_DEVICE_ID = 2;
6280 InputDeviceIdentifier identifier;
6281 identifier.name = DEVICE_NAME;
6282 identifier.location = USB2;
6283 InputDevice* device2 = new InputDevice(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6284 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
6285 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6286 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6287 0 /*flat*/, 0 /*fuzz*/);
6288 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6289 0 /*flat*/, 0 /*fuzz*/);
6290 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6291 0 /*flat*/, 0 /*fuzz*/);
6292 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6293 0 /*flat*/, 0 /*fuzz*/);
6294 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6295 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6296 String8("touchScreen"));
6297
6298 // Setup the second touch screen device.
6299 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2);
6300 device2->addMapper(mapper2);
6301 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6302 device2->reset(ARBITRARY_TIME);
6303
6304 // Setup PointerController.
6305 sp<FakePointerController> fakePointerController = new FakePointerController();
6306 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6307 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6308
6309 // Setup policy for associated displays and show touches.
6310 const uint8_t hdmi1 = 0;
6311 const uint8_t hdmi2 = 1;
6312 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6313 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6314 mFakePolicy->setShowTouches(true);
6315
6316 // Create displays.
6317 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6318 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6319
6320 // Default device will reconfigure above, need additional reconfiguration for another device.
6321 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6322 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6323
6324 // Two fingers down at default display.
6325 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6326 processPosition(mapper, x1, y1);
6327 processId(mapper, 1);
6328 processSlot(mapper, 1);
6329 processPosition(mapper, x2, y2);
6330 processId(mapper, 2);
6331 processSync(mapper);
6332
6333 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6334 fakePointerController->getSpots().find(DISPLAY_ID);
6335 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6336 ASSERT_EQ(size_t(2), iter->second.size());
6337
6338 // Two fingers down at second display.
6339 processPosition(mapper2, x1, y1);
6340 processId(mapper2, 1);
6341 processSlot(mapper2, 1);
6342 processPosition(mapper2, x2, y2);
6343 processId(mapper2, 2);
6344 processSync(mapper2);
6345
6346 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6347 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6348 ASSERT_EQ(size_t(2), iter->second.size());
6349}
6350
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006351TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6352 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6353 prepareAxes(POSITION);
6354 addConfigurationProperty("touch.deviceType", "touchScreen");
6355 prepareDisplay(DISPLAY_ORIENTATION_0);
6356 addMapperAndConfigure(mapper);
6357
6358 NotifyMotionArgs motionArgs;
6359 // Unrotated video frame
6360 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6361 std::vector<TouchVideoFrame> frames{frame};
6362 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6363 processPosition(mapper, 100, 200);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(frames, motionArgs.videoFrames);
6367
6368 // Subsequent touch events should not have any videoframes
6369 // This is implemented separately in FakeEventHub,
6370 // but that should match the behaviour of TouchVideoDevice.
6371 processPosition(mapper, 200, 200);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6375}
6376
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006377TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6378 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6379 prepareAxes(POSITION);
6380 addConfigurationProperty("touch.deviceType", "touchScreen");
6381 addMapperAndConfigure(mapper);
6382 // Unrotated video frame
6383 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6384 NotifyMotionArgs motionArgs;
6385
6386 // Test all 4 orientations
6387 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6388 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6389 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6390 clearViewports();
6391 prepareDisplay(orientation);
6392 std::vector<TouchVideoFrame> frames{frame};
6393 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6394 processPosition(mapper, 100, 200);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 frames[0].rotate(orientation);
6398 ASSERT_EQ(frames, motionArgs.videoFrames);
6399 }
6400}
6401
6402TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6403 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6404 prepareAxes(POSITION);
6405 addConfigurationProperty("touch.deviceType", "touchScreen");
6406 addMapperAndConfigure(mapper);
6407 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6408 // so mix these.
6409 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6410 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6411 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6412 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6413 NotifyMotionArgs motionArgs;
6414
6415 prepareDisplay(DISPLAY_ORIENTATION_90);
6416 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6417 processPosition(mapper, 100, 200);
6418 processSync(mapper);
6419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6420 std::for_each(frames.begin(), frames.end(),
6421 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6422 ASSERT_EQ(frames, motionArgs.videoFrames);
6423}
6424
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425} // namespace android