blob: 7cd8793d5abea6ecaf81d8743f2447b552cd92cf [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
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070028#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080030#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <math.h>
32
33namespace android {
34
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070035using std::chrono_literals::operator""ms;
36
37// Timeout for waiting for an expected event
38static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
39
Michael Wrightd02c5b62014-02-10 15:10:22 -080040// An arbitrary time value.
41static const nsecs_t ARBITRARY_TIME = 1234;
42
43// Arbitrary display properties.
44static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070045static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080046static const int32_t DISPLAY_WIDTH = 480;
47static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070048static const int32_t VIRTUAL_DISPLAY_ID = 1;
49static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
50static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070051static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070052static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
54// Error tolerance for floating point assertions.
55static const float EPSILON = 0.001f;
56
57template<typename T>
58static inline T min(T a, T b) {
59 return a < b ? a : b;
60}
61
62static inline float avg(float x, float y) {
63 return (x + y) / 2;
64}
65
66
67// --- FakePointerController ---
68
69class FakePointerController : public PointerControllerInterface {
70 bool mHaveBounds;
71 float mMinX, mMinY, mMaxX, mMaxY;
72 float mX, mY;
73 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080074 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080075
76protected:
77 virtual ~FakePointerController() { }
78
79public:
80 FakePointerController() :
81 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080082 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080083 }
84
85 void setBounds(float minX, float minY, float maxX, float maxY) {
86 mHaveBounds = true;
87 mMinX = minX;
88 mMinY = minY;
89 mMaxX = maxX;
90 mMaxY = maxY;
91 }
92
93 virtual void setPosition(float x, float y) {
94 mX = x;
95 mY = y;
96 }
97
98 virtual void setButtonState(int32_t buttonState) {
99 mButtonState = buttonState;
100 }
101
102 virtual int32_t getButtonState() const {
103 return mButtonState;
104 }
105
106 virtual void getPosition(float* outX, float* outY) const {
107 *outX = mX;
108 *outY = mY;
109 }
110
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800111 virtual int32_t getDisplayId() const {
112 return mDisplayId;
113 }
114
Garfield Tan888a6a42020-01-09 11:39:16 -0800115 virtual void setDisplayViewport(const DisplayViewport& viewport) {
116 mDisplayId = viewport.displayId;
117 }
118
Arthur Hung7c645402019-01-25 17:45:42 +0800119 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
120 return mSpotsByDisplay;
121 }
122
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123private:
124 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
125 *outMinX = mMinX;
126 *outMinY = mMinY;
127 *outMaxX = mMaxX;
128 *outMaxY = mMaxY;
129 return mHaveBounds;
130 }
131
132 virtual void move(float deltaX, float deltaY) {
133 mX += deltaX;
134 if (mX < mMinX) mX = mMinX;
135 if (mX > mMaxX) mX = mMaxX;
136 mY += deltaY;
137 if (mY < mMinY) mY = mMinY;
138 if (mY > mMaxY) mY = mMaxY;
139 }
140
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100141 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142 }
143
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100144 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 }
146
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100147 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 }
149
Arthur Hung7c645402019-01-25 17:45:42 +0800150 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
151 int32_t displayId) {
152 std::vector<int32_t> newSpots;
153 // Add spots for fingers that are down.
154 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
155 uint32_t id = idBits.clearFirstMarkedBit();
156 newSpots.push_back(id);
157 }
158
159 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160 }
161
162 virtual void clearSpots() {
163 }
Arthur Hung7c645402019-01-25 17:45:42 +0800164
165 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166};
167
168
169// --- FakeInputReaderPolicy ---
170
171class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700172 std::mutex mLock;
173 std::condition_variable mDevicesChangedCondition;
174
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175 InputReaderConfiguration mConfig;
176 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700177 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
178 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100179 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700180 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181
182protected:
183 virtual ~FakeInputReaderPolicy() { }
184
185public:
186 FakeInputReaderPolicy() {
187 }
188
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700189 void assertInputDevicesChanged() {
190 std::unique_lock<std::mutex> lock(mLock);
191 base::ScopedLockAssertion assumeLocked(mLock);
192
193 const bool devicesChanged =
194 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
195 return mInputDevicesChanged;
196 });
197 if (!devicesChanged) {
198 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
199 }
200 mInputDevicesChanged = false;
201 }
202
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700203 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100204 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100205 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700206 }
207
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700208 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
209 return mConfig.getDisplayViewportByUniqueId(uniqueId);
210 }
211 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
212 return mConfig.getDisplayViewportByType(type);
213 }
214
215 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
216 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700217 }
218
219 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700220 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
221 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700222 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700223 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700224 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100225 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226 }
227
Arthur Hung6cd19a42019-08-30 19:04:12 +0800228 bool updateViewport(const DisplayViewport& viewport) {
229 size_t count = mViewports.size();
230 for (size_t i = 0; i < count; i++) {
231 const DisplayViewport& currentViewport = mViewports[i];
232 if (currentViewport.displayId == viewport.displayId) {
233 mViewports[i] = viewport;
234 mConfig.setDisplayViewports(mViewports);
235 return true;
236 }
237 }
238 // no viewport found.
239 return false;
240 }
241
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100242 void addExcludedDeviceName(const std::string& deviceName) {
243 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244 }
245
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700246 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
247 mConfig.portAssociations.insert({inputPort, displayPort});
248 }
249
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000250 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700251
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000252 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700253
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
255 mPointerControllers.add(deviceId, controller);
256 }
257
258 const InputReaderConfiguration* getReaderConfiguration() const {
259 return &mConfig;
260 }
261
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800262 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 return mInputDevices;
264 }
265
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100266 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700267 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700268 return transform;
269 }
270
271 void setTouchAffineTransformation(const TouchAffineTransformation t) {
272 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800273 }
274
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800275 void setPointerCapture(bool enabled) {
276 mConfig.pointerCapture = enabled;
277 }
278
Arthur Hung7c645402019-01-25 17:45:42 +0800279 void setShowTouches(bool enabled) {
280 mConfig.showTouches = enabled;
281 }
282
Garfield Tan888a6a42020-01-09 11:39:16 -0800283 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
284 mConfig.defaultPointerDisplayId = pointerDisplayId;
285 }
286
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700288 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700289 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
290 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700291 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
292 || orientation == DISPLAY_ORIENTATION_270);
293 DisplayViewport v;
294 v.displayId = displayId;
295 v.orientation = orientation;
296 v.logicalLeft = 0;
297 v.logicalTop = 0;
298 v.logicalRight = isRotated ? height : width;
299 v.logicalBottom = isRotated ? width : height;
300 v.physicalLeft = 0;
301 v.physicalTop = 0;
302 v.physicalRight = isRotated ? height : width;
303 v.physicalBottom = isRotated ? width : height;
304 v.deviceWidth = isRotated ? height : width;
305 v.deviceHeight = isRotated ? width : height;
306 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700307 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100308 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700309 return v;
310 }
311
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
313 *outConfig = mConfig;
314 }
315
316 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
317 return mPointerControllers.valueFor(deviceId);
318 }
319
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800320 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700321 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700323 mInputDevicesChanged = true;
324 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 }
326
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100327 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700328 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100331 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
332 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 }
334};
335
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336// --- FakeEventHub ---
337
338class FakeEventHub : public EventHubInterface {
339 struct KeyInfo {
340 int32_t keyCode;
341 uint32_t flags;
342 };
343
344 struct Device {
345 InputDeviceIdentifier identifier;
346 uint32_t classes;
347 PropertyMap configuration;
348 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
349 KeyedVector<int, bool> relativeAxes;
350 KeyedVector<int32_t, int32_t> keyCodeStates;
351 KeyedVector<int32_t, int32_t> scanCodeStates;
352 KeyedVector<int32_t, int32_t> switchStates;
353 KeyedVector<int32_t, int32_t> absoluteAxisValue;
354 KeyedVector<int32_t, KeyInfo> keysByScanCode;
355 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
356 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800357 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700358 bool enabled;
359
360 status_t enable() {
361 enabled = true;
362 return OK;
363 }
364
365 status_t disable() {
366 enabled = false;
367 return OK;
368 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700370 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700371 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 }
373 };
374
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700375 std::mutex mLock;
376 std::condition_variable mEventsCondition;
377
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100379 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700380 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600381 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700383public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800384 virtual ~FakeEventHub() {
385 for (size_t i = 0; i < mDevices.size(); i++) {
386 delete mDevices.valueAt(i);
387 }
388 }
389
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390 FakeEventHub() { }
391
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100392 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 Device* device = new Device(classes);
394 device->identifier.name = name;
395 mDevices.add(deviceId, device);
396
397 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
398 }
399
400 void removeDevice(int32_t deviceId) {
401 delete mDevices.valueFor(deviceId);
402 mDevices.removeItem(deviceId);
403
404 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
405 }
406
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700407 bool isDeviceEnabled(int32_t deviceId) {
408 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700409 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700410 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
411 return false;
412 }
413 return device->enabled;
414 }
415
416 status_t enableDevice(int32_t deviceId) {
417 status_t result;
418 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700419 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700420 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
421 return BAD_VALUE;
422 }
423 if (device->enabled) {
424 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
425 return OK;
426 }
427 result = device->enable();
428 return result;
429 }
430
431 status_t disableDevice(int32_t deviceId) {
432 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700433 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700434 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
435 return BAD_VALUE;
436 }
437 if (!device->enabled) {
438 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
439 return OK;
440 }
441 return device->disable();
442 }
443
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444 void finishDeviceScan() {
445 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
446 }
447
448 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
449 Device* device = getDevice(deviceId);
450 device->configuration.addProperty(key, value);
451 }
452
453 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
454 Device* device = getDevice(deviceId);
455 device->configuration.addAll(configuration);
456 }
457
458 void addAbsoluteAxis(int32_t deviceId, int axis,
459 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
460 Device* device = getDevice(deviceId);
461
462 RawAbsoluteAxisInfo info;
463 info.valid = true;
464 info.minValue = minValue;
465 info.maxValue = maxValue;
466 info.flat = flat;
467 info.fuzz = fuzz;
468 info.resolution = resolution;
469 device->absoluteAxes.add(axis, info);
470 }
471
472 void addRelativeAxis(int32_t deviceId, int32_t axis) {
473 Device* device = getDevice(deviceId);
474 device->relativeAxes.add(axis, true);
475 }
476
477 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
478 Device* device = getDevice(deviceId);
479 device->keyCodeStates.replaceValueFor(keyCode, state);
480 }
481
482 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
483 Device* device = getDevice(deviceId);
484 device->scanCodeStates.replaceValueFor(scanCode, state);
485 }
486
487 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
488 Device* device = getDevice(deviceId);
489 device->switchStates.replaceValueFor(switchCode, state);
490 }
491
492 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
493 Device* device = getDevice(deviceId);
494 device->absoluteAxisValue.replaceValueFor(axis, value);
495 }
496
497 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
498 int32_t keyCode, uint32_t flags) {
499 Device* device = getDevice(deviceId);
500 KeyInfo info;
501 info.keyCode = keyCode;
502 info.flags = flags;
503 if (scanCode) {
504 device->keysByScanCode.add(scanCode, info);
505 }
506 if (usageCode) {
507 device->keysByUsageCode.add(usageCode, info);
508 }
509 }
510
511 void addLed(int32_t deviceId, int32_t led, bool initialState) {
512 Device* device = getDevice(deviceId);
513 device->leds.add(led, initialState);
514 }
515
516 bool getLedState(int32_t deviceId, int32_t led) {
517 Device* device = getDevice(deviceId);
518 return device->leds.valueFor(led);
519 }
520
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100521 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522 return mExcludedDevices;
523 }
524
525 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
526 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800527 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528 }
529
530 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
531 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700532 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533 RawEvent event;
534 event.when = when;
535 event.deviceId = deviceId;
536 event.type = type;
537 event.code = code;
538 event.value = value;
539 mEvents.push_back(event);
540
541 if (type == EV_ABS) {
542 setAbsoluteAxisValue(deviceId, code, value);
543 }
544 }
545
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600546 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
547 std::vector<TouchVideoFrame>> videoFrames) {
548 mVideoFrames = std::move(videoFrames);
549 }
550
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700552 std::unique_lock<std::mutex> lock(mLock);
553 base::ScopedLockAssertion assumeLocked(mLock);
554 const bool queueIsEmpty =
555 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
556 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
557 if (!queueIsEmpty) {
558 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
559 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560 }
561
562private:
563 Device* getDevice(int32_t deviceId) const {
564 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100565 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 }
567
568 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
569 Device* device = getDevice(deviceId);
570 return device ? device->classes : 0;
571 }
572
573 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
574 Device* device = getDevice(deviceId);
575 return device ? device->identifier : InputDeviceIdentifier();
576 }
577
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100578 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 return 0;
580 }
581
582 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
583 Device* device = getDevice(deviceId);
584 if (device) {
585 *outConfiguration = device->configuration;
586 }
587 }
588
589 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
590 RawAbsoluteAxisInfo* outAxisInfo) const {
591 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800592 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 ssize_t index = device->absoluteAxes.indexOfKey(axis);
594 if (index >= 0) {
595 *outAxisInfo = device->absoluteAxes.valueAt(index);
596 return OK;
597 }
598 }
599 outAxisInfo->clear();
600 return -1;
601 }
602
603 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
604 Device* device = getDevice(deviceId);
605 if (device) {
606 return device->relativeAxes.indexOfKey(axis) >= 0;
607 }
608 return false;
609 }
610
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100611 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 return false;
613 }
614
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700615 virtual status_t mapKey(int32_t deviceId,
616 int32_t scanCode, int32_t usageCode, int32_t metaState,
617 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 Device* device = getDevice(deviceId);
619 if (device) {
620 const KeyInfo* key = getKey(device, scanCode, usageCode);
621 if (key) {
622 if (outKeycode) {
623 *outKeycode = key->keyCode;
624 }
625 if (outFlags) {
626 *outFlags = key->flags;
627 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700628 if (outMetaState) {
629 *outMetaState = metaState;
630 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631 return OK;
632 }
633 }
634 return NAME_NOT_FOUND;
635 }
636
637 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
638 if (usageCode) {
639 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
640 if (index >= 0) {
641 return &device->keysByUsageCode.valueAt(index);
642 }
643 }
644 if (scanCode) {
645 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
646 if (index >= 0) {
647 return &device->keysByScanCode.valueAt(index);
648 }
649 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700650 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 }
652
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100653 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654 return NAME_NOT_FOUND;
655 }
656
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100657 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 mExcludedDevices = devices;
659 }
660
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100661 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700662 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 if (mEvents.empty()) {
664 return 0;
665 }
666
667 *buffer = *mEvents.begin();
668 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700669 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 return 1;
671 }
672
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800673 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600674 auto it = mVideoFrames.find(deviceId);
675 if (it != mVideoFrames.end()) {
676 std::vector<TouchVideoFrame> frames = std::move(it->second);
677 mVideoFrames.erase(deviceId);
678 return frames;
679 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800680 return {};
681 }
682
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
684 Device* device = getDevice(deviceId);
685 if (device) {
686 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
687 if (index >= 0) {
688 return device->scanCodeStates.valueAt(index);
689 }
690 }
691 return AKEY_STATE_UNKNOWN;
692 }
693
694 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
695 Device* device = getDevice(deviceId);
696 if (device) {
697 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
698 if (index >= 0) {
699 return device->keyCodeStates.valueAt(index);
700 }
701 }
702 return AKEY_STATE_UNKNOWN;
703 }
704
705 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
706 Device* device = getDevice(deviceId);
707 if (device) {
708 ssize_t index = device->switchStates.indexOfKey(sw);
709 if (index >= 0) {
710 return device->switchStates.valueAt(index);
711 }
712 }
713 return AKEY_STATE_UNKNOWN;
714 }
715
716 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
717 int32_t* outValue) const {
718 Device* device = getDevice(deviceId);
719 if (device) {
720 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
721 if (index >= 0) {
722 *outValue = device->absoluteAxisValue.valueAt(index);
723 return OK;
724 }
725 }
726 *outValue = 0;
727 return -1;
728 }
729
730 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
731 uint8_t* outFlags) const {
732 bool result = false;
733 Device* device = getDevice(deviceId);
734 if (device) {
735 for (size_t i = 0; i < numCodes; i++) {
736 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
737 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
738 outFlags[i] = 1;
739 result = true;
740 }
741 }
742 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
743 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
744 outFlags[i] = 1;
745 result = true;
746 }
747 }
748 }
749 }
750 return result;
751 }
752
753 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
754 Device* device = getDevice(deviceId);
755 if (device) {
756 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
757 return index >= 0;
758 }
759 return false;
760 }
761
762 virtual bool hasLed(int32_t deviceId, int32_t led) const {
763 Device* device = getDevice(deviceId);
764 return device && device->leds.indexOfKey(led) >= 0;
765 }
766
767 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
768 Device* device = getDevice(deviceId);
769 if (device) {
770 ssize_t index = device->leds.indexOfKey(led);
771 if (index >= 0) {
772 device->leds.replaceValueAt(led, on);
773 } else {
774 ADD_FAILURE()
775 << "Attempted to set the state of an LED that the EventHub declared "
776 "was not present. led=" << led;
777 }
778 }
779 }
780
781 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800782 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 outVirtualKeys.clear();
784
785 Device* device = getDevice(deviceId);
786 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800787 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788 }
789 }
790
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100791 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700792 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793 }
794
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100795 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796 return false;
797 }
798
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100799 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 }
801
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100802 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 }
804
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100805 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806 return false;
807 }
808
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800809 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
812 virtual void monitor() {
813 }
814
815 virtual void requestReopenDevices() {
816 }
817
818 virtual void wake() {
819 }
820};
821
822
823// --- FakeInputReaderContext ---
824
825class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700826 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 sp<InputReaderPolicyInterface> mPolicy;
828 sp<InputListenerInterface> mListener;
829 int32_t mGlobalMetaState;
830 bool mUpdateGlobalMetaStateWasCalled;
831 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800832 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833
834public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700835 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
836 const sp<InputReaderPolicyInterface>& policy,
837 const sp<InputListenerInterface>& listener)
838 : mEventHub(eventHub),
839 mPolicy(policy),
840 mListener(listener),
841 mGlobalMetaState(0),
842 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843
844 virtual ~FakeInputReaderContext() { }
845
846 void assertUpdateGlobalMetaStateWasCalled() {
847 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
848 << "Expected updateGlobalMetaState() to have been called.";
849 mUpdateGlobalMetaStateWasCalled = false;
850 }
851
852 void setGlobalMetaState(int32_t state) {
853 mGlobalMetaState = state;
854 }
855
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800856 uint32_t getGeneration() {
857 return mGeneration;
858 }
859
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860private:
861 virtual void updateGlobalMetaState() {
862 mUpdateGlobalMetaStateWasCalled = true;
863 }
864
865 virtual int32_t getGlobalMetaState() {
866 return mGlobalMetaState;
867 }
868
869 virtual EventHubInterface* getEventHub() {
870 return mEventHub.get();
871 }
872
873 virtual InputReaderPolicyInterface* getPolicy() {
874 return mPolicy.get();
875 }
876
877 virtual InputListenerInterface* getListener() {
878 return mListener.get();
879 }
880
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100881 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 }
883
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100884 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885 return false;
886 }
887
888 virtual void fadePointer() {
889 }
890
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100891 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 }
893
894 virtual int32_t bumpGeneration() {
895 return ++mGeneration;
896 }
Michael Wright842500e2015-03-13 17:32:02 -0700897
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800898 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700899
900 }
901
902 virtual void dispatchExternalStylusState(const StylusState&) {
903
904 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800905
906 virtual uint32_t getNextSequenceNum() {
907 return mNextSequenceNum++;
908 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909};
910
911
912// --- FakeInputMapper ---
913
914class FakeInputMapper : public InputMapper {
915 uint32_t mSources;
916 int32_t mKeyboardType;
917 int32_t mMetaState;
918 KeyedVector<int32_t, int32_t> mKeyCodeStates;
919 KeyedVector<int32_t, int32_t> mScanCodeStates;
920 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800921 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700923 std::mutex mLock;
924 std::condition_variable mStateChangedCondition;
925 bool mConfigureWasCalled GUARDED_BY(mLock);
926 bool mResetWasCalled GUARDED_BY(mLock);
927 bool mProcessWasCalled GUARDED_BY(mLock);
928 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
Arthur Hungc23540e2018-11-29 20:42:11 +0800930 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931public:
932 FakeInputMapper(InputDevice* device, uint32_t sources) :
933 InputMapper(device),
934 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
935 mMetaState(0),
936 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
937 }
938
939 virtual ~FakeInputMapper() { }
940
941 void setKeyboardType(int32_t keyboardType) {
942 mKeyboardType = keyboardType;
943 }
944
945 void setMetaState(int32_t metaState) {
946 mMetaState = metaState;
947 }
948
949 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700950 std::unique_lock<std::mutex> lock(mLock);
951 base::ScopedLockAssertion assumeLocked(mLock);
952 const bool configureCalled =
953 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
954 return mConfigureWasCalled;
955 });
956 if (!configureCalled) {
957 FAIL() << "Expected configure() to have been called.";
958 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 mConfigureWasCalled = false;
960 }
961
962 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700963 std::unique_lock<std::mutex> lock(mLock);
964 base::ScopedLockAssertion assumeLocked(mLock);
965 const bool resetCalled =
966 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
967 return mResetWasCalled;
968 });
969 if (!resetCalled) {
970 FAIL() << "Expected reset() to have been called.";
971 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972 mResetWasCalled = false;
973 }
974
Yi Kong9b14ac62018-07-17 13:48:38 -0700975 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700976 std::unique_lock<std::mutex> lock(mLock);
977 base::ScopedLockAssertion assumeLocked(mLock);
978 const bool processCalled =
979 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
980 return mProcessWasCalled;
981 });
982 if (!processCalled) {
983 FAIL() << "Expected process() to have been called.";
984 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 if (outLastEvent) {
986 *outLastEvent = mLastEvent;
987 }
988 mProcessWasCalled = false;
989 }
990
991 void setKeyCodeState(int32_t keyCode, int32_t state) {
992 mKeyCodeStates.replaceValueFor(keyCode, state);
993 }
994
995 void setScanCodeState(int32_t scanCode, int32_t state) {
996 mScanCodeStates.replaceValueFor(scanCode, state);
997 }
998
999 void setSwitchState(int32_t switchCode, int32_t state) {
1000 mSwitchStates.replaceValueFor(switchCode, state);
1001 }
1002
1003 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001004 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005 }
1006
1007private:
1008 virtual uint32_t getSources() {
1009 return mSources;
1010 }
1011
1012 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1013 InputMapper::populateDeviceInfo(deviceInfo);
1014
1015 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1016 deviceInfo->setKeyboardType(mKeyboardType);
1017 }
1018 }
1019
Arthur Hungc23540e2018-11-29 20:42:11 +08001020 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001021 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001023
1024 // Find the associated viewport if exist.
1025 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
1026 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1027 mViewport = config->getDisplayViewportByPort(*displayPort);
1028 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001029
1030 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 }
1032
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001033 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001034 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001035 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001036 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037 }
1038
1039 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001040 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041 mLastEvent = *rawEvent;
1042 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001043 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044 }
1045
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001046 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1048 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1049 }
1050
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001051 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1053 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1054 }
1055
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001056 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001057 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1058 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1059 }
1060
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001061 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062 const int32_t* keyCodes, uint8_t* outFlags) {
1063 bool result = false;
1064 for (size_t i = 0; i < numCodes; i++) {
1065 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1066 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1067 outFlags[i] = 1;
1068 result = true;
1069 }
1070 }
1071 }
1072 return result;
1073 }
1074
1075 virtual int32_t getMetaState() {
1076 return mMetaState;
1077 }
1078
1079 virtual void fadePointer() {
1080 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001081
1082 virtual std::optional<int32_t> getAssociatedDisplay() {
1083 if (mViewport) {
1084 return std::make_optional(mViewport->displayId);
1085 }
1086 return std::nullopt;
1087 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088};
1089
1090
1091// --- InstrumentedInputReader ---
1092
1093class InstrumentedInputReader : public InputReader {
1094 InputDevice* mNextDevice;
1095
1096public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001097 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1098 const sp<InputReaderPolicyInterface>& policy,
1099 const sp<InputListenerInterface>& listener)
1100 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101
1102 virtual ~InstrumentedInputReader() {
1103 if (mNextDevice) {
1104 delete mNextDevice;
1105 }
1106 }
1107
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001108 void setNextDevice(InputDevice* device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001110 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001111 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112 InputDeviceIdentifier identifier;
1113 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001114 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 int32_t generation = deviceId + 1;
1116 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001117 classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118 }
1119
Prabir Pradhan28efc192019-11-05 01:10:04 +00001120 // Make the protected loopOnce method accessible to tests.
1121 using InputReader::loopOnce;
1122
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123protected:
1124 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001125 const InputDeviceIdentifier& identifier,
1126 uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127 if (mNextDevice) {
1128 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001129 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 return device;
1131 }
1132 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1133 }
1134
1135 friend class InputReaderTest;
1136};
1137
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001138// --- InputReaderPolicyTest ---
1139class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001140protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001141 sp<FakeInputReaderPolicy> mFakePolicy;
1142
Prabir Pradhan28efc192019-11-05 01:10:04 +00001143 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1144 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001145};
1146
1147/**
1148 * Check that empty set of viewports is an acceptable configuration.
1149 * Also try to get internal viewport two different ways - by type and by uniqueId.
1150 *
1151 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1152 * Such configuration is not currently allowed.
1153 */
1154TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001155 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156
1157 // We didn't add any viewports yet, so there shouldn't be any.
1158 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001159 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001160 ASSERT_FALSE(internalViewport);
1161
1162 // Add an internal viewport, then clear it
1163 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001164 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001165
1166 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001167 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001169 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001170
1171 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001172 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001174 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001175
1176 mFakePolicy->clearViewports();
1177 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001178 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001180 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181 ASSERT_FALSE(internalViewport);
1182}
1183
1184TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1185 const std::string internalUniqueId = "local:0";
1186 const std::string externalUniqueId = "local:1";
1187 const std::string virtualUniqueId1 = "virtual:2";
1188 const std::string virtualUniqueId2 = "virtual:3";
1189 constexpr int32_t virtualDisplayId1 = 2;
1190 constexpr int32_t virtualDisplayId2 = 3;
1191
1192 // Add an internal viewport
1193 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001194 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001195 // Add an external viewport
1196 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001197 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001198 // Add an virtual viewport
1199 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001200 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001201 // Add another virtual viewport
1202 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001203 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001204
1205 // Check matching by type for internal
1206 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001207 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001208 ASSERT_TRUE(internalViewport);
1209 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1210
1211 // Check matching by type for external
1212 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001213 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214 ASSERT_TRUE(externalViewport);
1215 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1216
1217 // Check matching by uniqueId for virtual viewport #1
1218 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001219 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001220 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001221 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001222 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1223 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1224
1225 // Check matching by uniqueId for virtual viewport #2
1226 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001227 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001228 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001229 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001230 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1231 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1232}
1233
1234
1235/**
1236 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1237 * that lookup works by checking display id.
1238 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1239 */
1240TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1241 const std::string uniqueId1 = "uniqueId1";
1242 const std::string uniqueId2 = "uniqueId2";
1243 constexpr int32_t displayId1 = 2;
1244 constexpr int32_t displayId2 = 3;
1245
1246 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1247 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1248 for (const ViewportType& type : types) {
1249 mFakePolicy->clearViewports();
1250 // Add a viewport
1251 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001252 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001253 // Add another viewport
1254 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001255 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001256
1257 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001258 std::optional<DisplayViewport> viewport1 =
1259 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001260 ASSERT_TRUE(viewport1);
1261 ASSERT_EQ(displayId1, viewport1->displayId);
1262 ASSERT_EQ(type, viewport1->type);
1263
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001264 std::optional<DisplayViewport> viewport2 =
1265 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001266 ASSERT_TRUE(viewport2);
1267 ASSERT_EQ(displayId2, viewport2->displayId);
1268 ASSERT_EQ(type, viewport2->type);
1269
1270 // When there are multiple viewports of the same kind, and uniqueId is not specified
1271 // in the call to getDisplayViewport, then that situation is not supported.
1272 // The viewports can be stored in any order, so we cannot rely on the order, since that
1273 // is just implementation detail.
1274 // However, we can check that it still returns *a* viewport, we just cannot assert
1275 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001276 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001277 ASSERT_TRUE(someViewport);
1278 }
1279}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001281/**
1282 * Check getDisplayViewportByPort
1283 */
1284TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1285 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1286 const std::string uniqueId1 = "uniqueId1";
1287 const std::string uniqueId2 = "uniqueId2";
1288 constexpr int32_t displayId1 = 1;
1289 constexpr int32_t displayId2 = 2;
1290 const uint8_t hdmi1 = 0;
1291 const uint8_t hdmi2 = 1;
1292 const uint8_t hdmi3 = 2;
1293
1294 mFakePolicy->clearViewports();
1295 // Add a viewport that's associated with some display port that's not of interest.
1296 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1297 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1298 // Add another viewport, connected to HDMI1 port
1299 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1300 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1301
1302 // Check that correct display viewport was returned by comparing the display ports.
1303 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1304 ASSERT_TRUE(hdmi1Viewport);
1305 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1306 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1307
1308 // Check that we can still get the same viewport using the uniqueId
1309 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1310 ASSERT_TRUE(hdmi1Viewport);
1311 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1312 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1313 ASSERT_EQ(type, hdmi1Viewport->type);
1314
1315 // Check that we cannot find a port with "HDMI2", because we never added one
1316 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1317 ASSERT_FALSE(hdmi2Viewport);
1318}
1319
Michael Wrightd02c5b62014-02-10 15:10:22 -08001320// --- InputReaderTest ---
1321
1322class InputReaderTest : public testing::Test {
1323protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001324 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001326 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001327 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328
Prabir Pradhan28efc192019-11-05 01:10:04 +00001329 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001330 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001331 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001332 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001333
Prabir Pradhan28efc192019-11-05 01:10:04 +00001334 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1335 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001336 }
1337
Prabir Pradhan28efc192019-11-05 01:10:04 +00001338 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 mFakeListener.clear();
1340 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001341 }
1342
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001343 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001344 const PropertyMap* configuration) {
1345 mFakeEventHub->addDevice(deviceId, name, classes);
1346
1347 if (configuration) {
1348 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1349 }
1350 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001351 mReader->loopOnce();
1352 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001353 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1354 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001355 }
1356
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001357 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001358 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001359 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001360 }
1361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001362 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001363 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001364 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001365 }
1366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001367 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
1368 const std::string& name, uint32_t classes,
1369 uint32_t sources,
1370 const PropertyMap* configuration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001371 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001372 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373 mReader->setNextDevice(device);
1374 addDevice(deviceId, name, classes, configuration);
1375 return mapper;
1376 }
1377};
1378
1379TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001380 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001381 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001382 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001383 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001385 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 ASSERT_EQ(1U, inputDevices.size());
1388 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001389 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001390 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1391 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1392 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1393
1394 // Should also have received a notification describing the new input devices.
1395 inputDevices = mFakePolicy->getInputDevices();
1396 ASSERT_EQ(1U, inputDevices.size());
1397 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001398 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1400 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1401 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1402}
1403
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001404TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1405 constexpr int32_t deviceId = 1;
1406 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001407 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001408 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001409 device->addMapper<FakeInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001410 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001411 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001412
Yi Kong9b14ac62018-07-17 13:48:38 -07001413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001414
1415 NotifyDeviceResetArgs resetArgs;
1416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001417 ASSERT_EQ(deviceId, resetArgs.deviceId);
1418
1419 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001420 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001421 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001422
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001424 ASSERT_EQ(deviceId, resetArgs.deviceId);
1425 ASSERT_EQ(device->isEnabled(), false);
1426
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001427 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001428 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001431 ASSERT_EQ(device->isEnabled(), false);
1432
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001433 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001434 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001436 ASSERT_EQ(deviceId, resetArgs.deviceId);
1437 ASSERT_EQ(device->isEnabled(), true);
1438}
1439
Michael Wrightd02c5b62014-02-10 15:10:22 -08001440TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001441 FakeInputMapper& mapper =
1442 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1443 AINPUT_SOURCE_KEYBOARD, nullptr);
1444 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001445
1446 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1447 AINPUT_SOURCE_ANY, AKEYCODE_A))
1448 << "Should return unknown when the device id is >= 0 but unknown.";
1449
1450 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1451 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1452 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1453
1454 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1455 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1456 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1457
1458 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1459 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1460 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1461
1462 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1463 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1464 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1465}
1466
1467TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001468 FakeInputMapper& mapper =
1469 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1470 AINPUT_SOURCE_KEYBOARD, nullptr);
1471 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001472
1473 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1474 AINPUT_SOURCE_ANY, KEY_A))
1475 << "Should return unknown when the device id is >= 0 but unknown.";
1476
1477 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1478 AINPUT_SOURCE_TRACKBALL, KEY_A))
1479 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1480
1481 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1482 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1483 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1484
1485 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1486 AINPUT_SOURCE_TRACKBALL, KEY_A))
1487 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1488
1489 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1490 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1491 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1492}
1493
1494TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001495 FakeInputMapper& mapper =
1496 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1497 AINPUT_SOURCE_KEYBOARD, nullptr);
1498 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001499
1500 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1501 AINPUT_SOURCE_ANY, SW_LID))
1502 << "Should return unknown when the device id is >= 0 but unknown.";
1503
1504 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1505 AINPUT_SOURCE_TRACKBALL, SW_LID))
1506 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1507
1508 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1509 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1510 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1511
1512 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1513 AINPUT_SOURCE_TRACKBALL, SW_LID))
1514 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1515
1516 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1517 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1518 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1519}
1520
1521TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001522 FakeInputMapper& mapper =
1523 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1524 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001526 mapper.addSupportedKeyCode(AKEYCODE_A);
1527 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528
1529 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1530 uint8_t flags[4] = { 0, 0, 0, 1 };
1531
1532 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1533 << "Should return false when device id is >= 0 but unknown.";
1534 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1535
1536 flags[3] = 1;
1537 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1538 << "Should return false when device id is valid but the sources are not supported by the device.";
1539 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1540
1541 flags[3] = 1;
1542 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1543 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1544 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1545
1546 flags[3] = 1;
1547 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1548 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1549 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1550
1551 flags[3] = 1;
1552 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1553 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1554 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1555}
1556
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001557TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001558 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559
1560 NotifyConfigurationChangedArgs args;
1561
1562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1563 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1564}
1565
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001566TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001567 FakeInputMapper& mapper =
1568 addDeviceWithFakeInputMapper(1, 0, "fake", INPUT_DEVICE_CLASS_KEYBOARD,
1569 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570
1571 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001572 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1574
1575 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001576 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577 ASSERT_EQ(0, event.when);
1578 ASSERT_EQ(1, event.deviceId);
1579 ASSERT_EQ(EV_KEY, event.type);
1580 ASSERT_EQ(KEY_A, event.code);
1581 ASSERT_EQ(1, event.value);
1582}
1583
Prabir Pradhan42611e02018-11-27 14:04:02 -08001584TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1585 constexpr int32_t deviceId = 1;
1586 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001587 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001588 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001589 device->addMapper<FakeInputMapper>(AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001590 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001591 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001592
1593 NotifyDeviceResetArgs resetArgs;
1594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1595 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1596
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001597 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001598 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001600 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1601 prevSequenceNum = resetArgs.sequenceNum;
1602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001603 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001604 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001606 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1607 prevSequenceNum = resetArgs.sequenceNum;
1608
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001609 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001610 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001612 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1613 prevSequenceNum = resetArgs.sequenceNum;
1614}
1615
Arthur Hungc23540e2018-11-29 20:42:11 +08001616TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1617 constexpr int32_t deviceId = 1;
1618 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1619 const char* DEVICE_LOCATION = "USB1";
1620 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1621 DEVICE_LOCATION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001622 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001623 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001624
1625 const uint8_t hdmi1 = 1;
1626
1627 // Associated touch screen with second display.
1628 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1629
1630 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001631 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001632 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1633 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1634 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1635 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1636 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001637 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001638
1639 // Add the device, and make sure all of the callbacks are triggered.
1640 // The device is added after the input port associations are processed since
1641 // we do not yet support dynamic device-to-display associations.
1642 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001645 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001646
Arthur Hung2c9a3342019-07-23 14:18:59 +08001647 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001648 ASSERT_EQ(deviceId, device->getId());
1649 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1650 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001651
1652 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001653 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001654 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001655 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001656}
1657
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658
1659// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660class InputDeviceTest : public testing::Test {
1661protected:
1662 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001663 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 static const int32_t DEVICE_ID;
1665 static const int32_t DEVICE_GENERATION;
1666 static const int32_t DEVICE_CONTROLLER_NUMBER;
1667 static const uint32_t DEVICE_CLASSES;
1668
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001669 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001670 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001671 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672 FakeInputReaderContext* mFakeContext;
1673
1674 InputDevice* mDevice;
1675
Prabir Pradhan28efc192019-11-05 01:10:04 +00001676 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001677 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001678 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001679 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001680 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1681
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001682 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683 InputDeviceIdentifier identifier;
1684 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001685 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001686 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1687 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1688 }
1689
Prabir Pradhan28efc192019-11-05 01:10:04 +00001690 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001691 delete mDevice;
1692
1693 delete mFakeContext;
1694 mFakeListener.clear();
1695 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001696 }
1697};
1698
1699const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001700const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001701const int32_t InputDeviceTest::DEVICE_ID = 1;
1702const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1703const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1704const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1705 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1706
1707TEST_F(InputDeviceTest, ImmutableProperties) {
1708 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001709 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001710 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1711}
1712
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1714 ASSERT_EQ(mDevice->isEnabled(), true);
1715}
1716
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1718 // Configuration.
1719 InputReaderConfiguration config;
1720 mDevice->configure(ARBITRARY_TIME, &config, 0);
1721
1722 // Reset.
1723 mDevice->reset(ARBITRARY_TIME);
1724
1725 NotifyDeviceResetArgs resetArgs;
1726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1727 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1728 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1729
1730 // Metadata.
1731 ASSERT_TRUE(mDevice->isIgnored());
1732 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1733
1734 InputDeviceInfo info;
1735 mDevice->getDeviceInfo(&info);
1736 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001737 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1739 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1740
1741 // State queries.
1742 ASSERT_EQ(0, mDevice->getMetaState());
1743
1744 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1745 << "Ignored device should return unknown key code state.";
1746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1747 << "Ignored device should return unknown scan code state.";
1748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1749 << "Ignored device should return unknown switch state.";
1750
1751 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1752 uint8_t flags[2] = { 0, 1 };
1753 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1754 << "Ignored device should never mark any key codes.";
1755 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1756 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1757}
1758
1759TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1760 // Configuration.
1761 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1762
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001763 FakeInputMapper& mapper1 = mDevice->addMapper<FakeInputMapper>(AINPUT_SOURCE_KEYBOARD);
1764 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1765 mapper1.setMetaState(AMETA_ALT_ON);
1766 mapper1.addSupportedKeyCode(AKEYCODE_A);
1767 mapper1.addSupportedKeyCode(AKEYCODE_B);
1768 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1769 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1770 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
1771 mapper1.setScanCodeState(3, AKEY_STATE_UP);
1772 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001774 FakeInputMapper& mapper2 = mDevice->addMapper<FakeInputMapper>(AINPUT_SOURCE_TOUCHSCREEN);
1775 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001776
1777 InputReaderConfiguration config;
1778 mDevice->configure(ARBITRARY_TIME, &config, 0);
1779
1780 String8 propertyValue;
1781 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1782 << "Device should have read configuration during configuration phase.";
1783 ASSERT_STREQ("value", propertyValue.string());
1784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001785 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
1786 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001787
1788 // Reset
1789 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001790 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
1791 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001792
1793 NotifyDeviceResetArgs resetArgs;
1794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1795 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1796 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1797
1798 // Metadata.
1799 ASSERT_FALSE(mDevice->isIgnored());
1800 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1801
1802 InputDeviceInfo info;
1803 mDevice->getDeviceInfo(&info);
1804 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001805 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001806 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1807 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1808
1809 // State queries.
1810 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1811 << "Should query mappers and combine meta states.";
1812
1813 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1814 << "Should return unknown key code state when source not supported.";
1815 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1816 << "Should return unknown scan code state when source not supported.";
1817 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1818 << "Should return unknown switch state when source not supported.";
1819
1820 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1821 << "Should query mapper when source is supported.";
1822 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1823 << "Should query mapper when source is supported.";
1824 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1825 << "Should query mapper when source is supported.";
1826
1827 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1828 uint8_t flags[4] = { 0, 0, 0, 1 };
1829 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1830 << "Should do nothing when source is unsupported.";
1831 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1832 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1833 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1834 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1835
1836 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1837 << "Should query mapper when source is supported.";
1838 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1839 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1840 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1841 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1842
1843 // Event handling.
1844 RawEvent event;
1845 mDevice->process(&event, 1);
1846
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001847 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
1848 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849}
1850
Arthur Hung2c9a3342019-07-23 14:18:59 +08001851// A single input device is associated with a specific display. Check that:
1852// 1. Device is disabled if the viewport corresponding to the associated display is not found
1853// 2. Device is disabled when setEnabled API is called
1854TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 mDevice->addMapper<FakeInputMapper>(AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08001856
1857 // First Configuration.
1858 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1859
1860 // Device should be enabled by default.
1861 ASSERT_TRUE(mDevice->isEnabled());
1862
1863 // Prepare associated info.
1864 constexpr uint8_t hdmi = 1;
1865 const std::string UNIQUE_ID = "local:1";
1866
1867 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1868 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1869 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1870 // Device should be disabled because it is associated with a specific display via
1871 // input port <-> display port association, but the corresponding display is not found
1872 ASSERT_FALSE(mDevice->isEnabled());
1873
1874 // Prepare displays.
1875 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1876 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1877 ViewportType::VIEWPORT_INTERNAL);
1878 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1879 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1880 ASSERT_TRUE(mDevice->isEnabled());
1881
1882 // Device should be disabled after set disable.
1883 mFakePolicy->addDisabledDevice(mDevice->getId());
1884 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1885 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1886 ASSERT_FALSE(mDevice->isEnabled());
1887
1888 // Device should still be disabled even found the associated display.
1889 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1890 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1891 ASSERT_FALSE(mDevice->isEnabled());
1892}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001893
1894// --- InputMapperTest ---
1895
1896class InputMapperTest : public testing::Test {
1897protected:
1898 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001899 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900 static const int32_t DEVICE_ID;
1901 static const int32_t DEVICE_GENERATION;
1902 static const int32_t DEVICE_CONTROLLER_NUMBER;
1903 static const uint32_t DEVICE_CLASSES;
1904
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001905 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001906 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001907 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908 FakeInputReaderContext* mFakeContext;
1909 InputDevice* mDevice;
1910
Prabir Pradhan28efc192019-11-05 01:10:04 +00001911 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001912 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001913 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001914 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1916 InputDeviceIdentifier identifier;
1917 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001918 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001919 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1920 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1921
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001922 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923 }
1924
Prabir Pradhan28efc192019-11-05 01:10:04 +00001925 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926 delete mDevice;
1927 delete mFakeContext;
1928 mFakeListener.clear();
1929 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930 }
1931
1932 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001933 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001934 }
1935
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001936 void configureDevice(uint32_t changes) {
1937 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1938 }
1939
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001940 template <class T, typename... Args>
1941 T& addMapperAndConfigure(Args... args) {
1942 T& mapper = mDevice->addMapper<T>(args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001943 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001944 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001945 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001946 }
1947
1948 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001949 int32_t orientation, const std::string& uniqueId,
1950 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001951 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001952 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001953 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1954 }
1955
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001956 void clearViewports() {
1957 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001958 }
1959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001960 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
1961 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001962 RawEvent event;
1963 event.when = when;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001964 event.deviceId = mapper.getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001965 event.type = type;
1966 event.code = code;
1967 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001968 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001969 }
1970
1971 static void assertMotionRange(const InputDeviceInfo& info,
1972 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1973 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001974 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001975 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1976 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1977 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1978 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1979 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1980 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1981 }
1982
1983 static void assertPointerCoords(const PointerCoords& coords,
1984 float x, float y, float pressure, float size,
1985 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1986 float orientation, float distance) {
1987 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1988 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1989 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1990 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1991 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1992 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1993 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1994 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1995 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1996 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1997 }
1998
1999 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2000 float actualX, actualY;
2001 controller->getPosition(&actualX, &actualY);
2002 ASSERT_NEAR(x, actualX, 1);
2003 ASSERT_NEAR(y, actualY, 1);
2004 }
2005};
2006
2007const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002008const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002009const int32_t InputMapperTest::DEVICE_ID = 1;
2010const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2011const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2012const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2013
2014
2015// --- SwitchInputMapperTest ---
2016
2017class SwitchInputMapperTest : public InputMapperTest {
2018protected:
2019};
2020
2021TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002022 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002024 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002025}
2026
2027TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002028 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002029
2030 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002031 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032
2033 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002034 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002035}
2036
2037TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002038 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002040 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2041 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2042 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2043 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002044
2045 NotifySwitchArgs args;
2046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2047 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002048 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2049 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002050 args.switchMask);
2051 ASSERT_EQ(uint32_t(0), args.policyFlags);
2052}
2053
2054
2055// --- KeyboardInputMapperTest ---
2056
2057class KeyboardInputMapperTest : public InputMapperTest {
2058protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002059 const std::string UNIQUE_ID = "local:0";
2060
2061 void prepareDisplay(int32_t orientation);
2062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002063 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002064 int32_t originalKeyCode, int32_t rotatedKeyCode,
2065 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002066};
2067
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002068/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2069 * orientation.
2070 */
2071void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2072 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002073 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002074}
2075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002076void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002077 int32_t originalScanCode, int32_t originalKeyCode,
2078 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002079 NotifyKeyArgs args;
2080
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002081 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2083 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2084 ASSERT_EQ(originalScanCode, args.scanCode);
2085 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002086 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002087
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002088 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2091 ASSERT_EQ(originalScanCode, args.scanCode);
2092 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002093 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002094}
2095
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002097 KeyboardInputMapper& mapper =
2098 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2099 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002101 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002102}
2103
2104TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2105 const int32_t USAGE_A = 0x070004;
2106 const int32_t USAGE_UNKNOWN = 0x07ffff;
2107 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2108 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002110 KeyboardInputMapper& mapper =
2111 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2112 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002113
2114 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002115 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002116 NotifyKeyArgs args;
2117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2118 ASSERT_EQ(DEVICE_ID, args.deviceId);
2119 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2120 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2121 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2122 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2123 ASSERT_EQ(KEY_HOME, args.scanCode);
2124 ASSERT_EQ(AMETA_NONE, args.metaState);
2125 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2126 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2127 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2128
2129 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002130 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2132 ASSERT_EQ(DEVICE_ID, args.deviceId);
2133 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2134 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2135 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2136 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2137 ASSERT_EQ(KEY_HOME, args.scanCode);
2138 ASSERT_EQ(AMETA_NONE, args.metaState);
2139 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2140 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2141 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2142
2143 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002144 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2145 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2147 ASSERT_EQ(DEVICE_ID, args.deviceId);
2148 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2149 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2150 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2151 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2152 ASSERT_EQ(0, args.scanCode);
2153 ASSERT_EQ(AMETA_NONE, args.metaState);
2154 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2155 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2156 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2157
2158 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002159 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2160 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2162 ASSERT_EQ(DEVICE_ID, args.deviceId);
2163 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2164 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2165 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2166 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2167 ASSERT_EQ(0, args.scanCode);
2168 ASSERT_EQ(AMETA_NONE, args.metaState);
2169 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2170 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2171 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2172
2173 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002174 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2175 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2177 ASSERT_EQ(DEVICE_ID, args.deviceId);
2178 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2179 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2180 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2181 ASSERT_EQ(0, args.keyCode);
2182 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2183 ASSERT_EQ(AMETA_NONE, args.metaState);
2184 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2185 ASSERT_EQ(0U, args.policyFlags);
2186 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2187
2188 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002189 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2190 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2192 ASSERT_EQ(DEVICE_ID, args.deviceId);
2193 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2194 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2195 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2196 ASSERT_EQ(0, args.keyCode);
2197 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2198 ASSERT_EQ(AMETA_NONE, args.metaState);
2199 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2200 ASSERT_EQ(0U, args.policyFlags);
2201 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2202}
2203
2204TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2205 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2206 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2207
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002208 KeyboardInputMapper& mapper =
2209 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2210 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211
2212 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002213 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002214
2215 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002216 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002217 NotifyKeyArgs args;
2218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002221 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2222
2223 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002224 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2226 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002228
2229 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002230 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2232 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002233 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002234
2235 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002236 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2238 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002239 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2241}
2242
2243TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2244 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2245 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2246 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2247 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002249 KeyboardInputMapper& mapper =
2250 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2251 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002252
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002253 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002254 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2255 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2256 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2257 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2258 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2259 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2260 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2261 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2262}
2263
2264TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2265 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2266 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2267 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2268 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2269
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002271 KeyboardInputMapper& mapper =
2272 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2273 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002275 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002276 ASSERT_NO_FATAL_FAILURE(
2277 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2278 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2279 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2280 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2281 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2282 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2283 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002285 clearViewports();
2286 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002287 ASSERT_NO_FATAL_FAILURE(
2288 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2289 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2290 AKEYCODE_DPAD_UP, DISPLAY_ID));
2291 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2292 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2293 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2294 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002295
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002296 clearViewports();
2297 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002298 ASSERT_NO_FATAL_FAILURE(
2299 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2300 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2301 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2302 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2303 AKEYCODE_DPAD_UP, DISPLAY_ID));
2304 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2305 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002306
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002307 clearViewports();
2308 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002309 ASSERT_NO_FATAL_FAILURE(
2310 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2311 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2312 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2313 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2314 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2315 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2316 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002317
2318 // Special case: if orientation changes while key is down, we still emit the same keycode
2319 // in the key up as we did in the key down.
2320 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002321 clearViewports();
2322 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002323 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2325 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2326 ASSERT_EQ(KEY_UP, args.scanCode);
2327 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2328
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002329 clearViewports();
2330 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002331 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2333 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2334 ASSERT_EQ(KEY_UP, args.scanCode);
2335 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2336}
2337
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002338TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2339 // If the keyboard is not orientation aware,
2340 // key events should not be associated with a specific display id
2341 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2342
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002343 KeyboardInputMapper& mapper =
2344 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2345 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002346 NotifyKeyArgs args;
2347
2348 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002349 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002351 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2353 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2354
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002355 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002356 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002358 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2360 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2361}
2362
2363TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2364 // If the keyboard is orientation aware,
2365 // key events should be associated with the internal viewport
2366 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2367
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002368 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002369 KeyboardInputMapper& mapper =
2370 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2371 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002372 NotifyKeyArgs args;
2373
2374 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2375 // ^--- already checked by the previous test
2376
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002377 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002378 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002379 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002381 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2383 ASSERT_EQ(DISPLAY_ID, args.displayId);
2384
2385 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002386 clearViewports();
2387 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002388 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002389 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002391 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2393 ASSERT_EQ(newDisplayId, args.displayId);
2394}
2395
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002397 KeyboardInputMapper& mapper =
2398 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2399 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400
2401 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002402 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403
2404 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002405 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406}
2407
2408TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002409 KeyboardInputMapper& mapper =
2410 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2411 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412
2413 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002414 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415
2416 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002417 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418}
2419
2420TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002421 KeyboardInputMapper& mapper =
2422 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2423 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424
2425 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2426
2427 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2428 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002429 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 ASSERT_TRUE(flags[0]);
2431 ASSERT_FALSE(flags[1]);
2432}
2433
2434TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2435 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2436 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2437 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2438 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2439 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2440 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2441
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002442 KeyboardInputMapper& mapper =
2443 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2444 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445
2446 // Initialization should have turned all of the lights off.
2447 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2448 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2449 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2450
2451 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002452 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2453 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2455 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2456 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002457 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458
2459 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002460 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2461 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002462 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2463 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2464 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002465 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002466
2467 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002468 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2469 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2471 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2472 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002473 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474
2475 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002476 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2477 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2479 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2480 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002481 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002482
2483 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002484 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2485 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002486 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2487 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2488 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002489 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490
2491 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002492 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2493 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2495 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2496 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002497 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002498}
2499
Arthur Hung2c9a3342019-07-23 14:18:59 +08002500TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2501 // keyboard 1.
2502 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2503 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2504 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2505 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2506
2507 // keyboard 2.
2508 const std::string USB2 = "USB2";
2509 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2510 InputDeviceIdentifier identifier;
2511 identifier.name = "KEYBOARD2";
2512 identifier.location = USB2;
2513 std::unique_ptr<InputDevice> device2 =
2514 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2515 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2516 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2517 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2518 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2519 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2520 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2521
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002522 KeyboardInputMapper& mapper =
2523 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2524 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002526 KeyboardInputMapper& mapper2 =
2527 device2->addMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2528 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002529 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2530 device2->reset(ARBITRARY_TIME);
2531
2532 // Prepared displays and associated info.
2533 constexpr uint8_t hdmi1 = 0;
2534 constexpr uint8_t hdmi2 = 1;
2535 const std::string SECONDARY_UNIQUE_ID = "local:1";
2536
2537 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2538 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2539
2540 // No associated display viewport found, should disable the device.
2541 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2542 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2543 ASSERT_FALSE(device2->isEnabled());
2544
2545 // Prepare second display.
2546 constexpr int32_t newDisplayId = 2;
2547 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2548 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2549 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2550 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2551 // Default device will reconfigure above, need additional reconfiguration for another device.
2552 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2553 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2554
2555 // Device should be enabled after the associated display is found.
2556 ASSERT_TRUE(mDevice->isEnabled());
2557 ASSERT_TRUE(device2->isEnabled());
2558
2559 // Test pad key events
2560 ASSERT_NO_FATAL_FAILURE(
2561 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2562 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2563 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2564 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2565 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2567 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2568
2569 ASSERT_NO_FATAL_FAILURE(
2570 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2571 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2572 AKEYCODE_DPAD_RIGHT, newDisplayId));
2573 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2574 AKEYCODE_DPAD_DOWN, newDisplayId));
2575 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2576 AKEYCODE_DPAD_LEFT, newDisplayId));
2577}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002578
Powei Fengd041c5d2019-05-03 17:11:33 -07002579TEST_F(KeyboardInputMapperTest, ExternalDevice_WakeBehavior) {
2580 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2581 // marked as WAKE in the keylayout file to trigger wake.
2582 mDevice->setExternal(true);
2583
2584 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2585 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2586 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE, POLICY_FLAG_WAKE);
2587
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002588 KeyboardInputMapper& mapper =
2589 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2590 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002591
2592 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2593 NotifyKeyArgs args;
2594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2595 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2596
2597 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2599 ASSERT_EQ(uint32_t(0), args.policyFlags);
2600
2601 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2603 ASSERT_EQ(uint32_t(0), args.policyFlags);
2604
2605 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2607 ASSERT_EQ(uint32_t(0), args.policyFlags);
2608
2609 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2611 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2612
2613 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2615 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2616}
2617
2618TEST_F(KeyboardInputMapperTest, ExternalDevice_DoNotWakeByDefaultBehavior) {
2619 // Tv Remote key's wake behavior is prescribed by the keylayout file.
2620 mDevice->setExternal(true);
2621
2622 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2623 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2624 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
2625
Powei Fengd041c5d2019-05-03 17:11:33 -07002626 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002627 KeyboardInputMapper& mapper =
2628 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2629 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002630
2631 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2632 NotifyKeyArgs args;
2633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2634 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2635
2636 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2638 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2639
2640 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2642 ASSERT_EQ(uint32_t(0), args.policyFlags);
2643
2644 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2646 ASSERT_EQ(uint32_t(0), args.policyFlags);
2647
2648 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2650 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2651
2652 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2654 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2655}
2656
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657// --- CursorInputMapperTest ---
2658
2659class CursorInputMapperTest : public InputMapperTest {
2660protected:
2661 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2662
2663 sp<FakePointerController> mFakePointerController;
2664
Prabir Pradhan28efc192019-11-05 01:10:04 +00002665 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666 InputMapperTest::SetUp();
2667
2668 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002669 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 }
2671
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002672 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
2673 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002674
2675 void prepareDisplay(int32_t orientation) {
2676 const std::string uniqueId = "local:0";
2677 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2678 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2679 orientation, uniqueId, NO_PORT, viewportType);
2680 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681};
2682
2683const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2684
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002685void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
2686 int32_t originalY, int32_t rotatedX,
2687 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688 NotifyMotionArgs args;
2689
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002690 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2691 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2692 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2696 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2697 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2698 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2699}
2700
2701TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002703 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002705 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706}
2707
2708TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002710 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002712 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713}
2714
2715TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002717 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718
2719 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002720 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721
2722 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002723 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2724 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2726 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2727
2728 // When the bounds are set, then there should be a valid motion range.
2729 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2730
2731 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002732 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002733
2734 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2735 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2736 1, 800 - 1, 0.0f, 0.0f));
2737 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2738 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2739 2, 480 - 1, 0.0f, 0.0f));
2740 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2741 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2742 0.0f, 1.0f, 0.0f, 0.0f));
2743}
2744
2745TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002746 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002747 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748
2749 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002750 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751
2752 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2753 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2754 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2755 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2756 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2757 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2758 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2759 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2760 0.0f, 1.0f, 0.0f, 0.0f));
2761}
2762
2763TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002765 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766
2767 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2768
2769 NotifyMotionArgs args;
2770
2771 // Button press.
2772 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002773 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(ARBITRARY_TIME, args.eventTime);
2777 ASSERT_EQ(DEVICE_ID, args.deviceId);
2778 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2779 ASSERT_EQ(uint32_t(0), args.policyFlags);
2780 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2781 ASSERT_EQ(0, args.flags);
2782 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2783 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2784 ASSERT_EQ(0, args.edgeFlags);
2785 ASSERT_EQ(uint32_t(1), args.pointerCount);
2786 ASSERT_EQ(0, args.pointerProperties[0].id);
2787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2789 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2790 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2791 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2792 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2793
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2795 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2796 ASSERT_EQ(DEVICE_ID, args.deviceId);
2797 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2798 ASSERT_EQ(uint32_t(0), args.policyFlags);
2799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2800 ASSERT_EQ(0, args.flags);
2801 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2802 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2803 ASSERT_EQ(0, args.edgeFlags);
2804 ASSERT_EQ(uint32_t(1), args.pointerCount);
2805 ASSERT_EQ(0, args.pointerProperties[0].id);
2806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2808 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2809 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2810 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2811 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2812
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002814 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2815 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2817 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2818 ASSERT_EQ(DEVICE_ID, args.deviceId);
2819 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2820 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2822 ASSERT_EQ(0, args.flags);
2823 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2824 ASSERT_EQ(0, args.buttonState);
2825 ASSERT_EQ(0, args.edgeFlags);
2826 ASSERT_EQ(uint32_t(1), args.pointerCount);
2827 ASSERT_EQ(0, args.pointerProperties[0].id);
2828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2830 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2831 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2832 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2833 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2834
2835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2836 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2837 ASSERT_EQ(DEVICE_ID, args.deviceId);
2838 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2839 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2841 ASSERT_EQ(0, args.flags);
2842 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2843 ASSERT_EQ(0, args.buttonState);
2844 ASSERT_EQ(0, args.edgeFlags);
2845 ASSERT_EQ(uint32_t(1), args.pointerCount);
2846 ASSERT_EQ(0, args.pointerProperties[0].id);
2847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2849 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2850 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2851 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2852 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2853}
2854
2855TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002857 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858
2859 NotifyMotionArgs args;
2860
2861 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002862 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2863 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2867 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2868
2869 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002870 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2871 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2875 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2876}
2877
2878TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002880 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881
2882 NotifyMotionArgs args;
2883
2884 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002885 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2886 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2888 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2890 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2891
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2895 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2896
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002898 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2899 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2903 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2904
2905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002906 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2908 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2909}
2910
2911TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002913 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002914
2915 NotifyMotionArgs args;
2916
2917 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002918 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2919 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2920 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2921 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2925 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2926 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2927
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2929 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2931 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2932 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2933
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002935 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2936 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2937 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2941 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2942 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2943
2944 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002945 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2946 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002948 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2950 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2951
2952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002953 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2955 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2956}
2957
2958TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002960 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002962 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2964 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2965 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2966 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2967 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2968 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2969 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2970 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2971}
2972
2973TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 addConfigurationProperty("cursor.mode", "navigation");
2975 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002976 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002978 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2982 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2983 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2984 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2985 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2986 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2987
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002988 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2990 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2991 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2992 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2993 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2994 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2995 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2996 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2997
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002998 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3000 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3001 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3002 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3003 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3004 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3005 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3006 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3007
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003008 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3010 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3011 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3012 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3013 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3014 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3015 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3016 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3017}
3018
3019TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003021 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022
3023 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3024 mFakePointerController->setPosition(100, 200);
3025 mFakePointerController->setButtonState(0);
3026
3027 NotifyMotionArgs motionArgs;
3028 NotifyKeyArgs keyArgs;
3029
3030 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003031 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3032 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3034 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3035 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3036 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3038 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3039
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3041 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3042 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3043 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3045 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3046
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003047 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3048 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_EQ(0, motionArgs.buttonState);
3052 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3054 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3055
3056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003057 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 ASSERT_EQ(0, motionArgs.buttonState);
3059 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3061 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3062
3063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003065 ASSERT_EQ(0, motionArgs.buttonState);
3066 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3068 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3069
3070 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003071 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3072 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3073 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3075 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3076 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3077 motionArgs.buttonState);
3078 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3079 mFakePointerController->getButtonState());
3080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3081 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3082
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3085 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3086 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3087 mFakePointerController->getButtonState());
3088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3089 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3090
3091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3093 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3094 motionArgs.buttonState);
3095 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3096 mFakePointerController->getButtonState());
3097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3098 100.0f, 200.0f, 1.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_RIGHT, 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(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3105 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3107 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3108
3109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003111 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3112 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3114 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3115
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003116 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3117 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3120 ASSERT_EQ(0, motionArgs.buttonState);
3121 ASSERT_EQ(0, mFakePointerController->getButtonState());
3122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3123 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 -08003124 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3125 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003126
3127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 ASSERT_EQ(0, motionArgs.buttonState);
3129 ASSERT_EQ(0, mFakePointerController->getButtonState());
3130 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3131 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003133
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3135 ASSERT_EQ(0, motionArgs.buttonState);
3136 ASSERT_EQ(0, mFakePointerController->getButtonState());
3137 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3138 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
3141 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003142 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3143 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3146 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003147
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003149 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3151 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3153 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3154
3155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3157 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3158 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3160 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3161
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003162 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3163 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 ASSERT_EQ(0, motionArgs.buttonState);
3167 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3169 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3170
3171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003173 ASSERT_EQ(0, motionArgs.buttonState);
3174 ASSERT_EQ(0, mFakePointerController->getButtonState());
3175
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3177 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3179 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3180 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3181
3182 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003183 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3184 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3186 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3187 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003188
Michael Wrightd02c5b62014-02-10 15:10:22 -08003189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003190 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003191 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3192 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3194 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3195
3196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3198 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3199 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3201 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3202
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003203 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3204 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 ASSERT_EQ(0, motionArgs.buttonState);
3208 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3210 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 -08003211
3212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3214 ASSERT_EQ(0, motionArgs.buttonState);
3215 ASSERT_EQ(0, mFakePointerController->getButtonState());
3216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3217 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3218
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3220 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3221 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3222
3223 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003224 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3225 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3227 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3228 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003229
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3233 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3235 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3236
3237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3239 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3240 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3242 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3243
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003244 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3245 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248 ASSERT_EQ(0, motionArgs.buttonState);
3249 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3251 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 -08003252
3253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3255 ASSERT_EQ(0, motionArgs.buttonState);
3256 ASSERT_EQ(0, mFakePointerController->getButtonState());
3257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3258 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3259
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3261 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3262 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3263
3264 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003265 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3266 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3268 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3269 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003270
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3274 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3276 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3277
3278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3280 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3281 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3283 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3284
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003285 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3286 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 ASSERT_EQ(0, motionArgs.buttonState);
3290 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3292 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 -08003293
3294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3296 ASSERT_EQ(0, motionArgs.buttonState);
3297 ASSERT_EQ(0, mFakePointerController->getButtonState());
3298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3299 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3300
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3302 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3303 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3304}
3305
3306TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003308 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309
3310 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3311 mFakePointerController->setPosition(100, 200);
3312 mFakePointerController->setButtonState(0);
3313
3314 NotifyMotionArgs args;
3315
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003316 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3317 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3318 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003320 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3321 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3323 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3324 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3325}
3326
3327TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003328 addConfigurationProperty("cursor.mode", "pointer");
3329 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003330 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003331
3332 NotifyDeviceResetArgs resetArgs;
3333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3334 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3335 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3336
3337 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3338 mFakePointerController->setPosition(100, 200);
3339 mFakePointerController->setButtonState(0);
3340
3341 NotifyMotionArgs args;
3342
3343 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003344 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3345 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3346 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3348 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3351 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3352 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3353
3354 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003355 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3356 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3358 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3359 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3361 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3363 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3366 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3367
3368 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003369 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3370 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3372 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3373 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3375 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3377 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3378 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3380 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3381
3382 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003383 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3384 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3385 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3387 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3390 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3391 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3392
3393 // Disable pointer capture and check that the device generation got bumped
3394 // and events are generated the usual way.
3395 const uint32_t generation = mFakeContext->getGeneration();
3396 mFakePolicy->setPointerCapture(false);
3397 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3398 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3399
3400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3401 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3402 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3403
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003404 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3405 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3406 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3408 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3411 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3412 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3413}
3414
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003415TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003416 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003417
Garfield Tan888a6a42020-01-09 11:39:16 -08003418 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003419 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003420 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3421 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3422 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3423 ViewportType::VIEWPORT_EXTERNAL);
3424 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3425 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3426
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003427 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3428 mFakePointerController->setPosition(100, 200);
3429 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003430
3431 NotifyMotionArgs args;
3432 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3433 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3434 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3436 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3439 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3440 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3441 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3442}
3443
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444// --- TouchInputMapperTest ---
3445
3446class TouchInputMapperTest : public InputMapperTest {
3447protected:
3448 static const int32_t RAW_X_MIN;
3449 static const int32_t RAW_X_MAX;
3450 static const int32_t RAW_Y_MIN;
3451 static const int32_t RAW_Y_MAX;
3452 static const int32_t RAW_TOUCH_MIN;
3453 static const int32_t RAW_TOUCH_MAX;
3454 static const int32_t RAW_TOOL_MIN;
3455 static const int32_t RAW_TOOL_MAX;
3456 static const int32_t RAW_PRESSURE_MIN;
3457 static const int32_t RAW_PRESSURE_MAX;
3458 static const int32_t RAW_ORIENTATION_MIN;
3459 static const int32_t RAW_ORIENTATION_MAX;
3460 static const int32_t RAW_DISTANCE_MIN;
3461 static const int32_t RAW_DISTANCE_MAX;
3462 static const int32_t RAW_TILT_MIN;
3463 static const int32_t RAW_TILT_MAX;
3464 static const int32_t RAW_ID_MIN;
3465 static const int32_t RAW_ID_MAX;
3466 static const int32_t RAW_SLOT_MIN;
3467 static const int32_t RAW_SLOT_MAX;
3468 static const float X_PRECISION;
3469 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003470 static const float X_PRECISION_VIRTUAL;
3471 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472
3473 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003474 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475
3476 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3477
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003478 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003479 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003480
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481 enum Axes {
3482 POSITION = 1 << 0,
3483 TOUCH = 1 << 1,
3484 TOOL = 1 << 2,
3485 PRESSURE = 1 << 3,
3486 ORIENTATION = 1 << 4,
3487 MINOR = 1 << 5,
3488 ID = 1 << 6,
3489 DISTANCE = 1 << 7,
3490 TILT = 1 << 8,
3491 SLOT = 1 << 9,
3492 TOOL_TYPE = 1 << 10,
3493 };
3494
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003495 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3496 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003497 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003499 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500 int32_t toRawX(float displayX);
3501 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003502 float toCookedX(float rawX, float rawY);
3503 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003505 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003507 float toDisplayY(int32_t rawY, int32_t displayHeight);
3508
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509};
3510
3511const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3512const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3513const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3514const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3515const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3516const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3517const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3518const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003519const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3520const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3522const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3523const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3524const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3525const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3526const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3527const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3528const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3529const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3530const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3531const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3532const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003533const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3534 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3535const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3536 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003537const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3538 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
3540const float TouchInputMapperTest::GEOMETRIC_SCALE =
3541 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3542 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3543
3544const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3545 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3546 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3547};
3548
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003549void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003550 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003551 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3552}
3553
3554void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3555 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3556 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557}
3558
Santos Cordonfa5cf462017-04-05 10:37:00 -07003559void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003560 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3561 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003562 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003563}
3564
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565void TouchInputMapperTest::prepareVirtualKeys() {
3566 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3567 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3568 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3569 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3570}
3571
Jason Gerecke489fda82012-09-07 17:19:40 -07003572void TouchInputMapperTest::prepareLocationCalibration() {
3573 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3574}
3575
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576int32_t TouchInputMapperTest::toRawX(float displayX) {
3577 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3578}
3579
3580int32_t TouchInputMapperTest::toRawY(float displayY) {
3581 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3582}
3583
Jason Gerecke489fda82012-09-07 17:19:40 -07003584float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3585 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3586 return rawX;
3587}
3588
3589float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3590 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3591 return rawY;
3592}
3593
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003595 return toDisplayX(rawX, DISPLAY_WIDTH);
3596}
3597
3598float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3599 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600}
3601
3602float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003603 return toDisplayY(rawY, DISPLAY_HEIGHT);
3604}
3605
3606float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3607 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608}
3609
3610
3611// --- SingleTouchInputMapperTest ---
3612
3613class SingleTouchInputMapperTest : public TouchInputMapperTest {
3614protected:
3615 void prepareButtons();
3616 void prepareAxes(int axes);
3617
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003618 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3619 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3620 void processUp(SingleTouchInputMapper& mappery);
3621 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3622 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3623 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3624 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3625 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3626 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627};
3628
3629void SingleTouchInputMapperTest::prepareButtons() {
3630 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3631}
3632
3633void SingleTouchInputMapperTest::prepareAxes(int axes) {
3634 if (axes & POSITION) {
3635 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3636 RAW_X_MIN, RAW_X_MAX, 0, 0);
3637 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3638 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3639 }
3640 if (axes & PRESSURE) {
3641 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3642 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3643 }
3644 if (axes & TOOL) {
3645 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3646 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3647 }
3648 if (axes & DISTANCE) {
3649 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3650 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3651 }
3652 if (axes & TILT) {
3653 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3654 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3655 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3656 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3657 }
3658}
3659
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003660void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003661 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3662 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3663 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003664}
3665
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003666void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003667 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3668 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669}
3670
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003671void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003672 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673}
3674
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003675void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003676 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677}
3678
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003679void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
3680 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003681 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682}
3683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003684void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003685 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686}
3687
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
3689 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003690 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3691 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692}
3693
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003694void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
3695 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003696 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697}
3698
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003699void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003700 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701}
3702
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704 prepareButtons();
3705 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003706 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709}
3710
3711TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3713 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3714 prepareButtons();
3715 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003718 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719}
3720
3721TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722 prepareButtons();
3723 prepareAxes(POSITION);
3724 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728}
3729
3730TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731 prepareButtons();
3732 prepareAxes(POSITION);
3733 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737}
3738
3739TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740 addConfigurationProperty("touch.deviceType", "touchScreen");
3741 prepareDisplay(DISPLAY_ORIENTATION_0);
3742 prepareButtons();
3743 prepareAxes(POSITION);
3744 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003745 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746
3747 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749
3750 // Virtual key is down.
3751 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3752 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3753 processDown(mapper, x, y);
3754 processSync(mapper);
3755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003757 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758
3759 // Virtual key is up.
3760 processUp(mapper);
3761 processSync(mapper);
3762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765}
3766
3767TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768 addConfigurationProperty("touch.deviceType", "touchScreen");
3769 prepareDisplay(DISPLAY_ORIENTATION_0);
3770 prepareButtons();
3771 prepareAxes(POSITION);
3772 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003773 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774
3775 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777
3778 // Virtual key is down.
3779 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3780 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3781 processDown(mapper, x, y);
3782 processSync(mapper);
3783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
3787 // Virtual key is up.
3788 processUp(mapper);
3789 processSync(mapper);
3790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3791
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003792 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793}
3794
3795TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796 addConfigurationProperty("touch.deviceType", "touchScreen");
3797 prepareDisplay(DISPLAY_ORIENTATION_0);
3798 prepareButtons();
3799 prepareAxes(POSITION);
3800 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003801 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802
3803 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3804 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003805 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806 ASSERT_TRUE(flags[0]);
3807 ASSERT_FALSE(flags[1]);
3808}
3809
3810TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811 addConfigurationProperty("touch.deviceType", "touchScreen");
3812 prepareDisplay(DISPLAY_ORIENTATION_0);
3813 prepareButtons();
3814 prepareAxes(POSITION);
3815 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003816 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817
3818 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3819
3820 NotifyKeyArgs args;
3821
3822 // Press virtual key.
3823 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3824 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3825 processDown(mapper, x, y);
3826 processSync(mapper);
3827
3828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3829 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3830 ASSERT_EQ(DEVICE_ID, args.deviceId);
3831 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3832 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3833 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3834 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3835 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3836 ASSERT_EQ(KEY_HOME, args.scanCode);
3837 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3838 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3839
3840 // Release virtual key.
3841 processUp(mapper);
3842 processSync(mapper);
3843
3844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3845 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3846 ASSERT_EQ(DEVICE_ID, args.deviceId);
3847 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3848 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3849 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3850 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3851 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3852 ASSERT_EQ(KEY_HOME, args.scanCode);
3853 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3854 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3855
3856 // Should not have sent any motions.
3857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3858}
3859
3860TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861 addConfigurationProperty("touch.deviceType", "touchScreen");
3862 prepareDisplay(DISPLAY_ORIENTATION_0);
3863 prepareButtons();
3864 prepareAxes(POSITION);
3865 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867
3868 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3869
3870 NotifyKeyArgs keyArgs;
3871
3872 // Press virtual key.
3873 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3874 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3875 processDown(mapper, x, y);
3876 processSync(mapper);
3877
3878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3879 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3880 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3881 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3882 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3883 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3884 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3885 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3886 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3887 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3888 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3889
3890 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3891 // into the display area.
3892 y -= 100;
3893 processMove(mapper, x, y);
3894 processSync(mapper);
3895
3896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3897 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3898 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3899 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3900 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3901 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3902 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3903 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3904 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3905 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3906 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3907 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3908
3909 NotifyMotionArgs motionArgs;
3910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3911 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3912 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3913 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3914 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3915 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3916 ASSERT_EQ(0, motionArgs.flags);
3917 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3918 ASSERT_EQ(0, motionArgs.buttonState);
3919 ASSERT_EQ(0, motionArgs.edgeFlags);
3920 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3924 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3925 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3926 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3927 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3928
3929 // Keep moving out of bounds. Should generate a pointer move.
3930 y -= 50;
3931 processMove(mapper, x, y);
3932 processSync(mapper);
3933
3934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3936 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3937 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3938 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3940 ASSERT_EQ(0, motionArgs.flags);
3941 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3942 ASSERT_EQ(0, motionArgs.buttonState);
3943 ASSERT_EQ(0, motionArgs.edgeFlags);
3944 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3945 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3948 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3949 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3950 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3951 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3952
3953 // Release out of bounds. Should generate a pointer up.
3954 processUp(mapper);
3955 processSync(mapper);
3956
3957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3958 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3959 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3960 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3961 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3962 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3963 ASSERT_EQ(0, motionArgs.flags);
3964 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3965 ASSERT_EQ(0, motionArgs.buttonState);
3966 ASSERT_EQ(0, motionArgs.edgeFlags);
3967 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3968 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3971 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3972 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3973 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3974 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3975
3976 // Should not have sent any more keys or motions.
3977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3979}
3980
3981TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 addConfigurationProperty("touch.deviceType", "touchScreen");
3983 prepareDisplay(DISPLAY_ORIENTATION_0);
3984 prepareButtons();
3985 prepareAxes(POSITION);
3986 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003987 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988
3989 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3990
3991 NotifyMotionArgs motionArgs;
3992
3993 // Initially go down out of bounds.
3994 int32_t x = -10;
3995 int32_t y = -10;
3996 processDown(mapper, x, y);
3997 processSync(mapper);
3998
3999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4000
4001 // Move into the display area. Should generate a pointer down.
4002 x = 50;
4003 y = 75;
4004 processMove(mapper, x, y);
4005 processSync(mapper);
4006
4007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4008 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4009 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4010 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4011 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4013 ASSERT_EQ(0, motionArgs.flags);
4014 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4015 ASSERT_EQ(0, motionArgs.buttonState);
4016 ASSERT_EQ(0, motionArgs.edgeFlags);
4017 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4018 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4021 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4022 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4023 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4024 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4025
4026 // Release. Should generate a pointer up.
4027 processUp(mapper);
4028 processSync(mapper);
4029
4030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4031 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4032 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4033 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4034 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4035 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4036 ASSERT_EQ(0, motionArgs.flags);
4037 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4038 ASSERT_EQ(0, motionArgs.buttonState);
4039 ASSERT_EQ(0, motionArgs.edgeFlags);
4040 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4041 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4044 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4045 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4046 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4047 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4048
4049 // Should not have sent any more keys or motions.
4050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4052}
4053
Santos Cordonfa5cf462017-04-05 10:37:00 -07004054TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004055 addConfigurationProperty("touch.deviceType", "touchScreen");
4056 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4057
4058 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4059 prepareButtons();
4060 prepareAxes(POSITION);
4061 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004062 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004063
4064 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4065
4066 NotifyMotionArgs motionArgs;
4067
4068 // Down.
4069 int32_t x = 100;
4070 int32_t y = 125;
4071 processDown(mapper, x, y);
4072 processSync(mapper);
4073
4074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4075 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4076 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4077 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4078 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4079 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4081 ASSERT_EQ(0, motionArgs.flags);
4082 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4083 ASSERT_EQ(0, motionArgs.buttonState);
4084 ASSERT_EQ(0, motionArgs.edgeFlags);
4085 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4086 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4089 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4090 1, 0, 0, 0, 0, 0, 0, 0));
4091 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4092 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4094
4095 // Move.
4096 x += 50;
4097 y += 75;
4098 processMove(mapper, x, y);
4099 processSync(mapper);
4100
4101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4102 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4103 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4104 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4105 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4106 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4108 ASSERT_EQ(0, motionArgs.flags);
4109 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4110 ASSERT_EQ(0, motionArgs.buttonState);
4111 ASSERT_EQ(0, motionArgs.edgeFlags);
4112 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4113 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4116 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4117 1, 0, 0, 0, 0, 0, 0, 0));
4118 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4119 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4121
4122 // Up.
4123 processUp(mapper);
4124 processSync(mapper);
4125
4126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4128 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4129 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4130 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4131 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4132 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4133 ASSERT_EQ(0, motionArgs.flags);
4134 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4135 ASSERT_EQ(0, motionArgs.buttonState);
4136 ASSERT_EQ(0, motionArgs.edgeFlags);
4137 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4138 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4141 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4142 1, 0, 0, 0, 0, 0, 0, 0));
4143 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4144 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4145 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4146
4147 // Should not have sent any more keys or motions.
4148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4150}
4151
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153 addConfigurationProperty("touch.deviceType", "touchScreen");
4154 prepareDisplay(DISPLAY_ORIENTATION_0);
4155 prepareButtons();
4156 prepareAxes(POSITION);
4157 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004158 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159
4160 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4161
4162 NotifyMotionArgs motionArgs;
4163
4164 // Down.
4165 int32_t x = 100;
4166 int32_t y = 125;
4167 processDown(mapper, x, y);
4168 processSync(mapper);
4169
4170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4172 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4173 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4174 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4175 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4176 ASSERT_EQ(0, motionArgs.flags);
4177 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4178 ASSERT_EQ(0, motionArgs.buttonState);
4179 ASSERT_EQ(0, motionArgs.edgeFlags);
4180 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4181 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4184 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4185 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4186 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4188
4189 // Move.
4190 x += 50;
4191 y += 75;
4192 processMove(mapper, x, y);
4193 processSync(mapper);
4194
4195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4196 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4197 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4198 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4199 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4201 ASSERT_EQ(0, motionArgs.flags);
4202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4203 ASSERT_EQ(0, motionArgs.buttonState);
4204 ASSERT_EQ(0, motionArgs.edgeFlags);
4205 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4206 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4209 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4210 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4211 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4213
4214 // Up.
4215 processUp(mapper);
4216 processSync(mapper);
4217
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4219 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4220 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4221 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4222 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4223 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4224 ASSERT_EQ(0, motionArgs.flags);
4225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4226 ASSERT_EQ(0, motionArgs.buttonState);
4227 ASSERT_EQ(0, motionArgs.edgeFlags);
4228 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4229 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4232 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4233 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4234 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4235 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4236
4237 // Should not have sent any more keys or motions.
4238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4240}
4241
4242TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 addConfigurationProperty("touch.deviceType", "touchScreen");
4244 prepareButtons();
4245 prepareAxes(POSITION);
4246 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004247 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248
4249 NotifyMotionArgs args;
4250
4251 // Rotation 90.
4252 prepareDisplay(DISPLAY_ORIENTATION_90);
4253 processDown(mapper, toRawX(50), toRawY(75));
4254 processSync(mapper);
4255
4256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4257 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4258 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4259
4260 processUp(mapper);
4261 processSync(mapper);
4262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4263}
4264
4265TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 addConfigurationProperty("touch.deviceType", "touchScreen");
4267 prepareButtons();
4268 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004269 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
4271 NotifyMotionArgs args;
4272
4273 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004274 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 prepareDisplay(DISPLAY_ORIENTATION_0);
4276 processDown(mapper, toRawX(50), toRawY(75));
4277 processSync(mapper);
4278
4279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4280 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4281 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4282
4283 processUp(mapper);
4284 processSync(mapper);
4285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4286
4287 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004288 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 prepareDisplay(DISPLAY_ORIENTATION_90);
4290 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4291 processSync(mapper);
4292
4293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4294 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4295 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4296
4297 processUp(mapper);
4298 processSync(mapper);
4299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4300
4301 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004302 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 prepareDisplay(DISPLAY_ORIENTATION_180);
4304 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4305 processSync(mapper);
4306
4307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4308 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4309 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4310
4311 processUp(mapper);
4312 processSync(mapper);
4313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4314
4315 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004316 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 prepareDisplay(DISPLAY_ORIENTATION_270);
4318 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4319 processSync(mapper);
4320
4321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4322 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4323 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4324
4325 processUp(mapper);
4326 processSync(mapper);
4327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4328}
4329
4330TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 addConfigurationProperty("touch.deviceType", "touchScreen");
4332 prepareDisplay(DISPLAY_ORIENTATION_0);
4333 prepareButtons();
4334 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004335 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336
4337 // These calculations are based on the input device calibration documentation.
4338 int32_t rawX = 100;
4339 int32_t rawY = 200;
4340 int32_t rawPressure = 10;
4341 int32_t rawToolMajor = 12;
4342 int32_t rawDistance = 2;
4343 int32_t rawTiltX = 30;
4344 int32_t rawTiltY = 110;
4345
4346 float x = toDisplayX(rawX);
4347 float y = toDisplayY(rawY);
4348 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4349 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4350 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4351 float distance = float(rawDistance);
4352
4353 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4354 float tiltScale = M_PI / 180;
4355 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4356 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4357 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4358 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4359
4360 processDown(mapper, rawX, rawY);
4361 processPressure(mapper, rawPressure);
4362 processToolMajor(mapper, rawToolMajor);
4363 processDistance(mapper, rawDistance);
4364 processTilt(mapper, rawTiltX, rawTiltY);
4365 processSync(mapper);
4366
4367 NotifyMotionArgs args;
4368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4370 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4371 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4372}
4373
Jason Gerecke489fda82012-09-07 17:19:40 -07004374TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004375 addConfigurationProperty("touch.deviceType", "touchScreen");
4376 prepareDisplay(DISPLAY_ORIENTATION_0);
4377 prepareLocationCalibration();
4378 prepareButtons();
4379 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004380 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004381
4382 int32_t rawX = 100;
4383 int32_t rawY = 200;
4384
4385 float x = toDisplayX(toCookedX(rawX, rawY));
4386 float y = toDisplayY(toCookedY(rawX, rawY));
4387
4388 processDown(mapper, rawX, rawY);
4389 processSync(mapper);
4390
4391 NotifyMotionArgs args;
4392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4394 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4395}
4396
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 addConfigurationProperty("touch.deviceType", "touchScreen");
4399 prepareDisplay(DISPLAY_ORIENTATION_0);
4400 prepareButtons();
4401 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004402 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
4404 NotifyMotionArgs motionArgs;
4405 NotifyKeyArgs keyArgs;
4406
4407 processDown(mapper, 100, 200);
4408 processSync(mapper);
4409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4410 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4411 ASSERT_EQ(0, motionArgs.buttonState);
4412
4413 // press BTN_LEFT, release BTN_LEFT
4414 processKey(mapper, BTN_LEFT, 1);
4415 processSync(mapper);
4416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4418 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4419
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4422 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4423
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 processKey(mapper, BTN_LEFT, 0);
4425 processSync(mapper);
4426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004429
4430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433
4434 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4435 processKey(mapper, BTN_RIGHT, 1);
4436 processKey(mapper, BTN_MIDDLE, 1);
4437 processSync(mapper);
4438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4440 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4441 motionArgs.buttonState);
4442
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4444 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4445 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4446
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4449 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4450 motionArgs.buttonState);
4451
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 processKey(mapper, BTN_RIGHT, 0);
4453 processSync(mapper);
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004460 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461
4462 processKey(mapper, BTN_MIDDLE, 0);
4463 processSync(mapper);
4464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471
4472 // press BTN_BACK, release BTN_BACK
4473 processKey(mapper, BTN_BACK, 1);
4474 processSync(mapper);
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4476 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4477 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004478
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004481 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4482
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4485 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486
4487 processKey(mapper, BTN_BACK, 0);
4488 processSync(mapper);
4489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004495 ASSERT_EQ(0, motionArgs.buttonState);
4496
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4498 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4499 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4500
4501 // press BTN_SIDE, release BTN_SIDE
4502 processKey(mapper, BTN_SIDE, 1);
4503 processSync(mapper);
4504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4505 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4506 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004507
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004510 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4511
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4513 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4514 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515
4516 processKey(mapper, BTN_SIDE, 0);
4517 processSync(mapper);
4518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004519 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004521
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004524 ASSERT_EQ(0, motionArgs.buttonState);
4525
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4527 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4528 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4529
4530 // press BTN_FORWARD, release BTN_FORWARD
4531 processKey(mapper, BTN_FORWARD, 1);
4532 processSync(mapper);
4533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4534 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4535 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004536
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004539 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4540
4541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4542 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4543 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544
4545 processKey(mapper, BTN_FORWARD, 0);
4546 processSync(mapper);
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004548 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004550
4551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004553 ASSERT_EQ(0, motionArgs.buttonState);
4554
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4556 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4557 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4558
4559 // press BTN_EXTRA, release BTN_EXTRA
4560 processKey(mapper, BTN_EXTRA, 1);
4561 processSync(mapper);
4562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4563 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4564 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004565
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004568 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4569
4570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4571 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4572 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573
4574 processKey(mapper, BTN_EXTRA, 0);
4575 processSync(mapper);
4576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004577 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004582 ASSERT_EQ(0, motionArgs.buttonState);
4583
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4585 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4586 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4587
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4589
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 // press BTN_STYLUS, release BTN_STYLUS
4591 processKey(mapper, BTN_STYLUS, 1);
4592 processSync(mapper);
4593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4596
4597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4598 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4599 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600
4601 processKey(mapper, BTN_STYLUS, 0);
4602 processSync(mapper);
4603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004606
4607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610
4611 // press BTN_STYLUS2, release BTN_STYLUS2
4612 processKey(mapper, BTN_STYLUS2, 1);
4613 processSync(mapper);
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004616 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4617
4618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4620 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
4622 processKey(mapper, BTN_STYLUS2, 0);
4623 processSync(mapper);
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631
4632 // release touch
4633 processUp(mapper);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4637 ASSERT_EQ(0, motionArgs.buttonState);
4638}
4639
4640TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 addConfigurationProperty("touch.deviceType", "touchScreen");
4642 prepareDisplay(DISPLAY_ORIENTATION_0);
4643 prepareButtons();
4644 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004645 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646
4647 NotifyMotionArgs motionArgs;
4648
4649 // default tool type is finger
4650 processDown(mapper, 100, 200);
4651 processSync(mapper);
4652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4653 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4655
4656 // eraser
4657 processKey(mapper, BTN_TOOL_RUBBER, 1);
4658 processSync(mapper);
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4662
4663 // stylus
4664 processKey(mapper, BTN_TOOL_RUBBER, 0);
4665 processKey(mapper, BTN_TOOL_PEN, 1);
4666 processSync(mapper);
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4670
4671 // brush
4672 processKey(mapper, BTN_TOOL_PEN, 0);
4673 processKey(mapper, BTN_TOOL_BRUSH, 1);
4674 processSync(mapper);
4675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4678
4679 // pencil
4680 processKey(mapper, BTN_TOOL_BRUSH, 0);
4681 processKey(mapper, BTN_TOOL_PENCIL, 1);
4682 processSync(mapper);
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4686
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004687 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 processKey(mapper, BTN_TOOL_PENCIL, 0);
4689 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4690 processSync(mapper);
4691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4694
4695 // mouse
4696 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4697 processKey(mapper, BTN_TOOL_MOUSE, 1);
4698 processSync(mapper);
4699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4702
4703 // lens
4704 processKey(mapper, BTN_TOOL_MOUSE, 0);
4705 processKey(mapper, BTN_TOOL_LENS, 1);
4706 processSync(mapper);
4707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4710
4711 // double-tap
4712 processKey(mapper, BTN_TOOL_LENS, 0);
4713 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4714 processSync(mapper);
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4718
4719 // triple-tap
4720 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4721 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4722 processSync(mapper);
4723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4726
4727 // quad-tap
4728 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4729 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4730 processSync(mapper);
4731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4734
4735 // finger
4736 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4737 processKey(mapper, BTN_TOOL_FINGER, 1);
4738 processSync(mapper);
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4742
4743 // stylus trumps finger
4744 processKey(mapper, BTN_TOOL_PEN, 1);
4745 processSync(mapper);
4746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4749
4750 // eraser trumps stylus
4751 processKey(mapper, BTN_TOOL_RUBBER, 1);
4752 processSync(mapper);
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4756
4757 // mouse trumps eraser
4758 processKey(mapper, BTN_TOOL_MOUSE, 1);
4759 processSync(mapper);
4760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4763
4764 // back to default tool type
4765 processKey(mapper, BTN_TOOL_MOUSE, 0);
4766 processKey(mapper, BTN_TOOL_RUBBER, 0);
4767 processKey(mapper, BTN_TOOL_PEN, 0);
4768 processKey(mapper, BTN_TOOL_FINGER, 0);
4769 processSync(mapper);
4770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4773}
4774
4775TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 addConfigurationProperty("touch.deviceType", "touchScreen");
4777 prepareDisplay(DISPLAY_ORIENTATION_0);
4778 prepareButtons();
4779 prepareAxes(POSITION);
4780 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004781 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782
4783 NotifyMotionArgs motionArgs;
4784
4785 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4786 processKey(mapper, BTN_TOOL_FINGER, 1);
4787 processMove(mapper, 100, 200);
4788 processSync(mapper);
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4792 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4793
4794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4795 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4797 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4798
4799 // move a little
4800 processMove(mapper, 150, 250);
4801 processSync(mapper);
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4803 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4805 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4806
4807 // down when BTN_TOUCH is pressed, pressure defaults to 1
4808 processKey(mapper, BTN_TOUCH, 1);
4809 processSync(mapper);
4810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4811 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4813 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4814
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4818 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4819
4820 // up when BTN_TOUCH is released, hover restored
4821 processKey(mapper, BTN_TOUCH, 0);
4822 processSync(mapper);
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4824 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4826 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4827
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4829 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4831 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4832
4833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4836 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4837
4838 // exit hover when pointer goes away
4839 processKey(mapper, BTN_TOOL_FINGER, 0);
4840 processSync(mapper);
4841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4842 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4844 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4845}
4846
4847TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 addConfigurationProperty("touch.deviceType", "touchScreen");
4849 prepareDisplay(DISPLAY_ORIENTATION_0);
4850 prepareButtons();
4851 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004852 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853
4854 NotifyMotionArgs motionArgs;
4855
4856 // initially hovering because pressure is 0
4857 processDown(mapper, 100, 200);
4858 processPressure(mapper, 0);
4859 processSync(mapper);
4860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4861 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4863 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4864
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4866 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4868 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4869
4870 // move a little
4871 processMove(mapper, 150, 250);
4872 processSync(mapper);
4873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4876 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4877
4878 // down when pressure is non-zero
4879 processPressure(mapper, RAW_PRESSURE_MAX);
4880 processSync(mapper);
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4882 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4884 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4885
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4887 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4889 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4890
4891 // up when pressure becomes 0, hover restored
4892 processPressure(mapper, 0);
4893 processSync(mapper);
4894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4895 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4897 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4898
4899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4900 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4902 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4903
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4907 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4908
4909 // exit hover when pointer goes away
4910 processUp(mapper);
4911 processSync(mapper);
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4913 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4915 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4916}
4917
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918// --- MultiTouchInputMapperTest ---
4919
4920class MultiTouchInputMapperTest : public TouchInputMapperTest {
4921protected:
4922 void prepareAxes(int axes);
4923
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004924 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
4925 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
4926 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
4927 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
4928 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
4929 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
4930 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
4931 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
4932 void processId(MultiTouchInputMapper& mapper, int32_t id);
4933 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
4934 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
4935 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
4936 void processMTSync(MultiTouchInputMapper& mapper);
4937 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938};
4939
4940void MultiTouchInputMapperTest::prepareAxes(int axes) {
4941 if (axes & POSITION) {
4942 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4943 RAW_X_MIN, RAW_X_MAX, 0, 0);
4944 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4945 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4946 }
4947 if (axes & TOUCH) {
4948 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4949 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4950 if (axes & MINOR) {
4951 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4952 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4953 }
4954 }
4955 if (axes & TOOL) {
4956 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4957 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4958 if (axes & MINOR) {
4959 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4960 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4961 }
4962 }
4963 if (axes & ORIENTATION) {
4964 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4965 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4966 }
4967 if (axes & PRESSURE) {
4968 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4969 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4970 }
4971 if (axes & DISTANCE) {
4972 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4973 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4974 }
4975 if (axes & ID) {
4976 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4977 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4978 }
4979 if (axes & SLOT) {
4980 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4981 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4982 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4983 }
4984 if (axes & TOOL_TYPE) {
4985 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4986 0, MT_TOOL_MAX, 0, 0);
4987 }
4988}
4989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004990void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
4991 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004992 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4993 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994}
4995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004996void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
4997 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004998 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999}
5000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005001void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5002 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005003 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004}
5005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005006void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005007 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008}
5009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005010void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005011 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012}
5013
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005014void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5015 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005016 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017}
5018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005020 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021}
5022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005023void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005024 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025}
5026
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005027void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005028 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029}
5030
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005031void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005032 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033}
5034
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005035void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005036 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037}
5038
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005039void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5040 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005041 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042}
5043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005044void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005045 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046}
5047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005048void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005049 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050}
5051
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053 addConfigurationProperty("touch.deviceType", "touchScreen");
5054 prepareDisplay(DISPLAY_ORIENTATION_0);
5055 prepareAxes(POSITION);
5056 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005057 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058
5059 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5060
5061 NotifyMotionArgs motionArgs;
5062
5063 // Two fingers down at once.
5064 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5065 processPosition(mapper, x1, y1);
5066 processMTSync(mapper);
5067 processPosition(mapper, x2, y2);
5068 processMTSync(mapper);
5069 processSync(mapper);
5070
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5072 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5073 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5074 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5075 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5076 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5077 ASSERT_EQ(0, motionArgs.flags);
5078 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5079 ASSERT_EQ(0, motionArgs.buttonState);
5080 ASSERT_EQ(0, motionArgs.edgeFlags);
5081 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5082 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5085 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5086 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5087 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5088 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5089
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5091 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5092 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5093 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5094 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5095 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5096 motionArgs.action);
5097 ASSERT_EQ(0, motionArgs.flags);
5098 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5099 ASSERT_EQ(0, motionArgs.buttonState);
5100 ASSERT_EQ(0, motionArgs.edgeFlags);
5101 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5102 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5104 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5107 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5109 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5110 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5111 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5112 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5113
5114 // Move.
5115 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5116 processPosition(mapper, x1, y1);
5117 processMTSync(mapper);
5118 processPosition(mapper, x2, y2);
5119 processMTSync(mapper);
5120 processSync(mapper);
5121
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5123 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5124 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5125 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5126 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5128 ASSERT_EQ(0, motionArgs.flags);
5129 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5130 ASSERT_EQ(0, motionArgs.buttonState);
5131 ASSERT_EQ(0, motionArgs.edgeFlags);
5132 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5133 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5135 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5138 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5140 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5141 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5142 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5144
5145 // First finger up.
5146 x2 += 15; y2 -= 20;
5147 processPosition(mapper, x2, y2);
5148 processMTSync(mapper);
5149 processSync(mapper);
5150
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5153 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5154 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5155 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5156 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5157 motionArgs.action);
5158 ASSERT_EQ(0, motionArgs.flags);
5159 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5160 ASSERT_EQ(0, motionArgs.buttonState);
5161 ASSERT_EQ(0, motionArgs.edgeFlags);
5162 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5163 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5165 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5168 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5170 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5171 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5172 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5173 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5174
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5176 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5177 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5178 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5179 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5181 ASSERT_EQ(0, motionArgs.flags);
5182 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5183 ASSERT_EQ(0, motionArgs.buttonState);
5184 ASSERT_EQ(0, motionArgs.edgeFlags);
5185 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5186 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5189 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5190 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5191 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5192 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5193
5194 // Move.
5195 x2 += 20; y2 -= 25;
5196 processPosition(mapper, x2, y2);
5197 processMTSync(mapper);
5198 processSync(mapper);
5199
5200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5201 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5202 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5203 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5204 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5206 ASSERT_EQ(0, motionArgs.flags);
5207 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5208 ASSERT_EQ(0, motionArgs.buttonState);
5209 ASSERT_EQ(0, motionArgs.edgeFlags);
5210 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5211 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5214 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5215 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5216 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5218
5219 // New finger down.
5220 int32_t x3 = 700, y3 = 300;
5221 processPosition(mapper, x2, y2);
5222 processMTSync(mapper);
5223 processPosition(mapper, x3, y3);
5224 processMTSync(mapper);
5225 processSync(mapper);
5226
5227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5229 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5230 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5231 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5232 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5233 motionArgs.action);
5234 ASSERT_EQ(0, motionArgs.flags);
5235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5236 ASSERT_EQ(0, motionArgs.buttonState);
5237 ASSERT_EQ(0, motionArgs.edgeFlags);
5238 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5241 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5244 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5246 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5247 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5248 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5250
5251 // Second finger up.
5252 x3 += 30; y3 -= 20;
5253 processPosition(mapper, x3, y3);
5254 processMTSync(mapper);
5255 processSync(mapper);
5256
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5259 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5261 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5262 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5263 motionArgs.action);
5264 ASSERT_EQ(0, motionArgs.flags);
5265 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5266 ASSERT_EQ(0, motionArgs.buttonState);
5267 ASSERT_EQ(0, motionArgs.edgeFlags);
5268 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5269 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5271 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5274 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5276 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5277 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5278 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5280
5281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5282 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5283 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5284 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5285 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5287 ASSERT_EQ(0, motionArgs.flags);
5288 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5289 ASSERT_EQ(0, motionArgs.buttonState);
5290 ASSERT_EQ(0, motionArgs.edgeFlags);
5291 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5292 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5295 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5296 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5297 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5298 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5299
5300 // Last finger up.
5301 processMTSync(mapper);
5302 processSync(mapper);
5303
5304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5305 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5306 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5307 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5308 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5309 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5310 ASSERT_EQ(0, motionArgs.flags);
5311 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5312 ASSERT_EQ(0, motionArgs.buttonState);
5313 ASSERT_EQ(0, motionArgs.edgeFlags);
5314 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5315 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5318 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5319 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5320 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5322
5323 // Should not have sent any more keys or motions.
5324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5326}
5327
5328TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329 addConfigurationProperty("touch.deviceType", "touchScreen");
5330 prepareDisplay(DISPLAY_ORIENTATION_0);
5331 prepareAxes(POSITION | ID);
5332 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005333 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334
5335 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5336
5337 NotifyMotionArgs motionArgs;
5338
5339 // Two fingers down at once.
5340 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5341 processPosition(mapper, x1, y1);
5342 processId(mapper, 1);
5343 processMTSync(mapper);
5344 processPosition(mapper, x2, y2);
5345 processId(mapper, 2);
5346 processMTSync(mapper);
5347 processSync(mapper);
5348
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5350 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5351 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5352 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5355 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5356
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5358 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5359 motionArgs.action);
5360 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5361 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5363 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5366 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5368 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5369
5370 // Move.
5371 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5372 processPosition(mapper, x1, y1);
5373 processId(mapper, 1);
5374 processMTSync(mapper);
5375 processPosition(mapper, x2, y2);
5376 processId(mapper, 2);
5377 processMTSync(mapper);
5378 processSync(mapper);
5379
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5382 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5383 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5385 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5388 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5390 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5391
5392 // First finger up.
5393 x2 += 15; y2 -= 20;
5394 processPosition(mapper, x2, y2);
5395 processId(mapper, 2);
5396 processMTSync(mapper);
5397 processSync(mapper);
5398
5399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5400 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5401 motionArgs.action);
5402 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5403 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5405 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5408 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5410 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5411
5412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5414 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5415 ASSERT_EQ(1, 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(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5419
5420 // Move.
5421 x2 += 20; y2 -= 25;
5422 processPosition(mapper, x2, y2);
5423 processId(mapper, 2);
5424 processMTSync(mapper);
5425 processSync(mapper);
5426
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5429 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5430 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5433 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5434
5435 // New finger down.
5436 int32_t x3 = 700, y3 = 300;
5437 processPosition(mapper, x2, y2);
5438 processId(mapper, 2);
5439 processMTSync(mapper);
5440 processPosition(mapper, x3, y3);
5441 processId(mapper, 3);
5442 processMTSync(mapper);
5443 processSync(mapper);
5444
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5446 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5447 motionArgs.action);
5448 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5449 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5451 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5454 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5456 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5457
5458 // Second finger up.
5459 x3 += 30; y3 -= 20;
5460 processPosition(mapper, x3, y3);
5461 processId(mapper, 3);
5462 processMTSync(mapper);
5463 processSync(mapper);
5464
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5466 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5467 motionArgs.action);
5468 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5469 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5471 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5474 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5476 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5477
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5480 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5484 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5485
5486 // Last finger up.
5487 processMTSync(mapper);
5488 processSync(mapper);
5489
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5491 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5496 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5497
5498 // Should not have sent any more keys or motions.
5499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5501}
5502
5503TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504 addConfigurationProperty("touch.deviceType", "touchScreen");
5505 prepareDisplay(DISPLAY_ORIENTATION_0);
5506 prepareAxes(POSITION | ID | SLOT);
5507 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005508 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005509
5510 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5511
5512 NotifyMotionArgs motionArgs;
5513
5514 // Two fingers down at once.
5515 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5516 processPosition(mapper, x1, y1);
5517 processId(mapper, 1);
5518 processSlot(mapper, 1);
5519 processPosition(mapper, x2, y2);
5520 processId(mapper, 2);
5521 processSync(mapper);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5525 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5526 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5529 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5530
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5532 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5533 motionArgs.action);
5534 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5535 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5537 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5540 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5542 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5543
5544 // Move.
5545 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5546 processSlot(mapper, 0);
5547 processPosition(mapper, x1, y1);
5548 processSlot(mapper, 1);
5549 processPosition(mapper, x2, y2);
5550 processSync(mapper);
5551
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5554 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5557 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5560 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5563
5564 // First finger up.
5565 x2 += 15; y2 -= 20;
5566 processSlot(mapper, 0);
5567 processId(mapper, -1);
5568 processSlot(mapper, 1);
5569 processPosition(mapper, x2, y2);
5570 processSync(mapper);
5571
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5574 motionArgs.action);
5575 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5576 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5578 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5581 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5583 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5584
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5587 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5588 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5591 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5592
5593 // Move.
5594 x2 += 20; y2 -= 25;
5595 processPosition(mapper, x2, y2);
5596 processSync(mapper);
5597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5601 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5605
5606 // New finger down.
5607 int32_t x3 = 700, y3 = 300;
5608 processPosition(mapper, x2, y2);
5609 processSlot(mapper, 0);
5610 processId(mapper, 3);
5611 processPosition(mapper, x3, y3);
5612 processSync(mapper);
5613
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5616 motionArgs.action);
5617 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5618 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5620 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5623 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5625 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5626
5627 // Second finger up.
5628 x3 += 30; y3 -= 20;
5629 processSlot(mapper, 1);
5630 processId(mapper, -1);
5631 processSlot(mapper, 0);
5632 processPosition(mapper, x3, y3);
5633 processSync(mapper);
5634
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5636 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5637 motionArgs.action);
5638 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5639 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5641 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5644 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5646 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5647
5648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5650 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5654 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5655
5656 // Last finger up.
5657 processId(mapper, -1);
5658 processSync(mapper);
5659
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5661 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5662 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5663 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5666 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5667
5668 // Should not have sent any more keys or motions.
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5671}
5672
5673TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 addConfigurationProperty("touch.deviceType", "touchScreen");
5675 prepareDisplay(DISPLAY_ORIENTATION_0);
5676 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005677 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678
5679 // These calculations are based on the input device calibration documentation.
5680 int32_t rawX = 100;
5681 int32_t rawY = 200;
5682 int32_t rawTouchMajor = 7;
5683 int32_t rawTouchMinor = 6;
5684 int32_t rawToolMajor = 9;
5685 int32_t rawToolMinor = 8;
5686 int32_t rawPressure = 11;
5687 int32_t rawDistance = 0;
5688 int32_t rawOrientation = 3;
5689 int32_t id = 5;
5690
5691 float x = toDisplayX(rawX);
5692 float y = toDisplayY(rawY);
5693 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5694 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5695 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5696 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5697 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5698 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5699 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5700 float distance = float(rawDistance);
5701
5702 processPosition(mapper, rawX, rawY);
5703 processTouchMajor(mapper, rawTouchMajor);
5704 processTouchMinor(mapper, rawTouchMinor);
5705 processToolMajor(mapper, rawToolMajor);
5706 processToolMinor(mapper, rawToolMinor);
5707 processPressure(mapper, rawPressure);
5708 processOrientation(mapper, rawOrientation);
5709 processDistance(mapper, rawDistance);
5710 processId(mapper, id);
5711 processMTSync(mapper);
5712 processSync(mapper);
5713
5714 NotifyMotionArgs args;
5715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5716 ASSERT_EQ(0, args.pointerProperties[0].id);
5717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5718 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5719 orientation, distance));
5720}
5721
5722TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005723 addConfigurationProperty("touch.deviceType", "touchScreen");
5724 prepareDisplay(DISPLAY_ORIENTATION_0);
5725 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5726 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005727 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728
5729 // These calculations are based on the input device calibration documentation.
5730 int32_t rawX = 100;
5731 int32_t rawY = 200;
5732 int32_t rawTouchMajor = 140;
5733 int32_t rawTouchMinor = 120;
5734 int32_t rawToolMajor = 180;
5735 int32_t rawToolMinor = 160;
5736
5737 float x = toDisplayX(rawX);
5738 float y = toDisplayY(rawY);
5739 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5740 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5741 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5742 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5743 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5744
5745 processPosition(mapper, rawX, rawY);
5746 processTouchMajor(mapper, rawTouchMajor);
5747 processTouchMinor(mapper, rawTouchMinor);
5748 processToolMajor(mapper, rawToolMajor);
5749 processToolMinor(mapper, rawToolMinor);
5750 processMTSync(mapper);
5751 processSync(mapper);
5752
5753 NotifyMotionArgs args;
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5756 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5757}
5758
5759TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 addConfigurationProperty("touch.deviceType", "touchScreen");
5761 prepareDisplay(DISPLAY_ORIENTATION_0);
5762 prepareAxes(POSITION | TOUCH | TOOL);
5763 addConfigurationProperty("touch.size.calibration", "diameter");
5764 addConfigurationProperty("touch.size.scale", "10");
5765 addConfigurationProperty("touch.size.bias", "160");
5766 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005767 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768
5769 // These calculations are based on the input device calibration documentation.
5770 // Note: We only provide a single common touch/tool value because the device is assumed
5771 // not to emit separate values for each pointer (isSummed = 1).
5772 int32_t rawX = 100;
5773 int32_t rawY = 200;
5774 int32_t rawX2 = 150;
5775 int32_t rawY2 = 250;
5776 int32_t rawTouchMajor = 5;
5777 int32_t rawToolMajor = 8;
5778
5779 float x = toDisplayX(rawX);
5780 float y = toDisplayY(rawY);
5781 float x2 = toDisplayX(rawX2);
5782 float y2 = toDisplayY(rawY2);
5783 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5784 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5785 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5786
5787 processPosition(mapper, rawX, rawY);
5788 processTouchMajor(mapper, rawTouchMajor);
5789 processToolMajor(mapper, rawToolMajor);
5790 processMTSync(mapper);
5791 processPosition(mapper, rawX2, rawY2);
5792 processTouchMajor(mapper, rawTouchMajor);
5793 processToolMajor(mapper, rawToolMajor);
5794 processMTSync(mapper);
5795 processSync(mapper);
5796
5797 NotifyMotionArgs args;
5798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5799 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5800
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5802 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5803 args.action);
5804 ASSERT_EQ(size_t(2), args.pointerCount);
5805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5806 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5808 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5809}
5810
5811TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005812 addConfigurationProperty("touch.deviceType", "touchScreen");
5813 prepareDisplay(DISPLAY_ORIENTATION_0);
5814 prepareAxes(POSITION | TOUCH | TOOL);
5815 addConfigurationProperty("touch.size.calibration", "area");
5816 addConfigurationProperty("touch.size.scale", "43");
5817 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005818 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819
5820 // These calculations are based on the input device calibration documentation.
5821 int32_t rawX = 100;
5822 int32_t rawY = 200;
5823 int32_t rawTouchMajor = 5;
5824 int32_t rawToolMajor = 8;
5825
5826 float x = toDisplayX(rawX);
5827 float y = toDisplayY(rawY);
5828 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5829 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5830 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5831
5832 processPosition(mapper, rawX, rawY);
5833 processTouchMajor(mapper, rawTouchMajor);
5834 processToolMajor(mapper, rawToolMajor);
5835 processMTSync(mapper);
5836 processSync(mapper);
5837
5838 NotifyMotionArgs args;
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5841 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5842}
5843
5844TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 addConfigurationProperty("touch.deviceType", "touchScreen");
5846 prepareDisplay(DISPLAY_ORIENTATION_0);
5847 prepareAxes(POSITION | PRESSURE);
5848 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5849 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005850 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851
Michael Wrightaa449c92017-12-13 21:21:43 +00005852 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005853 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00005854 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5855 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5856 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5857
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858 // These calculations are based on the input device calibration documentation.
5859 int32_t rawX = 100;
5860 int32_t rawY = 200;
5861 int32_t rawPressure = 60;
5862
5863 float x = toDisplayX(rawX);
5864 float y = toDisplayY(rawY);
5865 float pressure = float(rawPressure) * 0.01f;
5866
5867 processPosition(mapper, rawX, rawY);
5868 processPressure(mapper, rawPressure);
5869 processMTSync(mapper);
5870 processSync(mapper);
5871
5872 NotifyMotionArgs args;
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5875 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5876}
5877
5878TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879 addConfigurationProperty("touch.deviceType", "touchScreen");
5880 prepareDisplay(DISPLAY_ORIENTATION_0);
5881 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005882 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883
5884 NotifyMotionArgs motionArgs;
5885 NotifyKeyArgs keyArgs;
5886
5887 processId(mapper, 1);
5888 processPosition(mapper, 100, 200);
5889 processSync(mapper);
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5891 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5892 ASSERT_EQ(0, motionArgs.buttonState);
5893
5894 // press BTN_LEFT, release BTN_LEFT
5895 processKey(mapper, BTN_LEFT, 1);
5896 processSync(mapper);
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5899 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5900
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5903 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5904
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 processKey(mapper, BTN_LEFT, 0);
5906 processSync(mapper);
5907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005908 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005910
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005913 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914
5915 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5916 processKey(mapper, BTN_RIGHT, 1);
5917 processKey(mapper, BTN_MIDDLE, 1);
5918 processSync(mapper);
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5921 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5922 motionArgs.buttonState);
5923
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5925 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5926 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5927
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5929 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5930 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5931 motionArgs.buttonState);
5932
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933 processKey(mapper, BTN_RIGHT, 0);
5934 processSync(mapper);
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005938
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005941 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942
5943 processKey(mapper, BTN_MIDDLE, 0);
5944 processSync(mapper);
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005946 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005948
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005951 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952
5953 // press BTN_BACK, release BTN_BACK
5954 processKey(mapper, BTN_BACK, 1);
5955 processSync(mapper);
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5957 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5958 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005959
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005962 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5963
5964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5966 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967
5968 processKey(mapper, BTN_BACK, 0);
5969 processSync(mapper);
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005973
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005976 ASSERT_EQ(0, motionArgs.buttonState);
5977
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5979 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5980 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5981
5982 // press BTN_SIDE, release BTN_SIDE
5983 processKey(mapper, BTN_SIDE, 1);
5984 processSync(mapper);
5985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5986 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5987 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005988
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5995 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005996
5997 processKey(mapper, BTN_SIDE, 0);
5998 processSync(mapper);
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006001 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006002
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006005 ASSERT_EQ(0, motionArgs.buttonState);
6006
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6008 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6009 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6010
6011 // press BTN_FORWARD, release BTN_FORWARD
6012 processKey(mapper, BTN_FORWARD, 1);
6013 processSync(mapper);
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6015 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6016 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006017
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006020 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6021
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6023 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6024 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025
6026 processKey(mapper, BTN_FORWARD, 0);
6027 processSync(mapper);
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006031
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006034 ASSERT_EQ(0, motionArgs.buttonState);
6035
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6037 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6038 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6039
6040 // press BTN_EXTRA, release BTN_EXTRA
6041 processKey(mapper, BTN_EXTRA, 1);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6044 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6045 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006046
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006049 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6050
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6053 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006054
6055 processKey(mapper, BTN_EXTRA, 0);
6056 processSync(mapper);
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063 ASSERT_EQ(0, motionArgs.buttonState);
6064
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6066 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6067 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6068
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6070
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071 // press BTN_STYLUS, release BTN_STYLUS
6072 processKey(mapper, BTN_STYLUS, 1);
6073 processSync(mapper);
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006076 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6077
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6080 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081
6082 processKey(mapper, BTN_STYLUS, 0);
6083 processSync(mapper);
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006087
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006090 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091
6092 // press BTN_STYLUS2, release BTN_STYLUS2
6093 processKey(mapper, BTN_STYLUS2, 1);
6094 processSync(mapper);
6095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006097 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6098
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6101 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102
6103 processKey(mapper, BTN_STYLUS2, 0);
6104 processSync(mapper);
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006108
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006111 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112
6113 // release touch
6114 processId(mapper, -1);
6115 processSync(mapper);
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6117 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6118 ASSERT_EQ(0, motionArgs.buttonState);
6119}
6120
6121TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122 addConfigurationProperty("touch.deviceType", "touchScreen");
6123 prepareDisplay(DISPLAY_ORIENTATION_0);
6124 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006125 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126
6127 NotifyMotionArgs motionArgs;
6128
6129 // default tool type is finger
6130 processId(mapper, 1);
6131 processPosition(mapper, 100, 200);
6132 processSync(mapper);
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6136
6137 // eraser
6138 processKey(mapper, BTN_TOOL_RUBBER, 1);
6139 processSync(mapper);
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6143
6144 // stylus
6145 processKey(mapper, BTN_TOOL_RUBBER, 0);
6146 processKey(mapper, BTN_TOOL_PEN, 1);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6151
6152 // brush
6153 processKey(mapper, BTN_TOOL_PEN, 0);
6154 processKey(mapper, BTN_TOOL_BRUSH, 1);
6155 processSync(mapper);
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6159
6160 // pencil
6161 processKey(mapper, BTN_TOOL_BRUSH, 0);
6162 processKey(mapper, BTN_TOOL_PENCIL, 1);
6163 processSync(mapper);
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6167
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006168 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 processKey(mapper, BTN_TOOL_PENCIL, 0);
6170 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6171 processSync(mapper);
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6175
6176 // mouse
6177 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6178 processKey(mapper, BTN_TOOL_MOUSE, 1);
6179 processSync(mapper);
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6183
6184 // lens
6185 processKey(mapper, BTN_TOOL_MOUSE, 0);
6186 processKey(mapper, BTN_TOOL_LENS, 1);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6191
6192 // double-tap
6193 processKey(mapper, BTN_TOOL_LENS, 0);
6194 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6199
6200 // triple-tap
6201 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6202 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6207
6208 // quad-tap
6209 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6210 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6211 processSync(mapper);
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6215
6216 // finger
6217 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6218 processKey(mapper, BTN_TOOL_FINGER, 1);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6223
6224 // stylus trumps finger
6225 processKey(mapper, BTN_TOOL_PEN, 1);
6226 processSync(mapper);
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6230
6231 // eraser trumps stylus
6232 processKey(mapper, BTN_TOOL_RUBBER, 1);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6237
6238 // mouse trumps eraser
6239 processKey(mapper, BTN_TOOL_MOUSE, 1);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6244
6245 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6246 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6247 processSync(mapper);
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6251
6252 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6253 processToolType(mapper, MT_TOOL_PEN);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6258
6259 // back to default tool type
6260 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6261 processKey(mapper, BTN_TOOL_MOUSE, 0);
6262 processKey(mapper, BTN_TOOL_RUBBER, 0);
6263 processKey(mapper, BTN_TOOL_PEN, 0);
6264 processKey(mapper, BTN_TOOL_FINGER, 0);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6269}
6270
6271TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 addConfigurationProperty("touch.deviceType", "touchScreen");
6273 prepareDisplay(DISPLAY_ORIENTATION_0);
6274 prepareAxes(POSITION | ID | SLOT);
6275 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006276 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277
6278 NotifyMotionArgs motionArgs;
6279
6280 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6281 processId(mapper, 1);
6282 processPosition(mapper, 100, 200);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6287 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6292 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6293
6294 // move a little
6295 processPosition(mapper, 150, 250);
6296 processSync(mapper);
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 // down when BTN_TOUCH is pressed, pressure defaults to 1
6303 processKey(mapper, BTN_TOUCH, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6311 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6313 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6314
6315 // up when BTN_TOUCH is released, hover restored
6316 processKey(mapper, BTN_TOUCH, 0);
6317 processSync(mapper);
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6321 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6322
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6324 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6326 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6331 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6332
6333 // exit hover when pointer goes away
6334 processId(mapper, -1);
6335 processSync(mapper);
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6339 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6340}
6341
6342TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 addConfigurationProperty("touch.deviceType", "touchScreen");
6344 prepareDisplay(DISPLAY_ORIENTATION_0);
6345 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006346 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347
6348 NotifyMotionArgs motionArgs;
6349
6350 // initially hovering because pressure is 0
6351 processId(mapper, 1);
6352 processPosition(mapper, 100, 200);
6353 processPressure(mapper, 0);
6354 processSync(mapper);
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6356 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6358 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6359
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6363 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6364
6365 // move a little
6366 processPosition(mapper, 150, 250);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6371 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6372
6373 // down when pressure becomes non-zero
6374 processPressure(mapper, RAW_PRESSURE_MAX);
6375 processSync(mapper);
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6379 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6380
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6384 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6385
6386 // up when pressure becomes 0, hover restored
6387 processPressure(mapper, 0);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6392 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6393
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6397 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6402 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6403
6404 // exit hover when pointer goes away
6405 processId(mapper, -1);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6410 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6411}
6412
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006413/**
6414 * Set the input device port <--> display port associations, and check that the
6415 * events are routed to the display that matches the display port.
6416 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6417 */
6418TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006419 const std::string usb2 = "USB2";
6420 const uint8_t hdmi1 = 0;
6421 const uint8_t hdmi2 = 1;
6422 const std::string secondaryUniqueId = "uniqueId2";
6423 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6424
6425 addConfigurationProperty("touch.deviceType", "touchScreen");
6426 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006427 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006428
6429 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6430 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6431
6432 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6433 // for this input device is specified, and the matching viewport is not present,
6434 // the input device should be disabled (at the mapper level).
6435
6436 // Add viewport for display 2 on hdmi2
6437 prepareSecondaryDisplay(type, hdmi2);
6438 // Send a touch event
6439 processPosition(mapper, 100, 100);
6440 processSync(mapper);
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6442
6443 // Add viewport for display 1 on hdmi1
6444 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6445 // Send a touch event again
6446 processPosition(mapper, 100, 100);
6447 processSync(mapper);
6448
6449 NotifyMotionArgs args;
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6451 ASSERT_EQ(DISPLAY_ID, args.displayId);
6452}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453
Arthur Hung41a712e2018-11-22 19:41:03 +08006454/**
6455 * Expect fallback to internal viewport if device is external and external viewport is not present.
6456 */
6457TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
Arthur Hung41a712e2018-11-22 19:41:03 +08006458 prepareAxes(POSITION);
6459 addConfigurationProperty("touch.deviceType", "touchScreen");
6460 prepareDisplay(DISPLAY_ORIENTATION_0);
6461 mDevice->setExternal(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006462 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung41a712e2018-11-22 19:41:03 +08006463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006464 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Arthur Hung41a712e2018-11-22 19:41:03 +08006465
6466 NotifyMotionArgs motionArgs;
6467
6468 // Expect the event to be sent to the internal viewport,
6469 // because an external viewport is not present.
6470 processPosition(mapper, 100, 100);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6474
6475 // Expect the event to be sent to the external viewport if it is present.
6476 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6477 processPosition(mapper, 100, 100);
6478 processSync(mapper);
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6481}
6482
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006483TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006484 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006485 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006486 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006487 fakePointerController->setPosition(100, 200);
6488 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006489 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6490
Garfield Tan888a6a42020-01-09 11:39:16 -08006491 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6492 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6493
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006494 prepareDisplay(DISPLAY_ORIENTATION_0);
6495 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006496 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006497
6498 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006499 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006500
6501 NotifyMotionArgs motionArgs;
6502 processPosition(mapper, 100, 100);
6503 processSync(mapper);
6504
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6507 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6508}
6509
Arthur Hung7c645402019-01-25 17:45:42 +08006510TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6511 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006512 prepareAxes(POSITION | ID | SLOT);
6513 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006514 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006515
6516 // Create the second touch screen device, and enable multi fingers.
6517 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006518 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006519 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006520 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006521 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006522 std::unique_ptr<InputDevice> device2 =
6523 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6524 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006525 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6526 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6527 0 /*flat*/, 0 /*fuzz*/);
6528 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6529 0 /*flat*/, 0 /*fuzz*/);
6530 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6531 0 /*flat*/, 0 /*fuzz*/);
6532 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6533 0 /*flat*/, 0 /*fuzz*/);
6534 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6535 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6536 String8("touchScreen"));
6537
6538 // Setup the second touch screen device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006539 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006540 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6541 device2->reset(ARBITRARY_TIME);
6542
6543 // Setup PointerController.
6544 sp<FakePointerController> fakePointerController = new FakePointerController();
6545 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6546 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6547
6548 // Setup policy for associated displays and show touches.
6549 const uint8_t hdmi1 = 0;
6550 const uint8_t hdmi2 = 1;
6551 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6552 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6553 mFakePolicy->setShowTouches(true);
6554
6555 // Create displays.
6556 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6557 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6558
6559 // Default device will reconfigure above, need additional reconfiguration for another device.
6560 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6561 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6562
6563 // Two fingers down at default display.
6564 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6565 processPosition(mapper, x1, y1);
6566 processId(mapper, 1);
6567 processSlot(mapper, 1);
6568 processPosition(mapper, x2, y2);
6569 processId(mapper, 2);
6570 processSync(mapper);
6571
6572 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6573 fakePointerController->getSpots().find(DISPLAY_ID);
6574 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6575 ASSERT_EQ(size_t(2), iter->second.size());
6576
6577 // Two fingers down at second display.
6578 processPosition(mapper2, x1, y1);
6579 processId(mapper2, 1);
6580 processSlot(mapper2, 1);
6581 processPosition(mapper2, x2, y2);
6582 processId(mapper2, 2);
6583 processSync(mapper2);
6584
6585 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6586 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6587 ASSERT_EQ(size_t(2), iter->second.size());
6588}
6589
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006590TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006591 prepareAxes(POSITION);
6592 addConfigurationProperty("touch.deviceType", "touchScreen");
6593 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006594 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006595
6596 NotifyMotionArgs motionArgs;
6597 // Unrotated video frame
6598 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6599 std::vector<TouchVideoFrame> frames{frame};
6600 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6601 processPosition(mapper, 100, 200);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(frames, motionArgs.videoFrames);
6605
6606 // Subsequent touch events should not have any videoframes
6607 // This is implemented separately in FakeEventHub,
6608 // but that should match the behaviour of TouchVideoDevice.
6609 processPosition(mapper, 200, 200);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6613}
6614
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006615TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006616 prepareAxes(POSITION);
6617 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006618 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006619 // Unrotated video frame
6620 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6621 NotifyMotionArgs motionArgs;
6622
6623 // Test all 4 orientations
6624 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6625 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6626 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6627 clearViewports();
6628 prepareDisplay(orientation);
6629 std::vector<TouchVideoFrame> frames{frame};
6630 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6631 processPosition(mapper, 100, 200);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 frames[0].rotate(orientation);
6635 ASSERT_EQ(frames, motionArgs.videoFrames);
6636 }
6637}
6638
6639TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006640 prepareAxes(POSITION);
6641 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006642 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006643 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6644 // so mix these.
6645 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6646 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6647 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6648 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6649 NotifyMotionArgs motionArgs;
6650
6651 prepareDisplay(DISPLAY_ORIENTATION_90);
6652 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6653 processPosition(mapper, 100, 200);
6654 processSync(mapper);
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6656 std::for_each(frames.begin(), frames.end(),
6657 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6658 ASSERT_EQ(frames, motionArgs.videoFrames);
6659}
6660
Arthur Hung9da14732019-09-02 16:16:58 +08006661/**
6662 * If we had defined port associations, but the viewport is not ready, the touch device would be
6663 * expected to be disabled, and it should be enabled after the viewport has found.
6664 */
6665TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006666 constexpr uint8_t hdmi2 = 1;
6667 const std::string secondaryUniqueId = "uniqueId2";
6668 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6669
6670 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6671
6672 addConfigurationProperty("touch.deviceType", "touchScreen");
6673 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006674 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006675
6676 ASSERT_EQ(mDevice->isEnabled(), false);
6677
6678 // Add display on hdmi2, the device should be enabled and can receive touch event.
6679 prepareSecondaryDisplay(type, hdmi2);
6680 ASSERT_EQ(mDevice->isEnabled(), true);
6681
6682 // Send a touch event.
6683 processPosition(mapper, 100, 100);
6684 processSync(mapper);
6685
6686 NotifyMotionArgs args;
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6688 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6689}
6690
Arthur Hung6cd19a42019-08-30 19:04:12 +08006691/**
6692 * Test touch should not work if outside of surface.
6693 */
6694TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
Arthur Hung6cd19a42019-08-30 19:04:12 +08006695 addConfigurationProperty("touch.deviceType", "touchScreen");
6696 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006697 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006698 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung6cd19a42019-08-30 19:04:12 +08006699
Arthur Hung05de5772019-09-26 18:31:26 +08006700 // Touch on left-top area should work.
6701 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6702 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6703 processPosition(mapper, rawX, rawY);
6704 processSync(mapper);
6705
6706 NotifyMotionArgs args;
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6708
6709 // Reset.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006710 mapper.reset(ARBITRARY_TIME);
Arthur Hung05de5772019-09-26 18:31:26 +08006711
6712 // Let logical display be different to physical display and rotate 90-degrees.
6713 std::optional<DisplayViewport> internalViewport =
6714 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6715 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6716 internalViewport->logicalLeft = 0;
6717 internalViewport->logicalTop = 0;
6718 internalViewport->logicalRight = DISPLAY_HEIGHT;
6719 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6720
6721 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6722 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6723 internalViewport->physicalRight = DISPLAY_HEIGHT;
6724 internalViewport->physicalBottom = DISPLAY_WIDTH;
6725
6726 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6727 internalViewport->deviceHeight = DISPLAY_WIDTH;
6728 mFakePolicy->updateViewport(internalViewport.value());
6729 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6730
6731 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006732 processPosition(mapper, rawX, rawY);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6735}
6736
Arthur Hung421eb1c2020-01-16 00:09:42 +08006737TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006738 addConfigurationProperty("touch.deviceType", "touchScreen");
6739 prepareDisplay(DISPLAY_ORIENTATION_0);
6740 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006741 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006742
6743 NotifyMotionArgs motionArgs;
6744
6745 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6746 // finger down
6747 processId(mapper, 1);
6748 processPosition(mapper, x1, y1);
6749 processSync(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6753
6754 // finger move
6755 processId(mapper, 1);
6756 processPosition(mapper, x2, y2);
6757 processSync(mapper);
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6761
6762 // finger up.
6763 processId(mapper, -1);
6764 processSync(mapper);
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6768
6769 // new finger down
6770 processId(mapper, 1);
6771 processPosition(mapper, x3, y3);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6776}
6777
6778/**
6779 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6780 * UP events should be ignored.
6781 */
6782TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006783 addConfigurationProperty("touch.deviceType", "touchScreen");
6784 prepareDisplay(DISPLAY_ORIENTATION_0);
6785 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006786 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006787
6788 NotifyMotionArgs motionArgs;
6789
6790 // default tool type is finger
6791 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6792 processId(mapper, 1);
6793 processPosition(mapper, x1, y1);
6794 processSync(mapper);
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6796 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6798
6799 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6800 processToolType(mapper, MT_TOOL_PALM);
6801 processSync(mapper);
6802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6803 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6804
6805 // Ignore the following MOVE and UP events if had detect a palm event.
6806 processId(mapper, 1);
6807 processPosition(mapper, x2, y2);
6808 processSync(mapper);
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6810
6811 // finger up.
6812 processId(mapper, -1);
6813 processSync(mapper);
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6815
6816 // new finger down
6817 processToolType(mapper, MT_TOOL_FINGER);
6818 processId(mapper, 1);
6819 processPosition(mapper, x3, y3);
6820 processSync(mapper);
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6822 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6824}
6825
Michael Wrightd02c5b62014-02-10 15:10:22 -08006826} // namespace android