blob: 578605fa40133b9d6b725dd1bb0e8a4d352cb4a6 [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. Lewisa7b82e12020-02-12 15:40:45 -08001106 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001107 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108 InputDeviceIdentifier identifier;
1109 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001110 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001112 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001113 }
1114
Prabir Pradhan28efc192019-11-05 01:10:04 +00001115 // Make the protected loopOnce method accessible to tests.
1116 using InputReader::loopOnce;
1117
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001119 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1120 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001121 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001122 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001123 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001124 return device;
1125 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001126 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127 }
1128
1129 friend class InputReaderTest;
1130};
1131
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001132// --- InputReaderPolicyTest ---
1133class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001134protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001135 sp<FakeInputReaderPolicy> mFakePolicy;
1136
Prabir Pradhan28efc192019-11-05 01:10:04 +00001137 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1138 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001139};
1140
1141/**
1142 * Check that empty set of viewports is an acceptable configuration.
1143 * Also try to get internal viewport two different ways - by type and by uniqueId.
1144 *
1145 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1146 * Such configuration is not currently allowed.
1147 */
1148TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001149 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001150
1151 // We didn't add any viewports yet, so there shouldn't be any.
1152 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001153 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001154 ASSERT_FALSE(internalViewport);
1155
1156 // Add an internal viewport, then clear it
1157 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001158 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001159
1160 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001161 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001162 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001163 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001164
1165 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001166 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001167 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001168 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001169
1170 mFakePolicy->clearViewports();
1171 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001172 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001174 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001175 ASSERT_FALSE(internalViewport);
1176}
1177
1178TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1179 const std::string internalUniqueId = "local:0";
1180 const std::string externalUniqueId = "local:1";
1181 const std::string virtualUniqueId1 = "virtual:2";
1182 const std::string virtualUniqueId2 = "virtual:3";
1183 constexpr int32_t virtualDisplayId1 = 2;
1184 constexpr int32_t virtualDisplayId2 = 3;
1185
1186 // Add an internal viewport
1187 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001188 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001189 // Add an external viewport
1190 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192 // Add an virtual viewport
1193 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001194 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001195 // Add another virtual viewport
1196 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001197 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001198
1199 // Check matching by type for internal
1200 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001201 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202 ASSERT_TRUE(internalViewport);
1203 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1204
1205 // Check matching by type for external
1206 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001207 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001208 ASSERT_TRUE(externalViewport);
1209 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1210
1211 // Check matching by uniqueId for virtual viewport #1
1212 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001213 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001215 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001216 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1217 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1218
1219 // Check matching by uniqueId for virtual viewport #2
1220 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001221 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001222 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001223 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001224 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1225 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1226}
1227
1228
1229/**
1230 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1231 * that lookup works by checking display id.
1232 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1233 */
1234TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1235 const std::string uniqueId1 = "uniqueId1";
1236 const std::string uniqueId2 = "uniqueId2";
1237 constexpr int32_t displayId1 = 2;
1238 constexpr int32_t displayId2 = 3;
1239
1240 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1241 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1242 for (const ViewportType& type : types) {
1243 mFakePolicy->clearViewports();
1244 // Add a viewport
1245 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001246 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001247 // Add another viewport
1248 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001249 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001250
1251 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001252 std::optional<DisplayViewport> viewport1 =
1253 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254 ASSERT_TRUE(viewport1);
1255 ASSERT_EQ(displayId1, viewport1->displayId);
1256 ASSERT_EQ(type, viewport1->type);
1257
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001258 std::optional<DisplayViewport> viewport2 =
1259 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001260 ASSERT_TRUE(viewport2);
1261 ASSERT_EQ(displayId2, viewport2->displayId);
1262 ASSERT_EQ(type, viewport2->type);
1263
1264 // When there are multiple viewports of the same kind, and uniqueId is not specified
1265 // in the call to getDisplayViewport, then that situation is not supported.
1266 // The viewports can be stored in any order, so we cannot rely on the order, since that
1267 // is just implementation detail.
1268 // However, we can check that it still returns *a* viewport, we just cannot assert
1269 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001270 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001271 ASSERT_TRUE(someViewport);
1272 }
1273}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001275/**
1276 * Check getDisplayViewportByPort
1277 */
1278TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1279 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1280 const std::string uniqueId1 = "uniqueId1";
1281 const std::string uniqueId2 = "uniqueId2";
1282 constexpr int32_t displayId1 = 1;
1283 constexpr int32_t displayId2 = 2;
1284 const uint8_t hdmi1 = 0;
1285 const uint8_t hdmi2 = 1;
1286 const uint8_t hdmi3 = 2;
1287
1288 mFakePolicy->clearViewports();
1289 // Add a viewport that's associated with some display port that's not of interest.
1290 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1291 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1292 // Add another viewport, connected to HDMI1 port
1293 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1294 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1295
1296 // Check that correct display viewport was returned by comparing the display ports.
1297 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1298 ASSERT_TRUE(hdmi1Viewport);
1299 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1300 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1301
1302 // Check that we can still get the same viewport using the uniqueId
1303 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1304 ASSERT_TRUE(hdmi1Viewport);
1305 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1306 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1307 ASSERT_EQ(type, hdmi1Viewport->type);
1308
1309 // Check that we cannot find a port with "HDMI2", because we never added one
1310 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1311 ASSERT_FALSE(hdmi2Viewport);
1312}
1313
Michael Wrightd02c5b62014-02-10 15:10:22 -08001314// --- InputReaderTest ---
1315
1316class InputReaderTest : public testing::Test {
1317protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001318 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001320 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001321 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322
Prabir Pradhan28efc192019-11-05 01:10:04 +00001323 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001324 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001326 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001327
Prabir Pradhan28efc192019-11-05 01:10:04 +00001328 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1329 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330 }
1331
Prabir Pradhan28efc192019-11-05 01:10:04 +00001332 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001333 mFakeListener.clear();
1334 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335 }
1336
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001337 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1338 const PropertyMap* configuration) {
1339 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340
1341 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001342 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001343 }
1344 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001345 mReader->loopOnce();
1346 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1348 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001349 }
1350
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001351 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001352 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001353 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001354 }
1355
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001356 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001357 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001358 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001359 }
1360
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001361 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001362 const std::string& name, uint32_t classes,
1363 uint32_t sources,
1364 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001365 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1366 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001367 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001368 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001369 return mapper;
1370 }
1371};
1372
1373TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001374 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001375 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001376 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001377 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001379 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001382 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001383 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1385 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1386 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1387
1388 // Should also have received a notification describing the new input devices.
1389 inputDevices = mFakePolicy->getInputDevices();
1390 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001391 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001392 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001393 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1394 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1395 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1396}
1397
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001398TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001399 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001400 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001401 constexpr int32_t eventHubId = 1;
1402 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001403 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001404 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001405 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001406 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001407
Yi Kong9b14ac62018-07-17 13:48:38 -07001408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001409
1410 NotifyDeviceResetArgs resetArgs;
1411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001412 ASSERT_EQ(deviceId, resetArgs.deviceId);
1413
1414 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001415 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001416 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001417
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001419 ASSERT_EQ(deviceId, resetArgs.deviceId);
1420 ASSERT_EQ(device->isEnabled(), false);
1421
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001422 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001423 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001426 ASSERT_EQ(device->isEnabled(), false);
1427
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001428 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001429 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001431 ASSERT_EQ(deviceId, resetArgs.deviceId);
1432 ASSERT_EQ(device->isEnabled(), true);
1433}
1434
Michael Wrightd02c5b62014-02-10 15:10:22 -08001435TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001436 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1437 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1438 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001439 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001440 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001441 AINPUT_SOURCE_KEYBOARD, nullptr);
1442 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443
1444 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1445 AINPUT_SOURCE_ANY, AKEYCODE_A))
1446 << "Should return unknown when the device id is >= 0 but unknown.";
1447
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001448 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1449 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1450 << "Should return unknown when the device id is valid but the sources are not "
1451 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001452
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001453 ASSERT_EQ(AKEY_STATE_DOWN,
1454 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1455 AKEYCODE_A))
1456 << "Should return value provided by mapper when device id is valid and the device "
1457 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458
1459 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1460 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1461 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1462
1463 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1464 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1465 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1466}
1467
1468TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001469 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1470 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1471 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001472 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001473 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001474 AINPUT_SOURCE_KEYBOARD, nullptr);
1475 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476
1477 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1478 AINPUT_SOURCE_ANY, KEY_A))
1479 << "Should return unknown when the device id is >= 0 but unknown.";
1480
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001481 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1482 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1483 << "Should return unknown when the device id is valid but the sources are not "
1484 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001485
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001486 ASSERT_EQ(AKEY_STATE_DOWN,
1487 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1488 KEY_A))
1489 << "Should return value provided by mapper when device id is valid and the device "
1490 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001491
1492 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1493 AINPUT_SOURCE_TRACKBALL, KEY_A))
1494 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1495
1496 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1497 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1498 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1499}
1500
1501TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001502 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1503 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1504 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001505 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001506 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001507 AINPUT_SOURCE_KEYBOARD, nullptr);
1508 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001509
1510 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1511 AINPUT_SOURCE_ANY, SW_LID))
1512 << "Should return unknown when the device id is >= 0 but unknown.";
1513
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001514 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1515 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1516 << "Should return unknown when the device id is valid but the sources are not "
1517 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 ASSERT_EQ(AKEY_STATE_DOWN,
1520 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1521 SW_LID))
1522 << "Should return value provided by mapper when device id is valid and the device "
1523 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524
1525 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1526 AINPUT_SOURCE_TRACKBALL, SW_LID))
1527 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1528
1529 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1530 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1531 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1532}
1533
1534TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001535 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1536 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1537 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001538 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001539 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001540 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001542 mapper.addSupportedKeyCode(AKEYCODE_A);
1543 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544
1545 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1546 uint8_t flags[4] = { 0, 0, 0, 1 };
1547
1548 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1549 << "Should return false when device id is >= 0 but unknown.";
1550 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1551
1552 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001553 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1554 << "Should return false when device id is valid but the sources are not supported by "
1555 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1557
1558 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001559 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1560 keyCodes, flags))
1561 << "Should return value provided by mapper when device id is valid and the device "
1562 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1564
1565 flags[3] = 1;
1566 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1567 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1568 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1569
1570 flags[3] = 1;
1571 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1572 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1573 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1574}
1575
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001576TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001577 constexpr int32_t eventHubId = 1;
1578 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579
1580 NotifyConfigurationChangedArgs args;
1581
1582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1583 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1584}
1585
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001586TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1588 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1589 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001590 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001592 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001594 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001595 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1597
1598 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001599 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001601 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001602 ASSERT_EQ(EV_KEY, event.type);
1603 ASSERT_EQ(KEY_A, event.code);
1604 ASSERT_EQ(1, event.value);
1605}
1606
Prabir Pradhan42611e02018-11-27 14:04:02 -08001607TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001609 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 constexpr int32_t eventHubId = 1;
1611 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001612 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001613 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001614 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001616
1617 NotifyDeviceResetArgs resetArgs;
1618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1619 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1620
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001621 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001622 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001624 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1625 prevSequenceNum = resetArgs.sequenceNum;
1626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001630 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1631 prevSequenceNum = resetArgs.sequenceNum;
1632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001633 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001634 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001636 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1637 prevSequenceNum = resetArgs.sequenceNum;
1638}
1639
Arthur Hungc23540e2018-11-29 20:42:11 +08001640TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001642 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001644 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1646 FakeInputMapper& mapper =
1647 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001648 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001649
1650 const uint8_t hdmi1 = 1;
1651
1652 // Associated touch screen with second display.
1653 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1654
1655 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001656 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001657 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1658 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1659 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1660 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1661 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001662 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001663
1664 // Add the device, and make sure all of the callbacks are triggered.
1665 // The device is added after the input port associations are processed since
1666 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001667 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001670 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001671
Arthur Hung2c9a3342019-07-23 14:18:59 +08001672 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001673 ASSERT_EQ(deviceId, device->getId());
1674 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1675 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001676
1677 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001678 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001679 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001680 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001681}
1682
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683
1684// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001685class InputDeviceTest : public testing::Test {
1686protected:
1687 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001688 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001689 static const int32_t DEVICE_ID;
1690 static const int32_t DEVICE_GENERATION;
1691 static const int32_t DEVICE_CONTROLLER_NUMBER;
1692 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001693 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001694
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001695 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001696 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001697 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001698 FakeInputReaderContext* mFakeContext;
1699
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001700 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001701
Prabir Pradhan28efc192019-11-05 01:10:04 +00001702 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001703 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001704 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001705 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001706 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1707
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001709 InputDeviceIdentifier identifier;
1710 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001711 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1713 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714 }
1715
Prabir Pradhan28efc192019-11-05 01:10:04 +00001716 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001717 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718 delete mFakeContext;
1719 mFakeListener.clear();
1720 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721 }
1722};
1723
1724const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001725const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001727const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1728const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1729const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1730 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001732
1733TEST_F(InputDeviceTest, ImmutableProperties) {
1734 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001735 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001736 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001737}
1738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001739TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
1740 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001741}
1742
Michael Wrightd02c5b62014-02-10 15:10:22 -08001743TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1744 // Configuration.
1745 InputReaderConfiguration config;
1746 mDevice->configure(ARBITRARY_TIME, &config, 0);
1747
1748 // Reset.
1749 mDevice->reset(ARBITRARY_TIME);
1750
1751 NotifyDeviceResetArgs resetArgs;
1752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1753 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1754 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1755
1756 // Metadata.
1757 ASSERT_TRUE(mDevice->isIgnored());
1758 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1759
1760 InputDeviceInfo info;
1761 mDevice->getDeviceInfo(&info);
1762 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001763 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001764 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1765 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1766
1767 // State queries.
1768 ASSERT_EQ(0, mDevice->getMetaState());
1769
1770 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1771 << "Ignored device should return unknown key code state.";
1772 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1773 << "Ignored device should return unknown scan code state.";
1774 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1775 << "Ignored device should return unknown switch state.";
1776
1777 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1778 uint8_t flags[2] = { 0, 1 };
1779 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1780 << "Ignored device should never mark any key codes.";
1781 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1782 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1783}
1784
1785TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1786 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001787 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001788
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001789 FakeInputMapper& mapper1 =
1790 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001791 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1792 mapper1.setMetaState(AMETA_ALT_ON);
1793 mapper1.addSupportedKeyCode(AKEYCODE_A);
1794 mapper1.addSupportedKeyCode(AKEYCODE_B);
1795 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1796 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1797 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
1798 mapper1.setScanCodeState(3, AKEY_STATE_UP);
1799 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001800
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001801 FakeInputMapper& mapper2 =
1802 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001803 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001804
1805 InputReaderConfiguration config;
1806 mDevice->configure(ARBITRARY_TIME, &config, 0);
1807
1808 String8 propertyValue;
1809 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1810 << "Device should have read configuration during configuration phase.";
1811 ASSERT_STREQ("value", propertyValue.string());
1812
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
1814 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001815
1816 // Reset
1817 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001818 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
1819 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820
1821 NotifyDeviceResetArgs resetArgs;
1822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1823 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1824 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1825
1826 // Metadata.
1827 ASSERT_FALSE(mDevice->isIgnored());
1828 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1829
1830 InputDeviceInfo info;
1831 mDevice->getDeviceInfo(&info);
1832 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001833 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001834 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1835 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1836
1837 // State queries.
1838 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1839 << "Should query mappers and combine meta states.";
1840
1841 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1842 << "Should return unknown key code state when source not supported.";
1843 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1844 << "Should return unknown scan code state when source not supported.";
1845 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1846 << "Should return unknown switch state when source not supported.";
1847
1848 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1849 << "Should query mapper when source is supported.";
1850 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1851 << "Should query mapper when source is supported.";
1852 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1853 << "Should query mapper when source is supported.";
1854
1855 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1856 uint8_t flags[4] = { 0, 0, 0, 1 };
1857 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1858 << "Should do nothing when source is unsupported.";
1859 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1860 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1861 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1862 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1863
1864 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1865 << "Should query mapper when source is supported.";
1866 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1867 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1868 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1869 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1870
1871 // Event handling.
1872 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001873 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001874 mDevice->process(&event, 1);
1875
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001876 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
1877 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001878}
1879
Arthur Hung2c9a3342019-07-23 14:18:59 +08001880// A single input device is associated with a specific display. Check that:
1881// 1. Device is disabled if the viewport corresponding to the associated display is not found
1882// 2. Device is disabled when setEnabled API is called
1883TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001884 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08001885
1886 // First Configuration.
1887 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1888
1889 // Device should be enabled by default.
1890 ASSERT_TRUE(mDevice->isEnabled());
1891
1892 // Prepare associated info.
1893 constexpr uint8_t hdmi = 1;
1894 const std::string UNIQUE_ID = "local:1";
1895
1896 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1897 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1898 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1899 // Device should be disabled because it is associated with a specific display via
1900 // input port <-> display port association, but the corresponding display is not found
1901 ASSERT_FALSE(mDevice->isEnabled());
1902
1903 // Prepare displays.
1904 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1905 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1906 ViewportType::VIEWPORT_INTERNAL);
1907 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1908 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1909 ASSERT_TRUE(mDevice->isEnabled());
1910
1911 // Device should be disabled after set disable.
1912 mFakePolicy->addDisabledDevice(mDevice->getId());
1913 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1914 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1915 ASSERT_FALSE(mDevice->isEnabled());
1916
1917 // Device should still be disabled even found the associated display.
1918 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1919 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1920 ASSERT_FALSE(mDevice->isEnabled());
1921}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001922
1923// --- InputMapperTest ---
1924
1925class InputMapperTest : public testing::Test {
1926protected:
1927 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001928 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001929 static const int32_t DEVICE_ID;
1930 static const int32_t DEVICE_GENERATION;
1931 static const int32_t DEVICE_CONTROLLER_NUMBER;
1932 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001934
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001935 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001936 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001937 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001938 FakeInputReaderContext* mFakeContext;
1939 InputDevice* mDevice;
1940
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001941 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001942 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001944 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1946 InputDeviceIdentifier identifier;
1947 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001948 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001949 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001950
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001951 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001952 }
1953
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001954 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
1955
Prabir Pradhan28efc192019-11-05 01:10:04 +00001956 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001957 delete mDevice;
1958 delete mFakeContext;
1959 mFakeListener.clear();
1960 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001961 }
1962
1963 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001964 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001965 }
1966
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001967 void configureDevice(uint32_t changes) {
1968 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1969 }
1970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001971 template <class T, typename... Args>
1972 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001973 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001974 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001975 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001976 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001977 }
1978
1979 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001980 int32_t orientation, const std::string& uniqueId,
1981 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001982 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001983 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001984 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1985 }
1986
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001987 void clearViewports() {
1988 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001989 }
1990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001991 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
1992 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001993 RawEvent event;
1994 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001996 event.type = type;
1997 event.code = code;
1998 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001999 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000 }
2001
2002 static void assertMotionRange(const InputDeviceInfo& info,
2003 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2004 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002005 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002006 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2007 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2008 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2009 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2010 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2011 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2012 }
2013
2014 static void assertPointerCoords(const PointerCoords& coords,
2015 float x, float y, float pressure, float size,
2016 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2017 float orientation, float distance) {
2018 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2019 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2020 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2021 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2022 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2023 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2024 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2025 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2026 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2027 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2028 }
2029
2030 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2031 float actualX, actualY;
2032 controller->getPosition(&actualX, &actualY);
2033 ASSERT_NEAR(x, actualX, 1);
2034 ASSERT_NEAR(y, actualY, 1);
2035 }
2036};
2037
2038const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002039const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002040const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002041const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2042const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2043const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002044const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002045
2046// --- SwitchInputMapperTest ---
2047
2048class SwitchInputMapperTest : public InputMapperTest {
2049protected:
2050};
2051
2052TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002053 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002055 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002056}
2057
2058TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002059 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002060
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002061 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002062 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002063
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002064 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002065 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002066}
2067
2068TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002069 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002070
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002071 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2072 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2073 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2074 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002075
2076 NotifySwitchArgs args;
2077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2078 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002079 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2080 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002081 args.switchMask);
2082 ASSERT_EQ(uint32_t(0), args.policyFlags);
2083}
2084
2085
2086// --- KeyboardInputMapperTest ---
2087
2088class KeyboardInputMapperTest : public InputMapperTest {
2089protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002090 const std::string UNIQUE_ID = "local:0";
2091
2092 void prepareDisplay(int32_t orientation);
2093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002094 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002095 int32_t originalKeyCode, int32_t rotatedKeyCode,
2096 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002097};
2098
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002099/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2100 * orientation.
2101 */
2102void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2103 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002104 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002105}
2106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002107void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002108 int32_t originalScanCode, int32_t originalKeyCode,
2109 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002110 NotifyKeyArgs args;
2111
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002112 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2114 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2115 ASSERT_EQ(originalScanCode, args.scanCode);
2116 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002117 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002118
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002119 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2121 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2122 ASSERT_EQ(originalScanCode, args.scanCode);
2123 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002124 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002125}
2126
Michael Wrightd02c5b62014-02-10 15:10:22 -08002127TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002128 KeyboardInputMapper& mapper =
2129 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2130 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002132 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133}
2134
2135TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2136 const int32_t USAGE_A = 0x070004;
2137 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002138 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2139 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002141 KeyboardInputMapper& mapper =
2142 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2143 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002144
2145 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002146 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002147 NotifyKeyArgs args;
2148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2149 ASSERT_EQ(DEVICE_ID, args.deviceId);
2150 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2151 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2152 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2153 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2154 ASSERT_EQ(KEY_HOME, args.scanCode);
2155 ASSERT_EQ(AMETA_NONE, args.metaState);
2156 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2157 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2158 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2159
2160 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002161 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 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_HOME, args.keyCode);
2168 ASSERT_EQ(KEY_HOME, 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 by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002175 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2176 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 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(AKEYCODE_A, args.keyCode);
2183 ASSERT_EQ(0, args.scanCode);
2184 ASSERT_EQ(AMETA_NONE, args.metaState);
2185 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2186 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2187 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2188
2189 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002190 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2191 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 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(AKEYCODE_A, args.keyCode);
2198 ASSERT_EQ(0, args.scanCode);
2199 ASSERT_EQ(AMETA_NONE, args.metaState);
2200 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2201 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2202 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2203
2204 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002205 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2206 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2208 ASSERT_EQ(DEVICE_ID, args.deviceId);
2209 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2210 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2211 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2212 ASSERT_EQ(0, args.keyCode);
2213 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2214 ASSERT_EQ(AMETA_NONE, args.metaState);
2215 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2216 ASSERT_EQ(0U, args.policyFlags);
2217 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2218
2219 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002220 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2221 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2223 ASSERT_EQ(DEVICE_ID, args.deviceId);
2224 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2225 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2226 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2227 ASSERT_EQ(0, args.keyCode);
2228 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2229 ASSERT_EQ(AMETA_NONE, args.metaState);
2230 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2231 ASSERT_EQ(0U, args.policyFlags);
2232 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2233}
2234
2235TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002236 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2237 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002239 KeyboardInputMapper& mapper =
2240 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2241 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002242
2243 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002244 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002245
2246 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002247 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002248 NotifyKeyArgs args;
2249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002252 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2253
2254 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002255 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002258 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002259
2260 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002261 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002265
2266 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002267 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2269 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002270 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002271 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2272}
2273
2274TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002275 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2276 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2277 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2278 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002280 KeyboardInputMapper& mapper =
2281 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2282 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002284 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2286 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2287 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2288 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2289 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2290 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2291 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2292 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2293}
2294
2295TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002296 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2297 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2298 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2299 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002300
Michael Wrightd02c5b62014-02-10 15:10:22 -08002301 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002302 KeyboardInputMapper& mapper =
2303 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2304 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002306 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002307 ASSERT_NO_FATAL_FAILURE(
2308 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2309 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2310 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2311 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2312 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2313 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2314 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002315
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002316 clearViewports();
2317 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002318 ASSERT_NO_FATAL_FAILURE(
2319 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2320 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2321 AKEYCODE_DPAD_UP, DISPLAY_ID));
2322 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2323 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2324 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2325 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002326
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002327 clearViewports();
2328 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002329 ASSERT_NO_FATAL_FAILURE(
2330 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2331 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2332 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2333 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2334 AKEYCODE_DPAD_UP, DISPLAY_ID));
2335 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2336 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002338 clearViewports();
2339 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002340 ASSERT_NO_FATAL_FAILURE(
2341 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2342 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2343 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2344 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2345 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2346 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2347 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002348
2349 // Special case: if orientation changes while key is down, we still emit the same keycode
2350 // in the key up as we did in the key down.
2351 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002352 clearViewports();
2353 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002354 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2357 ASSERT_EQ(KEY_UP, args.scanCode);
2358 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2359
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002360 clearViewports();
2361 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002362 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2364 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2365 ASSERT_EQ(KEY_UP, args.scanCode);
2366 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2367}
2368
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002369TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2370 // If the keyboard is not orientation aware,
2371 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002372 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002374 KeyboardInputMapper& mapper =
2375 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2376 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002377 NotifyKeyArgs args;
2378
2379 // Display id should be ADISPLAY_ID_NONE without any display configuration.
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(ADISPLAY_ID_NONE, args.displayId);
2385
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002386 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002387 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002389 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2391 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2392}
2393
2394TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2395 // If the keyboard is orientation aware,
2396 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002397 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002398
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002399 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002400 KeyboardInputMapper& mapper =
2401 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2402 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002403 NotifyKeyArgs args;
2404
2405 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2406 // ^--- already checked by the previous test
2407
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002408 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002409 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002410 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002412 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2414 ASSERT_EQ(DISPLAY_ID, args.displayId);
2415
2416 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002417 clearViewports();
2418 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002419 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002420 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002422 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2424 ASSERT_EQ(newDisplayId, args.displayId);
2425}
2426
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002428 KeyboardInputMapper& mapper =
2429 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2430 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002432 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002433 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002435 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002436 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437}
2438
2439TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002440 KeyboardInputMapper& mapper =
2441 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2442 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002444 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002445 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002447 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002448 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449}
2450
2451TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002452 KeyboardInputMapper& mapper =
2453 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2454 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002456 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457
2458 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2459 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002460 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461 ASSERT_TRUE(flags[0]);
2462 ASSERT_FALSE(flags[1]);
2463}
2464
2465TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002466 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2467 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2468 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2469 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2471 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002473 KeyboardInputMapper& mapper =
2474 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2475 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476
2477 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002478 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2479 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2480 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481
2482 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002483 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2484 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002485 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2486 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2487 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002488 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489
2490 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002491 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2492 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002493 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2494 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2495 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002496 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497
2498 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002499 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2500 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002501 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2502 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2503 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002504 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002505
2506 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002507 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2508 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002509 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2510 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2511 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002512 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513
2514 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002515 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2516 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002517 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2518 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2519 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002520 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521
2522 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002523 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2524 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002525 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2526 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2527 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002528 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529}
2530
Arthur Hung2c9a3342019-07-23 14:18:59 +08002531TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2532 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002533 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002537
2538 // keyboard 2.
2539 const std::string USB2 = "USB2";
2540 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002541 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002542 InputDeviceIdentifier identifier;
2543 identifier.name = "KEYBOARD2";
2544 identifier.location = USB2;
2545 std::unique_ptr<InputDevice> device2 =
2546 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002547 identifier);
2548 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2549 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2550 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2551 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2552 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002553
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002554 KeyboardInputMapper& mapper =
2555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2556 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002557
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002558 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002559 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002560 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002561 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2562 device2->reset(ARBITRARY_TIME);
2563
2564 // Prepared displays and associated info.
2565 constexpr uint8_t hdmi1 = 0;
2566 constexpr uint8_t hdmi2 = 1;
2567 const std::string SECONDARY_UNIQUE_ID = "local:1";
2568
2569 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2570 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2571
2572 // No associated display viewport found, should disable the device.
2573 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2574 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2575 ASSERT_FALSE(device2->isEnabled());
2576
2577 // Prepare second display.
2578 constexpr int32_t newDisplayId = 2;
2579 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2580 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2581 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2582 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2583 // Default device will reconfigure above, need additional reconfiguration for another device.
2584 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2585 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2586
2587 // Device should be enabled after the associated display is found.
2588 ASSERT_TRUE(mDevice->isEnabled());
2589 ASSERT_TRUE(device2->isEnabled());
2590
2591 // Test pad key events
2592 ASSERT_NO_FATAL_FAILURE(
2593 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2595 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2597 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2599 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2600
2601 ASSERT_NO_FATAL_FAILURE(
2602 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2603 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2604 AKEYCODE_DPAD_RIGHT, newDisplayId));
2605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2606 AKEYCODE_DPAD_DOWN, newDisplayId));
2607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2608 AKEYCODE_DPAD_LEFT, newDisplayId));
2609}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002611// --- KeyboardInputMapperTest_ExternalDevice ---
2612
2613class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2614protected:
2615 virtual void SetUp() override {
2616 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2617 }
2618};
2619
2620TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002621 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2622 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002623
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002624 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2625 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2626 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2627 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002629 KeyboardInputMapper& mapper =
2630 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002632
2633 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2634 NotifyKeyArgs args;
2635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2636 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2637
2638 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2640 ASSERT_EQ(uint32_t(0), args.policyFlags);
2641
2642 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2644 ASSERT_EQ(uint32_t(0), args.policyFlags);
2645
2646 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2648 ASSERT_EQ(uint32_t(0), args.policyFlags);
2649
2650 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2652 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2653
2654 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2656 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2657}
2658
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002659TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002660 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002661
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002662 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2663 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2664 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002665
Powei Fengd041c5d2019-05-03 17:11:33 -07002666 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002667 KeyboardInputMapper& mapper =
2668 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2669 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002670
2671 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2672 NotifyKeyArgs args;
2673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2674 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2675
2676 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2678 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2679
2680 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2682 ASSERT_EQ(uint32_t(0), args.policyFlags);
2683
2684 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2686 ASSERT_EQ(uint32_t(0), args.policyFlags);
2687
2688 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2690 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2691
2692 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2694 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2695}
2696
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697// --- CursorInputMapperTest ---
2698
2699class CursorInputMapperTest : public InputMapperTest {
2700protected:
2701 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2702
2703 sp<FakePointerController> mFakePointerController;
2704
Prabir Pradhan28efc192019-11-05 01:10:04 +00002705 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 InputMapperTest::SetUp();
2707
2708 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002709 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 }
2711
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002712 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
2713 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002714
2715 void prepareDisplay(int32_t orientation) {
2716 const std::string uniqueId = "local:0";
2717 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2718 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2719 orientation, uniqueId, NO_PORT, viewportType);
2720 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721};
2722
2723const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2724
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002725void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
2726 int32_t originalY, int32_t rotatedX,
2727 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728 NotifyMotionArgs args;
2729
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002730 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2731 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2732 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2736 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2737 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2738 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2739}
2740
2741TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002743 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002745 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002746}
2747
2748TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002750 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002752 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753}
2754
2755TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002756 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758
2759 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002760 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761
2762 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002763 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2764 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2766 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2767
2768 // When the bounds are set, then there should be a valid motion range.
2769 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2770
2771 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002773
2774 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2775 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2776 1, 800 - 1, 0.0f, 0.0f));
2777 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2778 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2779 2, 480 - 1, 0.0f, 0.0f));
2780 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2781 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2782 0.0f, 1.0f, 0.0f, 0.0f));
2783}
2784
2785TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788
2789 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002790 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791
2792 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2793 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2794 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2795 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2796 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2797 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2798 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2799 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2800 0.0f, 1.0f, 0.0f, 0.0f));
2801}
2802
2803TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
2807 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2808
2809 NotifyMotionArgs args;
2810
2811 // Button press.
2812 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002813 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2814 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2816 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2817 ASSERT_EQ(DEVICE_ID, args.deviceId);
2818 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2819 ASSERT_EQ(uint32_t(0), args.policyFlags);
2820 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2821 ASSERT_EQ(0, args.flags);
2822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2823 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2824 ASSERT_EQ(0, args.edgeFlags);
2825 ASSERT_EQ(uint32_t(1), args.pointerCount);
2826 ASSERT_EQ(0, args.pointerProperties[0].id);
2827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2829 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2830 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2831 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2832 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2833
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2835 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2836 ASSERT_EQ(DEVICE_ID, args.deviceId);
2837 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2838 ASSERT_EQ(uint32_t(0), args.policyFlags);
2839 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2840 ASSERT_EQ(0, args.flags);
2841 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2842 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2843 ASSERT_EQ(0, args.edgeFlags);
2844 ASSERT_EQ(uint32_t(1), args.pointerCount);
2845 ASSERT_EQ(0, args.pointerProperties[0].id);
2846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2848 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2849 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2850 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2851 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2852
Michael Wrightd02c5b62014-02-10 15:10:22 -08002853 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002854 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2855 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2857 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2858 ASSERT_EQ(DEVICE_ID, args.deviceId);
2859 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2860 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002861 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2862 ASSERT_EQ(0, args.flags);
2863 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2864 ASSERT_EQ(0, args.buttonState);
2865 ASSERT_EQ(0, args.edgeFlags);
2866 ASSERT_EQ(uint32_t(1), args.pointerCount);
2867 ASSERT_EQ(0, args.pointerProperties[0].id);
2868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2870 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2871 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2872 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2873 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2874
2875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2876 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2877 ASSERT_EQ(DEVICE_ID, args.deviceId);
2878 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2879 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2881 ASSERT_EQ(0, args.flags);
2882 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2883 ASSERT_EQ(0, args.buttonState);
2884 ASSERT_EQ(0, args.edgeFlags);
2885 ASSERT_EQ(uint32_t(1), args.pointerCount);
2886 ASSERT_EQ(0, args.pointerProperties[0].id);
2887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2889 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2890 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2891 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2892 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2893}
2894
2895TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002896 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002897 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002898
2899 NotifyMotionArgs args;
2900
2901 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002902 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2903 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2907 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2908
2909 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002910 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2911 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2915 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2916}
2917
2918TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002920 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921
2922 NotifyMotionArgs args;
2923
2924 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002925 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2926 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2928 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2930 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2931
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2935 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2936
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002938 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2939 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2943 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2944
2945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002946 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2948 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2949}
2950
2951TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002953 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954
2955 NotifyMotionArgs args;
2956
2957 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002958 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2959 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2960 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2961 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2963 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2965 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2966 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2967
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2969 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2971 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2972 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2973
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002975 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2976 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2977 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2981 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2982 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2983
2984 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002985 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2986 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2990 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2991
2992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2995 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2996}
2997
2998TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003000 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003002 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003003 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3004 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3005 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3006 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3007 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3008 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3009 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3010 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3011}
3012
3013TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 addConfigurationProperty("cursor.mode", "navigation");
3015 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003016 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003017
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003018 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3020 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3021 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3022 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3023 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3024 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3025 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3026 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3027
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003028 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3030 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3031 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3032 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3033 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3034 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3035 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3036 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3037
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003038 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3040 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3041 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3042 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3043 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3044 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3045 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3046 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3047
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003048 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3050 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3051 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3052 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3053 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3054 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3055 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3056 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3057}
3058
3059TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003061 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062
3063 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3064 mFakePointerController->setPosition(100, 200);
3065 mFakePointerController->setButtonState(0);
3066
3067 NotifyMotionArgs motionArgs;
3068 NotifyKeyArgs keyArgs;
3069
3070 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003071 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3072 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3074 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3075 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3076 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3078 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3079
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3082 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3083 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3085 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3086
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003087 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3088 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091 ASSERT_EQ(0, motionArgs.buttonState);
3092 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3094 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3095
3096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003097 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098 ASSERT_EQ(0, motionArgs.buttonState);
3099 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3101 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3102
3103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003105 ASSERT_EQ(0, motionArgs.buttonState);
3106 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3108 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3109
3110 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003111 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3112 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3113 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3115 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3116 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3117 motionArgs.buttonState);
3118 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3119 mFakePointerController->getButtonState());
3120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3121 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3122
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3125 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3126 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3127 mFakePointerController->getButtonState());
3128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3129 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3130
3131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3133 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3134 motionArgs.buttonState);
3135 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3136 mFakePointerController->getButtonState());
3137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3138 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3139
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003140 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3141 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3145 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3147 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3148
3149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003151 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3152 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3154 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3155
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003156 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3157 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3160 ASSERT_EQ(0, motionArgs.buttonState);
3161 ASSERT_EQ(0, mFakePointerController->getButtonState());
3162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3163 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 -08003164 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3165 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003166
3167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_EQ(0, motionArgs.buttonState);
3169 ASSERT_EQ(0, mFakePointerController->getButtonState());
3170 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3172 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 -08003173
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3175 ASSERT_EQ(0, motionArgs.buttonState);
3176 ASSERT_EQ(0, mFakePointerController->getButtonState());
3177 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3179 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3180
3181 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003182 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3183 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3185 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3186 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003187
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003189 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3191 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3193 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3194
3195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3196 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3197 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3198 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3200 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3201
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003202 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3203 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003205 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206 ASSERT_EQ(0, motionArgs.buttonState);
3207 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3209 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3210
3211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003213 ASSERT_EQ(0, motionArgs.buttonState);
3214 ASSERT_EQ(0, mFakePointerController->getButtonState());
3215
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3217 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3219 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3220 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3221
3222 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003223 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3224 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3226 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3227 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003228
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003230 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3232 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3234 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3235
3236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3238 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3239 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3241 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3242
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003243 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3244 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 ASSERT_EQ(0, motionArgs.buttonState);
3248 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3250 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 -08003251
3252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3253 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3254 ASSERT_EQ(0, motionArgs.buttonState);
3255 ASSERT_EQ(0, mFakePointerController->getButtonState());
3256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3257 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3258
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3260 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3261 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3262
3263 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003264 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3265 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3267 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3268 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003269
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003271 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3273 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3275 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3276
3277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3279 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3280 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3282 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3283
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003284 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3285 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 ASSERT_EQ(0, motionArgs.buttonState);
3289 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3291 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 -08003292
3293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3294 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3295 ASSERT_EQ(0, motionArgs.buttonState);
3296 ASSERT_EQ(0, mFakePointerController->getButtonState());
3297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3298 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3299
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3301 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3302 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3303
3304 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003305 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3306 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3308 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3309 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003310
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003312 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3314 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3316 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3317
3318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3320 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3321 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3323 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3324
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003325 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3326 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329 ASSERT_EQ(0, motionArgs.buttonState);
3330 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3332 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 -08003333
3334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3336 ASSERT_EQ(0, motionArgs.buttonState);
3337 ASSERT_EQ(0, mFakePointerController->getButtonState());
3338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3339 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3340
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3342 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3343 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3344}
3345
3346TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
3350 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3351 mFakePointerController->setPosition(100, 200);
3352 mFakePointerController->setButtonState(0);
3353
3354 NotifyMotionArgs args;
3355
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003356 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3357 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3358 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003360 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3363 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3364 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3365}
3366
3367TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003368 addConfigurationProperty("cursor.mode", "pointer");
3369 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003371
3372 NotifyDeviceResetArgs resetArgs;
3373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3374 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3375 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3376
3377 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3378 mFakePointerController->setPosition(100, 200);
3379 mFakePointerController->setButtonState(0);
3380
3381 NotifyMotionArgs args;
3382
3383 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003384 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3385 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
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 10.0f, 20.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 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003395 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3396 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3398 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3399 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3401 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3403 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3404 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3406 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3407
3408 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003409 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3410 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3412 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3413 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3415 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3417 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3418 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3420 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3421
3422 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003423 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3424 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3425 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3427 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3430 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3431 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3432
3433 // Disable pointer capture and check that the device generation got bumped
3434 // and events are generated the usual way.
3435 const uint32_t generation = mFakeContext->getGeneration();
3436 mFakePolicy->setPointerCapture(false);
3437 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3438 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3439
3440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3441 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3442 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3443
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003444 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3445 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3446 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3448 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3451 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3452 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3453}
3454
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003455TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003456 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003457
Garfield Tan888a6a42020-01-09 11:39:16 -08003458 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003459 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003460 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3461 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3462 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3463 ViewportType::VIEWPORT_EXTERNAL);
3464 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3465 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3466
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003467 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3468 mFakePointerController->setPosition(100, 200);
3469 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003470
3471 NotifyMotionArgs args;
3472 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3473 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3474 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3476 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3477 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3479 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3480 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3481 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3482}
3483
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484// --- TouchInputMapperTest ---
3485
3486class TouchInputMapperTest : public InputMapperTest {
3487protected:
3488 static const int32_t RAW_X_MIN;
3489 static const int32_t RAW_X_MAX;
3490 static const int32_t RAW_Y_MIN;
3491 static const int32_t RAW_Y_MAX;
3492 static const int32_t RAW_TOUCH_MIN;
3493 static const int32_t RAW_TOUCH_MAX;
3494 static const int32_t RAW_TOOL_MIN;
3495 static const int32_t RAW_TOOL_MAX;
3496 static const int32_t RAW_PRESSURE_MIN;
3497 static const int32_t RAW_PRESSURE_MAX;
3498 static const int32_t RAW_ORIENTATION_MIN;
3499 static const int32_t RAW_ORIENTATION_MAX;
3500 static const int32_t RAW_DISTANCE_MIN;
3501 static const int32_t RAW_DISTANCE_MAX;
3502 static const int32_t RAW_TILT_MIN;
3503 static const int32_t RAW_TILT_MAX;
3504 static const int32_t RAW_ID_MIN;
3505 static const int32_t RAW_ID_MAX;
3506 static const int32_t RAW_SLOT_MIN;
3507 static const int32_t RAW_SLOT_MAX;
3508 static const float X_PRECISION;
3509 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003510 static const float X_PRECISION_VIRTUAL;
3511 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512
3513 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003514 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515
3516 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3517
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003518 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003519 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003520
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 enum Axes {
3522 POSITION = 1 << 0,
3523 TOUCH = 1 << 1,
3524 TOOL = 1 << 2,
3525 PRESSURE = 1 << 3,
3526 ORIENTATION = 1 << 4,
3527 MINOR = 1 << 5,
3528 ID = 1 << 6,
3529 DISTANCE = 1 << 7,
3530 TILT = 1 << 8,
3531 SLOT = 1 << 9,
3532 TOOL_TYPE = 1 << 10,
3533 };
3534
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003535 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3536 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003537 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003539 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540 int32_t toRawX(float displayX);
3541 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003542 float toCookedX(float rawX, float rawY);
3543 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003545 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003547 float toDisplayY(int32_t rawY, int32_t displayHeight);
3548
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549};
3550
3551const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3552const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3553const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3554const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3555const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3556const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3557const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3558const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003559const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3560const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3562const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3563const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3564const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3565const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3566const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3567const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3568const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3569const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3570const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3571const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3572const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003573const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3574 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3575const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3576 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003577const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3578 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579
3580const float TouchInputMapperTest::GEOMETRIC_SCALE =
3581 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3582 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3583
3584const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3585 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3586 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3587};
3588
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003589void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003590 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003591 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3592}
3593
3594void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3595 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3596 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597}
3598
Santos Cordonfa5cf462017-04-05 10:37:00 -07003599void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003600 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3601 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003602 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003603}
3604
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003606 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3607 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3608 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3609 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610}
3611
Jason Gerecke489fda82012-09-07 17:19:40 -07003612void TouchInputMapperTest::prepareLocationCalibration() {
3613 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3614}
3615
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616int32_t TouchInputMapperTest::toRawX(float displayX) {
3617 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3618}
3619
3620int32_t TouchInputMapperTest::toRawY(float displayY) {
3621 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3622}
3623
Jason Gerecke489fda82012-09-07 17:19:40 -07003624float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3625 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3626 return rawX;
3627}
3628
3629float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3630 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3631 return rawY;
3632}
3633
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003635 return toDisplayX(rawX, DISPLAY_WIDTH);
3636}
3637
3638float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3639 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640}
3641
3642float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003643 return toDisplayY(rawY, DISPLAY_HEIGHT);
3644}
3645
3646float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3647 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003648}
3649
3650
3651// --- SingleTouchInputMapperTest ---
3652
3653class SingleTouchInputMapperTest : public TouchInputMapperTest {
3654protected:
3655 void prepareButtons();
3656 void prepareAxes(int axes);
3657
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003658 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3659 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3660 void processUp(SingleTouchInputMapper& mappery);
3661 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3662 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3663 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3664 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3665 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3666 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003667};
3668
3669void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003670 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671}
3672
3673void SingleTouchInputMapperTest::prepareAxes(int axes) {
3674 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003675 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3676 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677 }
3678 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003679 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
3680 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681 }
3682 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003683 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
3684 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685 }
3686 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003687 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
3688 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689 }
3690 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003691 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3692 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693 }
3694}
3695
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003697 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3698 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3699 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700}
3701
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003702void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003703 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3704 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705}
3706
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003708 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709}
3710
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003711void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003712 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713}
3714
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003715void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
3716 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003717 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718}
3719
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003720void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003721 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722}
3723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003724void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
3725 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003726 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3727 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728}
3729
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
3731 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003732 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733}
3734
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003736 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737}
3738
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740 prepareButtons();
3741 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003742 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003744 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745}
3746
3747TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
3749 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 prepareButtons();
3751 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003752 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755}
3756
3757TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758 prepareButtons();
3759 prepareAxes(POSITION);
3760 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003763 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764}
3765
3766TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767 prepareButtons();
3768 prepareAxes(POSITION);
3769 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003770 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003772 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773}
3774
3775TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776 addConfigurationProperty("touch.deviceType", "touchScreen");
3777 prepareDisplay(DISPLAY_ORIENTATION_0);
3778 prepareButtons();
3779 prepareAxes(POSITION);
3780 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003781 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782
3783 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003784 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785
3786 // Virtual key is down.
3787 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3788 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3789 processDown(mapper, x, y);
3790 processSync(mapper);
3791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3792
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
3795 // Virtual key is up.
3796 processUp(mapper);
3797 processSync(mapper);
3798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3799
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003800 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801}
3802
3803TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804 addConfigurationProperty("touch.deviceType", "touchScreen");
3805 prepareDisplay(DISPLAY_ORIENTATION_0);
3806 prepareButtons();
3807 prepareAxes(POSITION);
3808 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003809 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810
3811 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003812 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813
3814 // Virtual key is down.
3815 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3816 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3817 processDown(mapper, x, y);
3818 processSync(mapper);
3819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3820
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003821 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822
3823 // Virtual key is up.
3824 processUp(mapper);
3825 processSync(mapper);
3826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003828 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829}
3830
3831TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832 addConfigurationProperty("touch.deviceType", "touchScreen");
3833 prepareDisplay(DISPLAY_ORIENTATION_0);
3834 prepareButtons();
3835 prepareAxes(POSITION);
3836 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003837 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
3839 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3840 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003841 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842 ASSERT_TRUE(flags[0]);
3843 ASSERT_FALSE(flags[1]);
3844}
3845
3846TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847 addConfigurationProperty("touch.deviceType", "touchScreen");
3848 prepareDisplay(DISPLAY_ORIENTATION_0);
3849 prepareButtons();
3850 prepareAxes(POSITION);
3851 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003852 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853
3854 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3855
3856 NotifyKeyArgs args;
3857
3858 // Press virtual key.
3859 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3860 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3861 processDown(mapper, x, y);
3862 processSync(mapper);
3863
3864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3865 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3866 ASSERT_EQ(DEVICE_ID, args.deviceId);
3867 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3868 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3869 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3870 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3871 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3872 ASSERT_EQ(KEY_HOME, args.scanCode);
3873 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3874 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3875
3876 // Release virtual key.
3877 processUp(mapper);
3878 processSync(mapper);
3879
3880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3881 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3882 ASSERT_EQ(DEVICE_ID, args.deviceId);
3883 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3884 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3885 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3886 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3887 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3888 ASSERT_EQ(KEY_HOME, args.scanCode);
3889 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3890 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3891
3892 // Should not have sent any motions.
3893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3894}
3895
3896TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897 addConfigurationProperty("touch.deviceType", "touchScreen");
3898 prepareDisplay(DISPLAY_ORIENTATION_0);
3899 prepareButtons();
3900 prepareAxes(POSITION);
3901 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003902 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903
3904 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3905
3906 NotifyKeyArgs keyArgs;
3907
3908 // Press virtual key.
3909 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3910 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3911 processDown(mapper, x, y);
3912 processSync(mapper);
3913
3914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3915 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3916 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3917 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3918 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3919 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3920 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3921 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3922 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3923 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3924 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3925
3926 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3927 // into the display area.
3928 y -= 100;
3929 processMove(mapper, x, y);
3930 processSync(mapper);
3931
3932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3933 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3934 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3935 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3936 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3937 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3938 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3939 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3940 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3941 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3942 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3943 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3944
3945 NotifyMotionArgs motionArgs;
3946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3947 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3948 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3949 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3950 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3951 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3952 ASSERT_EQ(0, motionArgs.flags);
3953 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3954 ASSERT_EQ(0, motionArgs.buttonState);
3955 ASSERT_EQ(0, motionArgs.edgeFlags);
3956 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3957 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3958 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3960 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3961 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3962 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3963 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3964
3965 // Keep moving out of bounds. Should generate a pointer move.
3966 y -= 50;
3967 processMove(mapper, x, y);
3968 processSync(mapper);
3969
3970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3971 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3972 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3973 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3974 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3976 ASSERT_EQ(0, motionArgs.flags);
3977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3978 ASSERT_EQ(0, motionArgs.buttonState);
3979 ASSERT_EQ(0, motionArgs.edgeFlags);
3980 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3981 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3984 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3985 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3986 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3987 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3988
3989 // Release out of bounds. Should generate a pointer up.
3990 processUp(mapper);
3991 processSync(mapper);
3992
3993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3995 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3996 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3997 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3998 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3999 ASSERT_EQ(0, motionArgs.flags);
4000 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4001 ASSERT_EQ(0, motionArgs.buttonState);
4002 ASSERT_EQ(0, motionArgs.edgeFlags);
4003 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4004 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4007 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4008 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4009 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4010 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4011
4012 // Should not have sent any more keys or motions.
4013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4015}
4016
4017TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 addConfigurationProperty("touch.deviceType", "touchScreen");
4019 prepareDisplay(DISPLAY_ORIENTATION_0);
4020 prepareButtons();
4021 prepareAxes(POSITION);
4022 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004023 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024
4025 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4026
4027 NotifyMotionArgs motionArgs;
4028
4029 // Initially go down out of bounds.
4030 int32_t x = -10;
4031 int32_t y = -10;
4032 processDown(mapper, x, y);
4033 processSync(mapper);
4034
4035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4036
4037 // Move into the display area. Should generate a pointer down.
4038 x = 50;
4039 y = 75;
4040 processMove(mapper, x, y);
4041 processSync(mapper);
4042
4043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4044 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4045 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4046 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4047 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4048 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4049 ASSERT_EQ(0, motionArgs.flags);
4050 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4051 ASSERT_EQ(0, motionArgs.buttonState);
4052 ASSERT_EQ(0, motionArgs.edgeFlags);
4053 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4054 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4057 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4058 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4059 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4060 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4061
4062 // Release. Should generate a pointer up.
4063 processUp(mapper);
4064 processSync(mapper);
4065
4066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4068 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4069 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4070 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4071 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4072 ASSERT_EQ(0, motionArgs.flags);
4073 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4074 ASSERT_EQ(0, motionArgs.buttonState);
4075 ASSERT_EQ(0, motionArgs.edgeFlags);
4076 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4080 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4081 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4082 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4083 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4084
4085 // Should not have sent any more keys or motions.
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4088}
4089
Santos Cordonfa5cf462017-04-05 10:37:00 -07004090TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004091 addConfigurationProperty("touch.deviceType", "touchScreen");
4092 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4093
4094 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4095 prepareButtons();
4096 prepareAxes(POSITION);
4097 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004098 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004099
4100 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4101
4102 NotifyMotionArgs motionArgs;
4103
4104 // Down.
4105 int32_t x = 100;
4106 int32_t y = 125;
4107 processDown(mapper, x, y);
4108 processSync(mapper);
4109
4110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4111 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4112 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4113 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4114 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4115 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4116 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4117 ASSERT_EQ(0, motionArgs.flags);
4118 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4119 ASSERT_EQ(0, motionArgs.buttonState);
4120 ASSERT_EQ(0, motionArgs.edgeFlags);
4121 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4122 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4125 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4126 1, 0, 0, 0, 0, 0, 0, 0));
4127 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4128 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4130
4131 // Move.
4132 x += 50;
4133 y += 75;
4134 processMove(mapper, x, y);
4135 processSync(mapper);
4136
4137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4138 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4139 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4140 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4141 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4142 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4144 ASSERT_EQ(0, motionArgs.flags);
4145 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4146 ASSERT_EQ(0, motionArgs.buttonState);
4147 ASSERT_EQ(0, motionArgs.edgeFlags);
4148 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4149 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4152 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4153 1, 0, 0, 0, 0, 0, 0, 0));
4154 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4155 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4157
4158 // Up.
4159 processUp(mapper);
4160 processSync(mapper);
4161
4162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4163 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4164 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4165 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4166 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4167 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4168 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4169 ASSERT_EQ(0, motionArgs.flags);
4170 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4171 ASSERT_EQ(0, motionArgs.buttonState);
4172 ASSERT_EQ(0, motionArgs.edgeFlags);
4173 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4174 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4177 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4178 1, 0, 0, 0, 0, 0, 0, 0));
4179 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4180 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4181 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4182
4183 // Should not have sent any more keys or motions.
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4186}
4187
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 addConfigurationProperty("touch.deviceType", "touchScreen");
4190 prepareDisplay(DISPLAY_ORIENTATION_0);
4191 prepareButtons();
4192 prepareAxes(POSITION);
4193 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004194 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195
4196 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4197
4198 NotifyMotionArgs motionArgs;
4199
4200 // Down.
4201 int32_t x = 100;
4202 int32_t y = 125;
4203 processDown(mapper, x, y);
4204 processSync(mapper);
4205
4206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4207 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4208 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4209 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4210 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4211 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4212 ASSERT_EQ(0, motionArgs.flags);
4213 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4214 ASSERT_EQ(0, motionArgs.buttonState);
4215 ASSERT_EQ(0, motionArgs.edgeFlags);
4216 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4217 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4220 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4221 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4222 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4224
4225 // Move.
4226 x += 50;
4227 y += 75;
4228 processMove(mapper, x, y);
4229 processSync(mapper);
4230
4231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4232 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4233 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4234 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4235 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4237 ASSERT_EQ(0, motionArgs.flags);
4238 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4239 ASSERT_EQ(0, motionArgs.buttonState);
4240 ASSERT_EQ(0, motionArgs.edgeFlags);
4241 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4242 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4245 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4246 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4247 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4249
4250 // Up.
4251 processUp(mapper);
4252 processSync(mapper);
4253
4254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4256 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4257 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4258 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4259 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4260 ASSERT_EQ(0, motionArgs.flags);
4261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4262 ASSERT_EQ(0, motionArgs.buttonState);
4263 ASSERT_EQ(0, motionArgs.edgeFlags);
4264 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4265 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4268 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4269 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4270 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4272
4273 // Should not have sent any more keys or motions.
4274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4276}
4277
4278TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 addConfigurationProperty("touch.deviceType", "touchScreen");
4280 prepareButtons();
4281 prepareAxes(POSITION);
4282 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004283 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284
4285 NotifyMotionArgs args;
4286
4287 // Rotation 90.
4288 prepareDisplay(DISPLAY_ORIENTATION_90);
4289 processDown(mapper, toRawX(50), toRawY(75));
4290 processSync(mapper);
4291
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4293 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4294 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4295
4296 processUp(mapper);
4297 processSync(mapper);
4298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4299}
4300
4301TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 addConfigurationProperty("touch.deviceType", "touchScreen");
4303 prepareButtons();
4304 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004305 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306
4307 NotifyMotionArgs args;
4308
4309 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004310 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 prepareDisplay(DISPLAY_ORIENTATION_0);
4312 processDown(mapper, toRawX(50), toRawY(75));
4313 processSync(mapper);
4314
4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4316 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4317 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4318
4319 processUp(mapper);
4320 processSync(mapper);
4321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4322
4323 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004324 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 prepareDisplay(DISPLAY_ORIENTATION_90);
4326 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4327 processSync(mapper);
4328
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4330 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4331 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4332
4333 processUp(mapper);
4334 processSync(mapper);
4335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4336
4337 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004338 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 prepareDisplay(DISPLAY_ORIENTATION_180);
4340 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4341 processSync(mapper);
4342
4343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4344 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4345 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4346
4347 processUp(mapper);
4348 processSync(mapper);
4349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4350
4351 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004352 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 prepareDisplay(DISPLAY_ORIENTATION_270);
4354 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4355 processSync(mapper);
4356
4357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4358 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4359 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4360
4361 processUp(mapper);
4362 processSync(mapper);
4363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4364}
4365
4366TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 addConfigurationProperty("touch.deviceType", "touchScreen");
4368 prepareDisplay(DISPLAY_ORIENTATION_0);
4369 prepareButtons();
4370 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004371 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372
4373 // These calculations are based on the input device calibration documentation.
4374 int32_t rawX = 100;
4375 int32_t rawY = 200;
4376 int32_t rawPressure = 10;
4377 int32_t rawToolMajor = 12;
4378 int32_t rawDistance = 2;
4379 int32_t rawTiltX = 30;
4380 int32_t rawTiltY = 110;
4381
4382 float x = toDisplayX(rawX);
4383 float y = toDisplayY(rawY);
4384 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4385 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4386 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4387 float distance = float(rawDistance);
4388
4389 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4390 float tiltScale = M_PI / 180;
4391 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4392 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4393 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4394 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4395
4396 processDown(mapper, rawX, rawY);
4397 processPressure(mapper, rawPressure);
4398 processToolMajor(mapper, rawToolMajor);
4399 processDistance(mapper, rawDistance);
4400 processTilt(mapper, rawTiltX, rawTiltY);
4401 processSync(mapper);
4402
4403 NotifyMotionArgs args;
4404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4406 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4407 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4408}
4409
Jason Gerecke489fda82012-09-07 17:19:40 -07004410TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004411 addConfigurationProperty("touch.deviceType", "touchScreen");
4412 prepareDisplay(DISPLAY_ORIENTATION_0);
4413 prepareLocationCalibration();
4414 prepareButtons();
4415 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004416 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004417
4418 int32_t rawX = 100;
4419 int32_t rawY = 200;
4420
4421 float x = toDisplayX(toCookedX(rawX, rawY));
4422 float y = toDisplayY(toCookedY(rawX, rawY));
4423
4424 processDown(mapper, rawX, rawY);
4425 processSync(mapper);
4426
4427 NotifyMotionArgs args;
4428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4430 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4431}
4432
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 addConfigurationProperty("touch.deviceType", "touchScreen");
4435 prepareDisplay(DISPLAY_ORIENTATION_0);
4436 prepareButtons();
4437 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004438 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
4440 NotifyMotionArgs motionArgs;
4441 NotifyKeyArgs keyArgs;
4442
4443 processDown(mapper, 100, 200);
4444 processSync(mapper);
4445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4447 ASSERT_EQ(0, motionArgs.buttonState);
4448
4449 // press BTN_LEFT, release BTN_LEFT
4450 processKey(mapper, BTN_LEFT, 1);
4451 processSync(mapper);
4452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4454 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4455
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4458 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4459
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 processKey(mapper, BTN_LEFT, 0);
4461 processSync(mapper);
4462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465
4466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004468 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469
4470 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4471 processKey(mapper, BTN_RIGHT, 1);
4472 processKey(mapper, BTN_MIDDLE, 1);
4473 processSync(mapper);
4474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4476 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4477 motionArgs.buttonState);
4478
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4481 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4482
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4485 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4486 motionArgs.buttonState);
4487
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 processKey(mapper, BTN_RIGHT, 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(AMOTION_EVENT_BUTTON_TERTIARY, 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(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497
4498 processKey(mapper, BTN_MIDDLE, 0);
4499 processSync(mapper);
4500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004501 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004503
4504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004506 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507
4508 // press BTN_BACK, release BTN_BACK
4509 processKey(mapper, BTN_BACK, 1);
4510 processSync(mapper);
4511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4512 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4513 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004514
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004517 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4518
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4521 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522
4523 processKey(mapper, BTN_BACK, 0);
4524 processSync(mapper);
4525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004528
4529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004531 ASSERT_EQ(0, motionArgs.buttonState);
4532
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4535 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4536
4537 // press BTN_SIDE, release BTN_SIDE
4538 processKey(mapper, BTN_SIDE, 1);
4539 processSync(mapper);
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4541 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4542 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004543
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004546 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4547
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4550 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551
4552 processKey(mapper, BTN_SIDE, 0);
4553 processSync(mapper);
4554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004557
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004560 ASSERT_EQ(0, motionArgs.buttonState);
4561
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4563 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4564 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4565
4566 // press BTN_FORWARD, release BTN_FORWARD
4567 processKey(mapper, BTN_FORWARD, 1);
4568 processSync(mapper);
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4570 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4571 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004572
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004575 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4576
4577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4579 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580
4581 processKey(mapper, BTN_FORWARD, 0);
4582 processSync(mapper);
4583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586
4587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004589 ASSERT_EQ(0, motionArgs.buttonState);
4590
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4592 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4593 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4594
4595 // press BTN_EXTRA, release BTN_EXTRA
4596 processKey(mapper, BTN_EXTRA, 1);
4597 processSync(mapper);
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4599 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4600 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4605
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4608 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609
4610 processKey(mapper, BTN_EXTRA, 0);
4611 processSync(mapper);
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004613 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615
4616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004618 ASSERT_EQ(0, motionArgs.buttonState);
4619
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4621 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4622 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4623
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4625
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 // press BTN_STYLUS, release BTN_STYLUS
4627 processKey(mapper, BTN_STYLUS, 1);
4628 processSync(mapper);
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004631 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4632
4633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4635 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636
4637 processKey(mapper, BTN_STYLUS, 0);
4638 processSync(mapper);
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004642
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004645 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646
4647 // press BTN_STYLUS2, release BTN_STYLUS2
4648 processKey(mapper, BTN_STYLUS2, 1);
4649 processSync(mapper);
4650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4653
4654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4655 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4656 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657
4658 processKey(mapper, BTN_STYLUS2, 0);
4659 processSync(mapper);
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004666 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667
4668 // release touch
4669 processUp(mapper);
4670 processSync(mapper);
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4672 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4673 ASSERT_EQ(0, motionArgs.buttonState);
4674}
4675
4676TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677 addConfigurationProperty("touch.deviceType", "touchScreen");
4678 prepareDisplay(DISPLAY_ORIENTATION_0);
4679 prepareButtons();
4680 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004681 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682
4683 NotifyMotionArgs motionArgs;
4684
4685 // default tool type is finger
4686 processDown(mapper, 100, 200);
4687 processSync(mapper);
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4689 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4691
4692 // eraser
4693 processKey(mapper, BTN_TOOL_RUBBER, 1);
4694 processSync(mapper);
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4698
4699 // stylus
4700 processKey(mapper, BTN_TOOL_RUBBER, 0);
4701 processKey(mapper, BTN_TOOL_PEN, 1);
4702 processSync(mapper);
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4705 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4706
4707 // brush
4708 processKey(mapper, BTN_TOOL_PEN, 0);
4709 processKey(mapper, BTN_TOOL_BRUSH, 1);
4710 processSync(mapper);
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4714
4715 // pencil
4716 processKey(mapper, BTN_TOOL_BRUSH, 0);
4717 processKey(mapper, BTN_TOOL_PENCIL, 1);
4718 processSync(mapper);
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4722
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004723 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724 processKey(mapper, BTN_TOOL_PENCIL, 0);
4725 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4726 processSync(mapper);
4727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4730
4731 // mouse
4732 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4733 processKey(mapper, BTN_TOOL_MOUSE, 1);
4734 processSync(mapper);
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4738
4739 // lens
4740 processKey(mapper, BTN_TOOL_MOUSE, 0);
4741 processKey(mapper, BTN_TOOL_LENS, 1);
4742 processSync(mapper);
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4746
4747 // double-tap
4748 processKey(mapper, BTN_TOOL_LENS, 0);
4749 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4750 processSync(mapper);
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4754
4755 // triple-tap
4756 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4757 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4758 processSync(mapper);
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4762
4763 // quad-tap
4764 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4765 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4766 processSync(mapper);
4767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4770
4771 // finger
4772 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4773 processKey(mapper, BTN_TOOL_FINGER, 1);
4774 processSync(mapper);
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4778
4779 // stylus trumps finger
4780 processKey(mapper, BTN_TOOL_PEN, 1);
4781 processSync(mapper);
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4785
4786 // eraser trumps stylus
4787 processKey(mapper, BTN_TOOL_RUBBER, 1);
4788 processSync(mapper);
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4792
4793 // mouse trumps eraser
4794 processKey(mapper, BTN_TOOL_MOUSE, 1);
4795 processSync(mapper);
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4799
4800 // back to default tool type
4801 processKey(mapper, BTN_TOOL_MOUSE, 0);
4802 processKey(mapper, BTN_TOOL_RUBBER, 0);
4803 processKey(mapper, BTN_TOOL_PEN, 0);
4804 processKey(mapper, BTN_TOOL_FINGER, 0);
4805 processSync(mapper);
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4809}
4810
4811TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812 addConfigurationProperty("touch.deviceType", "touchScreen");
4813 prepareDisplay(DISPLAY_ORIENTATION_0);
4814 prepareButtons();
4815 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004816 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004817 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818
4819 NotifyMotionArgs motionArgs;
4820
4821 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4822 processKey(mapper, BTN_TOOL_FINGER, 1);
4823 processMove(mapper, 100, 200);
4824 processSync(mapper);
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4826 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4828 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4829
4830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4831 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4833 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4834
4835 // move a little
4836 processMove(mapper, 150, 250);
4837 processSync(mapper);
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4841 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4842
4843 // down when BTN_TOUCH is pressed, pressure defaults to 1
4844 processKey(mapper, BTN_TOUCH, 1);
4845 processSync(mapper);
4846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4849 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4850
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4854 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4855
4856 // up when BTN_TOUCH is released, hover restored
4857 processKey(mapper, BTN_TOUCH, 0);
4858 processSync(mapper);
4859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4860 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4862 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4863
4864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4867 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4868
4869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4872 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4873
4874 // exit hover when pointer goes away
4875 processKey(mapper, BTN_TOOL_FINGER, 0);
4876 processSync(mapper);
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4878 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4880 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4881}
4882
4883TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884 addConfigurationProperty("touch.deviceType", "touchScreen");
4885 prepareDisplay(DISPLAY_ORIENTATION_0);
4886 prepareButtons();
4887 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004888 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889
4890 NotifyMotionArgs motionArgs;
4891
4892 // initially hovering because pressure is 0
4893 processDown(mapper, 100, 200);
4894 processPressure(mapper, 0);
4895 processSync(mapper);
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4897 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4899 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4900
4901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4902 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4904 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4905
4906 // move a little
4907 processMove(mapper, 150, 250);
4908 processSync(mapper);
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4910 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4912 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4913
4914 // down when pressure is non-zero
4915 processPressure(mapper, RAW_PRESSURE_MAX);
4916 processSync(mapper);
4917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4920 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4921
4922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4925 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4926
4927 // up when pressure becomes 0, hover restored
4928 processPressure(mapper, 0);
4929 processSync(mapper);
4930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4931 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4933 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4934
4935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4938 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4939
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4941 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4943 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4944
4945 // exit hover when pointer goes away
4946 processUp(mapper);
4947 processSync(mapper);
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4949 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4951 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4952}
4953
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954// --- MultiTouchInputMapperTest ---
4955
4956class MultiTouchInputMapperTest : public TouchInputMapperTest {
4957protected:
4958 void prepareAxes(int axes);
4959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004960 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
4961 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
4962 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
4963 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
4964 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
4965 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
4966 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
4967 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
4968 void processId(MultiTouchInputMapper& mapper, int32_t id);
4969 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
4970 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
4971 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
4972 void processMTSync(MultiTouchInputMapper& mapper);
4973 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974};
4975
4976void MultiTouchInputMapperTest::prepareAxes(int axes) {
4977 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004978 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4979 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 }
4981 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
4983 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004985 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
4986 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987 }
4988 }
4989 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
4991 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004993 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
4994 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995 }
4996 }
4997 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004998 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
4999 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 }
5001 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005002 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5003 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 }
5005 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005006 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5007 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008 }
5009 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005010 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5011 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 }
5013 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5015 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016 }
5017 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005018 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019 }
5020}
5021
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005022void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5023 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005024 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5025 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026}
5027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005028void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5029 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005030 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005031}
5032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005033void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5034 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005035 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036}
5037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005038void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005039 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040}
5041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005042void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005043 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005044}
5045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005046void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5047 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005048 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049}
5050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005051void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005052 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053}
5054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005055void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005056 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057}
5058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005059void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005060 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061}
5062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005063void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005064 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065}
5066
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005067void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005068 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069}
5070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005071void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5072 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005073 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074}
5075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005076void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005077 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078}
5079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005080void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005081 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082}
5083
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085 addConfigurationProperty("touch.deviceType", "touchScreen");
5086 prepareDisplay(DISPLAY_ORIENTATION_0);
5087 prepareAxes(POSITION);
5088 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005089 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090
5091 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5092
5093 NotifyMotionArgs motionArgs;
5094
5095 // Two fingers down at once.
5096 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5097 processPosition(mapper, x1, y1);
5098 processMTSync(mapper);
5099 processPosition(mapper, x2, y2);
5100 processMTSync(mapper);
5101 processSync(mapper);
5102
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5104 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5105 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5107 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5108 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5109 ASSERT_EQ(0, motionArgs.flags);
5110 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5111 ASSERT_EQ(0, motionArgs.buttonState);
5112 ASSERT_EQ(0, motionArgs.edgeFlags);
5113 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5117 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5118 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5119 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5121
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5123 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5124 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5125 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5126 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5127 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5128 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 // Move.
5147 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5148 processPosition(mapper, x1, y1);
5149 processMTSync(mapper);
5150 processPosition(mapper, x2, y2);
5151 processMTSync(mapper);
5152 processSync(mapper);
5153
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5155 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5156 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5157 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5158 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5160 ASSERT_EQ(0, motionArgs.flags);
5161 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5162 ASSERT_EQ(0, motionArgs.buttonState);
5163 ASSERT_EQ(0, motionArgs.edgeFlags);
5164 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5165 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5167 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5170 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5172 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5173 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5174 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5175 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5176
5177 // First finger up.
5178 x2 += 15; y2 -= 20;
5179 processPosition(mapper, x2, y2);
5180 processMTSync(mapper);
5181 processSync(mapper);
5182
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5185 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5186 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5187 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5188 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5189 motionArgs.action);
5190 ASSERT_EQ(0, motionArgs.flags);
5191 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5192 ASSERT_EQ(0, motionArgs.buttonState);
5193 ASSERT_EQ(0, motionArgs.edgeFlags);
5194 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5195 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5197 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5200 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5203 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5204 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5206
5207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5209 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5210 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5211 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5213 ASSERT_EQ(0, motionArgs.flags);
5214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5215 ASSERT_EQ(0, motionArgs.buttonState);
5216 ASSERT_EQ(0, motionArgs.edgeFlags);
5217 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5218 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5221 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5222 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5223 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5225
5226 // Move.
5227 x2 += 20; y2 -= 25;
5228 processPosition(mapper, x2, y2);
5229 processMTSync(mapper);
5230 processSync(mapper);
5231
5232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5234 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5235 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5236 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5238 ASSERT_EQ(0, motionArgs.flags);
5239 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5240 ASSERT_EQ(0, motionArgs.buttonState);
5241 ASSERT_EQ(0, motionArgs.edgeFlags);
5242 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5243 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5246 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5247 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5248 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5250
5251 // New finger down.
5252 int32_t x3 = 700, y3 = 300;
5253 processPosition(mapper, x2, y2);
5254 processMTSync(mapper);
5255 processPosition(mapper, x3, y3);
5256 processMTSync(mapper);
5257 processSync(mapper);
5258
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5260 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5261 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5262 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5263 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5264 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5265 motionArgs.action);
5266 ASSERT_EQ(0, motionArgs.flags);
5267 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5268 ASSERT_EQ(0, motionArgs.buttonState);
5269 ASSERT_EQ(0, motionArgs.edgeFlags);
5270 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5271 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5273 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5276 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5278 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5279 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5280 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5281 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5282
5283 // Second finger up.
5284 x3 += 30; y3 -= 20;
5285 processPosition(mapper, x3, y3);
5286 processMTSync(mapper);
5287 processSync(mapper);
5288
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5290 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5291 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5292 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5293 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5294 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5295 motionArgs.action);
5296 ASSERT_EQ(0, motionArgs.flags);
5297 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5298 ASSERT_EQ(0, motionArgs.buttonState);
5299 ASSERT_EQ(0, motionArgs.edgeFlags);
5300 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5301 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5303 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5306 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5308 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5309 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5310 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5312
5313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5315 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5316 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5317 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5319 ASSERT_EQ(0, motionArgs.flags);
5320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5321 ASSERT_EQ(0, motionArgs.buttonState);
5322 ASSERT_EQ(0, motionArgs.edgeFlags);
5323 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5324 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5327 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5328 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5329 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5331
5332 // Last finger up.
5333 processMTSync(mapper);
5334 processSync(mapper);
5335
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5338 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5339 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5340 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5341 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5342 ASSERT_EQ(0, motionArgs.flags);
5343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5344 ASSERT_EQ(0, motionArgs.buttonState);
5345 ASSERT_EQ(0, motionArgs.edgeFlags);
5346 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5347 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5350 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5351 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5352 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5354
5355 // Should not have sent any more keys or motions.
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5358}
5359
5360TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361 addConfigurationProperty("touch.deviceType", "touchScreen");
5362 prepareDisplay(DISPLAY_ORIENTATION_0);
5363 prepareAxes(POSITION | ID);
5364 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005365 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366
5367 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5368
5369 NotifyMotionArgs motionArgs;
5370
5371 // Two fingers down at once.
5372 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
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_DOWN, motionArgs.action);
5383 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5384 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5387 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5388
5389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5390 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5391 motionArgs.action);
5392 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5393 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5395 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5398 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5400 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5401
5402 // Move.
5403 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5404 processPosition(mapper, x1, y1);
5405 processId(mapper, 1);
5406 processMTSync(mapper);
5407 processPosition(mapper, x2, y2);
5408 processId(mapper, 2);
5409 processMTSync(mapper);
5410 processSync(mapper);
5411
5412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5414 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5415 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5417 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5420 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5422 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5423
5424 // First finger up.
5425 x2 += 15; y2 -= 20;
5426 processPosition(mapper, x2, y2);
5427 processId(mapper, 2);
5428 processMTSync(mapper);
5429 processSync(mapper);
5430
5431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5432 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5433 motionArgs.action);
5434 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5435 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5437 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5440 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5442 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5443
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5446 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5447 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5450 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5451
5452 // Move.
5453 x2 += 20; y2 -= 25;
5454 processPosition(mapper, x2, y2);
5455 processId(mapper, 2);
5456 processMTSync(mapper);
5457 processSync(mapper);
5458
5459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5461 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5462 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5465 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5466
5467 // New finger down.
5468 int32_t x3 = 700, y3 = 300;
5469 processPosition(mapper, x2, y2);
5470 processId(mapper, 2);
5471 processMTSync(mapper);
5472 processPosition(mapper, x3, y3);
5473 processId(mapper, 3);
5474 processMTSync(mapper);
5475 processSync(mapper);
5476
5477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5478 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5479 motionArgs.action);
5480 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5483 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5486 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5488 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5489
5490 // Second finger up.
5491 x3 += 30; y3 -= 20;
5492 processPosition(mapper, x3, y3);
5493 processId(mapper, 3);
5494 processMTSync(mapper);
5495 processSync(mapper);
5496
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5498 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5499 motionArgs.action);
5500 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5501 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5503 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5506 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5508 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5509
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5512 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5516 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5517
5518 // Last finger up.
5519 processMTSync(mapper);
5520 processSync(mapper);
5521
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5523 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5524 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5525 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5528 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5529
5530 // Should not have sent any more keys or motions.
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5533}
5534
5535TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536 addConfigurationProperty("touch.deviceType", "touchScreen");
5537 prepareDisplay(DISPLAY_ORIENTATION_0);
5538 prepareAxes(POSITION | ID | SLOT);
5539 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005541
5542 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5543
5544 NotifyMotionArgs motionArgs;
5545
5546 // Two fingers down at once.
5547 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5548 processPosition(mapper, x1, y1);
5549 processId(mapper, 1);
5550 processSlot(mapper, 1);
5551 processPosition(mapper, x2, y2);
5552 processId(mapper, 2);
5553 processSync(mapper);
5554
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5556 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5557 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5558 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5561 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5562
5563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5564 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5565 motionArgs.action);
5566 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5567 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5569 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5572 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5574 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5575
5576 // Move.
5577 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5578 processSlot(mapper, 0);
5579 processPosition(mapper, x1, y1);
5580 processSlot(mapper, 1);
5581 processPosition(mapper, x2, y2);
5582 processSync(mapper);
5583
5584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5586 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5587 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5589 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5592 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5594 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5595
5596 // First finger up.
5597 x2 += 15; y2 -= 20;
5598 processSlot(mapper, 0);
5599 processId(mapper, -1);
5600 processSlot(mapper, 1);
5601 processPosition(mapper, x2, y2);
5602 processSync(mapper);
5603
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5605 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5606 motionArgs.action);
5607 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5610 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5613 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5615 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5616
5617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5619 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5620 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5623 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5624
5625 // Move.
5626 x2 += 20; y2 -= 25;
5627 processPosition(mapper, x2, y2);
5628 processSync(mapper);
5629
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5632 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5633 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5636 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5637
5638 // New finger down.
5639 int32_t x3 = 700, y3 = 300;
5640 processPosition(mapper, x2, y2);
5641 processSlot(mapper, 0);
5642 processId(mapper, 3);
5643 processPosition(mapper, x3, y3);
5644 processSync(mapper);
5645
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5647 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5648 motionArgs.action);
5649 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5652 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5655 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5657 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5658
5659 // Second finger up.
5660 x3 += 30; y3 -= 20;
5661 processSlot(mapper, 1);
5662 processId(mapper, -1);
5663 processSlot(mapper, 0);
5664 processPosition(mapper, x3, y3);
5665 processSync(mapper);
5666
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5668 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5669 motionArgs.action);
5670 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5671 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5673 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5676 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5678 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5679
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5682 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5683 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5686 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5687
5688 // Last finger up.
5689 processId(mapper, -1);
5690 processSync(mapper);
5691
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5693 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5694 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5695 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5698 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5699
5700 // Should not have sent any more keys or motions.
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5703}
5704
5705TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 addConfigurationProperty("touch.deviceType", "touchScreen");
5707 prepareDisplay(DISPLAY_ORIENTATION_0);
5708 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005709 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710
5711 // These calculations are based on the input device calibration documentation.
5712 int32_t rawX = 100;
5713 int32_t rawY = 200;
5714 int32_t rawTouchMajor = 7;
5715 int32_t rawTouchMinor = 6;
5716 int32_t rawToolMajor = 9;
5717 int32_t rawToolMinor = 8;
5718 int32_t rawPressure = 11;
5719 int32_t rawDistance = 0;
5720 int32_t rawOrientation = 3;
5721 int32_t id = 5;
5722
5723 float x = toDisplayX(rawX);
5724 float y = toDisplayY(rawY);
5725 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5726 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5727 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5728 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5729 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5730 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5731 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5732 float distance = float(rawDistance);
5733
5734 processPosition(mapper, rawX, rawY);
5735 processTouchMajor(mapper, rawTouchMajor);
5736 processTouchMinor(mapper, rawTouchMinor);
5737 processToolMajor(mapper, rawToolMajor);
5738 processToolMinor(mapper, rawToolMinor);
5739 processPressure(mapper, rawPressure);
5740 processOrientation(mapper, rawOrientation);
5741 processDistance(mapper, rawDistance);
5742 processId(mapper, id);
5743 processMTSync(mapper);
5744 processSync(mapper);
5745
5746 NotifyMotionArgs args;
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5748 ASSERT_EQ(0, args.pointerProperties[0].id);
5749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5750 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5751 orientation, distance));
5752}
5753
5754TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755 addConfigurationProperty("touch.deviceType", "touchScreen");
5756 prepareDisplay(DISPLAY_ORIENTATION_0);
5757 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5758 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005759 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760
5761 // These calculations are based on the input device calibration documentation.
5762 int32_t rawX = 100;
5763 int32_t rawY = 200;
5764 int32_t rawTouchMajor = 140;
5765 int32_t rawTouchMinor = 120;
5766 int32_t rawToolMajor = 180;
5767 int32_t rawToolMinor = 160;
5768
5769 float x = toDisplayX(rawX);
5770 float y = toDisplayY(rawY);
5771 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5772 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5773 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5774 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5775 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5776
5777 processPosition(mapper, rawX, rawY);
5778 processTouchMajor(mapper, rawTouchMajor);
5779 processTouchMinor(mapper, rawTouchMinor);
5780 processToolMajor(mapper, rawToolMajor);
5781 processToolMinor(mapper, rawToolMinor);
5782 processMTSync(mapper);
5783 processSync(mapper);
5784
5785 NotifyMotionArgs args;
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5788 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5789}
5790
5791TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 addConfigurationProperty("touch.deviceType", "touchScreen");
5793 prepareDisplay(DISPLAY_ORIENTATION_0);
5794 prepareAxes(POSITION | TOUCH | TOOL);
5795 addConfigurationProperty("touch.size.calibration", "diameter");
5796 addConfigurationProperty("touch.size.scale", "10");
5797 addConfigurationProperty("touch.size.bias", "160");
5798 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005799 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800
5801 // These calculations are based on the input device calibration documentation.
5802 // Note: We only provide a single common touch/tool value because the device is assumed
5803 // not to emit separate values for each pointer (isSummed = 1).
5804 int32_t rawX = 100;
5805 int32_t rawY = 200;
5806 int32_t rawX2 = 150;
5807 int32_t rawY2 = 250;
5808 int32_t rawTouchMajor = 5;
5809 int32_t rawToolMajor = 8;
5810
5811 float x = toDisplayX(rawX);
5812 float y = toDisplayY(rawY);
5813 float x2 = toDisplayX(rawX2);
5814 float y2 = toDisplayY(rawY2);
5815 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5816 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5817 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5818
5819 processPosition(mapper, rawX, rawY);
5820 processTouchMajor(mapper, rawTouchMajor);
5821 processToolMajor(mapper, rawToolMajor);
5822 processMTSync(mapper);
5823 processPosition(mapper, rawX2, rawY2);
5824 processTouchMajor(mapper, rawTouchMajor);
5825 processToolMajor(mapper, rawToolMajor);
5826 processMTSync(mapper);
5827 processSync(mapper);
5828
5829 NotifyMotionArgs args;
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5831 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5835 args.action);
5836 ASSERT_EQ(size_t(2), args.pointerCount);
5837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5838 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5840 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5841}
5842
5843TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844 addConfigurationProperty("touch.deviceType", "touchScreen");
5845 prepareDisplay(DISPLAY_ORIENTATION_0);
5846 prepareAxes(POSITION | TOUCH | TOOL);
5847 addConfigurationProperty("touch.size.calibration", "area");
5848 addConfigurationProperty("touch.size.scale", "43");
5849 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005850 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851
5852 // These calculations are based on the input device calibration documentation.
5853 int32_t rawX = 100;
5854 int32_t rawY = 200;
5855 int32_t rawTouchMajor = 5;
5856 int32_t rawToolMajor = 8;
5857
5858 float x = toDisplayX(rawX);
5859 float y = toDisplayY(rawY);
5860 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5861 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5862 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5863
5864 processPosition(mapper, rawX, rawY);
5865 processTouchMajor(mapper, rawTouchMajor);
5866 processToolMajor(mapper, rawToolMajor);
5867 processMTSync(mapper);
5868 processSync(mapper);
5869
5870 NotifyMotionArgs args;
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5873 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5874}
5875
5876TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877 addConfigurationProperty("touch.deviceType", "touchScreen");
5878 prepareDisplay(DISPLAY_ORIENTATION_0);
5879 prepareAxes(POSITION | PRESSURE);
5880 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5881 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005882 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883
Michael Wrightaa449c92017-12-13 21:21:43 +00005884 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005885 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00005886 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5887 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5888 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5889
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 // These calculations are based on the input device calibration documentation.
5891 int32_t rawX = 100;
5892 int32_t rawY = 200;
5893 int32_t rawPressure = 60;
5894
5895 float x = toDisplayX(rawX);
5896 float y = toDisplayY(rawY);
5897 float pressure = float(rawPressure) * 0.01f;
5898
5899 processPosition(mapper, rawX, rawY);
5900 processPressure(mapper, rawPressure);
5901 processMTSync(mapper);
5902 processSync(mapper);
5903
5904 NotifyMotionArgs args;
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5907 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5908}
5909
5910TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911 addConfigurationProperty("touch.deviceType", "touchScreen");
5912 prepareDisplay(DISPLAY_ORIENTATION_0);
5913 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005914 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915
5916 NotifyMotionArgs motionArgs;
5917 NotifyKeyArgs keyArgs;
5918
5919 processId(mapper, 1);
5920 processPosition(mapper, 100, 200);
5921 processSync(mapper);
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5924 ASSERT_EQ(0, motionArgs.buttonState);
5925
5926 // press BTN_LEFT, release BTN_LEFT
5927 processKey(mapper, BTN_LEFT, 1);
5928 processSync(mapper);
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5931 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5932
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5935 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5936
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937 processKey(mapper, BTN_LEFT, 0);
5938 processSync(mapper);
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005942
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005945 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946
5947 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5948 processKey(mapper, BTN_RIGHT, 1);
5949 processKey(mapper, BTN_MIDDLE, 1);
5950 processSync(mapper);
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5953 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5954 motionArgs.buttonState);
5955
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5958 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5959
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5962 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5963 motionArgs.buttonState);
5964
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 processKey(mapper, BTN_RIGHT, 0);
5966 processSync(mapper);
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005970
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005973 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974
5975 processKey(mapper, BTN_MIDDLE, 0);
5976 processSync(mapper);
5977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005980
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005983 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984
5985 // press BTN_BACK, release BTN_BACK
5986 processKey(mapper, BTN_BACK, 1);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5989 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5990 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005994 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5995
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5997 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5998 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999
6000 processKey(mapper, BTN_BACK, 0);
6001 processSync(mapper);
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006003 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006005
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006008 ASSERT_EQ(0, motionArgs.buttonState);
6009
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6011 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6012 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6013
6014 // press BTN_SIDE, release BTN_SIDE
6015 processKey(mapper, BTN_SIDE, 1);
6016 processSync(mapper);
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6018 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6019 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006020
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006023 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6024
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6027 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028
6029 processKey(mapper, BTN_SIDE, 0);
6030 processSync(mapper);
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006034
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006037 ASSERT_EQ(0, motionArgs.buttonState);
6038
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6040 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6041 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6042
6043 // press BTN_FORWARD, release BTN_FORWARD
6044 processKey(mapper, BTN_FORWARD, 1);
6045 processSync(mapper);
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6047 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6048 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006049
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006052 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6053
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6056 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057
6058 processKey(mapper, BTN_FORWARD, 0);
6059 processSync(mapper);
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006066 ASSERT_EQ(0, motionArgs.buttonState);
6067
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6069 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6070 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6071
6072 // press BTN_EXTRA, release BTN_EXTRA
6073 processKey(mapper, BTN_EXTRA, 1);
6074 processSync(mapper);
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6076 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6077 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006078
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006081 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6082
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6085 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086
6087 processKey(mapper, BTN_EXTRA, 0);
6088 processSync(mapper);
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006092
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006095 ASSERT_EQ(0, motionArgs.buttonState);
6096
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6098 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6099 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6100
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6102
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 // press BTN_STYLUS, release BTN_STYLUS
6104 processKey(mapper, BTN_STYLUS, 1);
6105 processSync(mapper);
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006108 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6109
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6112 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006113
6114 processKey(mapper, BTN_STYLUS, 0);
6115 processSync(mapper);
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006119
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006122 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123
6124 // press BTN_STYLUS2, release BTN_STYLUS2
6125 processKey(mapper, BTN_STYLUS2, 1);
6126 processSync(mapper);
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006129 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6130
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6133 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134
6135 processKey(mapper, BTN_STYLUS2, 0);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006143 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144
6145 // release touch
6146 processId(mapper, -1);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6149 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6150 ASSERT_EQ(0, motionArgs.buttonState);
6151}
6152
6153TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154 addConfigurationProperty("touch.deviceType", "touchScreen");
6155 prepareDisplay(DISPLAY_ORIENTATION_0);
6156 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006157 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158
6159 NotifyMotionArgs motionArgs;
6160
6161 // default tool type is finger
6162 processId(mapper, 1);
6163 processPosition(mapper, 100, 200);
6164 processSync(mapper);
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6166 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6168
6169 // eraser
6170 processKey(mapper, BTN_TOOL_RUBBER, 1);
6171 processSync(mapper);
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6175
6176 // stylus
6177 processKey(mapper, BTN_TOOL_RUBBER, 0);
6178 processKey(mapper, BTN_TOOL_PEN, 1);
6179 processSync(mapper);
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6183
6184 // brush
6185 processKey(mapper, BTN_TOOL_PEN, 0);
6186 processKey(mapper, BTN_TOOL_BRUSH, 1);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6191
6192 // pencil
6193 processKey(mapper, BTN_TOOL_BRUSH, 0);
6194 processKey(mapper, BTN_TOOL_PENCIL, 1);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6199
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006200 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 processKey(mapper, BTN_TOOL_PENCIL, 0);
6202 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6207
6208 // mouse
6209 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6210 processKey(mapper, BTN_TOOL_MOUSE, 1);
6211 processSync(mapper);
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6215
6216 // lens
6217 processKey(mapper, BTN_TOOL_MOUSE, 0);
6218 processKey(mapper, BTN_TOOL_LENS, 1);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6223
6224 // double-tap
6225 processKey(mapper, BTN_TOOL_LENS, 0);
6226 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6231
6232 // triple-tap
6233 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6234 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6239
6240 // quad-tap
6241 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6242 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6247
6248 // finger
6249 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6250 processKey(mapper, BTN_TOOL_FINGER, 1);
6251 processSync(mapper);
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6255
6256 // stylus trumps finger
6257 processKey(mapper, BTN_TOOL_PEN, 1);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6262
6263 // eraser trumps stylus
6264 processKey(mapper, BTN_TOOL_RUBBER, 1);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6269
6270 // mouse trumps eraser
6271 processKey(mapper, BTN_TOOL_MOUSE, 1);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6276
6277 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6278 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6283
6284 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6285 processToolType(mapper, MT_TOOL_PEN);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6290
6291 // back to default tool type
6292 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6293 processKey(mapper, BTN_TOOL_MOUSE, 0);
6294 processKey(mapper, BTN_TOOL_RUBBER, 0);
6295 processKey(mapper, BTN_TOOL_PEN, 0);
6296 processKey(mapper, BTN_TOOL_FINGER, 0);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6301}
6302
6303TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 addConfigurationProperty("touch.deviceType", "touchScreen");
6305 prepareDisplay(DISPLAY_ORIENTATION_0);
6306 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006307 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006308 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309
6310 NotifyMotionArgs motionArgs;
6311
6312 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6313 processId(mapper, 1);
6314 processPosition(mapper, 100, 200);
6315 processSync(mapper);
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6319 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6320
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6322 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6324 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6325
6326 // move a little
6327 processPosition(mapper, 150, 250);
6328 processSync(mapper);
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 // down when BTN_TOUCH is pressed, pressure defaults to 1
6335 processKey(mapper, BTN_TOUCH, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6345 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6346
6347 // up when BTN_TOUCH is released, hover restored
6348 processKey(mapper, BTN_TOUCH, 0);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6353 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6354
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6356 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6358 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6359
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6363 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6364
6365 // exit hover when pointer goes away
6366 processId(mapper, -1);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6371 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6372}
6373
6374TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 addConfigurationProperty("touch.deviceType", "touchScreen");
6376 prepareDisplay(DISPLAY_ORIENTATION_0);
6377 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006378 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379
6380 NotifyMotionArgs motionArgs;
6381
6382 // initially hovering because pressure is 0
6383 processId(mapper, 1);
6384 processPosition(mapper, 100, 200);
6385 processPressure(mapper, 0);
6386 processSync(mapper);
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6390 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6391
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6395 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6396
6397 // move a little
6398 processPosition(mapper, 150, 250);
6399 processSync(mapper);
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 // down when pressure becomes non-zero
6406 processPressure(mapper, RAW_PRESSURE_MAX);
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6416 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6417
6418 // up when pressure becomes 0, hover restored
6419 processPressure(mapper, 0);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6424 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6425
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6429 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6430
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6434 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6435
6436 // exit hover when pointer goes away
6437 processId(mapper, -1);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6443}
6444
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006445/**
6446 * Set the input device port <--> display port associations, and check that the
6447 * events are routed to the display that matches the display port.
6448 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6449 */
6450TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006451 const std::string usb2 = "USB2";
6452 const uint8_t hdmi1 = 0;
6453 const uint8_t hdmi2 = 1;
6454 const std::string secondaryUniqueId = "uniqueId2";
6455 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6456
6457 addConfigurationProperty("touch.deviceType", "touchScreen");
6458 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006459 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006460
6461 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6462 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6463
6464 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6465 // for this input device is specified, and the matching viewport is not present,
6466 // the input device should be disabled (at the mapper level).
6467
6468 // Add viewport for display 2 on hdmi2
6469 prepareSecondaryDisplay(type, hdmi2);
6470 // Send a touch event
6471 processPosition(mapper, 100, 100);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6474
6475 // Add viewport for display 1 on hdmi1
6476 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6477 // Send a touch event again
6478 processPosition(mapper, 100, 100);
6479 processSync(mapper);
6480
6481 NotifyMotionArgs args;
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6483 ASSERT_EQ(DISPLAY_ID, args.displayId);
6484}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006486TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006487 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006488 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006489 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006490 fakePointerController->setPosition(100, 200);
6491 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006492 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6493
Garfield Tan888a6a42020-01-09 11:39:16 -08006494 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6495 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6496
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006497 prepareDisplay(DISPLAY_ORIENTATION_0);
6498 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006499 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006500
6501 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006502 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006503
6504 NotifyMotionArgs motionArgs;
6505 processPosition(mapper, 100, 100);
6506 processSync(mapper);
6507
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6510 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6511}
6512
Arthur Hung7c645402019-01-25 17:45:42 +08006513TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6514 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006515 prepareAxes(POSITION | ID | SLOT);
6516 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006517 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006518
6519 // Create the second touch screen device, and enable multi fingers.
6520 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006521 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006522 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006523 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006524 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006525 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006526 std::unique_ptr<InputDevice> device2 =
6527 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006528 identifier);
6529 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6530 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6531 0 /*flat*/, 0 /*fuzz*/);
6532 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6533 0 /*flat*/, 0 /*fuzz*/);
6534 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6535 0 /*flat*/, 0 /*fuzz*/);
6536 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6537 0 /*flat*/, 0 /*fuzz*/);
6538 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6539 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6540 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006541
6542 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006543 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006544 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6545 device2->reset(ARBITRARY_TIME);
6546
6547 // Setup PointerController.
6548 sp<FakePointerController> fakePointerController = new FakePointerController();
6549 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6550 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6551
6552 // Setup policy for associated displays and show touches.
6553 const uint8_t hdmi1 = 0;
6554 const uint8_t hdmi2 = 1;
6555 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6556 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6557 mFakePolicy->setShowTouches(true);
6558
6559 // Create displays.
6560 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6561 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6562
6563 // Default device will reconfigure above, need additional reconfiguration for another device.
6564 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6565 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6566
6567 // Two fingers down at default display.
6568 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6569 processPosition(mapper, x1, y1);
6570 processId(mapper, 1);
6571 processSlot(mapper, 1);
6572 processPosition(mapper, x2, y2);
6573 processId(mapper, 2);
6574 processSync(mapper);
6575
6576 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6577 fakePointerController->getSpots().find(DISPLAY_ID);
6578 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6579 ASSERT_EQ(size_t(2), iter->second.size());
6580
6581 // Two fingers down at second display.
6582 processPosition(mapper2, x1, y1);
6583 processId(mapper2, 1);
6584 processSlot(mapper2, 1);
6585 processPosition(mapper2, x2, y2);
6586 processId(mapper2, 2);
6587 processSync(mapper2);
6588
6589 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6590 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6591 ASSERT_EQ(size_t(2), iter->second.size());
6592}
6593
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006594TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006595 prepareAxes(POSITION);
6596 addConfigurationProperty("touch.deviceType", "touchScreen");
6597 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006598 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006599
6600 NotifyMotionArgs motionArgs;
6601 // Unrotated video frame
6602 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6603 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006604 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006605 processPosition(mapper, 100, 200);
6606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6608 ASSERT_EQ(frames, motionArgs.videoFrames);
6609
6610 // Subsequent touch events should not have any videoframes
6611 // This is implemented separately in FakeEventHub,
6612 // but that should match the behaviour of TouchVideoDevice.
6613 processPosition(mapper, 200, 200);
6614 processSync(mapper);
6615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6616 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6617}
6618
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006619TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006620 prepareAxes(POSITION);
6621 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006622 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006623 // Unrotated video frame
6624 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6625 NotifyMotionArgs motionArgs;
6626
6627 // Test all 4 orientations
6628 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6629 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6630 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6631 clearViewports();
6632 prepareDisplay(orientation);
6633 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006634 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006635 processPosition(mapper, 100, 200);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 frames[0].rotate(orientation);
6639 ASSERT_EQ(frames, motionArgs.videoFrames);
6640 }
6641}
6642
6643TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006644 prepareAxes(POSITION);
6645 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006647 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6648 // so mix these.
6649 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6650 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6651 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6652 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6653 NotifyMotionArgs motionArgs;
6654
6655 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006656 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006657 processPosition(mapper, 100, 200);
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 std::for_each(frames.begin(), frames.end(),
6661 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6662 ASSERT_EQ(frames, motionArgs.videoFrames);
6663}
6664
Arthur Hung9da14732019-09-02 16:16:58 +08006665/**
6666 * If we had defined port associations, but the viewport is not ready, the touch device would be
6667 * expected to be disabled, and it should be enabled after the viewport has found.
6668 */
6669TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006670 constexpr uint8_t hdmi2 = 1;
6671 const std::string secondaryUniqueId = "uniqueId2";
6672 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6673
6674 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6675
6676 addConfigurationProperty("touch.deviceType", "touchScreen");
6677 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006678 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006679
6680 ASSERT_EQ(mDevice->isEnabled(), false);
6681
6682 // Add display on hdmi2, the device should be enabled and can receive touch event.
6683 prepareSecondaryDisplay(type, hdmi2);
6684 ASSERT_EQ(mDevice->isEnabled(), true);
6685
6686 // Send a touch event.
6687 processPosition(mapper, 100, 100);
6688 processSync(mapper);
6689
6690 NotifyMotionArgs args;
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6692 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6693}
6694
Arthur Hung6cd19a42019-08-30 19:04:12 +08006695/**
6696 * Test touch should not work if outside of surface.
6697 */
6698TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
Arthur Hung6cd19a42019-08-30 19:04:12 +08006699 addConfigurationProperty("touch.deviceType", "touchScreen");
6700 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006701 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006702 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung6cd19a42019-08-30 19:04:12 +08006703
Arthur Hung05de5772019-09-26 18:31:26 +08006704 // Touch on left-top area should work.
6705 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6706 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6707 processPosition(mapper, rawX, rawY);
6708 processSync(mapper);
6709
6710 NotifyMotionArgs args;
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6712
6713 // Reset.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006714 mapper.reset(ARBITRARY_TIME);
Arthur Hung05de5772019-09-26 18:31:26 +08006715
6716 // Let logical display be different to physical display and rotate 90-degrees.
6717 std::optional<DisplayViewport> internalViewport =
6718 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6719 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6720 internalViewport->logicalLeft = 0;
6721 internalViewport->logicalTop = 0;
6722 internalViewport->logicalRight = DISPLAY_HEIGHT;
6723 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6724
6725 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6726 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6727 internalViewport->physicalRight = DISPLAY_HEIGHT;
6728 internalViewport->physicalBottom = DISPLAY_WIDTH;
6729
6730 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6731 internalViewport->deviceHeight = DISPLAY_WIDTH;
6732 mFakePolicy->updateViewport(internalViewport.value());
6733 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6734
6735 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006736 processPosition(mapper, rawX, rawY);
6737 processSync(mapper);
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6739}
6740
Arthur Hung421eb1c2020-01-16 00:09:42 +08006741TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006742 addConfigurationProperty("touch.deviceType", "touchScreen");
6743 prepareDisplay(DISPLAY_ORIENTATION_0);
6744 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006745 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006746
6747 NotifyMotionArgs motionArgs;
6748
6749 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6750 // finger down
6751 processId(mapper, 1);
6752 processPosition(mapper, x1, y1);
6753 processSync(mapper);
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6755 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6757
6758 // finger move
6759 processId(mapper, 1);
6760 processPosition(mapper, x2, y2);
6761 processSync(mapper);
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6765
6766 // finger up.
6767 processId(mapper, -1);
6768 processSync(mapper);
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6772
6773 // new finger down
6774 processId(mapper, 1);
6775 processPosition(mapper, x3, y3);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6780}
6781
6782/**
6783 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6784 * UP events should be ignored.
6785 */
6786TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006787 addConfigurationProperty("touch.deviceType", "touchScreen");
6788 prepareDisplay(DISPLAY_ORIENTATION_0);
6789 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006790 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006791
6792 NotifyMotionArgs motionArgs;
6793
6794 // default tool type is finger
6795 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6796 processId(mapper, 1);
6797 processPosition(mapper, x1, y1);
6798 processSync(mapper);
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6802
6803 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6804 processToolType(mapper, MT_TOOL_PALM);
6805 processSync(mapper);
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6807 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6808
6809 // Ignore the following MOVE and UP events if had detect a palm event.
6810 processId(mapper, 1);
6811 processPosition(mapper, x2, y2);
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6814
6815 // finger up.
6816 processId(mapper, -1);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6819
6820 // new finger down
6821 processToolType(mapper, MT_TOOL_FINGER);
6822 processId(mapper, 1);
6823 processPosition(mapper, x3, y3);
6824 processSync(mapper);
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6826 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6828}
6829
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006830// --- MultiTouchInputMapperTest_ExternalDevice ---
6831
6832class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
6833protected:
6834 virtual void SetUp() override {
6835 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
6836 }
6837};
6838
6839/**
6840 * Expect fallback to internal viewport if device is external and external viewport is not present.
6841 */
6842TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
6843 prepareAxes(POSITION);
6844 addConfigurationProperty("touch.deviceType", "touchScreen");
6845 prepareDisplay(DISPLAY_ORIENTATION_0);
6846 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6847
6848 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6849
6850 NotifyMotionArgs motionArgs;
6851
6852 // Expect the event to be sent to the internal viewport,
6853 // because an external viewport is not present.
6854 processPosition(mapper, 100, 100);
6855 processSync(mapper);
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6857 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6858
6859 // Expect the event to be sent to the external viewport if it is present.
6860 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6861 processPosition(mapper, 100, 100);
6862 processSync(mapper);
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6864 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6865}
6866
Michael Wrightd02c5b62014-02-10 15:10:22 -08006867} // namespace android