blob: 752213a4997915f4d0894e46a081ad4f94ec006a [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;
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800833 wp<PointerControllerInterface> mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834
835public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700836 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
837 const sp<InputReaderPolicyInterface>& policy,
838 const sp<InputListenerInterface>& listener)
839 : mEventHub(eventHub),
840 mPolicy(policy),
841 mListener(listener),
842 mGlobalMetaState(0),
843 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844
845 virtual ~FakeInputReaderContext() { }
846
847 void assertUpdateGlobalMetaStateWasCalled() {
848 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
849 << "Expected updateGlobalMetaState() to have been called.";
850 mUpdateGlobalMetaStateWasCalled = false;
851 }
852
853 void setGlobalMetaState(int32_t state) {
854 mGlobalMetaState = state;
855 }
856
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800857 uint32_t getGeneration() {
858 return mGeneration;
859 }
860
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800861 void updatePointerDisplay() {
862 sp<PointerControllerInterface> controller = mPointerController.promote();
863 if (controller != nullptr) {
864 InputReaderConfiguration config;
865 mPolicy->getReaderConfiguration(&config);
866 auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
867 if (viewport) {
868 controller->setDisplayViewport(*viewport);
869 }
870 }
871 }
872
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873private:
874 virtual void updateGlobalMetaState() {
875 mUpdateGlobalMetaStateWasCalled = true;
876 }
877
878 virtual int32_t getGlobalMetaState() {
879 return mGlobalMetaState;
880 }
881
882 virtual EventHubInterface* getEventHub() {
883 return mEventHub.get();
884 }
885
886 virtual InputReaderPolicyInterface* getPolicy() {
887 return mPolicy.get();
888 }
889
890 virtual InputListenerInterface* getListener() {
891 return mListener.get();
892 }
893
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100894 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 }
896
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800897 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800899 virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) {
900 sp<PointerControllerInterface> controller = mPointerController.promote();
901 if (controller == nullptr) {
902 controller = mPolicy->obtainPointerController(deviceId);
903 mPointerController = controller;
904 updatePointerDisplay();
905 }
906 return controller;
907 }
908
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 virtual void fadePointer() {
910 }
911
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100912 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 }
914
915 virtual int32_t bumpGeneration() {
916 return ++mGeneration;
917 }
Michael Wright842500e2015-03-13 17:32:02 -0700918
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800919 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700920
921 }
922
923 virtual void dispatchExternalStylusState(const StylusState&) {
924
925 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800926
927 virtual uint32_t getNextSequenceNum() {
928 return mNextSequenceNum++;
929 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930};
931
932
933// --- FakeInputMapper ---
934
935class FakeInputMapper : public InputMapper {
936 uint32_t mSources;
937 int32_t mKeyboardType;
938 int32_t mMetaState;
939 KeyedVector<int32_t, int32_t> mKeyCodeStates;
940 KeyedVector<int32_t, int32_t> mScanCodeStates;
941 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800942 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700944 std::mutex mLock;
945 std::condition_variable mStateChangedCondition;
946 bool mConfigureWasCalled GUARDED_BY(mLock);
947 bool mResetWasCalled GUARDED_BY(mLock);
948 bool mProcessWasCalled GUARDED_BY(mLock);
949 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950
Arthur Hungc23540e2018-11-29 20:42:11 +0800951 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800953 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
954 : InputMapper(deviceContext),
955 mSources(sources),
956 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800958 mConfigureWasCalled(false),
959 mResetWasCalled(false),
960 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961
962 virtual ~FakeInputMapper() { }
963
964 void setKeyboardType(int32_t keyboardType) {
965 mKeyboardType = keyboardType;
966 }
967
968 void setMetaState(int32_t metaState) {
969 mMetaState = metaState;
970 }
971
972 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700973 std::unique_lock<std::mutex> lock(mLock);
974 base::ScopedLockAssertion assumeLocked(mLock);
975 const bool configureCalled =
976 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
977 return mConfigureWasCalled;
978 });
979 if (!configureCalled) {
980 FAIL() << "Expected configure() to have been called.";
981 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 mConfigureWasCalled = false;
983 }
984
985 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700986 std::unique_lock<std::mutex> lock(mLock);
987 base::ScopedLockAssertion assumeLocked(mLock);
988 const bool resetCalled =
989 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
990 return mResetWasCalled;
991 });
992 if (!resetCalled) {
993 FAIL() << "Expected reset() to have been called.";
994 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 mResetWasCalled = false;
996 }
997
Yi Kong9b14ac62018-07-17 13:48:38 -0700998 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700999 std::unique_lock<std::mutex> lock(mLock);
1000 base::ScopedLockAssertion assumeLocked(mLock);
1001 const bool processCalled =
1002 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1003 return mProcessWasCalled;
1004 });
1005 if (!processCalled) {
1006 FAIL() << "Expected process() to have been called.";
1007 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008 if (outLastEvent) {
1009 *outLastEvent = mLastEvent;
1010 }
1011 mProcessWasCalled = false;
1012 }
1013
1014 void setKeyCodeState(int32_t keyCode, int32_t state) {
1015 mKeyCodeStates.replaceValueFor(keyCode, state);
1016 }
1017
1018 void setScanCodeState(int32_t scanCode, int32_t state) {
1019 mScanCodeStates.replaceValueFor(scanCode, state);
1020 }
1021
1022 void setSwitchState(int32_t switchCode, int32_t state) {
1023 mSwitchStates.replaceValueFor(switchCode, state);
1024 }
1025
1026 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001027 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001028 }
1029
1030private:
1031 virtual uint32_t getSources() {
1032 return mSources;
1033 }
1034
1035 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1036 InputMapper::populateDeviceInfo(deviceInfo);
1037
1038 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1039 deviceInfo->setKeyboardType(mKeyboardType);
1040 }
1041 }
1042
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001044 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001046
1047 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001048 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001049 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1050 mViewport = config->getDisplayViewportByPort(*displayPort);
1051 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001052
1053 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001054 }
1055
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001056 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001057 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001059 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001060 }
1061
1062 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001063 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064 mLastEvent = *rawEvent;
1065 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001066 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067 }
1068
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001069 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1071 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1072 }
1073
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001074 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1076 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1077 }
1078
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001079 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1081 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1082 }
1083
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001084 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 const int32_t* keyCodes, uint8_t* outFlags) {
1086 bool result = false;
1087 for (size_t i = 0; i < numCodes; i++) {
1088 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1089 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1090 outFlags[i] = 1;
1091 result = true;
1092 }
1093 }
1094 }
1095 return result;
1096 }
1097
1098 virtual int32_t getMetaState() {
1099 return mMetaState;
1100 }
1101
1102 virtual void fadePointer() {
1103 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001104
1105 virtual std::optional<int32_t> getAssociatedDisplay() {
1106 if (mViewport) {
1107 return std::make_optional(mViewport->displayId);
1108 }
1109 return std::nullopt;
1110 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111};
1112
1113
1114// --- InstrumentedInputReader ---
1115
1116class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001117 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118
1119public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001120 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1121 const sp<InputReaderPolicyInterface>& policy,
1122 const sp<InputListenerInterface>& listener)
1123 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001124
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001125 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001127 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001129 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001130 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 InputDeviceIdentifier identifier;
1132 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001133 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001135 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001136 }
1137
Prabir Pradhan28efc192019-11-05 01:10:04 +00001138 // Make the protected loopOnce method accessible to tests.
1139 using InputReader::loopOnce;
1140
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001142 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1143 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001145 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001146 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147 return device;
1148 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001149 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150 }
1151
1152 friend class InputReaderTest;
1153};
1154
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001155// --- InputReaderPolicyTest ---
1156class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001157protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001158 sp<FakeInputReaderPolicy> mFakePolicy;
1159
Prabir Pradhan28efc192019-11-05 01:10:04 +00001160 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1161 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001162};
1163
1164/**
1165 * Check that empty set of viewports is an acceptable configuration.
1166 * Also try to get internal viewport two different ways - by type and by uniqueId.
1167 *
1168 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1169 * Such configuration is not currently allowed.
1170 */
1171TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001172 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173
1174 // We didn't add any viewports yet, so there shouldn't be any.
1175 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001176 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177 ASSERT_FALSE(internalViewport);
1178
1179 // Add an internal viewport, then clear it
1180 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001181 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001182
1183 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001184 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001186 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187
1188 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001189 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001190 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192
1193 mFakePolicy->clearViewports();
1194 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001195 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001196 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001197 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001198 ASSERT_FALSE(internalViewport);
1199}
1200
1201TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1202 const std::string internalUniqueId = "local:0";
1203 const std::string externalUniqueId = "local:1";
1204 const std::string virtualUniqueId1 = "virtual:2";
1205 const std::string virtualUniqueId2 = "virtual:3";
1206 constexpr int32_t virtualDisplayId1 = 2;
1207 constexpr int32_t virtualDisplayId2 = 3;
1208
1209 // Add an internal viewport
1210 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001211 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212 // Add an external viewport
1213 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001214 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001215 // Add an virtual viewport
1216 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001217 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001218 // Add another virtual viewport
1219 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001220 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221
1222 // Check matching by type for internal
1223 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001224 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001225 ASSERT_TRUE(internalViewport);
1226 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1227
1228 // Check matching by type for external
1229 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001230 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001231 ASSERT_TRUE(externalViewport);
1232 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1233
1234 // Check matching by uniqueId for virtual viewport #1
1235 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001236 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001238 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001239 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1240 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1241
1242 // Check matching by uniqueId for virtual viewport #2
1243 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001244 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001245 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001246 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001247 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1248 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1249}
1250
1251
1252/**
1253 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1254 * that lookup works by checking display id.
1255 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1256 */
1257TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1258 const std::string uniqueId1 = "uniqueId1";
1259 const std::string uniqueId2 = "uniqueId2";
1260 constexpr int32_t displayId1 = 2;
1261 constexpr int32_t displayId2 = 3;
1262
1263 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1264 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1265 for (const ViewportType& type : types) {
1266 mFakePolicy->clearViewports();
1267 // Add a viewport
1268 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001269 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270 // Add another viewport
1271 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001272 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001273
1274 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001275 std::optional<DisplayViewport> viewport1 =
1276 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001277 ASSERT_TRUE(viewport1);
1278 ASSERT_EQ(displayId1, viewport1->displayId);
1279 ASSERT_EQ(type, viewport1->type);
1280
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001281 std::optional<DisplayViewport> viewport2 =
1282 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001283 ASSERT_TRUE(viewport2);
1284 ASSERT_EQ(displayId2, viewport2->displayId);
1285 ASSERT_EQ(type, viewport2->type);
1286
1287 // When there are multiple viewports of the same kind, and uniqueId is not specified
1288 // in the call to getDisplayViewport, then that situation is not supported.
1289 // The viewports can be stored in any order, so we cannot rely on the order, since that
1290 // is just implementation detail.
1291 // However, we can check that it still returns *a* viewport, we just cannot assert
1292 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001293 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001294 ASSERT_TRUE(someViewport);
1295 }
1296}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001297
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001298/**
1299 * Check getDisplayViewportByPort
1300 */
1301TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1302 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1303 const std::string uniqueId1 = "uniqueId1";
1304 const std::string uniqueId2 = "uniqueId2";
1305 constexpr int32_t displayId1 = 1;
1306 constexpr int32_t displayId2 = 2;
1307 const uint8_t hdmi1 = 0;
1308 const uint8_t hdmi2 = 1;
1309 const uint8_t hdmi3 = 2;
1310
1311 mFakePolicy->clearViewports();
1312 // Add a viewport that's associated with some display port that's not of interest.
1313 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1314 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1315 // Add another viewport, connected to HDMI1 port
1316 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1317 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1318
1319 // Check that correct display viewport was returned by comparing the display ports.
1320 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1321 ASSERT_TRUE(hdmi1Viewport);
1322 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1323 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1324
1325 // Check that we can still get the same viewport using the uniqueId
1326 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1327 ASSERT_TRUE(hdmi1Viewport);
1328 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1329 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1330 ASSERT_EQ(type, hdmi1Viewport->type);
1331
1332 // Check that we cannot find a port with "HDMI2", because we never added one
1333 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1334 ASSERT_FALSE(hdmi2Viewport);
1335}
1336
Michael Wrightd02c5b62014-02-10 15:10:22 -08001337// --- InputReaderTest ---
1338
1339class InputReaderTest : public testing::Test {
1340protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001341 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001343 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001344 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001345
Prabir Pradhan28efc192019-11-05 01:10:04 +00001346 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001347 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001348 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001349 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001350
Prabir Pradhan28efc192019-11-05 01:10:04 +00001351 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1352 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001353 }
1354
Prabir Pradhan28efc192019-11-05 01:10:04 +00001355 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001356 mFakeListener.clear();
1357 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001358 }
1359
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001360 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1361 const PropertyMap* configuration) {
1362 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001363
1364 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001365 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001366 }
1367 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001368 mReader->loopOnce();
1369 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001370 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1371 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001372 }
1373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001374 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001375 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001376 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001377 }
1378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001379 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001380 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001381 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001382 }
1383
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001384 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001385 const std::string& name, uint32_t classes,
1386 uint32_t sources,
1387 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001388 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1389 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001390 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001391 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001392 return mapper;
1393 }
1394};
1395
1396TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001397 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001398 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001399 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001400 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001402 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001403 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001404 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001405 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001406 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1409 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1410
1411 // Should also have received a notification describing the new input devices.
1412 inputDevices = mFakePolicy->getInputDevices();
1413 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001414 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001415 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1417 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1418 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1419}
1420
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001421TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001422 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001423 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001424 constexpr int32_t eventHubId = 1;
1425 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001426 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001427 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001428 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001429 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001430
Yi Kong9b14ac62018-07-17 13:48:38 -07001431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001432
1433 NotifyDeviceResetArgs resetArgs;
1434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001435 ASSERT_EQ(deviceId, resetArgs.deviceId);
1436
1437 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001438 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001439 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001440
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001442 ASSERT_EQ(deviceId, resetArgs.deviceId);
1443 ASSERT_EQ(device->isEnabled(), false);
1444
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001445 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001446 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001449 ASSERT_EQ(device->isEnabled(), false);
1450
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001451 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001452 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001454 ASSERT_EQ(deviceId, resetArgs.deviceId);
1455 ASSERT_EQ(device->isEnabled(), true);
1456}
1457
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001459 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1460 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1461 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001462 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001463 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001464 AINPUT_SOURCE_KEYBOARD, nullptr);
1465 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001466
1467 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1468 AINPUT_SOURCE_ANY, AKEYCODE_A))
1469 << "Should return unknown when the device id is >= 0 but unknown.";
1470
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001471 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1472 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1473 << "Should return unknown when the device id is valid but the sources are not "
1474 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001476 ASSERT_EQ(AKEY_STATE_DOWN,
1477 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1478 AKEYCODE_A))
1479 << "Should return value provided by mapper when device id is valid and the device "
1480 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481
1482 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1483 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1484 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1485
1486 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1487 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1488 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1489}
1490
1491TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001492 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1493 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1494 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001495 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001496 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001497 AINPUT_SOURCE_KEYBOARD, nullptr);
1498 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001499
1500 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1501 AINPUT_SOURCE_ANY, KEY_A))
1502 << "Should return unknown when the device id is >= 0 but unknown.";
1503
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001504 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1505 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1506 << "Should return unknown when the device id is valid but the sources are not "
1507 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001508
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001509 ASSERT_EQ(AKEY_STATE_DOWN,
1510 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1511 KEY_A))
1512 << "Should return value provided by mapper when device id is valid and the device "
1513 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514
1515 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1516 AINPUT_SOURCE_TRACKBALL, KEY_A))
1517 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1518
1519 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1520 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1521 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1522}
1523
1524TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001525 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1526 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1527 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001528 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001529 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001530 AINPUT_SOURCE_KEYBOARD, nullptr);
1531 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001532
1533 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1534 AINPUT_SOURCE_ANY, SW_LID))
1535 << "Should return unknown when the device id is >= 0 but unknown.";
1536
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001537 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1538 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1539 << "Should return unknown when the device id is valid but the sources are not "
1540 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001541
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001542 ASSERT_EQ(AKEY_STATE_DOWN,
1543 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1544 SW_LID))
1545 << "Should return value provided by mapper when device id is valid and the device "
1546 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547
1548 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1549 AINPUT_SOURCE_TRACKBALL, SW_LID))
1550 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1551
1552 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1553 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1554 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1555}
1556
1557TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001558 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1559 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1560 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001561 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001562 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001563 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001565 mapper.addSupportedKeyCode(AKEYCODE_A);
1566 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567
1568 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1569 uint8_t flags[4] = { 0, 0, 0, 1 };
1570
1571 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1572 << "Should return false when device id is >= 0 but unknown.";
1573 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1574
1575 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001576 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1577 << "Should return false when device id is valid but the sources are not supported by "
1578 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1580
1581 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001582 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1583 keyCodes, flags))
1584 << "Should return value provided by mapper when device id is valid and the device "
1585 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1587
1588 flags[3] = 1;
1589 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1590 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1591 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1592
1593 flags[3] = 1;
1594 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1595 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1596 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1597}
1598
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001599TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001600 constexpr int32_t eventHubId = 1;
1601 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001602
1603 NotifyConfigurationChangedArgs args;
1604
1605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1606 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1607}
1608
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001609TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1611 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1612 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001613 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001615 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001617 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001618 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1620
1621 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001622 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001623 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001624 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 ASSERT_EQ(EV_KEY, event.type);
1626 ASSERT_EQ(KEY_A, event.code);
1627 ASSERT_EQ(1, event.value);
1628}
1629
Prabir Pradhan42611e02018-11-27 14:04:02 -08001630TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001631 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001632 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 constexpr int32_t eventHubId = 1;
1634 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001635 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001636 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001637 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001639
1640 NotifyDeviceResetArgs resetArgs;
1641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1642 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1643
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001644 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001645 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001647 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1648 prevSequenceNum = resetArgs.sequenceNum;
1649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001650 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001651 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001653 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1654 prevSequenceNum = resetArgs.sequenceNum;
1655
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001656 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001657 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001659 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1660 prevSequenceNum = resetArgs.sequenceNum;
1661}
1662
Arthur Hungc23540e2018-11-29 20:42:11 +08001663TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001664 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001665 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001666 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001667 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001668 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1669 FakeInputMapper& mapper =
1670 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001671 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001672
1673 const uint8_t hdmi1 = 1;
1674
1675 // Associated touch screen with second display.
1676 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1677
1678 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001679 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001680 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1681 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1682 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1683 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1684 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001685 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001686
1687 // Add the device, and make sure all of the callbacks are triggered.
1688 // The device is added after the input port associations are processed since
1689 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001693 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001694
Arthur Hung2c9a3342019-07-23 14:18:59 +08001695 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001696 ASSERT_EQ(deviceId, device->getId());
1697 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1698 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001699
1700 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001701 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001702 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001703 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001704}
1705
Michael Wrightd02c5b62014-02-10 15:10:22 -08001706
1707// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001708class InputDeviceTest : public testing::Test {
1709protected:
1710 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001711 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712 static const int32_t DEVICE_ID;
1713 static const int32_t DEVICE_GENERATION;
1714 static const int32_t DEVICE_CONTROLLER_NUMBER;
1715 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001716 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001718 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001719 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001720 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721 FakeInputReaderContext* mFakeContext;
1722
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001723 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001724
Prabir Pradhan28efc192019-11-05 01:10:04 +00001725 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001726 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001727 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001728 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001732 InputDeviceIdentifier identifier;
1733 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001734 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001735 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1736 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001737 }
1738
Prabir Pradhan28efc192019-11-05 01:10:04 +00001739 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001740 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001741 delete mFakeContext;
1742 mFakeListener.clear();
1743 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744 }
1745};
1746
1747const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001748const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001749const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001750const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1751const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1752const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1753 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001754const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001755
1756TEST_F(InputDeviceTest, ImmutableProperties) {
1757 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001758 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001759 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760}
1761
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001762TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
1763 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001764}
1765
Michael Wrightd02c5b62014-02-10 15:10:22 -08001766TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1767 // Configuration.
1768 InputReaderConfiguration config;
1769 mDevice->configure(ARBITRARY_TIME, &config, 0);
1770
1771 // Reset.
1772 mDevice->reset(ARBITRARY_TIME);
1773
1774 NotifyDeviceResetArgs resetArgs;
1775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1776 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1777 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1778
1779 // Metadata.
1780 ASSERT_TRUE(mDevice->isIgnored());
1781 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1782
1783 InputDeviceInfo info;
1784 mDevice->getDeviceInfo(&info);
1785 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001786 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001787 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1788 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1789
1790 // State queries.
1791 ASSERT_EQ(0, mDevice->getMetaState());
1792
1793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1794 << "Ignored device should return unknown key code state.";
1795 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1796 << "Ignored device should return unknown scan code state.";
1797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1798 << "Ignored device should return unknown switch state.";
1799
1800 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1801 uint8_t flags[2] = { 0, 1 };
1802 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1803 << "Ignored device should never mark any key codes.";
1804 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1805 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1806}
1807
1808TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1809 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 FakeInputMapper& mapper1 =
1813 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1815 mapper1.setMetaState(AMETA_ALT_ON);
1816 mapper1.addSupportedKeyCode(AKEYCODE_A);
1817 mapper1.addSupportedKeyCode(AKEYCODE_B);
1818 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1819 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1820 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
1821 mapper1.setScanCodeState(3, AKEY_STATE_UP);
1822 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001823
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001824 FakeInputMapper& mapper2 =
1825 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001826 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827
1828 InputReaderConfiguration config;
1829 mDevice->configure(ARBITRARY_TIME, &config, 0);
1830
1831 String8 propertyValue;
1832 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1833 << "Device should have read configuration during configuration phase.";
1834 ASSERT_STREQ("value", propertyValue.string());
1835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001836 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
1837 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001838
1839 // Reset
1840 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001841 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
1842 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001843
1844 NotifyDeviceResetArgs resetArgs;
1845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1846 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1847 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1848
1849 // Metadata.
1850 ASSERT_FALSE(mDevice->isIgnored());
1851 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1852
1853 InputDeviceInfo info;
1854 mDevice->getDeviceInfo(&info);
1855 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001856 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1858 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1859
1860 // State queries.
1861 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1862 << "Should query mappers and combine meta states.";
1863
1864 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1865 << "Should return unknown key code state when source not supported.";
1866 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1867 << "Should return unknown scan code state when source not supported.";
1868 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1869 << "Should return unknown switch state when source not supported.";
1870
1871 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1872 << "Should query mapper when source is supported.";
1873 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1874 << "Should query mapper when source is supported.";
1875 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1876 << "Should query mapper when source is supported.";
1877
1878 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1879 uint8_t flags[4] = { 0, 0, 0, 1 };
1880 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1881 << "Should do nothing when source is unsupported.";
1882 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1883 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1884 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1885 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1886
1887 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1888 << "Should query mapper when source is supported.";
1889 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1890 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1891 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1892 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1893
1894 // Event handling.
1895 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001896 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001897 mDevice->process(&event, 1);
1898
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001899 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
1900 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001901}
1902
Arthur Hung2c9a3342019-07-23 14:18:59 +08001903// A single input device is associated with a specific display. Check that:
1904// 1. Device is disabled if the viewport corresponding to the associated display is not found
1905// 2. Device is disabled when setEnabled API is called
1906TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001907 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08001908
1909 // First Configuration.
1910 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1911
1912 // Device should be enabled by default.
1913 ASSERT_TRUE(mDevice->isEnabled());
1914
1915 // Prepare associated info.
1916 constexpr uint8_t hdmi = 1;
1917 const std::string UNIQUE_ID = "local:1";
1918
1919 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1920 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1921 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1922 // Device should be disabled because it is associated with a specific display via
1923 // input port <-> display port association, but the corresponding display is not found
1924 ASSERT_FALSE(mDevice->isEnabled());
1925
1926 // Prepare displays.
1927 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1928 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1929 ViewportType::VIEWPORT_INTERNAL);
1930 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1931 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1932 ASSERT_TRUE(mDevice->isEnabled());
1933
1934 // Device should be disabled after set disable.
1935 mFakePolicy->addDisabledDevice(mDevice->getId());
1936 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1937 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1938 ASSERT_FALSE(mDevice->isEnabled());
1939
1940 // Device should still be disabled even found the associated display.
1941 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1942 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1943 ASSERT_FALSE(mDevice->isEnabled());
1944}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945
1946// --- InputMapperTest ---
1947
1948class InputMapperTest : public testing::Test {
1949protected:
1950 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001951 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001952 static const int32_t DEVICE_ID;
1953 static const int32_t DEVICE_GENERATION;
1954 static const int32_t DEVICE_CONTROLLER_NUMBER;
1955 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001956 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001957
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001958 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001959 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001960 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001961 FakeInputReaderContext* mFakeContext;
1962 InputDevice* mDevice;
1963
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001964 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001965 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001966 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001967 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001968 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1969 InputDeviceIdentifier identifier;
1970 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001971 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001972 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001973
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001974 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001975 }
1976
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001977 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
1978
Prabir Pradhan28efc192019-11-05 01:10:04 +00001979 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001980 delete mDevice;
1981 delete mFakeContext;
1982 mFakeListener.clear();
1983 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001984 }
1985
1986 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001987 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001988 }
1989
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001990 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001991 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1992 mFakeContext->updatePointerDisplay();
1993 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001994 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1995 }
1996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001997 template <class T, typename... Args>
1998 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001999 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002000 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002001 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002002 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002003 }
2004
2005 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002006 int32_t orientation, const std::string& uniqueId,
2007 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002008 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002009 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002010 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2011 }
2012
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002013 void clearViewports() {
2014 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002015 }
2016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002017 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2018 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002019 RawEvent event;
2020 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002021 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002022 event.type = type;
2023 event.code = code;
2024 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002025 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002026 }
2027
2028 static void assertMotionRange(const InputDeviceInfo& info,
2029 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2030 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002031 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2033 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2034 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2035 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2036 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2037 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2038 }
2039
2040 static void assertPointerCoords(const PointerCoords& coords,
2041 float x, float y, float pressure, float size,
2042 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2043 float orientation, float distance) {
2044 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2045 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2046 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2047 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2048 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2049 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2050 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2051 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2052 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2053 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2054 }
2055
2056 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2057 float actualX, actualY;
2058 controller->getPosition(&actualX, &actualY);
2059 ASSERT_NEAR(x, actualX, 1);
2060 ASSERT_NEAR(y, actualY, 1);
2061 }
2062};
2063
2064const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002065const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002066const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002067const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2068const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2069const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002070const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002071
2072// --- SwitchInputMapperTest ---
2073
2074class SwitchInputMapperTest : public InputMapperTest {
2075protected:
2076};
2077
2078TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002079 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002081 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002082}
2083
2084TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002085 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002086
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002087 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002088 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002089
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002090 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002091 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002092}
2093
2094TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002095 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002097 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2098 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2099 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2100 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002101
2102 NotifySwitchArgs args;
2103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2104 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002105 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2106 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002107 args.switchMask);
2108 ASSERT_EQ(uint32_t(0), args.policyFlags);
2109}
2110
2111
2112// --- KeyboardInputMapperTest ---
2113
2114class KeyboardInputMapperTest : public InputMapperTest {
2115protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002116 const std::string UNIQUE_ID = "local:0";
2117
2118 void prepareDisplay(int32_t orientation);
2119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002120 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002121 int32_t originalKeyCode, int32_t rotatedKeyCode,
2122 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002123};
2124
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002125/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2126 * orientation.
2127 */
2128void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2129 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002130 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002131}
2132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002133void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002134 int32_t originalScanCode, int32_t originalKeyCode,
2135 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002136 NotifyKeyArgs args;
2137
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002138 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2140 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2141 ASSERT_EQ(originalScanCode, args.scanCode);
2142 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002143 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002144
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002145 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2147 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2148 ASSERT_EQ(originalScanCode, args.scanCode);
2149 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002150 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002151}
2152
Michael Wrightd02c5b62014-02-10 15:10:22 -08002153TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002154 KeyboardInputMapper& mapper =
2155 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2156 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002158 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002159}
2160
2161TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2162 const int32_t USAGE_A = 0x070004;
2163 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002164 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2165 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002167 KeyboardInputMapper& mapper =
2168 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2169 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002170
2171 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002172 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002173 NotifyKeyArgs args;
2174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2175 ASSERT_EQ(DEVICE_ID, args.deviceId);
2176 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2177 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2178 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2179 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2180 ASSERT_EQ(KEY_HOME, args.scanCode);
2181 ASSERT_EQ(AMETA_NONE, args.metaState);
2182 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2183 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2184 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2185
2186 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002187 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2189 ASSERT_EQ(DEVICE_ID, args.deviceId);
2190 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2191 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2192 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2193 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2194 ASSERT_EQ(KEY_HOME, args.scanCode);
2195 ASSERT_EQ(AMETA_NONE, args.metaState);
2196 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2197 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2198 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2199
2200 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002201 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2202 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2204 ASSERT_EQ(DEVICE_ID, args.deviceId);
2205 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2206 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2207 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2208 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2209 ASSERT_EQ(0, args.scanCode);
2210 ASSERT_EQ(AMETA_NONE, args.metaState);
2211 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2212 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2213 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2214
2215 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002216 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2217 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2219 ASSERT_EQ(DEVICE_ID, args.deviceId);
2220 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2221 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2222 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2223 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2224 ASSERT_EQ(0, args.scanCode);
2225 ASSERT_EQ(AMETA_NONE, args.metaState);
2226 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2227 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2228 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2229
2230 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002231 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2232 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2234 ASSERT_EQ(DEVICE_ID, args.deviceId);
2235 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2236 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2237 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2238 ASSERT_EQ(0, args.keyCode);
2239 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2240 ASSERT_EQ(AMETA_NONE, args.metaState);
2241 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2242 ASSERT_EQ(0U, args.policyFlags);
2243 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2244
2245 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002246 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2247 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2249 ASSERT_EQ(DEVICE_ID, args.deviceId);
2250 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2251 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2252 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2253 ASSERT_EQ(0, args.keyCode);
2254 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2255 ASSERT_EQ(AMETA_NONE, args.metaState);
2256 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2257 ASSERT_EQ(0U, args.policyFlags);
2258 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2259}
2260
2261TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002262 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2263 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002265 KeyboardInputMapper& mapper =
2266 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2267 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002268
2269 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002270 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002271
2272 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002273 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274 NotifyKeyArgs args;
2275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002278 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2279
2280 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002281 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2283 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285
2286 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002287 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2289 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002291
2292 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002293 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2295 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002296 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002297 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2298}
2299
2300TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002301 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2302 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2303 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2304 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002306 KeyboardInputMapper& mapper =
2307 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2308 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002309
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002310 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002311 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2312 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2313 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2314 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2315 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2316 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2317 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2318 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2319}
2320
2321TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002322 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2323 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2324 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2325 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002326
Michael Wrightd02c5b62014-02-10 15:10:22 -08002327 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002328 KeyboardInputMapper& mapper =
2329 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2330 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002331
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002332 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002333 ASSERT_NO_FATAL_FAILURE(
2334 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2335 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2336 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2337 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2338 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2339 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2340 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002342 clearViewports();
2343 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002344 ASSERT_NO_FATAL_FAILURE(
2345 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2346 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2347 AKEYCODE_DPAD_UP, DISPLAY_ID));
2348 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2349 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2350 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2351 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002353 clearViewports();
2354 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002355 ASSERT_NO_FATAL_FAILURE(
2356 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2357 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2358 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2359 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2360 AKEYCODE_DPAD_UP, DISPLAY_ID));
2361 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2362 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002364 clearViewports();
2365 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002366 ASSERT_NO_FATAL_FAILURE(
2367 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2368 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2369 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2370 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2371 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2372 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2373 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002374
2375 // Special case: if orientation changes while key is down, we still emit the same keycode
2376 // in the key up as we did in the key down.
2377 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002378 clearViewports();
2379 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002380 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2382 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2383 ASSERT_EQ(KEY_UP, args.scanCode);
2384 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2385
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002386 clearViewports();
2387 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002388 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2390 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2391 ASSERT_EQ(KEY_UP, args.scanCode);
2392 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2393}
2394
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002395TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2396 // If the keyboard is not orientation aware,
2397 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002398 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002399
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.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002406 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002408 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2410 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2411
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002412 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002413 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002415 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2417 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2418}
2419
2420TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2421 // If the keyboard is orientation aware,
2422 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002423 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002424
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002425 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002426 KeyboardInputMapper& mapper =
2427 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2428 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002429 NotifyKeyArgs args;
2430
2431 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2432 // ^--- already checked by the previous test
2433
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002434 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002435 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002436 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002438 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2440 ASSERT_EQ(DISPLAY_ID, args.displayId);
2441
2442 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002443 clearViewports();
2444 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002445 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002446 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002448 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2450 ASSERT_EQ(newDisplayId, args.displayId);
2451}
2452
Michael Wrightd02c5b62014-02-10 15:10:22 -08002453TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002454 KeyboardInputMapper& mapper =
2455 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2456 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002458 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002459 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002461 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002462 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463}
2464
2465TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002466 KeyboardInputMapper& mapper =
2467 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2468 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002470 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002471 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002473 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002474 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475}
2476
2477TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002478 KeyboardInputMapper& mapper =
2479 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002482 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483
2484 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2485 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002486 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002487 ASSERT_TRUE(flags[0]);
2488 ASSERT_FALSE(flags[1]);
2489}
2490
2491TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002492 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2493 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2494 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2497 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002499 KeyboardInputMapper& mapper =
2500 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2501 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502
2503 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002504 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2505 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2506 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507
2508 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002509 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2510 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002511 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2512 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2513 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002514 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515
2516 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002517 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2518 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002519 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2520 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2521 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002522 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523
2524 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002525 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2526 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002527 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2528 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2529 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002530 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531
2532 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002533 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2534 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002535 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2536 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2537 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002538 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002539
2540 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002541 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2542 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002543 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2544 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2545 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002546 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002547
2548 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002549 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2550 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002551 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2552 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2553 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002554 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555}
2556
Arthur Hung2c9a3342019-07-23 14:18:59 +08002557TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2558 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2560 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002563
2564 // keyboard 2.
2565 const std::string USB2 = "USB2";
2566 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002567 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002568 InputDeviceIdentifier identifier;
2569 identifier.name = "KEYBOARD2";
2570 identifier.location = USB2;
2571 std::unique_ptr<InputDevice> device2 =
2572 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002573 identifier);
2574 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2575 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2576 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2577 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2578 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002580 KeyboardInputMapper& mapper =
2581 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2582 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002584 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002585 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002586 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002587 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2588 device2->reset(ARBITRARY_TIME);
2589
2590 // Prepared displays and associated info.
2591 constexpr uint8_t hdmi1 = 0;
2592 constexpr uint8_t hdmi2 = 1;
2593 const std::string SECONDARY_UNIQUE_ID = "local:1";
2594
2595 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2596 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2597
2598 // No associated display viewport found, should disable the device.
2599 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2600 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2601 ASSERT_FALSE(device2->isEnabled());
2602
2603 // Prepare second display.
2604 constexpr int32_t newDisplayId = 2;
2605 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2606 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2607 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2608 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2609 // Default device will reconfigure above, need additional reconfiguration for another device.
2610 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2611 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2612
2613 // Device should be enabled after the associated display is found.
2614 ASSERT_TRUE(mDevice->isEnabled());
2615 ASSERT_TRUE(device2->isEnabled());
2616
2617 // Test pad key events
2618 ASSERT_NO_FATAL_FAILURE(
2619 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2620 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2621 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2622 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2623 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2624 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2625 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2626
2627 ASSERT_NO_FATAL_FAILURE(
2628 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2629 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2630 AKEYCODE_DPAD_RIGHT, newDisplayId));
2631 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2632 AKEYCODE_DPAD_DOWN, newDisplayId));
2633 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2634 AKEYCODE_DPAD_LEFT, newDisplayId));
2635}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002637// --- KeyboardInputMapperTest_ExternalDevice ---
2638
2639class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2640protected:
2641 virtual void SetUp() override {
2642 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2643 }
2644};
2645
2646TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002647 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2648 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002649
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002650 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2651 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2652 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2653 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002654
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002655 KeyboardInputMapper& mapper =
2656 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2657 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002658
2659 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2660 NotifyKeyArgs args;
2661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2662 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2663
2664 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2666 ASSERT_EQ(uint32_t(0), args.policyFlags);
2667
2668 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2670 ASSERT_EQ(uint32_t(0), args.policyFlags);
2671
2672 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2674 ASSERT_EQ(uint32_t(0), args.policyFlags);
2675
2676 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2678 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2679
2680 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2682 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2683}
2684
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002685TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002686 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002687
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002688 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2689 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002691
Powei Fengd041c5d2019-05-03 17:11:33 -07002692 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002693 KeyboardInputMapper& mapper =
2694 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2695 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002696
2697 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2698 NotifyKeyArgs args;
2699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2700 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2701
2702 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2704 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2705
2706 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2708 ASSERT_EQ(uint32_t(0), args.policyFlags);
2709
2710 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2712 ASSERT_EQ(uint32_t(0), args.policyFlags);
2713
2714 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2716 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2717
2718 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2720 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2721}
2722
Michael Wrightd02c5b62014-02-10 15:10:22 -08002723// --- CursorInputMapperTest ---
2724
2725class CursorInputMapperTest : public InputMapperTest {
2726protected:
2727 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2728
2729 sp<FakePointerController> mFakePointerController;
2730
Prabir Pradhan28efc192019-11-05 01:10:04 +00002731 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732 InputMapperTest::SetUp();
2733
2734 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002735 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002736 }
2737
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002738 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
2739 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002740
2741 void prepareDisplay(int32_t orientation) {
2742 const std::string uniqueId = "local:0";
2743 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2744 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2745 orientation, uniqueId, NO_PORT, viewportType);
2746 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747};
2748
2749const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002751void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
2752 int32_t originalY, int32_t rotatedX,
2753 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754 NotifyMotionArgs args;
2755
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002756 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2757 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2758 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2762 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2763 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2764 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2765}
2766
2767TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002771 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772}
2773
2774TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002776 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002778 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779}
2780
2781TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002783 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
2785 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002786 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787
2788 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002789 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2790 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2792 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2793
2794 // When the bounds are set, then there should be a valid motion range.
2795 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2796
2797 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002798 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799
2800 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2801 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2802 1, 800 - 1, 0.0f, 0.0f));
2803 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2804 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2805 2, 480 - 1, 0.0f, 0.0f));
2806 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2807 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2808 0.0f, 1.0f, 0.0f, 0.0f));
2809}
2810
2811TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002813 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814
2815 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002816 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817
2818 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2819 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2820 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2821 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2822 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2823 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2824 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2825 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2826 0.0f, 1.0f, 0.0f, 0.0f));
2827}
2828
2829TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002831 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832
2833 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2834
2835 NotifyMotionArgs args;
2836
2837 // Button press.
2838 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002839 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2840 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2842 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2843 ASSERT_EQ(DEVICE_ID, args.deviceId);
2844 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2845 ASSERT_EQ(uint32_t(0), args.policyFlags);
2846 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2847 ASSERT_EQ(0, args.flags);
2848 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2849 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2850 ASSERT_EQ(0, args.edgeFlags);
2851 ASSERT_EQ(uint32_t(1), args.pointerCount);
2852 ASSERT_EQ(0, args.pointerProperties[0].id);
2853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2855 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2856 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2857 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2858 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2859
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2861 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2862 ASSERT_EQ(DEVICE_ID, args.deviceId);
2863 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2864 ASSERT_EQ(uint32_t(0), args.policyFlags);
2865 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2866 ASSERT_EQ(0, args.flags);
2867 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2868 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2869 ASSERT_EQ(0, args.edgeFlags);
2870 ASSERT_EQ(uint32_t(1), args.pointerCount);
2871 ASSERT_EQ(0, args.pointerProperties[0].id);
2872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2874 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2875 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2876 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2877 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2878
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002880 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2881 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2883 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2884 ASSERT_EQ(DEVICE_ID, args.deviceId);
2885 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2886 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2888 ASSERT_EQ(0, args.flags);
2889 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2890 ASSERT_EQ(0, args.buttonState);
2891 ASSERT_EQ(0, args.edgeFlags);
2892 ASSERT_EQ(uint32_t(1), args.pointerCount);
2893 ASSERT_EQ(0, args.pointerProperties[0].id);
2894 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2896 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2897 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2898 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2899 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2900
2901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2902 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2903 ASSERT_EQ(DEVICE_ID, args.deviceId);
2904 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2905 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002906 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2907 ASSERT_EQ(0, args.flags);
2908 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2909 ASSERT_EQ(0, args.buttonState);
2910 ASSERT_EQ(0, args.edgeFlags);
2911 ASSERT_EQ(uint32_t(1), args.pointerCount);
2912 ASSERT_EQ(0, args.pointerProperties[0].id);
2913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2915 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2916 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2917 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2918 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2919}
2920
2921TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002923 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924
2925 NotifyMotionArgs args;
2926
2927 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002928 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2929 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2933 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2934
2935 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002936 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2937 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2941 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2942}
2943
2944TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002946 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947
2948 NotifyMotionArgs args;
2949
2950 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002951 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2952 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2954 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2956 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2957
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2961 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2962
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002964 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2965 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002967 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2969 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2970
2971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2974 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2975}
2976
2977TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002979 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980
2981 NotifyMotionArgs args;
2982
2983 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002984 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2985 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2986 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2987 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2991 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2992 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2993
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2997 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2998 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2999
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003001 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3002 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3003 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3007 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3008 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3009
3010 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003011 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3012 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003014 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3016 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3017
3018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3021 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3022}
3023
3024TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003026 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027
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, 0, 1));
3030 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3031 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3032 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3033 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3034 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3035 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3036 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3037}
3038
3039TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040 addConfigurationProperty("cursor.mode", "navigation");
3041 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003042 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003044 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3046 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3047 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3048 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3049 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3050 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3051 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3052 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3053
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003054 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3056 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3057 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3058 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3059 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3060 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3061 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3062 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3063
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003064 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3066 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3067 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3068 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3069 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3070 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3071 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3072 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3073
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003074 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3076 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3077 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3078 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3079 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3081 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3082 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3083}
3084
3085TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088
3089 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3090 mFakePointerController->setPosition(100, 200);
3091 mFakePointerController->setButtonState(0);
3092
3093 NotifyMotionArgs motionArgs;
3094 NotifyKeyArgs keyArgs;
3095
3096 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003097 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3098 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3101 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3102 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3104 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3105
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3108 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3109 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3111 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3112
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003113 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3114 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117 ASSERT_EQ(0, motionArgs.buttonState);
3118 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3120 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3121
3122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003123 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 ASSERT_EQ(0, motionArgs.buttonState);
3125 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3127 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3128
3129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003131 ASSERT_EQ(0, motionArgs.buttonState);
3132 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3134 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3135
3136 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003137 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3138 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3139 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3142 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3143 motionArgs.buttonState);
3144 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3145 mFakePointerController->getButtonState());
3146 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
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3151 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3152 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3153 mFakePointerController->getButtonState());
3154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3155 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3156
3157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3158 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3159 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3160 motionArgs.buttonState);
3161 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3162 mFakePointerController->getButtonState());
3163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3164 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3165
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003166 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3167 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003169 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3171 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3173 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3174
3175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003177 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3178 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3180 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3181
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003182 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3183 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3186 ASSERT_EQ(0, motionArgs.buttonState);
3187 ASSERT_EQ(0, mFakePointerController->getButtonState());
3188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3189 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 -08003190 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3191 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003192
3193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194 ASSERT_EQ(0, motionArgs.buttonState);
3195 ASSERT_EQ(0, mFakePointerController->getButtonState());
3196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3198 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 -08003199
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3201 ASSERT_EQ(0, motionArgs.buttonState);
3202 ASSERT_EQ(0, mFakePointerController->getButtonState());
3203 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3205 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3206
3207 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003208 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3209 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3211 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3212 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003213
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003215 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3217 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3219 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3220
3221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3222 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3223 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3224 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3226 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3227
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003228 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3229 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232 ASSERT_EQ(0, motionArgs.buttonState);
3233 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3235 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3236
3237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003239 ASSERT_EQ(0, motionArgs.buttonState);
3240 ASSERT_EQ(0, mFakePointerController->getButtonState());
3241
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3243 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3245 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3246 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3247
3248 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003249 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3250 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3252 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3253 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003254
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3258 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3260 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3261
3262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3263 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3264 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3265 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3267 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3268
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003269 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3270 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 ASSERT_EQ(0, motionArgs.buttonState);
3274 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3276 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003277
3278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3279 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3280 ASSERT_EQ(0, motionArgs.buttonState);
3281 ASSERT_EQ(0, mFakePointerController->getButtonState());
3282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3283 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3284
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3286 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3287 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3288
3289 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003290 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3291 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3293 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3294 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003295
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003297 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3299 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3301 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3302
3303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3305 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3306 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3308 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3309
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003310 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3311 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_EQ(0, motionArgs.buttonState);
3315 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3317 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 -08003318
3319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3320 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3321 ASSERT_EQ(0, motionArgs.buttonState);
3322 ASSERT_EQ(0, mFakePointerController->getButtonState());
3323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3324 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3325
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3327 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3328 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3329
3330 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003331 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3332 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3334 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3335 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003336
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3340 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3342 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3343
3344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3346 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3347 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3349 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3350
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003351 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3352 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_EQ(0, motionArgs.buttonState);
3356 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3358 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 -08003359
3360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3362 ASSERT_EQ(0, motionArgs.buttonState);
3363 ASSERT_EQ(0, mFakePointerController->getButtonState());
3364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3365 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3366
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3368 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3369 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3370}
3371
3372TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003374 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375
3376 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3377 mFakePointerController->setPosition(100, 200);
3378 mFakePointerController->setButtonState(0);
3379
3380 NotifyMotionArgs args;
3381
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003382 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3383 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3384 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003386 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3389 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3390 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3391}
3392
3393TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003394 addConfigurationProperty("cursor.mode", "pointer");
3395 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003396 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003397
3398 NotifyDeviceResetArgs resetArgs;
3399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3400 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3401 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3402
3403 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3404 mFakePointerController->setPosition(100, 200);
3405 mFakePointerController->setButtonState(0);
3406
3407 NotifyMotionArgs args;
3408
3409 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003410 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3411 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3412 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3414 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3417 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3418 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3419
3420 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003421 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3422 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3424 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3425 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3427 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3429 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3432 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3433
3434 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003435 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3436 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3438 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3441 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3443 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3446 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3447
3448 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003449 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3450 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3451 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3453 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3456 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3457 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3458
3459 // Disable pointer capture and check that the device generation got bumped
3460 // and events are generated the usual way.
3461 const uint32_t generation = mFakeContext->getGeneration();
3462 mFakePolicy->setPointerCapture(false);
3463 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3464 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3465
3466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3467 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3468 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3469
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003470 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3471 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3472 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3474 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3477 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3478 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3479}
3480
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003481TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003483
Garfield Tan888a6a42020-01-09 11:39:16 -08003484 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003485 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003486 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3487 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3488 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3489 ViewportType::VIEWPORT_EXTERNAL);
3490 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3491 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3492
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003493 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3494 mFakePointerController->setPosition(100, 200);
3495 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003496
3497 NotifyMotionArgs args;
3498 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3499 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3500 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3502 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3503 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3505 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3506 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3507 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3508}
3509
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510// --- TouchInputMapperTest ---
3511
3512class TouchInputMapperTest : public InputMapperTest {
3513protected:
3514 static const int32_t RAW_X_MIN;
3515 static const int32_t RAW_X_MAX;
3516 static const int32_t RAW_Y_MIN;
3517 static const int32_t RAW_Y_MAX;
3518 static const int32_t RAW_TOUCH_MIN;
3519 static const int32_t RAW_TOUCH_MAX;
3520 static const int32_t RAW_TOOL_MIN;
3521 static const int32_t RAW_TOOL_MAX;
3522 static const int32_t RAW_PRESSURE_MIN;
3523 static const int32_t RAW_PRESSURE_MAX;
3524 static const int32_t RAW_ORIENTATION_MIN;
3525 static const int32_t RAW_ORIENTATION_MAX;
3526 static const int32_t RAW_DISTANCE_MIN;
3527 static const int32_t RAW_DISTANCE_MAX;
3528 static const int32_t RAW_TILT_MIN;
3529 static const int32_t RAW_TILT_MAX;
3530 static const int32_t RAW_ID_MIN;
3531 static const int32_t RAW_ID_MAX;
3532 static const int32_t RAW_SLOT_MIN;
3533 static const int32_t RAW_SLOT_MAX;
3534 static const float X_PRECISION;
3535 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003536 static const float X_PRECISION_VIRTUAL;
3537 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538
3539 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003540 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
3542 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3543
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003544 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003545 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003546
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547 enum Axes {
3548 POSITION = 1 << 0,
3549 TOUCH = 1 << 1,
3550 TOOL = 1 << 2,
3551 PRESSURE = 1 << 3,
3552 ORIENTATION = 1 << 4,
3553 MINOR = 1 << 5,
3554 ID = 1 << 6,
3555 DISTANCE = 1 << 7,
3556 TILT = 1 << 8,
3557 SLOT = 1 << 9,
3558 TOOL_TYPE = 1 << 10,
3559 };
3560
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003561 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3562 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003563 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003565 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 int32_t toRawX(float displayX);
3567 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003568 float toCookedX(float rawX, float rawY);
3569 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003571 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003573 float toDisplayY(int32_t rawY, int32_t displayHeight);
3574
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575};
3576
3577const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3578const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3579const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3580const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3581const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3582const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3583const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3584const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003585const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3586const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3588const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3589const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3590const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3591const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3592const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3593const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3594const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3595const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3596const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3597const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3598const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003599const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3600 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3601const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3602 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003603const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3604 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605
3606const float TouchInputMapperTest::GEOMETRIC_SCALE =
3607 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3608 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3609
3610const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3611 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3612 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3613};
3614
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003615void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003616 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003617 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3618}
3619
3620void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3621 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3622 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623}
3624
Santos Cordonfa5cf462017-04-05 10:37:00 -07003625void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003626 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3627 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003628 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003629}
3630
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003632 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3633 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636}
3637
Jason Gerecke489fda82012-09-07 17:19:40 -07003638void TouchInputMapperTest::prepareLocationCalibration() {
3639 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3640}
3641
Michael Wrightd02c5b62014-02-10 15:10:22 -08003642int32_t TouchInputMapperTest::toRawX(float displayX) {
3643 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3644}
3645
3646int32_t TouchInputMapperTest::toRawY(float displayY) {
3647 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3648}
3649
Jason Gerecke489fda82012-09-07 17:19:40 -07003650float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3651 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3652 return rawX;
3653}
3654
3655float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3656 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3657 return rawY;
3658}
3659
Michael Wrightd02c5b62014-02-10 15:10:22 -08003660float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003661 return toDisplayX(rawX, DISPLAY_WIDTH);
3662}
3663
3664float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3665 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666}
3667
3668float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003669 return toDisplayY(rawY, DISPLAY_HEIGHT);
3670}
3671
3672float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3673 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674}
3675
3676
3677// --- SingleTouchInputMapperTest ---
3678
3679class SingleTouchInputMapperTest : public TouchInputMapperTest {
3680protected:
3681 void prepareButtons();
3682 void prepareAxes(int axes);
3683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003684 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3685 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3686 void processUp(SingleTouchInputMapper& mappery);
3687 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3688 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3689 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3690 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3691 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3692 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693};
3694
3695void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003696 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697}
3698
3699void SingleTouchInputMapperTest::prepareAxes(int axes) {
3700 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003701 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3702 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703 }
3704 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003705 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
3706 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707 }
3708 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003709 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
3710 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711 }
3712 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003713 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
3714 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715 }
3716 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003717 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3718 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719 }
3720}
3721
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003722void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003723 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3724 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3725 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726}
3727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003729 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3730 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731}
3732
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003733void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003734 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735}
3736
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003738 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739}
3740
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003741void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
3742 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003743 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744}
3745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003747 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748}
3749
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003750void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
3751 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003752 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3753 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754}
3755
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003756void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
3757 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003758 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759}
3760
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003762 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003763}
3764
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766 prepareButtons();
3767 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003768 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003770 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771}
3772
3773TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003774 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
3775 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776 prepareButtons();
3777 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003778 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003780 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781}
3782
3783TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784 prepareButtons();
3785 prepareAxes(POSITION);
3786 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790}
3791
3792TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793 prepareButtons();
3794 prepareAxes(POSITION);
3795 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003798 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799}
3800
3801TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802 addConfigurationProperty("touch.deviceType", "touchScreen");
3803 prepareDisplay(DISPLAY_ORIENTATION_0);
3804 prepareButtons();
3805 prepareAxes(POSITION);
3806 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
3809 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811
3812 // Virtual key is down.
3813 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3814 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3815 processDown(mapper, x, y);
3816 processSync(mapper);
3817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003819 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003820
3821 // Virtual key is up.
3822 processUp(mapper);
3823 processSync(mapper);
3824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003826 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827}
3828
3829TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830 addConfigurationProperty("touch.deviceType", "touchScreen");
3831 prepareDisplay(DISPLAY_ORIENTATION_0);
3832 prepareButtons();
3833 prepareAxes(POSITION);
3834 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003835 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836
3837 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003838 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839
3840 // Virtual key is down.
3841 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3842 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3843 processDown(mapper, x, y);
3844 processSync(mapper);
3845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3846
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003847 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848
3849 // Virtual key is up.
3850 processUp(mapper);
3851 processSync(mapper);
3852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003854 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855}
3856
3857TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 addConfigurationProperty("touch.deviceType", "touchScreen");
3859 prepareDisplay(DISPLAY_ORIENTATION_0);
3860 prepareButtons();
3861 prepareAxes(POSITION);
3862 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003863 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864
3865 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3866 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003867 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868 ASSERT_TRUE(flags[0]);
3869 ASSERT_FALSE(flags[1]);
3870}
3871
3872TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873 addConfigurationProperty("touch.deviceType", "touchScreen");
3874 prepareDisplay(DISPLAY_ORIENTATION_0);
3875 prepareButtons();
3876 prepareAxes(POSITION);
3877 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003878 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879
3880 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3881
3882 NotifyKeyArgs args;
3883
3884 // Press virtual key.
3885 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3886 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3887 processDown(mapper, x, y);
3888 processSync(mapper);
3889
3890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3891 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3892 ASSERT_EQ(DEVICE_ID, args.deviceId);
3893 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3894 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3895 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3896 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3897 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3898 ASSERT_EQ(KEY_HOME, args.scanCode);
3899 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3900 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3901
3902 // Release virtual key.
3903 processUp(mapper);
3904 processSync(mapper);
3905
3906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3907 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3908 ASSERT_EQ(DEVICE_ID, args.deviceId);
3909 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3910 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3911 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3912 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3913 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3914 ASSERT_EQ(KEY_HOME, args.scanCode);
3915 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3916 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3917
3918 // Should not have sent any motions.
3919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3920}
3921
3922TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923 addConfigurationProperty("touch.deviceType", "touchScreen");
3924 prepareDisplay(DISPLAY_ORIENTATION_0);
3925 prepareButtons();
3926 prepareAxes(POSITION);
3927 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003928 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
3930 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3931
3932 NotifyKeyArgs keyArgs;
3933
3934 // Press virtual key.
3935 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3936 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3937 processDown(mapper, x, y);
3938 processSync(mapper);
3939
3940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3941 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3942 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3943 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3944 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3945 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3946 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3947 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3948 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3949 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3950 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3951
3952 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3953 // into the display area.
3954 y -= 100;
3955 processMove(mapper, x, y);
3956 processSync(mapper);
3957
3958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3959 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3960 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3961 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3962 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3963 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3964 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3965 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3966 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3967 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3968 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3969 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3970
3971 NotifyMotionArgs motionArgs;
3972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3973 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3974 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3975 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3976 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3978 ASSERT_EQ(0, motionArgs.flags);
3979 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3980 ASSERT_EQ(0, motionArgs.buttonState);
3981 ASSERT_EQ(0, motionArgs.edgeFlags);
3982 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3983 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3986 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3987 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3988 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3989 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3990
3991 // Keep moving out of bounds. Should generate a pointer move.
3992 y -= 50;
3993 processMove(mapper, x, y);
3994 processSync(mapper);
3995
3996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3997 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3998 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3999 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4000 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4002 ASSERT_EQ(0, motionArgs.flags);
4003 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4004 ASSERT_EQ(0, motionArgs.buttonState);
4005 ASSERT_EQ(0, motionArgs.edgeFlags);
4006 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4007 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4010 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4011 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4012 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4013 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4014
4015 // Release out of bounds. Should generate a pointer up.
4016 processUp(mapper);
4017 processSync(mapper);
4018
4019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4020 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4021 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4022 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4023 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4024 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4025 ASSERT_EQ(0, motionArgs.flags);
4026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4027 ASSERT_EQ(0, motionArgs.buttonState);
4028 ASSERT_EQ(0, motionArgs.edgeFlags);
4029 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4030 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4033 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4034 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4035 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4036 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4037
4038 // Should not have sent any more keys or motions.
4039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4041}
4042
4043TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 addConfigurationProperty("touch.deviceType", "touchScreen");
4045 prepareDisplay(DISPLAY_ORIENTATION_0);
4046 prepareButtons();
4047 prepareAxes(POSITION);
4048 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004049 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050
4051 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4052
4053 NotifyMotionArgs motionArgs;
4054
4055 // Initially go down out of bounds.
4056 int32_t x = -10;
4057 int32_t y = -10;
4058 processDown(mapper, x, y);
4059 processSync(mapper);
4060
4061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4062
4063 // Move into the display area. Should generate a pointer down.
4064 x = 50;
4065 y = 75;
4066 processMove(mapper, x, y);
4067 processSync(mapper);
4068
4069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4070 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4071 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4072 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4073 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4074 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4075 ASSERT_EQ(0, motionArgs.flags);
4076 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4077 ASSERT_EQ(0, motionArgs.buttonState);
4078 ASSERT_EQ(0, motionArgs.edgeFlags);
4079 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4080 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4083 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4084 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4085 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4086 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4087
4088 // Release. Should generate a pointer up.
4089 processUp(mapper);
4090 processSync(mapper);
4091
4092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4094 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4095 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4096 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4097 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4098 ASSERT_EQ(0, motionArgs.flags);
4099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4100 ASSERT_EQ(0, motionArgs.buttonState);
4101 ASSERT_EQ(0, motionArgs.edgeFlags);
4102 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4103 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4106 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4107 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4108 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4109 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4110
4111 // Should not have sent any more keys or motions.
4112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4114}
4115
Santos Cordonfa5cf462017-04-05 10:37:00 -07004116TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004117 addConfigurationProperty("touch.deviceType", "touchScreen");
4118 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4119
4120 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4121 prepareButtons();
4122 prepareAxes(POSITION);
4123 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004124 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004125
4126 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4127
4128 NotifyMotionArgs motionArgs;
4129
4130 // Down.
4131 int32_t x = 100;
4132 int32_t y = 125;
4133 processDown(mapper, x, y);
4134 processSync(mapper);
4135
4136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4138 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4139 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4140 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4141 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4142 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4143 ASSERT_EQ(0, motionArgs.flags);
4144 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4145 ASSERT_EQ(0, motionArgs.buttonState);
4146 ASSERT_EQ(0, motionArgs.edgeFlags);
4147 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4148 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4151 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4152 1, 0, 0, 0, 0, 0, 0, 0));
4153 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4154 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4155 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4156
4157 // Move.
4158 x += 50;
4159 y += 75;
4160 processMove(mapper, x, y);
4161 processSync(mapper);
4162
4163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4164 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4165 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4166 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4167 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4168 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4170 ASSERT_EQ(0, motionArgs.flags);
4171 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4172 ASSERT_EQ(0, motionArgs.buttonState);
4173 ASSERT_EQ(0, motionArgs.edgeFlags);
4174 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4178 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4179 1, 0, 0, 0, 0, 0, 0, 0));
4180 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4181 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4182 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4183
4184 // Up.
4185 processUp(mapper);
4186 processSync(mapper);
4187
4188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4189 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4190 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4191 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4192 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4193 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4194 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4195 ASSERT_EQ(0, motionArgs.flags);
4196 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4197 ASSERT_EQ(0, motionArgs.buttonState);
4198 ASSERT_EQ(0, motionArgs.edgeFlags);
4199 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4200 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4203 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4204 1, 0, 0, 0, 0, 0, 0, 0));
4205 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4206 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4207 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4208
4209 // Should not have sent any more keys or motions.
4210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4212}
4213
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 addConfigurationProperty("touch.deviceType", "touchScreen");
4216 prepareDisplay(DISPLAY_ORIENTATION_0);
4217 prepareButtons();
4218 prepareAxes(POSITION);
4219 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004220 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
4222 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4223
4224 NotifyMotionArgs motionArgs;
4225
4226 // Down.
4227 int32_t x = 100;
4228 int32_t y = 125;
4229 processDown(mapper, x, y);
4230 processSync(mapper);
4231
4232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4234 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4235 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4236 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4237 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4238 ASSERT_EQ(0, motionArgs.flags);
4239 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4240 ASSERT_EQ(0, motionArgs.buttonState);
4241 ASSERT_EQ(0, motionArgs.edgeFlags);
4242 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4243 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4246 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4247 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4248 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4250
4251 // Move.
4252 x += 50;
4253 y += 75;
4254 processMove(mapper, x, y);
4255 processSync(mapper);
4256
4257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4259 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4261 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4263 ASSERT_EQ(0, motionArgs.flags);
4264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4265 ASSERT_EQ(0, motionArgs.buttonState);
4266 ASSERT_EQ(0, motionArgs.edgeFlags);
4267 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4271 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4272 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4273 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4275
4276 // Up.
4277 processUp(mapper);
4278 processSync(mapper);
4279
4280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4281 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4282 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4283 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4284 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4285 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4286 ASSERT_EQ(0, motionArgs.flags);
4287 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4288 ASSERT_EQ(0, motionArgs.buttonState);
4289 ASSERT_EQ(0, motionArgs.edgeFlags);
4290 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4291 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4294 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4295 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4296 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4298
4299 // Should not have sent any more keys or motions.
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4302}
4303
4304TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 addConfigurationProperty("touch.deviceType", "touchScreen");
4306 prepareButtons();
4307 prepareAxes(POSITION);
4308 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004309 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310
4311 NotifyMotionArgs args;
4312
4313 // Rotation 90.
4314 prepareDisplay(DISPLAY_ORIENTATION_90);
4315 processDown(mapper, toRawX(50), toRawY(75));
4316 processSync(mapper);
4317
4318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4319 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4320 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4321
4322 processUp(mapper);
4323 processSync(mapper);
4324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4325}
4326
4327TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 addConfigurationProperty("touch.deviceType", "touchScreen");
4329 prepareButtons();
4330 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004331 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333 NotifyMotionArgs args;
4334
4335 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004336 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 prepareDisplay(DISPLAY_ORIENTATION_0);
4338 processDown(mapper, toRawX(50), toRawY(75));
4339 processSync(mapper);
4340
4341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4342 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4343 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4344
4345 processUp(mapper);
4346 processSync(mapper);
4347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4348
4349 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004350 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 prepareDisplay(DISPLAY_ORIENTATION_90);
4352 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4353 processSync(mapper);
4354
4355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4356 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4357 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4358
4359 processUp(mapper);
4360 processSync(mapper);
4361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4362
4363 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004364 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 prepareDisplay(DISPLAY_ORIENTATION_180);
4366 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4367 processSync(mapper);
4368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4370 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4371 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4372
4373 processUp(mapper);
4374 processSync(mapper);
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4376
4377 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004378 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 prepareDisplay(DISPLAY_ORIENTATION_270);
4380 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4381 processSync(mapper);
4382
4383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4384 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4385 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4386
4387 processUp(mapper);
4388 processSync(mapper);
4389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4390}
4391
4392TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 addConfigurationProperty("touch.deviceType", "touchScreen");
4394 prepareDisplay(DISPLAY_ORIENTATION_0);
4395 prepareButtons();
4396 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004397 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398
4399 // These calculations are based on the input device calibration documentation.
4400 int32_t rawX = 100;
4401 int32_t rawY = 200;
4402 int32_t rawPressure = 10;
4403 int32_t rawToolMajor = 12;
4404 int32_t rawDistance = 2;
4405 int32_t rawTiltX = 30;
4406 int32_t rawTiltY = 110;
4407
4408 float x = toDisplayX(rawX);
4409 float y = toDisplayY(rawY);
4410 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4411 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4412 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4413 float distance = float(rawDistance);
4414
4415 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4416 float tiltScale = M_PI / 180;
4417 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4418 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4419 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4420 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4421
4422 processDown(mapper, rawX, rawY);
4423 processPressure(mapper, rawPressure);
4424 processToolMajor(mapper, rawToolMajor);
4425 processDistance(mapper, rawDistance);
4426 processTilt(mapper, rawTiltX, rawTiltY);
4427 processSync(mapper);
4428
4429 NotifyMotionArgs args;
4430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4432 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4433 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4434}
4435
Jason Gerecke489fda82012-09-07 17:19:40 -07004436TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004437 addConfigurationProperty("touch.deviceType", "touchScreen");
4438 prepareDisplay(DISPLAY_ORIENTATION_0);
4439 prepareLocationCalibration();
4440 prepareButtons();
4441 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004442 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004443
4444 int32_t rawX = 100;
4445 int32_t rawY = 200;
4446
4447 float x = toDisplayX(toCookedX(rawX, rawY));
4448 float y = toDisplayY(toCookedY(rawX, rawY));
4449
4450 processDown(mapper, rawX, rawY);
4451 processSync(mapper);
4452
4453 NotifyMotionArgs args;
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4456 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4457}
4458
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 addConfigurationProperty("touch.deviceType", "touchScreen");
4461 prepareDisplay(DISPLAY_ORIENTATION_0);
4462 prepareButtons();
4463 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004464 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465
4466 NotifyMotionArgs motionArgs;
4467 NotifyKeyArgs keyArgs;
4468
4469 processDown(mapper, 100, 200);
4470 processSync(mapper);
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4472 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4473 ASSERT_EQ(0, motionArgs.buttonState);
4474
4475 // press BTN_LEFT, release BTN_LEFT
4476 processKey(mapper, BTN_LEFT, 1);
4477 processSync(mapper);
4478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4480 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4481
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4484 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4485
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 processKey(mapper, BTN_LEFT, 0);
4487 processSync(mapper);
4488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004491
4492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004494 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495
4496 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4497 processKey(mapper, BTN_RIGHT, 1);
4498 processKey(mapper, BTN_MIDDLE, 1);
4499 processSync(mapper);
4500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4502 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4503 motionArgs.buttonState);
4504
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4507 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4508
4509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4511 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4512 motionArgs.buttonState);
4513
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 processKey(mapper, BTN_RIGHT, 0);
4515 processSync(mapper);
4516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004519
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004522 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523
4524 processKey(mapper, BTN_MIDDLE, 0);
4525 processSync(mapper);
4526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004527 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004529
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004532 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
4534 // press BTN_BACK, release BTN_BACK
4535 processKey(mapper, BTN_BACK, 1);
4536 processSync(mapper);
4537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4538 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4539 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004540
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004543 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4544
4545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4546 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4547 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548
4549 processKey(mapper, BTN_BACK, 0);
4550 processSync(mapper);
4551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554
4555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004557 ASSERT_EQ(0, motionArgs.buttonState);
4558
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4560 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4561 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4562
4563 // press BTN_SIDE, release BTN_SIDE
4564 processKey(mapper, BTN_SIDE, 1);
4565 processSync(mapper);
4566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4567 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4568 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004569
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004572 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4573
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4576 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577
4578 processKey(mapper, BTN_SIDE, 0);
4579 processSync(mapper);
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004583
4584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586 ASSERT_EQ(0, motionArgs.buttonState);
4587
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4589 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4590 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4591
4592 // press BTN_FORWARD, release BTN_FORWARD
4593 processKey(mapper, BTN_FORWARD, 1);
4594 processSync(mapper);
4595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4596 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4597 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004598
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4602
4603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4605 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606
4607 processKey(mapper, BTN_FORWARD, 0);
4608 processSync(mapper);
4609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004610 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615 ASSERT_EQ(0, motionArgs.buttonState);
4616
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4618 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4619 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4620
4621 // press BTN_EXTRA, release BTN_EXTRA
4622 processKey(mapper, BTN_EXTRA, 1);
4623 processSync(mapper);
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4625 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4626 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4631
4632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4634 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635
4636 processKey(mapper, BTN_EXTRA, 0);
4637 processSync(mapper);
4638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004644 ASSERT_EQ(0, motionArgs.buttonState);
4645
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4647 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4648 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4649
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4651
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 // press BTN_STYLUS, release BTN_STYLUS
4653 processKey(mapper, BTN_STYLUS, 1);
4654 processSync(mapper);
4655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004657 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4658
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4661 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662
4663 processKey(mapper, BTN_STYLUS, 0);
4664 processSync(mapper);
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004668
4669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004671 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672
4673 // press BTN_STYLUS2, release BTN_STYLUS2
4674 processKey(mapper, BTN_STYLUS2, 1);
4675 processSync(mapper);
4676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004678 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4679
4680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4682 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683
4684 processKey(mapper, BTN_STYLUS2, 0);
4685 processSync(mapper);
4686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004689
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004692 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693
4694 // release touch
4695 processUp(mapper);
4696 processSync(mapper);
4697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4699 ASSERT_EQ(0, motionArgs.buttonState);
4700}
4701
4702TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 addConfigurationProperty("touch.deviceType", "touchScreen");
4704 prepareDisplay(DISPLAY_ORIENTATION_0);
4705 prepareButtons();
4706 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004707 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708
4709 NotifyMotionArgs motionArgs;
4710
4711 // default tool type is finger
4712 processDown(mapper, 100, 200);
4713 processSync(mapper);
4714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4715 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4717
4718 // eraser
4719 processKey(mapper, BTN_TOOL_RUBBER, 1);
4720 processSync(mapper);
4721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4724
4725 // stylus
4726 processKey(mapper, BTN_TOOL_RUBBER, 0);
4727 processKey(mapper, BTN_TOOL_PEN, 1);
4728 processSync(mapper);
4729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4732
4733 // brush
4734 processKey(mapper, BTN_TOOL_PEN, 0);
4735 processKey(mapper, BTN_TOOL_BRUSH, 1);
4736 processSync(mapper);
4737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4738 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4740
4741 // pencil
4742 processKey(mapper, BTN_TOOL_BRUSH, 0);
4743 processKey(mapper, BTN_TOOL_PENCIL, 1);
4744 processSync(mapper);
4745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4748
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004749 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750 processKey(mapper, BTN_TOOL_PENCIL, 0);
4751 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4752 processSync(mapper);
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4756
4757 // mouse
4758 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4759 processKey(mapper, BTN_TOOL_MOUSE, 1);
4760 processSync(mapper);
4761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4764
4765 // lens
4766 processKey(mapper, BTN_TOOL_MOUSE, 0);
4767 processKey(mapper, BTN_TOOL_LENS, 1);
4768 processSync(mapper);
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4772
4773 // double-tap
4774 processKey(mapper, BTN_TOOL_LENS, 0);
4775 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4776 processSync(mapper);
4777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4780
4781 // triple-tap
4782 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4783 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4784 processSync(mapper);
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4788
4789 // quad-tap
4790 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4791 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4792 processSync(mapper);
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4796
4797 // finger
4798 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4799 processKey(mapper, BTN_TOOL_FINGER, 1);
4800 processSync(mapper);
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4804
4805 // stylus trumps finger
4806 processKey(mapper, BTN_TOOL_PEN, 1);
4807 processSync(mapper);
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4811
4812 // eraser trumps stylus
4813 processKey(mapper, BTN_TOOL_RUBBER, 1);
4814 processSync(mapper);
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4818
4819 // mouse trumps eraser
4820 processKey(mapper, BTN_TOOL_MOUSE, 1);
4821 processSync(mapper);
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4825
4826 // back to default tool type
4827 processKey(mapper, BTN_TOOL_MOUSE, 0);
4828 processKey(mapper, BTN_TOOL_RUBBER, 0);
4829 processKey(mapper, BTN_TOOL_PEN, 0);
4830 processKey(mapper, BTN_TOOL_FINGER, 0);
4831 processSync(mapper);
4832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4835}
4836
4837TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838 addConfigurationProperty("touch.deviceType", "touchScreen");
4839 prepareDisplay(DISPLAY_ORIENTATION_0);
4840 prepareButtons();
4841 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004842 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004843 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844
4845 NotifyMotionArgs motionArgs;
4846
4847 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4848 processKey(mapper, BTN_TOOL_FINGER, 1);
4849 processMove(mapper, 100, 200);
4850 processSync(mapper);
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4854 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4855
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4857 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4859 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4860
4861 // move a little
4862 processMove(mapper, 150, 250);
4863 processSync(mapper);
4864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when BTN_TOUCH is pressed, pressure defaults to 1
4870 processKey(mapper, BTN_TOUCH, 1);
4871 processSync(mapper);
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4875 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4876
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4878 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4880 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4881
4882 // up when BTN_TOUCH is released, hover restored
4883 processKey(mapper, BTN_TOUCH, 0);
4884 processSync(mapper);
4885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4886 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4888 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4889
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4891 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4893 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4894
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4896 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4898 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4899
4900 // exit hover when pointer goes away
4901 processKey(mapper, BTN_TOOL_FINGER, 0);
4902 processSync(mapper);
4903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4904 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4906 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4907}
4908
4909TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 addConfigurationProperty("touch.deviceType", "touchScreen");
4911 prepareDisplay(DISPLAY_ORIENTATION_0);
4912 prepareButtons();
4913 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915
4916 NotifyMotionArgs motionArgs;
4917
4918 // initially hovering because pressure is 0
4919 processDown(mapper, 100, 200);
4920 processPressure(mapper, 0);
4921 processSync(mapper);
4922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4923 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4925 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4926
4927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4928 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4930 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4931
4932 // move a little
4933 processMove(mapper, 150, 250);
4934 processSync(mapper);
4935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure is non-zero
4941 processPressure(mapper, RAW_PRESSURE_MAX);
4942 processSync(mapper);
4943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4944 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4946 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4947
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4949 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4951 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4952
4953 // up when pressure becomes 0, hover restored
4954 processPressure(mapper, 0);
4955 processSync(mapper);
4956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4957 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4959 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4960
4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4962 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4964 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4965
4966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4967 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4969 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4970
4971 // exit hover when pointer goes away
4972 processUp(mapper);
4973 processSync(mapper);
4974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4975 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4977 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4978}
4979
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980// --- MultiTouchInputMapperTest ---
4981
4982class MultiTouchInputMapperTest : public TouchInputMapperTest {
4983protected:
4984 void prepareAxes(int axes);
4985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004986 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
4987 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
4988 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
4989 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
4990 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
4991 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
4992 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
4993 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
4994 void processId(MultiTouchInputMapper& mapper, int32_t id);
4995 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
4996 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
4997 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
4998 void processMTSync(MultiTouchInputMapper& mapper);
4999 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000};
5001
5002void MultiTouchInputMapperTest::prepareAxes(int axes) {
5003 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5005 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 }
5007 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005008 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5009 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005011 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5012 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013 }
5014 }
5015 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005016 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5017 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005019 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5020 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021 }
5022 }
5023 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005024 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5025 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026 }
5027 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005028 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5029 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030 }
5031 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005032 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5033 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034 }
5035 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005036 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5037 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038 }
5039 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005040 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5041 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 }
5043 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005044 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045 }
5046}
5047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005048void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5049 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005050 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5051 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052}
5053
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005054void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5055 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005056 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057}
5058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005059void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5060 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005061 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062}
5063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005064void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005065 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066}
5067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005068void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005069 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070}
5071
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005072void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5073 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005074 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075}
5076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005077void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005078 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079}
5080
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005081void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005082 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083}
5084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005085void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005086 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087}
5088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005089void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005090 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005091}
5092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005093void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005094 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095}
5096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005097void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5098 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005099 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100}
5101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005102void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005103 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104}
5105
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005106void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005107 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108}
5109
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 addConfigurationProperty("touch.deviceType", "touchScreen");
5112 prepareDisplay(DISPLAY_ORIENTATION_0);
5113 prepareAxes(POSITION);
5114 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005115 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116
5117 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5118
5119 NotifyMotionArgs motionArgs;
5120
5121 // Two fingers down at once.
5122 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5123 processPosition(mapper, x1, y1);
5124 processMTSync(mapper);
5125 processPosition(mapper, x2, y2);
5126 processMTSync(mapper);
5127 processSync(mapper);
5128
5129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5130 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5131 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5132 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5133 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5134 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5135 ASSERT_EQ(0, motionArgs.flags);
5136 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5137 ASSERT_EQ(0, motionArgs.buttonState);
5138 ASSERT_EQ(0, motionArgs.edgeFlags);
5139 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5140 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5143 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5144 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5145 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5147
5148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5149 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5150 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5151 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5152 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5153 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5154 motionArgs.action);
5155 ASSERT_EQ(0, motionArgs.flags);
5156 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5157 ASSERT_EQ(0, motionArgs.buttonState);
5158 ASSERT_EQ(0, motionArgs.edgeFlags);
5159 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5160 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5162 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5165 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5167 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5168 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5169 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5170 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5171
5172 // Move.
5173 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5174 processPosition(mapper, x1, y1);
5175 processMTSync(mapper);
5176 processPosition(mapper, x2, y2);
5177 processMTSync(mapper);
5178 processSync(mapper);
5179
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5182 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5183 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5184 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5186 ASSERT_EQ(0, motionArgs.flags);
5187 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5188 ASSERT_EQ(0, motionArgs.buttonState);
5189 ASSERT_EQ(0, motionArgs.edgeFlags);
5190 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5191 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5193 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5196 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5198 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5199 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5200 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5201 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5202
5203 // First finger up.
5204 x2 += 15; y2 -= 20;
5205 processPosition(mapper, x2, y2);
5206 processMTSync(mapper);
5207 processSync(mapper);
5208
5209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5210 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5211 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5212 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5213 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5214 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5215 motionArgs.action);
5216 ASSERT_EQ(0, motionArgs.flags);
5217 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5218 ASSERT_EQ(0, motionArgs.buttonState);
5219 ASSERT_EQ(0, motionArgs.edgeFlags);
5220 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5221 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5223 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5226 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5228 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5229 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5230 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5232
5233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5234 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5235 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5236 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5237 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5239 ASSERT_EQ(0, motionArgs.flags);
5240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5241 ASSERT_EQ(0, motionArgs.buttonState);
5242 ASSERT_EQ(0, motionArgs.edgeFlags);
5243 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5244 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5247 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5248 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5249 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5251
5252 // Move.
5253 x2 += 20; y2 -= 25;
5254 processPosition(mapper, x2, y2);
5255 processMTSync(mapper);
5256 processSync(mapper);
5257
5258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5260 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5261 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5262 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5264 ASSERT_EQ(0, motionArgs.flags);
5265 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5266 ASSERT_EQ(0, motionArgs.buttonState);
5267 ASSERT_EQ(0, motionArgs.edgeFlags);
5268 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5269 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5272 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5273 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5274 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5275 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5276
5277 // New finger down.
5278 int32_t x3 = 700, y3 = 300;
5279 processPosition(mapper, x2, y2);
5280 processMTSync(mapper);
5281 processPosition(mapper, x3, y3);
5282 processMTSync(mapper);
5283 processSync(mapper);
5284
5285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5287 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5288 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5289 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5290 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5291 motionArgs.action);
5292 ASSERT_EQ(0, motionArgs.flags);
5293 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5294 ASSERT_EQ(0, motionArgs.buttonState);
5295 ASSERT_EQ(0, motionArgs.edgeFlags);
5296 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5297 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5299 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5302 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5304 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5305 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5306 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5307 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5308
5309 // Second finger up.
5310 x3 += 30; y3 -= 20;
5311 processPosition(mapper, x3, y3);
5312 processMTSync(mapper);
5313 processSync(mapper);
5314
5315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5317 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5318 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5319 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5320 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5321 motionArgs.action);
5322 ASSERT_EQ(0, motionArgs.flags);
5323 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5324 ASSERT_EQ(0, motionArgs.buttonState);
5325 ASSERT_EQ(0, motionArgs.edgeFlags);
5326 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5327 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5329 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5332 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5334 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5335 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5336 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5338
5339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5341 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5342 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5343 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5345 ASSERT_EQ(0, motionArgs.flags);
5346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5347 ASSERT_EQ(0, motionArgs.buttonState);
5348 ASSERT_EQ(0, motionArgs.edgeFlags);
5349 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5350 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5353 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5354 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5355 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5356 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5357
5358 // Last finger up.
5359 processMTSync(mapper);
5360 processSync(mapper);
5361
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5364 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5365 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5366 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5368 ASSERT_EQ(0, motionArgs.flags);
5369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5370 ASSERT_EQ(0, motionArgs.buttonState);
5371 ASSERT_EQ(0, motionArgs.edgeFlags);
5372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5376 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5377 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5378 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5380
5381 // Should not have sent any more keys or motions.
5382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5384}
5385
5386TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387 addConfigurationProperty("touch.deviceType", "touchScreen");
5388 prepareDisplay(DISPLAY_ORIENTATION_0);
5389 prepareAxes(POSITION | ID);
5390 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005391 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392
5393 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5394
5395 NotifyMotionArgs motionArgs;
5396
5397 // Two fingers down at once.
5398 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5399 processPosition(mapper, x1, y1);
5400 processId(mapper, 1);
5401 processMTSync(mapper);
5402 processPosition(mapper, x2, y2);
5403 processId(mapper, 2);
5404 processMTSync(mapper);
5405 processSync(mapper);
5406
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5408 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5409 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5413 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5414
5415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5416 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5417 motionArgs.action);
5418 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5419 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5421 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5424 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5426 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5427
5428 // Move.
5429 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5430 processPosition(mapper, x1, y1);
5431 processId(mapper, 1);
5432 processMTSync(mapper);
5433 processPosition(mapper, x2, y2);
5434 processId(mapper, 2);
5435 processMTSync(mapper);
5436 processSync(mapper);
5437
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5440 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5441 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5443 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5446 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5448 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5449
5450 // First finger up.
5451 x2 += 15; y2 -= 20;
5452 processPosition(mapper, x2, y2);
5453 processId(mapper, 2);
5454 processMTSync(mapper);
5455 processSync(mapper);
5456
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5458 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5459 motionArgs.action);
5460 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5463 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5466 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5468 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5469
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5473 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5476 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5477
5478 // Move.
5479 x2 += 20; y2 -= 25;
5480 processPosition(mapper, x2, y2);
5481 processId(mapper, 2);
5482 processMTSync(mapper);
5483 processSync(mapper);
5484
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5487 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5488 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5491 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5492
5493 // New finger down.
5494 int32_t x3 = 700, y3 = 300;
5495 processPosition(mapper, x2, y2);
5496 processId(mapper, 2);
5497 processMTSync(mapper);
5498 processPosition(mapper, x3, y3);
5499 processId(mapper, 3);
5500 processMTSync(mapper);
5501 processSync(mapper);
5502
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5504 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5505 motionArgs.action);
5506 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5509 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5512 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5514 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5515
5516 // Second finger up.
5517 x3 += 30; y3 -= 20;
5518 processPosition(mapper, x3, y3);
5519 processId(mapper, 3);
5520 processMTSync(mapper);
5521 processSync(mapper);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5525 motionArgs.action);
5526 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5527 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5529 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5532 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5534 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5535
5536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5538 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5539 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5542 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5543
5544 // Last finger up.
5545 processMTSync(mapper);
5546 processSync(mapper);
5547
5548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5549 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5550 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5551 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5554 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5555
5556 // Should not have sent any more keys or motions.
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5559}
5560
5561TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562 addConfigurationProperty("touch.deviceType", "touchScreen");
5563 prepareDisplay(DISPLAY_ORIENTATION_0);
5564 prepareAxes(POSITION | ID | SLOT);
5565 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005566 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567
5568 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5569
5570 NotifyMotionArgs motionArgs;
5571
5572 // Two fingers down at once.
5573 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5574 processPosition(mapper, x1, y1);
5575 processId(mapper, 1);
5576 processSlot(mapper, 1);
5577 processPosition(mapper, x2, y2);
5578 processId(mapper, 2);
5579 processSync(mapper);
5580
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5583 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5584 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5587 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5588
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5590 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5591 motionArgs.action);
5592 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5593 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5595 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5598 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5600 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5601
5602 // Move.
5603 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5604 processSlot(mapper, 0);
5605 processPosition(mapper, x1, y1);
5606 processSlot(mapper, 1);
5607 processPosition(mapper, x2, y2);
5608 processSync(mapper);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5612 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5613 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5615 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5618 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5620 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5621
5622 // First finger up.
5623 x2 += 15; y2 -= 20;
5624 processSlot(mapper, 0);
5625 processId(mapper, -1);
5626 processSlot(mapper, 1);
5627 processPosition(mapper, x2, y2);
5628 processSync(mapper);
5629
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5631 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5632 motionArgs.action);
5633 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5634 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5636 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5639 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5641 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5642
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5645 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5646 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5649 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5650
5651 // Move.
5652 x2 += 20; y2 -= 25;
5653 processPosition(mapper, x2, y2);
5654 processSync(mapper);
5655
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5658 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5659 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5662 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5663
5664 // New finger down.
5665 int32_t x3 = 700, y3 = 300;
5666 processPosition(mapper, x2, y2);
5667 processSlot(mapper, 0);
5668 processId(mapper, 3);
5669 processPosition(mapper, x3, y3);
5670 processSync(mapper);
5671
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5673 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5674 motionArgs.action);
5675 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5678 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5681 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5683 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5684
5685 // Second finger up.
5686 x3 += 30; y3 -= 20;
5687 processSlot(mapper, 1);
5688 processId(mapper, -1);
5689 processSlot(mapper, 0);
5690 processPosition(mapper, x3, y3);
5691 processSync(mapper);
5692
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5694 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5695 motionArgs.action);
5696 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5697 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5699 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5702 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5704 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5705
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5708 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5709 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5712 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5713
5714 // Last finger up.
5715 processId(mapper, -1);
5716 processSync(mapper);
5717
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5720 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5724 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5725
5726 // Should not have sent any more keys or motions.
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5729}
5730
5731TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732 addConfigurationProperty("touch.deviceType", "touchScreen");
5733 prepareDisplay(DISPLAY_ORIENTATION_0);
5734 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005735 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736
5737 // These calculations are based on the input device calibration documentation.
5738 int32_t rawX = 100;
5739 int32_t rawY = 200;
5740 int32_t rawTouchMajor = 7;
5741 int32_t rawTouchMinor = 6;
5742 int32_t rawToolMajor = 9;
5743 int32_t rawToolMinor = 8;
5744 int32_t rawPressure = 11;
5745 int32_t rawDistance = 0;
5746 int32_t rawOrientation = 3;
5747 int32_t id = 5;
5748
5749 float x = toDisplayX(rawX);
5750 float y = toDisplayY(rawY);
5751 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5752 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5753 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5754 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5755 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5756 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5757 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5758 float distance = float(rawDistance);
5759
5760 processPosition(mapper, rawX, rawY);
5761 processTouchMajor(mapper, rawTouchMajor);
5762 processTouchMinor(mapper, rawTouchMinor);
5763 processToolMajor(mapper, rawToolMajor);
5764 processToolMinor(mapper, rawToolMinor);
5765 processPressure(mapper, rawPressure);
5766 processOrientation(mapper, rawOrientation);
5767 processDistance(mapper, rawDistance);
5768 processId(mapper, id);
5769 processMTSync(mapper);
5770 processSync(mapper);
5771
5772 NotifyMotionArgs args;
5773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5774 ASSERT_EQ(0, args.pointerProperties[0].id);
5775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5776 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5777 orientation, distance));
5778}
5779
5780TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781 addConfigurationProperty("touch.deviceType", "touchScreen");
5782 prepareDisplay(DISPLAY_ORIENTATION_0);
5783 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5784 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005785 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786
5787 // These calculations are based on the input device calibration documentation.
5788 int32_t rawX = 100;
5789 int32_t rawY = 200;
5790 int32_t rawTouchMajor = 140;
5791 int32_t rawTouchMinor = 120;
5792 int32_t rawToolMajor = 180;
5793 int32_t rawToolMinor = 160;
5794
5795 float x = toDisplayX(rawX);
5796 float y = toDisplayY(rawY);
5797 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5798 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5799 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5800 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5801 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5802
5803 processPosition(mapper, rawX, rawY);
5804 processTouchMajor(mapper, rawTouchMajor);
5805 processTouchMinor(mapper, rawTouchMinor);
5806 processToolMajor(mapper, rawToolMajor);
5807 processToolMinor(mapper, rawToolMinor);
5808 processMTSync(mapper);
5809 processSync(mapper);
5810
5811 NotifyMotionArgs args;
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5814 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5815}
5816
5817TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818 addConfigurationProperty("touch.deviceType", "touchScreen");
5819 prepareDisplay(DISPLAY_ORIENTATION_0);
5820 prepareAxes(POSITION | TOUCH | TOOL);
5821 addConfigurationProperty("touch.size.calibration", "diameter");
5822 addConfigurationProperty("touch.size.scale", "10");
5823 addConfigurationProperty("touch.size.bias", "160");
5824 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005825 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826
5827 // These calculations are based on the input device calibration documentation.
5828 // Note: We only provide a single common touch/tool value because the device is assumed
5829 // not to emit separate values for each pointer (isSummed = 1).
5830 int32_t rawX = 100;
5831 int32_t rawY = 200;
5832 int32_t rawX2 = 150;
5833 int32_t rawY2 = 250;
5834 int32_t rawTouchMajor = 5;
5835 int32_t rawToolMajor = 8;
5836
5837 float x = toDisplayX(rawX);
5838 float y = toDisplayY(rawY);
5839 float x2 = toDisplayX(rawX2);
5840 float y2 = toDisplayY(rawY2);
5841 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5842 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5843 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5844
5845 processPosition(mapper, rawX, rawY);
5846 processTouchMajor(mapper, rawTouchMajor);
5847 processToolMajor(mapper, rawToolMajor);
5848 processMTSync(mapper);
5849 processPosition(mapper, rawX2, rawY2);
5850 processTouchMajor(mapper, rawTouchMajor);
5851 processToolMajor(mapper, rawToolMajor);
5852 processMTSync(mapper);
5853 processSync(mapper);
5854
5855 NotifyMotionArgs args;
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5858
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5860 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5861 args.action);
5862 ASSERT_EQ(size_t(2), args.pointerCount);
5863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5864 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5866 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5867}
5868
5869TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 addConfigurationProperty("touch.deviceType", "touchScreen");
5871 prepareDisplay(DISPLAY_ORIENTATION_0);
5872 prepareAxes(POSITION | TOUCH | TOOL);
5873 addConfigurationProperty("touch.size.calibration", "area");
5874 addConfigurationProperty("touch.size.scale", "43");
5875 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005876 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877
5878 // These calculations are based on the input device calibration documentation.
5879 int32_t rawX = 100;
5880 int32_t rawY = 200;
5881 int32_t rawTouchMajor = 5;
5882 int32_t rawToolMajor = 8;
5883
5884 float x = toDisplayX(rawX);
5885 float y = toDisplayY(rawY);
5886 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5887 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5888 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5889
5890 processPosition(mapper, rawX, rawY);
5891 processTouchMajor(mapper, rawTouchMajor);
5892 processToolMajor(mapper, rawToolMajor);
5893 processMTSync(mapper);
5894 processSync(mapper);
5895
5896 NotifyMotionArgs args;
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5899 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5900}
5901
5902TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903 addConfigurationProperty("touch.deviceType", "touchScreen");
5904 prepareDisplay(DISPLAY_ORIENTATION_0);
5905 prepareAxes(POSITION | PRESSURE);
5906 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5907 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005908 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909
Michael Wrightaa449c92017-12-13 21:21:43 +00005910 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005911 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00005912 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5913 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5914 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5915
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 // These calculations are based on the input device calibration documentation.
5917 int32_t rawX = 100;
5918 int32_t rawY = 200;
5919 int32_t rawPressure = 60;
5920
5921 float x = toDisplayX(rawX);
5922 float y = toDisplayY(rawY);
5923 float pressure = float(rawPressure) * 0.01f;
5924
5925 processPosition(mapper, rawX, rawY);
5926 processPressure(mapper, rawPressure);
5927 processMTSync(mapper);
5928 processSync(mapper);
5929
5930 NotifyMotionArgs args;
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5933 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5934}
5935
5936TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937 addConfigurationProperty("touch.deviceType", "touchScreen");
5938 prepareDisplay(DISPLAY_ORIENTATION_0);
5939 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005940 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941
5942 NotifyMotionArgs motionArgs;
5943 NotifyKeyArgs keyArgs;
5944
5945 processId(mapper, 1);
5946 processPosition(mapper, 100, 200);
5947 processSync(mapper);
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5949 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5950 ASSERT_EQ(0, motionArgs.buttonState);
5951
5952 // press BTN_LEFT, release BTN_LEFT
5953 processKey(mapper, BTN_LEFT, 1);
5954 processSync(mapper);
5955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5957 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5958
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5961 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5962
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963 processKey(mapper, BTN_LEFT, 0);
5964 processSync(mapper);
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005968
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005971 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972
5973 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5974 processKey(mapper, BTN_RIGHT, 1);
5975 processKey(mapper, BTN_MIDDLE, 1);
5976 processSync(mapper);
5977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5979 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5980 motionArgs.buttonState);
5981
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5984 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5985
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5988 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5989 motionArgs.buttonState);
5990
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991 processKey(mapper, BTN_RIGHT, 0);
5992 processSync(mapper);
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005996
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005999 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000
6001 processKey(mapper, BTN_MIDDLE, 0);
6002 processSync(mapper);
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006009 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010
6011 // press BTN_BACK, release BTN_BACK
6012 processKey(mapper, BTN_BACK, 1);
6013 processSync(mapper);
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6015 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6016 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006017
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006020 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6021
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6023 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6024 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025
6026 processKey(mapper, BTN_BACK, 0);
6027 processSync(mapper);
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006031
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006034 ASSERT_EQ(0, motionArgs.buttonState);
6035
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6037 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6038 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6039
6040 // press BTN_SIDE, release BTN_SIDE
6041 processKey(mapper, BTN_SIDE, 1);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6044 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6045 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006046
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006049 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6050
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6053 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006054
6055 processKey(mapper, BTN_SIDE, 0);
6056 processSync(mapper);
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063 ASSERT_EQ(0, motionArgs.buttonState);
6064
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6066 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6067 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6068
6069 // press BTN_FORWARD, release BTN_FORWARD
6070 processKey(mapper, BTN_FORWARD, 1);
6071 processSync(mapper);
6072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6073 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6074 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006078 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6079
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6082 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083
6084 processKey(mapper, BTN_FORWARD, 0);
6085 processSync(mapper);
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006092 ASSERT_EQ(0, motionArgs.buttonState);
6093
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6095 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6096 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6097
6098 // press BTN_EXTRA, release BTN_EXTRA
6099 processKey(mapper, BTN_EXTRA, 1);
6100 processSync(mapper);
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6102 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6103 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006107 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6108
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6111 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112
6113 processKey(mapper, BTN_EXTRA, 0);
6114 processSync(mapper);
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121 ASSERT_EQ(0, motionArgs.buttonState);
6122
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6124 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6125 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6126
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6128
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 // press BTN_STYLUS, release BTN_STYLUS
6130 processKey(mapper, BTN_STYLUS, 1);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006134 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6135
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6138 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139
6140 processKey(mapper, BTN_STYLUS, 0);
6141 processSync(mapper);
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006145
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006148 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149
6150 // press BTN_STYLUS2, release BTN_STYLUS2
6151 processKey(mapper, BTN_STYLUS2, 1);
6152 processSync(mapper);
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006155 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6156
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6158 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6159 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160
6161 processKey(mapper, BTN_STYLUS2, 0);
6162 processSync(mapper);
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006166
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006169 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170
6171 // release touch
6172 processId(mapper, -1);
6173 processSync(mapper);
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6175 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6176 ASSERT_EQ(0, motionArgs.buttonState);
6177}
6178
6179TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180 addConfigurationProperty("touch.deviceType", "touchScreen");
6181 prepareDisplay(DISPLAY_ORIENTATION_0);
6182 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006183 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184
6185 NotifyMotionArgs motionArgs;
6186
6187 // default tool type is finger
6188 processId(mapper, 1);
6189 processPosition(mapper, 100, 200);
6190 processSync(mapper);
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6194
6195 // eraser
6196 processKey(mapper, BTN_TOOL_RUBBER, 1);
6197 processSync(mapper);
6198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6201
6202 // stylus
6203 processKey(mapper, BTN_TOOL_RUBBER, 0);
6204 processKey(mapper, BTN_TOOL_PEN, 1);
6205 processSync(mapper);
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6209
6210 // brush
6211 processKey(mapper, BTN_TOOL_PEN, 0);
6212 processKey(mapper, BTN_TOOL_BRUSH, 1);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6217
6218 // pencil
6219 processKey(mapper, BTN_TOOL_BRUSH, 0);
6220 processKey(mapper, BTN_TOOL_PENCIL, 1);
6221 processSync(mapper);
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6225
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006226 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 processKey(mapper, BTN_TOOL_PENCIL, 0);
6228 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6229 processSync(mapper);
6230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6233
6234 // mouse
6235 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6236 processKey(mapper, BTN_TOOL_MOUSE, 1);
6237 processSync(mapper);
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6241
6242 // lens
6243 processKey(mapper, BTN_TOOL_MOUSE, 0);
6244 processKey(mapper, BTN_TOOL_LENS, 1);
6245 processSync(mapper);
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6249
6250 // double-tap
6251 processKey(mapper, BTN_TOOL_LENS, 0);
6252 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6253 processSync(mapper);
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6257
6258 // triple-tap
6259 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6260 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6265
6266 // quad-tap
6267 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6268 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6273
6274 // finger
6275 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6276 processKey(mapper, BTN_TOOL_FINGER, 1);
6277 processSync(mapper);
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6281
6282 // stylus trumps finger
6283 processKey(mapper, BTN_TOOL_PEN, 1);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6288
6289 // eraser trumps stylus
6290 processKey(mapper, BTN_TOOL_RUBBER, 1);
6291 processSync(mapper);
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6295
6296 // mouse trumps eraser
6297 processKey(mapper, BTN_TOOL_MOUSE, 1);
6298 processSync(mapper);
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6302
6303 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6304 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6309
6310 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6311 processToolType(mapper, MT_TOOL_PEN);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6316
6317 // back to default tool type
6318 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6319 processKey(mapper, BTN_TOOL_MOUSE, 0);
6320 processKey(mapper, BTN_TOOL_RUBBER, 0);
6321 processKey(mapper, BTN_TOOL_PEN, 0);
6322 processKey(mapper, BTN_TOOL_FINGER, 0);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6327}
6328
6329TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 addConfigurationProperty("touch.deviceType", "touchScreen");
6331 prepareDisplay(DISPLAY_ORIENTATION_0);
6332 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006333 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335
6336 NotifyMotionArgs motionArgs;
6337
6338 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6339 processId(mapper, 1);
6340 processPosition(mapper, 100, 200);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6345 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6346
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6350 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6351
6352 // move a little
6353 processPosition(mapper, 150, 250);
6354 processSync(mapper);
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6356 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when BTN_TOUCH is pressed, pressure defaults to 1
6361 processKey(mapper, BTN_TOUCH, 1);
6362 processSync(mapper);
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6364 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6366 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6367
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6371 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6372
6373 // up when BTN_TOUCH is released, hover restored
6374 processKey(mapper, BTN_TOUCH, 0);
6375 processSync(mapper);
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6377 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6379 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6380
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6384 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6385
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6389 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6390
6391 // exit hover when pointer goes away
6392 processId(mapper, -1);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6397 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6398}
6399
6400TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 addConfigurationProperty("touch.deviceType", "touchScreen");
6402 prepareDisplay(DISPLAY_ORIENTATION_0);
6403 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006404 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405
6406 NotifyMotionArgs motionArgs;
6407
6408 // initially hovering because pressure is 0
6409 processId(mapper, 1);
6410 processPosition(mapper, 100, 200);
6411 processPressure(mapper, 0);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6416 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6421 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6422
6423 // move a little
6424 processPosition(mapper, 150, 250);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure becomes non-zero
6432 processPressure(mapper, RAW_PRESSURE_MAX);
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6437 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6438
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6443
6444 // up when pressure becomes 0, hover restored
6445 processPressure(mapper, 0);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6450 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6451
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6455 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6458 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6460 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6461
6462 // exit hover when pointer goes away
6463 processId(mapper, -1);
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6468 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6469}
6470
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006471/**
6472 * Set the input device port <--> display port associations, and check that the
6473 * events are routed to the display that matches the display port.
6474 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6475 */
6476TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006477 const std::string usb2 = "USB2";
6478 const uint8_t hdmi1 = 0;
6479 const uint8_t hdmi2 = 1;
6480 const std::string secondaryUniqueId = "uniqueId2";
6481 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6482
6483 addConfigurationProperty("touch.deviceType", "touchScreen");
6484 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006485 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006486
6487 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6488 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6489
6490 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6491 // for this input device is specified, and the matching viewport is not present,
6492 // the input device should be disabled (at the mapper level).
6493
6494 // Add viewport for display 2 on hdmi2
6495 prepareSecondaryDisplay(type, hdmi2);
6496 // Send a touch event
6497 processPosition(mapper, 100, 100);
6498 processSync(mapper);
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6500
6501 // Add viewport for display 1 on hdmi1
6502 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6503 // Send a touch event again
6504 processPosition(mapper, 100, 100);
6505 processSync(mapper);
6506
6507 NotifyMotionArgs args;
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6509 ASSERT_EQ(DISPLAY_ID, args.displayId);
6510}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006512TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006513 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006514 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006515 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006516 fakePointerController->setPosition(100, 200);
6517 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006518 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6519
Garfield Tan888a6a42020-01-09 11:39:16 -08006520 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6521 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6522
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006523 prepareDisplay(DISPLAY_ORIENTATION_0);
6524 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006525 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006526
6527 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006528 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006529
6530 NotifyMotionArgs motionArgs;
6531 processPosition(mapper, 100, 100);
6532 processSync(mapper);
6533
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6536 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6537}
6538
Arthur Hung7c645402019-01-25 17:45:42 +08006539TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6540 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006541 prepareAxes(POSITION | ID | SLOT);
6542 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006543 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006544
6545 // Create the second touch screen device, and enable multi fingers.
6546 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006547 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006548 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006549 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006550 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006551 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006552 std::unique_ptr<InputDevice> device2 =
6553 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006554 identifier);
6555 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6556 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6557 0 /*flat*/, 0 /*fuzz*/);
6558 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6559 0 /*flat*/, 0 /*fuzz*/);
6560 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6561 0 /*flat*/, 0 /*fuzz*/);
6562 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6563 0 /*flat*/, 0 /*fuzz*/);
6564 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6565 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6566 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006567
6568 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006569 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006570 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6571 device2->reset(ARBITRARY_TIME);
6572
6573 // Setup PointerController.
6574 sp<FakePointerController> fakePointerController = new FakePointerController();
6575 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6576 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6577
6578 // Setup policy for associated displays and show touches.
6579 const uint8_t hdmi1 = 0;
6580 const uint8_t hdmi2 = 1;
6581 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6582 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6583 mFakePolicy->setShowTouches(true);
6584
6585 // Create displays.
6586 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6587 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6588
6589 // Default device will reconfigure above, need additional reconfiguration for another device.
6590 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6591 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6592
6593 // Two fingers down at default display.
6594 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6595 processPosition(mapper, x1, y1);
6596 processId(mapper, 1);
6597 processSlot(mapper, 1);
6598 processPosition(mapper, x2, y2);
6599 processId(mapper, 2);
6600 processSync(mapper);
6601
6602 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6603 fakePointerController->getSpots().find(DISPLAY_ID);
6604 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6605 ASSERT_EQ(size_t(2), iter->second.size());
6606
6607 // Two fingers down at second display.
6608 processPosition(mapper2, x1, y1);
6609 processId(mapper2, 1);
6610 processSlot(mapper2, 1);
6611 processPosition(mapper2, x2, y2);
6612 processId(mapper2, 2);
6613 processSync(mapper2);
6614
6615 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6616 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6617 ASSERT_EQ(size_t(2), iter->second.size());
6618}
6619
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006620TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006621 prepareAxes(POSITION);
6622 addConfigurationProperty("touch.deviceType", "touchScreen");
6623 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006624 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006625
6626 NotifyMotionArgs motionArgs;
6627 // Unrotated video frame
6628 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6629 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006630 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006631 processPosition(mapper, 100, 200);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 ASSERT_EQ(frames, motionArgs.videoFrames);
6635
6636 // Subsequent touch events should not have any videoframes
6637 // This is implemented separately in FakeEventHub,
6638 // but that should match the behaviour of TouchVideoDevice.
6639 processPosition(mapper, 200, 200);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6643}
6644
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006645TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006646 prepareAxes(POSITION);
6647 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006648 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006649 // Unrotated video frame
6650 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6651 NotifyMotionArgs motionArgs;
6652
6653 // Test all 4 orientations
6654 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6655 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6656 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6657 clearViewports();
6658 prepareDisplay(orientation);
6659 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006660 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006661 processPosition(mapper, 100, 200);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 frames[0].rotate(orientation);
6665 ASSERT_EQ(frames, motionArgs.videoFrames);
6666 }
6667}
6668
6669TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006670 prepareAxes(POSITION);
6671 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006672 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006673 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6674 // so mix these.
6675 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6676 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6677 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6678 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6679 NotifyMotionArgs motionArgs;
6680
6681 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006682 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006683 processPosition(mapper, 100, 200);
6684 processSync(mapper);
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 std::for_each(frames.begin(), frames.end(),
6687 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6688 ASSERT_EQ(frames, motionArgs.videoFrames);
6689}
6690
Arthur Hung9da14732019-09-02 16:16:58 +08006691/**
6692 * If we had defined port associations, but the viewport is not ready, the touch device would be
6693 * expected to be disabled, and it should be enabled after the viewport has found.
6694 */
6695TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006696 constexpr uint8_t hdmi2 = 1;
6697 const std::string secondaryUniqueId = "uniqueId2";
6698 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6699
6700 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6701
6702 addConfigurationProperty("touch.deviceType", "touchScreen");
6703 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006704 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006705
6706 ASSERT_EQ(mDevice->isEnabled(), false);
6707
6708 // Add display on hdmi2, the device should be enabled and can receive touch event.
6709 prepareSecondaryDisplay(type, hdmi2);
6710 ASSERT_EQ(mDevice->isEnabled(), true);
6711
6712 // Send a touch event.
6713 processPosition(mapper, 100, 100);
6714 processSync(mapper);
6715
6716 NotifyMotionArgs args;
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6718 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6719}
6720
Arthur Hung6cd19a42019-08-30 19:04:12 +08006721/**
6722 * Test touch should not work if outside of surface.
6723 */
6724TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
Arthur Hung6cd19a42019-08-30 19:04:12 +08006725 addConfigurationProperty("touch.deviceType", "touchScreen");
6726 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006727 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung6cd19a42019-08-30 19:04:12 +08006729
Arthur Hung05de5772019-09-26 18:31:26 +08006730 // Touch on left-top area should work.
6731 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6732 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6733 processPosition(mapper, rawX, rawY);
6734 processSync(mapper);
6735
6736 NotifyMotionArgs args;
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6738
6739 // Reset.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006740 mapper.reset(ARBITRARY_TIME);
Arthur Hung05de5772019-09-26 18:31:26 +08006741
6742 // Let logical display be different to physical display and rotate 90-degrees.
6743 std::optional<DisplayViewport> internalViewport =
6744 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6745 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6746 internalViewport->logicalLeft = 0;
6747 internalViewport->logicalTop = 0;
6748 internalViewport->logicalRight = DISPLAY_HEIGHT;
6749 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6750
6751 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6752 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6753 internalViewport->physicalRight = DISPLAY_HEIGHT;
6754 internalViewport->physicalBottom = DISPLAY_WIDTH;
6755
6756 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6757 internalViewport->deviceHeight = DISPLAY_WIDTH;
6758 mFakePolicy->updateViewport(internalViewport.value());
6759 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6760
6761 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006762 processPosition(mapper, rawX, rawY);
6763 processSync(mapper);
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6765}
6766
Arthur Hung421eb1c2020-01-16 00:09:42 +08006767TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006768 addConfigurationProperty("touch.deviceType", "touchScreen");
6769 prepareDisplay(DISPLAY_ORIENTATION_0);
6770 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006771 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006772
6773 NotifyMotionArgs motionArgs;
6774
6775 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6776 // finger down
6777 processId(mapper, 1);
6778 processPosition(mapper, x1, y1);
6779 processSync(mapper);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6781 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6783
6784 // finger move
6785 processId(mapper, 1);
6786 processPosition(mapper, x2, y2);
6787 processSync(mapper);
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6791
6792 // finger up.
6793 processId(mapper, -1);
6794 processSync(mapper);
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6796 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6798
6799 // new finger down
6800 processId(mapper, 1);
6801 processPosition(mapper, x3, y3);
6802 processSync(mapper);
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6806}
6807
6808/**
6809 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6810 * UP events should be ignored.
6811 */
6812TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006813 addConfigurationProperty("touch.deviceType", "touchScreen");
6814 prepareDisplay(DISPLAY_ORIENTATION_0);
6815 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006816 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006817
6818 NotifyMotionArgs motionArgs;
6819
6820 // default tool type is finger
6821 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6822 processId(mapper, 1);
6823 processPosition(mapper, x1, y1);
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 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6830 processToolType(mapper, MT_TOOL_PALM);
6831 processSync(mapper);
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6833 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6834
6835 // Ignore the following MOVE and UP events if had detect a palm event.
6836 processId(mapper, 1);
6837 processPosition(mapper, x2, y2);
6838 processSync(mapper);
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6840
6841 // finger up.
6842 processId(mapper, -1);
6843 processSync(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6845
6846 // new finger down
6847 processToolType(mapper, MT_TOOL_FINGER);
6848 processId(mapper, 1);
6849 processPosition(mapper, x3, y3);
6850 processSync(mapper);
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6852 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6854}
6855
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006856// --- MultiTouchInputMapperTest_ExternalDevice ---
6857
6858class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
6859protected:
6860 virtual void SetUp() override {
6861 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
6862 }
6863};
6864
6865/**
6866 * Expect fallback to internal viewport if device is external and external viewport is not present.
6867 */
6868TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
6869 prepareAxes(POSITION);
6870 addConfigurationProperty("touch.deviceType", "touchScreen");
6871 prepareDisplay(DISPLAY_ORIENTATION_0);
6872 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6873
6874 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6875
6876 NotifyMotionArgs motionArgs;
6877
6878 // Expect the event to be sent to the internal viewport,
6879 // because an external viewport is not present.
6880 processPosition(mapper, 100, 100);
6881 processSync(mapper);
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6884
6885 // Expect the event to be sent to the external viewport if it is present.
6886 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6887 processPosition(mapper, 100, 100);
6888 processSync(mapper);
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6890 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6891}
6892
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893} // namespace android