blob: 2153108186cb520df469d03786d1a998c70fa578 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
21#include <KeyboardInputMapper.h>
22#include <MultiTouchInputMapper.h>
23#include <SingleTouchInputMapper.h>
24#include <SwitchInputMapper.h>
25#include <TestInputListener.h>
26#include <TouchInputMapper.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080029#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <math.h>
31
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080032
Michael Wrightd02c5b62014-02-10 15:10:22 -080033namespace android {
34
35// An arbitrary time value.
36static const nsecs_t ARBITRARY_TIME = 1234;
37
38// Arbitrary display properties.
39static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070040static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080041static const int32_t DISPLAY_WIDTH = 480;
42static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070043static const int32_t VIRTUAL_DISPLAY_ID = 1;
44static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
45static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070046static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070047static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080048
49// Error tolerance for floating point assertions.
50static const float EPSILON = 0.001f;
51
52template<typename T>
53static inline T min(T a, T b) {
54 return a < b ? a : b;
55}
56
57static inline float avg(float x, float y) {
58 return (x + y) / 2;
59}
60
61
62// --- FakePointerController ---
63
64class FakePointerController : public PointerControllerInterface {
65 bool mHaveBounds;
66 float mMinX, mMinY, mMaxX, mMaxY;
67 float mX, mY;
68 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080069 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080070
71protected:
72 virtual ~FakePointerController() { }
73
74public:
75 FakePointerController() :
76 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080077 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080078 }
79
80 void setBounds(float minX, float minY, float maxX, float maxY) {
81 mHaveBounds = true;
82 mMinX = minX;
83 mMinY = minY;
84 mMaxX = maxX;
85 mMaxY = maxY;
86 }
87
Arthur Hungc7ad2d02018-12-18 17:41:29 +080088 void setDisplayId(int32_t displayId) {
89 mDisplayId = displayId;
90 }
91
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 virtual void setPosition(float x, float y) {
93 mX = x;
94 mY = y;
95 }
96
97 virtual void setButtonState(int32_t buttonState) {
98 mButtonState = buttonState;
99 }
100
101 virtual int32_t getButtonState() const {
102 return mButtonState;
103 }
104
105 virtual void getPosition(float* outX, float* outY) const {
106 *outX = mX;
107 *outY = mY;
108 }
109
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800110 virtual int32_t getDisplayId() const {
111 return mDisplayId;
112 }
113
Arthur Hung7c645402019-01-25 17:45:42 +0800114 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
115 return mSpotsByDisplay;
116 }
117
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118private:
119 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
120 *outMinX = mMinX;
121 *outMinY = mMinY;
122 *outMaxX = mMaxX;
123 *outMaxY = mMaxY;
124 return mHaveBounds;
125 }
126
127 virtual void move(float deltaX, float deltaY) {
128 mX += deltaX;
129 if (mX < mMinX) mX = mMinX;
130 if (mX > mMaxX) mX = mMaxX;
131 mY += deltaY;
132 if (mY < mMinY) mY = mMinY;
133 if (mY > mMaxY) mY = mMaxY;
134 }
135
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100136 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137 }
138
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100139 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 }
141
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100142 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 }
144
Arthur Hung7c645402019-01-25 17:45:42 +0800145 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
146 int32_t displayId) {
147 std::vector<int32_t> newSpots;
148 // Add spots for fingers that are down.
149 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
150 uint32_t id = idBits.clearFirstMarkedBit();
151 newSpots.push_back(id);
152 }
153
154 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155 }
156
157 virtual void clearSpots() {
158 }
Arthur Hung7c645402019-01-25 17:45:42 +0800159
160 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161};
162
163
164// --- FakeInputReaderPolicy ---
165
166class FakeInputReaderPolicy : public InputReaderPolicyInterface {
167 InputReaderConfiguration mConfig;
168 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800169 std::vector<InputDeviceInfo> mInputDevices;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100170 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700171 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
173protected:
174 virtual ~FakeInputReaderPolicy() { }
175
176public:
177 FakeInputReaderPolicy() {
178 }
179
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700180 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100181 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100182 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700183 }
184
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700185 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
186 return mConfig.getDisplayViewportByUniqueId(uniqueId);
187 }
188 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
189 return mConfig.getDisplayViewportByType(type);
190 }
191
192 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
193 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700194 }
195
196 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700197 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
198 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700199 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700200 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700201 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100202 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 }
204
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100205 void addExcludedDeviceName(const std::string& deviceName) {
206 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 }
208
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700209 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
210 mConfig.portAssociations.insert({inputPort, displayPort});
211 }
212
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000213 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700214
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000215 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700216
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
218 mPointerControllers.add(deviceId, controller);
219 }
220
221 const InputReaderConfiguration* getReaderConfiguration() const {
222 return &mConfig;
223 }
224
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800225 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226 return mInputDevices;
227 }
228
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100229 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700230 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700231 return transform;
232 }
233
234 void setTouchAffineTransformation(const TouchAffineTransformation t) {
235 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800236 }
237
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800238 void setPointerCapture(bool enabled) {
239 mConfig.pointerCapture = enabled;
240 }
241
Arthur Hung7c645402019-01-25 17:45:42 +0800242 void setShowTouches(bool enabled) {
243 mConfig.showTouches = enabled;
244 }
245
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700247 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700248 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
249 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700250 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
251 || orientation == DISPLAY_ORIENTATION_270);
252 DisplayViewport v;
253 v.displayId = displayId;
254 v.orientation = orientation;
255 v.logicalLeft = 0;
256 v.logicalTop = 0;
257 v.logicalRight = isRotated ? height : width;
258 v.logicalBottom = isRotated ? width : height;
259 v.physicalLeft = 0;
260 v.physicalTop = 0;
261 v.physicalRight = isRotated ? height : width;
262 v.physicalBottom = isRotated ? width : height;
263 v.deviceWidth = isRotated ? height : width;
264 v.deviceHeight = isRotated ? width : height;
265 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700266 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100267 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700268 return v;
269 }
270
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
272 *outConfig = mConfig;
273 }
274
275 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
276 return mPointerControllers.valueFor(deviceId);
277 }
278
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800279 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 mInputDevices = inputDevices;
281 }
282
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100283 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700284 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 }
286
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100287 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
288 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 }
290};
291
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292// --- FakeEventHub ---
293
294class FakeEventHub : public EventHubInterface {
295 struct KeyInfo {
296 int32_t keyCode;
297 uint32_t flags;
298 };
299
300 struct Device {
301 InputDeviceIdentifier identifier;
302 uint32_t classes;
303 PropertyMap configuration;
304 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
305 KeyedVector<int, bool> relativeAxes;
306 KeyedVector<int32_t, int32_t> keyCodeStates;
307 KeyedVector<int32_t, int32_t> scanCodeStates;
308 KeyedVector<int32_t, int32_t> switchStates;
309 KeyedVector<int32_t, int32_t> absoluteAxisValue;
310 KeyedVector<int32_t, KeyInfo> keysByScanCode;
311 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
312 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800313 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700314 bool enabled;
315
316 status_t enable() {
317 enabled = true;
318 return OK;
319 }
320
321 status_t disable() {
322 enabled = false;
323 return OK;
324 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700326 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700327 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 }
329 };
330
331 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100332 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 List<RawEvent> mEvents;
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600334 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700336public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 virtual ~FakeEventHub() {
338 for (size_t i = 0; i < mDevices.size(); i++) {
339 delete mDevices.valueAt(i);
340 }
341 }
342
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 FakeEventHub() { }
344
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100345 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 Device* device = new Device(classes);
347 device->identifier.name = name;
348 mDevices.add(deviceId, device);
349
350 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
351 }
352
353 void removeDevice(int32_t deviceId) {
354 delete mDevices.valueFor(deviceId);
355 mDevices.removeItem(deviceId);
356
357 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
358 }
359
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700360 bool isDeviceEnabled(int32_t deviceId) {
361 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700362 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700363 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
364 return false;
365 }
366 return device->enabled;
367 }
368
369 status_t enableDevice(int32_t deviceId) {
370 status_t result;
371 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700372 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700373 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
374 return BAD_VALUE;
375 }
376 if (device->enabled) {
377 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
378 return OK;
379 }
380 result = device->enable();
381 return result;
382 }
383
384 status_t disableDevice(int32_t deviceId) {
385 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700386 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700387 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
388 return BAD_VALUE;
389 }
390 if (!device->enabled) {
391 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
392 return OK;
393 }
394 return device->disable();
395 }
396
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 void finishDeviceScan() {
398 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
399 }
400
401 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
402 Device* device = getDevice(deviceId);
403 device->configuration.addProperty(key, value);
404 }
405
406 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
407 Device* device = getDevice(deviceId);
408 device->configuration.addAll(configuration);
409 }
410
411 void addAbsoluteAxis(int32_t deviceId, int axis,
412 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
413 Device* device = getDevice(deviceId);
414
415 RawAbsoluteAxisInfo info;
416 info.valid = true;
417 info.minValue = minValue;
418 info.maxValue = maxValue;
419 info.flat = flat;
420 info.fuzz = fuzz;
421 info.resolution = resolution;
422 device->absoluteAxes.add(axis, info);
423 }
424
425 void addRelativeAxis(int32_t deviceId, int32_t axis) {
426 Device* device = getDevice(deviceId);
427 device->relativeAxes.add(axis, true);
428 }
429
430 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
431 Device* device = getDevice(deviceId);
432 device->keyCodeStates.replaceValueFor(keyCode, state);
433 }
434
435 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
436 Device* device = getDevice(deviceId);
437 device->scanCodeStates.replaceValueFor(scanCode, state);
438 }
439
440 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
441 Device* device = getDevice(deviceId);
442 device->switchStates.replaceValueFor(switchCode, state);
443 }
444
445 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
446 Device* device = getDevice(deviceId);
447 device->absoluteAxisValue.replaceValueFor(axis, value);
448 }
449
450 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
451 int32_t keyCode, uint32_t flags) {
452 Device* device = getDevice(deviceId);
453 KeyInfo info;
454 info.keyCode = keyCode;
455 info.flags = flags;
456 if (scanCode) {
457 device->keysByScanCode.add(scanCode, info);
458 }
459 if (usageCode) {
460 device->keysByUsageCode.add(usageCode, info);
461 }
462 }
463
464 void addLed(int32_t deviceId, int32_t led, bool initialState) {
465 Device* device = getDevice(deviceId);
466 device->leds.add(led, initialState);
467 }
468
469 bool getLedState(int32_t deviceId, int32_t led) {
470 Device* device = getDevice(deviceId);
471 return device->leds.valueFor(led);
472 }
473
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100474 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 return mExcludedDevices;
476 }
477
478 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
479 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800480 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481 }
482
483 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
484 int32_t code, int32_t value) {
485 RawEvent event;
486 event.when = when;
487 event.deviceId = deviceId;
488 event.type = type;
489 event.code = code;
490 event.value = value;
491 mEvents.push_back(event);
492
493 if (type == EV_ABS) {
494 setAbsoluteAxisValue(deviceId, code, value);
495 }
496 }
497
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600498 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
499 std::vector<TouchVideoFrame>> videoFrames) {
500 mVideoFrames = std::move(videoFrames);
501 }
502
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 void assertQueueIsEmpty() {
504 ASSERT_EQ(size_t(0), mEvents.size())
505 << "Expected the event queue to be empty (fully consumed).";
506 }
507
508private:
509 Device* getDevice(int32_t deviceId) const {
510 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100511 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 }
513
514 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
515 Device* device = getDevice(deviceId);
516 return device ? device->classes : 0;
517 }
518
519 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
520 Device* device = getDevice(deviceId);
521 return device ? device->identifier : InputDeviceIdentifier();
522 }
523
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100524 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 return 0;
526 }
527
528 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
529 Device* device = getDevice(deviceId);
530 if (device) {
531 *outConfiguration = device->configuration;
532 }
533 }
534
535 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
536 RawAbsoluteAxisInfo* outAxisInfo) const {
537 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800538 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 ssize_t index = device->absoluteAxes.indexOfKey(axis);
540 if (index >= 0) {
541 *outAxisInfo = device->absoluteAxes.valueAt(index);
542 return OK;
543 }
544 }
545 outAxisInfo->clear();
546 return -1;
547 }
548
549 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
550 Device* device = getDevice(deviceId);
551 if (device) {
552 return device->relativeAxes.indexOfKey(axis) >= 0;
553 }
554 return false;
555 }
556
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100557 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558 return false;
559 }
560
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700561 virtual status_t mapKey(int32_t deviceId,
562 int32_t scanCode, int32_t usageCode, int32_t metaState,
563 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800564 Device* device = getDevice(deviceId);
565 if (device) {
566 const KeyInfo* key = getKey(device, scanCode, usageCode);
567 if (key) {
568 if (outKeycode) {
569 *outKeycode = key->keyCode;
570 }
571 if (outFlags) {
572 *outFlags = key->flags;
573 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700574 if (outMetaState) {
575 *outMetaState = metaState;
576 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 return OK;
578 }
579 }
580 return NAME_NOT_FOUND;
581 }
582
583 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
584 if (usageCode) {
585 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
586 if (index >= 0) {
587 return &device->keysByUsageCode.valueAt(index);
588 }
589 }
590 if (scanCode) {
591 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
592 if (index >= 0) {
593 return &device->keysByScanCode.valueAt(index);
594 }
595 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700596 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 }
598
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100599 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 return NAME_NOT_FOUND;
601 }
602
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100603 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604 mExcludedDevices = devices;
605 }
606
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100607 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608 if (mEvents.empty()) {
609 return 0;
610 }
611
612 *buffer = *mEvents.begin();
613 mEvents.erase(mEvents.begin());
614 return 1;
615 }
616
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800617 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600618 auto it = mVideoFrames.find(deviceId);
619 if (it != mVideoFrames.end()) {
620 std::vector<TouchVideoFrame> frames = std::move(it->second);
621 mVideoFrames.erase(deviceId);
622 return frames;
623 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800624 return {};
625 }
626
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
628 Device* device = getDevice(deviceId);
629 if (device) {
630 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
631 if (index >= 0) {
632 return device->scanCodeStates.valueAt(index);
633 }
634 }
635 return AKEY_STATE_UNKNOWN;
636 }
637
638 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
639 Device* device = getDevice(deviceId);
640 if (device) {
641 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
642 if (index >= 0) {
643 return device->keyCodeStates.valueAt(index);
644 }
645 }
646 return AKEY_STATE_UNKNOWN;
647 }
648
649 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
650 Device* device = getDevice(deviceId);
651 if (device) {
652 ssize_t index = device->switchStates.indexOfKey(sw);
653 if (index >= 0) {
654 return device->switchStates.valueAt(index);
655 }
656 }
657 return AKEY_STATE_UNKNOWN;
658 }
659
660 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
661 int32_t* outValue) const {
662 Device* device = getDevice(deviceId);
663 if (device) {
664 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
665 if (index >= 0) {
666 *outValue = device->absoluteAxisValue.valueAt(index);
667 return OK;
668 }
669 }
670 *outValue = 0;
671 return -1;
672 }
673
674 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
675 uint8_t* outFlags) const {
676 bool result = false;
677 Device* device = getDevice(deviceId);
678 if (device) {
679 for (size_t i = 0; i < numCodes; i++) {
680 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
681 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
682 outFlags[i] = 1;
683 result = true;
684 }
685 }
686 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
687 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
688 outFlags[i] = 1;
689 result = true;
690 }
691 }
692 }
693 }
694 return result;
695 }
696
697 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
698 Device* device = getDevice(deviceId);
699 if (device) {
700 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
701 return index >= 0;
702 }
703 return false;
704 }
705
706 virtual bool hasLed(int32_t deviceId, int32_t led) const {
707 Device* device = getDevice(deviceId);
708 return device && device->leds.indexOfKey(led) >= 0;
709 }
710
711 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
712 Device* device = getDevice(deviceId);
713 if (device) {
714 ssize_t index = device->leds.indexOfKey(led);
715 if (index >= 0) {
716 device->leds.replaceValueAt(led, on);
717 } else {
718 ADD_FAILURE()
719 << "Attempted to set the state of an LED that the EventHub declared "
720 "was not present. led=" << led;
721 }
722 }
723 }
724
725 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800726 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 outVirtualKeys.clear();
728
729 Device* device = getDevice(deviceId);
730 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800731 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732 }
733 }
734
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100735 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700736 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 }
738
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100739 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 return false;
741 }
742
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100743 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744 }
745
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100746 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 }
748
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100749 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 return false;
751 }
752
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800753 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 }
755
756 virtual void monitor() {
757 }
758
759 virtual void requestReopenDevices() {
760 }
761
762 virtual void wake() {
763 }
764};
765
766
767// --- FakeInputReaderContext ---
768
769class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700770 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 sp<InputReaderPolicyInterface> mPolicy;
772 sp<InputListenerInterface> mListener;
773 int32_t mGlobalMetaState;
774 bool mUpdateGlobalMetaStateWasCalled;
775 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800776 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777
778public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700779 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
780 const sp<InputReaderPolicyInterface>& policy,
781 const sp<InputListenerInterface>& listener)
782 : mEventHub(eventHub),
783 mPolicy(policy),
784 mListener(listener),
785 mGlobalMetaState(0),
786 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788 virtual ~FakeInputReaderContext() { }
789
790 void assertUpdateGlobalMetaStateWasCalled() {
791 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
792 << "Expected updateGlobalMetaState() to have been called.";
793 mUpdateGlobalMetaStateWasCalled = false;
794 }
795
796 void setGlobalMetaState(int32_t state) {
797 mGlobalMetaState = state;
798 }
799
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800800 uint32_t getGeneration() {
801 return mGeneration;
802 }
803
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804private:
805 virtual void updateGlobalMetaState() {
806 mUpdateGlobalMetaStateWasCalled = true;
807 }
808
809 virtual int32_t getGlobalMetaState() {
810 return mGlobalMetaState;
811 }
812
813 virtual EventHubInterface* getEventHub() {
814 return mEventHub.get();
815 }
816
817 virtual InputReaderPolicyInterface* getPolicy() {
818 return mPolicy.get();
819 }
820
821 virtual InputListenerInterface* getListener() {
822 return mListener.get();
823 }
824
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100825 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 }
827
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100828 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 return false;
830 }
831
832 virtual void fadePointer() {
833 }
834
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100835 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 }
837
838 virtual int32_t bumpGeneration() {
839 return ++mGeneration;
840 }
Michael Wright842500e2015-03-13 17:32:02 -0700841
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800842 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700843
844 }
845
846 virtual void dispatchExternalStylusState(const StylusState&) {
847
848 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800849
850 virtual uint32_t getNextSequenceNum() {
851 return mNextSequenceNum++;
852 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853};
854
855
856// --- FakeInputMapper ---
857
858class FakeInputMapper : public InputMapper {
859 uint32_t mSources;
860 int32_t mKeyboardType;
861 int32_t mMetaState;
862 KeyedVector<int32_t, int32_t> mKeyCodeStates;
863 KeyedVector<int32_t, int32_t> mScanCodeStates;
864 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800865 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866 RawEvent mLastEvent;
867
868 bool mConfigureWasCalled;
869 bool mResetWasCalled;
870 bool mProcessWasCalled;
871
Arthur Hungc23540e2018-11-29 20:42:11 +0800872 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873public:
874 FakeInputMapper(InputDevice* device, uint32_t sources) :
875 InputMapper(device),
876 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
877 mMetaState(0),
878 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
879 }
880
881 virtual ~FakeInputMapper() { }
882
883 void setKeyboardType(int32_t keyboardType) {
884 mKeyboardType = keyboardType;
885 }
886
887 void setMetaState(int32_t metaState) {
888 mMetaState = metaState;
889 }
890
891 void assertConfigureWasCalled() {
892 ASSERT_TRUE(mConfigureWasCalled)
893 << "Expected configure() to have been called.";
894 mConfigureWasCalled = false;
895 }
896
897 void assertResetWasCalled() {
898 ASSERT_TRUE(mResetWasCalled)
899 << "Expected reset() to have been called.";
900 mResetWasCalled = false;
901 }
902
Yi Kong9b14ac62018-07-17 13:48:38 -0700903 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 ASSERT_TRUE(mProcessWasCalled)
905 << "Expected process() to have been called.";
906 if (outLastEvent) {
907 *outLastEvent = mLastEvent;
908 }
909 mProcessWasCalled = false;
910 }
911
912 void setKeyCodeState(int32_t keyCode, int32_t state) {
913 mKeyCodeStates.replaceValueFor(keyCode, state);
914 }
915
916 void setScanCodeState(int32_t scanCode, int32_t state) {
917 mScanCodeStates.replaceValueFor(scanCode, state);
918 }
919
920 void setSwitchState(int32_t switchCode, int32_t state) {
921 mSwitchStates.replaceValueFor(switchCode, state);
922 }
923
924 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800925 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 }
927
928private:
929 virtual uint32_t getSources() {
930 return mSources;
931 }
932
933 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
934 InputMapper::populateDeviceInfo(deviceInfo);
935
936 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
937 deviceInfo->setKeyboardType(mKeyboardType);
938 }
939 }
940
Arthur Hungc23540e2018-11-29 20:42:11 +0800941 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800943
944 // Find the associated viewport if exist.
945 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
946 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
947 mViewport = config->getDisplayViewportByPort(*displayPort);
948 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 }
950
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100951 virtual void reset(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 mResetWasCalled = true;
953 }
954
955 virtual void process(const RawEvent* rawEvent) {
956 mLastEvent = *rawEvent;
957 mProcessWasCalled = true;
958 }
959
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100960 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
962 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
963 }
964
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100965 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
967 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
968 }
969
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100970 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971 ssize_t index = mSwitchStates.indexOfKey(switchCode);
972 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
973 }
974
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100975 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976 const int32_t* keyCodes, uint8_t* outFlags) {
977 bool result = false;
978 for (size_t i = 0; i < numCodes; i++) {
979 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
980 if (keyCodes[i] == mSupportedKeyCodes[j]) {
981 outFlags[i] = 1;
982 result = true;
983 }
984 }
985 }
986 return result;
987 }
988
989 virtual int32_t getMetaState() {
990 return mMetaState;
991 }
992
993 virtual void fadePointer() {
994 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800995
996 virtual std::optional<int32_t> getAssociatedDisplay() {
997 if (mViewport) {
998 return std::make_optional(mViewport->displayId);
999 }
1000 return std::nullopt;
1001 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002};
1003
1004
1005// --- InstrumentedInputReader ---
1006
1007class InstrumentedInputReader : public InputReader {
1008 InputDevice* mNextDevice;
1009
1010public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001011 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1012 const sp<InputReaderPolicyInterface>& policy,
1013 const sp<InputListenerInterface>& listener)
1014 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015
1016 virtual ~InstrumentedInputReader() {
1017 if (mNextDevice) {
1018 delete mNextDevice;
1019 }
1020 }
1021
1022 void setNextDevice(InputDevice* device) {
1023 mNextDevice = device;
1024 }
1025
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001026 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Arthur Hungc23540e2018-11-29 20:42:11 +08001027 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001028 InputDeviceIdentifier identifier;
1029 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001030 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 int32_t generation = deviceId + 1;
1032 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
1033 classes);
1034 }
1035
1036protected:
1037 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
1038 const InputDeviceIdentifier& identifier, uint32_t classes) {
1039 if (mNextDevice) {
1040 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001041 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042 return device;
1043 }
1044 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1045 }
1046
1047 friend class InputReaderTest;
1048};
1049
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001050// --- InputReaderPolicyTest ---
1051class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001052protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001053 sp<FakeInputReaderPolicy> mFakePolicy;
1054
1055 virtual void SetUp() {
1056 mFakePolicy = new FakeInputReaderPolicy();
1057 }
1058 virtual void TearDown() {
1059 mFakePolicy.clear();
1060 }
1061};
1062
1063/**
1064 * Check that empty set of viewports is an acceptable configuration.
1065 * Also try to get internal viewport two different ways - by type and by uniqueId.
1066 *
1067 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1068 * Such configuration is not currently allowed.
1069 */
1070TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001071 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001072
1073 // We didn't add any viewports yet, so there shouldn't be any.
1074 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001075 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001076 ASSERT_FALSE(internalViewport);
1077
1078 // Add an internal viewport, then clear it
1079 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001080 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001081
1082 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001083 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001084 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001085 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001086
1087 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001088 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001089 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001090 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001091
1092 mFakePolicy->clearViewports();
1093 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001094 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001095 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001096 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001097 ASSERT_FALSE(internalViewport);
1098}
1099
1100TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1101 const std::string internalUniqueId = "local:0";
1102 const std::string externalUniqueId = "local:1";
1103 const std::string virtualUniqueId1 = "virtual:2";
1104 const std::string virtualUniqueId2 = "virtual:3";
1105 constexpr int32_t virtualDisplayId1 = 2;
1106 constexpr int32_t virtualDisplayId2 = 3;
1107
1108 // Add an internal viewport
1109 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001110 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001111 // Add an external viewport
1112 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001113 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001114 // Add an virtual viewport
1115 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001116 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001117 // Add another virtual viewport
1118 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001119 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001120
1121 // Check matching by type for internal
1122 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001123 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001124 ASSERT_TRUE(internalViewport);
1125 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1126
1127 // Check matching by type for external
1128 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001129 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001130 ASSERT_TRUE(externalViewport);
1131 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1132
1133 // Check matching by uniqueId for virtual viewport #1
1134 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001135 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001136 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001137 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001138 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1139 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1140
1141 // Check matching by uniqueId for virtual viewport #2
1142 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001143 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001144 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001145 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001146 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1147 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1148}
1149
1150
1151/**
1152 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1153 * that lookup works by checking display id.
1154 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1155 */
1156TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1157 const std::string uniqueId1 = "uniqueId1";
1158 const std::string uniqueId2 = "uniqueId2";
1159 constexpr int32_t displayId1 = 2;
1160 constexpr int32_t displayId2 = 3;
1161
1162 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1163 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1164 for (const ViewportType& type : types) {
1165 mFakePolicy->clearViewports();
1166 // Add a viewport
1167 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001168 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001169 // Add another viewport
1170 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001171 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001172
1173 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001174 std::optional<DisplayViewport> viewport1 =
1175 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001176 ASSERT_TRUE(viewport1);
1177 ASSERT_EQ(displayId1, viewport1->displayId);
1178 ASSERT_EQ(type, viewport1->type);
1179
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001180 std::optional<DisplayViewport> viewport2 =
1181 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001182 ASSERT_TRUE(viewport2);
1183 ASSERT_EQ(displayId2, viewport2->displayId);
1184 ASSERT_EQ(type, viewport2->type);
1185
1186 // When there are multiple viewports of the same kind, and uniqueId is not specified
1187 // in the call to getDisplayViewport, then that situation is not supported.
1188 // The viewports can be stored in any order, so we cannot rely on the order, since that
1189 // is just implementation detail.
1190 // However, we can check that it still returns *a* viewport, we just cannot assert
1191 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001192 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001193 ASSERT_TRUE(someViewport);
1194 }
1195}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001197/**
1198 * Check getDisplayViewportByPort
1199 */
1200TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1201 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1202 const std::string uniqueId1 = "uniqueId1";
1203 const std::string uniqueId2 = "uniqueId2";
1204 constexpr int32_t displayId1 = 1;
1205 constexpr int32_t displayId2 = 2;
1206 const uint8_t hdmi1 = 0;
1207 const uint8_t hdmi2 = 1;
1208 const uint8_t hdmi3 = 2;
1209
1210 mFakePolicy->clearViewports();
1211 // Add a viewport that's associated with some display port that's not of interest.
1212 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1213 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1214 // Add another viewport, connected to HDMI1 port
1215 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1216 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1217
1218 // Check that correct display viewport was returned by comparing the display ports.
1219 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1220 ASSERT_TRUE(hdmi1Viewport);
1221 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1222 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1223
1224 // Check that we can still get the same viewport using the uniqueId
1225 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1226 ASSERT_TRUE(hdmi1Viewport);
1227 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1228 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1229 ASSERT_EQ(type, hdmi1Viewport->type);
1230
1231 // Check that we cannot find a port with "HDMI2", because we never added one
1232 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1233 ASSERT_FALSE(hdmi2Viewport);
1234}
1235
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236// --- InputReaderTest ---
1237
1238class InputReaderTest : public testing::Test {
1239protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001240 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001242 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 sp<InstrumentedInputReader> mReader;
1244
1245 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001246 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001248 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249
1250 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1251 }
1252
1253 virtual void TearDown() {
1254 mReader.clear();
1255
1256 mFakeListener.clear();
1257 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258 }
1259
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001260 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 const PropertyMap* configuration) {
1262 mFakeEventHub->addDevice(deviceId, name, classes);
1263
1264 if (configuration) {
1265 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1266 }
1267 mFakeEventHub->finishDeviceScan();
1268 mReader->loopOnce();
1269 mReader->loopOnce();
1270 mFakeEventHub->assertQueueIsEmpty();
1271 }
1272
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001273 void disableDevice(int32_t deviceId, InputDevice* device) {
1274 mFakePolicy->addDisabledDevice(deviceId);
1275 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1276 }
1277
1278 void enableDevice(int32_t deviceId, InputDevice* device) {
1279 mFakePolicy->removeDisabledDevice(deviceId);
1280 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1281 }
1282
1283 void configureDevice(uint32_t changes, InputDevice* device) {
1284 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1285 }
1286
Michael Wrightd02c5b62014-02-10 15:10:22 -08001287 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001288 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001289 const PropertyMap* configuration) {
1290 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1291 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1292 device->addMapper(mapper);
1293 mReader->setNextDevice(device);
1294 addDevice(deviceId, name, classes, configuration);
1295 return mapper;
1296 }
1297};
1298
1299TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001300 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001301 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001302 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001303 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001304
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001305
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001306 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001307 mReader->getInputDevices(inputDevices);
1308
1309 ASSERT_EQ(1U, inputDevices.size());
1310 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001311 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1313 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1314 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1315
1316 // Should also have received a notification describing the new input devices.
1317 inputDevices = mFakePolicy->getInputDevices();
1318 ASSERT_EQ(1U, inputDevices.size());
1319 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001320 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1322 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1323 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1324}
1325
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001326TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1327 constexpr int32_t deviceId = 1;
1328 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001329 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001330 // Must add at least one mapper or the device will be ignored!
1331 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1332 device->addMapper(mapper);
1333 mReader->setNextDevice(device);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001334 addDevice(deviceId, "fake", deviceClass, nullptr);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001335
Yi Kong9b14ac62018-07-17 13:48:38 -07001336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001337
1338 NotifyDeviceResetArgs resetArgs;
1339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1340 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1341 ASSERT_EQ(deviceId, resetArgs.deviceId);
1342
1343 ASSERT_EQ(device->isEnabled(), true);
1344 disableDevice(deviceId, device);
1345 mReader->loopOnce();
1346
1347 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1348 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1349 ASSERT_EQ(deviceId, resetArgs.deviceId);
1350 ASSERT_EQ(device->isEnabled(), false);
1351
1352 disableDevice(deviceId, device);
1353 mReader->loopOnce();
1354 mFakeListener->assertNotifyDeviceResetWasNotCalled();
1355 mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1356 ASSERT_EQ(device->isEnabled(), false);
1357
1358 enableDevice(deviceId, device);
1359 mReader->loopOnce();
1360 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1361 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1362 ASSERT_EQ(deviceId, resetArgs.deviceId);
1363 ASSERT_EQ(device->isEnabled(), true);
1364}
1365
Michael Wrightd02c5b62014-02-10 15:10:22 -08001366TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001367 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001368 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001369 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1371
1372 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1373 AINPUT_SOURCE_ANY, AKEYCODE_A))
1374 << "Should return unknown when the device id is >= 0 but unknown.";
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 valid but the sources are not supported by the 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 valid and the device supports some of the sources.";
1383
1384 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1385 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1386 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1387
1388 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1389 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1390 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1391}
1392
1393TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001394 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001395 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001396 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1398
1399 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1400 AINPUT_SOURCE_ANY, KEY_A))
1401 << "Should return unknown when the device id is >= 0 but unknown.";
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 valid but the sources are not supported by the 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 valid and the device supports some of the sources.";
1410
1411 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1412 AINPUT_SOURCE_TRACKBALL, KEY_A))
1413 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1414
1415 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1416 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1417 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1418}
1419
1420TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001421 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001422 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001423 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001424 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1425
1426 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1427 AINPUT_SOURCE_ANY, SW_LID))
1428 << "Should return unknown when the device id is >= 0 but unknown.";
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 valid but the sources are not supported by the 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 valid and the device supports some of the sources.";
1437
1438 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1439 AINPUT_SOURCE_TRACKBALL, SW_LID))
1440 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1441
1442 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1443 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1444 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1445}
1446
1447TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001448 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001449 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001450 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001451
Michael Wrightd02c5b62014-02-10 15:10:22 -08001452 mapper->addSupportedKeyCode(AKEYCODE_A);
1453 mapper->addSupportedKeyCode(AKEYCODE_B);
1454
1455 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1456 uint8_t flags[4] = { 0, 0, 0, 1 };
1457
1458 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1459 << "Should return false when device id is >= 0 but unknown.";
1460 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1461
1462 flags[3] = 1;
1463 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1464 << "Should return false when device id is valid but the sources are not supported by the device.";
1465 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1466
1467 flags[3] = 1;
1468 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1469 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1470 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1471
1472 flags[3] = 1;
1473 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1474 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1475 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1476
1477 flags[3] = 1;
1478 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1479 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1480 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1481}
1482
1483TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001484 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001485
1486 NotifyConfigurationChangedArgs args;
1487
1488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1489 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1490}
1491
1492TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001493 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001494 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001495 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001496
1497 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1498 mReader->loopOnce();
1499 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1500
1501 RawEvent event;
1502 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1503 ASSERT_EQ(0, event.when);
1504 ASSERT_EQ(1, event.deviceId);
1505 ASSERT_EQ(EV_KEY, event.type);
1506 ASSERT_EQ(KEY_A, event.code);
1507 ASSERT_EQ(1, event.value);
1508}
1509
Prabir Pradhan42611e02018-11-27 14:04:02 -08001510TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1511 constexpr int32_t deviceId = 1;
1512 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001513 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001514 // Must add at least one mapper or the device will be ignored!
1515 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1516 device->addMapper(mapper);
1517 mReader->setNextDevice(device);
1518 addDevice(deviceId, "fake", deviceClass, nullptr);
1519
1520 NotifyDeviceResetArgs resetArgs;
1521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1522 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1523
1524 disableDevice(deviceId, device);
1525 mReader->loopOnce();
1526 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1527 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1528 prevSequenceNum = resetArgs.sequenceNum;
1529
1530 enableDevice(deviceId, device);
1531 mReader->loopOnce();
1532 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1533 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1534 prevSequenceNum = resetArgs.sequenceNum;
1535
1536 disableDevice(deviceId, device);
1537 mReader->loopOnce();
1538 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1539 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1540 prevSequenceNum = resetArgs.sequenceNum;
1541}
1542
Arthur Hungc23540e2018-11-29 20:42:11 +08001543TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1544 constexpr int32_t deviceId = 1;
1545 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1546 const char* DEVICE_LOCATION = "USB1";
1547 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1548 DEVICE_LOCATION);
1549 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1550 device->addMapper(mapper);
1551 mReader->setNextDevice(device);
1552 addDevice(deviceId, "fake", deviceClass, nullptr);
1553
1554 const uint8_t hdmi1 = 1;
1555
1556 // Associated touch screen with second display.
1557 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1558
1559 // Add default and second display.
1560 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1561 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1562 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1563 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1564 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1565 mReader->loopOnce();
1566
Arthur Hung2c9a3342019-07-23 14:18:59 +08001567 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001568 ASSERT_EQ(deviceId, device->getId());
1569 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1570 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001571
1572 // Can't dispatch event from a disabled device.
1573 disableDevice(deviceId, device);
1574 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001575}
1576
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577
1578// --- InputDeviceTest ---
1579
1580class InputDeviceTest : public testing::Test {
1581protected:
1582 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001583 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584 static const int32_t DEVICE_ID;
1585 static const int32_t DEVICE_GENERATION;
1586 static const int32_t DEVICE_CONTROLLER_NUMBER;
1587 static const uint32_t DEVICE_CLASSES;
1588
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001589 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001591 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 FakeInputReaderContext* mFakeContext;
1593
1594 InputDevice* mDevice;
1595
1596 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001597 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001599 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1601
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001602 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603 InputDeviceIdentifier identifier;
1604 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001605 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1607 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1608 }
1609
1610 virtual void TearDown() {
1611 delete mDevice;
1612
1613 delete mFakeContext;
1614 mFakeListener.clear();
1615 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616 }
1617};
1618
1619const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001620const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621const int32_t InputDeviceTest::DEVICE_ID = 1;
1622const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1623const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1624const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1625 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1626
1627TEST_F(InputDeviceTest, ImmutableProperties) {
1628 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001629 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1631}
1632
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001633TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1634 ASSERT_EQ(mDevice->isEnabled(), true);
1635}
1636
Michael Wrightd02c5b62014-02-10 15:10:22 -08001637TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1638 // Configuration.
1639 InputReaderConfiguration config;
1640 mDevice->configure(ARBITRARY_TIME, &config, 0);
1641
1642 // Reset.
1643 mDevice->reset(ARBITRARY_TIME);
1644
1645 NotifyDeviceResetArgs resetArgs;
1646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1647 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1648 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1649
1650 // Metadata.
1651 ASSERT_TRUE(mDevice->isIgnored());
1652 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1653
1654 InputDeviceInfo info;
1655 mDevice->getDeviceInfo(&info);
1656 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001657 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1659 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1660
1661 // State queries.
1662 ASSERT_EQ(0, mDevice->getMetaState());
1663
1664 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1665 << "Ignored device should return unknown key code state.";
1666 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1667 << "Ignored device should return unknown scan code state.";
1668 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1669 << "Ignored device should return unknown switch state.";
1670
1671 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1672 uint8_t flags[2] = { 0, 1 };
1673 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1674 << "Ignored device should never mark any key codes.";
1675 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1676 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1677}
1678
1679TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1680 // Configuration.
1681 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1682
1683 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1684 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1685 mapper1->setMetaState(AMETA_ALT_ON);
1686 mapper1->addSupportedKeyCode(AKEYCODE_A);
1687 mapper1->addSupportedKeyCode(AKEYCODE_B);
1688 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1689 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1690 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1691 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1692 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1693 mDevice->addMapper(mapper1);
1694
1695 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1696 mapper2->setMetaState(AMETA_SHIFT_ON);
1697 mDevice->addMapper(mapper2);
1698
1699 InputReaderConfiguration config;
1700 mDevice->configure(ARBITRARY_TIME, &config, 0);
1701
1702 String8 propertyValue;
1703 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1704 << "Device should have read configuration during configuration phase.";
1705 ASSERT_STREQ("value", propertyValue.string());
1706
1707 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1708 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1709
1710 // Reset
1711 mDevice->reset(ARBITRARY_TIME);
1712 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1713 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1714
1715 NotifyDeviceResetArgs resetArgs;
1716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1717 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1718 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1719
1720 // Metadata.
1721 ASSERT_FALSE(mDevice->isIgnored());
1722 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1723
1724 InputDeviceInfo info;
1725 mDevice->getDeviceInfo(&info);
1726 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001727 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001728 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1729 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1730
1731 // State queries.
1732 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1733 << "Should query mappers and combine meta states.";
1734
1735 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1736 << "Should return unknown key code state when source not supported.";
1737 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1738 << "Should return unknown scan code state when source not supported.";
1739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1740 << "Should return unknown switch state when source not supported.";
1741
1742 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1743 << "Should query mapper when source is supported.";
1744 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1745 << "Should query mapper when source is supported.";
1746 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1747 << "Should query mapper when source is supported.";
1748
1749 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1750 uint8_t flags[4] = { 0, 0, 0, 1 };
1751 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1752 << "Should do nothing when source is unsupported.";
1753 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1754 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1755 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1756 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1757
1758 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1759 << "Should query mapper when source is supported.";
1760 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1761 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1762 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1763 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1764
1765 // Event handling.
1766 RawEvent event;
1767 mDevice->process(&event, 1);
1768
1769 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1770 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1771}
1772
Arthur Hung2c9a3342019-07-23 14:18:59 +08001773// A single input device is associated with a specific display. Check that:
1774// 1. Device is disabled if the viewport corresponding to the associated display is not found
1775// 2. Device is disabled when setEnabled API is called
1776TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
1777 FakeInputMapper* mapper = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1778 mDevice->addMapper(mapper);
1779
1780 // First Configuration.
1781 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1782
1783 // Device should be enabled by default.
1784 ASSERT_TRUE(mDevice->isEnabled());
1785
1786 // Prepare associated info.
1787 constexpr uint8_t hdmi = 1;
1788 const std::string UNIQUE_ID = "local:1";
1789
1790 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1791 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1792 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1793 // Device should be disabled because it is associated with a specific display via
1794 // input port <-> display port association, but the corresponding display is not found
1795 ASSERT_FALSE(mDevice->isEnabled());
1796
1797 // Prepare displays.
1798 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1799 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1800 ViewportType::VIEWPORT_INTERNAL);
1801 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1802 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1803 ASSERT_TRUE(mDevice->isEnabled());
1804
1805 // Device should be disabled after set disable.
1806 mFakePolicy->addDisabledDevice(mDevice->getId());
1807 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1808 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1809 ASSERT_FALSE(mDevice->isEnabled());
1810
1811 // Device should still be disabled even found the associated display.
1812 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1813 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1814 ASSERT_FALSE(mDevice->isEnabled());
1815}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001816
1817// --- InputMapperTest ---
1818
1819class InputMapperTest : public testing::Test {
1820protected:
1821 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001822 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001823 static const int32_t DEVICE_ID;
1824 static const int32_t DEVICE_GENERATION;
1825 static const int32_t DEVICE_CONTROLLER_NUMBER;
1826 static const uint32_t DEVICE_CLASSES;
1827
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001828 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001829 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001830 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831 FakeInputReaderContext* mFakeContext;
1832 InputDevice* mDevice;
1833
1834 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001835 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001836 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001837 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001838 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1839 InputDeviceIdentifier identifier;
1840 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001841 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1843 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1844
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001845 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001846 }
1847
1848 virtual void TearDown() {
1849 delete mDevice;
1850 delete mFakeContext;
1851 mFakeListener.clear();
1852 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001853 }
1854
1855 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001856 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857 }
1858
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001859 void configureDevice(uint32_t changes) {
1860 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1861 }
1862
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863 void addMapperAndConfigure(InputMapper* mapper) {
1864 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001865 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866 mDevice->reset(ARBITRARY_TIME);
1867 }
1868
1869 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001870 int32_t orientation, const std::string& uniqueId,
1871 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001872 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001873 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001874 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1875 }
1876
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001877 void clearViewports() {
1878 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001879 }
1880
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001881 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001882 int32_t code, int32_t value) {
1883 RawEvent event;
1884 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001885 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886 event.type = type;
1887 event.code = code;
1888 event.value = value;
1889 mapper->process(&event);
1890 }
1891
1892 static void assertMotionRange(const InputDeviceInfo& info,
1893 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1894 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001895 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1897 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1898 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1899 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1900 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1901 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1902 }
1903
1904 static void assertPointerCoords(const PointerCoords& coords,
1905 float x, float y, float pressure, float size,
1906 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1907 float orientation, float distance) {
1908 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1909 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1910 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1911 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1912 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1913 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1914 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1915 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1916 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1917 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1918 }
1919
1920 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1921 float actualX, actualY;
1922 controller->getPosition(&actualX, &actualY);
1923 ASSERT_NEAR(x, actualX, 1);
1924 ASSERT_NEAR(y, actualY, 1);
1925 }
1926};
1927
1928const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001929const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930const int32_t InputMapperTest::DEVICE_ID = 1;
1931const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1932const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1933const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1934
1935
1936// --- SwitchInputMapperTest ---
1937
1938class SwitchInputMapperTest : public InputMapperTest {
1939protected:
1940};
1941
1942TEST_F(SwitchInputMapperTest, GetSources) {
1943 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1944 addMapperAndConfigure(mapper);
1945
1946 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1947}
1948
1949TEST_F(SwitchInputMapperTest, GetSwitchState) {
1950 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1951 addMapperAndConfigure(mapper);
1952
1953 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1954 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1955
1956 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1957 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1958}
1959
1960TEST_F(SwitchInputMapperTest, Process) {
1961 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1962 addMapperAndConfigure(mapper);
1963
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001964 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
1965 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1966 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
1967 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001968
1969 NotifySwitchArgs args;
1970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1971 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08001972 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1973 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001974 args.switchMask);
1975 ASSERT_EQ(uint32_t(0), args.policyFlags);
1976}
1977
1978
1979// --- KeyboardInputMapperTest ---
1980
1981class KeyboardInputMapperTest : public InputMapperTest {
1982protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001983 const std::string UNIQUE_ID = "local:0";
1984
1985 void prepareDisplay(int32_t orientation);
1986
Arthur Hung2c9a3342019-07-23 14:18:59 +08001987 void testDPadKeyRotation(KeyboardInputMapper* mapper, int32_t originalScanCode,
1988 int32_t originalKeyCode, int32_t rotatedKeyCode,
1989 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001990};
1991
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001992/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
1993 * orientation.
1994 */
1995void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
1996 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001997 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001998}
1999
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002001 int32_t originalScanCode, int32_t originalKeyCode,
2002 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002003 NotifyKeyArgs args;
2004
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002005 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2007 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2008 ASSERT_EQ(originalScanCode, args.scanCode);
2009 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002010 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002011
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002012 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2014 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2015 ASSERT_EQ(originalScanCode, args.scanCode);
2016 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002017 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002018}
2019
Michael Wrightd02c5b62014-02-10 15:10:22 -08002020TEST_F(KeyboardInputMapperTest, GetSources) {
2021 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2022 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2023 addMapperAndConfigure(mapper);
2024
2025 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
2026}
2027
2028TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2029 const int32_t USAGE_A = 0x070004;
2030 const int32_t USAGE_UNKNOWN = 0x07ffff;
2031 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2032 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2033
2034 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2035 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2036 addMapperAndConfigure(mapper);
2037
2038 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002039 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002040 NotifyKeyArgs args;
2041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2042 ASSERT_EQ(DEVICE_ID, args.deviceId);
2043 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2044 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2045 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2046 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2047 ASSERT_EQ(KEY_HOME, args.scanCode);
2048 ASSERT_EQ(AMETA_NONE, args.metaState);
2049 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2050 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2051 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2052
2053 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002054 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2056 ASSERT_EQ(DEVICE_ID, args.deviceId);
2057 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2058 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2059 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2060 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2061 ASSERT_EQ(KEY_HOME, args.scanCode);
2062 ASSERT_EQ(AMETA_NONE, args.metaState);
2063 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2064 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2065 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2066
2067 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002068 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2069 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2071 ASSERT_EQ(DEVICE_ID, args.deviceId);
2072 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2073 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2074 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2075 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2076 ASSERT_EQ(0, args.scanCode);
2077 ASSERT_EQ(AMETA_NONE, args.metaState);
2078 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2079 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2080 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2081
2082 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002083 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2084 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2086 ASSERT_EQ(DEVICE_ID, args.deviceId);
2087 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2088 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2089 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2090 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2091 ASSERT_EQ(0, args.scanCode);
2092 ASSERT_EQ(AMETA_NONE, args.metaState);
2093 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2094 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2095 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2096
2097 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002098 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2099 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2101 ASSERT_EQ(DEVICE_ID, args.deviceId);
2102 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2103 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2104 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2105 ASSERT_EQ(0, args.keyCode);
2106 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2107 ASSERT_EQ(AMETA_NONE, args.metaState);
2108 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2109 ASSERT_EQ(0U, args.policyFlags);
2110 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2111
2112 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002113 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2114 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2116 ASSERT_EQ(DEVICE_ID, args.deviceId);
2117 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2118 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2119 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2120 ASSERT_EQ(0, args.keyCode);
2121 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2122 ASSERT_EQ(AMETA_NONE, args.metaState);
2123 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2124 ASSERT_EQ(0U, args.policyFlags);
2125 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2126}
2127
2128TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2129 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2130 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2131
2132 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2133 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2134 addMapperAndConfigure(mapper);
2135
2136 // Initial metastate.
2137 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2138
2139 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002140 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002141 NotifyKeyArgs args;
2142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2144 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2145 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2146
2147 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002148 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2150 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2151 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2152
2153 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002154 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2156 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2157 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2158
2159 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002160 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2162 ASSERT_EQ(AMETA_NONE, args.metaState);
2163 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2164 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2165}
2166
2167TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2168 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2169 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2170 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2171 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2172
2173 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2174 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2175 addMapperAndConfigure(mapper);
2176
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002177 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002178 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2179 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2180 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2181 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2182 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2183 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2184 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2185 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2186}
2187
2188TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2189 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2190 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2191 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2192 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2193
2194 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2195 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2196 addConfigurationProperty("keyboard.orientationAware", "1");
2197 addMapperAndConfigure(mapper);
2198
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002199 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002200 ASSERT_NO_FATAL_FAILURE(
2201 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2202 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2203 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2204 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2205 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2206 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2207 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002208
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002209 clearViewports();
2210 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002211 ASSERT_NO_FATAL_FAILURE(
2212 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2213 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2214 AKEYCODE_DPAD_UP, DISPLAY_ID));
2215 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2216 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2217 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2218 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002219
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002220 clearViewports();
2221 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002222 ASSERT_NO_FATAL_FAILURE(
2223 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2224 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2225 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2226 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2227 AKEYCODE_DPAD_UP, DISPLAY_ID));
2228 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2229 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002230
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002231 clearViewports();
2232 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002233 ASSERT_NO_FATAL_FAILURE(
2234 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2235 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2236 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2237 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2238 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2239 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2240 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002241
2242 // Special case: if orientation changes while key is down, we still emit the same keycode
2243 // in the key up as we did in the key down.
2244 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002245 clearViewports();
2246 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002247 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2249 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2250 ASSERT_EQ(KEY_UP, args.scanCode);
2251 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2252
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002253 clearViewports();
2254 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002255 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2257 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2258 ASSERT_EQ(KEY_UP, args.scanCode);
2259 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2260}
2261
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002262TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2263 // If the keyboard is not orientation aware,
2264 // key events should not be associated with a specific display id
2265 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2266
2267 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2268 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2269 addMapperAndConfigure(mapper);
2270 NotifyKeyArgs args;
2271
2272 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002273 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002275 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2277 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2278
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002279 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002280 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002282 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2284 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2285}
2286
2287TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2288 // If the keyboard is orientation aware,
2289 // key events should be associated with the internal viewport
2290 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2291
2292 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2293 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2294 addConfigurationProperty("keyboard.orientationAware", "1");
2295 addMapperAndConfigure(mapper);
2296 NotifyKeyArgs args;
2297
2298 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2299 // ^--- already checked by the previous test
2300
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002301 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002302 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002303 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002305 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2307 ASSERT_EQ(DISPLAY_ID, args.displayId);
2308
2309 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002310 clearViewports();
2311 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002312 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002313 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002315 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2317 ASSERT_EQ(newDisplayId, args.displayId);
2318}
2319
Michael Wrightd02c5b62014-02-10 15:10:22 -08002320TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2321 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2322 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2323 addMapperAndConfigure(mapper);
2324
2325 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2326 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2327
2328 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2329 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2330}
2331
2332TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2333 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2334 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2335 addMapperAndConfigure(mapper);
2336
2337 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2338 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2339
2340 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2341 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2342}
2343
2344TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2345 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2346 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2347 addMapperAndConfigure(mapper);
2348
2349 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2350
2351 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2352 uint8_t flags[2] = { 0, 0 };
2353 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2354 ASSERT_TRUE(flags[0]);
2355 ASSERT_FALSE(flags[1]);
2356}
2357
2358TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2359 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2360 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2361 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2362 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2363 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2364 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2365
2366 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2367 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2368 addMapperAndConfigure(mapper);
2369
2370 // Initialization should have turned all of the lights off.
2371 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2372 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2373 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2374
2375 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002376 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2377 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2379 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2380 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2381 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2382
2383 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002384 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2385 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2387 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2388 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2389 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2390
2391 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002392 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2393 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2395 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2396 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2397 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2398
2399 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002400 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2401 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2403 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2404 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2405 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2406
2407 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002408 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2409 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2411 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2412 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2413 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2414
2415 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002416 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2417 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2419 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2420 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2421 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2422}
2423
Arthur Hung2c9a3342019-07-23 14:18:59 +08002424TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2425 // keyboard 1.
2426 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2427 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2428 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2429 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2430
2431 // keyboard 2.
2432 const std::string USB2 = "USB2";
2433 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2434 InputDeviceIdentifier identifier;
2435 identifier.name = "KEYBOARD2";
2436 identifier.location = USB2;
2437 std::unique_ptr<InputDevice> device2 =
2438 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2439 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2440 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2441 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2442 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2443 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2444 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2445
2446 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2447 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2448 addMapperAndConfigure(mapper);
2449
2450 KeyboardInputMapper* mapper2 = new KeyboardInputMapper(device2.get(), AINPUT_SOURCE_KEYBOARD,
2451 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2452 device2->addMapper(mapper2);
2453 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2454 device2->reset(ARBITRARY_TIME);
2455
2456 // Prepared displays and associated info.
2457 constexpr uint8_t hdmi1 = 0;
2458 constexpr uint8_t hdmi2 = 1;
2459 const std::string SECONDARY_UNIQUE_ID = "local:1";
2460
2461 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2462 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2463
2464 // No associated display viewport found, should disable the device.
2465 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2466 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2467 ASSERT_FALSE(device2->isEnabled());
2468
2469 // Prepare second display.
2470 constexpr int32_t newDisplayId = 2;
2471 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2472 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2473 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2474 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2475 // Default device will reconfigure above, need additional reconfiguration for another device.
2476 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2477 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2478
2479 // Device should be enabled after the associated display is found.
2480 ASSERT_TRUE(mDevice->isEnabled());
2481 ASSERT_TRUE(device2->isEnabled());
2482
2483 // Test pad key events
2484 ASSERT_NO_FATAL_FAILURE(
2485 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2486 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2487 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2488 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2489 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2490 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2491 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2492
2493 ASSERT_NO_FATAL_FAILURE(
2494 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2495 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2496 AKEYCODE_DPAD_RIGHT, newDisplayId));
2497 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2498 AKEYCODE_DPAD_DOWN, newDisplayId));
2499 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2500 AKEYCODE_DPAD_LEFT, newDisplayId));
2501}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502
2503// --- CursorInputMapperTest ---
2504
2505class CursorInputMapperTest : public InputMapperTest {
2506protected:
2507 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2508
2509 sp<FakePointerController> mFakePointerController;
2510
2511 virtual void SetUp() {
2512 InputMapperTest::SetUp();
2513
2514 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002515 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516 }
2517
2518 void testMotionRotation(CursorInputMapper* mapper,
2519 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002520
2521 void prepareDisplay(int32_t orientation) {
2522 const std::string uniqueId = "local:0";
2523 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2524 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2525 orientation, uniqueId, NO_PORT, viewportType);
2526 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002527};
2528
2529const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2530
2531void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2532 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2533 NotifyMotionArgs args;
2534
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002535 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2536 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2537 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2541 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2542 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2543 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2544}
2545
2546TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2547 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2548 addConfigurationProperty("cursor.mode", "pointer");
2549 addMapperAndConfigure(mapper);
2550
2551 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2552}
2553
2554TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2555 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2556 addConfigurationProperty("cursor.mode", "navigation");
2557 addMapperAndConfigure(mapper);
2558
2559 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2560}
2561
2562TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2563 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2564 addConfigurationProperty("cursor.mode", "pointer");
2565 addMapperAndConfigure(mapper);
2566
2567 InputDeviceInfo info;
2568 mapper->populateDeviceInfo(&info);
2569
2570 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002571 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2572 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2574 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2575
2576 // When the bounds are set, then there should be a valid motion range.
2577 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2578
2579 InputDeviceInfo info2;
2580 mapper->populateDeviceInfo(&info2);
2581
2582 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2583 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2584 1, 800 - 1, 0.0f, 0.0f));
2585 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2586 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2587 2, 480 - 1, 0.0f, 0.0f));
2588 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2589 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2590 0.0f, 1.0f, 0.0f, 0.0f));
2591}
2592
2593TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2594 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2595 addConfigurationProperty("cursor.mode", "navigation");
2596 addMapperAndConfigure(mapper);
2597
2598 InputDeviceInfo info;
2599 mapper->populateDeviceInfo(&info);
2600
2601 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2602 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2603 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2604 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2605 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2606 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2607 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2608 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2609 0.0f, 1.0f, 0.0f, 0.0f));
2610}
2611
2612TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2613 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2614 addConfigurationProperty("cursor.mode", "navigation");
2615 addMapperAndConfigure(mapper);
2616
2617 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2618
2619 NotifyMotionArgs args;
2620
2621 // Button press.
2622 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002623 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2624 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2626 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2627 ASSERT_EQ(DEVICE_ID, args.deviceId);
2628 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2629 ASSERT_EQ(uint32_t(0), args.policyFlags);
2630 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2631 ASSERT_EQ(0, args.flags);
2632 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2633 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2634 ASSERT_EQ(0, args.edgeFlags);
2635 ASSERT_EQ(uint32_t(1), args.pointerCount);
2636 ASSERT_EQ(0, args.pointerProperties[0].id);
2637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2639 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2640 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2641 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2642 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2643
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2645 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2646 ASSERT_EQ(DEVICE_ID, args.deviceId);
2647 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2648 ASSERT_EQ(uint32_t(0), args.policyFlags);
2649 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2650 ASSERT_EQ(0, args.flags);
2651 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2652 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2653 ASSERT_EQ(0, args.edgeFlags);
2654 ASSERT_EQ(uint32_t(1), args.pointerCount);
2655 ASSERT_EQ(0, args.pointerProperties[0].id);
2656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2658 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2659 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2660 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2661 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2662
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002664 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2665 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2667 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2668 ASSERT_EQ(DEVICE_ID, args.deviceId);
2669 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2670 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2672 ASSERT_EQ(0, args.flags);
2673 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2674 ASSERT_EQ(0, args.buttonState);
2675 ASSERT_EQ(0, args.edgeFlags);
2676 ASSERT_EQ(uint32_t(1), args.pointerCount);
2677 ASSERT_EQ(0, args.pointerProperties[0].id);
2678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2680 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2681 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2682 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2683 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2684
2685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2686 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2687 ASSERT_EQ(DEVICE_ID, args.deviceId);
2688 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2689 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2691 ASSERT_EQ(0, args.flags);
2692 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2693 ASSERT_EQ(0, args.buttonState);
2694 ASSERT_EQ(0, args.edgeFlags);
2695 ASSERT_EQ(uint32_t(1), args.pointerCount);
2696 ASSERT_EQ(0, args.pointerProperties[0].id);
2697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2699 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2700 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2701 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2702 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2703}
2704
2705TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2706 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2707 addConfigurationProperty("cursor.mode", "navigation");
2708 addMapperAndConfigure(mapper);
2709
2710 NotifyMotionArgs args;
2711
2712 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002713 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2714 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2718 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2719
2720 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002721 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2722 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2726 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2727}
2728
2729TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2730 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2731 addConfigurationProperty("cursor.mode", "navigation");
2732 addMapperAndConfigure(mapper);
2733
2734 NotifyMotionArgs args;
2735
2736 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002737 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2738 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2740 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2742 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2743
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2745 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2747 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2748
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002750 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2751 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2755 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2756
2757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2760 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2761}
2762
2763TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2764 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2765 addConfigurationProperty("cursor.mode", "navigation");
2766 addMapperAndConfigure(mapper);
2767
2768 NotifyMotionArgs args;
2769
2770 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002771 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2772 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2773 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2774 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2776 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2778 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2779 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2780
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2782 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2784 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2785 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2786
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002788 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2789 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 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(&args));
2792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2794 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2795 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2796
2797 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002798 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2799 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2803 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2804
2805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2808 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2809}
2810
2811TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2812 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2813 addConfigurationProperty("cursor.mode", "navigation");
2814 addMapperAndConfigure(mapper);
2815
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002816 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2818 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2819 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2820 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2821 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2822 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2823 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2824 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2825}
2826
2827TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2828 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2829 addConfigurationProperty("cursor.mode", "navigation");
2830 addConfigurationProperty("cursor.orientationAware", "1");
2831 addMapperAndConfigure(mapper);
2832
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002833 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2835 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2836 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2837 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2838 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2839 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2840 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2841 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2842
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002843 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2845 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2846 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2847 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2848 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2849 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2850 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2851 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2852
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002853 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2855 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2856 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2857 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2858 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2859 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2860 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2861 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2862
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002863 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2865 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2866 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2867 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2868 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2869 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2870 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2871 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2872}
2873
2874TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2875 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2876 addConfigurationProperty("cursor.mode", "pointer");
2877 addMapperAndConfigure(mapper);
2878
2879 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2880 mFakePointerController->setPosition(100, 200);
2881 mFakePointerController->setButtonState(0);
2882
2883 NotifyMotionArgs motionArgs;
2884 NotifyKeyArgs keyArgs;
2885
2886 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002887 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2888 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2890 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2891 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2892 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2894 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2895
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2898 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2899 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2901 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2902
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002903 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2904 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907 ASSERT_EQ(0, motionArgs.buttonState);
2908 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2910 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2911
2912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002913 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002914 ASSERT_EQ(0, motionArgs.buttonState);
2915 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2917 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2918
2919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002921 ASSERT_EQ(0, motionArgs.buttonState);
2922 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2924 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2925
2926 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002927 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2928 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2929 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2931 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2932 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2933 motionArgs.buttonState);
2934 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2935 mFakePointerController->getButtonState());
2936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2937 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2938
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2941 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2942 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2943 mFakePointerController->getButtonState());
2944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2945 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2946
2947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2948 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2949 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2950 motionArgs.buttonState);
2951 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2952 mFakePointerController->getButtonState());
2953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2954 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2955
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002956 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
2957 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2961 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2963 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2964
2965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002967 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2968 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2970 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2971
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002972 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2973 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002975 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2976 ASSERT_EQ(0, motionArgs.buttonState);
2977 ASSERT_EQ(0, mFakePointerController->getButtonState());
2978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2979 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002980 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2981 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002982
2983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002984 ASSERT_EQ(0, motionArgs.buttonState);
2985 ASSERT_EQ(0, mFakePointerController->getButtonState());
2986 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2988 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 -08002989
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2991 ASSERT_EQ(0, motionArgs.buttonState);
2992 ASSERT_EQ(0, mFakePointerController->getButtonState());
2993 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2995 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2996
2997 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002998 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
2999 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3001 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3002 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003003
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_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3007 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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));
3010
3011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3013 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3014 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 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
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003018 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3019 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 ASSERT_EQ(0, motionArgs.buttonState);
3023 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3025 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3026
3027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003029 ASSERT_EQ(0, motionArgs.buttonState);
3030 ASSERT_EQ(0, mFakePointerController->getButtonState());
3031
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3033 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3035 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3036 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3037
3038 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003039 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3040 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3042 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3043 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003044
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003046 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3048 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3050 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3051
3052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3054 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3055 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3057 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3058
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003059 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3060 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003062 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 ASSERT_EQ(0, motionArgs.buttonState);
3064 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3066 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 -08003067
3068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3069 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3070 ASSERT_EQ(0, motionArgs.buttonState);
3071 ASSERT_EQ(0, mFakePointerController->getButtonState());
3072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3073 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3074
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3076 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3077 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3078
3079 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003080 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3081 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3083 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3084 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003085
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003087 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3089 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3091 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3092
3093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3095 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3096 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3098 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3099
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003100 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3101 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104 ASSERT_EQ(0, motionArgs.buttonState);
3105 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3107 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 -08003108
3109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3110 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3111 ASSERT_EQ(0, motionArgs.buttonState);
3112 ASSERT_EQ(0, mFakePointerController->getButtonState());
3113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3114 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3115
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3117 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3118 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3119
3120 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003121 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3122 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3124 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3125 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003126
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003128 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3130 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3132 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3133
3134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3136 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3137 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3139 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3140
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003141 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3142 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003144 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145 ASSERT_EQ(0, motionArgs.buttonState);
3146 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3148 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 -08003149
3150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3151 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3152 ASSERT_EQ(0, motionArgs.buttonState);
3153 ASSERT_EQ(0, mFakePointerController->getButtonState());
3154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3155 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3156
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3158 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3159 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3160}
3161
3162TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3163 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3164 addConfigurationProperty("cursor.mode", "pointer");
3165 addMapperAndConfigure(mapper);
3166
3167 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3168 mFakePointerController->setPosition(100, 200);
3169 mFakePointerController->setButtonState(0);
3170
3171 NotifyMotionArgs args;
3172
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003173 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3174 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3175 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003177 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3180 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3181 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3182}
3183
3184TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3185 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3186 addConfigurationProperty("cursor.mode", "pointer");
3187 mFakePolicy->setPointerCapture(true);
3188 addMapperAndConfigure(mapper);
3189
3190 NotifyDeviceResetArgs resetArgs;
3191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3192 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3193 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3194
3195 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3196 mFakePointerController->setPosition(100, 200);
3197 mFakePointerController->setButtonState(0);
3198
3199 NotifyMotionArgs args;
3200
3201 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003202 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3203 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3204 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3206 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3209 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3210 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3211
3212 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003213 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3214 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3216 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3217 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3219 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3221 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3222 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3224 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3225
3226 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003227 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3228 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3230 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3233 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3235 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3236 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3238 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3239
3240 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003241 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3242 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3243 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3245 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3248 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3249 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3250
3251 // Disable pointer capture and check that the device generation got bumped
3252 // and events are generated the usual way.
3253 const uint32_t generation = mFakeContext->getGeneration();
3254 mFakePolicy->setPointerCapture(false);
3255 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3256 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3257
3258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3259 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3260 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3261
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003262 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3263 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3264 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3266 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3269 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3270 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3271}
3272
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003273TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3274 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3275 addMapperAndConfigure(mapper);
3276
3277 // Setup PointerController for second display.
3278 constexpr int32_t SECOND_DISPLAY_ID = 1;
3279 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3280 mFakePointerController->setPosition(100, 200);
3281 mFakePointerController->setButtonState(0);
3282 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3283
3284 NotifyMotionArgs args;
3285 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3286 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3287 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3289 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3292 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3293 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3294 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3295}
3296
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297
3298// --- TouchInputMapperTest ---
3299
3300class TouchInputMapperTest : public InputMapperTest {
3301protected:
3302 static const int32_t RAW_X_MIN;
3303 static const int32_t RAW_X_MAX;
3304 static const int32_t RAW_Y_MIN;
3305 static const int32_t RAW_Y_MAX;
3306 static const int32_t RAW_TOUCH_MIN;
3307 static const int32_t RAW_TOUCH_MAX;
3308 static const int32_t RAW_TOOL_MIN;
3309 static const int32_t RAW_TOOL_MAX;
3310 static const int32_t RAW_PRESSURE_MIN;
3311 static const int32_t RAW_PRESSURE_MAX;
3312 static const int32_t RAW_ORIENTATION_MIN;
3313 static const int32_t RAW_ORIENTATION_MAX;
3314 static const int32_t RAW_DISTANCE_MIN;
3315 static const int32_t RAW_DISTANCE_MAX;
3316 static const int32_t RAW_TILT_MIN;
3317 static const int32_t RAW_TILT_MAX;
3318 static const int32_t RAW_ID_MIN;
3319 static const int32_t RAW_ID_MAX;
3320 static const int32_t RAW_SLOT_MIN;
3321 static const int32_t RAW_SLOT_MAX;
3322 static const float X_PRECISION;
3323 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003324 static const float X_PRECISION_VIRTUAL;
3325 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326
3327 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003328 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329
3330 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3331
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003332 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003333 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003334
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335 enum Axes {
3336 POSITION = 1 << 0,
3337 TOUCH = 1 << 1,
3338 TOOL = 1 << 2,
3339 PRESSURE = 1 << 3,
3340 ORIENTATION = 1 << 4,
3341 MINOR = 1 << 5,
3342 ID = 1 << 6,
3343 DISTANCE = 1 << 7,
3344 TILT = 1 << 8,
3345 SLOT = 1 << 9,
3346 TOOL_TYPE = 1 << 10,
3347 };
3348
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003349 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3350 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003351 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003353 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 int32_t toRawX(float displayX);
3355 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003356 float toCookedX(float rawX, float rawY);
3357 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003359 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003361 float toDisplayY(int32_t rawY, int32_t displayHeight);
3362
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363};
3364
3365const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3366const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3367const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3368const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3369const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3370const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3371const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3372const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003373const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3374const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3376const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3377const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3378const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3379const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3380const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3381const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3382const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3383const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3384const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3385const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3386const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003387const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3388 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3389const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3390 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003391const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3392 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393
3394const float TouchInputMapperTest::GEOMETRIC_SCALE =
3395 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3396 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3397
3398const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3399 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3400 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3401};
3402
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003403void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003404 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003405 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3406}
3407
3408void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3409 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3410 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411}
3412
Santos Cordonfa5cf462017-04-05 10:37:00 -07003413void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003414 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3415 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003416 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003417}
3418
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419void TouchInputMapperTest::prepareVirtualKeys() {
3420 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3421 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3422 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3423 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3424}
3425
Jason Gerecke489fda82012-09-07 17:19:40 -07003426void TouchInputMapperTest::prepareLocationCalibration() {
3427 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3428}
3429
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430int32_t TouchInputMapperTest::toRawX(float displayX) {
3431 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3432}
3433
3434int32_t TouchInputMapperTest::toRawY(float displayY) {
3435 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3436}
3437
Jason Gerecke489fda82012-09-07 17:19:40 -07003438float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3439 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3440 return rawX;
3441}
3442
3443float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3444 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3445 return rawY;
3446}
3447
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003449 return toDisplayX(rawX, DISPLAY_WIDTH);
3450}
3451
3452float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3453 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454}
3455
3456float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003457 return toDisplayY(rawY, DISPLAY_HEIGHT);
3458}
3459
3460float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3461 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462}
3463
3464
3465// --- SingleTouchInputMapperTest ---
3466
3467class SingleTouchInputMapperTest : public TouchInputMapperTest {
3468protected:
3469 void prepareButtons();
3470 void prepareAxes(int axes);
3471
3472 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3473 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3474 void processUp(SingleTouchInputMapper* mappery);
3475 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3476 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3477 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3478 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3479 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3480 void processSync(SingleTouchInputMapper* mapper);
3481};
3482
3483void SingleTouchInputMapperTest::prepareButtons() {
3484 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3485}
3486
3487void SingleTouchInputMapperTest::prepareAxes(int axes) {
3488 if (axes & POSITION) {
3489 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3490 RAW_X_MIN, RAW_X_MAX, 0, 0);
3491 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3492 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3493 }
3494 if (axes & PRESSURE) {
3495 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3496 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3497 }
3498 if (axes & TOOL) {
3499 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3500 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3501 }
3502 if (axes & DISTANCE) {
3503 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3504 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3505 }
3506 if (axes & TILT) {
3507 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3508 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3509 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3510 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3511 }
3512}
3513
3514void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003515 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3516 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3517 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518}
3519
3520void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003521 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3522 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523}
3524
3525void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003526 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527}
3528
3529void SingleTouchInputMapperTest::processPressure(
3530 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003531 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532}
3533
3534void SingleTouchInputMapperTest::processToolMajor(
3535 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003536 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537}
3538
3539void SingleTouchInputMapperTest::processDistance(
3540 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003541 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542}
3543
3544void SingleTouchInputMapperTest::processTilt(
3545 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003546 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3547 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548}
3549
3550void SingleTouchInputMapperTest::processKey(
3551 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003552 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553}
3554
3555void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003556 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557}
3558
3559
3560TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3561 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3562 prepareButtons();
3563 prepareAxes(POSITION);
3564 addMapperAndConfigure(mapper);
3565
3566 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3567}
3568
3569TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3570 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3571 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3572 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3573 prepareButtons();
3574 prepareAxes(POSITION);
3575 addMapperAndConfigure(mapper);
3576
3577 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3578}
3579
3580TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3581 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3582 prepareButtons();
3583 prepareAxes(POSITION);
3584 addConfigurationProperty("touch.deviceType", "touchPad");
3585 addMapperAndConfigure(mapper);
3586
3587 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3588}
3589
3590TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3591 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3592 prepareButtons();
3593 prepareAxes(POSITION);
3594 addConfigurationProperty("touch.deviceType", "touchScreen");
3595 addMapperAndConfigure(mapper);
3596
3597 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3598}
3599
3600TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3601 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3602 addConfigurationProperty("touch.deviceType", "touchScreen");
3603 prepareDisplay(DISPLAY_ORIENTATION_0);
3604 prepareButtons();
3605 prepareAxes(POSITION);
3606 prepareVirtualKeys();
3607 addMapperAndConfigure(mapper);
3608
3609 // Unknown key.
3610 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3611
3612 // Virtual key is down.
3613 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3614 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3615 processDown(mapper, x, y);
3616 processSync(mapper);
3617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3618
3619 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3620
3621 // Virtual key is up.
3622 processUp(mapper);
3623 processSync(mapper);
3624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3625
3626 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3627}
3628
3629TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3630 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3631 addConfigurationProperty("touch.deviceType", "touchScreen");
3632 prepareDisplay(DISPLAY_ORIENTATION_0);
3633 prepareButtons();
3634 prepareAxes(POSITION);
3635 prepareVirtualKeys();
3636 addMapperAndConfigure(mapper);
3637
3638 // Unknown key.
3639 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3640
3641 // Virtual key is down.
3642 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3643 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3644 processDown(mapper, x, y);
3645 processSync(mapper);
3646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3647
3648 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3649
3650 // Virtual key is up.
3651 processUp(mapper);
3652 processSync(mapper);
3653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3654
3655 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3656}
3657
3658TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3659 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3660 addConfigurationProperty("touch.deviceType", "touchScreen");
3661 prepareDisplay(DISPLAY_ORIENTATION_0);
3662 prepareButtons();
3663 prepareAxes(POSITION);
3664 prepareVirtualKeys();
3665 addMapperAndConfigure(mapper);
3666
3667 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3668 uint8_t flags[2] = { 0, 0 };
3669 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3670 ASSERT_TRUE(flags[0]);
3671 ASSERT_FALSE(flags[1]);
3672}
3673
3674TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3675 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3676 addConfigurationProperty("touch.deviceType", "touchScreen");
3677 prepareDisplay(DISPLAY_ORIENTATION_0);
3678 prepareButtons();
3679 prepareAxes(POSITION);
3680 prepareVirtualKeys();
3681 addMapperAndConfigure(mapper);
3682
3683 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3684
3685 NotifyKeyArgs args;
3686
3687 // Press virtual key.
3688 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3689 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3690 processDown(mapper, x, y);
3691 processSync(mapper);
3692
3693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3694 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3695 ASSERT_EQ(DEVICE_ID, args.deviceId);
3696 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3697 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3698 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3699 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3700 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3701 ASSERT_EQ(KEY_HOME, args.scanCode);
3702 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3703 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3704
3705 // Release virtual key.
3706 processUp(mapper);
3707 processSync(mapper);
3708
3709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3710 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3711 ASSERT_EQ(DEVICE_ID, args.deviceId);
3712 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3713 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3714 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3715 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3716 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3717 ASSERT_EQ(KEY_HOME, args.scanCode);
3718 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3719 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3720
3721 // Should not have sent any motions.
3722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3723}
3724
3725TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3726 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3727 addConfigurationProperty("touch.deviceType", "touchScreen");
3728 prepareDisplay(DISPLAY_ORIENTATION_0);
3729 prepareButtons();
3730 prepareAxes(POSITION);
3731 prepareVirtualKeys();
3732 addMapperAndConfigure(mapper);
3733
3734 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3735
3736 NotifyKeyArgs keyArgs;
3737
3738 // Press virtual key.
3739 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3740 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3741 processDown(mapper, x, y);
3742 processSync(mapper);
3743
3744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3745 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3746 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3747 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3748 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3749 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3750 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3751 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3752 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3753 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3754 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3755
3756 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3757 // into the display area.
3758 y -= 100;
3759 processMove(mapper, x, y);
3760 processSync(mapper);
3761
3762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3763 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3764 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3765 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3766 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3767 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3768 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3769 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3770 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3771 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3772 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3773 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3774
3775 NotifyMotionArgs motionArgs;
3776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3777 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3778 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3779 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3780 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3781 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3782 ASSERT_EQ(0, motionArgs.flags);
3783 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3784 ASSERT_EQ(0, motionArgs.buttonState);
3785 ASSERT_EQ(0, motionArgs.edgeFlags);
3786 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3787 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3790 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3791 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3792 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3793 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3794
3795 // Keep moving out of bounds. Should generate a pointer move.
3796 y -= 50;
3797 processMove(mapper, x, y);
3798 processSync(mapper);
3799
3800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3801 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3802 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3803 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3804 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3806 ASSERT_EQ(0, motionArgs.flags);
3807 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3808 ASSERT_EQ(0, motionArgs.buttonState);
3809 ASSERT_EQ(0, motionArgs.edgeFlags);
3810 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3811 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3814 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3815 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3816 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3817 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3818
3819 // Release out of bounds. Should generate a pointer up.
3820 processUp(mapper);
3821 processSync(mapper);
3822
3823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3824 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3825 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3826 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3827 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3828 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3829 ASSERT_EQ(0, motionArgs.flags);
3830 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3831 ASSERT_EQ(0, motionArgs.buttonState);
3832 ASSERT_EQ(0, motionArgs.edgeFlags);
3833 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3834 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3837 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3838 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3839 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3840 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3841
3842 // Should not have sent any more keys or motions.
3843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3845}
3846
3847TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3848 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3849 addConfigurationProperty("touch.deviceType", "touchScreen");
3850 prepareDisplay(DISPLAY_ORIENTATION_0);
3851 prepareButtons();
3852 prepareAxes(POSITION);
3853 prepareVirtualKeys();
3854 addMapperAndConfigure(mapper);
3855
3856 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3857
3858 NotifyMotionArgs motionArgs;
3859
3860 // Initially go down out of bounds.
3861 int32_t x = -10;
3862 int32_t y = -10;
3863 processDown(mapper, x, y);
3864 processSync(mapper);
3865
3866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3867
3868 // Move into the display area. Should generate a pointer down.
3869 x = 50;
3870 y = 75;
3871 processMove(mapper, x, y);
3872 processSync(mapper);
3873
3874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3875 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3876 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3877 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3878 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3880 ASSERT_EQ(0, motionArgs.flags);
3881 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3882 ASSERT_EQ(0, motionArgs.buttonState);
3883 ASSERT_EQ(0, motionArgs.edgeFlags);
3884 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3885 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3888 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3889 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3890 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3891 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3892
3893 // Release. Should generate a pointer up.
3894 processUp(mapper);
3895 processSync(mapper);
3896
3897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3898 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3899 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3900 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3901 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3902 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3903 ASSERT_EQ(0, motionArgs.flags);
3904 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3905 ASSERT_EQ(0, motionArgs.buttonState);
3906 ASSERT_EQ(0, motionArgs.edgeFlags);
3907 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3908 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3911 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3912 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3913 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3914 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3915
3916 // Should not have sent any more keys or motions.
3917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3919}
3920
Santos Cordonfa5cf462017-04-05 10:37:00 -07003921TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3922 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3923 addConfigurationProperty("touch.deviceType", "touchScreen");
3924 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3925
3926 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3927 prepareButtons();
3928 prepareAxes(POSITION);
3929 prepareVirtualKeys();
3930 addMapperAndConfigure(mapper);
3931
3932 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3933
3934 NotifyMotionArgs motionArgs;
3935
3936 // Down.
3937 int32_t x = 100;
3938 int32_t y = 125;
3939 processDown(mapper, x, y);
3940 processSync(mapper);
3941
3942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3943 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3944 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3945 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3946 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3947 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3948 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3949 ASSERT_EQ(0, motionArgs.flags);
3950 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3951 ASSERT_EQ(0, motionArgs.buttonState);
3952 ASSERT_EQ(0, motionArgs.edgeFlags);
3953 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3954 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3957 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3958 1, 0, 0, 0, 0, 0, 0, 0));
3959 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3960 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3961 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3962
3963 // Move.
3964 x += 50;
3965 y += 75;
3966 processMove(mapper, x, y);
3967 processSync(mapper);
3968
3969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3970 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3971 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3972 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3973 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3974 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3976 ASSERT_EQ(0, motionArgs.flags);
3977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3978 ASSERT_EQ(0, motionArgs.buttonState);
3979 ASSERT_EQ(0, motionArgs.edgeFlags);
3980 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3981 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3984 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3985 1, 0, 0, 0, 0, 0, 0, 0));
3986 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3987 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3988 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3989
3990 // Up.
3991 processUp(mapper);
3992 processSync(mapper);
3993
3994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3995 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3996 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3997 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3998 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3999 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4000 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4001 ASSERT_EQ(0, motionArgs.flags);
4002 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4003 ASSERT_EQ(0, motionArgs.buttonState);
4004 ASSERT_EQ(0, motionArgs.edgeFlags);
4005 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4006 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4009 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4010 1, 0, 0, 0, 0, 0, 0, 0));
4011 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4012 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4013 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4014
4015 // Should not have sent any more keys or motions.
4016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4018}
4019
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4021 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4022 addConfigurationProperty("touch.deviceType", "touchScreen");
4023 prepareDisplay(DISPLAY_ORIENTATION_0);
4024 prepareButtons();
4025 prepareAxes(POSITION);
4026 prepareVirtualKeys();
4027 addMapperAndConfigure(mapper);
4028
4029 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4030
4031 NotifyMotionArgs motionArgs;
4032
4033 // Down.
4034 int32_t x = 100;
4035 int32_t y = 125;
4036 processDown(mapper, x, y);
4037 processSync(mapper);
4038
4039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4040 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4041 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4042 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4043 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4045 ASSERT_EQ(0, motionArgs.flags);
4046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4047 ASSERT_EQ(0, motionArgs.buttonState);
4048 ASSERT_EQ(0, motionArgs.edgeFlags);
4049 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4050 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4053 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4054 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4055 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4056 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4057
4058 // Move.
4059 x += 50;
4060 y += 75;
4061 processMove(mapper, x, y);
4062 processSync(mapper);
4063
4064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4065 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4066 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4067 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4068 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4070 ASSERT_EQ(0, motionArgs.flags);
4071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4072 ASSERT_EQ(0, motionArgs.buttonState);
4073 ASSERT_EQ(0, motionArgs.edgeFlags);
4074 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4078 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4079 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4080 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4082
4083 // Up.
4084 processUp(mapper);
4085 processSync(mapper);
4086
4087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4088 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4089 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4090 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4091 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4092 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4093 ASSERT_EQ(0, motionArgs.flags);
4094 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4095 ASSERT_EQ(0, motionArgs.buttonState);
4096 ASSERT_EQ(0, motionArgs.edgeFlags);
4097 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4098 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4101 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4102 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4103 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4104 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4105
4106 // Should not have sent any more keys or motions.
4107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4109}
4110
4111TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4112 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4113 addConfigurationProperty("touch.deviceType", "touchScreen");
4114 prepareButtons();
4115 prepareAxes(POSITION);
4116 addConfigurationProperty("touch.orientationAware", "0");
4117 addMapperAndConfigure(mapper);
4118
4119 NotifyMotionArgs args;
4120
4121 // Rotation 90.
4122 prepareDisplay(DISPLAY_ORIENTATION_90);
4123 processDown(mapper, toRawX(50), toRawY(75));
4124 processSync(mapper);
4125
4126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4127 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4128 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4129
4130 processUp(mapper);
4131 processSync(mapper);
4132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4133}
4134
4135TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4136 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4137 addConfigurationProperty("touch.deviceType", "touchScreen");
4138 prepareButtons();
4139 prepareAxes(POSITION);
4140 addMapperAndConfigure(mapper);
4141
4142 NotifyMotionArgs args;
4143
4144 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004145 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 prepareDisplay(DISPLAY_ORIENTATION_0);
4147 processDown(mapper, toRawX(50), toRawY(75));
4148 processSync(mapper);
4149
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4151 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4152 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4153
4154 processUp(mapper);
4155 processSync(mapper);
4156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4157
4158 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004159 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 prepareDisplay(DISPLAY_ORIENTATION_90);
4161 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4162 processSync(mapper);
4163
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4165 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4166 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4167
4168 processUp(mapper);
4169 processSync(mapper);
4170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4171
4172 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004173 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 prepareDisplay(DISPLAY_ORIENTATION_180);
4175 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4176 processSync(mapper);
4177
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4179 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4180 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4181
4182 processUp(mapper);
4183 processSync(mapper);
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4185
4186 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004187 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188 prepareDisplay(DISPLAY_ORIENTATION_270);
4189 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4190 processSync(mapper);
4191
4192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4193 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4194 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4195
4196 processUp(mapper);
4197 processSync(mapper);
4198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4199}
4200
4201TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4202 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4203 addConfigurationProperty("touch.deviceType", "touchScreen");
4204 prepareDisplay(DISPLAY_ORIENTATION_0);
4205 prepareButtons();
4206 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4207 addMapperAndConfigure(mapper);
4208
4209 // These calculations are based on the input device calibration documentation.
4210 int32_t rawX = 100;
4211 int32_t rawY = 200;
4212 int32_t rawPressure = 10;
4213 int32_t rawToolMajor = 12;
4214 int32_t rawDistance = 2;
4215 int32_t rawTiltX = 30;
4216 int32_t rawTiltY = 110;
4217
4218 float x = toDisplayX(rawX);
4219 float y = toDisplayY(rawY);
4220 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4221 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4222 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4223 float distance = float(rawDistance);
4224
4225 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4226 float tiltScale = M_PI / 180;
4227 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4228 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4229 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4230 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4231
4232 processDown(mapper, rawX, rawY);
4233 processPressure(mapper, rawPressure);
4234 processToolMajor(mapper, rawToolMajor);
4235 processDistance(mapper, rawDistance);
4236 processTilt(mapper, rawTiltX, rawTiltY);
4237 processSync(mapper);
4238
4239 NotifyMotionArgs args;
4240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4242 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4243 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4244}
4245
Jason Gerecke489fda82012-09-07 17:19:40 -07004246TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4247 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4248 addConfigurationProperty("touch.deviceType", "touchScreen");
4249 prepareDisplay(DISPLAY_ORIENTATION_0);
4250 prepareLocationCalibration();
4251 prepareButtons();
4252 prepareAxes(POSITION);
4253 addMapperAndConfigure(mapper);
4254
4255 int32_t rawX = 100;
4256 int32_t rawY = 200;
4257
4258 float x = toDisplayX(toCookedX(rawX, rawY));
4259 float y = toDisplayY(toCookedY(rawX, rawY));
4260
4261 processDown(mapper, rawX, rawY);
4262 processSync(mapper);
4263
4264 NotifyMotionArgs args;
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4267 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4268}
4269
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4271 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4272 addConfigurationProperty("touch.deviceType", "touchScreen");
4273 prepareDisplay(DISPLAY_ORIENTATION_0);
4274 prepareButtons();
4275 prepareAxes(POSITION);
4276 addMapperAndConfigure(mapper);
4277
4278 NotifyMotionArgs motionArgs;
4279 NotifyKeyArgs keyArgs;
4280
4281 processDown(mapper, 100, 200);
4282 processSync(mapper);
4283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4284 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4285 ASSERT_EQ(0, motionArgs.buttonState);
4286
4287 // press BTN_LEFT, release BTN_LEFT
4288 processKey(mapper, BTN_LEFT, 1);
4289 processSync(mapper);
4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4292 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4293
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4296 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4297
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 processKey(mapper, BTN_LEFT, 0);
4299 processSync(mapper);
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303
4304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004306 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307
4308 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4309 processKey(mapper, BTN_RIGHT, 1);
4310 processKey(mapper, BTN_MIDDLE, 1);
4311 processSync(mapper);
4312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4314 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4315 motionArgs.buttonState);
4316
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4319 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4320
4321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4323 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4324 motionArgs.buttonState);
4325
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 processKey(mapper, BTN_RIGHT, 0);
4327 processSync(mapper);
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331
4332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335
4336 processKey(mapper, BTN_MIDDLE, 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_BACK, release BTN_BACK
4347 processKey(mapper, BTN_BACK, 1);
4348 processSync(mapper);
4349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4351 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4356
4357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360
4361 processKey(mapper, BTN_BACK, 0);
4362 processSync(mapper);
4363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004366
4367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369 ASSERT_EQ(0, motionArgs.buttonState);
4370
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4372 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4373 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4374
4375 // press BTN_SIDE, release BTN_SIDE
4376 processKey(mapper, BTN_SIDE, 1);
4377 processSync(mapper);
4378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4379 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4380 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004381
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4385
4386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4388 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389
4390 processKey(mapper, BTN_SIDE, 0);
4391 processSync(mapper);
4392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395
4396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398 ASSERT_EQ(0, motionArgs.buttonState);
4399
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4402 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4403
4404 // press BTN_FORWARD, release BTN_FORWARD
4405 processKey(mapper, BTN_FORWARD, 1);
4406 processSync(mapper);
4407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4408 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4409 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4414
4415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4417 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418
4419 processKey(mapper, BTN_FORWARD, 0);
4420 processSync(mapper);
4421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004424
4425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004427 ASSERT_EQ(0, motionArgs.buttonState);
4428
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4430 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4431 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4432
4433 // press BTN_EXTRA, release BTN_EXTRA
4434 processKey(mapper, BTN_EXTRA, 1);
4435 processSync(mapper);
4436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4437 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4438 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004439
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4443
4444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4446 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
4448 processKey(mapper, BTN_EXTRA, 0);
4449 processSync(mapper);
4450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456 ASSERT_EQ(0, motionArgs.buttonState);
4457
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4460 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4461
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4463
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 // press BTN_STYLUS, release BTN_STYLUS
4465 processKey(mapper, BTN_STYLUS, 1);
4466 processSync(mapper);
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004469 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4470
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4472 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4473 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474
4475 processKey(mapper, BTN_STYLUS, 0);
4476 processSync(mapper);
4477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004478 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004480
4481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004483 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484
4485 // press BTN_STYLUS2, release BTN_STYLUS2
4486 processKey(mapper, BTN_STYLUS2, 1);
4487 processSync(mapper);
4488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4491
4492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4494 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495
4496 processKey(mapper, BTN_STYLUS2, 0);
4497 processSync(mapper);
4498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004499 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004501
4502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004504 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505
4506 // release touch
4507 processUp(mapper);
4508 processSync(mapper);
4509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4510 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4511 ASSERT_EQ(0, motionArgs.buttonState);
4512}
4513
4514TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4515 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4516 addConfigurationProperty("touch.deviceType", "touchScreen");
4517 prepareDisplay(DISPLAY_ORIENTATION_0);
4518 prepareButtons();
4519 prepareAxes(POSITION);
4520 addMapperAndConfigure(mapper);
4521
4522 NotifyMotionArgs motionArgs;
4523
4524 // default tool type is finger
4525 processDown(mapper, 100, 200);
4526 processSync(mapper);
4527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4528 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4530
4531 // eraser
4532 processKey(mapper, BTN_TOOL_RUBBER, 1);
4533 processSync(mapper);
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4537
4538 // stylus
4539 processKey(mapper, BTN_TOOL_RUBBER, 0);
4540 processKey(mapper, BTN_TOOL_PEN, 1);
4541 processSync(mapper);
4542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4545
4546 // brush
4547 processKey(mapper, BTN_TOOL_PEN, 0);
4548 processKey(mapper, BTN_TOOL_BRUSH, 1);
4549 processSync(mapper);
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4553
4554 // pencil
4555 processKey(mapper, BTN_TOOL_BRUSH, 0);
4556 processKey(mapper, BTN_TOOL_PENCIL, 1);
4557 processSync(mapper);
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4561
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004562 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563 processKey(mapper, BTN_TOOL_PENCIL, 0);
4564 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4565 processSync(mapper);
4566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4569
4570 // mouse
4571 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4572 processKey(mapper, BTN_TOOL_MOUSE, 1);
4573 processSync(mapper);
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4577
4578 // lens
4579 processKey(mapper, BTN_TOOL_MOUSE, 0);
4580 processKey(mapper, BTN_TOOL_LENS, 1);
4581 processSync(mapper);
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4585
4586 // double-tap
4587 processKey(mapper, BTN_TOOL_LENS, 0);
4588 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4589 processSync(mapper);
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4593
4594 // triple-tap
4595 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4596 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4597 processSync(mapper);
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4601
4602 // quad-tap
4603 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4604 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4605 processSync(mapper);
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4609
4610 // finger
4611 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4612 processKey(mapper, BTN_TOOL_FINGER, 1);
4613 processSync(mapper);
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4617
4618 // stylus trumps finger
4619 processKey(mapper, BTN_TOOL_PEN, 1);
4620 processSync(mapper);
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4624
4625 // eraser trumps stylus
4626 processKey(mapper, BTN_TOOL_RUBBER, 1);
4627 processSync(mapper);
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4631
4632 // mouse trumps eraser
4633 processKey(mapper, BTN_TOOL_MOUSE, 1);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4638
4639 // back to default tool type
4640 processKey(mapper, BTN_TOOL_MOUSE, 0);
4641 processKey(mapper, BTN_TOOL_RUBBER, 0);
4642 processKey(mapper, BTN_TOOL_PEN, 0);
4643 processKey(mapper, BTN_TOOL_FINGER, 0);
4644 processSync(mapper);
4645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4648}
4649
4650TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4651 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4652 addConfigurationProperty("touch.deviceType", "touchScreen");
4653 prepareDisplay(DISPLAY_ORIENTATION_0);
4654 prepareButtons();
4655 prepareAxes(POSITION);
4656 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4657 addMapperAndConfigure(mapper);
4658
4659 NotifyMotionArgs motionArgs;
4660
4661 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4662 processKey(mapper, BTN_TOOL_FINGER, 1);
4663 processMove(mapper, 100, 200);
4664 processSync(mapper);
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4666 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4668 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4669
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4673 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4674
4675 // move a little
4676 processMove(mapper, 150, 250);
4677 processSync(mapper);
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4681 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4682
4683 // down when BTN_TOUCH is pressed, pressure defaults to 1
4684 processKey(mapper, BTN_TOUCH, 1);
4685 processSync(mapper);
4686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4689 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4690
4691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4692 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4694 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4695
4696 // up when BTN_TOUCH is released, hover restored
4697 processKey(mapper, BTN_TOUCH, 0);
4698 processSync(mapper);
4699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4700 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4702 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4703
4704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4707 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4708
4709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4710 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4712 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4713
4714 // exit hover when pointer goes away
4715 processKey(mapper, BTN_TOOL_FINGER, 0);
4716 processSync(mapper);
4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4718 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4720 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4721}
4722
4723TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4724 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4725 addConfigurationProperty("touch.deviceType", "touchScreen");
4726 prepareDisplay(DISPLAY_ORIENTATION_0);
4727 prepareButtons();
4728 prepareAxes(POSITION | PRESSURE);
4729 addMapperAndConfigure(mapper);
4730
4731 NotifyMotionArgs motionArgs;
4732
4733 // initially hovering because pressure is 0
4734 processDown(mapper, 100, 200);
4735 processPressure(mapper, 0);
4736 processSync(mapper);
4737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4738 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4740 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4741
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4745 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4746
4747 // move a little
4748 processMove(mapper, 150, 250);
4749 processSync(mapper);
4750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4753 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4754
4755 // down when pressure is non-zero
4756 processPressure(mapper, RAW_PRESSURE_MAX);
4757 processSync(mapper);
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4761 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4762
4763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4766 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4767
4768 // up when pressure becomes 0, hover restored
4769 processPressure(mapper, 0);
4770 processSync(mapper);
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4772 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4774 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4775
4776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4777 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4779 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4780
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4784 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4785
4786 // exit hover when pointer goes away
4787 processUp(mapper);
4788 processSync(mapper);
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4792 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4793}
4794
Dan Harmsaca28402018-12-17 13:55:20 -08004795
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796// --- MultiTouchInputMapperTest ---
4797
4798class MultiTouchInputMapperTest : public TouchInputMapperTest {
4799protected:
4800 void prepareAxes(int axes);
4801
4802 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4803 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4804 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4805 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4806 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4807 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4808 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4809 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4810 void processId(MultiTouchInputMapper* mapper, int32_t id);
4811 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4812 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4813 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4814 void processMTSync(MultiTouchInputMapper* mapper);
4815 void processSync(MultiTouchInputMapper* mapper);
4816};
4817
4818void MultiTouchInputMapperTest::prepareAxes(int axes) {
4819 if (axes & POSITION) {
4820 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4821 RAW_X_MIN, RAW_X_MAX, 0, 0);
4822 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4823 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4824 }
4825 if (axes & TOUCH) {
4826 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4827 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4828 if (axes & MINOR) {
4829 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4830 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4831 }
4832 }
4833 if (axes & TOOL) {
4834 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4835 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4836 if (axes & MINOR) {
4837 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4838 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4839 }
4840 }
4841 if (axes & ORIENTATION) {
4842 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4843 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4844 }
4845 if (axes & PRESSURE) {
4846 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4847 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4848 }
4849 if (axes & DISTANCE) {
4850 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4851 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4852 }
4853 if (axes & ID) {
4854 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4855 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4856 }
4857 if (axes & SLOT) {
4858 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4859 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4860 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4861 }
4862 if (axes & TOOL_TYPE) {
4863 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4864 0, MT_TOOL_MAX, 0, 0);
4865 }
4866}
4867
4868void MultiTouchInputMapperTest::processPosition(
4869 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004870 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4871 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872}
4873
4874void MultiTouchInputMapperTest::processTouchMajor(
4875 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004876 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877}
4878
4879void MultiTouchInputMapperTest::processTouchMinor(
4880 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004881 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882}
4883
4884void MultiTouchInputMapperTest::processToolMajor(
4885 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004886 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887}
4888
4889void MultiTouchInputMapperTest::processToolMinor(
4890 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004891 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892}
4893
4894void MultiTouchInputMapperTest::processOrientation(
4895 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004896 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897}
4898
4899void MultiTouchInputMapperTest::processPressure(
4900 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004901 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902}
4903
4904void MultiTouchInputMapperTest::processDistance(
4905 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004906 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907}
4908
4909void MultiTouchInputMapperTest::processId(
4910 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004911 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912}
4913
4914void MultiTouchInputMapperTest::processSlot(
4915 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004916 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917}
4918
4919void MultiTouchInputMapperTest::processToolType(
4920 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004921 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922}
4923
4924void MultiTouchInputMapperTest::processKey(
4925 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004926 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927}
4928
4929void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004930 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931}
4932
4933void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004934 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935}
4936
4937
4938TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4939 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4940 addConfigurationProperty("touch.deviceType", "touchScreen");
4941 prepareDisplay(DISPLAY_ORIENTATION_0);
4942 prepareAxes(POSITION);
4943 prepareVirtualKeys();
4944 addMapperAndConfigure(mapper);
4945
4946 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4947
4948 NotifyMotionArgs motionArgs;
4949
4950 // Two fingers down at once.
4951 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4952 processPosition(mapper, x1, y1);
4953 processMTSync(mapper);
4954 processPosition(mapper, x2, y2);
4955 processMTSync(mapper);
4956 processSync(mapper);
4957
4958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4959 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4960 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4961 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4962 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4963 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4964 ASSERT_EQ(0, motionArgs.flags);
4965 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4966 ASSERT_EQ(0, motionArgs.buttonState);
4967 ASSERT_EQ(0, motionArgs.edgeFlags);
4968 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4969 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4972 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4973 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4974 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4975 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4976
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4979 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4980 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4981 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4982 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4983 motionArgs.action);
4984 ASSERT_EQ(0, motionArgs.flags);
4985 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4986 ASSERT_EQ(0, motionArgs.buttonState);
4987 ASSERT_EQ(0, motionArgs.edgeFlags);
4988 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4989 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4991 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4992 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4994 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4996 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4997 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4998 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4999 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5000
5001 // Move.
5002 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5003 processPosition(mapper, x1, y1);
5004 processMTSync(mapper);
5005 processPosition(mapper, x2, y2);
5006 processMTSync(mapper);
5007 processSync(mapper);
5008
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5011 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5012 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5013 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5015 ASSERT_EQ(0, motionArgs.flags);
5016 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5017 ASSERT_EQ(0, motionArgs.buttonState);
5018 ASSERT_EQ(0, motionArgs.edgeFlags);
5019 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5020 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5022 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5025 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5027 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5028 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5029 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5030 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5031
5032 // First finger up.
5033 x2 += 15; y2 -= 20;
5034 processPosition(mapper, x2, y2);
5035 processMTSync(mapper);
5036 processSync(mapper);
5037
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5040 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5041 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5042 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5043 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5044 motionArgs.action);
5045 ASSERT_EQ(0, motionArgs.flags);
5046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5047 ASSERT_EQ(0, motionArgs.buttonState);
5048 ASSERT_EQ(0, motionArgs.edgeFlags);
5049 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5050 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5052 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5055 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5057 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5058 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5059 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5060 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5061
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5063 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5064 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5065 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5066 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5068 ASSERT_EQ(0, motionArgs.flags);
5069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5070 ASSERT_EQ(0, motionArgs.buttonState);
5071 ASSERT_EQ(0, motionArgs.edgeFlags);
5072 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5073 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5076 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5077 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5078 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5080
5081 // Move.
5082 x2 += 20; y2 -= 25;
5083 processPosition(mapper, x2, y2);
5084 processMTSync(mapper);
5085 processSync(mapper);
5086
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5088 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5089 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5090 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5091 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5093 ASSERT_EQ(0, motionArgs.flags);
5094 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5095 ASSERT_EQ(0, motionArgs.buttonState);
5096 ASSERT_EQ(0, motionArgs.edgeFlags);
5097 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5098 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5101 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5102 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5103 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5104 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5105
5106 // New finger down.
5107 int32_t x3 = 700, y3 = 300;
5108 processPosition(mapper, x2, y2);
5109 processMTSync(mapper);
5110 processPosition(mapper, x3, y3);
5111 processMTSync(mapper);
5112 processSync(mapper);
5113
5114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5115 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5116 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5117 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5118 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5119 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5120 motionArgs.action);
5121 ASSERT_EQ(0, motionArgs.flags);
5122 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5123 ASSERT_EQ(0, motionArgs.buttonState);
5124 ASSERT_EQ(0, motionArgs.edgeFlags);
5125 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5126 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5128 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5131 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5133 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5134 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5135 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5136 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5137
5138 // Second finger up.
5139 x3 += 30; y3 -= 20;
5140 processPosition(mapper, x3, y3);
5141 processMTSync(mapper);
5142 processSync(mapper);
5143
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5145 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5146 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5147 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5148 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5149 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5150 motionArgs.action);
5151 ASSERT_EQ(0, motionArgs.flags);
5152 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5153 ASSERT_EQ(0, motionArgs.buttonState);
5154 ASSERT_EQ(0, motionArgs.edgeFlags);
5155 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5156 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5158 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5161 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5163 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5164 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5165 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5166 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5169 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5170 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5171 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5172 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5174 ASSERT_EQ(0, motionArgs.flags);
5175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5176 ASSERT_EQ(0, motionArgs.buttonState);
5177 ASSERT_EQ(0, motionArgs.edgeFlags);
5178 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5179 ASSERT_EQ(0, 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(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5183 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5184 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5186
5187 // Last finger up.
5188 processMTSync(mapper);
5189 processSync(mapper);
5190
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5192 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5193 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5194 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5195 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5197 ASSERT_EQ(0, motionArgs.flags);
5198 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5199 ASSERT_EQ(0, motionArgs.buttonState);
5200 ASSERT_EQ(0, motionArgs.edgeFlags);
5201 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5205 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5206 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5207 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5209
5210 // Should not have sent any more keys or motions.
5211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5213}
5214
5215TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5216 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5217 addConfigurationProperty("touch.deviceType", "touchScreen");
5218 prepareDisplay(DISPLAY_ORIENTATION_0);
5219 prepareAxes(POSITION | ID);
5220 prepareVirtualKeys();
5221 addMapperAndConfigure(mapper);
5222
5223 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5224
5225 NotifyMotionArgs motionArgs;
5226
5227 // Two fingers down at once.
5228 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5229 processPosition(mapper, x1, y1);
5230 processId(mapper, 1);
5231 processMTSync(mapper);
5232 processPosition(mapper, x2, y2);
5233 processId(mapper, 2);
5234 processMTSync(mapper);
5235 processSync(mapper);
5236
5237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5238 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5239 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5240 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5243 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5244
5245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5246 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5247 motionArgs.action);
5248 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5249 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5251 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5254 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5256 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5257
5258 // Move.
5259 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5260 processPosition(mapper, x1, y1);
5261 processId(mapper, 1);
5262 processMTSync(mapper);
5263 processPosition(mapper, x2, y2);
5264 processId(mapper, 2);
5265 processMTSync(mapper);
5266 processSync(mapper);
5267
5268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5270 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5271 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5273 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5276 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5278 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5279
5280 // First finger up.
5281 x2 += 15; y2 -= 20;
5282 processPosition(mapper, x2, y2);
5283 processId(mapper, 2);
5284 processMTSync(mapper);
5285 processSync(mapper);
5286
5287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5288 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5289 motionArgs.action);
5290 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5291 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5293 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5296 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5298 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5299
5300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5302 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5303 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5306 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5307
5308 // Move.
5309 x2 += 20; y2 -= 25;
5310 processPosition(mapper, x2, y2);
5311 processId(mapper, 2);
5312 processMTSync(mapper);
5313 processSync(mapper);
5314
5315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5317 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5318 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5321 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5322
5323 // New finger down.
5324 int32_t x3 = 700, y3 = 300;
5325 processPosition(mapper, x2, y2);
5326 processId(mapper, 2);
5327 processMTSync(mapper);
5328 processPosition(mapper, x3, y3);
5329 processId(mapper, 3);
5330 processMTSync(mapper);
5331 processSync(mapper);
5332
5333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5334 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5335 motionArgs.action);
5336 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5339 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5342 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5344 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5345
5346 // Second finger up.
5347 x3 += 30; y3 -= 20;
5348 processPosition(mapper, x3, y3);
5349 processId(mapper, 3);
5350 processMTSync(mapper);
5351 processSync(mapper);
5352
5353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5354 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5355 motionArgs.action);
5356 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5357 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5359 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5362 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5364 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5365
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5368 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5369 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5372 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5373
5374 // Last finger up.
5375 processMTSync(mapper);
5376 processSync(mapper);
5377
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5379 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5380 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5381 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5384 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5385
5386 // Should not have sent any more keys or motions.
5387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5389}
5390
5391TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5392 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5393 addConfigurationProperty("touch.deviceType", "touchScreen");
5394 prepareDisplay(DISPLAY_ORIENTATION_0);
5395 prepareAxes(POSITION | ID | SLOT);
5396 prepareVirtualKeys();
5397 addMapperAndConfigure(mapper);
5398
5399 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5400
5401 NotifyMotionArgs motionArgs;
5402
5403 // Two fingers down at once.
5404 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5405 processPosition(mapper, x1, y1);
5406 processId(mapper, 1);
5407 processSlot(mapper, 1);
5408 processPosition(mapper, x2, y2);
5409 processId(mapper, 2);
5410 processSync(mapper);
5411
5412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5414 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5415 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5418 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5419
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5421 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5422 motionArgs.action);
5423 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5424 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5426 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5429 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5431 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5432
5433 // Move.
5434 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5435 processSlot(mapper, 0);
5436 processPosition(mapper, x1, y1);
5437 processSlot(mapper, 1);
5438 processPosition(mapper, x2, y2);
5439 processSync(mapper);
5440
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5443 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5444 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5446 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5449 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5451 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5452
5453 // First finger up.
5454 x2 += 15; y2 -= 20;
5455 processSlot(mapper, 0);
5456 processId(mapper, -1);
5457 processSlot(mapper, 1);
5458 processPosition(mapper, x2, y2);
5459 processSync(mapper);
5460
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5462 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5463 motionArgs.action);
5464 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5465 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5467 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5470 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5472 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5473
5474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5476 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5477 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5480 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5481
5482 // Move.
5483 x2 += 20; y2 -= 25;
5484 processPosition(mapper, x2, y2);
5485 processSync(mapper);
5486
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5489 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5490 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5493 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5494
5495 // New finger down.
5496 int32_t x3 = 700, y3 = 300;
5497 processPosition(mapper, x2, y2);
5498 processSlot(mapper, 0);
5499 processId(mapper, 3);
5500 processPosition(mapper, x3, y3);
5501 processSync(mapper);
5502
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5504 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5505 motionArgs.action);
5506 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5509 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5512 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5514 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5515
5516 // Second finger up.
5517 x3 += 30; y3 -= 20;
5518 processSlot(mapper, 1);
5519 processId(mapper, -1);
5520 processSlot(mapper, 0);
5521 processPosition(mapper, x3, y3);
5522 processSync(mapper);
5523
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5525 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5526 motionArgs.action);
5527 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5528 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5530 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5533 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5535 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5536
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5539 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5540 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5543 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5544
5545 // Last finger up.
5546 processId(mapper, -1);
5547 processSync(mapper);
5548
5549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5550 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5551 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5552 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5555 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5556
5557 // Should not have sent any more keys or motions.
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5560}
5561
5562TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5563 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5564 addConfigurationProperty("touch.deviceType", "touchScreen");
5565 prepareDisplay(DISPLAY_ORIENTATION_0);
5566 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5567 addMapperAndConfigure(mapper);
5568
5569 // These calculations are based on the input device calibration documentation.
5570 int32_t rawX = 100;
5571 int32_t rawY = 200;
5572 int32_t rawTouchMajor = 7;
5573 int32_t rawTouchMinor = 6;
5574 int32_t rawToolMajor = 9;
5575 int32_t rawToolMinor = 8;
5576 int32_t rawPressure = 11;
5577 int32_t rawDistance = 0;
5578 int32_t rawOrientation = 3;
5579 int32_t id = 5;
5580
5581 float x = toDisplayX(rawX);
5582 float y = toDisplayY(rawY);
5583 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5584 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5585 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5586 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5587 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5588 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5589 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5590 float distance = float(rawDistance);
5591
5592 processPosition(mapper, rawX, rawY);
5593 processTouchMajor(mapper, rawTouchMajor);
5594 processTouchMinor(mapper, rawTouchMinor);
5595 processToolMajor(mapper, rawToolMajor);
5596 processToolMinor(mapper, rawToolMinor);
5597 processPressure(mapper, rawPressure);
5598 processOrientation(mapper, rawOrientation);
5599 processDistance(mapper, rawDistance);
5600 processId(mapper, id);
5601 processMTSync(mapper);
5602 processSync(mapper);
5603
5604 NotifyMotionArgs args;
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5606 ASSERT_EQ(0, args.pointerProperties[0].id);
5607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5608 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5609 orientation, distance));
5610}
5611
5612TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5613 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5614 addConfigurationProperty("touch.deviceType", "touchScreen");
5615 prepareDisplay(DISPLAY_ORIENTATION_0);
5616 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5617 addConfigurationProperty("touch.size.calibration", "geometric");
5618 addMapperAndConfigure(mapper);
5619
5620 // These calculations are based on the input device calibration documentation.
5621 int32_t rawX = 100;
5622 int32_t rawY = 200;
5623 int32_t rawTouchMajor = 140;
5624 int32_t rawTouchMinor = 120;
5625 int32_t rawToolMajor = 180;
5626 int32_t rawToolMinor = 160;
5627
5628 float x = toDisplayX(rawX);
5629 float y = toDisplayY(rawY);
5630 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5631 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5632 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5633 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5634 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5635
5636 processPosition(mapper, rawX, rawY);
5637 processTouchMajor(mapper, rawTouchMajor);
5638 processTouchMinor(mapper, rawTouchMinor);
5639 processToolMajor(mapper, rawToolMajor);
5640 processToolMinor(mapper, rawToolMinor);
5641 processMTSync(mapper);
5642 processSync(mapper);
5643
5644 NotifyMotionArgs args;
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5647 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5648}
5649
5650TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5651 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5652 addConfigurationProperty("touch.deviceType", "touchScreen");
5653 prepareDisplay(DISPLAY_ORIENTATION_0);
5654 prepareAxes(POSITION | TOUCH | TOOL);
5655 addConfigurationProperty("touch.size.calibration", "diameter");
5656 addConfigurationProperty("touch.size.scale", "10");
5657 addConfigurationProperty("touch.size.bias", "160");
5658 addConfigurationProperty("touch.size.isSummed", "1");
5659 addMapperAndConfigure(mapper);
5660
5661 // These calculations are based on the input device calibration documentation.
5662 // Note: We only provide a single common touch/tool value because the device is assumed
5663 // not to emit separate values for each pointer (isSummed = 1).
5664 int32_t rawX = 100;
5665 int32_t rawY = 200;
5666 int32_t rawX2 = 150;
5667 int32_t rawY2 = 250;
5668 int32_t rawTouchMajor = 5;
5669 int32_t rawToolMajor = 8;
5670
5671 float x = toDisplayX(rawX);
5672 float y = toDisplayY(rawY);
5673 float x2 = toDisplayX(rawX2);
5674 float y2 = toDisplayY(rawY2);
5675 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5676 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5677 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5678
5679 processPosition(mapper, rawX, rawY);
5680 processTouchMajor(mapper, rawTouchMajor);
5681 processToolMajor(mapper, rawToolMajor);
5682 processMTSync(mapper);
5683 processPosition(mapper, rawX2, rawY2);
5684 processTouchMajor(mapper, rawTouchMajor);
5685 processToolMajor(mapper, rawToolMajor);
5686 processMTSync(mapper);
5687 processSync(mapper);
5688
5689 NotifyMotionArgs args;
5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5691 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5692
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5694 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5695 args.action);
5696 ASSERT_EQ(size_t(2), args.pointerCount);
5697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5698 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5700 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5701}
5702
5703TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5704 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5705 addConfigurationProperty("touch.deviceType", "touchScreen");
5706 prepareDisplay(DISPLAY_ORIENTATION_0);
5707 prepareAxes(POSITION | TOUCH | TOOL);
5708 addConfigurationProperty("touch.size.calibration", "area");
5709 addConfigurationProperty("touch.size.scale", "43");
5710 addConfigurationProperty("touch.size.bias", "3");
5711 addMapperAndConfigure(mapper);
5712
5713 // These calculations are based on the input device calibration documentation.
5714 int32_t rawX = 100;
5715 int32_t rawY = 200;
5716 int32_t rawTouchMajor = 5;
5717 int32_t rawToolMajor = 8;
5718
5719 float x = toDisplayX(rawX);
5720 float y = toDisplayY(rawY);
5721 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5722 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5723 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5724
5725 processPosition(mapper, rawX, rawY);
5726 processTouchMajor(mapper, rawTouchMajor);
5727 processToolMajor(mapper, rawToolMajor);
5728 processMTSync(mapper);
5729 processSync(mapper);
5730
5731 NotifyMotionArgs args;
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5734 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5735}
5736
5737TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5738 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5739 addConfigurationProperty("touch.deviceType", "touchScreen");
5740 prepareDisplay(DISPLAY_ORIENTATION_0);
5741 prepareAxes(POSITION | PRESSURE);
5742 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5743 addConfigurationProperty("touch.pressure.scale", "0.01");
5744 addMapperAndConfigure(mapper);
5745
Michael Wrightaa449c92017-12-13 21:21:43 +00005746 InputDeviceInfo info;
5747 mapper->populateDeviceInfo(&info);
5748 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5749 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5750 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5751
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752 // These calculations are based on the input device calibration documentation.
5753 int32_t rawX = 100;
5754 int32_t rawY = 200;
5755 int32_t rawPressure = 60;
5756
5757 float x = toDisplayX(rawX);
5758 float y = toDisplayY(rawY);
5759 float pressure = float(rawPressure) * 0.01f;
5760
5761 processPosition(mapper, rawX, rawY);
5762 processPressure(mapper, rawPressure);
5763 processMTSync(mapper);
5764 processSync(mapper);
5765
5766 NotifyMotionArgs args;
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5769 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5770}
5771
5772TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5773 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5774 addConfigurationProperty("touch.deviceType", "touchScreen");
5775 prepareDisplay(DISPLAY_ORIENTATION_0);
5776 prepareAxes(POSITION | ID | SLOT);
5777 addMapperAndConfigure(mapper);
5778
5779 NotifyMotionArgs motionArgs;
5780 NotifyKeyArgs keyArgs;
5781
5782 processId(mapper, 1);
5783 processPosition(mapper, 100, 200);
5784 processSync(mapper);
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5787 ASSERT_EQ(0, motionArgs.buttonState);
5788
5789 // press BTN_LEFT, release BTN_LEFT
5790 processKey(mapper, BTN_LEFT, 1);
5791 processSync(mapper);
5792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5794 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5795
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5798 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5799
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800 processKey(mapper, BTN_LEFT, 0);
5801 processSync(mapper);
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005805
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005808 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809
5810 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5811 processKey(mapper, BTN_RIGHT, 1);
5812 processKey(mapper, BTN_MIDDLE, 1);
5813 processSync(mapper);
5814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5816 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5817 motionArgs.buttonState);
5818
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5821 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5822
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5825 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5826 motionArgs.buttonState);
5827
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828 processKey(mapper, BTN_RIGHT, 0);
5829 processSync(mapper);
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005831 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005833
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005836 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837
5838 processKey(mapper, BTN_MIDDLE, 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_BACK, release BTN_BACK
5849 processKey(mapper, BTN_BACK, 1);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5852 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5853 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005854
Michael Wrightd02c5b62014-02-10 15:10:22 -08005855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005857 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5858
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5861 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862
5863 processKey(mapper, BTN_BACK, 0);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005868
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005871 ASSERT_EQ(0, motionArgs.buttonState);
5872
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5874 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5875 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5876
5877 // press BTN_SIDE, release BTN_SIDE
5878 processKey(mapper, BTN_SIDE, 1);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5881 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5882 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005883
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005886 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5887
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5890 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891
5892 processKey(mapper, BTN_SIDE, 0);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005897
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005900 ASSERT_EQ(0, motionArgs.buttonState);
5901
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5903 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5904 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5905
5906 // press BTN_FORWARD, release BTN_FORWARD
5907 processKey(mapper, BTN_FORWARD, 1);
5908 processSync(mapper);
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5910 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5911 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005912
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005915 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5916
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5919 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920
5921 processKey(mapper, BTN_FORWARD, 0);
5922 processSync(mapper);
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005926
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005929 ASSERT_EQ(0, motionArgs.buttonState);
5930
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5932 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5933 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5934
5935 // press BTN_EXTRA, release BTN_EXTRA
5936 processKey(mapper, BTN_EXTRA, 1);
5937 processSync(mapper);
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5939 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5940 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005941
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005944 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5945
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5948 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949
5950 processKey(mapper, BTN_EXTRA, 0);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005953 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005958 ASSERT_EQ(0, motionArgs.buttonState);
5959
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5961 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5962 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5963
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5965
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 // press BTN_STYLUS, release BTN_STYLUS
5967 processKey(mapper, BTN_STYLUS, 1);
5968 processSync(mapper);
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005971 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5972
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5975 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976
5977 processKey(mapper, BTN_STYLUS, 0);
5978 processSync(mapper);
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005985 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986
5987 // press BTN_STYLUS2, release BTN_STYLUS2
5988 processKey(mapper, BTN_STYLUS2, 1);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5993
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5996 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
5998 processKey(mapper, BTN_STYLUS2, 0);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007
6008 // release touch
6009 processId(mapper, -1);
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6012 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6013 ASSERT_EQ(0, motionArgs.buttonState);
6014}
6015
6016TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6017 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6018 addConfigurationProperty("touch.deviceType", "touchScreen");
6019 prepareDisplay(DISPLAY_ORIENTATION_0);
6020 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6021 addMapperAndConfigure(mapper);
6022
6023 NotifyMotionArgs motionArgs;
6024
6025 // default tool type is finger
6026 processId(mapper, 1);
6027 processPosition(mapper, 100, 200);
6028 processSync(mapper);
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6030 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6032
6033 // eraser
6034 processKey(mapper, BTN_TOOL_RUBBER, 1);
6035 processSync(mapper);
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6039
6040 // stylus
6041 processKey(mapper, BTN_TOOL_RUBBER, 0);
6042 processKey(mapper, BTN_TOOL_PEN, 1);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6047
6048 // brush
6049 processKey(mapper, BTN_TOOL_PEN, 0);
6050 processKey(mapper, BTN_TOOL_BRUSH, 1);
6051 processSync(mapper);
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6055
6056 // pencil
6057 processKey(mapper, BTN_TOOL_BRUSH, 0);
6058 processKey(mapper, BTN_TOOL_PENCIL, 1);
6059 processSync(mapper);
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6063
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006064 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 processKey(mapper, BTN_TOOL_PENCIL, 0);
6066 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6067 processSync(mapper);
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6071
6072 // mouse
6073 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6074 processKey(mapper, BTN_TOOL_MOUSE, 1);
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6079
6080 // lens
6081 processKey(mapper, BTN_TOOL_MOUSE, 0);
6082 processKey(mapper, BTN_TOOL_LENS, 1);
6083 processSync(mapper);
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6087
6088 // double-tap
6089 processKey(mapper, BTN_TOOL_LENS, 0);
6090 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6091 processSync(mapper);
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6095
6096 // triple-tap
6097 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6098 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6103
6104 // quad-tap
6105 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6106 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6107 processSync(mapper);
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6111
6112 // finger
6113 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6114 processKey(mapper, BTN_TOOL_FINGER, 1);
6115 processSync(mapper);
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6119
6120 // stylus trumps finger
6121 processKey(mapper, BTN_TOOL_PEN, 1);
6122 processSync(mapper);
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6126
6127 // eraser trumps stylus
6128 processKey(mapper, BTN_TOOL_RUBBER, 1);
6129 processSync(mapper);
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6133
6134 // mouse trumps eraser
6135 processKey(mapper, BTN_TOOL_MOUSE, 1);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6140
6141 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6142 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6143 processSync(mapper);
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6147
6148 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6149 processToolType(mapper, MT_TOOL_PEN);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6154
6155 // back to default tool type
6156 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6157 processKey(mapper, BTN_TOOL_MOUSE, 0);
6158 processKey(mapper, BTN_TOOL_RUBBER, 0);
6159 processKey(mapper, BTN_TOOL_PEN, 0);
6160 processKey(mapper, BTN_TOOL_FINGER, 0);
6161 processSync(mapper);
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6165}
6166
6167TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6168 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6169 addConfigurationProperty("touch.deviceType", "touchScreen");
6170 prepareDisplay(DISPLAY_ORIENTATION_0);
6171 prepareAxes(POSITION | ID | SLOT);
6172 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6173 addMapperAndConfigure(mapper);
6174
6175 NotifyMotionArgs motionArgs;
6176
6177 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6178 processId(mapper, 1);
6179 processPosition(mapper, 100, 200);
6180 processSync(mapper);
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6184 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6185
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6187 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6189 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6190
6191 // move a little
6192 processPosition(mapper, 150, 250);
6193 processSync(mapper);
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6195 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6197 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6198
6199 // down when BTN_TOUCH is pressed, pressure defaults to 1
6200 processKey(mapper, BTN_TOUCH, 1);
6201 processSync(mapper);
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6203 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6205 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6210 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6211
6212 // up when BTN_TOUCH is released, hover restored
6213 processKey(mapper, BTN_TOUCH, 0);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6218 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6219
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6223 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6224
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6228 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6229
6230 // exit hover when pointer goes away
6231 processId(mapper, -1);
6232 processSync(mapper);
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6234 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6236 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6237}
6238
6239TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6240 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6241 addConfigurationProperty("touch.deviceType", "touchScreen");
6242 prepareDisplay(DISPLAY_ORIENTATION_0);
6243 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6244 addMapperAndConfigure(mapper);
6245
6246 NotifyMotionArgs motionArgs;
6247
6248 // initially hovering because pressure is 0
6249 processId(mapper, 1);
6250 processPosition(mapper, 100, 200);
6251 processPressure(mapper, 0);
6252 processSync(mapper);
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6256 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6257
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6259 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6261 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6262
6263 // move a little
6264 processPosition(mapper, 150, 250);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6269 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6270
6271 // down when pressure becomes non-zero
6272 processPressure(mapper, RAW_PRESSURE_MAX);
6273 processSync(mapper);
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6278
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6282 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6283
6284 // up when pressure becomes 0, hover restored
6285 processPressure(mapper, 0);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6290 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6291
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6295 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6300 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6301
6302 // exit hover when pointer goes away
6303 processId(mapper, -1);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6308 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6309}
6310
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006311/**
6312 * Set the input device port <--> display port associations, and check that the
6313 * events are routed to the display that matches the display port.
6314 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6315 */
6316TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6317 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6318 const std::string usb2 = "USB2";
6319 const uint8_t hdmi1 = 0;
6320 const uint8_t hdmi2 = 1;
6321 const std::string secondaryUniqueId = "uniqueId2";
6322 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6323
6324 addConfigurationProperty("touch.deviceType", "touchScreen");
6325 prepareAxes(POSITION);
6326 addMapperAndConfigure(mapper);
6327
6328 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6329 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6330
6331 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6332 // for this input device is specified, and the matching viewport is not present,
6333 // the input device should be disabled (at the mapper level).
6334
6335 // Add viewport for display 2 on hdmi2
6336 prepareSecondaryDisplay(type, hdmi2);
6337 // Send a touch event
6338 processPosition(mapper, 100, 100);
6339 processSync(mapper);
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6341
6342 // Add viewport for display 1 on hdmi1
6343 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6344 // Send a touch event again
6345 processPosition(mapper, 100, 100);
6346 processSync(mapper);
6347
6348 NotifyMotionArgs args;
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6350 ASSERT_EQ(DISPLAY_ID, args.displayId);
6351}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352
Arthur Hung41a712e2018-11-22 19:41:03 +08006353/**
6354 * Expect fallback to internal viewport if device is external and external viewport is not present.
6355 */
6356TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6357 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6358 prepareAxes(POSITION);
6359 addConfigurationProperty("touch.deviceType", "touchScreen");
6360 prepareDisplay(DISPLAY_ORIENTATION_0);
6361 mDevice->setExternal(true);
6362 addMapperAndConfigure(mapper);
6363
6364 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6365
6366 NotifyMotionArgs motionArgs;
6367
6368 // Expect the event to be sent to the internal viewport,
6369 // because an external viewport is not present.
6370 processPosition(mapper, 100, 100);
6371 processSync(mapper);
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6373 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6374
6375 // Expect the event to be sent to the external viewport if it is present.
6376 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6377 processPosition(mapper, 100, 100);
6378 processSync(mapper);
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6380 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6381}
6382
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006383TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6384 // Setup PointerController for second display.
6385 sp<FakePointerController> fakePointerController = new FakePointerController();
6386 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6387 fakePointerController->setPosition(100, 200);
6388 fakePointerController->setButtonState(0);
6389 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6390 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6391
6392 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6393 prepareDisplay(DISPLAY_ORIENTATION_0);
6394 prepareAxes(POSITION);
6395 addMapperAndConfigure(mapper);
6396
6397 // Check source is mouse that would obtain the PointerController.
6398 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6399
6400 NotifyMotionArgs motionArgs;
6401 processPosition(mapper, 100, 100);
6402 processSync(mapper);
6403
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6406 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6407}
6408
Arthur Hung7c645402019-01-25 17:45:42 +08006409TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6410 // Setup the first touch screen device.
6411 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6412 prepareAxes(POSITION | ID | SLOT);
6413 addConfigurationProperty("touch.deviceType", "touchScreen");
6414 addMapperAndConfigure(mapper);
6415
6416 // Create the second touch screen device, and enable multi fingers.
6417 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006418 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006419 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006420 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006421 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006422 std::unique_ptr<InputDevice> device2 =
6423 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6424 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006425 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6426 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6427 0 /*flat*/, 0 /*fuzz*/);
6428 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6429 0 /*flat*/, 0 /*fuzz*/);
6430 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6431 0 /*flat*/, 0 /*fuzz*/);
6432 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6433 0 /*flat*/, 0 /*fuzz*/);
6434 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6435 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6436 String8("touchScreen"));
6437
6438 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006439 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006440 device2->addMapper(mapper2);
6441 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6442 device2->reset(ARBITRARY_TIME);
6443
6444 // Setup PointerController.
6445 sp<FakePointerController> fakePointerController = new FakePointerController();
6446 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6447 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6448
6449 // Setup policy for associated displays and show touches.
6450 const uint8_t hdmi1 = 0;
6451 const uint8_t hdmi2 = 1;
6452 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6453 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6454 mFakePolicy->setShowTouches(true);
6455
6456 // Create displays.
6457 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6458 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6459
6460 // Default device will reconfigure above, need additional reconfiguration for another device.
6461 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6462 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6463
6464 // Two fingers down at default display.
6465 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6466 processPosition(mapper, x1, y1);
6467 processId(mapper, 1);
6468 processSlot(mapper, 1);
6469 processPosition(mapper, x2, y2);
6470 processId(mapper, 2);
6471 processSync(mapper);
6472
6473 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6474 fakePointerController->getSpots().find(DISPLAY_ID);
6475 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6476 ASSERT_EQ(size_t(2), iter->second.size());
6477
6478 // Two fingers down at second display.
6479 processPosition(mapper2, x1, y1);
6480 processId(mapper2, 1);
6481 processSlot(mapper2, 1);
6482 processPosition(mapper2, x2, y2);
6483 processId(mapper2, 2);
6484 processSync(mapper2);
6485
6486 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6487 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6488 ASSERT_EQ(size_t(2), iter->second.size());
6489}
6490
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006491TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6492 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6493 prepareAxes(POSITION);
6494 addConfigurationProperty("touch.deviceType", "touchScreen");
6495 prepareDisplay(DISPLAY_ORIENTATION_0);
6496 addMapperAndConfigure(mapper);
6497
6498 NotifyMotionArgs motionArgs;
6499 // Unrotated video frame
6500 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6501 std::vector<TouchVideoFrame> frames{frame};
6502 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6503 processPosition(mapper, 100, 200);
6504 processSync(mapper);
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(frames, motionArgs.videoFrames);
6507
6508 // Subsequent touch events should not have any videoframes
6509 // This is implemented separately in FakeEventHub,
6510 // but that should match the behaviour of TouchVideoDevice.
6511 processPosition(mapper, 200, 200);
6512 processSync(mapper);
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6514 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6515}
6516
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006517TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6518 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6519 prepareAxes(POSITION);
6520 addConfigurationProperty("touch.deviceType", "touchScreen");
6521 addMapperAndConfigure(mapper);
6522 // Unrotated video frame
6523 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6524 NotifyMotionArgs motionArgs;
6525
6526 // Test all 4 orientations
6527 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6528 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6529 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6530 clearViewports();
6531 prepareDisplay(orientation);
6532 std::vector<TouchVideoFrame> frames{frame};
6533 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6534 processPosition(mapper, 100, 200);
6535 processSync(mapper);
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 frames[0].rotate(orientation);
6538 ASSERT_EQ(frames, motionArgs.videoFrames);
6539 }
6540}
6541
6542TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6543 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6544 prepareAxes(POSITION);
6545 addConfigurationProperty("touch.deviceType", "touchScreen");
6546 addMapperAndConfigure(mapper);
6547 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6548 // so mix these.
6549 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6550 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6551 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6552 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6553 NotifyMotionArgs motionArgs;
6554
6555 prepareDisplay(DISPLAY_ORIENTATION_90);
6556 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6557 processPosition(mapper, 100, 200);
6558 processSync(mapper);
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 std::for_each(frames.begin(), frames.end(),
6561 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6562 ASSERT_EQ(frames, motionArgs.videoFrames);
6563}
6564
Arthur Hung9da14732019-09-02 16:16:58 +08006565/**
6566 * If we had defined port associations, but the viewport is not ready, the touch device would be
6567 * expected to be disabled, and it should be enabled after the viewport has found.
6568 */
6569TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6570 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6571 constexpr uint8_t hdmi2 = 1;
6572 const std::string secondaryUniqueId = "uniqueId2";
6573 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6574
6575 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6576
6577 addConfigurationProperty("touch.deviceType", "touchScreen");
6578 prepareAxes(POSITION);
6579 addMapperAndConfigure(mapper);
6580
6581 ASSERT_EQ(mDevice->isEnabled(), false);
6582
6583 // Add display on hdmi2, the device should be enabled and can receive touch event.
6584 prepareSecondaryDisplay(type, hdmi2);
6585 ASSERT_EQ(mDevice->isEnabled(), true);
6586
6587 // Send a touch event.
6588 processPosition(mapper, 100, 100);
6589 processSync(mapper);
6590
6591 NotifyMotionArgs args;
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6593 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6594}
6595
Michael Wrightd02c5b62014-02-10 15:10:22 -08006596} // namespace android