blob: d870a01bfa6555a42c9271da5e913feae2b7d55b [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
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800884 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885
886 virtual void fadePointer() {
887 }
888
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100889 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890 }
891
892 virtual int32_t bumpGeneration() {
893 return ++mGeneration;
894 }
Michael Wright842500e2015-03-13 17:32:02 -0700895
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800896 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700897
898 }
899
900 virtual void dispatchExternalStylusState(const StylusState&) {
901
902 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800903
904 virtual uint32_t getNextSequenceNum() {
905 return mNextSequenceNum++;
906 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907};
908
909
910// --- FakeInputMapper ---
911
912class FakeInputMapper : public InputMapper {
913 uint32_t mSources;
914 int32_t mKeyboardType;
915 int32_t mMetaState;
916 KeyedVector<int32_t, int32_t> mKeyCodeStates;
917 KeyedVector<int32_t, int32_t> mScanCodeStates;
918 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800919 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700921 std::mutex mLock;
922 std::condition_variable mStateChangedCondition;
923 bool mConfigureWasCalled GUARDED_BY(mLock);
924 bool mResetWasCalled GUARDED_BY(mLock);
925 bool mProcessWasCalled GUARDED_BY(mLock);
926 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Arthur Hungc23540e2018-11-29 20:42:11 +0800928 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800930 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
931 : InputMapper(deviceContext),
932 mSources(sources),
933 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800935 mConfigureWasCalled(false),
936 mResetWasCalled(false),
937 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938
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.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001025 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001026 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 {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001094 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095
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
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001102 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001104 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001106 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, int32_t controllerNumber,
1107 const std::string& name, uint32_t classes,
1108 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 InputDeviceIdentifier identifier;
1110 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001111 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112 int32_t generation = deviceId + 1;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001113 return std::make_shared<InputDevice>(&mContext, deviceId, generation, controllerNumber,
1114 identifier, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 }
1116
Prabir Pradhan28efc192019-11-05 01:10:04 +00001117 // Make the protected loopOnce method accessible to tests.
1118 using InputReader::loopOnce;
1119
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120protected:
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001121 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t deviceId,
1122 int32_t controllerNumber,
1123 const InputDeviceIdentifier& identifier,
1124 uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001126 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001127 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 return device;
1129 }
1130 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1131 }
1132
1133 friend class InputReaderTest;
1134};
1135
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001136// --- InputReaderPolicyTest ---
1137class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001138protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001139 sp<FakeInputReaderPolicy> mFakePolicy;
1140
Prabir Pradhan28efc192019-11-05 01:10:04 +00001141 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1142 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001143};
1144
1145/**
1146 * Check that empty set of viewports is an acceptable configuration.
1147 * Also try to get internal viewport two different ways - by type and by uniqueId.
1148 *
1149 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1150 * Such configuration is not currently allowed.
1151 */
1152TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001153 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001154
1155 // We didn't add any viewports yet, so there shouldn't be any.
1156 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001157 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001158 ASSERT_FALSE(internalViewport);
1159
1160 // Add an internal viewport, then clear it
1161 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001162 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001163
1164 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001165 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001166 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001167 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168
1169 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001170 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001171 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001172 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173
1174 mFakePolicy->clearViewports();
1175 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001176 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001178 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179 ASSERT_FALSE(internalViewport);
1180}
1181
1182TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1183 const std::string internalUniqueId = "local:0";
1184 const std::string externalUniqueId = "local:1";
1185 const std::string virtualUniqueId1 = "virtual:2";
1186 const std::string virtualUniqueId2 = "virtual:3";
1187 constexpr int32_t virtualDisplayId1 = 2;
1188 constexpr int32_t virtualDisplayId2 = 3;
1189
1190 // Add an internal viewport
1191 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001192 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001193 // Add an external viewport
1194 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001195 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001196 // Add an virtual viewport
1197 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001198 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001199 // Add another virtual viewport
1200 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001201 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202
1203 // Check matching by type for internal
1204 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001205 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001206 ASSERT_TRUE(internalViewport);
1207 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1208
1209 // Check matching by type for external
1210 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001211 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212 ASSERT_TRUE(externalViewport);
1213 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1214
1215 // Check matching by uniqueId for virtual viewport #1
1216 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001217 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001218 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001219 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001220 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1221 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1222
1223 // Check matching by uniqueId for virtual viewport #2
1224 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001225 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001226 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001227 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001228 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1229 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1230}
1231
1232
1233/**
1234 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1235 * that lookup works by checking display id.
1236 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1237 */
1238TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1239 const std::string uniqueId1 = "uniqueId1";
1240 const std::string uniqueId2 = "uniqueId2";
1241 constexpr int32_t displayId1 = 2;
1242 constexpr int32_t displayId2 = 3;
1243
1244 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1245 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1246 for (const ViewportType& type : types) {
1247 mFakePolicy->clearViewports();
1248 // Add a viewport
1249 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001250 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001251 // Add another viewport
1252 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001253 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254
1255 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001256 std::optional<DisplayViewport> viewport1 =
1257 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001258 ASSERT_TRUE(viewport1);
1259 ASSERT_EQ(displayId1, viewport1->displayId);
1260 ASSERT_EQ(type, viewport1->type);
1261
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001262 std::optional<DisplayViewport> viewport2 =
1263 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264 ASSERT_TRUE(viewport2);
1265 ASSERT_EQ(displayId2, viewport2->displayId);
1266 ASSERT_EQ(type, viewport2->type);
1267
1268 // When there are multiple viewports of the same kind, and uniqueId is not specified
1269 // in the call to getDisplayViewport, then that situation is not supported.
1270 // The viewports can be stored in any order, so we cannot rely on the order, since that
1271 // is just implementation detail.
1272 // However, we can check that it still returns *a* viewport, we just cannot assert
1273 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001274 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275 ASSERT_TRUE(someViewport);
1276 }
1277}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001279/**
1280 * Check getDisplayViewportByPort
1281 */
1282TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1283 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1284 const std::string uniqueId1 = "uniqueId1";
1285 const std::string uniqueId2 = "uniqueId2";
1286 constexpr int32_t displayId1 = 1;
1287 constexpr int32_t displayId2 = 2;
1288 const uint8_t hdmi1 = 0;
1289 const uint8_t hdmi2 = 1;
1290 const uint8_t hdmi3 = 2;
1291
1292 mFakePolicy->clearViewports();
1293 // Add a viewport that's associated with some display port that's not of interest.
1294 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1295 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1296 // Add another viewport, connected to HDMI1 port
1297 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1298 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1299
1300 // Check that correct display viewport was returned by comparing the display ports.
1301 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1302 ASSERT_TRUE(hdmi1Viewport);
1303 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1304 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1305
1306 // Check that we can still get the same viewport using the uniqueId
1307 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1308 ASSERT_TRUE(hdmi1Viewport);
1309 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1310 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1311 ASSERT_EQ(type, hdmi1Viewport->type);
1312
1313 // Check that we cannot find a port with "HDMI2", because we never added one
1314 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1315 ASSERT_FALSE(hdmi2Viewport);
1316}
1317
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318// --- InputReaderTest ---
1319
1320class InputReaderTest : public testing::Test {
1321protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001322 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001323 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001324 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001325 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326
Prabir Pradhan28efc192019-11-05 01:10:04 +00001327 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001328 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001330 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001331
Prabir Pradhan28efc192019-11-05 01:10:04 +00001332 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1333 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001334 }
1335
Prabir Pradhan28efc192019-11-05 01:10:04 +00001336 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001337 mFakeListener.clear();
1338 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 }
1340
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001341 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342 const PropertyMap* configuration) {
1343 mFakeEventHub->addDevice(deviceId, name, classes);
1344
1345 if (configuration) {
1346 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1347 }
1348 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001349 mReader->loopOnce();
1350 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001351 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1352 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001353 }
1354
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001355 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001356 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001357 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001358 }
1359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001360 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001361 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001362 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001363 }
1364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001365 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
1366 const std::string& name, uint32_t classes,
1367 uint32_t sources,
1368 const PropertyMap* configuration) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001369 std::shared_ptr<InputDevice> device =
1370 mReader->newDevice(deviceId, controllerNumber, name, classes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001371 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001372 mReader->setNextDevice(device);
1373 addDevice(deviceId, name, classes, configuration);
1374 return mapper;
1375 }
1376};
1377
1378TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001379 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001380 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001381 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001382 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001384 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001385 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 ASSERT_EQ(1U, inputDevices.size());
1387 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001388 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001389 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1390 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1391 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1392
1393 // Should also have received a notification describing the new input devices.
1394 inputDevices = mFakePolicy->getInputDevices();
1395 ASSERT_EQ(1U, inputDevices.size());
1396 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001397 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001398 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1399 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1400 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1401}
1402
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001403TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1404 constexpr int32_t deviceId = 1;
1405 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001406 std::shared_ptr<InputDevice> device =
1407 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;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001587 std::shared_ptr<InputDevice> device =
1588 mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001589 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001590 device->addMapper<FakeInputMapper>(AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001591 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001592 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001593
1594 NotifyDeviceResetArgs resetArgs;
1595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1596 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1597
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001598 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001599 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001601 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1602 prevSequenceNum = resetArgs.sequenceNum;
1603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001604 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001605 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001607 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1608 prevSequenceNum = resetArgs.sequenceNum;
1609
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001610 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001611 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001613 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1614 prevSequenceNum = resetArgs.sequenceNum;
1615}
1616
Arthur Hungc23540e2018-11-29 20:42:11 +08001617TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1618 constexpr int32_t deviceId = 1;
1619 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1620 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001621 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, 0 /*controllerNumber*/,
1622 "fake", deviceClass, DEVICE_LOCATION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001623 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001624 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001625
1626 const uint8_t hdmi1 = 1;
1627
1628 // Associated touch screen with second display.
1629 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1630
1631 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001632 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001633 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1634 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1635 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1636 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1637 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001638 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001639
1640 // Add the device, and make sure all of the callbacks are triggered.
1641 // The device is added after the input port associations are processed since
1642 // we do not yet support dynamic device-to-display associations.
1643 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001646 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001647
Arthur Hung2c9a3342019-07-23 14:18:59 +08001648 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001649 ASSERT_EQ(deviceId, device->getId());
1650 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1651 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001652
1653 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001654 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001655 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001656 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001657}
1658
Michael Wrightd02c5b62014-02-10 15:10:22 -08001659
1660// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001661class InputDeviceTest : public testing::Test {
1662protected:
1663 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001664 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665 static const int32_t DEVICE_ID;
1666 static const int32_t DEVICE_GENERATION;
1667 static const int32_t DEVICE_CONTROLLER_NUMBER;
1668 static const uint32_t DEVICE_CLASSES;
1669
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001670 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001671 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001672 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001673 FakeInputReaderContext* mFakeContext;
1674
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001675 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001676
Prabir Pradhan28efc192019-11-05 01:10:04 +00001677 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001678 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001680 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1682
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001683 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001684 InputDeviceIdentifier identifier;
1685 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001686 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001687 mDevice =
1688 std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1689 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001690 }
1691
Prabir Pradhan28efc192019-11-05 01:10:04 +00001692 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001693 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001694 delete mFakeContext;
1695 mFakeListener.clear();
1696 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001697 }
1698};
1699
1700const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001701const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001702const int32_t InputDeviceTest::DEVICE_ID = 1;
1703const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1704const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1705const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1706 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1707
1708TEST_F(InputDeviceTest, ImmutableProperties) {
1709 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001710 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1712}
1713
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1715 ASSERT_EQ(mDevice->isEnabled(), true);
1716}
1717
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1719 // Configuration.
1720 InputReaderConfiguration config;
1721 mDevice->configure(ARBITRARY_TIME, &config, 0);
1722
1723 // Reset.
1724 mDevice->reset(ARBITRARY_TIME);
1725
1726 NotifyDeviceResetArgs resetArgs;
1727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1728 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1729 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1730
1731 // Metadata.
1732 ASSERT_TRUE(mDevice->isIgnored());
1733 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1734
1735 InputDeviceInfo info;
1736 mDevice->getDeviceInfo(&info);
1737 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001738 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001739 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1740 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1741
1742 // State queries.
1743 ASSERT_EQ(0, mDevice->getMetaState());
1744
1745 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1746 << "Ignored device should return unknown key code state.";
1747 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1748 << "Ignored device should return unknown scan code state.";
1749 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1750 << "Ignored device should return unknown switch state.";
1751
1752 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1753 uint8_t flags[2] = { 0, 1 };
1754 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1755 << "Ignored device should never mark any key codes.";
1756 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1757 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1758}
1759
1760TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1761 // Configuration.
1762 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001764 FakeInputMapper& mapper1 = mDevice->addMapper<FakeInputMapper>(AINPUT_SOURCE_KEYBOARD);
1765 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1766 mapper1.setMetaState(AMETA_ALT_ON);
1767 mapper1.addSupportedKeyCode(AKEYCODE_A);
1768 mapper1.addSupportedKeyCode(AKEYCODE_B);
1769 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1770 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1771 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
1772 mapper1.setScanCodeState(3, AKEY_STATE_UP);
1773 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001775 FakeInputMapper& mapper2 = mDevice->addMapper<FakeInputMapper>(AINPUT_SOURCE_TOUCHSCREEN);
1776 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001777
1778 InputReaderConfiguration config;
1779 mDevice->configure(ARBITRARY_TIME, &config, 0);
1780
1781 String8 propertyValue;
1782 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1783 << "Device should have read configuration during configuration phase.";
1784 ASSERT_STREQ("value", propertyValue.string());
1785
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001786 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
1787 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001788
1789 // Reset
1790 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001791 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
1792 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001793
1794 NotifyDeviceResetArgs resetArgs;
1795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1796 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1797 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1798
1799 // Metadata.
1800 ASSERT_FALSE(mDevice->isIgnored());
1801 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1802
1803 InputDeviceInfo info;
1804 mDevice->getDeviceInfo(&info);
1805 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001806 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001807 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1808 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1809
1810 // State queries.
1811 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1812 << "Should query mappers and combine meta states.";
1813
1814 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1815 << "Should return unknown key code state when source not supported.";
1816 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1817 << "Should return unknown scan code state when source not supported.";
1818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1819 << "Should return unknown switch state when source not supported.";
1820
1821 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1822 << "Should query mapper when source is supported.";
1823 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1824 << "Should query mapper when source is supported.";
1825 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1826 << "Should query mapper when source is supported.";
1827
1828 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1829 uint8_t flags[4] = { 0, 0, 0, 1 };
1830 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1831 << "Should do nothing when source is unsupported.";
1832 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1833 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1834 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1835 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1836
1837 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1838 << "Should query mapper when source is supported.";
1839 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1840 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1841 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1842 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1843
1844 // Event handling.
1845 RawEvent event;
1846 mDevice->process(&event, 1);
1847
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001848 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
1849 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850}
1851
Arthur Hung2c9a3342019-07-23 14:18:59 +08001852// A single input device is associated with a specific display. Check that:
1853// 1. Device is disabled if the viewport corresponding to the associated display is not found
1854// 2. Device is disabled when setEnabled API is called
1855TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001856 mDevice->addMapper<FakeInputMapper>(AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08001857
1858 // First Configuration.
1859 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1860
1861 // Device should be enabled by default.
1862 ASSERT_TRUE(mDevice->isEnabled());
1863
1864 // Prepare associated info.
1865 constexpr uint8_t hdmi = 1;
1866 const std::string UNIQUE_ID = "local:1";
1867
1868 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1869 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1870 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1871 // Device should be disabled because it is associated with a specific display via
1872 // input port <-> display port association, but the corresponding display is not found
1873 ASSERT_FALSE(mDevice->isEnabled());
1874
1875 // Prepare displays.
1876 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1877 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1878 ViewportType::VIEWPORT_INTERNAL);
1879 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1880 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1881 ASSERT_TRUE(mDevice->isEnabled());
1882
1883 // Device should be disabled after set disable.
1884 mFakePolicy->addDisabledDevice(mDevice->getId());
1885 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1886 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1887 ASSERT_FALSE(mDevice->isEnabled());
1888
1889 // Device should still be disabled even found the associated display.
1890 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1891 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1892 ASSERT_FALSE(mDevice->isEnabled());
1893}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894
1895// --- InputMapperTest ---
1896
1897class InputMapperTest : public testing::Test {
1898protected:
1899 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001900 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001901 static const int32_t DEVICE_ID;
1902 static const int32_t DEVICE_GENERATION;
1903 static const int32_t DEVICE_CONTROLLER_NUMBER;
1904 static const uint32_t DEVICE_CLASSES;
1905
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001906 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001907 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001908 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909 FakeInputReaderContext* mFakeContext;
1910 InputDevice* mDevice;
1911
Prabir Pradhan28efc192019-11-05 01:10:04 +00001912 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001913 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001915 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001916 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1917 InputDeviceIdentifier identifier;
1918 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001919 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001920 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1921 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1922
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001923 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001924 }
1925
Prabir Pradhan28efc192019-11-05 01:10:04 +00001926 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001927 delete mDevice;
1928 delete mFakeContext;
1929 mFakeListener.clear();
1930 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001931 }
1932
1933 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001934 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001935 }
1936
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001937 void configureDevice(uint32_t changes) {
1938 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1939 }
1940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001941 template <class T, typename... Args>
1942 T& addMapperAndConfigure(Args... args) {
1943 T& mapper = mDevice->addMapper<T>(args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001944 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001946 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001947 }
1948
1949 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001950 int32_t orientation, const std::string& uniqueId,
1951 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001952 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001953 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001954 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1955 }
1956
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001957 void clearViewports() {
1958 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001959 }
1960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001961 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
1962 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001963 RawEvent event;
1964 event.when = when;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001965 event.deviceId = mapper.getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001966 event.type = type;
1967 event.code = code;
1968 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001969 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001970 }
1971
1972 static void assertMotionRange(const InputDeviceInfo& info,
1973 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1974 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001975 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001976 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1977 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1978 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1979 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1980 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1981 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1982 }
1983
1984 static void assertPointerCoords(const PointerCoords& coords,
1985 float x, float y, float pressure, float size,
1986 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1987 float orientation, float distance) {
1988 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1989 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1990 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1991 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1992 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1993 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1994 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1995 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1996 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1997 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1998 }
1999
2000 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2001 float actualX, actualY;
2002 controller->getPosition(&actualX, &actualY);
2003 ASSERT_NEAR(x, actualX, 1);
2004 ASSERT_NEAR(y, actualY, 1);
2005 }
2006};
2007
2008const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002009const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002010const int32_t InputMapperTest::DEVICE_ID = 1;
2011const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2012const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2013const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2014
2015
2016// --- SwitchInputMapperTest ---
2017
2018class SwitchInputMapperTest : public InputMapperTest {
2019protected:
2020};
2021
2022TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002023 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002025 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002026}
2027
2028TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002029 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002030
2031 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002032 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002033
2034 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002035 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002036}
2037
2038TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002039 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002040
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002041 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2042 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2043 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2044 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002045
2046 NotifySwitchArgs args;
2047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2048 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002049 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2050 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002051 args.switchMask);
2052 ASSERT_EQ(uint32_t(0), args.policyFlags);
2053}
2054
2055
2056// --- KeyboardInputMapperTest ---
2057
2058class KeyboardInputMapperTest : public InputMapperTest {
2059protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002060 const std::string UNIQUE_ID = "local:0";
2061
2062 void prepareDisplay(int32_t orientation);
2063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002064 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002065 int32_t originalKeyCode, int32_t rotatedKeyCode,
2066 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002067};
2068
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002069/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2070 * orientation.
2071 */
2072void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2073 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002074 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002075}
2076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002077void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002078 int32_t originalScanCode, int32_t originalKeyCode,
2079 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080 NotifyKeyArgs args;
2081
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002082 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2084 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2085 ASSERT_EQ(originalScanCode, args.scanCode);
2086 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002087 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002088
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002089 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2091 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2092 ASSERT_EQ(originalScanCode, args.scanCode);
2093 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002094 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002095}
2096
Michael Wrightd02c5b62014-02-10 15:10:22 -08002097TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002098 KeyboardInputMapper& mapper =
2099 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2100 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002102 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002103}
2104
2105TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2106 const int32_t USAGE_A = 0x070004;
2107 const int32_t USAGE_UNKNOWN = 0x07ffff;
2108 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2109 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002111 KeyboardInputMapper& mapper =
2112 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2113 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002114
2115 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002116 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002117 NotifyKeyArgs args;
2118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2119 ASSERT_EQ(DEVICE_ID, args.deviceId);
2120 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2121 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2122 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2123 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2124 ASSERT_EQ(KEY_HOME, args.scanCode);
2125 ASSERT_EQ(AMETA_NONE, args.metaState);
2126 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2127 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2128 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2129
2130 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002131 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2133 ASSERT_EQ(DEVICE_ID, args.deviceId);
2134 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2135 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2136 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2137 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2138 ASSERT_EQ(KEY_HOME, args.scanCode);
2139 ASSERT_EQ(AMETA_NONE, args.metaState);
2140 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2141 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2142 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2143
2144 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002145 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2146 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2148 ASSERT_EQ(DEVICE_ID, args.deviceId);
2149 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2150 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2151 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2152 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2153 ASSERT_EQ(0, args.scanCode);
2154 ASSERT_EQ(AMETA_NONE, args.metaState);
2155 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2156 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2157 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2158
2159 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002160 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2161 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2163 ASSERT_EQ(DEVICE_ID, args.deviceId);
2164 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2165 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2166 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2167 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2168 ASSERT_EQ(0, args.scanCode);
2169 ASSERT_EQ(AMETA_NONE, args.metaState);
2170 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2171 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2172 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2173
2174 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002175 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2176 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2178 ASSERT_EQ(DEVICE_ID, args.deviceId);
2179 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2180 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2181 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2182 ASSERT_EQ(0, args.keyCode);
2183 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2184 ASSERT_EQ(AMETA_NONE, args.metaState);
2185 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2186 ASSERT_EQ(0U, args.policyFlags);
2187 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2188
2189 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002190 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2191 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2193 ASSERT_EQ(DEVICE_ID, args.deviceId);
2194 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2195 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2196 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2197 ASSERT_EQ(0, args.keyCode);
2198 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2199 ASSERT_EQ(AMETA_NONE, args.metaState);
2200 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2201 ASSERT_EQ(0U, args.policyFlags);
2202 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2203}
2204
2205TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2206 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2207 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002209 KeyboardInputMapper& mapper =
2210 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2211 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002212
2213 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002214 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002215
2216 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002217 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002218 NotifyKeyArgs args;
2219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002222 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2223
2224 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002225 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002228 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002229
2230 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002231 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2233 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002234 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235
2236 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002237 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2239 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002240 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002241 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2242}
2243
2244TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2245 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2246 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2247 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2248 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2249
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002250 KeyboardInputMapper& mapper =
2251 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2252 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002253
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002254 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002255 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2256 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2257 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2258 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2259 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2260 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2261 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2262 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2263}
2264
2265TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2266 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2267 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2268 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2269 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2270
Michael Wrightd02c5b62014-02-10 15:10:22 -08002271 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002272 KeyboardInputMapper& mapper =
2273 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2274 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002275
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002276 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002277 ASSERT_NO_FATAL_FAILURE(
2278 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2279 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2280 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2281 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2282 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2283 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2284 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002286 clearViewports();
2287 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002288 ASSERT_NO_FATAL_FAILURE(
2289 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2290 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2291 AKEYCODE_DPAD_UP, DISPLAY_ID));
2292 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2293 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2294 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2295 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002296
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002297 clearViewports();
2298 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002299 ASSERT_NO_FATAL_FAILURE(
2300 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2301 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2302 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2303 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2304 AKEYCODE_DPAD_UP, DISPLAY_ID));
2305 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2306 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002307
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002308 clearViewports();
2309 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002310 ASSERT_NO_FATAL_FAILURE(
2311 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2312 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2313 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2314 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2315 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2316 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2317 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002318
2319 // Special case: if orientation changes while key is down, we still emit the same keycode
2320 // in the key up as we did in the key down.
2321 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002322 clearViewports();
2323 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002324 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2326 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2327 ASSERT_EQ(KEY_UP, args.scanCode);
2328 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002330 clearViewports();
2331 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002332 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2334 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2335 ASSERT_EQ(KEY_UP, args.scanCode);
2336 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2337}
2338
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002339TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2340 // If the keyboard is not orientation aware,
2341 // key events should not be associated with a specific display id
2342 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2343
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002344 KeyboardInputMapper& mapper =
2345 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2346 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002347 NotifyKeyArgs args;
2348
2349 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002350 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002352 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2354 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2355
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002356 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002357 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002359 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2361 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2362}
2363
2364TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2365 // If the keyboard is orientation aware,
2366 // key events should be associated with the internal viewport
2367 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2368
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002369 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002370 KeyboardInputMapper& mapper =
2371 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2372 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002373 NotifyKeyArgs args;
2374
2375 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2376 // ^--- already checked by the previous test
2377
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002378 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002379 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002380 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002382 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2384 ASSERT_EQ(DISPLAY_ID, args.displayId);
2385
2386 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002387 clearViewports();
2388 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002389 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002390 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002392 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2394 ASSERT_EQ(newDisplayId, args.displayId);
2395}
2396
Michael Wrightd02c5b62014-02-10 15:10:22 -08002397TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002398 KeyboardInputMapper& mapper =
2399 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2400 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401
2402 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002403 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404
2405 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002406 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407}
2408
2409TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002410 KeyboardInputMapper& mapper =
2411 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2412 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413
2414 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002415 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416
2417 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002418 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419}
2420
2421TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002422 KeyboardInputMapper& mapper =
2423 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2424 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425
2426 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2427
2428 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2429 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002430 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431 ASSERT_TRUE(flags[0]);
2432 ASSERT_FALSE(flags[1]);
2433}
2434
2435TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2436 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2437 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2438 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2439 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2440 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2441 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002443 KeyboardInputMapper& mapper =
2444 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2445 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446
2447 // Initialization should have turned all of the lights off.
2448 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2449 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2450 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2451
2452 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002453 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2454 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2456 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2457 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002458 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459
2460 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002461 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2462 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2464 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2465 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002466 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467
2468 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002469 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2470 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2472 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2473 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002474 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475
2476 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002477 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2478 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002479 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2480 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2481 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002482 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483
2484 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002485 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2486 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002487 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2488 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2489 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002490 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491
2492 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002493 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2494 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2496 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2497 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002498 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002499}
2500
Arthur Hung2c9a3342019-07-23 14:18:59 +08002501TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2502 // keyboard 1.
2503 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2504 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2505 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2506 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2507
2508 // keyboard 2.
2509 const std::string USB2 = "USB2";
2510 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2511 InputDeviceIdentifier identifier;
2512 identifier.name = "KEYBOARD2";
2513 identifier.location = USB2;
2514 std::unique_ptr<InputDevice> device2 =
2515 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2516 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2517 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2518 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2519 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2520 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2521 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2522
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002523 KeyboardInputMapper& mapper =
2524 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2525 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002526
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002527 KeyboardInputMapper& mapper2 =
2528 device2->addMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2529 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002530 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2531 device2->reset(ARBITRARY_TIME);
2532
2533 // Prepared displays and associated info.
2534 constexpr uint8_t hdmi1 = 0;
2535 constexpr uint8_t hdmi2 = 1;
2536 const std::string SECONDARY_UNIQUE_ID = "local:1";
2537
2538 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2539 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2540
2541 // No associated display viewport found, should disable the device.
2542 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2543 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2544 ASSERT_FALSE(device2->isEnabled());
2545
2546 // Prepare second display.
2547 constexpr int32_t newDisplayId = 2;
2548 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2549 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2550 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2551 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2552 // Default device will reconfigure above, need additional reconfiguration for another device.
2553 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2554 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2555
2556 // Device should be enabled after the associated display is found.
2557 ASSERT_TRUE(mDevice->isEnabled());
2558 ASSERT_TRUE(device2->isEnabled());
2559
2560 // Test pad key events
2561 ASSERT_NO_FATAL_FAILURE(
2562 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2563 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2564 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2565 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2566 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2567 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2568 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2569
2570 ASSERT_NO_FATAL_FAILURE(
2571 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2573 AKEYCODE_DPAD_RIGHT, newDisplayId));
2574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2575 AKEYCODE_DPAD_DOWN, newDisplayId));
2576 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2577 AKEYCODE_DPAD_LEFT, newDisplayId));
2578}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579
Powei Fengd041c5d2019-05-03 17:11:33 -07002580TEST_F(KeyboardInputMapperTest, ExternalDevice_WakeBehavior) {
2581 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2582 // marked as WAKE in the keylayout file to trigger wake.
2583 mDevice->setExternal(true);
2584
2585 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2586 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2587 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE, POLICY_FLAG_WAKE);
2588
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002589 KeyboardInputMapper& mapper =
2590 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2591 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002592
2593 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2594 NotifyKeyArgs args;
2595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2596 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2597
2598 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2600 ASSERT_EQ(uint32_t(0), args.policyFlags);
2601
2602 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2604 ASSERT_EQ(uint32_t(0), args.policyFlags);
2605
2606 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2608 ASSERT_EQ(uint32_t(0), args.policyFlags);
2609
2610 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2612 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2613
2614 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2616 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2617}
2618
2619TEST_F(KeyboardInputMapperTest, ExternalDevice_DoNotWakeByDefaultBehavior) {
2620 // Tv Remote key's wake behavior is prescribed by the keylayout file.
2621 mDevice->setExternal(true);
2622
2623 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2624 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2625 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
2626
Powei Fengd041c5d2019-05-03 17:11:33 -07002627 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002628 KeyboardInputMapper& mapper =
2629 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2630 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002631
2632 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2633 NotifyKeyArgs args;
2634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2635 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2636
2637 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2639 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2640
2641 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2643 ASSERT_EQ(uint32_t(0), args.policyFlags);
2644
2645 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2647 ASSERT_EQ(uint32_t(0), args.policyFlags);
2648
2649 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2651 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2652
2653 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2655 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2656}
2657
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658// --- CursorInputMapperTest ---
2659
2660class CursorInputMapperTest : public InputMapperTest {
2661protected:
2662 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2663
2664 sp<FakePointerController> mFakePointerController;
2665
Prabir Pradhan28efc192019-11-05 01:10:04 +00002666 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 InputMapperTest::SetUp();
2668
2669 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002670 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671 }
2672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002673 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
2674 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002675
2676 void prepareDisplay(int32_t orientation) {
2677 const std::string uniqueId = "local:0";
2678 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2679 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2680 orientation, uniqueId, NO_PORT, viewportType);
2681 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682};
2683
2684const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002686void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
2687 int32_t originalY, int32_t rotatedX,
2688 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 NotifyMotionArgs args;
2690
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002691 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2692 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2693 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2697 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2698 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2699 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2700}
2701
2702TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002704 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002706 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707}
2708
2709TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002711 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002713 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714}
2715
2716TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002718 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719
2720 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002721 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002722
2723 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002724 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2725 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2727 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2728
2729 // When the bounds are set, then there should be a valid motion range.
2730 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2731
2732 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002733 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002734
2735 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2736 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2737 1, 800 - 1, 0.0f, 0.0f));
2738 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2739 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2740 2, 480 - 1, 0.0f, 0.0f));
2741 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2742 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2743 0.0f, 1.0f, 0.0f, 0.0f));
2744}
2745
2746TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002748 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749
2750 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002751 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752
2753 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2754 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2755 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2756 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2757 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2758 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2759 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2760 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2761 0.0f, 1.0f, 0.0f, 0.0f));
2762}
2763
2764TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002766 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767
2768 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2769
2770 NotifyMotionArgs args;
2771
2772 // Button press.
2773 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002774 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2775 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2777 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2778 ASSERT_EQ(DEVICE_ID, args.deviceId);
2779 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2780 ASSERT_EQ(uint32_t(0), args.policyFlags);
2781 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2782 ASSERT_EQ(0, args.flags);
2783 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2784 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2785 ASSERT_EQ(0, args.edgeFlags);
2786 ASSERT_EQ(uint32_t(1), args.pointerCount);
2787 ASSERT_EQ(0, args.pointerProperties[0].id);
2788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2790 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2791 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2792 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2793 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2794
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2796 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2797 ASSERT_EQ(DEVICE_ID, args.deviceId);
2798 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2799 ASSERT_EQ(uint32_t(0), args.policyFlags);
2800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2801 ASSERT_EQ(0, args.flags);
2802 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2803 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2804 ASSERT_EQ(0, args.edgeFlags);
2805 ASSERT_EQ(uint32_t(1), args.pointerCount);
2806 ASSERT_EQ(0, args.pointerProperties[0].id);
2807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2809 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2810 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2811 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2812 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2813
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002815 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2816 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2818 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2819 ASSERT_EQ(DEVICE_ID, args.deviceId);
2820 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2821 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2823 ASSERT_EQ(0, args.flags);
2824 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2825 ASSERT_EQ(0, args.buttonState);
2826 ASSERT_EQ(0, args.edgeFlags);
2827 ASSERT_EQ(uint32_t(1), args.pointerCount);
2828 ASSERT_EQ(0, args.pointerProperties[0].id);
2829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2831 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2832 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2833 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2834 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2835
2836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2837 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2838 ASSERT_EQ(DEVICE_ID, args.deviceId);
2839 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2840 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2842 ASSERT_EQ(0, args.flags);
2843 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2844 ASSERT_EQ(0, args.buttonState);
2845 ASSERT_EQ(0, args.edgeFlags);
2846 ASSERT_EQ(uint32_t(1), args.pointerCount);
2847 ASSERT_EQ(0, args.pointerProperties[0].id);
2848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2850 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2851 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2852 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2853 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2854}
2855
2856TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002858 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859
2860 NotifyMotionArgs args;
2861
2862 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002863 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2864 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2868 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2869
2870 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002871 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2872 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2876 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2877}
2878
2879TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002881 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882
2883 NotifyMotionArgs args;
2884
2885 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002886 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2887 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2889 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2891 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2892
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2894 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2896 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2897
Michael Wrightd02c5b62014-02-10 15:10:22 -08002898 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002899 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2900 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2904 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2905
2906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2909 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2910}
2911
2912TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002914 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
2916 NotifyMotionArgs args;
2917
2918 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002919 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2920 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2921 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2922 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2926 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2927 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2928
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2932 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2933 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2934
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002936 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2937 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2938 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2942 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2943 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2944
2945 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002946 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2947 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002949 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2951 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2952
2953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2956 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2957}
2958
2959TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002961 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002963 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2965 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2966 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2967 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2968 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2969 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2970 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2971 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2972}
2973
2974TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975 addConfigurationProperty("cursor.mode", "navigation");
2976 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002977 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002979 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2982 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2983 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2984 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2985 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2986 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2987 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2988
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002989 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2991 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2992 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2993 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2994 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2995 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2996 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2997 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2998
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002999 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3001 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3002 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3003 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3004 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3005 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3006 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3007 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3008
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003009 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3011 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3012 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3013 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3014 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3015 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3016 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3017 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3018}
3019
3020TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003022 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003023
3024 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3025 mFakePointerController->setPosition(100, 200);
3026 mFakePointerController->setButtonState(0);
3027
3028 NotifyMotionArgs motionArgs;
3029 NotifyKeyArgs keyArgs;
3030
3031 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003032 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3033 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3035 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3036 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3037 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3039 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3040
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3043 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3044 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3046 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3047
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003048 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3049 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003051 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052 ASSERT_EQ(0, motionArgs.buttonState);
3053 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3055 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3056
3057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003058 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059 ASSERT_EQ(0, motionArgs.buttonState);
3060 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3062 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3063
3064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003066 ASSERT_EQ(0, motionArgs.buttonState);
3067 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3069 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3070
3071 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003072 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3073 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3074 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3076 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3077 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3078 motionArgs.buttonState);
3079 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3080 mFakePointerController->getButtonState());
3081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3082 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3083
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3086 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3087 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3088 mFakePointerController->getButtonState());
3089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3090 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3091
3092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3094 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3095 motionArgs.buttonState);
3096 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3097 mFakePointerController->getButtonState());
3098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3099 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3100
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003101 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3102 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3106 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3108 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3109
3110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003112 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3113 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3115 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3116
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003117 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3118 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003120 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3121 ASSERT_EQ(0, motionArgs.buttonState);
3122 ASSERT_EQ(0, mFakePointerController->getButtonState());
3123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3124 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003125 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3126 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003127
3128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129 ASSERT_EQ(0, motionArgs.buttonState);
3130 ASSERT_EQ(0, mFakePointerController->getButtonState());
3131 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3133 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 -08003134
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3136 ASSERT_EQ(0, motionArgs.buttonState);
3137 ASSERT_EQ(0, mFakePointerController->getButtonState());
3138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3140 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3141
3142 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003143 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3144 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3146 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3147 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003148
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003150 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3152 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3154 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3155
3156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3158 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3159 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3161 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3162
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003163 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3164 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167 ASSERT_EQ(0, motionArgs.buttonState);
3168 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3170 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3171
3172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003174 ASSERT_EQ(0, motionArgs.buttonState);
3175 ASSERT_EQ(0, mFakePointerController->getButtonState());
3176
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3178 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3180 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3181 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3182
3183 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003184 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3185 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3187 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3188 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003189
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3193 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3195 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3196
3197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3199 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3200 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3202 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3203
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003204 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3205 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003208 ASSERT_EQ(0, motionArgs.buttonState);
3209 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3211 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 -08003212
3213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3214 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3215 ASSERT_EQ(0, motionArgs.buttonState);
3216 ASSERT_EQ(0, mFakePointerController->getButtonState());
3217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3218 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3219
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3221 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3222 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3223
3224 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003225 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3226 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3228 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3229 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003230
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003232 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3234 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3236 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3237
3238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3240 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3241 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3243 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3244
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003245 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3246 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 ASSERT_EQ(0, motionArgs.buttonState);
3250 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3252 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 -08003253
3254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3255 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3256 ASSERT_EQ(0, motionArgs.buttonState);
3257 ASSERT_EQ(0, mFakePointerController->getButtonState());
3258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3259 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3260
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3262 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3263 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3264
3265 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003266 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3267 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3269 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3270 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003271
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3275 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3277 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3278
3279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3281 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3282 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3284 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3285
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003286 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3287 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 ASSERT_EQ(0, motionArgs.buttonState);
3291 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3293 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 -08003294
3295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3296 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3297 ASSERT_EQ(0, motionArgs.buttonState);
3298 ASSERT_EQ(0, mFakePointerController->getButtonState());
3299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3300 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3301
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3303 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3304 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3305}
3306
3307TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003309 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310
3311 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3312 mFakePointerController->setPosition(100, 200);
3313 mFakePointerController->setButtonState(0);
3314
3315 NotifyMotionArgs args;
3316
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003317 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3318 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3319 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003321 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3322 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3324 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3325 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3326}
3327
3328TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003329 addConfigurationProperty("cursor.mode", "pointer");
3330 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003331 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003332
3333 NotifyDeviceResetArgs resetArgs;
3334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3335 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3336 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3337
3338 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3339 mFakePointerController->setPosition(100, 200);
3340 mFakePointerController->setButtonState(0);
3341
3342 NotifyMotionArgs args;
3343
3344 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003345 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3346 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3347 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3349 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3352 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3353 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3354
3355 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003356 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3357 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3359 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3360 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3362 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3364 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3367 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3368
3369 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003370 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3371 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3373 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3376 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3378 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3379 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3381 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3382
3383 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003384 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3385 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3386 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3388 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3391 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3392 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3393
3394 // Disable pointer capture and check that the device generation got bumped
3395 // and events are generated the usual way.
3396 const uint32_t generation = mFakeContext->getGeneration();
3397 mFakePolicy->setPointerCapture(false);
3398 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3399 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3400
3401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3402 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3403 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3404
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003405 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3406 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3407 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3409 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3412 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3413 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3414}
3415
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003416TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003417 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003418
Garfield Tan888a6a42020-01-09 11:39:16 -08003419 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003420 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003421 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3422 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3423 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3424 ViewportType::VIEWPORT_EXTERNAL);
3425 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3426 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3427
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003428 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3429 mFakePointerController->setPosition(100, 200);
3430 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003431
3432 NotifyMotionArgs args;
3433 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3434 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3435 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3437 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3438 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3440 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3441 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3442 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3443}
3444
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445// --- TouchInputMapperTest ---
3446
3447class TouchInputMapperTest : public InputMapperTest {
3448protected:
3449 static const int32_t RAW_X_MIN;
3450 static const int32_t RAW_X_MAX;
3451 static const int32_t RAW_Y_MIN;
3452 static const int32_t RAW_Y_MAX;
3453 static const int32_t RAW_TOUCH_MIN;
3454 static const int32_t RAW_TOUCH_MAX;
3455 static const int32_t RAW_TOOL_MIN;
3456 static const int32_t RAW_TOOL_MAX;
3457 static const int32_t RAW_PRESSURE_MIN;
3458 static const int32_t RAW_PRESSURE_MAX;
3459 static const int32_t RAW_ORIENTATION_MIN;
3460 static const int32_t RAW_ORIENTATION_MAX;
3461 static const int32_t RAW_DISTANCE_MIN;
3462 static const int32_t RAW_DISTANCE_MAX;
3463 static const int32_t RAW_TILT_MIN;
3464 static const int32_t RAW_TILT_MAX;
3465 static const int32_t RAW_ID_MIN;
3466 static const int32_t RAW_ID_MAX;
3467 static const int32_t RAW_SLOT_MIN;
3468 static const int32_t RAW_SLOT_MAX;
3469 static const float X_PRECISION;
3470 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003471 static const float X_PRECISION_VIRTUAL;
3472 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473
3474 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003475 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476
3477 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3478
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003479 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003480 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003481
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482 enum Axes {
3483 POSITION = 1 << 0,
3484 TOUCH = 1 << 1,
3485 TOOL = 1 << 2,
3486 PRESSURE = 1 << 3,
3487 ORIENTATION = 1 << 4,
3488 MINOR = 1 << 5,
3489 ID = 1 << 6,
3490 DISTANCE = 1 << 7,
3491 TILT = 1 << 8,
3492 SLOT = 1 << 9,
3493 TOOL_TYPE = 1 << 10,
3494 };
3495
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003496 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3497 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003498 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003500 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501 int32_t toRawX(float displayX);
3502 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003503 float toCookedX(float rawX, float rawY);
3504 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003506 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003508 float toDisplayY(int32_t rawY, int32_t displayHeight);
3509
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510};
3511
3512const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3513const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3514const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3515const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3516const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3517const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3518const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3519const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003520const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3521const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3523const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3524const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3525const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3526const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3527const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3528const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3529const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3530const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3531const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3532const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3533const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003534const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3535 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3536const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3537 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003538const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3539 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540
3541const float TouchInputMapperTest::GEOMETRIC_SCALE =
3542 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3543 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3544
3545const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3546 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3547 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3548};
3549
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003550void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003551 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003552 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3553}
3554
3555void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3556 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3557 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558}
3559
Santos Cordonfa5cf462017-04-05 10:37:00 -07003560void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003561 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3562 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003563 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003564}
3565
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566void TouchInputMapperTest::prepareVirtualKeys() {
3567 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3568 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3569 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3570 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3571}
3572
Jason Gerecke489fda82012-09-07 17:19:40 -07003573void TouchInputMapperTest::prepareLocationCalibration() {
3574 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3575}
3576
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577int32_t TouchInputMapperTest::toRawX(float displayX) {
3578 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3579}
3580
3581int32_t TouchInputMapperTest::toRawY(float displayY) {
3582 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3583}
3584
Jason Gerecke489fda82012-09-07 17:19:40 -07003585float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3586 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3587 return rawX;
3588}
3589
3590float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3591 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3592 return rawY;
3593}
3594
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003596 return toDisplayX(rawX, DISPLAY_WIDTH);
3597}
3598
3599float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3600 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601}
3602
3603float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003604 return toDisplayY(rawY, DISPLAY_HEIGHT);
3605}
3606
3607float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3608 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609}
3610
3611
3612// --- SingleTouchInputMapperTest ---
3613
3614class SingleTouchInputMapperTest : public TouchInputMapperTest {
3615protected:
3616 void prepareButtons();
3617 void prepareAxes(int axes);
3618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003619 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3620 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3621 void processUp(SingleTouchInputMapper& mappery);
3622 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3623 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3624 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3625 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3626 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3627 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628};
3629
3630void SingleTouchInputMapperTest::prepareButtons() {
3631 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3632}
3633
3634void SingleTouchInputMapperTest::prepareAxes(int axes) {
3635 if (axes & POSITION) {
3636 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3637 RAW_X_MIN, RAW_X_MAX, 0, 0);
3638 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3639 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3640 }
3641 if (axes & PRESSURE) {
3642 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3643 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3644 }
3645 if (axes & TOOL) {
3646 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3647 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3648 }
3649 if (axes & DISTANCE) {
3650 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3651 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3652 }
3653 if (axes & TILT) {
3654 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3655 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3656 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3657 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3658 }
3659}
3660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003661void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003662 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3663 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3664 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665}
3666
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003667void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003668 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3669 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670}
3671
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003672void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003673 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674}
3675
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003676void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003677 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678}
3679
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003680void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
3681 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003682 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683}
3684
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003685void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003686 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687}
3688
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003689void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
3690 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003691 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3692 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693}
3694
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003695void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
3696 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003697 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698}
3699
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003700void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003701 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702}
3703
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705 prepareButtons();
3706 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003708
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003709 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710}
3711
3712TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3714 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3715 prepareButtons();
3716 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003717 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003719 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720}
3721
3722TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003723 prepareButtons();
3724 prepareAxes(POSITION);
3725 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003726 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729}
3730
3731TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 prepareButtons();
3733 prepareAxes(POSITION);
3734 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738}
3739
3740TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741 addConfigurationProperty("touch.deviceType", "touchScreen");
3742 prepareDisplay(DISPLAY_ORIENTATION_0);
3743 prepareButtons();
3744 prepareAxes(POSITION);
3745 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747
3748 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003749 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750
3751 // Virtual key is down.
3752 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3753 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3754 processDown(mapper, x, y);
3755 processSync(mapper);
3756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3757
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003758 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
3760 // Virtual key is up.
3761 processUp(mapper);
3762 processSync(mapper);
3763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3764
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003765 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766}
3767
3768TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769 addConfigurationProperty("touch.deviceType", "touchScreen");
3770 prepareDisplay(DISPLAY_ORIENTATION_0);
3771 prepareButtons();
3772 prepareAxes(POSITION);
3773 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775
3776 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778
3779 // Virtual key is down.
3780 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3781 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3782 processDown(mapper, x, y);
3783 processSync(mapper);
3784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3785
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003786 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003787
3788 // Virtual key is up.
3789 processUp(mapper);
3790 processSync(mapper);
3791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3792
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794}
3795
3796TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797 addConfigurationProperty("touch.deviceType", "touchScreen");
3798 prepareDisplay(DISPLAY_ORIENTATION_0);
3799 prepareButtons();
3800 prepareAxes(POSITION);
3801 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003802 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803
3804 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3805 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807 ASSERT_TRUE(flags[0]);
3808 ASSERT_FALSE(flags[1]);
3809}
3810
3811TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812 addConfigurationProperty("touch.deviceType", "touchScreen");
3813 prepareDisplay(DISPLAY_ORIENTATION_0);
3814 prepareButtons();
3815 prepareAxes(POSITION);
3816 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003817 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818
3819 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3820
3821 NotifyKeyArgs args;
3822
3823 // Press virtual key.
3824 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3825 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3826 processDown(mapper, x, y);
3827 processSync(mapper);
3828
3829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3830 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3831 ASSERT_EQ(DEVICE_ID, args.deviceId);
3832 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3833 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3834 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3835 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3836 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3837 ASSERT_EQ(KEY_HOME, args.scanCode);
3838 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3839 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3840
3841 // Release virtual key.
3842 processUp(mapper);
3843 processSync(mapper);
3844
3845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3846 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3847 ASSERT_EQ(DEVICE_ID, args.deviceId);
3848 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3849 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3850 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3851 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3852 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3853 ASSERT_EQ(KEY_HOME, args.scanCode);
3854 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3855 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3856
3857 // Should not have sent any motions.
3858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3859}
3860
3861TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862 addConfigurationProperty("touch.deviceType", "touchScreen");
3863 prepareDisplay(DISPLAY_ORIENTATION_0);
3864 prepareButtons();
3865 prepareAxes(POSITION);
3866 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003867 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868
3869 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3870
3871 NotifyKeyArgs keyArgs;
3872
3873 // Press virtual key.
3874 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3875 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3876 processDown(mapper, x, y);
3877 processSync(mapper);
3878
3879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3880 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3881 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3882 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3883 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3884 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3885 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3886 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3887 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3888 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3889 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3890
3891 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3892 // into the display area.
3893 y -= 100;
3894 processMove(mapper, x, y);
3895 processSync(mapper);
3896
3897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3898 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3899 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3900 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3901 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3902 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3903 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3904 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3905 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3906 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3907 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3908 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3909
3910 NotifyMotionArgs motionArgs;
3911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3913 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3914 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3915 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3916 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3917 ASSERT_EQ(0, motionArgs.flags);
3918 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3919 ASSERT_EQ(0, motionArgs.buttonState);
3920 ASSERT_EQ(0, motionArgs.edgeFlags);
3921 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3922 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3925 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3926 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3927 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3928 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3929
3930 // Keep moving out of bounds. Should generate a pointer move.
3931 y -= 50;
3932 processMove(mapper, x, y);
3933 processSync(mapper);
3934
3935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3936 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3937 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3938 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3939 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3941 ASSERT_EQ(0, motionArgs.flags);
3942 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3943 ASSERT_EQ(0, motionArgs.buttonState);
3944 ASSERT_EQ(0, motionArgs.edgeFlags);
3945 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3946 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3949 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3950 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3951 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3952 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3953
3954 // Release out of bounds. Should generate a pointer up.
3955 processUp(mapper);
3956 processSync(mapper);
3957
3958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3959 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3960 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3961 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3962 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3963 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3964 ASSERT_EQ(0, motionArgs.flags);
3965 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3966 ASSERT_EQ(0, motionArgs.buttonState);
3967 ASSERT_EQ(0, motionArgs.edgeFlags);
3968 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3969 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3972 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3973 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3974 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3975 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3976
3977 // Should not have sent any more keys or motions.
3978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3980}
3981
3982TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983 addConfigurationProperty("touch.deviceType", "touchScreen");
3984 prepareDisplay(DISPLAY_ORIENTATION_0);
3985 prepareButtons();
3986 prepareAxes(POSITION);
3987 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003988 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003989
3990 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3991
3992 NotifyMotionArgs motionArgs;
3993
3994 // Initially go down out of bounds.
3995 int32_t x = -10;
3996 int32_t y = -10;
3997 processDown(mapper, x, y);
3998 processSync(mapper);
3999
4000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4001
4002 // Move into the display area. Should generate a pointer down.
4003 x = 50;
4004 y = 75;
4005 processMove(mapper, x, y);
4006 processSync(mapper);
4007
4008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4009 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4010 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4011 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4012 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4013 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4014 ASSERT_EQ(0, motionArgs.flags);
4015 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4016 ASSERT_EQ(0, motionArgs.buttonState);
4017 ASSERT_EQ(0, motionArgs.edgeFlags);
4018 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4019 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4022 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4023 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4024 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4025 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4026
4027 // Release. Should generate a pointer up.
4028 processUp(mapper);
4029 processSync(mapper);
4030
4031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4032 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4033 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4034 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4035 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4036 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4037 ASSERT_EQ(0, motionArgs.flags);
4038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4039 ASSERT_EQ(0, motionArgs.buttonState);
4040 ASSERT_EQ(0, motionArgs.edgeFlags);
4041 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4042 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4045 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4046 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4047 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4049
4050 // Should not have sent any more keys or motions.
4051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4053}
4054
Santos Cordonfa5cf462017-04-05 10:37:00 -07004055TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004056 addConfigurationProperty("touch.deviceType", "touchScreen");
4057 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4058
4059 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4060 prepareButtons();
4061 prepareAxes(POSITION);
4062 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004064
4065 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4066
4067 NotifyMotionArgs motionArgs;
4068
4069 // Down.
4070 int32_t x = 100;
4071 int32_t y = 125;
4072 processDown(mapper, x, y);
4073 processSync(mapper);
4074
4075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4077 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4078 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4079 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4080 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4081 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4082 ASSERT_EQ(0, motionArgs.flags);
4083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4084 ASSERT_EQ(0, motionArgs.buttonState);
4085 ASSERT_EQ(0, motionArgs.edgeFlags);
4086 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4087 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4090 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4091 1, 0, 0, 0, 0, 0, 0, 0));
4092 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4093 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4094 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4095
4096 // Move.
4097 x += 50;
4098 y += 75;
4099 processMove(mapper, x, y);
4100 processSync(mapper);
4101
4102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4103 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4104 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4105 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4107 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4109 ASSERT_EQ(0, motionArgs.flags);
4110 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4111 ASSERT_EQ(0, motionArgs.buttonState);
4112 ASSERT_EQ(0, motionArgs.edgeFlags);
4113 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4117 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4118 1, 0, 0, 0, 0, 0, 0, 0));
4119 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4120 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4121 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4122
4123 // Up.
4124 processUp(mapper);
4125 processSync(mapper);
4126
4127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4128 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4129 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4130 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4131 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4132 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4133 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4134 ASSERT_EQ(0, motionArgs.flags);
4135 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4136 ASSERT_EQ(0, motionArgs.buttonState);
4137 ASSERT_EQ(0, motionArgs.edgeFlags);
4138 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4139 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4142 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4143 1, 0, 0, 0, 0, 0, 0, 0));
4144 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4145 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4147
4148 // Should not have sent any more keys or motions.
4149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4151}
4152
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 addConfigurationProperty("touch.deviceType", "touchScreen");
4155 prepareDisplay(DISPLAY_ORIENTATION_0);
4156 prepareButtons();
4157 prepareAxes(POSITION);
4158 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004159 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
4161 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4162
4163 NotifyMotionArgs motionArgs;
4164
4165 // Down.
4166 int32_t x = 100;
4167 int32_t y = 125;
4168 processDown(mapper, x, y);
4169 processSync(mapper);
4170
4171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4173 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4174 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4175 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4177 ASSERT_EQ(0, motionArgs.flags);
4178 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4179 ASSERT_EQ(0, motionArgs.buttonState);
4180 ASSERT_EQ(0, motionArgs.edgeFlags);
4181 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4182 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4185 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4186 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4187 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4188 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4189
4190 // Move.
4191 x += 50;
4192 y += 75;
4193 processMove(mapper, x, y);
4194 processSync(mapper);
4195
4196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4197 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4198 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4199 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4200 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4202 ASSERT_EQ(0, motionArgs.flags);
4203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4204 ASSERT_EQ(0, motionArgs.buttonState);
4205 ASSERT_EQ(0, motionArgs.edgeFlags);
4206 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4210 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4211 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4212 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4214
4215 // Up.
4216 processUp(mapper);
4217 processSync(mapper);
4218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4220 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4221 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4222 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4223 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4224 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4225 ASSERT_EQ(0, motionArgs.flags);
4226 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4227 ASSERT_EQ(0, motionArgs.buttonState);
4228 ASSERT_EQ(0, motionArgs.edgeFlags);
4229 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4230 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4233 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4234 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4235 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4237
4238 // Should not have sent any more keys or motions.
4239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4241}
4242
4243TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 addConfigurationProperty("touch.deviceType", "touchScreen");
4245 prepareButtons();
4246 prepareAxes(POSITION);
4247 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004248 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249
4250 NotifyMotionArgs args;
4251
4252 // Rotation 90.
4253 prepareDisplay(DISPLAY_ORIENTATION_90);
4254 processDown(mapper, toRawX(50), toRawY(75));
4255 processSync(mapper);
4256
4257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4258 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4259 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4260
4261 processUp(mapper);
4262 processSync(mapper);
4263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4264}
4265
4266TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 addConfigurationProperty("touch.deviceType", "touchScreen");
4268 prepareButtons();
4269 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004270 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 NotifyMotionArgs args;
4273
4274 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004275 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 prepareDisplay(DISPLAY_ORIENTATION_0);
4277 processDown(mapper, toRawX(50), toRawY(75));
4278 processSync(mapper);
4279
4280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4281 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4282 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4283
4284 processUp(mapper);
4285 processSync(mapper);
4286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4287
4288 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004289 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 prepareDisplay(DISPLAY_ORIENTATION_90);
4291 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4292 processSync(mapper);
4293
4294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4295 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4296 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4297
4298 processUp(mapper);
4299 processSync(mapper);
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4301
4302 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004303 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 prepareDisplay(DISPLAY_ORIENTATION_180);
4305 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4306 processSync(mapper);
4307
4308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4309 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4310 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4311
4312 processUp(mapper);
4313 processSync(mapper);
4314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4315
4316 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004317 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 prepareDisplay(DISPLAY_ORIENTATION_270);
4319 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4320 processSync(mapper);
4321
4322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4323 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4324 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4325
4326 processUp(mapper);
4327 processSync(mapper);
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4329}
4330
4331TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 addConfigurationProperty("touch.deviceType", "touchScreen");
4333 prepareDisplay(DISPLAY_ORIENTATION_0);
4334 prepareButtons();
4335 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004336 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337
4338 // These calculations are based on the input device calibration documentation.
4339 int32_t rawX = 100;
4340 int32_t rawY = 200;
4341 int32_t rawPressure = 10;
4342 int32_t rawToolMajor = 12;
4343 int32_t rawDistance = 2;
4344 int32_t rawTiltX = 30;
4345 int32_t rawTiltY = 110;
4346
4347 float x = toDisplayX(rawX);
4348 float y = toDisplayY(rawY);
4349 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4350 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4351 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4352 float distance = float(rawDistance);
4353
4354 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4355 float tiltScale = M_PI / 180;
4356 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4357 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4358 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4359 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4360
4361 processDown(mapper, rawX, rawY);
4362 processPressure(mapper, rawPressure);
4363 processToolMajor(mapper, rawToolMajor);
4364 processDistance(mapper, rawDistance);
4365 processTilt(mapper, rawTiltX, rawTiltY);
4366 processSync(mapper);
4367
4368 NotifyMotionArgs args;
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4371 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4372 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4373}
4374
Jason Gerecke489fda82012-09-07 17:19:40 -07004375TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004376 addConfigurationProperty("touch.deviceType", "touchScreen");
4377 prepareDisplay(DISPLAY_ORIENTATION_0);
4378 prepareLocationCalibration();
4379 prepareButtons();
4380 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004381 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004382
4383 int32_t rawX = 100;
4384 int32_t rawY = 200;
4385
4386 float x = toDisplayX(toCookedX(rawX, rawY));
4387 float y = toDisplayY(toCookedY(rawX, rawY));
4388
4389 processDown(mapper, rawX, rawY);
4390 processSync(mapper);
4391
4392 NotifyMotionArgs args;
4393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4395 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4396}
4397
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 addConfigurationProperty("touch.deviceType", "touchScreen");
4400 prepareDisplay(DISPLAY_ORIENTATION_0);
4401 prepareButtons();
4402 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004403 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404
4405 NotifyMotionArgs motionArgs;
4406 NotifyKeyArgs keyArgs;
4407
4408 processDown(mapper, 100, 200);
4409 processSync(mapper);
4410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4411 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4412 ASSERT_EQ(0, motionArgs.buttonState);
4413
4414 // press BTN_LEFT, release BTN_LEFT
4415 processKey(mapper, BTN_LEFT, 1);
4416 processSync(mapper);
4417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4419 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4420
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4423 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4424
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 processKey(mapper, BTN_LEFT, 0);
4426 processSync(mapper);
4427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004430
4431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
4435 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4436 processKey(mapper, BTN_RIGHT, 1);
4437 processKey(mapper, BTN_MIDDLE, 1);
4438 processSync(mapper);
4439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4441 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4442 motionArgs.buttonState);
4443
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4446 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4447
4448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4450 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4451 motionArgs.buttonState);
4452
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 processKey(mapper, BTN_RIGHT, 0);
4454 processSync(mapper);
4455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004458
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462
4463 processKey(mapper, BTN_MIDDLE, 0);
4464 processSync(mapper);
4465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004468
4469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004471 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472
4473 // press BTN_BACK, release BTN_BACK
4474 processKey(mapper, BTN_BACK, 1);
4475 processSync(mapper);
4476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4477 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4478 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4483
4484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4485 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4486 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487
4488 processKey(mapper, BTN_BACK, 0);
4489 processSync(mapper);
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493
4494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004496 ASSERT_EQ(0, motionArgs.buttonState);
4497
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4499 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4500 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4501
4502 // press BTN_SIDE, release BTN_SIDE
4503 processKey(mapper, BTN_SIDE, 1);
4504 processSync(mapper);
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4506 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4507 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004508
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004511 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4512
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4515 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516
4517 processKey(mapper, BTN_SIDE, 0);
4518 processSync(mapper);
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004522
4523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004525 ASSERT_EQ(0, motionArgs.buttonState);
4526
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4528 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4529 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4530
4531 // press BTN_FORWARD, release BTN_FORWARD
4532 processKey(mapper, BTN_FORWARD, 1);
4533 processSync(mapper);
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4535 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4536 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004537
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004540 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4541
4542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4544 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545
4546 processKey(mapper, BTN_FORWARD, 0);
4547 processSync(mapper);
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004551
4552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554 ASSERT_EQ(0, motionArgs.buttonState);
4555
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4557 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4558 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4559
4560 // press BTN_EXTRA, release BTN_EXTRA
4561 processKey(mapper, BTN_EXTRA, 1);
4562 processSync(mapper);
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4564 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4565 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004569 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4570
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4573 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574
4575 processKey(mapper, BTN_EXTRA, 0);
4576 processSync(mapper);
4577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004580
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004583 ASSERT_EQ(0, motionArgs.buttonState);
4584
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4586 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4587 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4588
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4590
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 // press BTN_STYLUS, release BTN_STYLUS
4592 processKey(mapper, BTN_STYLUS, 1);
4593 processSync(mapper);
4594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004596 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4597
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4600 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601
4602 processKey(mapper, BTN_STYLUS, 0);
4603 processSync(mapper);
4604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004605 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607
4608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004610 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611
4612 // press BTN_STYLUS2, release BTN_STYLUS2
4613 processKey(mapper, BTN_STYLUS2, 1);
4614 processSync(mapper);
4615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004617 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4618
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4620 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4621 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622
4623 processKey(mapper, BTN_STYLUS2, 0);
4624 processSync(mapper);
4625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004626 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004628
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004631 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
4633 // release touch
4634 processUp(mapper);
4635 processSync(mapper);
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4637 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4638 ASSERT_EQ(0, motionArgs.buttonState);
4639}
4640
4641TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642 addConfigurationProperty("touch.deviceType", "touchScreen");
4643 prepareDisplay(DISPLAY_ORIENTATION_0);
4644 prepareButtons();
4645 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004646 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647
4648 NotifyMotionArgs motionArgs;
4649
4650 // default tool type is finger
4651 processDown(mapper, 100, 200);
4652 processSync(mapper);
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4654 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4656
4657 // eraser
4658 processKey(mapper, BTN_TOOL_RUBBER, 1);
4659 processSync(mapper);
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4663
4664 // stylus
4665 processKey(mapper, BTN_TOOL_RUBBER, 0);
4666 processKey(mapper, BTN_TOOL_PEN, 1);
4667 processSync(mapper);
4668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4671
4672 // brush
4673 processKey(mapper, BTN_TOOL_PEN, 0);
4674 processKey(mapper, BTN_TOOL_BRUSH, 1);
4675 processSync(mapper);
4676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4679
4680 // pencil
4681 processKey(mapper, BTN_TOOL_BRUSH, 0);
4682 processKey(mapper, BTN_TOOL_PENCIL, 1);
4683 processSync(mapper);
4684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4685 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4687
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004688 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 processKey(mapper, BTN_TOOL_PENCIL, 0);
4690 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4691 processSync(mapper);
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4695
4696 // mouse
4697 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4698 processKey(mapper, BTN_TOOL_MOUSE, 1);
4699 processSync(mapper);
4700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4703
4704 // lens
4705 processKey(mapper, BTN_TOOL_MOUSE, 0);
4706 processKey(mapper, BTN_TOOL_LENS, 1);
4707 processSync(mapper);
4708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4711
4712 // double-tap
4713 processKey(mapper, BTN_TOOL_LENS, 0);
4714 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4715 processSync(mapper);
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4719
4720 // triple-tap
4721 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4722 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4723 processSync(mapper);
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4727
4728 // quad-tap
4729 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4730 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4731 processSync(mapper);
4732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4735
4736 // finger
4737 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4738 processKey(mapper, BTN_TOOL_FINGER, 1);
4739 processSync(mapper);
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4743
4744 // stylus trumps finger
4745 processKey(mapper, BTN_TOOL_PEN, 1);
4746 processSync(mapper);
4747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4750
4751 // eraser trumps stylus
4752 processKey(mapper, BTN_TOOL_RUBBER, 1);
4753 processSync(mapper);
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4757
4758 // mouse trumps eraser
4759 processKey(mapper, BTN_TOOL_MOUSE, 1);
4760 processSync(mapper);
4761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4764
4765 // back to default tool type
4766 processKey(mapper, BTN_TOOL_MOUSE, 0);
4767 processKey(mapper, BTN_TOOL_RUBBER, 0);
4768 processKey(mapper, BTN_TOOL_PEN, 0);
4769 processKey(mapper, BTN_TOOL_FINGER, 0);
4770 processSync(mapper);
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4774}
4775
4776TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777 addConfigurationProperty("touch.deviceType", "touchScreen");
4778 prepareDisplay(DISPLAY_ORIENTATION_0);
4779 prepareButtons();
4780 prepareAxes(POSITION);
4781 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004782 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783
4784 NotifyMotionArgs motionArgs;
4785
4786 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4787 processKey(mapper, BTN_TOOL_FINGER, 1);
4788 processMove(mapper, 100, 200);
4789 processSync(mapper);
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4791 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4793 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4794
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4798 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4799
4800 // move a little
4801 processMove(mapper, 150, 250);
4802 processSync(mapper);
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4806 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4807
4808 // down when BTN_TOUCH is pressed, pressure defaults to 1
4809 processKey(mapper, BTN_TOUCH, 1);
4810 processSync(mapper);
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4812 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4814 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4815
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4817 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4819 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4820
4821 // up when BTN_TOUCH is released, hover restored
4822 processKey(mapper, BTN_TOUCH, 0);
4823 processSync(mapper);
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4825 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4827 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4828
4829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4832 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4833
4834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4837 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4838
4839 // exit hover when pointer goes away
4840 processKey(mapper, BTN_TOOL_FINGER, 0);
4841 processSync(mapper);
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4845 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4846}
4847
4848TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849 addConfigurationProperty("touch.deviceType", "touchScreen");
4850 prepareDisplay(DISPLAY_ORIENTATION_0);
4851 prepareButtons();
4852 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004853 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854
4855 NotifyMotionArgs motionArgs;
4856
4857 // initially hovering because pressure is 0
4858 processDown(mapper, 100, 200);
4859 processPressure(mapper, 0);
4860 processSync(mapper);
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4862 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4864 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4865
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4867 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4869 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4870
4871 // move a little
4872 processMove(mapper, 150, 250);
4873 processSync(mapper);
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4875 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4877 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4878
4879 // down when pressure is non-zero
4880 processPressure(mapper, RAW_PRESSURE_MAX);
4881 processSync(mapper);
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4883 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4885 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4886
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4888 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4890 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4891
4892 // up when pressure becomes 0, hover restored
4893 processPressure(mapper, 0);
4894 processSync(mapper);
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4896 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4898 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4899
4900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4903 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4904
4905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4906 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4908 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4909
4910 // exit hover when pointer goes away
4911 processUp(mapper);
4912 processSync(mapper);
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4914 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4916 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4917}
4918
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919// --- MultiTouchInputMapperTest ---
4920
4921class MultiTouchInputMapperTest : public TouchInputMapperTest {
4922protected:
4923 void prepareAxes(int axes);
4924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004925 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
4926 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
4927 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
4928 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
4929 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
4930 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
4931 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
4932 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
4933 void processId(MultiTouchInputMapper& mapper, int32_t id);
4934 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
4935 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
4936 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
4937 void processMTSync(MultiTouchInputMapper& mapper);
4938 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939};
4940
4941void MultiTouchInputMapperTest::prepareAxes(int axes) {
4942 if (axes & POSITION) {
4943 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4944 RAW_X_MIN, RAW_X_MAX, 0, 0);
4945 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4946 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4947 }
4948 if (axes & TOUCH) {
4949 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4950 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4951 if (axes & MINOR) {
4952 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4953 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4954 }
4955 }
4956 if (axes & TOOL) {
4957 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4958 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4959 if (axes & MINOR) {
4960 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4961 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4962 }
4963 }
4964 if (axes & ORIENTATION) {
4965 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4966 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4967 }
4968 if (axes & PRESSURE) {
4969 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4970 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4971 }
4972 if (axes & DISTANCE) {
4973 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4974 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4975 }
4976 if (axes & ID) {
4977 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4978 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4979 }
4980 if (axes & SLOT) {
4981 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4982 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4983 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4984 }
4985 if (axes & TOOL_TYPE) {
4986 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4987 0, MT_TOOL_MAX, 0, 0);
4988 }
4989}
4990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004991void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
4992 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004993 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4994 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995}
4996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
4998 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004999 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000}
5001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005002void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5003 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005004 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005}
5006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005007void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005008 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009}
5010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005011void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005012 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013}
5014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5016 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005017 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018}
5019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005020void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005021 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022}
5023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005024void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005025 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026}
5027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005028void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005029 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030}
5031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005032void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005033 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034}
5035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005036void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005037 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038}
5039
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005040void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5041 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005042 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043}
5044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005045void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005046 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047}
5048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005049void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005050 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005051}
5052
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 addConfigurationProperty("touch.deviceType", "touchScreen");
5055 prepareDisplay(DISPLAY_ORIENTATION_0);
5056 prepareAxes(POSITION);
5057 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005058 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059
5060 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5061
5062 NotifyMotionArgs motionArgs;
5063
5064 // Two fingers down at once.
5065 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5066 processPosition(mapper, x1, y1);
5067 processMTSync(mapper);
5068 processPosition(mapper, x2, y2);
5069 processMTSync(mapper);
5070 processSync(mapper);
5071
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5073 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5074 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5075 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5076 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5077 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5078 ASSERT_EQ(0, motionArgs.flags);
5079 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5080 ASSERT_EQ(0, motionArgs.buttonState);
5081 ASSERT_EQ(0, motionArgs.edgeFlags);
5082 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5083 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5086 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5087 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5088 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5089 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5090
5091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5092 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5093 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5094 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5095 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5096 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5097 motionArgs.action);
5098 ASSERT_EQ(0, motionArgs.flags);
5099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5100 ASSERT_EQ(0, motionArgs.buttonState);
5101 ASSERT_EQ(0, motionArgs.edgeFlags);
5102 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5103 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5105 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5108 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5110 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5111 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5112 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5113 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5114
5115 // Move.
5116 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5117 processPosition(mapper, x1, y1);
5118 processMTSync(mapper);
5119 processPosition(mapper, x2, y2);
5120 processMTSync(mapper);
5121 processSync(mapper);
5122
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5125 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5126 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5127 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5129 ASSERT_EQ(0, motionArgs.flags);
5130 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5131 ASSERT_EQ(0, motionArgs.buttonState);
5132 ASSERT_EQ(0, motionArgs.edgeFlags);
5133 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5134 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5136 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5139 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5141 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5142 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5143 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5144 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5145
5146 // First finger up.
5147 x2 += 15; y2 -= 20;
5148 processPosition(mapper, x2, y2);
5149 processMTSync(mapper);
5150 processSync(mapper);
5151
5152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5153 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5154 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5155 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5156 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5157 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5158 motionArgs.action);
5159 ASSERT_EQ(0, motionArgs.flags);
5160 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5161 ASSERT_EQ(0, motionArgs.buttonState);
5162 ASSERT_EQ(0, motionArgs.edgeFlags);
5163 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5164 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5166 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5169 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5171 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5172 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5173 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5174 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5175
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5177 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5178 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5179 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5180 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5182 ASSERT_EQ(0, motionArgs.flags);
5183 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5184 ASSERT_EQ(0, motionArgs.buttonState);
5185 ASSERT_EQ(0, motionArgs.edgeFlags);
5186 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5187 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5190 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5191 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5192 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5193 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5194
5195 // Move.
5196 x2 += 20; y2 -= 25;
5197 processPosition(mapper, x2, y2);
5198 processMTSync(mapper);
5199 processSync(mapper);
5200
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5202 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5203 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5204 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5205 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5207 ASSERT_EQ(0, motionArgs.flags);
5208 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5209 ASSERT_EQ(0, motionArgs.buttonState);
5210 ASSERT_EQ(0, motionArgs.edgeFlags);
5211 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5212 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5215 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5216 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5217 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5218 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5219
5220 // New finger down.
5221 int32_t x3 = 700, y3 = 300;
5222 processPosition(mapper, x2, y2);
5223 processMTSync(mapper);
5224 processPosition(mapper, x3, y3);
5225 processMTSync(mapper);
5226 processSync(mapper);
5227
5228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5229 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5230 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5231 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5232 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5233 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5234 motionArgs.action);
5235 ASSERT_EQ(0, motionArgs.flags);
5236 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5237 ASSERT_EQ(0, motionArgs.buttonState);
5238 ASSERT_EQ(0, motionArgs.edgeFlags);
5239 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5240 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5242 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5245 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5247 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5248 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5249 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5251
5252 // Second finger up.
5253 x3 += 30; y3 -= 20;
5254 processPosition(mapper, x3, y3);
5255 processMTSync(mapper);
5256 processSync(mapper);
5257
5258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5260 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5261 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5262 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5263 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5264 motionArgs.action);
5265 ASSERT_EQ(0, motionArgs.flags);
5266 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5267 ASSERT_EQ(0, motionArgs.buttonState);
5268 ASSERT_EQ(0, motionArgs.edgeFlags);
5269 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5270 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5272 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5275 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5277 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5278 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5279 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5280 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5281
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5284 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5285 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5286 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5288 ASSERT_EQ(0, motionArgs.flags);
5289 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5290 ASSERT_EQ(0, motionArgs.buttonState);
5291 ASSERT_EQ(0, motionArgs.edgeFlags);
5292 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5293 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5296 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5297 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5298 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5299 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5300
5301 // Last finger up.
5302 processMTSync(mapper);
5303 processSync(mapper);
5304
5305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5306 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5307 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5308 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5309 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5310 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5311 ASSERT_EQ(0, motionArgs.flags);
5312 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5313 ASSERT_EQ(0, motionArgs.buttonState);
5314 ASSERT_EQ(0, motionArgs.edgeFlags);
5315 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5316 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5319 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5320 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5321 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5323
5324 // Should not have sent any more keys or motions.
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5327}
5328
5329TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 addConfigurationProperty("touch.deviceType", "touchScreen");
5331 prepareDisplay(DISPLAY_ORIENTATION_0);
5332 prepareAxes(POSITION | ID);
5333 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335
5336 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5337
5338 NotifyMotionArgs motionArgs;
5339
5340 // Two fingers down at once.
5341 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5342 processPosition(mapper, x1, y1);
5343 processId(mapper, 1);
5344 processMTSync(mapper);
5345 processPosition(mapper, x2, y2);
5346 processId(mapper, 2);
5347 processMTSync(mapper);
5348 processSync(mapper);
5349
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5352 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5353 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5357
5358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5359 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5360 motionArgs.action);
5361 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5364 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5367 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5369 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5370
5371 // Move.
5372 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5373 processPosition(mapper, x1, y1);
5374 processId(mapper, 1);
5375 processMTSync(mapper);
5376 processPosition(mapper, x2, y2);
5377 processId(mapper, 2);
5378 processMTSync(mapper);
5379 processSync(mapper);
5380
5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5383 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5384 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5386 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5389 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5391 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5392
5393 // First finger up.
5394 x2 += 15; y2 -= 20;
5395 processPosition(mapper, x2, y2);
5396 processId(mapper, 2);
5397 processMTSync(mapper);
5398 processSync(mapper);
5399
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5401 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5402 motionArgs.action);
5403 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5404 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5406 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5409 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5411 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5412
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5415 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5416 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5419 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5420
5421 // Move.
5422 x2 += 20; y2 -= 25;
5423 processPosition(mapper, x2, y2);
5424 processId(mapper, 2);
5425 processMTSync(mapper);
5426 processSync(mapper);
5427
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5430 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5431 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5434 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5435
5436 // New finger down.
5437 int32_t x3 = 700, y3 = 300;
5438 processPosition(mapper, x2, y2);
5439 processId(mapper, 2);
5440 processMTSync(mapper);
5441 processPosition(mapper, x3, y3);
5442 processId(mapper, 3);
5443 processMTSync(mapper);
5444 processSync(mapper);
5445
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5447 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5448 motionArgs.action);
5449 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5452 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5455 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5457 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5458
5459 // Second finger up.
5460 x3 += 30; y3 -= 20;
5461 processPosition(mapper, x3, y3);
5462 processId(mapper, 3);
5463 processMTSync(mapper);
5464 processSync(mapper);
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5468 motionArgs.action);
5469 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5472 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5475 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5477 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5478
5479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5481 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5485 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5486
5487 // Last finger up.
5488 processMTSync(mapper);
5489 processSync(mapper);
5490
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5492 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5493 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5497 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5498
5499 // Should not have sent any more keys or motions.
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5502}
5503
5504TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 addConfigurationProperty("touch.deviceType", "touchScreen");
5506 prepareDisplay(DISPLAY_ORIENTATION_0);
5507 prepareAxes(POSITION | ID | SLOT);
5508 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005509 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510
5511 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5512
5513 NotifyMotionArgs motionArgs;
5514
5515 // Two fingers down at once.
5516 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5517 processPosition(mapper, x1, y1);
5518 processId(mapper, 1);
5519 processSlot(mapper, 1);
5520 processPosition(mapper, x2, y2);
5521 processId(mapper, 2);
5522 processSync(mapper);
5523
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5525 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5526 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5527 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5530 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5531
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5533 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5534 motionArgs.action);
5535 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5536 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5538 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5541 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5543 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5544
5545 // Move.
5546 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5547 processSlot(mapper, 0);
5548 processPosition(mapper, x1, y1);
5549 processSlot(mapper, 1);
5550 processPosition(mapper, x2, y2);
5551 processSync(mapper);
5552
5553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5555 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5556 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5558 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5561 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5563 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5564
5565 // First finger up.
5566 x2 += 15; y2 -= 20;
5567 processSlot(mapper, 0);
5568 processId(mapper, -1);
5569 processSlot(mapper, 1);
5570 processPosition(mapper, x2, y2);
5571 processSync(mapper);
5572
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5574 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5575 motionArgs.action);
5576 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5577 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5579 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5582 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5584 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5585
5586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5588 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5589 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5592 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5593
5594 // Move.
5595 x2 += 20; y2 -= 25;
5596 processPosition(mapper, x2, y2);
5597 processSync(mapper);
5598
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5601 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5602 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5605 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5606
5607 // New finger down.
5608 int32_t x3 = 700, y3 = 300;
5609 processPosition(mapper, x2, y2);
5610 processSlot(mapper, 0);
5611 processId(mapper, 3);
5612 processPosition(mapper, x3, y3);
5613 processSync(mapper);
5614
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5616 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5617 motionArgs.action);
5618 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5619 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5621 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5624 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5626 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5627
5628 // Second finger up.
5629 x3 += 30; y3 -= 20;
5630 processSlot(mapper, 1);
5631 processId(mapper, -1);
5632 processSlot(mapper, 0);
5633 processPosition(mapper, x3, y3);
5634 processSync(mapper);
5635
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5637 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5638 motionArgs.action);
5639 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5640 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5642 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5645 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5647 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5648
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5651 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5655 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5656
5657 // Last finger up.
5658 processId(mapper, -1);
5659 processSync(mapper);
5660
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5663 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5664 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5667 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5668
5669 // Should not have sent any more keys or motions.
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5672}
5673
5674TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675 addConfigurationProperty("touch.deviceType", "touchScreen");
5676 prepareDisplay(DISPLAY_ORIENTATION_0);
5677 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005678 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679
5680 // These calculations are based on the input device calibration documentation.
5681 int32_t rawX = 100;
5682 int32_t rawY = 200;
5683 int32_t rawTouchMajor = 7;
5684 int32_t rawTouchMinor = 6;
5685 int32_t rawToolMajor = 9;
5686 int32_t rawToolMinor = 8;
5687 int32_t rawPressure = 11;
5688 int32_t rawDistance = 0;
5689 int32_t rawOrientation = 3;
5690 int32_t id = 5;
5691
5692 float x = toDisplayX(rawX);
5693 float y = toDisplayY(rawY);
5694 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5695 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5696 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5697 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5698 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5699 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5700 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5701 float distance = float(rawDistance);
5702
5703 processPosition(mapper, rawX, rawY);
5704 processTouchMajor(mapper, rawTouchMajor);
5705 processTouchMinor(mapper, rawTouchMinor);
5706 processToolMajor(mapper, rawToolMajor);
5707 processToolMinor(mapper, rawToolMinor);
5708 processPressure(mapper, rawPressure);
5709 processOrientation(mapper, rawOrientation);
5710 processDistance(mapper, rawDistance);
5711 processId(mapper, id);
5712 processMTSync(mapper);
5713 processSync(mapper);
5714
5715 NotifyMotionArgs args;
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5717 ASSERT_EQ(0, args.pointerProperties[0].id);
5718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5719 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5720 orientation, distance));
5721}
5722
5723TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 addConfigurationProperty("touch.deviceType", "touchScreen");
5725 prepareDisplay(DISPLAY_ORIENTATION_0);
5726 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5727 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729
5730 // These calculations are based on the input device calibration documentation.
5731 int32_t rawX = 100;
5732 int32_t rawY = 200;
5733 int32_t rawTouchMajor = 140;
5734 int32_t rawTouchMinor = 120;
5735 int32_t rawToolMajor = 180;
5736 int32_t rawToolMinor = 160;
5737
5738 float x = toDisplayX(rawX);
5739 float y = toDisplayY(rawY);
5740 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5741 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5742 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5743 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5744 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5745
5746 processPosition(mapper, rawX, rawY);
5747 processTouchMajor(mapper, rawTouchMajor);
5748 processTouchMinor(mapper, rawTouchMinor);
5749 processToolMajor(mapper, rawToolMajor);
5750 processToolMinor(mapper, rawToolMinor);
5751 processMTSync(mapper);
5752 processSync(mapper);
5753
5754 NotifyMotionArgs args;
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5757 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5758}
5759
5760TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005761 addConfigurationProperty("touch.deviceType", "touchScreen");
5762 prepareDisplay(DISPLAY_ORIENTATION_0);
5763 prepareAxes(POSITION | TOUCH | TOOL);
5764 addConfigurationProperty("touch.size.calibration", "diameter");
5765 addConfigurationProperty("touch.size.scale", "10");
5766 addConfigurationProperty("touch.size.bias", "160");
5767 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005768 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769
5770 // These calculations are based on the input device calibration documentation.
5771 // Note: We only provide a single common touch/tool value because the device is assumed
5772 // not to emit separate values for each pointer (isSummed = 1).
5773 int32_t rawX = 100;
5774 int32_t rawY = 200;
5775 int32_t rawX2 = 150;
5776 int32_t rawY2 = 250;
5777 int32_t rawTouchMajor = 5;
5778 int32_t rawToolMajor = 8;
5779
5780 float x = toDisplayX(rawX);
5781 float y = toDisplayY(rawY);
5782 float x2 = toDisplayX(rawX2);
5783 float y2 = toDisplayY(rawY2);
5784 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5785 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5786 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5787
5788 processPosition(mapper, rawX, rawY);
5789 processTouchMajor(mapper, rawTouchMajor);
5790 processToolMajor(mapper, rawToolMajor);
5791 processMTSync(mapper);
5792 processPosition(mapper, rawX2, rawY2);
5793 processTouchMajor(mapper, rawTouchMajor);
5794 processToolMajor(mapper, rawToolMajor);
5795 processMTSync(mapper);
5796 processSync(mapper);
5797
5798 NotifyMotionArgs args;
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5801
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5803 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5804 args.action);
5805 ASSERT_EQ(size_t(2), args.pointerCount);
5806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5807 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5809 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5810}
5811
5812TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813 addConfigurationProperty("touch.deviceType", "touchScreen");
5814 prepareDisplay(DISPLAY_ORIENTATION_0);
5815 prepareAxes(POSITION | TOUCH | TOOL);
5816 addConfigurationProperty("touch.size.calibration", "area");
5817 addConfigurationProperty("touch.size.scale", "43");
5818 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005819 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005820
5821 // These calculations are based on the input device calibration documentation.
5822 int32_t rawX = 100;
5823 int32_t rawY = 200;
5824 int32_t rawTouchMajor = 5;
5825 int32_t rawToolMajor = 8;
5826
5827 float x = toDisplayX(rawX);
5828 float y = toDisplayY(rawY);
5829 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5830 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5831 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5832
5833 processPosition(mapper, rawX, rawY);
5834 processTouchMajor(mapper, rawTouchMajor);
5835 processToolMajor(mapper, rawToolMajor);
5836 processMTSync(mapper);
5837 processSync(mapper);
5838
5839 NotifyMotionArgs args;
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5842 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5843}
5844
5845TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846 addConfigurationProperty("touch.deviceType", "touchScreen");
5847 prepareDisplay(DISPLAY_ORIENTATION_0);
5848 prepareAxes(POSITION | PRESSURE);
5849 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5850 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005851 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852
Michael Wrightaa449c92017-12-13 21:21:43 +00005853 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005854 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00005855 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5856 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5857 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5858
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 // These calculations are based on the input device calibration documentation.
5860 int32_t rawX = 100;
5861 int32_t rawY = 200;
5862 int32_t rawPressure = 60;
5863
5864 float x = toDisplayX(rawX);
5865 float y = toDisplayY(rawY);
5866 float pressure = float(rawPressure) * 0.01f;
5867
5868 processPosition(mapper, rawX, rawY);
5869 processPressure(mapper, rawPressure);
5870 processMTSync(mapper);
5871 processSync(mapper);
5872
5873 NotifyMotionArgs args;
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5876 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5877}
5878
5879TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005880 addConfigurationProperty("touch.deviceType", "touchScreen");
5881 prepareDisplay(DISPLAY_ORIENTATION_0);
5882 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005883 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884
5885 NotifyMotionArgs motionArgs;
5886 NotifyKeyArgs keyArgs;
5887
5888 processId(mapper, 1);
5889 processPosition(mapper, 100, 200);
5890 processSync(mapper);
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5892 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5893 ASSERT_EQ(0, motionArgs.buttonState);
5894
5895 // press BTN_LEFT, release BTN_LEFT
5896 processKey(mapper, BTN_LEFT, 1);
5897 processSync(mapper);
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5900 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5901
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5903 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5904 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5905
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 processKey(mapper, BTN_LEFT, 0);
5907 processSync(mapper);
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005911
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005914 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915
5916 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5917 processKey(mapper, BTN_RIGHT, 1);
5918 processKey(mapper, BTN_MIDDLE, 1);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5922 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5923 motionArgs.buttonState);
5924
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5927 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5928
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5931 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5932 motionArgs.buttonState);
5933
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 processKey(mapper, BTN_RIGHT, 0);
5935 processSync(mapper);
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005937 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005939
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005942 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943
5944 processKey(mapper, BTN_MIDDLE, 0);
5945 processSync(mapper);
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005948 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005949
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005952 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953
5954 // press BTN_BACK, release BTN_BACK
5955 processKey(mapper, BTN_BACK, 1);
5956 processSync(mapper);
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5958 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5959 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005960
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5964
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5967 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968
5969 processKey(mapper, BTN_BACK, 0);
5970 processSync(mapper);
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005973 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005974
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005977 ASSERT_EQ(0, motionArgs.buttonState);
5978
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5980 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5981 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5982
5983 // press BTN_SIDE, release BTN_SIDE
5984 processKey(mapper, BTN_SIDE, 1);
5985 processSync(mapper);
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5987 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5988 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005989
Michael Wrightd02c5b62014-02-10 15:10:22 -08005990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5993
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5996 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
5998 processKey(mapper, BTN_SIDE, 0);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006 ASSERT_EQ(0, motionArgs.buttonState);
6007
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6009 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6010 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6011
6012 // press BTN_FORWARD, release BTN_FORWARD
6013 processKey(mapper, BTN_FORWARD, 1);
6014 processSync(mapper);
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6016 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6017 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006018
Michael Wrightd02c5b62014-02-10 15:10:22 -08006019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6022
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6025 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006026
6027 processKey(mapper, BTN_FORWARD, 0);
6028 processSync(mapper);
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006032
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006035 ASSERT_EQ(0, motionArgs.buttonState);
6036
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6038 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6039 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6040
6041 // press BTN_EXTRA, release BTN_EXTRA
6042 processKey(mapper, BTN_EXTRA, 1);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6045 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6046 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006047
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006050 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6051
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6054 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055
6056 processKey(mapper, BTN_EXTRA, 0);
6057 processSync(mapper);
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006059 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006061
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006064 ASSERT_EQ(0, motionArgs.buttonState);
6065
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6067 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6068 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6069
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6071
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072 // press BTN_STYLUS, release BTN_STYLUS
6073 processKey(mapper, BTN_STYLUS, 1);
6074 processSync(mapper);
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6078
6079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6080 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6081 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082
6083 processKey(mapper, BTN_STYLUS, 0);
6084 processSync(mapper);
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006088
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006091 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092
6093 // press BTN_STYLUS2, release BTN_STYLUS2
6094 processKey(mapper, BTN_STYLUS2, 1);
6095 processSync(mapper);
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006098 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6099
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6102 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103
6104 processKey(mapper, BTN_STYLUS2, 0);
6105 processSync(mapper);
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006109
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006112 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006113
6114 // release touch
6115 processId(mapper, -1);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6119 ASSERT_EQ(0, motionArgs.buttonState);
6120}
6121
6122TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123 addConfigurationProperty("touch.deviceType", "touchScreen");
6124 prepareDisplay(DISPLAY_ORIENTATION_0);
6125 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006126 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127
6128 NotifyMotionArgs motionArgs;
6129
6130 // default tool type is finger
6131 processId(mapper, 1);
6132 processPosition(mapper, 100, 200);
6133 processSync(mapper);
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6135 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6137
6138 // eraser
6139 processKey(mapper, BTN_TOOL_RUBBER, 1);
6140 processSync(mapper);
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6144
6145 // stylus
6146 processKey(mapper, BTN_TOOL_RUBBER, 0);
6147 processKey(mapper, BTN_TOOL_PEN, 1);
6148 processSync(mapper);
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6152
6153 // brush
6154 processKey(mapper, BTN_TOOL_PEN, 0);
6155 processKey(mapper, BTN_TOOL_BRUSH, 1);
6156 processSync(mapper);
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6160
6161 // pencil
6162 processKey(mapper, BTN_TOOL_BRUSH, 0);
6163 processKey(mapper, BTN_TOOL_PENCIL, 1);
6164 processSync(mapper);
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6168
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006169 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170 processKey(mapper, BTN_TOOL_PENCIL, 0);
6171 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6176
6177 // mouse
6178 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6179 processKey(mapper, BTN_TOOL_MOUSE, 1);
6180 processSync(mapper);
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6184
6185 // lens
6186 processKey(mapper, BTN_TOOL_MOUSE, 0);
6187 processKey(mapper, BTN_TOOL_LENS, 1);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6192
6193 // double-tap
6194 processKey(mapper, BTN_TOOL_LENS, 0);
6195 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6200
6201 // triple-tap
6202 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6203 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6204 processSync(mapper);
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6208
6209 // quad-tap
6210 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6211 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6212 processSync(mapper);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6216
6217 // finger
6218 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6219 processKey(mapper, BTN_TOOL_FINGER, 1);
6220 processSync(mapper);
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6224
6225 // stylus trumps finger
6226 processKey(mapper, BTN_TOOL_PEN, 1);
6227 processSync(mapper);
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6231
6232 // eraser trumps stylus
6233 processKey(mapper, BTN_TOOL_RUBBER, 1);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6238
6239 // mouse trumps eraser
6240 processKey(mapper, BTN_TOOL_MOUSE, 1);
6241 processSync(mapper);
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6245
6246 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6247 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6248 processSync(mapper);
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6252
6253 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6254 processToolType(mapper, MT_TOOL_PEN);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6259
6260 // back to default tool type
6261 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6262 processKey(mapper, BTN_TOOL_MOUSE, 0);
6263 processKey(mapper, BTN_TOOL_RUBBER, 0);
6264 processKey(mapper, BTN_TOOL_PEN, 0);
6265 processKey(mapper, BTN_TOOL_FINGER, 0);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6270}
6271
6272TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273 addConfigurationProperty("touch.deviceType", "touchScreen");
6274 prepareDisplay(DISPLAY_ORIENTATION_0);
6275 prepareAxes(POSITION | ID | SLOT);
6276 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006277 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278
6279 NotifyMotionArgs motionArgs;
6280
6281 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6282 processId(mapper, 1);
6283 processPosition(mapper, 100, 200);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6288 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6289
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6293 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6294
6295 // move a little
6296 processPosition(mapper, 150, 250);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6299 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6301 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6302
6303 // down when BTN_TOUCH is pressed, pressure defaults to 1
6304 processKey(mapper, BTN_TOUCH, 1);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6309 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6310
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6312 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6314 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6315
6316 // up when BTN_TOUCH is released, hover restored
6317 processKey(mapper, BTN_TOUCH, 0);
6318 processSync(mapper);
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6322 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6323
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6327 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6328
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6332 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6333
6334 // exit hover when pointer goes away
6335 processId(mapper, -1);
6336 processSync(mapper);
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6340 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6341}
6342
6343TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344 addConfigurationProperty("touch.deviceType", "touchScreen");
6345 prepareDisplay(DISPLAY_ORIENTATION_0);
6346 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006347 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348
6349 NotifyMotionArgs motionArgs;
6350
6351 // initially hovering because pressure is 0
6352 processId(mapper, 1);
6353 processPosition(mapper, 100, 200);
6354 processPressure(mapper, 0);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6359 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6362 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6364 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6365
6366 // move a little
6367 processPosition(mapper, 150, 250);
6368 processSync(mapper);
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6372 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6373
6374 // down when pressure becomes non-zero
6375 processPressure(mapper, RAW_PRESSURE_MAX);
6376 processSync(mapper);
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6380 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6381
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6383 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6385 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6386
6387 // up when pressure becomes 0, hover restored
6388 processPressure(mapper, 0);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6393 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6394
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6398 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6399
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6403 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6404
6405 // exit hover when pointer goes away
6406 processId(mapper, -1);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6411 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6412}
6413
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006414/**
6415 * Set the input device port <--> display port associations, and check that the
6416 * events are routed to the display that matches the display port.
6417 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6418 */
6419TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006420 const std::string usb2 = "USB2";
6421 const uint8_t hdmi1 = 0;
6422 const uint8_t hdmi2 = 1;
6423 const std::string secondaryUniqueId = "uniqueId2";
6424 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6425
6426 addConfigurationProperty("touch.deviceType", "touchScreen");
6427 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006428 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006429
6430 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6431 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6432
6433 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6434 // for this input device is specified, and the matching viewport is not present,
6435 // the input device should be disabled (at the mapper level).
6436
6437 // Add viewport for display 2 on hdmi2
6438 prepareSecondaryDisplay(type, hdmi2);
6439 // Send a touch event
6440 processPosition(mapper, 100, 100);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6443
6444 // Add viewport for display 1 on hdmi1
6445 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6446 // Send a touch event again
6447 processPosition(mapper, 100, 100);
6448 processSync(mapper);
6449
6450 NotifyMotionArgs args;
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6452 ASSERT_EQ(DISPLAY_ID, args.displayId);
6453}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454
Arthur Hung41a712e2018-11-22 19:41:03 +08006455/**
6456 * Expect fallback to internal viewport if device is external and external viewport is not present.
6457 */
6458TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
Arthur Hung41a712e2018-11-22 19:41:03 +08006459 prepareAxes(POSITION);
6460 addConfigurationProperty("touch.deviceType", "touchScreen");
6461 prepareDisplay(DISPLAY_ORIENTATION_0);
6462 mDevice->setExternal(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006463 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung41a712e2018-11-22 19:41:03 +08006464
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006465 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Arthur Hung41a712e2018-11-22 19:41:03 +08006466
6467 NotifyMotionArgs motionArgs;
6468
6469 // Expect the event to be sent to the internal viewport,
6470 // because an external viewport is not present.
6471 processPosition(mapper, 100, 100);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6475
6476 // Expect the event to be sent to the external viewport if it is present.
6477 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6478 processPosition(mapper, 100, 100);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6481 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6482}
6483
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006484TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006485 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006486 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006487 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006488 fakePointerController->setPosition(100, 200);
6489 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006490 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6491
Garfield Tan888a6a42020-01-09 11:39:16 -08006492 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6493 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6494
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006495 prepareDisplay(DISPLAY_ORIENTATION_0);
6496 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006497 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006498
6499 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006500 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006501
6502 NotifyMotionArgs motionArgs;
6503 processPosition(mapper, 100, 100);
6504 processSync(mapper);
6505
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6508 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6509}
6510
Arthur Hung7c645402019-01-25 17:45:42 +08006511TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6512 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006513 prepareAxes(POSITION | ID | SLOT);
6514 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006515 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006516
6517 // Create the second touch screen device, and enable multi fingers.
6518 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006519 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006520 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006521 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006522 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006523 std::unique_ptr<InputDevice> device2 =
6524 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6525 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006526 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6527 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6528 0 /*flat*/, 0 /*fuzz*/);
6529 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6530 0 /*flat*/, 0 /*fuzz*/);
6531 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6532 0 /*flat*/, 0 /*fuzz*/);
6533 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6534 0 /*flat*/, 0 /*fuzz*/);
6535 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6536 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6537 String8("touchScreen"));
6538
6539 // Setup the second touch screen device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006540 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006541 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6542 device2->reset(ARBITRARY_TIME);
6543
6544 // Setup PointerController.
6545 sp<FakePointerController> fakePointerController = new FakePointerController();
6546 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6547 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6548
6549 // Setup policy for associated displays and show touches.
6550 const uint8_t hdmi1 = 0;
6551 const uint8_t hdmi2 = 1;
6552 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6553 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6554 mFakePolicy->setShowTouches(true);
6555
6556 // Create displays.
6557 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6558 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6559
6560 // Default device will reconfigure above, need additional reconfiguration for another device.
6561 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6562 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6563
6564 // Two fingers down at default display.
6565 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6566 processPosition(mapper, x1, y1);
6567 processId(mapper, 1);
6568 processSlot(mapper, 1);
6569 processPosition(mapper, x2, y2);
6570 processId(mapper, 2);
6571 processSync(mapper);
6572
6573 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6574 fakePointerController->getSpots().find(DISPLAY_ID);
6575 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6576 ASSERT_EQ(size_t(2), iter->second.size());
6577
6578 // Two fingers down at second display.
6579 processPosition(mapper2, x1, y1);
6580 processId(mapper2, 1);
6581 processSlot(mapper2, 1);
6582 processPosition(mapper2, x2, y2);
6583 processId(mapper2, 2);
6584 processSync(mapper2);
6585
6586 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6587 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6588 ASSERT_EQ(size_t(2), iter->second.size());
6589}
6590
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006591TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006592 prepareAxes(POSITION);
6593 addConfigurationProperty("touch.deviceType", "touchScreen");
6594 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006595 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006596
6597 NotifyMotionArgs motionArgs;
6598 // Unrotated video frame
6599 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6600 std::vector<TouchVideoFrame> frames{frame};
6601 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6602 processPosition(mapper, 100, 200);
6603 processSync(mapper);
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(frames, motionArgs.videoFrames);
6606
6607 // Subsequent touch events should not have any videoframes
6608 // This is implemented separately in FakeEventHub,
6609 // but that should match the behaviour of TouchVideoDevice.
6610 processPosition(mapper, 200, 200);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6614}
6615
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006616TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006617 prepareAxes(POSITION);
6618 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006619 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006620 // Unrotated video frame
6621 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6622 NotifyMotionArgs motionArgs;
6623
6624 // Test all 4 orientations
6625 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6626 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6627 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6628 clearViewports();
6629 prepareDisplay(orientation);
6630 std::vector<TouchVideoFrame> frames{frame};
6631 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6632 processPosition(mapper, 100, 200);
6633 processSync(mapper);
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6635 frames[0].rotate(orientation);
6636 ASSERT_EQ(frames, motionArgs.videoFrames);
6637 }
6638}
6639
6640TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006641 prepareAxes(POSITION);
6642 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006643 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006644 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6645 // so mix these.
6646 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6647 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6648 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6649 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6650 NotifyMotionArgs motionArgs;
6651
6652 prepareDisplay(DISPLAY_ORIENTATION_90);
6653 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6654 processPosition(mapper, 100, 200);
6655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 std::for_each(frames.begin(), frames.end(),
6658 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6659 ASSERT_EQ(frames, motionArgs.videoFrames);
6660}
6661
Arthur Hung9da14732019-09-02 16:16:58 +08006662/**
6663 * If we had defined port associations, but the viewport is not ready, the touch device would be
6664 * expected to be disabled, and it should be enabled after the viewport has found.
6665 */
6666TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006667 constexpr uint8_t hdmi2 = 1;
6668 const std::string secondaryUniqueId = "uniqueId2";
6669 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6670
6671 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6672
6673 addConfigurationProperty("touch.deviceType", "touchScreen");
6674 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006675 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006676
6677 ASSERT_EQ(mDevice->isEnabled(), false);
6678
6679 // Add display on hdmi2, the device should be enabled and can receive touch event.
6680 prepareSecondaryDisplay(type, hdmi2);
6681 ASSERT_EQ(mDevice->isEnabled(), true);
6682
6683 // Send a touch event.
6684 processPosition(mapper, 100, 100);
6685 processSync(mapper);
6686
6687 NotifyMotionArgs args;
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6689 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6690}
6691
Arthur Hung6cd19a42019-08-30 19:04:12 +08006692/**
6693 * Test touch should not work if outside of surface.
6694 */
6695TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
Arthur Hung6cd19a42019-08-30 19:04:12 +08006696 addConfigurationProperty("touch.deviceType", "touchScreen");
6697 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006698 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung6cd19a42019-08-30 19:04:12 +08006700
Arthur Hung05de5772019-09-26 18:31:26 +08006701 // Touch on left-top area should work.
6702 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6703 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6704 processPosition(mapper, rawX, rawY);
6705 processSync(mapper);
6706
6707 NotifyMotionArgs args;
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6709
6710 // Reset.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006711 mapper.reset(ARBITRARY_TIME);
Arthur Hung05de5772019-09-26 18:31:26 +08006712
6713 // Let logical display be different to physical display and rotate 90-degrees.
6714 std::optional<DisplayViewport> internalViewport =
6715 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6716 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6717 internalViewport->logicalLeft = 0;
6718 internalViewport->logicalTop = 0;
6719 internalViewport->logicalRight = DISPLAY_HEIGHT;
6720 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6721
6722 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6723 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6724 internalViewport->physicalRight = DISPLAY_HEIGHT;
6725 internalViewport->physicalBottom = DISPLAY_WIDTH;
6726
6727 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6728 internalViewport->deviceHeight = DISPLAY_WIDTH;
6729 mFakePolicy->updateViewport(internalViewport.value());
6730 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6731
6732 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006733 processPosition(mapper, rawX, rawY);
6734 processSync(mapper);
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6736}
6737
Arthur Hung421eb1c2020-01-16 00:09:42 +08006738TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006739 addConfigurationProperty("touch.deviceType", "touchScreen");
6740 prepareDisplay(DISPLAY_ORIENTATION_0);
6741 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006742 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006743
6744 NotifyMotionArgs motionArgs;
6745
6746 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6747 // finger down
6748 processId(mapper, 1);
6749 processPosition(mapper, x1, y1);
6750 processSync(mapper);
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6754
6755 // finger move
6756 processId(mapper, 1);
6757 processPosition(mapper, x2, y2);
6758 processSync(mapper);
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6762
6763 // finger up.
6764 processId(mapper, -1);
6765 processSync(mapper);
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6769
6770 // new finger down
6771 processId(mapper, 1);
6772 processPosition(mapper, x3, y3);
6773 processSync(mapper);
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6777}
6778
6779/**
6780 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6781 * UP events should be ignored.
6782 */
6783TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006784 addConfigurationProperty("touch.deviceType", "touchScreen");
6785 prepareDisplay(DISPLAY_ORIENTATION_0);
6786 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006787 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006788
6789 NotifyMotionArgs motionArgs;
6790
6791 // default tool type is finger
6792 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6793 processId(mapper, 1);
6794 processPosition(mapper, x1, y1);
6795 processSync(mapper);
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6797 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6799
6800 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6801 processToolType(mapper, MT_TOOL_PALM);
6802 processSync(mapper);
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6804 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6805
6806 // Ignore the following MOVE and UP events if had detect a palm event.
6807 processId(mapper, 1);
6808 processPosition(mapper, x2, y2);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6811
6812 // finger up.
6813 processId(mapper, -1);
6814 processSync(mapper);
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6816
6817 // new finger down
6818 processToolType(mapper, MT_TOOL_FINGER);
6819 processId(mapper, 1);
6820 processPosition(mapper, x3, y3);
6821 processSync(mapper);
6822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6823 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6825}
6826
Michael Wrightd02c5b62014-02-10 15:10:22 -08006827} // namespace android