blob: 46cd4809fc666943ae212a1ea6d36bf255e34b53 [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>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
25#include <SingleTouchInputMapper.h>
26#include <SwitchInputMapper.h>
27#include <TestInputListener.h>
28#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080029#include <UinputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070031#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080033#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <math.h>
35
36namespace android {
37
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038using std::chrono_literals::operator""ms;
39
40// Timeout for waiting for an expected event
41static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
42
Michael Wrightd02c5b62014-02-10 15:10:22 -080043// An arbitrary time value.
44static const nsecs_t ARBITRARY_TIME = 1234;
45
46// Arbitrary display properties.
47static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070048static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080049static const int32_t DISPLAY_WIDTH = 480;
50static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070051static const int32_t VIRTUAL_DISPLAY_ID = 1;
52static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
53static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070054static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070055static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Error tolerance for floating point assertions.
58static const float EPSILON = 0.001f;
59
60template<typename T>
61static inline T min(T a, T b) {
62 return a < b ? a : b;
63}
64
65static inline float avg(float x, float y) {
66 return (x + y) / 2;
67}
68
69
70// --- FakePointerController ---
71
72class FakePointerController : public PointerControllerInterface {
73 bool mHaveBounds;
74 float mMinX, mMinY, mMaxX, mMaxY;
75 float mX, mY;
76 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080077 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
79protected:
80 virtual ~FakePointerController() { }
81
82public:
83 FakePointerController() :
84 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080085 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080086 }
87
88 void setBounds(float minX, float minY, float maxX, float maxY) {
89 mHaveBounds = true;
90 mMinX = minX;
91 mMinY = minY;
92 mMaxX = maxX;
93 mMaxY = maxY;
94 }
95
96 virtual void setPosition(float x, float y) {
97 mX = x;
98 mY = y;
99 }
100
101 virtual void setButtonState(int32_t buttonState) {
102 mButtonState = buttonState;
103 }
104
105 virtual int32_t getButtonState() const {
106 return mButtonState;
107 }
108
109 virtual void getPosition(float* outX, float* outY) const {
110 *outX = mX;
111 *outY = mY;
112 }
113
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800114 virtual int32_t getDisplayId() const {
115 return mDisplayId;
116 }
117
Garfield Tan888a6a42020-01-09 11:39:16 -0800118 virtual void setDisplayViewport(const DisplayViewport& viewport) {
119 mDisplayId = viewport.displayId;
120 }
121
Arthur Hung7c645402019-01-25 17:45:42 +0800122 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
123 return mSpotsByDisplay;
124 }
125
Michael Wrightd02c5b62014-02-10 15:10:22 -0800126private:
127 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
128 *outMinX = mMinX;
129 *outMinY = mMinY;
130 *outMaxX = mMaxX;
131 *outMaxY = mMaxY;
132 return mHaveBounds;
133 }
134
135 virtual void move(float deltaX, float deltaY) {
136 mX += deltaX;
137 if (mX < mMinX) mX = mMinX;
138 if (mX > mMaxX) mX = mMaxX;
139 mY += deltaY;
140 if (mY < mMinY) mY = mMinY;
141 if (mY > mMaxY) mY = mMaxY;
142 }
143
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100144 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 }
146
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100147 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 }
149
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100150 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 }
152
Arthur Hung7c645402019-01-25 17:45:42 +0800153 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
154 int32_t displayId) {
155 std::vector<int32_t> newSpots;
156 // Add spots for fingers that are down.
157 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
158 uint32_t id = idBits.clearFirstMarkedBit();
159 newSpots.push_back(id);
160 }
161
162 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 }
164
165 virtual void clearSpots() {
166 }
Arthur Hung7c645402019-01-25 17:45:42 +0800167
168 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169};
170
171
172// --- FakeInputReaderPolicy ---
173
174class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700175 std::mutex mLock;
176 std::condition_variable mDevicesChangedCondition;
177
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 InputReaderConfiguration mConfig;
179 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700180 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
181 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100182 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700183 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
185protected:
186 virtual ~FakeInputReaderPolicy() { }
187
188public:
189 FakeInputReaderPolicy() {
190 }
191
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700192 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800193 waitForInputDevices([](bool devicesChanged) {
194 if (!devicesChanged) {
195 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
196 }
197 });
198 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700199
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800200 void assertInputDevicesNotChanged() {
201 waitForInputDevices([](bool devicesChanged) {
202 if (devicesChanged) {
203 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
204 }
205 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700206 }
207
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700208 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100209 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100210 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700211 }
212
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700213 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
214 return mConfig.getDisplayViewportByUniqueId(uniqueId);
215 }
216 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
217 return mConfig.getDisplayViewportByType(type);
218 }
219
220 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
221 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700222 }
223
224 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700225 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
226 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700227 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700228 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700229 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100230 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 }
232
Arthur Hung6cd19a42019-08-30 19:04:12 +0800233 bool updateViewport(const DisplayViewport& viewport) {
234 size_t count = mViewports.size();
235 for (size_t i = 0; i < count; i++) {
236 const DisplayViewport& currentViewport = mViewports[i];
237 if (currentViewport.displayId == viewport.displayId) {
238 mViewports[i] = viewport;
239 mConfig.setDisplayViewports(mViewports);
240 return true;
241 }
242 }
243 // no viewport found.
244 return false;
245 }
246
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100247 void addExcludedDeviceName(const std::string& deviceName) {
248 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 }
250
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700251 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
252 mConfig.portAssociations.insert({inputPort, displayPort});
253 }
254
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000255 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700256
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000257 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
260 mPointerControllers.add(deviceId, controller);
261 }
262
263 const InputReaderConfiguration* getReaderConfiguration() const {
264 return &mConfig;
265 }
266
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800267 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 return mInputDevices;
269 }
270
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100271 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700272 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700273 return transform;
274 }
275
276 void setTouchAffineTransformation(const TouchAffineTransformation t) {
277 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800278 }
279
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800280 void setPointerCapture(bool enabled) {
281 mConfig.pointerCapture = enabled;
282 }
283
Arthur Hung7c645402019-01-25 17:45:42 +0800284 void setShowTouches(bool enabled) {
285 mConfig.showTouches = enabled;
286 }
287
Garfield Tan888a6a42020-01-09 11:39:16 -0800288 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
289 mConfig.defaultPointerDisplayId = pointerDisplayId;
290 }
291
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700293 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700294 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
295 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700296 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
297 || orientation == DISPLAY_ORIENTATION_270);
298 DisplayViewport v;
299 v.displayId = displayId;
300 v.orientation = orientation;
301 v.logicalLeft = 0;
302 v.logicalTop = 0;
303 v.logicalRight = isRotated ? height : width;
304 v.logicalBottom = isRotated ? width : height;
305 v.physicalLeft = 0;
306 v.physicalTop = 0;
307 v.physicalRight = isRotated ? height : width;
308 v.physicalBottom = isRotated ? width : height;
309 v.deviceWidth = isRotated ? height : width;
310 v.deviceHeight = isRotated ? width : height;
311 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700312 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100313 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700314 return v;
315 }
316
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
318 *outConfig = mConfig;
319 }
320
321 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
322 return mPointerControllers.valueFor(deviceId);
323 }
324
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800325 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700326 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700328 mInputDevicesChanged = true;
329 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 }
331
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100332 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700333 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100336 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
337 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800339
340 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
341 std::unique_lock<std::mutex> lock(mLock);
342 base::ScopedLockAssertion assumeLocked(mLock);
343
344 const bool devicesChanged =
345 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
346 return mInputDevicesChanged;
347 });
348 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
349 mInputDevicesChanged = false;
350 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351};
352
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353// --- FakeEventHub ---
354
355class FakeEventHub : public EventHubInterface {
356 struct KeyInfo {
357 int32_t keyCode;
358 uint32_t flags;
359 };
360
361 struct Device {
362 InputDeviceIdentifier identifier;
363 uint32_t classes;
364 PropertyMap configuration;
365 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
366 KeyedVector<int, bool> relativeAxes;
367 KeyedVector<int32_t, int32_t> keyCodeStates;
368 KeyedVector<int32_t, int32_t> scanCodeStates;
369 KeyedVector<int32_t, int32_t> switchStates;
370 KeyedVector<int32_t, int32_t> absoluteAxisValue;
371 KeyedVector<int32_t, KeyInfo> keysByScanCode;
372 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
373 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800374 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700375 bool enabled;
376
377 status_t enable() {
378 enabled = true;
379 return OK;
380 }
381
382 status_t disable() {
383 enabled = false;
384 return OK;
385 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700387 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700388 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 }
390 };
391
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700392 std::mutex mLock;
393 std::condition_variable mEventsCondition;
394
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100396 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700397 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600398 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700400public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 virtual ~FakeEventHub() {
402 for (size_t i = 0; i < mDevices.size(); i++) {
403 delete mDevices.valueAt(i);
404 }
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 FakeEventHub() { }
408
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100409 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 Device* device = new Device(classes);
411 device->identifier.name = name;
412 mDevices.add(deviceId, device);
413
414 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
415 }
416
417 void removeDevice(int32_t deviceId) {
418 delete mDevices.valueFor(deviceId);
419 mDevices.removeItem(deviceId);
420
421 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
422 }
423
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700424 bool isDeviceEnabled(int32_t deviceId) {
425 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700426 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700427 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
428 return false;
429 }
430 return device->enabled;
431 }
432
433 status_t enableDevice(int32_t deviceId) {
434 status_t result;
435 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700436 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
438 return BAD_VALUE;
439 }
440 if (device->enabled) {
441 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
442 return OK;
443 }
444 result = device->enable();
445 return result;
446 }
447
448 status_t disableDevice(int32_t deviceId) {
449 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700450 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700451 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
452 return BAD_VALUE;
453 }
454 if (!device->enabled) {
455 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
456 return OK;
457 }
458 return device->disable();
459 }
460
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 void finishDeviceScan() {
462 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
463 }
464
465 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
466 Device* device = getDevice(deviceId);
467 device->configuration.addProperty(key, value);
468 }
469
470 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
471 Device* device = getDevice(deviceId);
472 device->configuration.addAll(configuration);
473 }
474
475 void addAbsoluteAxis(int32_t deviceId, int axis,
476 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
477 Device* device = getDevice(deviceId);
478
479 RawAbsoluteAxisInfo info;
480 info.valid = true;
481 info.minValue = minValue;
482 info.maxValue = maxValue;
483 info.flat = flat;
484 info.fuzz = fuzz;
485 info.resolution = resolution;
486 device->absoluteAxes.add(axis, info);
487 }
488
489 void addRelativeAxis(int32_t deviceId, int32_t axis) {
490 Device* device = getDevice(deviceId);
491 device->relativeAxes.add(axis, true);
492 }
493
494 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
495 Device* device = getDevice(deviceId);
496 device->keyCodeStates.replaceValueFor(keyCode, state);
497 }
498
499 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
500 Device* device = getDevice(deviceId);
501 device->scanCodeStates.replaceValueFor(scanCode, state);
502 }
503
504 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
505 Device* device = getDevice(deviceId);
506 device->switchStates.replaceValueFor(switchCode, state);
507 }
508
509 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
510 Device* device = getDevice(deviceId);
511 device->absoluteAxisValue.replaceValueFor(axis, value);
512 }
513
514 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
515 int32_t keyCode, uint32_t flags) {
516 Device* device = getDevice(deviceId);
517 KeyInfo info;
518 info.keyCode = keyCode;
519 info.flags = flags;
520 if (scanCode) {
521 device->keysByScanCode.add(scanCode, info);
522 }
523 if (usageCode) {
524 device->keysByUsageCode.add(usageCode, info);
525 }
526 }
527
528 void addLed(int32_t deviceId, int32_t led, bool initialState) {
529 Device* device = getDevice(deviceId);
530 device->leds.add(led, initialState);
531 }
532
533 bool getLedState(int32_t deviceId, int32_t led) {
534 Device* device = getDevice(deviceId);
535 return device->leds.valueFor(led);
536 }
537
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100538 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 return mExcludedDevices;
540 }
541
542 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
543 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800544 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 }
546
547 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
548 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700549 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 RawEvent event;
551 event.when = when;
552 event.deviceId = deviceId;
553 event.type = type;
554 event.code = code;
555 event.value = value;
556 mEvents.push_back(event);
557
558 if (type == EV_ABS) {
559 setAbsoluteAxisValue(deviceId, code, value);
560 }
561 }
562
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600563 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
564 std::vector<TouchVideoFrame>> videoFrames) {
565 mVideoFrames = std::move(videoFrames);
566 }
567
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700569 std::unique_lock<std::mutex> lock(mLock);
570 base::ScopedLockAssertion assumeLocked(mLock);
571 const bool queueIsEmpty =
572 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
573 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
574 if (!queueIsEmpty) {
575 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
576 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
579private:
580 Device* getDevice(int32_t deviceId) const {
581 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100582 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 }
584
585 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
586 Device* device = getDevice(deviceId);
587 return device ? device->classes : 0;
588 }
589
590 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
591 Device* device = getDevice(deviceId);
592 return device ? device->identifier : InputDeviceIdentifier();
593 }
594
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100595 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 return 0;
597 }
598
599 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
600 Device* device = getDevice(deviceId);
601 if (device) {
602 *outConfiguration = device->configuration;
603 }
604 }
605
606 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
607 RawAbsoluteAxisInfo* outAxisInfo) const {
608 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800609 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 ssize_t index = device->absoluteAxes.indexOfKey(axis);
611 if (index >= 0) {
612 *outAxisInfo = device->absoluteAxes.valueAt(index);
613 return OK;
614 }
615 }
616 outAxisInfo->clear();
617 return -1;
618 }
619
620 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
621 Device* device = getDevice(deviceId);
622 if (device) {
623 return device->relativeAxes.indexOfKey(axis) >= 0;
624 }
625 return false;
626 }
627
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100628 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 return false;
630 }
631
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700632 virtual status_t mapKey(int32_t deviceId,
633 int32_t scanCode, int32_t usageCode, int32_t metaState,
634 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 Device* device = getDevice(deviceId);
636 if (device) {
637 const KeyInfo* key = getKey(device, scanCode, usageCode);
638 if (key) {
639 if (outKeycode) {
640 *outKeycode = key->keyCode;
641 }
642 if (outFlags) {
643 *outFlags = key->flags;
644 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700645 if (outMetaState) {
646 *outMetaState = metaState;
647 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 return OK;
649 }
650 }
651 return NAME_NOT_FOUND;
652 }
653
654 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
655 if (usageCode) {
656 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
657 if (index >= 0) {
658 return &device->keysByUsageCode.valueAt(index);
659 }
660 }
661 if (scanCode) {
662 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
663 if (index >= 0) {
664 return &device->keysByScanCode.valueAt(index);
665 }
666 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700667 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 }
669
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100670 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 return NAME_NOT_FOUND;
672 }
673
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100674 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 mExcludedDevices = devices;
676 }
677
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100678 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700679 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 if (mEvents.empty()) {
681 return 0;
682 }
683
684 *buffer = *mEvents.begin();
685 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700686 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 return 1;
688 }
689
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800690 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600691 auto it = mVideoFrames.find(deviceId);
692 if (it != mVideoFrames.end()) {
693 std::vector<TouchVideoFrame> frames = std::move(it->second);
694 mVideoFrames.erase(deviceId);
695 return frames;
696 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800697 return {};
698 }
699
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
701 Device* device = getDevice(deviceId);
702 if (device) {
703 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
704 if (index >= 0) {
705 return device->scanCodeStates.valueAt(index);
706 }
707 }
708 return AKEY_STATE_UNKNOWN;
709 }
710
711 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
712 Device* device = getDevice(deviceId);
713 if (device) {
714 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
715 if (index >= 0) {
716 return device->keyCodeStates.valueAt(index);
717 }
718 }
719 return AKEY_STATE_UNKNOWN;
720 }
721
722 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
723 Device* device = getDevice(deviceId);
724 if (device) {
725 ssize_t index = device->switchStates.indexOfKey(sw);
726 if (index >= 0) {
727 return device->switchStates.valueAt(index);
728 }
729 }
730 return AKEY_STATE_UNKNOWN;
731 }
732
733 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
734 int32_t* outValue) const {
735 Device* device = getDevice(deviceId);
736 if (device) {
737 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
738 if (index >= 0) {
739 *outValue = device->absoluteAxisValue.valueAt(index);
740 return OK;
741 }
742 }
743 *outValue = 0;
744 return -1;
745 }
746
747 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
748 uint8_t* outFlags) const {
749 bool result = false;
750 Device* device = getDevice(deviceId);
751 if (device) {
752 for (size_t i = 0; i < numCodes; i++) {
753 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
754 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
755 outFlags[i] = 1;
756 result = true;
757 }
758 }
759 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
760 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
761 outFlags[i] = 1;
762 result = true;
763 }
764 }
765 }
766 }
767 return result;
768 }
769
770 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
771 Device* device = getDevice(deviceId);
772 if (device) {
773 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
774 return index >= 0;
775 }
776 return false;
777 }
778
779 virtual bool hasLed(int32_t deviceId, int32_t led) const {
780 Device* device = getDevice(deviceId);
781 return device && device->leds.indexOfKey(led) >= 0;
782 }
783
784 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
785 Device* device = getDevice(deviceId);
786 if (device) {
787 ssize_t index = device->leds.indexOfKey(led);
788 if (index >= 0) {
789 device->leds.replaceValueAt(led, on);
790 } else {
791 ADD_FAILURE()
792 << "Attempted to set the state of an LED that the EventHub declared "
793 "was not present. led=" << led;
794 }
795 }
796 }
797
798 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800799 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 outVirtualKeys.clear();
801
802 Device* device = getDevice(deviceId);
803 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800804 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 }
806 }
807
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100808 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700809 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100812 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 return false;
814 }
815
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100816 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 }
818
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100819 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100822 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 return false;
824 }
825
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800826 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
828
829 virtual void monitor() {
830 }
831
832 virtual void requestReopenDevices() {
833 }
834
835 virtual void wake() {
836 }
837};
838
839
840// --- FakeInputReaderContext ---
841
842class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700843 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 sp<InputReaderPolicyInterface> mPolicy;
845 sp<InputListenerInterface> mListener;
846 int32_t mGlobalMetaState;
847 bool mUpdateGlobalMetaStateWasCalled;
848 int32_t mGeneration;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800849 int32_t mNextId;
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800850 wp<PointerControllerInterface> mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700853 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
854 const sp<InputReaderPolicyInterface>& policy,
855 const sp<InputListenerInterface>& listener)
856 : mEventHub(eventHub),
857 mPolicy(policy),
858 mListener(listener),
859 mGlobalMetaState(0),
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800860 mNextId(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
862 virtual ~FakeInputReaderContext() { }
863
864 void assertUpdateGlobalMetaStateWasCalled() {
865 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
866 << "Expected updateGlobalMetaState() to have been called.";
867 mUpdateGlobalMetaStateWasCalled = false;
868 }
869
870 void setGlobalMetaState(int32_t state) {
871 mGlobalMetaState = state;
872 }
873
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800874 uint32_t getGeneration() {
875 return mGeneration;
876 }
877
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800878 void updatePointerDisplay() {
879 sp<PointerControllerInterface> controller = mPointerController.promote();
880 if (controller != nullptr) {
881 InputReaderConfiguration config;
882 mPolicy->getReaderConfiguration(&config);
883 auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
884 if (viewport) {
885 controller->setDisplayViewport(*viewport);
886 }
887 }
888 }
889
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890private:
891 virtual void updateGlobalMetaState() {
892 mUpdateGlobalMetaStateWasCalled = true;
893 }
894
895 virtual int32_t getGlobalMetaState() {
896 return mGlobalMetaState;
897 }
898
899 virtual EventHubInterface* getEventHub() {
900 return mEventHub.get();
901 }
902
903 virtual InputReaderPolicyInterface* getPolicy() {
904 return mPolicy.get();
905 }
906
907 virtual InputListenerInterface* getListener() {
908 return mListener.get();
909 }
910
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100911 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 }
913
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800914 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800916 virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) {
917 sp<PointerControllerInterface> controller = mPointerController.promote();
918 if (controller == nullptr) {
919 controller = mPolicy->obtainPointerController(deviceId);
920 mPointerController = controller;
921 updatePointerDisplay();
922 }
923 return controller;
924 }
925
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 virtual void fadePointer() {
927 }
928
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100929 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 }
931
932 virtual int32_t bumpGeneration() {
933 return ++mGeneration;
934 }
Michael Wright842500e2015-03-13 17:32:02 -0700935
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800936 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700937
938 }
939
940 virtual void dispatchExternalStylusState(const StylusState&) {
941
942 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800943
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800944 virtual int32_t getNextId() { return mNextId++; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945};
946
947
948// --- FakeInputMapper ---
949
950class FakeInputMapper : public InputMapper {
951 uint32_t mSources;
952 int32_t mKeyboardType;
953 int32_t mMetaState;
954 KeyedVector<int32_t, int32_t> mKeyCodeStates;
955 KeyedVector<int32_t, int32_t> mScanCodeStates;
956 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800957 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700959 std::mutex mLock;
960 std::condition_variable mStateChangedCondition;
961 bool mConfigureWasCalled GUARDED_BY(mLock);
962 bool mResetWasCalled GUARDED_BY(mLock);
963 bool mProcessWasCalled GUARDED_BY(mLock);
964 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
Arthur Hungc23540e2018-11-29 20:42:11 +0800966 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800968 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
969 : InputMapper(deviceContext),
970 mSources(sources),
971 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800973 mConfigureWasCalled(false),
974 mResetWasCalled(false),
975 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976
977 virtual ~FakeInputMapper() { }
978
979 void setKeyboardType(int32_t keyboardType) {
980 mKeyboardType = keyboardType;
981 }
982
983 void setMetaState(int32_t metaState) {
984 mMetaState = metaState;
985 }
986
987 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700988 std::unique_lock<std::mutex> lock(mLock);
989 base::ScopedLockAssertion assumeLocked(mLock);
990 const bool configureCalled =
991 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
992 return mConfigureWasCalled;
993 });
994 if (!configureCalled) {
995 FAIL() << "Expected configure() to have been called.";
996 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 mConfigureWasCalled = false;
998 }
999
1000 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001001 std::unique_lock<std::mutex> lock(mLock);
1002 base::ScopedLockAssertion assumeLocked(mLock);
1003 const bool resetCalled =
1004 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1005 return mResetWasCalled;
1006 });
1007 if (!resetCalled) {
1008 FAIL() << "Expected reset() to have been called.";
1009 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010 mResetWasCalled = false;
1011 }
1012
Yi Kong9b14ac62018-07-17 13:48:38 -07001013 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001014 std::unique_lock<std::mutex> lock(mLock);
1015 base::ScopedLockAssertion assumeLocked(mLock);
1016 const bool processCalled =
1017 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1018 return mProcessWasCalled;
1019 });
1020 if (!processCalled) {
1021 FAIL() << "Expected process() to have been called.";
1022 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 if (outLastEvent) {
1024 *outLastEvent = mLastEvent;
1025 }
1026 mProcessWasCalled = false;
1027 }
1028
1029 void setKeyCodeState(int32_t keyCode, int32_t state) {
1030 mKeyCodeStates.replaceValueFor(keyCode, state);
1031 }
1032
1033 void setScanCodeState(int32_t scanCode, int32_t state) {
1034 mScanCodeStates.replaceValueFor(scanCode, state);
1035 }
1036
1037 void setSwitchState(int32_t switchCode, int32_t state) {
1038 mSwitchStates.replaceValueFor(switchCode, state);
1039 }
1040
1041 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001042 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 }
1044
1045private:
1046 virtual uint32_t getSources() {
1047 return mSources;
1048 }
1049
1050 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1051 InputMapper::populateDeviceInfo(deviceInfo);
1052
1053 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1054 deviceInfo->setKeyboardType(mKeyboardType);
1055 }
1056 }
1057
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001059 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001060 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001061
1062 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001063 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001064 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1065 mViewport = config->getDisplayViewportByPort(*displayPort);
1066 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001067
1068 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069 }
1070
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001071 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001072 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001074 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075 }
1076
1077 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001078 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079 mLastEvent = *rawEvent;
1080 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001081 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 }
1083
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001084 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1086 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1087 }
1088
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001089 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1091 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1092 }
1093
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001094 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1096 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1097 }
1098
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001099 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 const int32_t* keyCodes, uint8_t* outFlags) {
1101 bool result = false;
1102 for (size_t i = 0; i < numCodes; i++) {
1103 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1104 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1105 outFlags[i] = 1;
1106 result = true;
1107 }
1108 }
1109 }
1110 return result;
1111 }
1112
1113 virtual int32_t getMetaState() {
1114 return mMetaState;
1115 }
1116
1117 virtual void fadePointer() {
1118 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001119
1120 virtual std::optional<int32_t> getAssociatedDisplay() {
1121 if (mViewport) {
1122 return std::make_optional(mViewport->displayId);
1123 }
1124 return std::nullopt;
1125 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126};
1127
1128
1129// --- InstrumentedInputReader ---
1130
1131class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001132 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133
1134public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001135 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1136 const sp<InputReaderPolicyInterface>& policy,
1137 const sp<InputListenerInterface>& listener)
1138 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001139
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001140 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001142 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001144 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001145 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 InputDeviceIdentifier identifier;
1147 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001148 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001150 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 }
1152
Prabir Pradhan28efc192019-11-05 01:10:04 +00001153 // Make the protected loopOnce method accessible to tests.
1154 using InputReader::loopOnce;
1155
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001157 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1158 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001160 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001161 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 return device;
1163 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001164 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165 }
1166
1167 friend class InputReaderTest;
1168};
1169
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001170// --- InputReaderPolicyTest ---
1171class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001172protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173 sp<FakeInputReaderPolicy> mFakePolicy;
1174
Prabir Pradhan28efc192019-11-05 01:10:04 +00001175 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1176 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177};
1178
1179/**
1180 * Check that empty set of viewports is an acceptable configuration.
1181 * Also try to get internal viewport two different ways - by type and by uniqueId.
1182 *
1183 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1184 * Such configuration is not currently allowed.
1185 */
1186TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001187 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001188
1189 // We didn't add any viewports yet, so there shouldn't be any.
1190 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192 ASSERT_FALSE(internalViewport);
1193
1194 // Add an internal viewport, then clear it
1195 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001196 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001197
1198 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001199 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001200 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001201 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202
1203 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001204 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001205 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001206 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207
1208 mFakePolicy->clearViewports();
1209 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001210 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001211 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001212 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 ASSERT_FALSE(internalViewport);
1214}
1215
1216TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1217 const std::string internalUniqueId = "local:0";
1218 const std::string externalUniqueId = "local:1";
1219 const std::string virtualUniqueId1 = "virtual:2";
1220 const std::string virtualUniqueId2 = "virtual:3";
1221 constexpr int32_t virtualDisplayId1 = 2;
1222 constexpr int32_t virtualDisplayId2 = 3;
1223
1224 // Add an internal viewport
1225 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001226 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001227 // Add an external viewport
1228 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001229 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001230 // Add an virtual viewport
1231 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001232 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001233 // Add another virtual viewport
1234 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001235 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001236
1237 // Check matching by type for internal
1238 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001239 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 ASSERT_TRUE(internalViewport);
1241 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1242
1243 // Check matching by type for external
1244 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001245 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246 ASSERT_TRUE(externalViewport);
1247 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1248
1249 // Check matching by uniqueId for virtual viewport #1
1250 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001251 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001253 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1255 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1256
1257 // Check matching by uniqueId for virtual viewport #2
1258 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001259 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001260 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001261 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001262 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1263 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1264}
1265
1266
1267/**
1268 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1269 * that lookup works by checking display id.
1270 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1271 */
1272TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1273 const std::string uniqueId1 = "uniqueId1";
1274 const std::string uniqueId2 = "uniqueId2";
1275 constexpr int32_t displayId1 = 2;
1276 constexpr int32_t displayId2 = 3;
1277
1278 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1279 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1280 for (const ViewportType& type : types) {
1281 mFakePolicy->clearViewports();
1282 // Add a viewport
1283 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001284 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285 // Add another viewport
1286 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001287 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001288
1289 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001290 std::optional<DisplayViewport> viewport1 =
1291 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001292 ASSERT_TRUE(viewport1);
1293 ASSERT_EQ(displayId1, viewport1->displayId);
1294 ASSERT_EQ(type, viewport1->type);
1295
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001296 std::optional<DisplayViewport> viewport2 =
1297 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298 ASSERT_TRUE(viewport2);
1299 ASSERT_EQ(displayId2, viewport2->displayId);
1300 ASSERT_EQ(type, viewport2->type);
1301
1302 // When there are multiple viewports of the same kind, and uniqueId is not specified
1303 // in the call to getDisplayViewport, then that situation is not supported.
1304 // The viewports can be stored in any order, so we cannot rely on the order, since that
1305 // is just implementation detail.
1306 // However, we can check that it still returns *a* viewport, we just cannot assert
1307 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001308 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001309 ASSERT_TRUE(someViewport);
1310 }
1311}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001313/**
1314 * Check getDisplayViewportByPort
1315 */
1316TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1317 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1318 const std::string uniqueId1 = "uniqueId1";
1319 const std::string uniqueId2 = "uniqueId2";
1320 constexpr int32_t displayId1 = 1;
1321 constexpr int32_t displayId2 = 2;
1322 const uint8_t hdmi1 = 0;
1323 const uint8_t hdmi2 = 1;
1324 const uint8_t hdmi3 = 2;
1325
1326 mFakePolicy->clearViewports();
1327 // Add a viewport that's associated with some display port that's not of interest.
1328 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1329 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1330 // Add another viewport, connected to HDMI1 port
1331 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1332 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1333
1334 // Check that correct display viewport was returned by comparing the display ports.
1335 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1336 ASSERT_TRUE(hdmi1Viewport);
1337 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1338 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1339
1340 // Check that we can still get the same viewport using the uniqueId
1341 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1342 ASSERT_TRUE(hdmi1Viewport);
1343 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1344 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1345 ASSERT_EQ(type, hdmi1Viewport->type);
1346
1347 // Check that we cannot find a port with "HDMI2", because we never added one
1348 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1349 ASSERT_FALSE(hdmi2Viewport);
1350}
1351
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352// --- InputReaderTest ---
1353
1354class InputReaderTest : public testing::Test {
1355protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001356 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001357 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001358 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001359 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001360
Prabir Pradhan28efc192019-11-05 01:10:04 +00001361 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001362 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001363 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001364 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365
Prabir Pradhan28efc192019-11-05 01:10:04 +00001366 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1367 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001368 }
1369
Prabir Pradhan28efc192019-11-05 01:10:04 +00001370 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001371 mFakeListener.clear();
1372 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373 }
1374
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001375 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1376 const PropertyMap* configuration) {
1377 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378
1379 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001380 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 }
1382 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001383 mReader->loopOnce();
1384 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001385 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1386 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 }
1388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001389 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001390 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001391 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001392 }
1393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001394 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001395 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001396 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001397 }
1398
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001399 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001400 const std::string& name, uint32_t classes,
1401 uint32_t sources,
1402 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001403 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1404 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001405 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001406 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001407 return mapper;
1408 }
1409};
1410
1411TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001412 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001413 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001414 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001415 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001417 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001419 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001420 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001421 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001422 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1423 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1424 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1425
1426 // Should also have received a notification describing the new input devices.
1427 inputDevices = mFakePolicy->getInputDevices();
1428 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001429 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001430 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001431 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1432 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1433 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1434}
1435
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001436TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001437 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001438 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001439 constexpr int32_t eventHubId = 1;
1440 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001441 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001442 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001443 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001444 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001445
Yi Kong9b14ac62018-07-17 13:48:38 -07001446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001447
1448 NotifyDeviceResetArgs resetArgs;
1449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001450 ASSERT_EQ(deviceId, resetArgs.deviceId);
1451
1452 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001453 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001454 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001455
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001457 ASSERT_EQ(deviceId, resetArgs.deviceId);
1458 ASSERT_EQ(device->isEnabled(), false);
1459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001460 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001461 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001464 ASSERT_EQ(device->isEnabled(), false);
1465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001466 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001467 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001469 ASSERT_EQ(deviceId, resetArgs.deviceId);
1470 ASSERT_EQ(device->isEnabled(), true);
1471}
1472
Michael Wrightd02c5b62014-02-10 15:10:22 -08001473TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001474 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1475 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1476 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001477 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001478 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001479 AINPUT_SOURCE_KEYBOARD, nullptr);
1480 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481
1482 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1483 AINPUT_SOURCE_ANY, AKEYCODE_A))
1484 << "Should return unknown when the device id is >= 0 but unknown.";
1485
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001486 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1487 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1488 << "Should return unknown when the device id is valid but the sources are not "
1489 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001490
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001491 ASSERT_EQ(AKEY_STATE_DOWN,
1492 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1493 AKEYCODE_A))
1494 << "Should return value provided by mapper when device id is valid and the device "
1495 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001496
1497 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1498 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1499 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1500
1501 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1502 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1503 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1504}
1505
1506TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001507 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1508 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1509 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001510 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001511 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001512 AINPUT_SOURCE_KEYBOARD, nullptr);
1513 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514
1515 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1516 AINPUT_SOURCE_ANY, KEY_A))
1517 << "Should return unknown when the device id is >= 0 but unknown.";
1518
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1520 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1521 << "Should return unknown when the device id is valid but the sources are not "
1522 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001523
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 ASSERT_EQ(AKEY_STATE_DOWN,
1525 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1526 KEY_A))
1527 << "Should return value provided by mapper when device id is valid and the device "
1528 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529
1530 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1531 AINPUT_SOURCE_TRACKBALL, KEY_A))
1532 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1533
1534 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1535 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1536 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1537}
1538
1539TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001540 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1541 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1542 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001543 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001544 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001545 AINPUT_SOURCE_KEYBOARD, nullptr);
1546 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547
1548 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1549 AINPUT_SOURCE_ANY, SW_LID))
1550 << "Should return unknown when the device id is >= 0 but unknown.";
1551
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1553 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1554 << "Should return unknown when the device id is valid but the sources are not "
1555 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001557 ASSERT_EQ(AKEY_STATE_DOWN,
1558 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1559 SW_LID))
1560 << "Should return value provided by mapper when device id is valid and the device "
1561 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562
1563 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1564 AINPUT_SOURCE_TRACKBALL, SW_LID))
1565 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1566
1567 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1568 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1569 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1570}
1571
1572TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001573 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1574 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1575 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001576 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001577 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001578 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001580 mapper.addSupportedKeyCode(AKEYCODE_A);
1581 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582
1583 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1584 uint8_t flags[4] = { 0, 0, 0, 1 };
1585
1586 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1587 << "Should return false when device id is >= 0 but unknown.";
1588 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1589
1590 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1592 << "Should return false when device id is valid but the sources are not supported by "
1593 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1595
1596 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001597 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1598 keyCodes, flags))
1599 << "Should return value provided by mapper when device id is valid and the device "
1600 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1602
1603 flags[3] = 1;
1604 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1605 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1606 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1607
1608 flags[3] = 1;
1609 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1610 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1611 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1612}
1613
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001614TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 constexpr int32_t eventHubId = 1;
1616 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617
1618 NotifyConfigurationChangedArgs args;
1619
1620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1621 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1622}
1623
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001624TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1626 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1627 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001628 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001630 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001633 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1635
1636 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001637 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001638 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 ASSERT_EQ(EV_KEY, event.type);
1641 ASSERT_EQ(KEY_A, event.code);
1642 ASSERT_EQ(1, event.value);
1643}
1644
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001645TEST_F(InputReaderTest, DeviceReset_IncrementsId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001647 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 constexpr int32_t eventHubId = 1;
1649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001650 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001651 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001652 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001654
1655 NotifyDeviceResetArgs resetArgs;
1656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001657 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001658
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001659 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001660 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001662 ASSERT_TRUE(prevId < resetArgs.id);
1663 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001664
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001665 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001666 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001668 ASSERT_TRUE(prevId < resetArgs.id);
1669 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001670
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001671 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001672 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001674 ASSERT_TRUE(prevId < resetArgs.id);
1675 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001676}
1677
Arthur Hungc23540e2018-11-29 20:42:11 +08001678TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001679 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001680 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001682 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1684 FakeInputMapper& mapper =
1685 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001686 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001687
1688 const uint8_t hdmi1 = 1;
1689
1690 // Associated touch screen with second display.
1691 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1692
1693 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001694 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001695 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1696 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1697 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1698 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1699 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001700 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001701
1702 // Add the device, and make sure all of the callbacks are triggered.
1703 // The device is added after the input port associations are processed since
1704 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001705 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001708 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001709
Arthur Hung2c9a3342019-07-23 14:18:59 +08001710 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001711 ASSERT_EQ(deviceId, device->getId());
1712 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1713 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001714
1715 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001716 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001717 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001718 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001719}
1720
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001721// --- InputReaderIntegrationTest ---
1722
1723// These tests create and interact with the InputReader only through its interface.
1724// The InputReader is started during SetUp(), which starts its processing in its own
1725// thread. The tests use linux uinput to emulate input devices.
1726// NOTE: Interacting with the physical device while these tests are running may cause
1727// the tests to fail.
1728class InputReaderIntegrationTest : public testing::Test {
1729protected:
1730 sp<TestInputListener> mTestListener;
1731 sp<FakeInputReaderPolicy> mFakePolicy;
1732 sp<InputReaderInterface> mReader;
1733
1734 virtual void SetUp() override {
1735 mFakePolicy = new FakeInputReaderPolicy();
1736 mTestListener = new TestInputListener();
1737
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001738 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001739 ASSERT_EQ(mReader->start(), OK);
1740
1741 // Since this test is run on a real device, all the input devices connected
1742 // to the test device will show up in mReader. We wait for those input devices to
1743 // show up before beginning the tests.
1744 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1745 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1746 }
1747
1748 virtual void TearDown() override {
1749 ASSERT_EQ(mReader->stop(), OK);
1750 mTestListener.clear();
1751 mFakePolicy.clear();
1752 }
1753};
1754
1755TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1756 // An invalid input device that is only used for this test.
1757 class InvalidUinputDevice : public UinputDevice {
1758 public:
1759 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1760
1761 private:
1762 void configureDevice(int fd, uinput_user_dev* device) override {}
1763 };
1764
1765 const size_t numDevices = mFakePolicy->getInputDevices().size();
1766
1767 // UinputDevice does not set any event or key bits, so InputReader should not
1768 // consider it as a valid device.
1769 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1770 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1771 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1772 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1773
1774 invalidDevice.reset();
1775 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1777 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1778}
1779
1780TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1781 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1782
1783 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1784 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1785 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1786 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1787
1788 // Find the test device by its name.
1789 std::vector<InputDeviceInfo> inputDevices;
1790 mReader->getInputDevices(inputDevices);
1791 InputDeviceInfo* keyboardInfo = nullptr;
1792 const char* keyboardName = keyboard->getName();
1793 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1794 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1795 keyboardInfo = &inputDevices[i];
1796 break;
1797 }
1798 }
1799 ASSERT_NE(keyboardInfo, nullptr);
1800 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1801 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1802 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1803
1804 keyboard.reset();
1805 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1806 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1807 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1808}
1809
1810TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1811 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1812 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1813
1814 NotifyConfigurationChangedArgs configChangedArgs;
1815 ASSERT_NO_FATAL_FAILURE(
1816 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001817 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001818 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1819
1820 NotifyKeyArgs keyArgs;
1821 keyboard->pressAndReleaseHomeKey();
1822 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1823 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001824 ASSERT_LT(prevId, keyArgs.id);
1825 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001826 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1827 prevTimestamp = keyArgs.eventTime;
1828
1829 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1830 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001831 ASSERT_LT(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001832 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1833}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001834
1835// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001836class InputDeviceTest : public testing::Test {
1837protected:
1838 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001839 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001840 static const int32_t DEVICE_ID;
1841 static const int32_t DEVICE_GENERATION;
1842 static const int32_t DEVICE_CONTROLLER_NUMBER;
1843 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001845
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001846 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001847 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001848 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849 FakeInputReaderContext* mFakeContext;
1850
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001851 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852
Prabir Pradhan28efc192019-11-05 01:10:04 +00001853 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001854 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001855 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001856 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001859 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860 InputDeviceIdentifier identifier;
1861 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001862 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001863 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1864 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865 }
1866
Prabir Pradhan28efc192019-11-05 01:10:04 +00001867 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001868 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 delete mFakeContext;
1870 mFakeListener.clear();
1871 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001872 }
1873};
1874
1875const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001876const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001878const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1879const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1880const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1881 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001882const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001883
1884TEST_F(InputDeviceTest, ImmutableProperties) {
1885 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001886 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001887 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001888}
1889
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001890TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
1891 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001892}
1893
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1895 // Configuration.
1896 InputReaderConfiguration config;
1897 mDevice->configure(ARBITRARY_TIME, &config, 0);
1898
1899 // Reset.
1900 mDevice->reset(ARBITRARY_TIME);
1901
1902 NotifyDeviceResetArgs resetArgs;
1903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1904 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1905 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1906
1907 // Metadata.
1908 ASSERT_TRUE(mDevice->isIgnored());
1909 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1910
1911 InputDeviceInfo info;
1912 mDevice->getDeviceInfo(&info);
1913 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001914 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1916 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1917
1918 // State queries.
1919 ASSERT_EQ(0, mDevice->getMetaState());
1920
1921 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1922 << "Ignored device should return unknown key code state.";
1923 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1924 << "Ignored device should return unknown scan code state.";
1925 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1926 << "Ignored device should return unknown switch state.";
1927
1928 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1929 uint8_t flags[2] = { 0, 1 };
1930 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1931 << "Ignored device should never mark any key codes.";
1932 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1933 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1934}
1935
1936TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1937 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001939
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001940 FakeInputMapper& mapper1 =
1941 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001942 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1943 mapper1.setMetaState(AMETA_ALT_ON);
1944 mapper1.addSupportedKeyCode(AKEYCODE_A);
1945 mapper1.addSupportedKeyCode(AKEYCODE_B);
1946 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1947 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1948 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
1949 mapper1.setScanCodeState(3, AKEY_STATE_UP);
1950 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001951
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001952 FakeInputMapper& mapper2 =
1953 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001954 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001955
1956 InputReaderConfiguration config;
1957 mDevice->configure(ARBITRARY_TIME, &config, 0);
1958
1959 String8 propertyValue;
1960 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1961 << "Device should have read configuration during configuration phase.";
1962 ASSERT_STREQ("value", propertyValue.string());
1963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001964 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
1965 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001966
1967 // Reset
1968 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001969 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
1970 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001971
1972 NotifyDeviceResetArgs resetArgs;
1973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1974 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1975 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1976
1977 // Metadata.
1978 ASSERT_FALSE(mDevice->isIgnored());
1979 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1980
1981 InputDeviceInfo info;
1982 mDevice->getDeviceInfo(&info);
1983 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001984 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001985 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1986 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1987
1988 // State queries.
1989 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1990 << "Should query mappers and combine meta states.";
1991
1992 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1993 << "Should return unknown key code state when source not supported.";
1994 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1995 << "Should return unknown scan code state when source not supported.";
1996 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1997 << "Should return unknown switch state when source not supported.";
1998
1999 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2000 << "Should query mapper when source is supported.";
2001 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2002 << "Should query mapper when source is supported.";
2003 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2004 << "Should query mapper when source is supported.";
2005
2006 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2007 uint8_t flags[4] = { 0, 0, 0, 1 };
2008 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2009 << "Should do nothing when source is unsupported.";
2010 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2011 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2012 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2013 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2014
2015 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2016 << "Should query mapper when source is supported.";
2017 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2018 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2019 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2020 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2021
2022 // Event handling.
2023 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002024 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002025 mDevice->process(&event, 1);
2026
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002027 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2028 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002029}
2030
Arthur Hung2c9a3342019-07-23 14:18:59 +08002031// A single input device is associated with a specific display. Check that:
2032// 1. Device is disabled if the viewport corresponding to the associated display is not found
2033// 2. Device is disabled when setEnabled API is called
2034TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002035 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002036
2037 // First Configuration.
2038 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2039
2040 // Device should be enabled by default.
2041 ASSERT_TRUE(mDevice->isEnabled());
2042
2043 // Prepare associated info.
2044 constexpr uint8_t hdmi = 1;
2045 const std::string UNIQUE_ID = "local:1";
2046
2047 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2048 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2049 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2050 // Device should be disabled because it is associated with a specific display via
2051 // input port <-> display port association, but the corresponding display is not found
2052 ASSERT_FALSE(mDevice->isEnabled());
2053
2054 // Prepare displays.
2055 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2056 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2057 ViewportType::VIEWPORT_INTERNAL);
2058 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2059 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2060 ASSERT_TRUE(mDevice->isEnabled());
2061
2062 // Device should be disabled after set disable.
2063 mFakePolicy->addDisabledDevice(mDevice->getId());
2064 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2065 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2066 ASSERT_FALSE(mDevice->isEnabled());
2067
2068 // Device should still be disabled even found the associated display.
2069 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2070 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2071 ASSERT_FALSE(mDevice->isEnabled());
2072}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002073
2074// --- InputMapperTest ---
2075
2076class InputMapperTest : public testing::Test {
2077protected:
2078 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002079 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080 static const int32_t DEVICE_ID;
2081 static const int32_t DEVICE_GENERATION;
2082 static const int32_t DEVICE_CONTROLLER_NUMBER;
2083 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002084 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002085
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002086 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002087 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002088 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002089 FakeInputReaderContext* mFakeContext;
2090 InputDevice* mDevice;
2091
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002092 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002093 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002094 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002095 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2097 InputDeviceIdentifier identifier;
2098 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002099 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002100 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002101
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002102 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002103 }
2104
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002105 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2106
Prabir Pradhan28efc192019-11-05 01:10:04 +00002107 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002108 delete mDevice;
2109 delete mFakeContext;
2110 mFakeListener.clear();
2111 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002112 }
2113
2114 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002115 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002116 }
2117
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002118 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002119 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2120 mFakeContext->updatePointerDisplay();
2121 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002122 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2123 }
2124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002125 template <class T, typename... Args>
2126 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002127 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002128 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002129 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002130 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002131 }
2132
2133 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002134 int32_t orientation, const std::string& uniqueId,
2135 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002136 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002137 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002138 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2139 }
2140
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002141 void clearViewports() {
2142 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002143 }
2144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002145 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2146 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002147 RawEvent event;
2148 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002149 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002150 event.type = type;
2151 event.code = code;
2152 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002153 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154 }
2155
2156 static void assertMotionRange(const InputDeviceInfo& info,
2157 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2158 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002159 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002160 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2161 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2162 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2163 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2164 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2165 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2166 }
2167
2168 static void assertPointerCoords(const PointerCoords& coords,
2169 float x, float y, float pressure, float size,
2170 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2171 float orientation, float distance) {
2172 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2173 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2174 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2175 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2176 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2177 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2178 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2179 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2180 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2181 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2182 }
2183
2184 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2185 float actualX, actualY;
2186 controller->getPosition(&actualX, &actualY);
2187 ASSERT_NEAR(x, actualX, 1);
2188 ASSERT_NEAR(y, actualY, 1);
2189 }
2190};
2191
2192const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002193const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002194const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002195const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2196const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2197const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002198const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002199
2200// --- SwitchInputMapperTest ---
2201
2202class SwitchInputMapperTest : public InputMapperTest {
2203protected:
2204};
2205
2206TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002207 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002209 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210}
2211
2212TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002213 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002214
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002215 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002216 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002217
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002218 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002219 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220}
2221
2222TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002223 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002224
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002225 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2226 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2227 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2228 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002229
2230 NotifySwitchArgs args;
2231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2232 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002233 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2234 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235 args.switchMask);
2236 ASSERT_EQ(uint32_t(0), args.policyFlags);
2237}
2238
2239
2240// --- KeyboardInputMapperTest ---
2241
2242class KeyboardInputMapperTest : public InputMapperTest {
2243protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002244 const std::string UNIQUE_ID = "local:0";
2245
2246 void prepareDisplay(int32_t orientation);
2247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002248 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002249 int32_t originalKeyCode, int32_t rotatedKeyCode,
2250 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002251};
2252
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002253/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2254 * orientation.
2255 */
2256void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2257 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002258 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002259}
2260
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002261void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002262 int32_t originalScanCode, int32_t originalKeyCode,
2263 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264 NotifyKeyArgs args;
2265
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002266 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2268 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2269 ASSERT_EQ(originalScanCode, args.scanCode);
2270 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002271 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002272
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002273 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2275 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2276 ASSERT_EQ(originalScanCode, args.scanCode);
2277 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002278 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279}
2280
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002282 KeyboardInputMapper& mapper =
2283 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2284 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002286 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002287}
2288
2289TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2290 const int32_t USAGE_A = 0x070004;
2291 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002292 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2293 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002294
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002295 KeyboardInputMapper& mapper =
2296 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2297 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002298
2299 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002300 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002301 NotifyKeyArgs args;
2302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2303 ASSERT_EQ(DEVICE_ID, args.deviceId);
2304 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2305 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2306 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2307 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2308 ASSERT_EQ(KEY_HOME, args.scanCode);
2309 ASSERT_EQ(AMETA_NONE, args.metaState);
2310 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2311 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2312 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2313
2314 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002315 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2317 ASSERT_EQ(DEVICE_ID, args.deviceId);
2318 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2319 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2320 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2321 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2322 ASSERT_EQ(KEY_HOME, args.scanCode);
2323 ASSERT_EQ(AMETA_NONE, args.metaState);
2324 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2325 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2326 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2327
2328 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002329 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2330 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2332 ASSERT_EQ(DEVICE_ID, args.deviceId);
2333 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2334 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2335 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2336 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2337 ASSERT_EQ(0, args.scanCode);
2338 ASSERT_EQ(AMETA_NONE, args.metaState);
2339 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2340 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2341 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2342
2343 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002344 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2345 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2347 ASSERT_EQ(DEVICE_ID, args.deviceId);
2348 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2349 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2350 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2351 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2352 ASSERT_EQ(0, args.scanCode);
2353 ASSERT_EQ(AMETA_NONE, args.metaState);
2354 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2355 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2356 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2357
2358 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002359 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2360 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2362 ASSERT_EQ(DEVICE_ID, args.deviceId);
2363 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2364 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2365 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2366 ASSERT_EQ(0, args.keyCode);
2367 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2368 ASSERT_EQ(AMETA_NONE, args.metaState);
2369 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2370 ASSERT_EQ(0U, args.policyFlags);
2371 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2372
2373 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002374 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2375 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2377 ASSERT_EQ(DEVICE_ID, args.deviceId);
2378 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2379 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2380 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2381 ASSERT_EQ(0, args.keyCode);
2382 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2383 ASSERT_EQ(AMETA_NONE, args.metaState);
2384 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2385 ASSERT_EQ(0U, args.policyFlags);
2386 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2387}
2388
2389TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002390 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2391 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002393 KeyboardInputMapper& mapper =
2394 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2395 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396
2397 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002398 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399
2400 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002401 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 NotifyKeyArgs args;
2403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2404 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2407
2408 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002409 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413
2414 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002415 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2417 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002418 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419
2420 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002421 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2423 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002424 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2426}
2427
2428TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002429 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2430 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2431 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2432 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002434 KeyboardInputMapper& mapper =
2435 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2436 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002438 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2440 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2441 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2442 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2443 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2444 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2445 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2446 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2447}
2448
2449TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002450 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2451 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2452 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2453 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002456 KeyboardInputMapper& mapper =
2457 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2458 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002460 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002461 ASSERT_NO_FATAL_FAILURE(
2462 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2463 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2464 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2465 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2466 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2467 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2468 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002470 clearViewports();
2471 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002472 ASSERT_NO_FATAL_FAILURE(
2473 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2474 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2475 AKEYCODE_DPAD_UP, DISPLAY_ID));
2476 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2477 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2478 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2479 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002481 clearViewports();
2482 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002483 ASSERT_NO_FATAL_FAILURE(
2484 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2485 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2486 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2487 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2488 AKEYCODE_DPAD_UP, DISPLAY_ID));
2489 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2490 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002492 clearViewports();
2493 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002494 ASSERT_NO_FATAL_FAILURE(
2495 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2496 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2497 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2498 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2499 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2500 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2501 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502
2503 // Special case: if orientation changes while key is down, we still emit the same keycode
2504 // in the key up as we did in the key down.
2505 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002506 clearViewports();
2507 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002508 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2510 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2511 ASSERT_EQ(KEY_UP, args.scanCode);
2512 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2513
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002514 clearViewports();
2515 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002516 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2518 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2519 ASSERT_EQ(KEY_UP, args.scanCode);
2520 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2521}
2522
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002523TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2524 // If the keyboard is not orientation aware,
2525 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002526 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002528 KeyboardInputMapper& mapper =
2529 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2530 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002531 NotifyKeyArgs args;
2532
2533 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002534 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002536 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2538 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2539
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002540 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002541 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002543 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2545 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2546}
2547
2548TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2549 // If the keyboard is orientation aware,
2550 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002552
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002553 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002554 KeyboardInputMapper& mapper =
2555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2556 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002557 NotifyKeyArgs args;
2558
2559 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2560 // ^--- already checked by the previous test
2561
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002562 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002563 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002564 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002566 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2568 ASSERT_EQ(DISPLAY_ID, args.displayId);
2569
2570 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002571 clearViewports();
2572 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002573 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002574 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002576 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2578 ASSERT_EQ(newDisplayId, args.displayId);
2579}
2580
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002582 KeyboardInputMapper& mapper =
2583 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2584 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002586 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002587 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002588
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002589 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002590 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591}
2592
2593TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002594 KeyboardInputMapper& mapper =
2595 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2596 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002598 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002599 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002601 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002602 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603}
2604
2605TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002606 KeyboardInputMapper& mapper =
2607 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2608 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611
2612 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2613 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002614 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002615 ASSERT_TRUE(flags[0]);
2616 ASSERT_FALSE(flags[1]);
2617}
2618
2619TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002620 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2621 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2622 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2623 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2624 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2625 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002627 KeyboardInputMapper& mapper =
2628 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2629 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630
2631 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002632 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2633 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2634 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
2636 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002637 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2638 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002639 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2640 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2641 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002642 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643
2644 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002645 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2646 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002647 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2648 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2649 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002650 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
2652 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002653 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2654 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002655 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2656 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2657 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002658 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659
2660 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002661 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2662 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002663 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2664 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2665 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002666 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667
2668 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002669 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2670 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002671 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2672 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2673 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002674 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675
2676 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002677 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2678 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002679 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2680 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2681 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002682 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683}
2684
Arthur Hung2c9a3342019-07-23 14:18:59 +08002685TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2686 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002687 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2688 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2689 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002691
2692 // keyboard 2.
2693 const std::string USB2 = "USB2";
2694 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002695 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002696 InputDeviceIdentifier identifier;
2697 identifier.name = "KEYBOARD2";
2698 identifier.location = USB2;
2699 std::unique_ptr<InputDevice> device2 =
2700 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002701 identifier);
2702 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2703 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2704 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2705 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2706 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002708 KeyboardInputMapper& mapper =
2709 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2710 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002711
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002712 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002713 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002714 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002715 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2716 device2->reset(ARBITRARY_TIME);
2717
2718 // Prepared displays and associated info.
2719 constexpr uint8_t hdmi1 = 0;
2720 constexpr uint8_t hdmi2 = 1;
2721 const std::string SECONDARY_UNIQUE_ID = "local:1";
2722
2723 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2724 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2725
2726 // No associated display viewport found, should disable the device.
2727 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2728 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2729 ASSERT_FALSE(device2->isEnabled());
2730
2731 // Prepare second display.
2732 constexpr int32_t newDisplayId = 2;
2733 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2734 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2735 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2736 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2737 // Default device will reconfigure above, need additional reconfiguration for another device.
2738 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2739 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2740
2741 // Device should be enabled after the associated display is found.
2742 ASSERT_TRUE(mDevice->isEnabled());
2743 ASSERT_TRUE(device2->isEnabled());
2744
2745 // Test pad key events
2746 ASSERT_NO_FATAL_FAILURE(
2747 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2748 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2749 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2750 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2751 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2752 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2753 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2754
2755 ASSERT_NO_FATAL_FAILURE(
2756 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2757 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2758 AKEYCODE_DPAD_RIGHT, newDisplayId));
2759 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2760 AKEYCODE_DPAD_DOWN, newDisplayId));
2761 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2762 AKEYCODE_DPAD_LEFT, newDisplayId));
2763}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002765// --- KeyboardInputMapperTest_ExternalDevice ---
2766
2767class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2768protected:
2769 virtual void SetUp() override {
2770 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2771 }
2772};
2773
2774TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002775 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2776 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002777
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002778 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2779 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2780 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2781 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002782
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002783 KeyboardInputMapper& mapper =
2784 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2785 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002786
2787 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2788 NotifyKeyArgs args;
2789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2790 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2791
2792 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2794 ASSERT_EQ(uint32_t(0), args.policyFlags);
2795
2796 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2798 ASSERT_EQ(uint32_t(0), args.policyFlags);
2799
2800 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2802 ASSERT_EQ(uint32_t(0), args.policyFlags);
2803
2804 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2806 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2807
2808 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2810 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2811}
2812
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002813TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002814 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2817 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2818 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002819
Powei Fengd041c5d2019-05-03 17:11:33 -07002820 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002821 KeyboardInputMapper& mapper =
2822 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2823 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002824
2825 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2826 NotifyKeyArgs args;
2827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2828 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2829
2830 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2832 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2833
2834 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2836 ASSERT_EQ(uint32_t(0), args.policyFlags);
2837
2838 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2840 ASSERT_EQ(uint32_t(0), args.policyFlags);
2841
2842 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2844 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2845
2846 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2848 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2849}
2850
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851// --- CursorInputMapperTest ---
2852
2853class CursorInputMapperTest : public InputMapperTest {
2854protected:
2855 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2856
2857 sp<FakePointerController> mFakePointerController;
2858
Prabir Pradhan28efc192019-11-05 01:10:04 +00002859 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860 InputMapperTest::SetUp();
2861
2862 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002863 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864 }
2865
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002866 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
2867 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002868
2869 void prepareDisplay(int32_t orientation) {
2870 const std::string uniqueId = "local:0";
2871 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2872 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2873 orientation, uniqueId, NO_PORT, viewportType);
2874 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002875};
2876
2877const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002879void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
2880 int32_t originalY, int32_t rotatedX,
2881 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882 NotifyMotionArgs args;
2883
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002884 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2885 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2886 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2890 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2891 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2892 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2893}
2894
2895TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002896 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002897 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002898
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002899 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002900}
2901
2902TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002904 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002906 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907}
2908
2909TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002911 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912
2913 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002914 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
2916 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002917 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2918 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2920 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2921
2922 // When the bounds are set, then there should be a valid motion range.
2923 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2924
2925 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002926 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
2928 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2929 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2930 1, 800 - 1, 0.0f, 0.0f));
2931 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2932 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2933 2, 480 - 1, 0.0f, 0.0f));
2934 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2935 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2936 0.0f, 1.0f, 0.0f, 0.0f));
2937}
2938
2939TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002941 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942
2943 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002944 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945
2946 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2947 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2948 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2949 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2950 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2951 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2952 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2953 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2954 0.0f, 1.0f, 0.0f, 0.0f));
2955}
2956
2957TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002959 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960
2961 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2962
2963 NotifyMotionArgs args;
2964
2965 // Button press.
2966 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002967 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2968 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2970 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2971 ASSERT_EQ(DEVICE_ID, args.deviceId);
2972 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2973 ASSERT_EQ(uint32_t(0), args.policyFlags);
2974 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2975 ASSERT_EQ(0, args.flags);
2976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2977 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2978 ASSERT_EQ(0, args.edgeFlags);
2979 ASSERT_EQ(uint32_t(1), args.pointerCount);
2980 ASSERT_EQ(0, args.pointerProperties[0].id);
2981 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2983 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2984 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2985 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2986 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2987
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2989 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2990 ASSERT_EQ(DEVICE_ID, args.deviceId);
2991 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2992 ASSERT_EQ(uint32_t(0), args.policyFlags);
2993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2994 ASSERT_EQ(0, args.flags);
2995 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2996 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2997 ASSERT_EQ(0, args.edgeFlags);
2998 ASSERT_EQ(uint32_t(1), args.pointerCount);
2999 ASSERT_EQ(0, args.pointerProperties[0].id);
3000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3002 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3003 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3004 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3005 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3006
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003008 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3009 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3011 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3012 ASSERT_EQ(DEVICE_ID, args.deviceId);
3013 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3014 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3016 ASSERT_EQ(0, args.flags);
3017 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3018 ASSERT_EQ(0, args.buttonState);
3019 ASSERT_EQ(0, args.edgeFlags);
3020 ASSERT_EQ(uint32_t(1), args.pointerCount);
3021 ASSERT_EQ(0, args.pointerProperties[0].id);
3022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3024 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3025 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3026 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3027 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3028
3029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3030 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3031 ASSERT_EQ(DEVICE_ID, args.deviceId);
3032 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3033 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3035 ASSERT_EQ(0, args.flags);
3036 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3037 ASSERT_EQ(0, args.buttonState);
3038 ASSERT_EQ(0, args.edgeFlags);
3039 ASSERT_EQ(uint32_t(1), args.pointerCount);
3040 ASSERT_EQ(0, args.pointerProperties[0].id);
3041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3043 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3044 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3045 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3046 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3047}
3048
3049TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003051 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052
3053 NotifyMotionArgs args;
3054
3055 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003056 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3057 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3061 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3062
3063 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003064 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3065 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3069 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3070}
3071
3072TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075
3076 NotifyMotionArgs args;
3077
3078 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003079 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3080 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3082 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3084 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3085
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3089 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3090
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003092 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3093 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3097 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3098
3099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3102 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3103}
3104
3105TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003106 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003107 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
3109 NotifyMotionArgs args;
3110
3111 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003112 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3113 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3114 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3115 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3117 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3119 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3120 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3121
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3123 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3125 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3126 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3127
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003129 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3130 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3131 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3135 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3136 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3137
3138 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003139 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3140 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3144 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3145
3146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3149 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3150}
3151
3152TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003154 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003156 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3165}
3166
3167TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 addConfigurationProperty("cursor.mode", "navigation");
3169 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003170 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003172 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3181
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003182 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3191
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003192 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3201
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003202 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3211}
3212
3213TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003215 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216
3217 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3218 mFakePointerController->setPosition(100, 200);
3219 mFakePointerController->setButtonState(0);
3220
3221 NotifyMotionArgs motionArgs;
3222 NotifyKeyArgs keyArgs;
3223
3224 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003225 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3226 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3228 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3229 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3230 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3232 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3233
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3236 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3237 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3239 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3240
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003241 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3242 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245 ASSERT_EQ(0, motionArgs.buttonState);
3246 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3248 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3249
3250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003251 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252 ASSERT_EQ(0, motionArgs.buttonState);
3253 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3255 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3256
3257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003259 ASSERT_EQ(0, motionArgs.buttonState);
3260 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3262 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3263
3264 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003265 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3266 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3267 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3270 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3271 motionArgs.buttonState);
3272 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3273 mFakePointerController->getButtonState());
3274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3275 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3276
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3279 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3280 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3281 mFakePointerController->getButtonState());
3282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3283 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3284
3285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3287 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3288 motionArgs.buttonState);
3289 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3290 mFakePointerController->getButtonState());
3291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3292 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3293
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003294 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3295 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
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_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3299 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3301 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3302
3303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003305 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3306 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3308 100.0f, 200.0f, 1.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_MIDDLE, 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);
3314 ASSERT_EQ(0, motionArgs.buttonState);
3315 ASSERT_EQ(0, mFakePointerController->getButtonState());
3316 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));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003318 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3319 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003320
3321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322 ASSERT_EQ(0, motionArgs.buttonState);
3323 ASSERT_EQ(0, mFakePointerController->getButtonState());
3324 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3326 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 -08003327
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3329 ASSERT_EQ(0, motionArgs.buttonState);
3330 ASSERT_EQ(0, mFakePointerController->getButtonState());
3331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3333 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3334
3335 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003336 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3337 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3339 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3340 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003341
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3345 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3347 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3348
3349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3351 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3352 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3354 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3355
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003356 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3357 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003359 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360 ASSERT_EQ(0, motionArgs.buttonState);
3361 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3363 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3364
3365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003367 ASSERT_EQ(0, motionArgs.buttonState);
3368 ASSERT_EQ(0, mFakePointerController->getButtonState());
3369
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3371 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3373 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3374 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3375
3376 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003377 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3378 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3380 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3381 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003382
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003384 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3386 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3388 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3389
3390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3392 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3393 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3395 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3396
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003397 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3398 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401 ASSERT_EQ(0, motionArgs.buttonState);
3402 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3404 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 -08003405
3406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3407 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3408 ASSERT_EQ(0, motionArgs.buttonState);
3409 ASSERT_EQ(0, mFakePointerController->getButtonState());
3410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3411 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3412
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3414 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3415 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3416
3417 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003418 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3419 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3421 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3422 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003423
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3427 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3429 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3430
3431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3433 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3434 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3436 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3437
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003438 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3439 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442 ASSERT_EQ(0, motionArgs.buttonState);
3443 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3445 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 -08003446
3447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3448 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3449 ASSERT_EQ(0, motionArgs.buttonState);
3450 ASSERT_EQ(0, mFakePointerController->getButtonState());
3451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3452 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3453
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3455 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3456 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3457
3458 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003459 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3460 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3462 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3463 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003464
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003466 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3468 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3470 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3471
3472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3473 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3474 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3475 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3477 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3478
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003479 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3480 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003482 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_EQ(0, motionArgs.buttonState);
3484 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3486 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 -08003487
3488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3490 ASSERT_EQ(0, motionArgs.buttonState);
3491 ASSERT_EQ(0, mFakePointerController->getButtonState());
3492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3493 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3494
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3496 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3497 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3498}
3499
3500TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003502 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
3504 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3505 mFakePointerController->setPosition(100, 200);
3506 mFakePointerController->setButtonState(0);
3507
3508 NotifyMotionArgs args;
3509
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003510 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3511 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3512 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003514 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3515 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3517 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3518 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3519}
3520
3521TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003522 addConfigurationProperty("cursor.mode", "pointer");
3523 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003524 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003525
3526 NotifyDeviceResetArgs resetArgs;
3527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3528 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3529 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3530
3531 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3532 mFakePointerController->setPosition(100, 200);
3533 mFakePointerController->setButtonState(0);
3534
3535 NotifyMotionArgs args;
3536
3537 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003538 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3539 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3540 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3542 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3545 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3546 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3547
3548 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003549 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3550 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3552 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3553 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3555 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3557 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3560 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3561
3562 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003563 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3564 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3566 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3567 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3569 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3571 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3574 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3575
3576 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003577 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3578 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3579 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3581 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3584 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3585 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3586
3587 // Disable pointer capture and check that the device generation got bumped
3588 // and events are generated the usual way.
3589 const uint32_t generation = mFakeContext->getGeneration();
3590 mFakePolicy->setPointerCapture(false);
3591 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3592 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3593
3594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3595 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3596 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3597
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003598 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3599 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3600 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3602 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3605 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3606 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3607}
3608
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003609TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003610 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003611
Garfield Tan888a6a42020-01-09 11:39:16 -08003612 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003613 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003614 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3615 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3616 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3617 ViewportType::VIEWPORT_EXTERNAL);
3618 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3619 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3620
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003621 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3622 mFakePointerController->setPosition(100, 200);
3623 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003624
3625 NotifyMotionArgs args;
3626 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3627 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3628 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3630 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3631 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3633 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3634 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3635 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3636}
3637
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638// --- TouchInputMapperTest ---
3639
3640class TouchInputMapperTest : public InputMapperTest {
3641protected:
3642 static const int32_t RAW_X_MIN;
3643 static const int32_t RAW_X_MAX;
3644 static const int32_t RAW_Y_MIN;
3645 static const int32_t RAW_Y_MAX;
3646 static const int32_t RAW_TOUCH_MIN;
3647 static const int32_t RAW_TOUCH_MAX;
3648 static const int32_t RAW_TOOL_MIN;
3649 static const int32_t RAW_TOOL_MAX;
3650 static const int32_t RAW_PRESSURE_MIN;
3651 static const int32_t RAW_PRESSURE_MAX;
3652 static const int32_t RAW_ORIENTATION_MIN;
3653 static const int32_t RAW_ORIENTATION_MAX;
3654 static const int32_t RAW_DISTANCE_MIN;
3655 static const int32_t RAW_DISTANCE_MAX;
3656 static const int32_t RAW_TILT_MIN;
3657 static const int32_t RAW_TILT_MAX;
3658 static const int32_t RAW_ID_MIN;
3659 static const int32_t RAW_ID_MAX;
3660 static const int32_t RAW_SLOT_MIN;
3661 static const int32_t RAW_SLOT_MAX;
3662 static const float X_PRECISION;
3663 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003664 static const float X_PRECISION_VIRTUAL;
3665 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666
3667 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003668 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669
3670 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3671
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003672 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003673 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003674
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675 enum Axes {
3676 POSITION = 1 << 0,
3677 TOUCH = 1 << 1,
3678 TOOL = 1 << 2,
3679 PRESSURE = 1 << 3,
3680 ORIENTATION = 1 << 4,
3681 MINOR = 1 << 5,
3682 ID = 1 << 6,
3683 DISTANCE = 1 << 7,
3684 TILT = 1 << 8,
3685 SLOT = 1 << 9,
3686 TOOL_TYPE = 1 << 10,
3687 };
3688
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003689 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3690 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003691 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003693 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694 int32_t toRawX(float displayX);
3695 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003696 float toCookedX(float rawX, float rawY);
3697 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003699 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003701 float toDisplayY(int32_t rawY, int32_t displayHeight);
3702
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703};
3704
3705const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3706const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3707const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3708const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3709const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3710const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3711const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3712const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003713const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3714const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3716const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3717const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3718const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3719const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3720const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3721const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3722const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3723const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3724const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3725const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3726const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003727const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3728 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3729const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3730 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003731const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3732 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
3734const float TouchInputMapperTest::GEOMETRIC_SCALE =
3735 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3736 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3737
3738const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3739 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3740 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3741};
3742
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003743void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003744 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003745 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3746}
3747
3748void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3749 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3750 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751}
3752
Santos Cordonfa5cf462017-04-05 10:37:00 -07003753void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003754 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3755 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003756 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003757}
3758
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003760 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3761 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3762 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3763 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764}
3765
Jason Gerecke489fda82012-09-07 17:19:40 -07003766void TouchInputMapperTest::prepareLocationCalibration() {
3767 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3768}
3769
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770int32_t TouchInputMapperTest::toRawX(float displayX) {
3771 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3772}
3773
3774int32_t TouchInputMapperTest::toRawY(float displayY) {
3775 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3776}
3777
Jason Gerecke489fda82012-09-07 17:19:40 -07003778float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3779 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3780 return rawX;
3781}
3782
3783float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3784 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3785 return rawY;
3786}
3787
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003789 return toDisplayX(rawX, DISPLAY_WIDTH);
3790}
3791
3792float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3793 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794}
3795
3796float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003797 return toDisplayY(rawY, DISPLAY_HEIGHT);
3798}
3799
3800float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3801 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802}
3803
3804
3805// --- SingleTouchInputMapperTest ---
3806
3807class SingleTouchInputMapperTest : public TouchInputMapperTest {
3808protected:
3809 void prepareButtons();
3810 void prepareAxes(int axes);
3811
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003812 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3813 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3814 void processUp(SingleTouchInputMapper& mappery);
3815 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3816 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3817 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3818 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3819 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3820 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821};
3822
3823void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003824 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003825}
3826
3827void SingleTouchInputMapperTest::prepareAxes(int axes) {
3828 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003829 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3830 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831 }
3832 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003833 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
3834 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835 }
3836 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003837 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
3838 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839 }
3840 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003841 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
3842 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843 }
3844 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003845 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3846 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847 }
3848}
3849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003850void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003851 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3852 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3853 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854}
3855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003857 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3858 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859}
3860
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003861void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003862 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863}
3864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003866 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867}
3868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003869void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
3870 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003871 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872}
3873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003874void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003875 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876}
3877
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003878void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
3879 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003880 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3881 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882}
3883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003884void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
3885 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003886 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887}
3888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003889void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003890 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891}
3892
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003894 prepareButtons();
3895 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003896 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003898 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899}
3900
3901TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003902 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
3903 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904 prepareButtons();
3905 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003906 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003907
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003908 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909}
3910
3911TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003912 prepareButtons();
3913 prepareAxes(POSITION);
3914 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003915 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003917 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918}
3919
3920TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921 prepareButtons();
3922 prepareAxes(POSITION);
3923 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003924 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003926 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927}
3928
3929TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003930 addConfigurationProperty("touch.deviceType", "touchScreen");
3931 prepareDisplay(DISPLAY_ORIENTATION_0);
3932 prepareButtons();
3933 prepareAxes(POSITION);
3934 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003935 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
3937 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003938 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939
3940 // Virtual key is down.
3941 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3942 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3943 processDown(mapper, x, y);
3944 processSync(mapper);
3945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003947 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948
3949 // Virtual key is up.
3950 processUp(mapper);
3951 processSync(mapper);
3952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003954 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955}
3956
3957TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958 addConfigurationProperty("touch.deviceType", "touchScreen");
3959 prepareDisplay(DISPLAY_ORIENTATION_0);
3960 prepareButtons();
3961 prepareAxes(POSITION);
3962 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003963 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964
3965 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003966 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967
3968 // Virtual key is down.
3969 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3970 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3971 processDown(mapper, x, y);
3972 processSync(mapper);
3973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003975 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976
3977 // Virtual key is up.
3978 processUp(mapper);
3979 processSync(mapper);
3980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3981
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003982 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983}
3984
3985TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986 addConfigurationProperty("touch.deviceType", "touchScreen");
3987 prepareDisplay(DISPLAY_ORIENTATION_0);
3988 prepareButtons();
3989 prepareAxes(POSITION);
3990 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003991 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992
3993 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3994 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996 ASSERT_TRUE(flags[0]);
3997 ASSERT_FALSE(flags[1]);
3998}
3999
4000TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 addConfigurationProperty("touch.deviceType", "touchScreen");
4002 prepareDisplay(DISPLAY_ORIENTATION_0);
4003 prepareButtons();
4004 prepareAxes(POSITION);
4005 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004006 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007
4008 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4009
4010 NotifyKeyArgs args;
4011
4012 // Press virtual key.
4013 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4014 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4015 processDown(mapper, x, y);
4016 processSync(mapper);
4017
4018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4019 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4020 ASSERT_EQ(DEVICE_ID, args.deviceId);
4021 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4022 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4023 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4024 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4025 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4026 ASSERT_EQ(KEY_HOME, args.scanCode);
4027 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4028 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4029
4030 // Release virtual key.
4031 processUp(mapper);
4032 processSync(mapper);
4033
4034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4035 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4036 ASSERT_EQ(DEVICE_ID, args.deviceId);
4037 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4038 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4039 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4040 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4041 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4042 ASSERT_EQ(KEY_HOME, args.scanCode);
4043 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4044 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4045
4046 // Should not have sent any motions.
4047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4048}
4049
4050TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051 addConfigurationProperty("touch.deviceType", "touchScreen");
4052 prepareDisplay(DISPLAY_ORIENTATION_0);
4053 prepareButtons();
4054 prepareAxes(POSITION);
4055 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004056 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057
4058 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4059
4060 NotifyKeyArgs keyArgs;
4061
4062 // Press virtual key.
4063 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4064 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4065 processDown(mapper, x, y);
4066 processSync(mapper);
4067
4068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4069 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4070 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4071 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4072 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4073 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4074 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4075 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4076 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4077 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4078 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4079
4080 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4081 // into the display area.
4082 y -= 100;
4083 processMove(mapper, x, y);
4084 processSync(mapper);
4085
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4087 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4088 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4089 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4090 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4091 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4092 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4093 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4094 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4095 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4097 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4098
4099 NotifyMotionArgs motionArgs;
4100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4102 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4103 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4104 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4105 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4106 ASSERT_EQ(0, motionArgs.flags);
4107 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4108 ASSERT_EQ(0, motionArgs.buttonState);
4109 ASSERT_EQ(0, motionArgs.edgeFlags);
4110 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4111 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4114 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4115 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4116 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4117 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4118
4119 // Keep moving out of bounds. Should generate a pointer move.
4120 y -= 50;
4121 processMove(mapper, x, y);
4122 processSync(mapper);
4123
4124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4125 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4126 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4127 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4128 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4130 ASSERT_EQ(0, motionArgs.flags);
4131 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4132 ASSERT_EQ(0, motionArgs.buttonState);
4133 ASSERT_EQ(0, motionArgs.edgeFlags);
4134 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4135 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4138 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4139 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4140 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4141 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4142
4143 // Release out of bounds. Should generate a pointer up.
4144 processUp(mapper);
4145 processSync(mapper);
4146
4147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4148 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4149 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4150 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4151 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4152 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4153 ASSERT_EQ(0, motionArgs.flags);
4154 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4155 ASSERT_EQ(0, motionArgs.buttonState);
4156 ASSERT_EQ(0, motionArgs.edgeFlags);
4157 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4158 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4161 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4162 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4163 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4164 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4165
4166 // Should not have sent any more keys or motions.
4167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4169}
4170
4171TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 addConfigurationProperty("touch.deviceType", "touchScreen");
4173 prepareDisplay(DISPLAY_ORIENTATION_0);
4174 prepareButtons();
4175 prepareAxes(POSITION);
4176 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004177 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178
4179 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4180
4181 NotifyMotionArgs motionArgs;
4182
4183 // Initially go down out of bounds.
4184 int32_t x = -10;
4185 int32_t y = -10;
4186 processDown(mapper, x, y);
4187 processSync(mapper);
4188
4189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4190
4191 // Move into the display area. Should generate a pointer down.
4192 x = 50;
4193 y = 75;
4194 processMove(mapper, x, y);
4195 processSync(mapper);
4196
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4198 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4199 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4200 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4201 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4203 ASSERT_EQ(0, motionArgs.flags);
4204 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4205 ASSERT_EQ(0, motionArgs.buttonState);
4206 ASSERT_EQ(0, motionArgs.edgeFlags);
4207 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4208 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4211 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4212 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4213 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4214 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4215
4216 // Release. Should generate a pointer up.
4217 processUp(mapper);
4218 processSync(mapper);
4219
4220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4221 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4222 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4223 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4224 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4225 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4226 ASSERT_EQ(0, motionArgs.flags);
4227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4228 ASSERT_EQ(0, motionArgs.buttonState);
4229 ASSERT_EQ(0, motionArgs.edgeFlags);
4230 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4231 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4234 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4235 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4236 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4238
4239 // Should not have sent any more keys or motions.
4240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4242}
4243
Santos Cordonfa5cf462017-04-05 10:37:00 -07004244TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004245 addConfigurationProperty("touch.deviceType", "touchScreen");
4246 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4247
4248 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4249 prepareButtons();
4250 prepareAxes(POSITION);
4251 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004252 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004253
4254 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4255
4256 NotifyMotionArgs motionArgs;
4257
4258 // Down.
4259 int32_t x = 100;
4260 int32_t y = 125;
4261 processDown(mapper, x, y);
4262 processSync(mapper);
4263
4264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4266 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4267 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4268 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4269 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4270 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4271 ASSERT_EQ(0, motionArgs.flags);
4272 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4273 ASSERT_EQ(0, motionArgs.buttonState);
4274 ASSERT_EQ(0, motionArgs.edgeFlags);
4275 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4276 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4279 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4280 1, 0, 0, 0, 0, 0, 0, 0));
4281 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4282 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4284
4285 // Move.
4286 x += 50;
4287 y += 75;
4288 processMove(mapper, x, y);
4289 processSync(mapper);
4290
4291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4293 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4294 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4298 ASSERT_EQ(0, motionArgs.flags);
4299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4300 ASSERT_EQ(0, motionArgs.buttonState);
4301 ASSERT_EQ(0, motionArgs.edgeFlags);
4302 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4306 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4307 1, 0, 0, 0, 0, 0, 0, 0));
4308 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4309 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4311
4312 // Up.
4313 processUp(mapper);
4314 processSync(mapper);
4315
4316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4317 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4318 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4319 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4320 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4321 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4322 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4323 ASSERT_EQ(0, motionArgs.flags);
4324 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4325 ASSERT_EQ(0, motionArgs.buttonState);
4326 ASSERT_EQ(0, motionArgs.edgeFlags);
4327 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4328 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4331 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4332 1, 0, 0, 0, 0, 0, 0, 0));
4333 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4334 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4335 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4336
4337 // Should not have sent any more keys or motions.
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4340}
4341
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 addConfigurationProperty("touch.deviceType", "touchScreen");
4344 prepareDisplay(DISPLAY_ORIENTATION_0);
4345 prepareButtons();
4346 prepareAxes(POSITION);
4347 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004348 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349
4350 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4351
4352 NotifyMotionArgs motionArgs;
4353
4354 // Down.
4355 int32_t x = 100;
4356 int32_t y = 125;
4357 processDown(mapper, x, y);
4358 processSync(mapper);
4359
4360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4362 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4363 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4364 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4365 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4366 ASSERT_EQ(0, motionArgs.flags);
4367 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4368 ASSERT_EQ(0, motionArgs.buttonState);
4369 ASSERT_EQ(0, motionArgs.edgeFlags);
4370 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4374 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4375 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4376 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4378
4379 // Move.
4380 x += 50;
4381 y += 75;
4382 processMove(mapper, x, y);
4383 processSync(mapper);
4384
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4386 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4387 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4388 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4389 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4391 ASSERT_EQ(0, motionArgs.flags);
4392 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4393 ASSERT_EQ(0, motionArgs.buttonState);
4394 ASSERT_EQ(0, motionArgs.edgeFlags);
4395 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4396 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4399 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4400 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4401 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4403
4404 // Up.
4405 processUp(mapper);
4406 processSync(mapper);
4407
4408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4410 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4411 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4412 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4414 ASSERT_EQ(0, motionArgs.flags);
4415 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4416 ASSERT_EQ(0, motionArgs.buttonState);
4417 ASSERT_EQ(0, motionArgs.edgeFlags);
4418 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4419 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4422 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4423 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4424 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4425 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4426
4427 // Should not have sent any more keys or motions.
4428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4430}
4431
4432TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 addConfigurationProperty("touch.deviceType", "touchScreen");
4434 prepareButtons();
4435 prepareAxes(POSITION);
4436 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004437 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438
4439 NotifyMotionArgs args;
4440
4441 // Rotation 90.
4442 prepareDisplay(DISPLAY_ORIENTATION_90);
4443 processDown(mapper, toRawX(50), toRawY(75));
4444 processSync(mapper);
4445
4446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4447 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4448 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4449
4450 processUp(mapper);
4451 processSync(mapper);
4452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4453}
4454
4455TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 addConfigurationProperty("touch.deviceType", "touchScreen");
4457 prepareButtons();
4458 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004459 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460
4461 NotifyMotionArgs args;
4462
4463 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004464 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 prepareDisplay(DISPLAY_ORIENTATION_0);
4466 processDown(mapper, toRawX(50), toRawY(75));
4467 processSync(mapper);
4468
4469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4470 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4471 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4472
4473 processUp(mapper);
4474 processSync(mapper);
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4476
4477 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004478 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 prepareDisplay(DISPLAY_ORIENTATION_90);
4480 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4481 processSync(mapper);
4482
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4484 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4485 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4486
4487 processUp(mapper);
4488 processSync(mapper);
4489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4490
4491 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004492 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 prepareDisplay(DISPLAY_ORIENTATION_180);
4494 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4495 processSync(mapper);
4496
4497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4498 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4499 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4500
4501 processUp(mapper);
4502 processSync(mapper);
4503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4504
4505 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004506 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507 prepareDisplay(DISPLAY_ORIENTATION_270);
4508 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4509 processSync(mapper);
4510
4511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4512 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4513 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4514
4515 processUp(mapper);
4516 processSync(mapper);
4517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4518}
4519
4520TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 addConfigurationProperty("touch.deviceType", "touchScreen");
4522 prepareDisplay(DISPLAY_ORIENTATION_0);
4523 prepareButtons();
4524 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004525 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526
4527 // These calculations are based on the input device calibration documentation.
4528 int32_t rawX = 100;
4529 int32_t rawY = 200;
4530 int32_t rawPressure = 10;
4531 int32_t rawToolMajor = 12;
4532 int32_t rawDistance = 2;
4533 int32_t rawTiltX = 30;
4534 int32_t rawTiltY = 110;
4535
4536 float x = toDisplayX(rawX);
4537 float y = toDisplayY(rawY);
4538 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4539 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4540 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4541 float distance = float(rawDistance);
4542
4543 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4544 float tiltScale = M_PI / 180;
4545 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4546 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4547 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4548 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4549
4550 processDown(mapper, rawX, rawY);
4551 processPressure(mapper, rawPressure);
4552 processToolMajor(mapper, rawToolMajor);
4553 processDistance(mapper, rawDistance);
4554 processTilt(mapper, rawTiltX, rawTiltY);
4555 processSync(mapper);
4556
4557 NotifyMotionArgs args;
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4560 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4561 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4562}
4563
Jason Gerecke489fda82012-09-07 17:19:40 -07004564TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004565 addConfigurationProperty("touch.deviceType", "touchScreen");
4566 prepareDisplay(DISPLAY_ORIENTATION_0);
4567 prepareLocationCalibration();
4568 prepareButtons();
4569 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004570 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004571
4572 int32_t rawX = 100;
4573 int32_t rawY = 200;
4574
4575 float x = toDisplayX(toCookedX(rawX, rawY));
4576 float y = toDisplayY(toCookedY(rawX, rawY));
4577
4578 processDown(mapper, rawX, rawY);
4579 processSync(mapper);
4580
4581 NotifyMotionArgs args;
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4584 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4585}
4586
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 addConfigurationProperty("touch.deviceType", "touchScreen");
4589 prepareDisplay(DISPLAY_ORIENTATION_0);
4590 prepareButtons();
4591 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004592 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593
4594 NotifyMotionArgs motionArgs;
4595 NotifyKeyArgs keyArgs;
4596
4597 processDown(mapper, 100, 200);
4598 processSync(mapper);
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4600 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4601 ASSERT_EQ(0, motionArgs.buttonState);
4602
4603 // press BTN_LEFT, release BTN_LEFT
4604 processKey(mapper, BTN_LEFT, 1);
4605 processSync(mapper);
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4608 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4609
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4611 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4612 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4613
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 processKey(mapper, BTN_LEFT, 0);
4615 processSync(mapper);
4616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004622 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623
4624 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4625 processKey(mapper, BTN_RIGHT, 1);
4626 processKey(mapper, BTN_MIDDLE, 1);
4627 processSync(mapper);
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4630 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4631 motionArgs.buttonState);
4632
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4635 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4636
4637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4639 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4640 motionArgs.buttonState);
4641
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642 processKey(mapper, BTN_RIGHT, 0);
4643 processSync(mapper);
4644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004645 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004647
4648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004650 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651
4652 processKey(mapper, BTN_MIDDLE, 0);
4653 processSync(mapper);
4654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004657
4658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004660 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661
4662 // press BTN_BACK, release BTN_BACK
4663 processKey(mapper, BTN_BACK, 1);
4664 processSync(mapper);
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4666 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4667 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004668
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4672
4673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4675 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676
4677 processKey(mapper, BTN_BACK, 0);
4678 processSync(mapper);
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004685 ASSERT_EQ(0, motionArgs.buttonState);
4686
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4688 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4689 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4690
4691 // press BTN_SIDE, release BTN_SIDE
4692 processKey(mapper, BTN_SIDE, 1);
4693 processSync(mapper);
4694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4696 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004700 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4701
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4703 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4704 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705
4706 processKey(mapper, BTN_SIDE, 0);
4707 processSync(mapper);
4708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004711
4712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004714 ASSERT_EQ(0, motionArgs.buttonState);
4715
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4717 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4718 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4719
4720 // press BTN_FORWARD, release BTN_FORWARD
4721 processKey(mapper, BTN_FORWARD, 1);
4722 processSync(mapper);
4723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4724 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4725 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004726
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004729 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4730
4731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4732 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4733 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734
4735 processKey(mapper, BTN_FORWARD, 0);
4736 processSync(mapper);
4737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740
4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004743 ASSERT_EQ(0, motionArgs.buttonState);
4744
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4746 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4747 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4748
4749 // press BTN_EXTRA, release BTN_EXTRA
4750 processKey(mapper, BTN_EXTRA, 1);
4751 processSync(mapper);
4752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4753 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4754 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004755
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004758 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4759
4760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4761 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4762 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763
4764 processKey(mapper, BTN_EXTRA, 0);
4765 processSync(mapper);
4766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004767 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004769
4770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004772 ASSERT_EQ(0, motionArgs.buttonState);
4773
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4775 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4776 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4777
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4779
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 // press BTN_STYLUS, release BTN_STYLUS
4781 processKey(mapper, BTN_STYLUS, 1);
4782 processSync(mapper);
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004785 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4786
4787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4788 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4789 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790
4791 processKey(mapper, BTN_STYLUS, 0);
4792 processSync(mapper);
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004796
4797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800
4801 // press BTN_STYLUS2, release BTN_STYLUS2
4802 processKey(mapper, BTN_STYLUS2, 1);
4803 processSync(mapper);
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004806 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4807
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4810 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811
4812 processKey(mapper, BTN_STYLUS2, 0);
4813 processSync(mapper);
4814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004815 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004817
4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004820 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821
4822 // release touch
4823 processUp(mapper);
4824 processSync(mapper);
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4826 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4827 ASSERT_EQ(0, motionArgs.buttonState);
4828}
4829
4830TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 addConfigurationProperty("touch.deviceType", "touchScreen");
4832 prepareDisplay(DISPLAY_ORIENTATION_0);
4833 prepareButtons();
4834 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004835 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836
4837 NotifyMotionArgs motionArgs;
4838
4839 // default tool type is finger
4840 processDown(mapper, 100, 200);
4841 processSync(mapper);
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4845
4846 // eraser
4847 processKey(mapper, BTN_TOOL_RUBBER, 1);
4848 processSync(mapper);
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4852
4853 // stylus
4854 processKey(mapper, BTN_TOOL_RUBBER, 0);
4855 processKey(mapper, BTN_TOOL_PEN, 1);
4856 processSync(mapper);
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4860
4861 // brush
4862 processKey(mapper, BTN_TOOL_PEN, 0);
4863 processKey(mapper, BTN_TOOL_BRUSH, 1);
4864 processSync(mapper);
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4868
4869 // pencil
4870 processKey(mapper, BTN_TOOL_BRUSH, 0);
4871 processKey(mapper, BTN_TOOL_PENCIL, 1);
4872 processSync(mapper);
4873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4876
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004877 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 processKey(mapper, BTN_TOOL_PENCIL, 0);
4879 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4880 processSync(mapper);
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4884
4885 // mouse
4886 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4887 processKey(mapper, BTN_TOOL_MOUSE, 1);
4888 processSync(mapper);
4889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4892
4893 // lens
4894 processKey(mapper, BTN_TOOL_MOUSE, 0);
4895 processKey(mapper, BTN_TOOL_LENS, 1);
4896 processSync(mapper);
4897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4900
4901 // double-tap
4902 processKey(mapper, BTN_TOOL_LENS, 0);
4903 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4904 processSync(mapper);
4905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4908
4909 // triple-tap
4910 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4911 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4912 processSync(mapper);
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4915 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4916
4917 // quad-tap
4918 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4919 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4920 processSync(mapper);
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4924
4925 // finger
4926 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4927 processKey(mapper, BTN_TOOL_FINGER, 1);
4928 processSync(mapper);
4929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4932
4933 // stylus trumps finger
4934 processKey(mapper, BTN_TOOL_PEN, 1);
4935 processSync(mapper);
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4939
4940 // eraser trumps stylus
4941 processKey(mapper, BTN_TOOL_RUBBER, 1);
4942 processSync(mapper);
4943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4946
4947 // mouse trumps eraser
4948 processKey(mapper, BTN_TOOL_MOUSE, 1);
4949 processSync(mapper);
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4953
4954 // back to default tool type
4955 processKey(mapper, BTN_TOOL_MOUSE, 0);
4956 processKey(mapper, BTN_TOOL_RUBBER, 0);
4957 processKey(mapper, BTN_TOOL_PEN, 0);
4958 processKey(mapper, BTN_TOOL_FINGER, 0);
4959 processSync(mapper);
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4962 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4963}
4964
4965TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 addConfigurationProperty("touch.deviceType", "touchScreen");
4967 prepareDisplay(DISPLAY_ORIENTATION_0);
4968 prepareButtons();
4969 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004970 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004971 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972
4973 NotifyMotionArgs motionArgs;
4974
4975 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4976 processKey(mapper, BTN_TOOL_FINGER, 1);
4977 processMove(mapper, 100, 200);
4978 processSync(mapper);
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4980 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4982 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4983
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4985 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4987 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4988
4989 // move a little
4990 processMove(mapper, 150, 250);
4991 processSync(mapper);
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4993 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4995 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4996
4997 // down when BTN_TOUCH is pressed, pressure defaults to 1
4998 processKey(mapper, BTN_TOUCH, 1);
4999 processSync(mapper);
5000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5001 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5003 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5004
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5006 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5008 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5009
5010 // up when BTN_TOUCH is released, hover restored
5011 processKey(mapper, BTN_TOUCH, 0);
5012 processSync(mapper);
5013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5014 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5016 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5017
5018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5019 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5021 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5022
5023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5024 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5026 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5027
5028 // exit hover when pointer goes away
5029 processKey(mapper, BTN_TOOL_FINGER, 0);
5030 processSync(mapper);
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5032 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5034 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5035}
5036
5037TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038 addConfigurationProperty("touch.deviceType", "touchScreen");
5039 prepareDisplay(DISPLAY_ORIENTATION_0);
5040 prepareButtons();
5041 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005042 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043
5044 NotifyMotionArgs motionArgs;
5045
5046 // initially hovering because pressure is 0
5047 processDown(mapper, 100, 200);
5048 processPressure(mapper, 0);
5049 processSync(mapper);
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5051 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5053 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5054
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5056 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5058 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5059
5060 // move a little
5061 processMove(mapper, 150, 250);
5062 processSync(mapper);
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5064 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5066 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5067
5068 // down when pressure is non-zero
5069 processPressure(mapper, RAW_PRESSURE_MAX);
5070 processSync(mapper);
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5072 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5074 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5075
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5077 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5079 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5080
5081 // up when pressure becomes 0, hover restored
5082 processPressure(mapper, 0);
5083 processSync(mapper);
5084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5085 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5087 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5088
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5090 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5092 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5093
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5095 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5097 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5098
5099 // exit hover when pointer goes away
5100 processUp(mapper);
5101 processSync(mapper);
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5103 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5105 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5106}
5107
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108// --- MultiTouchInputMapperTest ---
5109
5110class MultiTouchInputMapperTest : public TouchInputMapperTest {
5111protected:
5112 void prepareAxes(int axes);
5113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005114 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5115 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5116 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5117 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5118 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5119 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5120 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5121 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5122 void processId(MultiTouchInputMapper& mapper, int32_t id);
5123 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5124 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5125 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5126 void processMTSync(MultiTouchInputMapper& mapper);
5127 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128};
5129
5130void MultiTouchInputMapperTest::prepareAxes(int axes) {
5131 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005132 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134 }
5135 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005136 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5137 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5140 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141 }
5142 }
5143 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5145 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5148 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 }
5150 }
5151 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005152 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5153 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005154 }
5155 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005156 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5157 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158 }
5159 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005160 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5161 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005162 }
5163 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005164 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5165 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166 }
5167 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005168 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5169 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170 }
5171 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005172 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 }
5174}
5175
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005176void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5177 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005178 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5179 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180}
5181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005182void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5183 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005184 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185}
5186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005187void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5188 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005189 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005190}
5191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005192void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005193 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194}
5195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005196void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005197 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198}
5199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005200void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5201 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005202 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203}
5204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005205void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005206 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207}
5208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005209void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005210 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211}
5212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005213void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005214 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215}
5216
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005217void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005218 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005219}
5220
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005221void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005222 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223}
5224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005225void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5226 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005227 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228}
5229
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005230void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005231 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232}
5233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005234void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005235 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236}
5237
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239 addConfigurationProperty("touch.deviceType", "touchScreen");
5240 prepareDisplay(DISPLAY_ORIENTATION_0);
5241 prepareAxes(POSITION);
5242 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005243 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244
5245 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5246
5247 NotifyMotionArgs motionArgs;
5248
5249 // Two fingers down at once.
5250 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5251 processPosition(mapper, x1, y1);
5252 processMTSync(mapper);
5253 processPosition(mapper, x2, y2);
5254 processMTSync(mapper);
5255 processSync(mapper);
5256
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5259 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5261 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5263 ASSERT_EQ(0, motionArgs.flags);
5264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5265 ASSERT_EQ(0, motionArgs.buttonState);
5266 ASSERT_EQ(0, motionArgs.edgeFlags);
5267 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5271 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5272 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5273 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5275
5276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5277 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5278 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5279 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5280 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5281 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5282 motionArgs.action);
5283 ASSERT_EQ(0, motionArgs.flags);
5284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5285 ASSERT_EQ(0, motionArgs.buttonState);
5286 ASSERT_EQ(0, motionArgs.edgeFlags);
5287 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5288 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5290 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5293 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5295 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5296 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5297 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5298 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5299
5300 // Move.
5301 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5302 processPosition(mapper, x1, y1);
5303 processMTSync(mapper);
5304 processPosition(mapper, x2, y2);
5305 processMTSync(mapper);
5306 processSync(mapper);
5307
5308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5310 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5311 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5312 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5314 ASSERT_EQ(0, motionArgs.flags);
5315 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5316 ASSERT_EQ(0, motionArgs.buttonState);
5317 ASSERT_EQ(0, motionArgs.edgeFlags);
5318 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5319 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5321 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5324 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5326 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5327 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5328 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5330
5331 // First finger up.
5332 x2 += 15; y2 -= 20;
5333 processPosition(mapper, x2, y2);
5334 processMTSync(mapper);
5335 processSync(mapper);
5336
5337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5339 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5340 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5341 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5342 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5343 motionArgs.action);
5344 ASSERT_EQ(0, motionArgs.flags);
5345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5346 ASSERT_EQ(0, motionArgs.buttonState);
5347 ASSERT_EQ(0, motionArgs.edgeFlags);
5348 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5349 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5351 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5354 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5356 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5357 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5358 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5359 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5360
5361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5362 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5363 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5364 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5365 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5367 ASSERT_EQ(0, motionArgs.flags);
5368 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5369 ASSERT_EQ(0, motionArgs.buttonState);
5370 ASSERT_EQ(0, motionArgs.edgeFlags);
5371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5372 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5376 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5377 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5379
5380 // Move.
5381 x2 += 20; y2 -= 25;
5382 processPosition(mapper, x2, y2);
5383 processMTSync(mapper);
5384 processSync(mapper);
5385
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5387 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5388 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5389 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5390 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5392 ASSERT_EQ(0, motionArgs.flags);
5393 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5394 ASSERT_EQ(0, motionArgs.buttonState);
5395 ASSERT_EQ(0, motionArgs.edgeFlags);
5396 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5397 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5400 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5401 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5402 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5404
5405 // New finger down.
5406 int32_t x3 = 700, y3 = 300;
5407 processPosition(mapper, x2, y2);
5408 processMTSync(mapper);
5409 processPosition(mapper, x3, y3);
5410 processMTSync(mapper);
5411 processSync(mapper);
5412
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5415 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5416 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5417 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5418 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5419 motionArgs.action);
5420 ASSERT_EQ(0, motionArgs.flags);
5421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5422 ASSERT_EQ(0, motionArgs.buttonState);
5423 ASSERT_EQ(0, motionArgs.edgeFlags);
5424 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5427 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5430 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5432 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5433 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5434 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5435 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5436
5437 // Second finger up.
5438 x3 += 30; y3 -= 20;
5439 processPosition(mapper, x3, y3);
5440 processMTSync(mapper);
5441 processSync(mapper);
5442
5443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5444 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5445 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5446 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5447 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5448 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5449 motionArgs.action);
5450 ASSERT_EQ(0, motionArgs.flags);
5451 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5452 ASSERT_EQ(0, motionArgs.buttonState);
5453 ASSERT_EQ(0, motionArgs.edgeFlags);
5454 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5455 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5457 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5460 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5462 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5463 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5464 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5466
5467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5468 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5469 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5470 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5471 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5473 ASSERT_EQ(0, motionArgs.flags);
5474 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5475 ASSERT_EQ(0, motionArgs.buttonState);
5476 ASSERT_EQ(0, motionArgs.edgeFlags);
5477 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5478 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5481 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5482 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5483 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5484 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5485
5486 // Last finger up.
5487 processMTSync(mapper);
5488 processSync(mapper);
5489
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5491 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5492 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5493 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5494 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5495 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5496 ASSERT_EQ(0, motionArgs.flags);
5497 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5498 ASSERT_EQ(0, motionArgs.buttonState);
5499 ASSERT_EQ(0, motionArgs.edgeFlags);
5500 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5501 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5504 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5505 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5506 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5507 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5508
5509 // Should not have sent any more keys or motions.
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5512}
5513
5514TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 addConfigurationProperty("touch.deviceType", "touchScreen");
5516 prepareDisplay(DISPLAY_ORIENTATION_0);
5517 prepareAxes(POSITION | ID);
5518 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005519 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520
5521 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5522
5523 NotifyMotionArgs motionArgs;
5524
5525 // Two fingers down at once.
5526 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5527 processPosition(mapper, x1, y1);
5528 processId(mapper, 1);
5529 processMTSync(mapper);
5530 processPosition(mapper, x2, y2);
5531 processId(mapper, 2);
5532 processMTSync(mapper);
5533 processSync(mapper);
5534
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5536 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5537 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5538 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5541 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5545 motionArgs.action);
5546 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5547 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5549 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5552 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5554 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5555
5556 // Move.
5557 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5558 processPosition(mapper, x1, y1);
5559 processId(mapper, 1);
5560 processMTSync(mapper);
5561 processPosition(mapper, x2, y2);
5562 processId(mapper, 2);
5563 processMTSync(mapper);
5564 processSync(mapper);
5565
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5568 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5571 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5574 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5576 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5577
5578 // First finger up.
5579 x2 += 15; y2 -= 20;
5580 processPosition(mapper, x2, y2);
5581 processId(mapper, 2);
5582 processMTSync(mapper);
5583 processSync(mapper);
5584
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5586 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5587 motionArgs.action);
5588 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5589 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5591 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5594 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5596 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5601 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5605
5606 // Move.
5607 x2 += 20; y2 -= 25;
5608 processPosition(mapper, x2, y2);
5609 processId(mapper, 2);
5610 processMTSync(mapper);
5611 processSync(mapper);
5612
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5615 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5616 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5620
5621 // New finger down.
5622 int32_t x3 = 700, y3 = 300;
5623 processPosition(mapper, x2, y2);
5624 processId(mapper, 2);
5625 processMTSync(mapper);
5626 processPosition(mapper, x3, y3);
5627 processId(mapper, 3);
5628 processMTSync(mapper);
5629 processSync(mapper);
5630
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5632 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5633 motionArgs.action);
5634 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5637 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5640 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5643
5644 // Second finger up.
5645 x3 += 30; y3 -= 20;
5646 processPosition(mapper, x3, y3);
5647 processId(mapper, 3);
5648 processMTSync(mapper);
5649 processSync(mapper);
5650
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5652 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5653 motionArgs.action);
5654 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5655 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5657 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5660 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5662 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5663
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5666 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5667 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5670 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5671
5672 // Last finger up.
5673 processMTSync(mapper);
5674 processSync(mapper);
5675
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5678 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5679 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5682 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5683
5684 // Should not have sent any more keys or motions.
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5687}
5688
5689TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690 addConfigurationProperty("touch.deviceType", "touchScreen");
5691 prepareDisplay(DISPLAY_ORIENTATION_0);
5692 prepareAxes(POSITION | ID | SLOT);
5693 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005694 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695
5696 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5697
5698 NotifyMotionArgs motionArgs;
5699
5700 // Two fingers down at once.
5701 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5702 processPosition(mapper, x1, y1);
5703 processId(mapper, 1);
5704 processSlot(mapper, 1);
5705 processPosition(mapper, x2, y2);
5706 processId(mapper, 2);
5707 processSync(mapper);
5708
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5710 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5711 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5715 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5716
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5719 motionArgs.action);
5720 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5723 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5726 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5728 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5729
5730 // Move.
5731 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5732 processSlot(mapper, 0);
5733 processPosition(mapper, x1, y1);
5734 processSlot(mapper, 1);
5735 processPosition(mapper, x2, y2);
5736 processSync(mapper);
5737
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5740 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5741 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5743 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5746 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5748 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5749
5750 // First finger up.
5751 x2 += 15; y2 -= 20;
5752 processSlot(mapper, 0);
5753 processId(mapper, -1);
5754 processSlot(mapper, 1);
5755 processPosition(mapper, x2, y2);
5756 processSync(mapper);
5757
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5759 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5760 motionArgs.action);
5761 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5762 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5764 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5767 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5769 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5770
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5773 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5774 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5777 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5778
5779 // Move.
5780 x2 += 20; y2 -= 25;
5781 processPosition(mapper, x2, y2);
5782 processSync(mapper);
5783
5784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5786 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5787 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5790 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5791
5792 // New finger down.
5793 int32_t x3 = 700, y3 = 300;
5794 processPosition(mapper, x2, y2);
5795 processSlot(mapper, 0);
5796 processId(mapper, 3);
5797 processPosition(mapper, x3, y3);
5798 processSync(mapper);
5799
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5801 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5802 motionArgs.action);
5803 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5804 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5806 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5809 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5811 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5812
5813 // Second finger up.
5814 x3 += 30; y3 -= 20;
5815 processSlot(mapper, 1);
5816 processId(mapper, -1);
5817 processSlot(mapper, 0);
5818 processPosition(mapper, x3, y3);
5819 processSync(mapper);
5820
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5822 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5823 motionArgs.action);
5824 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5825 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5827 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5830 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5832 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5833
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5836 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5837 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5840 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5841
5842 // Last finger up.
5843 processId(mapper, -1);
5844 processSync(mapper);
5845
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5847 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5848 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5849 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5852 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5853
5854 // Should not have sent any more keys or motions.
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5857}
5858
5859TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 addConfigurationProperty("touch.deviceType", "touchScreen");
5861 prepareDisplay(DISPLAY_ORIENTATION_0);
5862 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005864
5865 // These calculations are based on the input device calibration documentation.
5866 int32_t rawX = 100;
5867 int32_t rawY = 200;
5868 int32_t rawTouchMajor = 7;
5869 int32_t rawTouchMinor = 6;
5870 int32_t rawToolMajor = 9;
5871 int32_t rawToolMinor = 8;
5872 int32_t rawPressure = 11;
5873 int32_t rawDistance = 0;
5874 int32_t rawOrientation = 3;
5875 int32_t id = 5;
5876
5877 float x = toDisplayX(rawX);
5878 float y = toDisplayY(rawY);
5879 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5880 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5881 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5882 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5883 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5884 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5885 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5886 float distance = float(rawDistance);
5887
5888 processPosition(mapper, rawX, rawY);
5889 processTouchMajor(mapper, rawTouchMajor);
5890 processTouchMinor(mapper, rawTouchMinor);
5891 processToolMajor(mapper, rawToolMajor);
5892 processToolMinor(mapper, rawToolMinor);
5893 processPressure(mapper, rawPressure);
5894 processOrientation(mapper, rawOrientation);
5895 processDistance(mapper, rawDistance);
5896 processId(mapper, id);
5897 processMTSync(mapper);
5898 processSync(mapper);
5899
5900 NotifyMotionArgs args;
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5902 ASSERT_EQ(0, args.pointerProperties[0].id);
5903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5904 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5905 orientation, distance));
5906}
5907
5908TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909 addConfigurationProperty("touch.deviceType", "touchScreen");
5910 prepareDisplay(DISPLAY_ORIENTATION_0);
5911 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5912 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005913 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914
5915 // These calculations are based on the input device calibration documentation.
5916 int32_t rawX = 100;
5917 int32_t rawY = 200;
5918 int32_t rawTouchMajor = 140;
5919 int32_t rawTouchMinor = 120;
5920 int32_t rawToolMajor = 180;
5921 int32_t rawToolMinor = 160;
5922
5923 float x = toDisplayX(rawX);
5924 float y = toDisplayY(rawY);
5925 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5926 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5927 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5928 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5929 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5930
5931 processPosition(mapper, rawX, rawY);
5932 processTouchMajor(mapper, rawTouchMajor);
5933 processTouchMinor(mapper, rawTouchMinor);
5934 processToolMajor(mapper, rawToolMajor);
5935 processToolMinor(mapper, rawToolMinor);
5936 processMTSync(mapper);
5937 processSync(mapper);
5938
5939 NotifyMotionArgs args;
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5942 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5943}
5944
5945TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946 addConfigurationProperty("touch.deviceType", "touchScreen");
5947 prepareDisplay(DISPLAY_ORIENTATION_0);
5948 prepareAxes(POSITION | TOUCH | TOOL);
5949 addConfigurationProperty("touch.size.calibration", "diameter");
5950 addConfigurationProperty("touch.size.scale", "10");
5951 addConfigurationProperty("touch.size.bias", "160");
5952 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005953 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954
5955 // These calculations are based on the input device calibration documentation.
5956 // Note: We only provide a single common touch/tool value because the device is assumed
5957 // not to emit separate values for each pointer (isSummed = 1).
5958 int32_t rawX = 100;
5959 int32_t rawY = 200;
5960 int32_t rawX2 = 150;
5961 int32_t rawY2 = 250;
5962 int32_t rawTouchMajor = 5;
5963 int32_t rawToolMajor = 8;
5964
5965 float x = toDisplayX(rawX);
5966 float y = toDisplayY(rawY);
5967 float x2 = toDisplayX(rawX2);
5968 float y2 = toDisplayY(rawY2);
5969 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5970 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5971 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5972
5973 processPosition(mapper, rawX, rawY);
5974 processTouchMajor(mapper, rawTouchMajor);
5975 processToolMajor(mapper, rawToolMajor);
5976 processMTSync(mapper);
5977 processPosition(mapper, rawX2, rawY2);
5978 processTouchMajor(mapper, rawTouchMajor);
5979 processToolMajor(mapper, rawToolMajor);
5980 processMTSync(mapper);
5981 processSync(mapper);
5982
5983 NotifyMotionArgs args;
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5986
5987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5988 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5989 args.action);
5990 ASSERT_EQ(size_t(2), args.pointerCount);
5991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5992 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5994 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5995}
5996
5997TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998 addConfigurationProperty("touch.deviceType", "touchScreen");
5999 prepareDisplay(DISPLAY_ORIENTATION_0);
6000 prepareAxes(POSITION | TOUCH | TOOL);
6001 addConfigurationProperty("touch.size.calibration", "area");
6002 addConfigurationProperty("touch.size.scale", "43");
6003 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006004 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005
6006 // These calculations are based on the input device calibration documentation.
6007 int32_t rawX = 100;
6008 int32_t rawY = 200;
6009 int32_t rawTouchMajor = 5;
6010 int32_t rawToolMajor = 8;
6011
6012 float x = toDisplayX(rawX);
6013 float y = toDisplayY(rawY);
6014 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6015 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6016 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6017
6018 processPosition(mapper, rawX, rawY);
6019 processTouchMajor(mapper, rawTouchMajor);
6020 processToolMajor(mapper, rawToolMajor);
6021 processMTSync(mapper);
6022 processSync(mapper);
6023
6024 NotifyMotionArgs args;
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6027 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6028}
6029
6030TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 addConfigurationProperty("touch.deviceType", "touchScreen");
6032 prepareDisplay(DISPLAY_ORIENTATION_0);
6033 prepareAxes(POSITION | PRESSURE);
6034 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6035 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006036 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037
Michael Wrightaa449c92017-12-13 21:21:43 +00006038 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006039 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006040 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6041 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6042 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6043
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 // These calculations are based on the input device calibration documentation.
6045 int32_t rawX = 100;
6046 int32_t rawY = 200;
6047 int32_t rawPressure = 60;
6048
6049 float x = toDisplayX(rawX);
6050 float y = toDisplayY(rawY);
6051 float pressure = float(rawPressure) * 0.01f;
6052
6053 processPosition(mapper, rawX, rawY);
6054 processPressure(mapper, rawPressure);
6055 processMTSync(mapper);
6056 processSync(mapper);
6057
6058 NotifyMotionArgs args;
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6061 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6062}
6063
6064TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 addConfigurationProperty("touch.deviceType", "touchScreen");
6066 prepareDisplay(DISPLAY_ORIENTATION_0);
6067 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006068 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069
6070 NotifyMotionArgs motionArgs;
6071 NotifyKeyArgs keyArgs;
6072
6073 processId(mapper, 1);
6074 processPosition(mapper, 100, 200);
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6077 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6078 ASSERT_EQ(0, motionArgs.buttonState);
6079
6080 // press BTN_LEFT, release BTN_LEFT
6081 processKey(mapper, BTN_LEFT, 1);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6085 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6086
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6089 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6090
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 processKey(mapper, BTN_LEFT, 0);
6092 processSync(mapper);
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006096
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006099 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100
6101 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6102 processKey(mapper, BTN_RIGHT, 1);
6103 processKey(mapper, BTN_MIDDLE, 1);
6104 processSync(mapper);
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6107 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6108 motionArgs.buttonState);
6109
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6112 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6113
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6116 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6117 motionArgs.buttonState);
6118
Michael Wrightd02c5b62014-02-10 15:10:22 -08006119 processKey(mapper, BTN_RIGHT, 0);
6120 processSync(mapper);
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006124
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006127 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128
6129 processKey(mapper, BTN_MIDDLE, 0);
6130 processSync(mapper);
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006134
6135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006137 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 // press BTN_BACK, release BTN_BACK
6140 processKey(mapper, BTN_BACK, 1);
6141 processSync(mapper);
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6143 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6144 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006145
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6149
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6151 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6152 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153
6154 processKey(mapper, BTN_BACK, 0);
6155 processSync(mapper);
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006159
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006162 ASSERT_EQ(0, motionArgs.buttonState);
6163
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6165 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6166 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6167
6168 // press BTN_SIDE, release BTN_SIDE
6169 processKey(mapper, BTN_SIDE, 1);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6172 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6173 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006174
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006177 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6178
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6181 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182
6183 processKey(mapper, BTN_SIDE, 0);
6184 processSync(mapper);
6185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006191 ASSERT_EQ(0, motionArgs.buttonState);
6192
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6194 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6195 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6196
6197 // press BTN_FORWARD, release BTN_FORWARD
6198 processKey(mapper, BTN_FORWARD, 1);
6199 processSync(mapper);
6200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6201 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6202 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006203
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006206 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6210 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211
6212 processKey(mapper, BTN_FORWARD, 0);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006217
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006220 ASSERT_EQ(0, motionArgs.buttonState);
6221
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6223 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6224 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6225
6226 // press BTN_EXTRA, release BTN_EXTRA
6227 processKey(mapper, BTN_EXTRA, 1);
6228 processSync(mapper);
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6230 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6231 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006235 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6236
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6239 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240
6241 processKey(mapper, BTN_EXTRA, 0);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006246
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006249 ASSERT_EQ(0, motionArgs.buttonState);
6250
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6252 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6253 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6254
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6256
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257 // press BTN_STYLUS, release BTN_STYLUS
6258 processKey(mapper, BTN_STYLUS, 1);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006262 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6266 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 processKey(mapper, BTN_STYLUS, 0);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277
6278 // press BTN_STYLUS2, release BTN_STYLUS2
6279 processKey(mapper, BTN_STYLUS2, 1);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006283 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6284
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6287 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288
6289 processKey(mapper, BTN_STYLUS2, 0);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006297 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298
6299 // release touch
6300 processId(mapper, -1);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6304 ASSERT_EQ(0, motionArgs.buttonState);
6305}
6306
6307TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 addConfigurationProperty("touch.deviceType", "touchScreen");
6309 prepareDisplay(DISPLAY_ORIENTATION_0);
6310 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312
6313 NotifyMotionArgs motionArgs;
6314
6315 // default tool type is finger
6316 processId(mapper, 1);
6317 processPosition(mapper, 100, 200);
6318 processSync(mapper);
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6322
6323 // eraser
6324 processKey(mapper, BTN_TOOL_RUBBER, 1);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6329
6330 // stylus
6331 processKey(mapper, BTN_TOOL_RUBBER, 0);
6332 processKey(mapper, BTN_TOOL_PEN, 1);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6337
6338 // brush
6339 processKey(mapper, BTN_TOOL_PEN, 0);
6340 processKey(mapper, BTN_TOOL_BRUSH, 1);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6345
6346 // pencil
6347 processKey(mapper, BTN_TOOL_BRUSH, 0);
6348 processKey(mapper, BTN_TOOL_PENCIL, 1);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6353
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006354 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355 processKey(mapper, BTN_TOOL_PENCIL, 0);
6356 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6361
6362 // mouse
6363 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6364 processKey(mapper, BTN_TOOL_MOUSE, 1);
6365 processSync(mapper);
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6369
6370 // lens
6371 processKey(mapper, BTN_TOOL_MOUSE, 0);
6372 processKey(mapper, BTN_TOOL_LENS, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6377
6378 // double-tap
6379 processKey(mapper, BTN_TOOL_LENS, 0);
6380 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6381 processSync(mapper);
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6385
6386 // triple-tap
6387 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6388 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6393
6394 // quad-tap
6395 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6396 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6401
6402 // finger
6403 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6404 processKey(mapper, BTN_TOOL_FINGER, 1);
6405 processSync(mapper);
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6409
6410 // stylus trumps finger
6411 processKey(mapper, BTN_TOOL_PEN, 1);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6416
6417 // eraser trumps stylus
6418 processKey(mapper, BTN_TOOL_RUBBER, 1);
6419 processSync(mapper);
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6423
6424 // mouse trumps eraser
6425 processKey(mapper, BTN_TOOL_MOUSE, 1);
6426 processSync(mapper);
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6430
6431 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6432 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6437
6438 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6439 processToolType(mapper, MT_TOOL_PEN);
6440 processSync(mapper);
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6444
6445 // back to default tool type
6446 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6447 processKey(mapper, BTN_TOOL_MOUSE, 0);
6448 processKey(mapper, BTN_TOOL_RUBBER, 0);
6449 processKey(mapper, BTN_TOOL_PEN, 0);
6450 processKey(mapper, BTN_TOOL_FINGER, 0);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6455}
6456
6457TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 addConfigurationProperty("touch.deviceType", "touchScreen");
6459 prepareDisplay(DISPLAY_ORIENTATION_0);
6460 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006461 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006462 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463
6464 NotifyMotionArgs motionArgs;
6465
6466 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6467 processId(mapper, 1);
6468 processPosition(mapper, 100, 200);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6473 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6474
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6478 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6479
6480 // move a little
6481 processPosition(mapper, 150, 250);
6482 processSync(mapper);
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6487
6488 // down when BTN_TOUCH is pressed, pressure defaults to 1
6489 processKey(mapper, BTN_TOUCH, 1);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6494 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6495
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6497 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6499 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6500
6501 // up when BTN_TOUCH is released, hover restored
6502 processKey(mapper, BTN_TOUCH, 0);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6507 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6508
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6512 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6513
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6517 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6518
6519 // exit hover when pointer goes away
6520 processId(mapper, -1);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6525 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6526}
6527
6528TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529 addConfigurationProperty("touch.deviceType", "touchScreen");
6530 prepareDisplay(DISPLAY_ORIENTATION_0);
6531 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006532 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533
6534 NotifyMotionArgs motionArgs;
6535
6536 // initially hovering because pressure is 0
6537 processId(mapper, 1);
6538 processPosition(mapper, 100, 200);
6539 processPressure(mapper, 0);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6544 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6545
6546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6547 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6549 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6550
6551 // move a little
6552 processPosition(mapper, 150, 250);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6557 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6558
6559 // down when pressure becomes non-zero
6560 processPressure(mapper, RAW_PRESSURE_MAX);
6561 processSync(mapper);
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6565 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6566
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6570 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6571
6572 // up when pressure becomes 0, hover restored
6573 processPressure(mapper, 0);
6574 processSync(mapper);
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6578 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6579
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6583 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6584
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6588 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6589
6590 // exit hover when pointer goes away
6591 processId(mapper, -1);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6596 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6597}
6598
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006599/**
6600 * Set the input device port <--> display port associations, and check that the
6601 * events are routed to the display that matches the display port.
6602 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6603 */
6604TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006605 const std::string usb2 = "USB2";
6606 const uint8_t hdmi1 = 0;
6607 const uint8_t hdmi2 = 1;
6608 const std::string secondaryUniqueId = "uniqueId2";
6609 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6610
6611 addConfigurationProperty("touch.deviceType", "touchScreen");
6612 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006613 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006614
6615 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6616 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6617
6618 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6619 // for this input device is specified, and the matching viewport is not present,
6620 // the input device should be disabled (at the mapper level).
6621
6622 // Add viewport for display 2 on hdmi2
6623 prepareSecondaryDisplay(type, hdmi2);
6624 // Send a touch event
6625 processPosition(mapper, 100, 100);
6626 processSync(mapper);
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6628
6629 // Add viewport for display 1 on hdmi1
6630 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6631 // Send a touch event again
6632 processPosition(mapper, 100, 100);
6633 processSync(mapper);
6634
6635 NotifyMotionArgs args;
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6637 ASSERT_EQ(DISPLAY_ID, args.displayId);
6638}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006639
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006640TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006641 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006642 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006643 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006644 fakePointerController->setPosition(100, 200);
6645 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006646 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6647
Garfield Tan888a6a42020-01-09 11:39:16 -08006648 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6649 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6650
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006651 prepareDisplay(DISPLAY_ORIENTATION_0);
6652 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006653 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006654
6655 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006656 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006657
6658 NotifyMotionArgs motionArgs;
6659 processPosition(mapper, 100, 100);
6660 processSync(mapper);
6661
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6664 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6665}
6666
Arthur Hung7c645402019-01-25 17:45:42 +08006667TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6668 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006669 prepareAxes(POSITION | ID | SLOT);
6670 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006671 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006672
6673 // Create the second touch screen device, and enable multi fingers.
6674 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006675 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006676 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006677 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006678 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006679 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006680 std::unique_ptr<InputDevice> device2 =
6681 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006682 identifier);
6683 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6684 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6685 0 /*flat*/, 0 /*fuzz*/);
6686 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6687 0 /*flat*/, 0 /*fuzz*/);
6688 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6689 0 /*flat*/, 0 /*fuzz*/);
6690 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6691 0 /*flat*/, 0 /*fuzz*/);
6692 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6693 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6694 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006695
6696 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006697 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006698 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6699 device2->reset(ARBITRARY_TIME);
6700
6701 // Setup PointerController.
6702 sp<FakePointerController> fakePointerController = new FakePointerController();
6703 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6704 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6705
6706 // Setup policy for associated displays and show touches.
6707 const uint8_t hdmi1 = 0;
6708 const uint8_t hdmi2 = 1;
6709 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6710 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6711 mFakePolicy->setShowTouches(true);
6712
6713 // Create displays.
6714 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6715 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6716
6717 // Default device will reconfigure above, need additional reconfiguration for another device.
6718 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6719 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6720
6721 // Two fingers down at default display.
6722 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6723 processPosition(mapper, x1, y1);
6724 processId(mapper, 1);
6725 processSlot(mapper, 1);
6726 processPosition(mapper, x2, y2);
6727 processId(mapper, 2);
6728 processSync(mapper);
6729
6730 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6731 fakePointerController->getSpots().find(DISPLAY_ID);
6732 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6733 ASSERT_EQ(size_t(2), iter->second.size());
6734
6735 // Two fingers down at second display.
6736 processPosition(mapper2, x1, y1);
6737 processId(mapper2, 1);
6738 processSlot(mapper2, 1);
6739 processPosition(mapper2, x2, y2);
6740 processId(mapper2, 2);
6741 processSync(mapper2);
6742
6743 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6744 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6745 ASSERT_EQ(size_t(2), iter->second.size());
6746}
6747
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006748TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006749 prepareAxes(POSITION);
6750 addConfigurationProperty("touch.deviceType", "touchScreen");
6751 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006752 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006753
6754 NotifyMotionArgs motionArgs;
6755 // Unrotated video frame
6756 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6757 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006758 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006759 processPosition(mapper, 100, 200);
6760 processSync(mapper);
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6762 ASSERT_EQ(frames, motionArgs.videoFrames);
6763
6764 // Subsequent touch events should not have any videoframes
6765 // This is implemented separately in FakeEventHub,
6766 // but that should match the behaviour of TouchVideoDevice.
6767 processPosition(mapper, 200, 200);
6768 processSync(mapper);
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6771}
6772
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006773TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006774 prepareAxes(POSITION);
6775 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006776 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006777 // Unrotated video frame
6778 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6779 NotifyMotionArgs motionArgs;
6780
6781 // Test all 4 orientations
6782 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6783 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6784 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6785 clearViewports();
6786 prepareDisplay(orientation);
6787 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006788 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006789 processPosition(mapper, 100, 200);
6790 processSync(mapper);
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 frames[0].rotate(orientation);
6793 ASSERT_EQ(frames, motionArgs.videoFrames);
6794 }
6795}
6796
6797TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006798 prepareAxes(POSITION);
6799 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006800 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006801 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6802 // so mix these.
6803 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6804 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6805 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6806 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6807 NotifyMotionArgs motionArgs;
6808
6809 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006810 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006811 processPosition(mapper, 100, 200);
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6814 std::for_each(frames.begin(), frames.end(),
6815 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6816 ASSERT_EQ(frames, motionArgs.videoFrames);
6817}
6818
Arthur Hung9da14732019-09-02 16:16:58 +08006819/**
6820 * If we had defined port associations, but the viewport is not ready, the touch device would be
6821 * expected to be disabled, and it should be enabled after the viewport has found.
6822 */
6823TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006824 constexpr uint8_t hdmi2 = 1;
6825 const std::string secondaryUniqueId = "uniqueId2";
6826 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6827
6828 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6829
6830 addConfigurationProperty("touch.deviceType", "touchScreen");
6831 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006832 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006833
6834 ASSERT_EQ(mDevice->isEnabled(), false);
6835
6836 // Add display on hdmi2, the device should be enabled and can receive touch event.
6837 prepareSecondaryDisplay(type, hdmi2);
6838 ASSERT_EQ(mDevice->isEnabled(), true);
6839
6840 // Send a touch event.
6841 processPosition(mapper, 100, 100);
6842 processSync(mapper);
6843
6844 NotifyMotionArgs args;
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6846 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6847}
6848
Arthur Hung6cd19a42019-08-30 19:04:12 +08006849/**
6850 * Test touch should not work if outside of surface.
6851 */
6852TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
Arthur Hung6cd19a42019-08-30 19:04:12 +08006853 addConfigurationProperty("touch.deviceType", "touchScreen");
6854 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006855 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006856 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung6cd19a42019-08-30 19:04:12 +08006857
Arthur Hung05de5772019-09-26 18:31:26 +08006858 // Touch on left-top area should work.
6859 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6860 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6861 processPosition(mapper, rawX, rawY);
6862 processSync(mapper);
6863
6864 NotifyMotionArgs args;
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6866
6867 // Reset.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006868 mapper.reset(ARBITRARY_TIME);
Arthur Hung05de5772019-09-26 18:31:26 +08006869
6870 // Let logical display be different to physical display and rotate 90-degrees.
6871 std::optional<DisplayViewport> internalViewport =
6872 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6873 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6874 internalViewport->logicalLeft = 0;
6875 internalViewport->logicalTop = 0;
6876 internalViewport->logicalRight = DISPLAY_HEIGHT;
6877 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6878
6879 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6880 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6881 internalViewport->physicalRight = DISPLAY_HEIGHT;
6882 internalViewport->physicalBottom = DISPLAY_WIDTH;
6883
6884 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6885 internalViewport->deviceHeight = DISPLAY_WIDTH;
6886 mFakePolicy->updateViewport(internalViewport.value());
6887 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6888
6889 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006890 processPosition(mapper, rawX, rawY);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6893}
6894
Arthur Hung421eb1c2020-01-16 00:09:42 +08006895TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006896 addConfigurationProperty("touch.deviceType", "touchScreen");
6897 prepareDisplay(DISPLAY_ORIENTATION_0);
6898 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006899 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006900
6901 NotifyMotionArgs motionArgs;
6902
6903 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6904 // finger down
6905 processId(mapper, 1);
6906 processPosition(mapper, x1, y1);
6907 processSync(mapper);
6908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6909 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6911
6912 // finger move
6913 processId(mapper, 1);
6914 processPosition(mapper, x2, y2);
6915 processSync(mapper);
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6919
6920 // finger up.
6921 processId(mapper, -1);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6924 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6926
6927 // new finger down
6928 processId(mapper, 1);
6929 processPosition(mapper, x3, y3);
6930 processSync(mapper);
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6932 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6934}
6935
6936/**
6937 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6938 * UP events should be ignored.
6939 */
6940TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006941 addConfigurationProperty("touch.deviceType", "touchScreen");
6942 prepareDisplay(DISPLAY_ORIENTATION_0);
6943 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006944 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006945
6946 NotifyMotionArgs motionArgs;
6947
6948 // default tool type is finger
6949 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6950 processId(mapper, 1);
6951 processPosition(mapper, x1, y1);
6952 processSync(mapper);
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6954 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6956
6957 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6958 processToolType(mapper, MT_TOOL_PALM);
6959 processSync(mapper);
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6961 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6962
6963 // Ignore the following MOVE and UP events if had detect a palm event.
6964 processId(mapper, 1);
6965 processPosition(mapper, x2, y2);
6966 processSync(mapper);
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6968
6969 // finger up.
6970 processId(mapper, -1);
6971 processSync(mapper);
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6973
6974 // new finger down
6975 processToolType(mapper, MT_TOOL_FINGER);
6976 processId(mapper, 1);
6977 processPosition(mapper, x3, y3);
6978 processSync(mapper);
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6980 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6981 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6982}
6983
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006984// --- MultiTouchInputMapperTest_ExternalDevice ---
6985
6986class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
6987protected:
6988 virtual void SetUp() override {
6989 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
6990 }
6991};
6992
6993/**
6994 * Expect fallback to internal viewport if device is external and external viewport is not present.
6995 */
6996TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
6997 prepareAxes(POSITION);
6998 addConfigurationProperty("touch.deviceType", "touchScreen");
6999 prepareDisplay(DISPLAY_ORIENTATION_0);
7000 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7001
7002 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7003
7004 NotifyMotionArgs motionArgs;
7005
7006 // Expect the event to be sent to the internal viewport,
7007 // because an external viewport is not present.
7008 processPosition(mapper, 100, 100);
7009 processSync(mapper);
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7011 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7012
7013 // Expect the event to be sent to the external viewport if it is present.
7014 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
7015 processPosition(mapper, 100, 100);
7016 processSync(mapper);
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7018 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7019}
7020
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021} // namespace android