blob: 4da9d938908f5df7b4083cdba0c16a0c589b0796 [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;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800849 uint32_t mNextSequenceNum;
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),
860 mNextSequenceNum(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
944 virtual uint32_t getNextSequenceNum() {
945 return mNextSequenceNum++;
946 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947};
948
949
950// --- FakeInputMapper ---
951
952class FakeInputMapper : public InputMapper {
953 uint32_t mSources;
954 int32_t mKeyboardType;
955 int32_t mMetaState;
956 KeyedVector<int32_t, int32_t> mKeyCodeStates;
957 KeyedVector<int32_t, int32_t> mScanCodeStates;
958 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800959 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700961 std::mutex mLock;
962 std::condition_variable mStateChangedCondition;
963 bool mConfigureWasCalled GUARDED_BY(mLock);
964 bool mResetWasCalled GUARDED_BY(mLock);
965 bool mProcessWasCalled GUARDED_BY(mLock);
966 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Arthur Hungc23540e2018-11-29 20:42:11 +0800968 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800970 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
971 : InputMapper(deviceContext),
972 mSources(sources),
973 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800975 mConfigureWasCalled(false),
976 mResetWasCalled(false),
977 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978
979 virtual ~FakeInputMapper() { }
980
981 void setKeyboardType(int32_t keyboardType) {
982 mKeyboardType = keyboardType;
983 }
984
985 void setMetaState(int32_t metaState) {
986 mMetaState = metaState;
987 }
988
989 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700990 std::unique_lock<std::mutex> lock(mLock);
991 base::ScopedLockAssertion assumeLocked(mLock);
992 const bool configureCalled =
993 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
994 return mConfigureWasCalled;
995 });
996 if (!configureCalled) {
997 FAIL() << "Expected configure() to have been called.";
998 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999 mConfigureWasCalled = false;
1000 }
1001
1002 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001003 std::unique_lock<std::mutex> lock(mLock);
1004 base::ScopedLockAssertion assumeLocked(mLock);
1005 const bool resetCalled =
1006 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1007 return mResetWasCalled;
1008 });
1009 if (!resetCalled) {
1010 FAIL() << "Expected reset() to have been called.";
1011 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012 mResetWasCalled = false;
1013 }
1014
Yi Kong9b14ac62018-07-17 13:48:38 -07001015 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001016 std::unique_lock<std::mutex> lock(mLock);
1017 base::ScopedLockAssertion assumeLocked(mLock);
1018 const bool processCalled =
1019 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1020 return mProcessWasCalled;
1021 });
1022 if (!processCalled) {
1023 FAIL() << "Expected process() to have been called.";
1024 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 if (outLastEvent) {
1026 *outLastEvent = mLastEvent;
1027 }
1028 mProcessWasCalled = false;
1029 }
1030
1031 void setKeyCodeState(int32_t keyCode, int32_t state) {
1032 mKeyCodeStates.replaceValueFor(keyCode, state);
1033 }
1034
1035 void setScanCodeState(int32_t scanCode, int32_t state) {
1036 mScanCodeStates.replaceValueFor(scanCode, state);
1037 }
1038
1039 void setSwitchState(int32_t switchCode, int32_t state) {
1040 mSwitchStates.replaceValueFor(switchCode, state);
1041 }
1042
1043 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001044 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045 }
1046
1047private:
1048 virtual uint32_t getSources() {
1049 return mSources;
1050 }
1051
1052 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1053 InputMapper::populateDeviceInfo(deviceInfo);
1054
1055 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1056 deviceInfo->setKeyboardType(mKeyboardType);
1057 }
1058 }
1059
Arthur Hungc23540e2018-11-29 20:42:11 +08001060 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001061 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001063
1064 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001065 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1067 mViewport = config->getDisplayViewportByPort(*displayPort);
1068 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001069
1070 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071 }
1072
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001073 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001074 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 }
1078
1079 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001080 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 mLastEvent = *rawEvent;
1082 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001083 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084 }
1085
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001086 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1088 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1089 }
1090
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001091 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1093 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1094 }
1095
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001096 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1098 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1099 }
1100
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001101 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 const int32_t* keyCodes, uint8_t* outFlags) {
1103 bool result = false;
1104 for (size_t i = 0; i < numCodes; i++) {
1105 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1106 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1107 outFlags[i] = 1;
1108 result = true;
1109 }
1110 }
1111 }
1112 return result;
1113 }
1114
1115 virtual int32_t getMetaState() {
1116 return mMetaState;
1117 }
1118
1119 virtual void fadePointer() {
1120 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001121
1122 virtual std::optional<int32_t> getAssociatedDisplay() {
1123 if (mViewport) {
1124 return std::make_optional(mViewport->displayId);
1125 }
1126 return std::nullopt;
1127 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128};
1129
1130
1131// --- InstrumentedInputReader ---
1132
1133class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001134 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135
1136public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001137 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1138 const sp<InputReaderPolicyInterface>& policy,
1139 const sp<InputListenerInterface>& listener)
1140 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001142 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001144 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001145
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001146 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001147 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 InputDeviceIdentifier identifier;
1149 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001150 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001152 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153 }
1154
Prabir Pradhan28efc192019-11-05 01:10:04 +00001155 // Make the protected loopOnce method accessible to tests.
1156 using InputReader::loopOnce;
1157
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001159 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1160 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001162 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001163 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164 return device;
1165 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001166 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 }
1168
1169 friend class InputReaderTest;
1170};
1171
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001172// --- InputReaderPolicyTest ---
1173class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001174protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001175 sp<FakeInputReaderPolicy> mFakePolicy;
1176
Prabir Pradhan28efc192019-11-05 01:10:04 +00001177 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1178 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179};
1180
1181/**
1182 * Check that empty set of viewports is an acceptable configuration.
1183 * Also try to get internal viewport two different ways - by type and by uniqueId.
1184 *
1185 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1186 * Such configuration is not currently allowed.
1187 */
1188TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001189 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001190
1191 // We didn't add any viewports yet, so there shouldn't be any.
1192 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001193 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001194 ASSERT_FALSE(internalViewport);
1195
1196 // Add an internal viewport, then clear it
1197 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001198 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001199
1200 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001201 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001203 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001204
1205 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001206 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001208 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001209
1210 mFakePolicy->clearViewports();
1211 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001212 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001214 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001215 ASSERT_FALSE(internalViewport);
1216}
1217
1218TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1219 const std::string internalUniqueId = "local:0";
1220 const std::string externalUniqueId = "local:1";
1221 const std::string virtualUniqueId1 = "virtual:2";
1222 const std::string virtualUniqueId2 = "virtual:3";
1223 constexpr int32_t virtualDisplayId1 = 2;
1224 constexpr int32_t virtualDisplayId2 = 3;
1225
1226 // Add an internal viewport
1227 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001228 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001229 // Add an external viewport
1230 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001231 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001232 // Add an virtual viewport
1233 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001234 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001235 // Add another virtual viewport
1236 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001237 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001238
1239 // Check matching by type for internal
1240 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001241 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001242 ASSERT_TRUE(internalViewport);
1243 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1244
1245 // Check matching by type for external
1246 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001247 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001248 ASSERT_TRUE(externalViewport);
1249 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1250
1251 // Check matching by uniqueId for virtual viewport #1
1252 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001253 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001255 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001256 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1257 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1258
1259 // Check matching by uniqueId for virtual viewport #2
1260 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001261 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001262 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001263 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1265 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1266}
1267
1268
1269/**
1270 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1271 * that lookup works by checking display id.
1272 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1273 */
1274TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1275 const std::string uniqueId1 = "uniqueId1";
1276 const std::string uniqueId2 = "uniqueId2";
1277 constexpr int32_t displayId1 = 2;
1278 constexpr int32_t displayId2 = 3;
1279
1280 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1281 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1282 for (const ViewportType& type : types) {
1283 mFakePolicy->clearViewports();
1284 // Add a viewport
1285 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001286 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001287 // Add another viewport
1288 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001289 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001290
1291 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001292 std::optional<DisplayViewport> viewport1 =
1293 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001294 ASSERT_TRUE(viewport1);
1295 ASSERT_EQ(displayId1, viewport1->displayId);
1296 ASSERT_EQ(type, viewport1->type);
1297
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001298 std::optional<DisplayViewport> viewport2 =
1299 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001300 ASSERT_TRUE(viewport2);
1301 ASSERT_EQ(displayId2, viewport2->displayId);
1302 ASSERT_EQ(type, viewport2->type);
1303
1304 // When there are multiple viewports of the same kind, and uniqueId is not specified
1305 // in the call to getDisplayViewport, then that situation is not supported.
1306 // The viewports can be stored in any order, so we cannot rely on the order, since that
1307 // is just implementation detail.
1308 // However, we can check that it still returns *a* viewport, we just cannot assert
1309 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001310 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001311 ASSERT_TRUE(someViewport);
1312 }
1313}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001314
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001315/**
1316 * Check getDisplayViewportByPort
1317 */
1318TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1319 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1320 const std::string uniqueId1 = "uniqueId1";
1321 const std::string uniqueId2 = "uniqueId2";
1322 constexpr int32_t displayId1 = 1;
1323 constexpr int32_t displayId2 = 2;
1324 const uint8_t hdmi1 = 0;
1325 const uint8_t hdmi2 = 1;
1326 const uint8_t hdmi3 = 2;
1327
1328 mFakePolicy->clearViewports();
1329 // Add a viewport that's associated with some display port that's not of interest.
1330 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1331 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1332 // Add another viewport, connected to HDMI1 port
1333 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1334 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1335
1336 // Check that correct display viewport was returned by comparing the display ports.
1337 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1338 ASSERT_TRUE(hdmi1Viewport);
1339 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1340 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1341
1342 // Check that we can still get the same viewport using the uniqueId
1343 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1344 ASSERT_TRUE(hdmi1Viewport);
1345 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1346 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1347 ASSERT_EQ(type, hdmi1Viewport->type);
1348
1349 // Check that we cannot find a port with "HDMI2", because we never added one
1350 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1351 ASSERT_FALSE(hdmi2Viewport);
1352}
1353
Michael Wrightd02c5b62014-02-10 15:10:22 -08001354// --- InputReaderTest ---
1355
1356class InputReaderTest : public testing::Test {
1357protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001358 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001359 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001360 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001361 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001362
Prabir Pradhan28efc192019-11-05 01:10:04 +00001363 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001364 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001366 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001367
Prabir Pradhan28efc192019-11-05 01:10:04 +00001368 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1369 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370 }
1371
Prabir Pradhan28efc192019-11-05 01:10:04 +00001372 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373 mFakeListener.clear();
1374 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001375 }
1376
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001377 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1378 const PropertyMap* configuration) {
1379 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380
1381 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001382 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383 }
1384 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001385 mReader->loopOnce();
1386 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1388 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001389 }
1390
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001391 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001392 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001393 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001394 }
1395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001396 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001397 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001398 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001399 }
1400
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001401 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001402 const std::string& name, uint32_t classes,
1403 uint32_t sources,
1404 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001405 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1406 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001407 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001408 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001409 return mapper;
1410 }
1411};
1412
1413TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001414 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001415 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001416 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001417 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001419 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001420 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001421 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001422 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001423 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001424 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1425 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1426 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1427
1428 // Should also have received a notification describing the new input devices.
1429 inputDevices = mFakePolicy->getInputDevices();
1430 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001431 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001432 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001433 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1434 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1435 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1436}
1437
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001438TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001439 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001440 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001441 constexpr int32_t eventHubId = 1;
1442 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001443 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001444 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001445 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001446 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001447
Yi Kong9b14ac62018-07-17 13:48:38 -07001448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001449
1450 NotifyDeviceResetArgs resetArgs;
1451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001452 ASSERT_EQ(deviceId, resetArgs.deviceId);
1453
1454 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001455 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001456 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001457
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001459 ASSERT_EQ(deviceId, resetArgs.deviceId);
1460 ASSERT_EQ(device->isEnabled(), false);
1461
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001462 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001463 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001466 ASSERT_EQ(device->isEnabled(), false);
1467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001468 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001469 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001471 ASSERT_EQ(deviceId, resetArgs.deviceId);
1472 ASSERT_EQ(device->isEnabled(), true);
1473}
1474
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001476 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1477 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1478 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001479 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001480 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001481 AINPUT_SOURCE_KEYBOARD, nullptr);
1482 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001483
1484 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1485 AINPUT_SOURCE_ANY, AKEYCODE_A))
1486 << "Should return unknown when the device id is >= 0 but unknown.";
1487
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001488 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1489 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1490 << "Should return unknown when the device id is valid but the sources are not "
1491 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001492
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001493 ASSERT_EQ(AKEY_STATE_DOWN,
1494 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1495 AKEYCODE_A))
1496 << "Should return value provided by mapper when device id is valid and the device "
1497 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001498
1499 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1500 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1501 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1502
1503 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1504 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1505 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1506}
1507
1508TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001509 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1510 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1511 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001512 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001513 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001514 AINPUT_SOURCE_KEYBOARD, nullptr);
1515 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516
1517 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1518 AINPUT_SOURCE_ANY, KEY_A))
1519 << "Should return unknown when the device id is >= 0 but unknown.";
1520
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001521 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1522 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1523 << "Should return unknown when the device id is valid but the sources are not "
1524 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001525
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001526 ASSERT_EQ(AKEY_STATE_DOWN,
1527 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1528 KEY_A))
1529 << "Should return value provided by mapper when device id is valid and the device "
1530 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001531
1532 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1533 AINPUT_SOURCE_TRACKBALL, KEY_A))
1534 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1535
1536 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1537 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1538 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1539}
1540
1541TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001542 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1543 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1544 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001545 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001546 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001547 AINPUT_SOURCE_KEYBOARD, nullptr);
1548 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001549
1550 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1551 AINPUT_SOURCE_ANY, SW_LID))
1552 << "Should return unknown when the device id is >= 0 but unknown.";
1553
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001554 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1555 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1556 << "Should return unknown when the device id is valid but the sources are not "
1557 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001559 ASSERT_EQ(AKEY_STATE_DOWN,
1560 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1561 SW_LID))
1562 << "Should return value provided by mapper when device id is valid and the device "
1563 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564
1565 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1566 AINPUT_SOURCE_TRACKBALL, SW_LID))
1567 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1568
1569 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1570 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1571 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1572}
1573
1574TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001575 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1576 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1577 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001578 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001579 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001580 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001582 mapper.addSupportedKeyCode(AKEYCODE_A);
1583 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584
1585 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1586 uint8_t flags[4] = { 0, 0, 0, 1 };
1587
1588 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1589 << "Should return false when device id is >= 0 but unknown.";
1590 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1591
1592 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001593 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1594 << "Should return false when device id is valid but the sources are not supported by "
1595 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1597
1598 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001599 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1600 keyCodes, flags))
1601 << "Should return value provided by mapper when device id is valid and the device "
1602 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1604
1605 flags[3] = 1;
1606 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1607 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1608 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1609
1610 flags[3] = 1;
1611 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1612 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1613 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1614}
1615
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001616TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001617 constexpr int32_t eventHubId = 1;
1618 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619
1620 NotifyConfigurationChangedArgs args;
1621
1622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1623 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1624}
1625
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001626TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1628 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1629 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001630 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001631 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001632 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001634 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001635 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001636 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1637
1638 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001639 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642 ASSERT_EQ(EV_KEY, event.type);
1643 ASSERT_EQ(KEY_A, event.code);
1644 ASSERT_EQ(1, event.value);
1645}
1646
Prabir Pradhan42611e02018-11-27 14:04:02 -08001647TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001649 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001650 constexpr int32_t eventHubId = 1;
1651 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001652 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001654 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001656
1657 NotifyDeviceResetArgs resetArgs;
1658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1659 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001661 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001662 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001664 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1665 prevSequenceNum = resetArgs.sequenceNum;
1666
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001667 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001668 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001670 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1671 prevSequenceNum = resetArgs.sequenceNum;
1672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001673 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001674 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001676 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1677 prevSequenceNum = resetArgs.sequenceNum;
1678}
1679
Arthur Hungc23540e2018-11-29 20:42:11 +08001680TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001682 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001684 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1686 FakeInputMapper& mapper =
1687 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001688 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001689
1690 const uint8_t hdmi1 = 1;
1691
1692 // Associated touch screen with second display.
1693 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1694
1695 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001696 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001697 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1698 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1699 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1700 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1701 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001702 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001703
1704 // Add the device, and make sure all of the callbacks are triggered.
1705 // The device is added after the input port associations are processed since
1706 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001710 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001711
Arthur Hung2c9a3342019-07-23 14:18:59 +08001712 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001713 ASSERT_EQ(deviceId, device->getId());
1714 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1715 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001716
1717 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001718 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001719 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001720 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001721}
1722
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001723// --- InputReaderIntegrationTest ---
1724
1725// These tests create and interact with the InputReader only through its interface.
1726// The InputReader is started during SetUp(), which starts its processing in its own
1727// thread. The tests use linux uinput to emulate input devices.
1728// NOTE: Interacting with the physical device while these tests are running may cause
1729// the tests to fail.
1730class InputReaderIntegrationTest : public testing::Test {
1731protected:
1732 sp<TestInputListener> mTestListener;
1733 sp<FakeInputReaderPolicy> mFakePolicy;
1734 sp<InputReaderInterface> mReader;
1735
1736 virtual void SetUp() override {
1737 mFakePolicy = new FakeInputReaderPolicy();
1738 mTestListener = new TestInputListener();
1739
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001740 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001741 ASSERT_EQ(mReader->start(), OK);
1742
1743 // Since this test is run on a real device, all the input devices connected
1744 // to the test device will show up in mReader. We wait for those input devices to
1745 // show up before beginning the tests.
1746 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1747 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1748 }
1749
1750 virtual void TearDown() override {
1751 ASSERT_EQ(mReader->stop(), OK);
1752 mTestListener.clear();
1753 mFakePolicy.clear();
1754 }
1755};
1756
1757TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1758 // An invalid input device that is only used for this test.
1759 class InvalidUinputDevice : public UinputDevice {
1760 public:
1761 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1762
1763 private:
1764 void configureDevice(int fd, uinput_user_dev* device) override {}
1765 };
1766
1767 const size_t numDevices = mFakePolicy->getInputDevices().size();
1768
1769 // UinputDevice does not set any event or key bits, so InputReader should not
1770 // consider it as a valid device.
1771 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1772 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1773 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1774 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1775
1776 invalidDevice.reset();
1777 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1778 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1779 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1780}
1781
1782TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1783 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1784
1785 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1786 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1787 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1788 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1789
1790 // Find the test device by its name.
1791 std::vector<InputDeviceInfo> inputDevices;
1792 mReader->getInputDevices(inputDevices);
1793 InputDeviceInfo* keyboardInfo = nullptr;
1794 const char* keyboardName = keyboard->getName();
1795 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1796 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1797 keyboardInfo = &inputDevices[i];
1798 break;
1799 }
1800 }
1801 ASSERT_NE(keyboardInfo, nullptr);
1802 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1803 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1804 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1805
1806 keyboard.reset();
1807 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1808 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1809 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1810}
1811
1812TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1813 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1814 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1815
1816 NotifyConfigurationChangedArgs configChangedArgs;
1817 ASSERT_NO_FATAL_FAILURE(
1818 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
1819 uint32_t prevSequenceNum = configChangedArgs.sequenceNum;
1820 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1821
1822 NotifyKeyArgs keyArgs;
1823 keyboard->pressAndReleaseHomeKey();
1824 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1825 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
1826 ASSERT_LT(prevSequenceNum, keyArgs.sequenceNum);
1827 prevSequenceNum = keyArgs.sequenceNum;
1828 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1829 prevTimestamp = keyArgs.eventTime;
1830
1831 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1832 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
1833 ASSERT_LT(prevSequenceNum, keyArgs.sequenceNum);
1834 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1835}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001836
1837// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001838class InputDeviceTest : public testing::Test {
1839protected:
1840 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001841 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842 static const int32_t DEVICE_ID;
1843 static const int32_t DEVICE_GENERATION;
1844 static const int32_t DEVICE_CONTROLLER_NUMBER;
1845 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001846 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001847
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001848 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001850 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851 FakeInputReaderContext* mFakeContext;
1852
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001853 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001854
Prabir Pradhan28efc192019-11-05 01:10:04 +00001855 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001856 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001858 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001861 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001862 InputDeviceIdentifier identifier;
1863 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001864 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001865 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1866 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001867 }
1868
Prabir Pradhan28efc192019-11-05 01:10:04 +00001869 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001870 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001871 delete mFakeContext;
1872 mFakeListener.clear();
1873 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001874 }
1875};
1876
1877const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001878const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001879const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1881const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1882const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1883 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001884const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001885
1886TEST_F(InputDeviceTest, ImmutableProperties) {
1887 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001888 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001889 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890}
1891
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
1893 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001894}
1895
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1897 // Configuration.
1898 InputReaderConfiguration config;
1899 mDevice->configure(ARBITRARY_TIME, &config, 0);
1900
1901 // Reset.
1902 mDevice->reset(ARBITRARY_TIME);
1903
1904 NotifyDeviceResetArgs resetArgs;
1905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1906 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1907 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1908
1909 // Metadata.
1910 ASSERT_TRUE(mDevice->isIgnored());
1911 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1912
1913 InputDeviceInfo info;
1914 mDevice->getDeviceInfo(&info);
1915 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001916 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1918 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1919
1920 // State queries.
1921 ASSERT_EQ(0, mDevice->getMetaState());
1922
1923 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1924 << "Ignored device should return unknown key code state.";
1925 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1926 << "Ignored device should return unknown scan code state.";
1927 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1928 << "Ignored device should return unknown switch state.";
1929
1930 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1931 uint8_t flags[2] = { 0, 1 };
1932 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1933 << "Ignored device should never mark any key codes.";
1934 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1935 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1936}
1937
1938TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1939 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001940 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001941
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001942 FakeInputMapper& mapper1 =
1943 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001944 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1945 mapper1.setMetaState(AMETA_ALT_ON);
1946 mapper1.addSupportedKeyCode(AKEYCODE_A);
1947 mapper1.addSupportedKeyCode(AKEYCODE_B);
1948 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1949 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1950 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
1951 mapper1.setScanCodeState(3, AKEY_STATE_UP);
1952 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001953
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001954 FakeInputMapper& mapper2 =
1955 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001956 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001957
1958 InputReaderConfiguration config;
1959 mDevice->configure(ARBITRARY_TIME, &config, 0);
1960
1961 String8 propertyValue;
1962 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1963 << "Device should have read configuration during configuration phase.";
1964 ASSERT_STREQ("value", propertyValue.string());
1965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001966 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
1967 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001968
1969 // Reset
1970 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001971 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
1972 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001973
1974 NotifyDeviceResetArgs resetArgs;
1975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1976 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1977 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1978
1979 // Metadata.
1980 ASSERT_FALSE(mDevice->isIgnored());
1981 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1982
1983 InputDeviceInfo info;
1984 mDevice->getDeviceInfo(&info);
1985 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001986 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001987 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1988 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1989
1990 // State queries.
1991 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1992 << "Should query mappers and combine meta states.";
1993
1994 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1995 << "Should return unknown key code state when source not supported.";
1996 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1997 << "Should return unknown scan code state when source not supported.";
1998 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1999 << "Should return unknown switch state when source not supported.";
2000
2001 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2002 << "Should query mapper when source is supported.";
2003 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2004 << "Should query mapper when source is supported.";
2005 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2006 << "Should query mapper when source is supported.";
2007
2008 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2009 uint8_t flags[4] = { 0, 0, 0, 1 };
2010 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2011 << "Should do nothing when source is unsupported.";
2012 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2013 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2014 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2015 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2016
2017 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2018 << "Should query mapper when source is supported.";
2019 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2020 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2021 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2022 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2023
2024 // Event handling.
2025 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002026 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002027 mDevice->process(&event, 1);
2028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002029 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2030 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002031}
2032
Arthur Hung2c9a3342019-07-23 14:18:59 +08002033// A single input device is associated with a specific display. Check that:
2034// 1. Device is disabled if the viewport corresponding to the associated display is not found
2035// 2. Device is disabled when setEnabled API is called
2036TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002037 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002038
2039 // First Configuration.
2040 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2041
2042 // Device should be enabled by default.
2043 ASSERT_TRUE(mDevice->isEnabled());
2044
2045 // Prepare associated info.
2046 constexpr uint8_t hdmi = 1;
2047 const std::string UNIQUE_ID = "local:1";
2048
2049 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2050 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2051 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2052 // Device should be disabled because it is associated with a specific display via
2053 // input port <-> display port association, but the corresponding display is not found
2054 ASSERT_FALSE(mDevice->isEnabled());
2055
2056 // Prepare displays.
2057 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2058 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2059 ViewportType::VIEWPORT_INTERNAL);
2060 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2061 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2062 ASSERT_TRUE(mDevice->isEnabled());
2063
2064 // Device should be disabled after set disable.
2065 mFakePolicy->addDisabledDevice(mDevice->getId());
2066 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2067 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2068 ASSERT_FALSE(mDevice->isEnabled());
2069
2070 // Device should still be disabled even found the associated display.
2071 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2072 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2073 ASSERT_FALSE(mDevice->isEnabled());
2074}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002075
2076// --- InputMapperTest ---
2077
2078class InputMapperTest : public testing::Test {
2079protected:
2080 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002081 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002082 static const int32_t DEVICE_ID;
2083 static const int32_t DEVICE_GENERATION;
2084 static const int32_t DEVICE_CONTROLLER_NUMBER;
2085 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002086 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002087
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002088 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002089 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002090 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002091 FakeInputReaderContext* mFakeContext;
2092 InputDevice* mDevice;
2093
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002094 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002095 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002097 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002098 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2099 InputDeviceIdentifier identifier;
2100 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002101 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002102 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002103
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002104 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002105 }
2106
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002107 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2108
Prabir Pradhan28efc192019-11-05 01:10:04 +00002109 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002110 delete mDevice;
2111 delete mFakeContext;
2112 mFakeListener.clear();
2113 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002114 }
2115
2116 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002117 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002118 }
2119
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002120 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002121 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2122 mFakeContext->updatePointerDisplay();
2123 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002124 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2125 }
2126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002127 template <class T, typename... Args>
2128 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002129 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002130 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002131 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002132 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133 }
2134
2135 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002136 int32_t orientation, const std::string& uniqueId,
2137 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002138 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002139 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002140 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2141 }
2142
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002143 void clearViewports() {
2144 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002145 }
2146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002147 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2148 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002149 RawEvent event;
2150 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002151 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002152 event.type = type;
2153 event.code = code;
2154 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002155 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002156 }
2157
2158 static void assertMotionRange(const InputDeviceInfo& info,
2159 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2160 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002161 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002162 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2163 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2164 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2165 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2166 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2167 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2168 }
2169
2170 static void assertPointerCoords(const PointerCoords& coords,
2171 float x, float y, float pressure, float size,
2172 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2173 float orientation, float distance) {
2174 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2175 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2176 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2177 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2178 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2179 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2180 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2181 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2182 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2183 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2184 }
2185
2186 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2187 float actualX, actualY;
2188 controller->getPosition(&actualX, &actualY);
2189 ASSERT_NEAR(x, actualX, 1);
2190 ASSERT_NEAR(y, actualY, 1);
2191 }
2192};
2193
2194const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002195const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002196const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002197const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2198const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2199const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002200const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002201
2202// --- SwitchInputMapperTest ---
2203
2204class SwitchInputMapperTest : public InputMapperTest {
2205protected:
2206};
2207
2208TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002209 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002211 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002212}
2213
2214TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002215 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002216
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002217 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002218 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002219
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002220 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002221 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002222}
2223
2224TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002225 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002226
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002227 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2228 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2229 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2230 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002231
2232 NotifySwitchArgs args;
2233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2234 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002235 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2236 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002237 args.switchMask);
2238 ASSERT_EQ(uint32_t(0), args.policyFlags);
2239}
2240
2241
2242// --- KeyboardInputMapperTest ---
2243
2244class KeyboardInputMapperTest : public InputMapperTest {
2245protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002246 const std::string UNIQUE_ID = "local:0";
2247
2248 void prepareDisplay(int32_t orientation);
2249
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002250 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002251 int32_t originalKeyCode, int32_t rotatedKeyCode,
2252 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002253};
2254
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002255/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2256 * orientation.
2257 */
2258void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2259 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002260 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002261}
2262
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002263void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002264 int32_t originalScanCode, int32_t originalKeyCode,
2265 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002266 NotifyKeyArgs args;
2267
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002268 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2270 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2271 ASSERT_EQ(originalScanCode, args.scanCode);
2272 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002273 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002275 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2277 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2278 ASSERT_EQ(originalScanCode, args.scanCode);
2279 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002280 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281}
2282
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002284 KeyboardInputMapper& mapper =
2285 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2286 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002288 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289}
2290
2291TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2292 const int32_t USAGE_A = 0x070004;
2293 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002294 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2295 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002296
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002297 KeyboardInputMapper& mapper =
2298 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2299 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002300
2301 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002302 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303 NotifyKeyArgs args;
2304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2305 ASSERT_EQ(DEVICE_ID, args.deviceId);
2306 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2307 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2308 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2309 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2310 ASSERT_EQ(KEY_HOME, args.scanCode);
2311 ASSERT_EQ(AMETA_NONE, args.metaState);
2312 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2313 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2314 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2315
2316 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002317 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2319 ASSERT_EQ(DEVICE_ID, args.deviceId);
2320 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2321 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2322 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2323 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2324 ASSERT_EQ(KEY_HOME, args.scanCode);
2325 ASSERT_EQ(AMETA_NONE, args.metaState);
2326 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2327 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2328 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2329
2330 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002331 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2332 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2334 ASSERT_EQ(DEVICE_ID, args.deviceId);
2335 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2336 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2337 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2338 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2339 ASSERT_EQ(0, args.scanCode);
2340 ASSERT_EQ(AMETA_NONE, args.metaState);
2341 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2342 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2343 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2344
2345 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002346 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2347 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2349 ASSERT_EQ(DEVICE_ID, args.deviceId);
2350 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2351 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2353 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2354 ASSERT_EQ(0, args.scanCode);
2355 ASSERT_EQ(AMETA_NONE, args.metaState);
2356 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2357 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2358 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2359
2360 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002361 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2362 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2364 ASSERT_EQ(DEVICE_ID, args.deviceId);
2365 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2366 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2367 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2368 ASSERT_EQ(0, args.keyCode);
2369 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2370 ASSERT_EQ(AMETA_NONE, args.metaState);
2371 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2372 ASSERT_EQ(0U, args.policyFlags);
2373 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2374
2375 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002376 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2377 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2379 ASSERT_EQ(DEVICE_ID, args.deviceId);
2380 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2381 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2382 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2383 ASSERT_EQ(0, args.keyCode);
2384 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2385 ASSERT_EQ(AMETA_NONE, args.metaState);
2386 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2387 ASSERT_EQ(0U, args.policyFlags);
2388 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2389}
2390
2391TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002392 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2393 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002395 KeyboardInputMapper& mapper =
2396 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2397 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398
2399 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002400 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401
2402 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002403 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404 NotifyKeyArgs args;
2405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2406 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002407 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2409
2410 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002411 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002414 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415
2416 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002417 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2419 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002420 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002421
2422 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002423 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2425 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002426 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2428}
2429
2430TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002431 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2432 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2433 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2434 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002436 KeyboardInputMapper& mapper =
2437 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2438 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002440 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002441 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2442 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2443 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2444 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2445 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2446 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2447 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2448 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2449}
2450
2451TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002452 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2453 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2454 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2455 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002458 KeyboardInputMapper& mapper =
2459 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2460 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002462 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002463 ASSERT_NO_FATAL_FAILURE(
2464 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2465 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2466 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2467 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2468 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2469 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2470 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002472 clearViewports();
2473 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002474 ASSERT_NO_FATAL_FAILURE(
2475 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2476 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2477 AKEYCODE_DPAD_UP, DISPLAY_ID));
2478 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2479 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2480 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2481 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002482
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002483 clearViewports();
2484 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002485 ASSERT_NO_FATAL_FAILURE(
2486 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2487 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2488 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2489 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2490 AKEYCODE_DPAD_UP, DISPLAY_ID));
2491 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2492 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002493
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002494 clearViewports();
2495 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002496 ASSERT_NO_FATAL_FAILURE(
2497 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2498 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2499 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2500 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2501 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2502 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2503 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002504
2505 // Special case: if orientation changes while key is down, we still emit the same keycode
2506 // in the key up as we did in the key down.
2507 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002508 clearViewports();
2509 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002510 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2512 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2513 ASSERT_EQ(KEY_UP, args.scanCode);
2514 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2515
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002516 clearViewports();
2517 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002518 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2520 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2521 ASSERT_EQ(KEY_UP, args.scanCode);
2522 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2523}
2524
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002525TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2526 // If the keyboard is not orientation aware,
2527 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002528 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002529
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002530 KeyboardInputMapper& mapper =
2531 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2532 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002533 NotifyKeyArgs args;
2534
2535 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002536 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002538 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2540 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2541
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002542 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002543 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002545 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2547 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2548}
2549
2550TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2551 // If the keyboard is orientation aware,
2552 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002553 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002554
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002555 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002556 KeyboardInputMapper& mapper =
2557 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2558 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002559 NotifyKeyArgs args;
2560
2561 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2562 // ^--- already checked by the previous test
2563
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002564 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002565 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002566 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002568 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2570 ASSERT_EQ(DISPLAY_ID, args.displayId);
2571
2572 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002573 clearViewports();
2574 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002575 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002576 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002578 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2580 ASSERT_EQ(newDisplayId, args.displayId);
2581}
2582
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002584 KeyboardInputMapper& mapper =
2585 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2586 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002588 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002589 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002591 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002592 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593}
2594
2595TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002596 KeyboardInputMapper& mapper =
2597 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2598 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002600 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002601 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002603 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002604 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002605}
2606
2607TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002608 KeyboardInputMapper& mapper =
2609 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2610 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002612 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
2614 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2615 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002616 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617 ASSERT_TRUE(flags[0]);
2618 ASSERT_FALSE(flags[1]);
2619}
2620
2621TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002622 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2623 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2624 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2625 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2626 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2627 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002629 KeyboardInputMapper& mapper =
2630 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632
2633 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002634 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2635 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2636 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637
2638 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002639 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2640 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002641 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2642 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2643 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002644 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645
2646 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002647 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2648 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002649 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2650 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2651 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002652 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
2654 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002655 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2656 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002657 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2658 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2659 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002660 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661
2662 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002663 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2664 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002665 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2666 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2667 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002668 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669
2670 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002671 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2672 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002673 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2674 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2675 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002676 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677
2678 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002679 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2680 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002681 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2682 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2683 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002684 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685}
2686
Arthur Hung2c9a3342019-07-23 14:18:59 +08002687TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2688 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002689 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2691 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2692 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002693
2694 // keyboard 2.
2695 const std::string USB2 = "USB2";
2696 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002697 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002698 InputDeviceIdentifier identifier;
2699 identifier.name = "KEYBOARD2";
2700 identifier.location = USB2;
2701 std::unique_ptr<InputDevice> device2 =
2702 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002703 identifier);
2704 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2705 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2706 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2707 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2708 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002710 KeyboardInputMapper& mapper =
2711 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2712 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002713
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002714 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002715 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002716 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002717 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2718 device2->reset(ARBITRARY_TIME);
2719
2720 // Prepared displays and associated info.
2721 constexpr uint8_t hdmi1 = 0;
2722 constexpr uint8_t hdmi2 = 1;
2723 const std::string SECONDARY_UNIQUE_ID = "local:1";
2724
2725 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2726 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2727
2728 // No associated display viewport found, should disable the device.
2729 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2730 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2731 ASSERT_FALSE(device2->isEnabled());
2732
2733 // Prepare second display.
2734 constexpr int32_t newDisplayId = 2;
2735 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2736 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2737 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2738 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2739 // Default device will reconfigure above, need additional reconfiguration for another device.
2740 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2741 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2742
2743 // Device should be enabled after the associated display is found.
2744 ASSERT_TRUE(mDevice->isEnabled());
2745 ASSERT_TRUE(device2->isEnabled());
2746
2747 // Test pad key events
2748 ASSERT_NO_FATAL_FAILURE(
2749 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2750 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2751 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2752 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2753 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2754 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2755 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2756
2757 ASSERT_NO_FATAL_FAILURE(
2758 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2759 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2760 AKEYCODE_DPAD_RIGHT, newDisplayId));
2761 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2762 AKEYCODE_DPAD_DOWN, newDisplayId));
2763 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2764 AKEYCODE_DPAD_LEFT, newDisplayId));
2765}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002767// --- KeyboardInputMapperTest_ExternalDevice ---
2768
2769class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2770protected:
2771 virtual void SetUp() override {
2772 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2773 }
2774};
2775
2776TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002777 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2778 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002779
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002780 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2781 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2782 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2783 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002785 KeyboardInputMapper& mapper =
2786 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2787 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002788
2789 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2790 NotifyKeyArgs args;
2791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2792 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2793
2794 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2796 ASSERT_EQ(uint32_t(0), args.policyFlags);
2797
2798 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2800 ASSERT_EQ(uint32_t(0), args.policyFlags);
2801
2802 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2804 ASSERT_EQ(uint32_t(0), args.policyFlags);
2805
2806 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2808 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2809
2810 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2812 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2813}
2814
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002815TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002816 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002817
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002818 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2819 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2820 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002821
Powei Fengd041c5d2019-05-03 17:11:33 -07002822 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002823 KeyboardInputMapper& mapper =
2824 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2825 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002826
2827 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2828 NotifyKeyArgs args;
2829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2830 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2831
2832 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2834 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2835
2836 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2838 ASSERT_EQ(uint32_t(0), args.policyFlags);
2839
2840 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2842 ASSERT_EQ(uint32_t(0), args.policyFlags);
2843
2844 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2846 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2847
2848 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2850 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2851}
2852
Michael Wrightd02c5b62014-02-10 15:10:22 -08002853// --- CursorInputMapperTest ---
2854
2855class CursorInputMapperTest : public InputMapperTest {
2856protected:
2857 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2858
2859 sp<FakePointerController> mFakePointerController;
2860
Prabir Pradhan28efc192019-11-05 01:10:04 +00002861 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862 InputMapperTest::SetUp();
2863
2864 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002865 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866 }
2867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002868 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
2869 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002870
2871 void prepareDisplay(int32_t orientation) {
2872 const std::string uniqueId = "local:0";
2873 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2874 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2875 orientation, uniqueId, NO_PORT, viewportType);
2876 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877};
2878
2879const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002881void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
2882 int32_t originalY, int32_t rotatedX,
2883 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002884 NotifyMotionArgs args;
2885
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002886 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2887 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2888 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2892 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2893 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2894 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2895}
2896
2897TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002898 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002899 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002900
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002901 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902}
2903
2904TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002906 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002908 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909}
2910
2911TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002913 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002914
2915 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002916 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917
2918 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002919 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2920 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2922 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2923
2924 // When the bounds are set, then there should be a valid motion range.
2925 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2926
2927 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002928 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929
2930 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2931 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2932 1, 800 - 1, 0.0f, 0.0f));
2933 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2934 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2935 2, 480 - 1, 0.0f, 0.0f));
2936 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2937 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2938 0.0f, 1.0f, 0.0f, 0.0f));
2939}
2940
2941TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002943 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944
2945 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002946 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947
2948 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2949 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2950 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2951 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2952 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2953 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2954 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2955 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2956 0.0f, 1.0f, 0.0f, 0.0f));
2957}
2958
2959TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002961 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962
2963 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2964
2965 NotifyMotionArgs args;
2966
2967 // Button press.
2968 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002969 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2970 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2972 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2973 ASSERT_EQ(DEVICE_ID, args.deviceId);
2974 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2975 ASSERT_EQ(uint32_t(0), args.policyFlags);
2976 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2977 ASSERT_EQ(0, args.flags);
2978 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2979 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2980 ASSERT_EQ(0, args.edgeFlags);
2981 ASSERT_EQ(uint32_t(1), args.pointerCount);
2982 ASSERT_EQ(0, args.pointerProperties[0].id);
2983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2985 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2986 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2987 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2988 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2989
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2991 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2992 ASSERT_EQ(DEVICE_ID, args.deviceId);
2993 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2994 ASSERT_EQ(uint32_t(0), args.policyFlags);
2995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2996 ASSERT_EQ(0, args.flags);
2997 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2998 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2999 ASSERT_EQ(0, args.edgeFlags);
3000 ASSERT_EQ(uint32_t(1), args.pointerCount);
3001 ASSERT_EQ(0, args.pointerProperties[0].id);
3002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3004 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3005 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3006 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3007 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3008
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003010 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3011 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3013 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3014 ASSERT_EQ(DEVICE_ID, args.deviceId);
3015 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3016 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003017 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3018 ASSERT_EQ(0, args.flags);
3019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3020 ASSERT_EQ(0, args.buttonState);
3021 ASSERT_EQ(0, args.edgeFlags);
3022 ASSERT_EQ(uint32_t(1), args.pointerCount);
3023 ASSERT_EQ(0, args.pointerProperties[0].id);
3024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3026 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3027 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3028 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3029 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3030
3031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3032 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3033 ASSERT_EQ(DEVICE_ID, args.deviceId);
3034 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3035 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3037 ASSERT_EQ(0, args.flags);
3038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3039 ASSERT_EQ(0, args.buttonState);
3040 ASSERT_EQ(0, args.edgeFlags);
3041 ASSERT_EQ(uint32_t(1), args.pointerCount);
3042 ASSERT_EQ(0, args.pointerProperties[0].id);
3043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3045 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3046 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3047 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3048 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3049}
3050
3051TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003053 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054
3055 NotifyMotionArgs args;
3056
3057 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003058 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3059 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3063 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3064
3065 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003066 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3067 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3071 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3072}
3073
3074TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
3078 NotifyMotionArgs args;
3079
3080 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003081 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3082 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3086 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3087
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3091 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3092
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003094 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3095 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3099 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3100
3101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3104 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3105}
3106
3107TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003109 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
3111 NotifyMotionArgs args;
3112
3113 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003114 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3115 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3116 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3117 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3119 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3121 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3122 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3123
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3127 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3128 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3129
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003131 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3132 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3133 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3137 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3138 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3139
3140 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003141 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3142 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003144 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3146 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3147
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3151 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3152}
3153
3154TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003156 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003158 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3167}
3168
3169TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170 addConfigurationProperty("cursor.mode", "navigation");
3171 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003172 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003174 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3183
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003184 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3193
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003194 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3203
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003204 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3213}
3214
3215TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218
3219 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3220 mFakePointerController->setPosition(100, 200);
3221 mFakePointerController->setButtonState(0);
3222
3223 NotifyMotionArgs motionArgs;
3224 NotifyKeyArgs keyArgs;
3225
3226 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003227 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3228 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3230 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3231 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3232 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3234 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3235
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3238 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3239 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3241 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3242
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003243 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3244 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 ASSERT_EQ(0, motionArgs.buttonState);
3248 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3250 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3251
3252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003253 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254 ASSERT_EQ(0, motionArgs.buttonState);
3255 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3257 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3258
3259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003261 ASSERT_EQ(0, motionArgs.buttonState);
3262 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3264 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3265
3266 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003267 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3268 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3269 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3271 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3272 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3273 motionArgs.buttonState);
3274 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3275 mFakePointerController->getButtonState());
3276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3277 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3278
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3281 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3282 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3283 mFakePointerController->getButtonState());
3284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3285 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3286
3287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3289 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3290 motionArgs.buttonState);
3291 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3292 mFakePointerController->getButtonState());
3293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3294 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3295
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003296 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3297 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3301 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3303 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3304
3305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003307 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3308 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3310 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3311
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003312 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3313 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3316 ASSERT_EQ(0, motionArgs.buttonState);
3317 ASSERT_EQ(0, mFakePointerController->getButtonState());
3318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3319 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 -08003320 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3321 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003322
3323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324 ASSERT_EQ(0, motionArgs.buttonState);
3325 ASSERT_EQ(0, mFakePointerController->getButtonState());
3326 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3328 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 -08003329
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3331 ASSERT_EQ(0, motionArgs.buttonState);
3332 ASSERT_EQ(0, mFakePointerController->getButtonState());
3333 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3335 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3336
3337 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003338 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3339 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3341 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3342 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003343
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3347 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3349 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3350
3351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3353 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3354 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3356 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3357
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003358 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3359 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362 ASSERT_EQ(0, motionArgs.buttonState);
3363 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3365 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3366
3367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003369 ASSERT_EQ(0, motionArgs.buttonState);
3370 ASSERT_EQ(0, mFakePointerController->getButtonState());
3371
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3373 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3375 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3376 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3377
3378 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003379 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3380 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3382 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3383 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003384
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3388 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3390 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3391
3392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3394 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3395 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3397 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3398
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003399 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3400 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 ASSERT_EQ(0, motionArgs.buttonState);
3404 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3406 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 -08003407
3408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3410 ASSERT_EQ(0, motionArgs.buttonState);
3411 ASSERT_EQ(0, mFakePointerController->getButtonState());
3412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3413 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3414
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3416 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3417 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3418
3419 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003420 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3421 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3423 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3424 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003425
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3429 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3431 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3432
3433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3434 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3435 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3436 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3438 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3439
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003440 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3441 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444 ASSERT_EQ(0, motionArgs.buttonState);
3445 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3447 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 -08003448
3449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3450 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3451 ASSERT_EQ(0, motionArgs.buttonState);
3452 ASSERT_EQ(0, mFakePointerController->getButtonState());
3453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3454 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3455
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3457 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3458 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3459
3460 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003461 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3462 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3464 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3465 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003466
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3470 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3472 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3473
3474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3476 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3477 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3479 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3480
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003481 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3482 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485 ASSERT_EQ(0, motionArgs.buttonState);
3486 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3488 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 -08003489
3490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3492 ASSERT_EQ(0, motionArgs.buttonState);
3493 ASSERT_EQ(0, mFakePointerController->getButtonState());
3494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3495 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3496
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3498 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3499 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3500}
3501
3502TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003504 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
3506 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3507 mFakePointerController->setPosition(100, 200);
3508 mFakePointerController->setButtonState(0);
3509
3510 NotifyMotionArgs args;
3511
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003512 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3513 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3514 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003516 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3519 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3520 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3521}
3522
3523TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003524 addConfigurationProperty("cursor.mode", "pointer");
3525 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003527
3528 NotifyDeviceResetArgs resetArgs;
3529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3530 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3531 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3532
3533 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3534 mFakePointerController->setPosition(100, 200);
3535 mFakePointerController->setButtonState(0);
3536
3537 NotifyMotionArgs args;
3538
3539 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003540 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3541 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3542 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3544 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3547 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3548 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3549
3550 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003551 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3552 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3554 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3555 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3557 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3559 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3560 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3562 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3563
3564 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003565 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3566 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3568 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3571 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3573 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3574 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3576 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3577
3578 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003579 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3580 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3581 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3583 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3586 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3587 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3588
3589 // Disable pointer capture and check that the device generation got bumped
3590 // and events are generated the usual way.
3591 const uint32_t generation = mFakeContext->getGeneration();
3592 mFakePolicy->setPointerCapture(false);
3593 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3594 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3595
3596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3597 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3598 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3599
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003600 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3601 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3602 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3604 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3607 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3608 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3609}
3610
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003611TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003612 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003613
Garfield Tan888a6a42020-01-09 11:39:16 -08003614 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003615 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003616 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3617 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3618 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3619 ViewportType::VIEWPORT_EXTERNAL);
3620 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3621 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3622
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003623 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3624 mFakePointerController->setPosition(100, 200);
3625 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003626
3627 NotifyMotionArgs args;
3628 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3629 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3630 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3632 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3635 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3636 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3637 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3638}
3639
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640// --- TouchInputMapperTest ---
3641
3642class TouchInputMapperTest : public InputMapperTest {
3643protected:
3644 static const int32_t RAW_X_MIN;
3645 static const int32_t RAW_X_MAX;
3646 static const int32_t RAW_Y_MIN;
3647 static const int32_t RAW_Y_MAX;
3648 static const int32_t RAW_TOUCH_MIN;
3649 static const int32_t RAW_TOUCH_MAX;
3650 static const int32_t RAW_TOOL_MIN;
3651 static const int32_t RAW_TOOL_MAX;
3652 static const int32_t RAW_PRESSURE_MIN;
3653 static const int32_t RAW_PRESSURE_MAX;
3654 static const int32_t RAW_ORIENTATION_MIN;
3655 static const int32_t RAW_ORIENTATION_MAX;
3656 static const int32_t RAW_DISTANCE_MIN;
3657 static const int32_t RAW_DISTANCE_MAX;
3658 static const int32_t RAW_TILT_MIN;
3659 static const int32_t RAW_TILT_MAX;
3660 static const int32_t RAW_ID_MIN;
3661 static const int32_t RAW_ID_MAX;
3662 static const int32_t RAW_SLOT_MIN;
3663 static const int32_t RAW_SLOT_MAX;
3664 static const float X_PRECISION;
3665 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003666 static const float X_PRECISION_VIRTUAL;
3667 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668
3669 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003670 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671
3672 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3673
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003674 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003675 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003676
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677 enum Axes {
3678 POSITION = 1 << 0,
3679 TOUCH = 1 << 1,
3680 TOOL = 1 << 2,
3681 PRESSURE = 1 << 3,
3682 ORIENTATION = 1 << 4,
3683 MINOR = 1 << 5,
3684 ID = 1 << 6,
3685 DISTANCE = 1 << 7,
3686 TILT = 1 << 8,
3687 SLOT = 1 << 9,
3688 TOOL_TYPE = 1 << 10,
3689 };
3690
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003691 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3692 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003693 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003695 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696 int32_t toRawX(float displayX);
3697 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003698 float toCookedX(float rawX, float rawY);
3699 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003701 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003703 float toDisplayY(int32_t rawY, int32_t displayHeight);
3704
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705};
3706
3707const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3708const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3709const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3710const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3711const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3712const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3713const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3714const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003715const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3716const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3718const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3719const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3720const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3721const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3722const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3723const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3724const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3725const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3726const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3727const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3728const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003729const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3730 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3731const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3732 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003733const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3734 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
3736const float TouchInputMapperTest::GEOMETRIC_SCALE =
3737 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3738 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3739
3740const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3741 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3742 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3743};
3744
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003745void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003746 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003747 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3748}
3749
3750void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3751 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3752 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753}
3754
Santos Cordonfa5cf462017-04-05 10:37:00 -07003755void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003756 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3757 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003758 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003759}
3760
Michael Wrightd02c5b62014-02-10 15:10:22 -08003761void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003762 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3763 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3764 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3765 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766}
3767
Jason Gerecke489fda82012-09-07 17:19:40 -07003768void TouchInputMapperTest::prepareLocationCalibration() {
3769 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3770}
3771
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772int32_t TouchInputMapperTest::toRawX(float displayX) {
3773 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3774}
3775
3776int32_t TouchInputMapperTest::toRawY(float displayY) {
3777 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3778}
3779
Jason Gerecke489fda82012-09-07 17:19:40 -07003780float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3781 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3782 return rawX;
3783}
3784
3785float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3786 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3787 return rawY;
3788}
3789
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003791 return toDisplayX(rawX, DISPLAY_WIDTH);
3792}
3793
3794float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3795 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796}
3797
3798float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003799 return toDisplayY(rawY, DISPLAY_HEIGHT);
3800}
3801
3802float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3803 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804}
3805
3806
3807// --- SingleTouchInputMapperTest ---
3808
3809class SingleTouchInputMapperTest : public TouchInputMapperTest {
3810protected:
3811 void prepareButtons();
3812 void prepareAxes(int axes);
3813
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003814 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3815 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3816 void processUp(SingleTouchInputMapper& mappery);
3817 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3818 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3819 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3820 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3821 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3822 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823};
3824
3825void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003826 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827}
3828
3829void SingleTouchInputMapperTest::prepareAxes(int axes) {
3830 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003831 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3832 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833 }
3834 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003835 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
3836 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837 }
3838 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003839 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
3840 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841 }
3842 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003843 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
3844 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845 }
3846 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3848 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849 }
3850}
3851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003852void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003853 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3854 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3855 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856}
3857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003859 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3860 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861}
3862
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003863void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003864 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865}
3866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003867void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003868 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869}
3870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
3872 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003873 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874}
3875
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003877 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878}
3879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
3881 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003882 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3883 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884}
3885
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003886void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
3887 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003888 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889}
3890
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003891void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003892 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893}
3894
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896 prepareButtons();
3897 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003898 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003900 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901}
3902
3903TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003904 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
3905 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 prepareButtons();
3907 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003908 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003910 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911}
3912
3913TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914 prepareButtons();
3915 prepareAxes(POSITION);
3916 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003917 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003919 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920}
3921
3922TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923 prepareButtons();
3924 prepareAxes(POSITION);
3925 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003926 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003928 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929}
3930
3931TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932 addConfigurationProperty("touch.deviceType", "touchScreen");
3933 prepareDisplay(DISPLAY_ORIENTATION_0);
3934 prepareButtons();
3935 prepareAxes(POSITION);
3936 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003937 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938
3939 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003940 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941
3942 // Virtual key is down.
3943 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3944 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3945 processDown(mapper, x, y);
3946 processSync(mapper);
3947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003949 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950
3951 // Virtual key is up.
3952 processUp(mapper);
3953 processSync(mapper);
3954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003956 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003957}
3958
3959TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960 addConfigurationProperty("touch.deviceType", "touchScreen");
3961 prepareDisplay(DISPLAY_ORIENTATION_0);
3962 prepareButtons();
3963 prepareAxes(POSITION);
3964 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003965 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966
3967 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003968 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969
3970 // Virtual key is down.
3971 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3972 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3973 processDown(mapper, x, y);
3974 processSync(mapper);
3975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3976
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003977 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978
3979 // Virtual key is up.
3980 processUp(mapper);
3981 processSync(mapper);
3982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003984 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985}
3986
3987TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988 addConfigurationProperty("touch.deviceType", "touchScreen");
3989 prepareDisplay(DISPLAY_ORIENTATION_0);
3990 prepareButtons();
3991 prepareAxes(POSITION);
3992 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003993 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003994
3995 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3996 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998 ASSERT_TRUE(flags[0]);
3999 ASSERT_FALSE(flags[1]);
4000}
4001
4002TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003 addConfigurationProperty("touch.deviceType", "touchScreen");
4004 prepareDisplay(DISPLAY_ORIENTATION_0);
4005 prepareButtons();
4006 prepareAxes(POSITION);
4007 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004008 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009
4010 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4011
4012 NotifyKeyArgs args;
4013
4014 // Press virtual key.
4015 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4016 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4017 processDown(mapper, x, y);
4018 processSync(mapper);
4019
4020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4021 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4022 ASSERT_EQ(DEVICE_ID, args.deviceId);
4023 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4024 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4025 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4026 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4027 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4028 ASSERT_EQ(KEY_HOME, args.scanCode);
4029 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4030 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4031
4032 // Release virtual key.
4033 processUp(mapper);
4034 processSync(mapper);
4035
4036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4037 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4038 ASSERT_EQ(DEVICE_ID, args.deviceId);
4039 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4040 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4041 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4042 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4043 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4044 ASSERT_EQ(KEY_HOME, args.scanCode);
4045 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4046 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4047
4048 // Should not have sent any motions.
4049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4050}
4051
4052TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053 addConfigurationProperty("touch.deviceType", "touchScreen");
4054 prepareDisplay(DISPLAY_ORIENTATION_0);
4055 prepareButtons();
4056 prepareAxes(POSITION);
4057 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004058 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059
4060 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4061
4062 NotifyKeyArgs keyArgs;
4063
4064 // Press virtual key.
4065 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4066 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4067 processDown(mapper, x, y);
4068 processSync(mapper);
4069
4070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4071 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4072 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4073 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4074 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4075 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4076 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4077 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4078 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4079 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4080 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4081
4082 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4083 // into the display area.
4084 y -= 100;
4085 processMove(mapper, x, y);
4086 processSync(mapper);
4087
4088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4089 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4090 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4091 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4092 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4093 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4094 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4095 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4096 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4097 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4098 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4099 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4100
4101 NotifyMotionArgs motionArgs;
4102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4103 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4104 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4105 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4106 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4107 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4108 ASSERT_EQ(0, motionArgs.flags);
4109 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4110 ASSERT_EQ(0, motionArgs.buttonState);
4111 ASSERT_EQ(0, motionArgs.edgeFlags);
4112 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4113 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4116 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4117 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4118 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4119 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4120
4121 // Keep moving out of bounds. Should generate a pointer move.
4122 y -= 50;
4123 processMove(mapper, x, y);
4124 processSync(mapper);
4125
4126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4128 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4129 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4130 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4132 ASSERT_EQ(0, motionArgs.flags);
4133 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4134 ASSERT_EQ(0, motionArgs.buttonState);
4135 ASSERT_EQ(0, motionArgs.edgeFlags);
4136 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4137 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4140 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4141 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4142 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4144
4145 // Release out of bounds. Should generate a pointer up.
4146 processUp(mapper);
4147 processSync(mapper);
4148
4149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4150 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4151 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4152 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4153 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4154 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4155 ASSERT_EQ(0, motionArgs.flags);
4156 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4157 ASSERT_EQ(0, motionArgs.buttonState);
4158 ASSERT_EQ(0, motionArgs.edgeFlags);
4159 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4160 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4163 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4164 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4165 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4166 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4167
4168 // Should not have sent any more keys or motions.
4169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4171}
4172
4173TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 addConfigurationProperty("touch.deviceType", "touchScreen");
4175 prepareDisplay(DISPLAY_ORIENTATION_0);
4176 prepareButtons();
4177 prepareAxes(POSITION);
4178 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004179 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180
4181 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4182
4183 NotifyMotionArgs motionArgs;
4184
4185 // Initially go down out of bounds.
4186 int32_t x = -10;
4187 int32_t y = -10;
4188 processDown(mapper, x, y);
4189 processSync(mapper);
4190
4191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4192
4193 // Move into the display area. Should generate a pointer down.
4194 x = 50;
4195 y = 75;
4196 processMove(mapper, x, y);
4197 processSync(mapper);
4198
4199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4200 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4201 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4202 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4203 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4204 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4205 ASSERT_EQ(0, motionArgs.flags);
4206 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4207 ASSERT_EQ(0, motionArgs.buttonState);
4208 ASSERT_EQ(0, motionArgs.edgeFlags);
4209 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4210 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4213 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4214 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4215 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4217
4218 // Release. Should generate a pointer up.
4219 processUp(mapper);
4220 processSync(mapper);
4221
4222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4227 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4228 ASSERT_EQ(0, motionArgs.flags);
4229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4230 ASSERT_EQ(0, motionArgs.buttonState);
4231 ASSERT_EQ(0, motionArgs.edgeFlags);
4232 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4236 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4237 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4238 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4240
4241 // Should not have sent any more keys or motions.
4242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4244}
4245
Santos Cordonfa5cf462017-04-05 10:37:00 -07004246TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004247 addConfigurationProperty("touch.deviceType", "touchScreen");
4248 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4249
4250 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4251 prepareButtons();
4252 prepareAxes(POSITION);
4253 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004254 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004255
4256 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4257
4258 NotifyMotionArgs motionArgs;
4259
4260 // Down.
4261 int32_t x = 100;
4262 int32_t y = 125;
4263 processDown(mapper, x, y);
4264 processSync(mapper);
4265
4266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4268 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4269 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4270 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4271 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4272 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4273 ASSERT_EQ(0, motionArgs.flags);
4274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4275 ASSERT_EQ(0, motionArgs.buttonState);
4276 ASSERT_EQ(0, motionArgs.edgeFlags);
4277 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4278 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4281 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4282 1, 0, 0, 0, 0, 0, 0, 0));
4283 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4284 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4286
4287 // Move.
4288 x += 50;
4289 y += 75;
4290 processMove(mapper, x, y);
4291 processSync(mapper);
4292
4293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4295 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4296 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4297 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4298 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4300 ASSERT_EQ(0, motionArgs.flags);
4301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4302 ASSERT_EQ(0, motionArgs.buttonState);
4303 ASSERT_EQ(0, motionArgs.edgeFlags);
4304 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4305 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4308 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4309 1, 0, 0, 0, 0, 0, 0, 0));
4310 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4311 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4313
4314 // Up.
4315 processUp(mapper);
4316 processSync(mapper);
4317
4318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4320 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4321 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4322 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4323 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4324 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4325 ASSERT_EQ(0, motionArgs.flags);
4326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4327 ASSERT_EQ(0, motionArgs.buttonState);
4328 ASSERT_EQ(0, motionArgs.edgeFlags);
4329 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4333 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4334 1, 0, 0, 0, 0, 0, 0, 0));
4335 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4336 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4338
4339 // Should not have sent any more keys or motions.
4340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4342}
4343
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 addConfigurationProperty("touch.deviceType", "touchScreen");
4346 prepareDisplay(DISPLAY_ORIENTATION_0);
4347 prepareButtons();
4348 prepareAxes(POSITION);
4349 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004350 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351
4352 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4353
4354 NotifyMotionArgs motionArgs;
4355
4356 // Down.
4357 int32_t x = 100;
4358 int32_t y = 125;
4359 processDown(mapper, x, y);
4360 processSync(mapper);
4361
4362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4364 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4365 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4366 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4367 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4368 ASSERT_EQ(0, motionArgs.flags);
4369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4370 ASSERT_EQ(0, motionArgs.buttonState);
4371 ASSERT_EQ(0, motionArgs.edgeFlags);
4372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4376 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4377 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4378 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4380
4381 // Move.
4382 x += 50;
4383 y += 75;
4384 processMove(mapper, x, y);
4385 processSync(mapper);
4386
4387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4388 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4389 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4390 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4391 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4393 ASSERT_EQ(0, motionArgs.flags);
4394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4395 ASSERT_EQ(0, motionArgs.buttonState);
4396 ASSERT_EQ(0, motionArgs.edgeFlags);
4397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4398 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4401 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4402 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4403 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4405
4406 // Up.
4407 processUp(mapper);
4408 processSync(mapper);
4409
4410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4411 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4412 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4413 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4414 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4415 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4416 ASSERT_EQ(0, motionArgs.flags);
4417 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4418 ASSERT_EQ(0, motionArgs.buttonState);
4419 ASSERT_EQ(0, motionArgs.edgeFlags);
4420 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4421 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4424 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4425 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4426 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4428
4429 // Should not have sent any more keys or motions.
4430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4432}
4433
4434TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 addConfigurationProperty("touch.deviceType", "touchScreen");
4436 prepareButtons();
4437 prepareAxes(POSITION);
4438 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004439 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440
4441 NotifyMotionArgs args;
4442
4443 // Rotation 90.
4444 prepareDisplay(DISPLAY_ORIENTATION_90);
4445 processDown(mapper, toRawX(50), toRawY(75));
4446 processSync(mapper);
4447
4448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4450 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4451
4452 processUp(mapper);
4453 processSync(mapper);
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4455}
4456
4457TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 addConfigurationProperty("touch.deviceType", "touchScreen");
4459 prepareButtons();
4460 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004461 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462
4463 NotifyMotionArgs args;
4464
4465 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004466 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 prepareDisplay(DISPLAY_ORIENTATION_0);
4468 processDown(mapper, toRawX(50), toRawY(75));
4469 processSync(mapper);
4470
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4472 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4473 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4474
4475 processUp(mapper);
4476 processSync(mapper);
4477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4478
4479 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004480 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 prepareDisplay(DISPLAY_ORIENTATION_90);
4482 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4483 processSync(mapper);
4484
4485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4486 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4487 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4488
4489 processUp(mapper);
4490 processSync(mapper);
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4492
4493 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004494 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 prepareDisplay(DISPLAY_ORIENTATION_180);
4496 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4497 processSync(mapper);
4498
4499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4500 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4501 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4502
4503 processUp(mapper);
4504 processSync(mapper);
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4506
4507 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004508 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 prepareDisplay(DISPLAY_ORIENTATION_270);
4510 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4511 processSync(mapper);
4512
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4514 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4515 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4516
4517 processUp(mapper);
4518 processSync(mapper);
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4520}
4521
4522TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 addConfigurationProperty("touch.deviceType", "touchScreen");
4524 prepareDisplay(DISPLAY_ORIENTATION_0);
4525 prepareButtons();
4526 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004527 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528
4529 // These calculations are based on the input device calibration documentation.
4530 int32_t rawX = 100;
4531 int32_t rawY = 200;
4532 int32_t rawPressure = 10;
4533 int32_t rawToolMajor = 12;
4534 int32_t rawDistance = 2;
4535 int32_t rawTiltX = 30;
4536 int32_t rawTiltY = 110;
4537
4538 float x = toDisplayX(rawX);
4539 float y = toDisplayY(rawY);
4540 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4541 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4542 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4543 float distance = float(rawDistance);
4544
4545 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4546 float tiltScale = M_PI / 180;
4547 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4548 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4549 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4550 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4551
4552 processDown(mapper, rawX, rawY);
4553 processPressure(mapper, rawPressure);
4554 processToolMajor(mapper, rawToolMajor);
4555 processDistance(mapper, rawDistance);
4556 processTilt(mapper, rawTiltX, rawTiltY);
4557 processSync(mapper);
4558
4559 NotifyMotionArgs args;
4560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4562 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4563 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4564}
4565
Jason Gerecke489fda82012-09-07 17:19:40 -07004566TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004567 addConfigurationProperty("touch.deviceType", "touchScreen");
4568 prepareDisplay(DISPLAY_ORIENTATION_0);
4569 prepareLocationCalibration();
4570 prepareButtons();
4571 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004572 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004573
4574 int32_t rawX = 100;
4575 int32_t rawY = 200;
4576
4577 float x = toDisplayX(toCookedX(rawX, rawY));
4578 float y = toDisplayY(toCookedY(rawX, rawY));
4579
4580 processDown(mapper, rawX, rawY);
4581 processSync(mapper);
4582
4583 NotifyMotionArgs args;
4584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4586 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4587}
4588
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 addConfigurationProperty("touch.deviceType", "touchScreen");
4591 prepareDisplay(DISPLAY_ORIENTATION_0);
4592 prepareButtons();
4593 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004594 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595
4596 NotifyMotionArgs motionArgs;
4597 NotifyKeyArgs keyArgs;
4598
4599 processDown(mapper, 100, 200);
4600 processSync(mapper);
4601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4602 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4603 ASSERT_EQ(0, motionArgs.buttonState);
4604
4605 // press BTN_LEFT, release BTN_LEFT
4606 processKey(mapper, BTN_LEFT, 1);
4607 processSync(mapper);
4608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4610 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4611
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4613 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4614 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4615
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 processKey(mapper, BTN_LEFT, 0);
4617 processSync(mapper);
4618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004624 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625
4626 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4627 processKey(mapper, BTN_RIGHT, 1);
4628 processKey(mapper, BTN_MIDDLE, 1);
4629 processSync(mapper);
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4632 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4633 motionArgs.buttonState);
4634
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4637 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4638
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4641 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4642 motionArgs.buttonState);
4643
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 processKey(mapper, BTN_RIGHT, 0);
4645 processSync(mapper);
4646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004649
4650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653
4654 processKey(mapper, BTN_MIDDLE, 0);
4655 processSync(mapper);
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004659
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004662 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663
4664 // press BTN_BACK, release BTN_BACK
4665 processKey(mapper, BTN_BACK, 1);
4666 processSync(mapper);
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4668 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4669 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004670
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004673 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4674
4675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4676 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4677 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678
4679 processKey(mapper, BTN_BACK, 0);
4680 processSync(mapper);
4681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004684
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004687 ASSERT_EQ(0, motionArgs.buttonState);
4688
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4690 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4691 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4692
4693 // press BTN_SIDE, release BTN_SIDE
4694 processKey(mapper, BTN_SIDE, 1);
4695 processSync(mapper);
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4697 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4698 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004699
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4703
4704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4705 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4706 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707
4708 processKey(mapper, BTN_SIDE, 0);
4709 processSync(mapper);
4710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004711 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004713
4714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004716 ASSERT_EQ(0, motionArgs.buttonState);
4717
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4719 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4720 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4721
4722 // press BTN_FORWARD, release BTN_FORWARD
4723 processKey(mapper, BTN_FORWARD, 1);
4724 processSync(mapper);
4725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4726 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4727 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004728
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004731 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4732
4733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4734 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4735 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736
4737 processKey(mapper, BTN_FORWARD, 0);
4738 processSync(mapper);
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004742
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004745 ASSERT_EQ(0, motionArgs.buttonState);
4746
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4748 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4749 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4750
4751 // press BTN_EXTRA, release BTN_EXTRA
4752 processKey(mapper, BTN_EXTRA, 1);
4753 processSync(mapper);
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4755 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4756 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004757
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004760 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4761
4762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4763 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4764 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765
4766 processKey(mapper, BTN_EXTRA, 0);
4767 processSync(mapper);
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004769 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004771
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004774 ASSERT_EQ(0, motionArgs.buttonState);
4775
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4777 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4778 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4779
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4781
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 // press BTN_STYLUS, release BTN_STYLUS
4783 processKey(mapper, BTN_STYLUS, 1);
4784 processSync(mapper);
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004787 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4788
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4791 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792
4793 processKey(mapper, BTN_STYLUS, 0);
4794 processSync(mapper);
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004796 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004798
4799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802
4803 // press BTN_STYLUS2, release BTN_STYLUS2
4804 processKey(mapper, BTN_STYLUS2, 1);
4805 processSync(mapper);
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004808 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4809
4810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4812 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813
4814 processKey(mapper, BTN_STYLUS2, 0);
4815 processSync(mapper);
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004817 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004819
4820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004822 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
4824 // release touch
4825 processUp(mapper);
4826 processSync(mapper);
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4828 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4829 ASSERT_EQ(0, motionArgs.buttonState);
4830}
4831
4832TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 addConfigurationProperty("touch.deviceType", "touchScreen");
4834 prepareDisplay(DISPLAY_ORIENTATION_0);
4835 prepareButtons();
4836 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004837 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838
4839 NotifyMotionArgs motionArgs;
4840
4841 // default tool type is finger
4842 processDown(mapper, 100, 200);
4843 processSync(mapper);
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4845 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4847
4848 // eraser
4849 processKey(mapper, BTN_TOOL_RUBBER, 1);
4850 processSync(mapper);
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4854
4855 // stylus
4856 processKey(mapper, BTN_TOOL_RUBBER, 0);
4857 processKey(mapper, BTN_TOOL_PEN, 1);
4858 processSync(mapper);
4859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4861 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4862
4863 // brush
4864 processKey(mapper, BTN_TOOL_PEN, 0);
4865 processKey(mapper, BTN_TOOL_BRUSH, 1);
4866 processSync(mapper);
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4870
4871 // pencil
4872 processKey(mapper, BTN_TOOL_BRUSH, 0);
4873 processKey(mapper, BTN_TOOL_PENCIL, 1);
4874 processSync(mapper);
4875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4878
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004879 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 processKey(mapper, BTN_TOOL_PENCIL, 0);
4881 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4882 processSync(mapper);
4883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4885 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4886
4887 // mouse
4888 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4889 processKey(mapper, BTN_TOOL_MOUSE, 1);
4890 processSync(mapper);
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4894
4895 // lens
4896 processKey(mapper, BTN_TOOL_MOUSE, 0);
4897 processKey(mapper, BTN_TOOL_LENS, 1);
4898 processSync(mapper);
4899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4902
4903 // double-tap
4904 processKey(mapper, BTN_TOOL_LENS, 0);
4905 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4906 processSync(mapper);
4907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4910
4911 // triple-tap
4912 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4913 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4914 processSync(mapper);
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4918
4919 // quad-tap
4920 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4921 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4922 processSync(mapper);
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4926
4927 // finger
4928 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4929 processKey(mapper, BTN_TOOL_FINGER, 1);
4930 processSync(mapper);
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4934
4935 // stylus trumps finger
4936 processKey(mapper, BTN_TOOL_PEN, 1);
4937 processSync(mapper);
4938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4940 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4941
4942 // eraser trumps stylus
4943 processKey(mapper, BTN_TOOL_RUBBER, 1);
4944 processSync(mapper);
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4948
4949 // mouse trumps eraser
4950 processKey(mapper, BTN_TOOL_MOUSE, 1);
4951 processSync(mapper);
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4955
4956 // back to default tool type
4957 processKey(mapper, BTN_TOOL_MOUSE, 0);
4958 processKey(mapper, BTN_TOOL_RUBBER, 0);
4959 processKey(mapper, BTN_TOOL_PEN, 0);
4960 processKey(mapper, BTN_TOOL_FINGER, 0);
4961 processSync(mapper);
4962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4965}
4966
4967TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968 addConfigurationProperty("touch.deviceType", "touchScreen");
4969 prepareDisplay(DISPLAY_ORIENTATION_0);
4970 prepareButtons();
4971 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004972 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004973 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974
4975 NotifyMotionArgs motionArgs;
4976
4977 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4978 processKey(mapper, BTN_TOOL_FINGER, 1);
4979 processMove(mapper, 100, 200);
4980 processSync(mapper);
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4982 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4984 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4985
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4987 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4989 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4990
4991 // move a little
4992 processMove(mapper, 150, 250);
4993 processSync(mapper);
4994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4995 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4997 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4998
4999 // down when BTN_TOUCH is pressed, pressure defaults to 1
5000 processKey(mapper, BTN_TOUCH, 1);
5001 processSync(mapper);
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5003 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5005 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5006
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5008 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5010 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5011
5012 // up when BTN_TOUCH is released, hover restored
5013 processKey(mapper, BTN_TOUCH, 0);
5014 processSync(mapper);
5015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5016 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5018 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5019
5020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5021 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5023 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5024
5025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5028 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5029
5030 // exit hover when pointer goes away
5031 processKey(mapper, BTN_TOOL_FINGER, 0);
5032 processSync(mapper);
5033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5034 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5036 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5037}
5038
5039TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 addConfigurationProperty("touch.deviceType", "touchScreen");
5041 prepareDisplay(DISPLAY_ORIENTATION_0);
5042 prepareButtons();
5043 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005044 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045
5046 NotifyMotionArgs motionArgs;
5047
5048 // initially hovering because pressure is 0
5049 processDown(mapper, 100, 200);
5050 processPressure(mapper, 0);
5051 processSync(mapper);
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5053 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5055 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5056
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5059 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5060 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5061
5062 // move a little
5063 processMove(mapper, 150, 250);
5064 processSync(mapper);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5068 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5069
5070 // down when pressure is non-zero
5071 processPressure(mapper, RAW_PRESSURE_MAX);
5072 processSync(mapper);
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5074 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5076 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5077
5078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5079 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5081 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5082
5083 // up when pressure becomes 0, hover restored
5084 processPressure(mapper, 0);
5085 processSync(mapper);
5086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5087 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5089 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5090
5091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5092 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5094 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5095
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5097 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5099 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5100
5101 // exit hover when pointer goes away
5102 processUp(mapper);
5103 processSync(mapper);
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5105 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5107 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5108}
5109
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110// --- MultiTouchInputMapperTest ---
5111
5112class MultiTouchInputMapperTest : public TouchInputMapperTest {
5113protected:
5114 void prepareAxes(int axes);
5115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005116 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5117 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5118 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5119 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5120 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5121 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5122 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5123 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5124 void processId(MultiTouchInputMapper& mapper, int32_t id);
5125 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5126 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5127 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5128 void processMTSync(MultiTouchInputMapper& mapper);
5129 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130};
5131
5132void MultiTouchInputMapperTest::prepareAxes(int axes) {
5133 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005134 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136 }
5137 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005138 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5139 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005141 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5142 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143 }
5144 }
5145 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5147 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005149 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5150 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151 }
5152 }
5153 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005154 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5155 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156 }
5157 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005158 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5159 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005160 }
5161 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5163 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164 }
5165 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005166 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5167 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168 }
5169 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005170 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5171 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172 }
5173 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005174 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175 }
5176}
5177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005178void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5179 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005180 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5181 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182}
5183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005184void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5185 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005186 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187}
5188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005189void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5190 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005191 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192}
5193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005194void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005195 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196}
5197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005198void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005199 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200}
5201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005202void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5203 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005204 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205}
5206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005207void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005208 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209}
5210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005211void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005212 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213}
5214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005215void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005216 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217}
5218
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005219void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005220 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221}
5222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005223void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005224 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005225}
5226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005227void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5228 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005229 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230}
5231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005232void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005233 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234}
5235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005236void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005237 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238}
5239
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005241 addConfigurationProperty("touch.deviceType", "touchScreen");
5242 prepareDisplay(DISPLAY_ORIENTATION_0);
5243 prepareAxes(POSITION);
5244 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005245 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246
5247 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5248
5249 NotifyMotionArgs motionArgs;
5250
5251 // Two fingers down at once.
5252 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5253 processPosition(mapper, x1, y1);
5254 processMTSync(mapper);
5255 processPosition(mapper, x2, y2);
5256 processMTSync(mapper);
5257 processSync(mapper);
5258
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5260 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5261 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5262 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5263 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5264 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5265 ASSERT_EQ(0, motionArgs.flags);
5266 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5267 ASSERT_EQ(0, motionArgs.buttonState);
5268 ASSERT_EQ(0, motionArgs.edgeFlags);
5269 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5270 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5273 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5274 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5275 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5277
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5280 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5281 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5282 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5283 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5284 motionArgs.action);
5285 ASSERT_EQ(0, motionArgs.flags);
5286 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5287 ASSERT_EQ(0, motionArgs.buttonState);
5288 ASSERT_EQ(0, motionArgs.edgeFlags);
5289 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5290 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5292 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5295 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5297 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5298 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5299 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5301
5302 // Move.
5303 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5304 processPosition(mapper, x1, y1);
5305 processMTSync(mapper);
5306 processPosition(mapper, x2, y2);
5307 processMTSync(mapper);
5308 processSync(mapper);
5309
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5312 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5313 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5314 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5316 ASSERT_EQ(0, motionArgs.flags);
5317 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5318 ASSERT_EQ(0, motionArgs.buttonState);
5319 ASSERT_EQ(0, motionArgs.edgeFlags);
5320 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5321 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5323 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5326 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5328 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5329 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5330 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5331 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5332
5333 // First finger up.
5334 x2 += 15; y2 -= 20;
5335 processPosition(mapper, x2, y2);
5336 processMTSync(mapper);
5337 processSync(mapper);
5338
5339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5341 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5342 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5343 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5344 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5345 motionArgs.action);
5346 ASSERT_EQ(0, motionArgs.flags);
5347 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5348 ASSERT_EQ(0, motionArgs.buttonState);
5349 ASSERT_EQ(0, motionArgs.edgeFlags);
5350 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5351 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5353 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5358 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5359 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5360 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5362
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5365 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5366 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5367 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5369 ASSERT_EQ(0, motionArgs.flags);
5370 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5371 ASSERT_EQ(0, motionArgs.buttonState);
5372 ASSERT_EQ(0, motionArgs.edgeFlags);
5373 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5374 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5377 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5378 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5379 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5381
5382 // Move.
5383 x2 += 20; y2 -= 25;
5384 processPosition(mapper, x2, y2);
5385 processMTSync(mapper);
5386 processSync(mapper);
5387
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5390 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5391 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5392 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5394 ASSERT_EQ(0, motionArgs.flags);
5395 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5396 ASSERT_EQ(0, motionArgs.buttonState);
5397 ASSERT_EQ(0, motionArgs.edgeFlags);
5398 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5399 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5402 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5403 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5404 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5406
5407 // New finger down.
5408 int32_t x3 = 700, y3 = 300;
5409 processPosition(mapper, x2, y2);
5410 processMTSync(mapper);
5411 processPosition(mapper, x3, y3);
5412 processMTSync(mapper);
5413 processSync(mapper);
5414
5415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5416 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5417 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5418 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5419 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5420 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5421 motionArgs.action);
5422 ASSERT_EQ(0, motionArgs.flags);
5423 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5424 ASSERT_EQ(0, motionArgs.buttonState);
5425 ASSERT_EQ(0, motionArgs.edgeFlags);
5426 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5427 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5429 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5432 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5434 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5435 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5436 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5437 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5438
5439 // Second finger up.
5440 x3 += 30; y3 -= 20;
5441 processPosition(mapper, x3, y3);
5442 processMTSync(mapper);
5443 processSync(mapper);
5444
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5447 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5448 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5449 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5450 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5451 motionArgs.action);
5452 ASSERT_EQ(0, motionArgs.flags);
5453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5454 ASSERT_EQ(0, motionArgs.buttonState);
5455 ASSERT_EQ(0, motionArgs.edgeFlags);
5456 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5457 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5459 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5462 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5464 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5465 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5466 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5468
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5470 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5471 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5472 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5473 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5475 ASSERT_EQ(0, motionArgs.flags);
5476 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5477 ASSERT_EQ(0, motionArgs.buttonState);
5478 ASSERT_EQ(0, motionArgs.edgeFlags);
5479 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5480 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5481 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5483 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5484 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5485 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5486 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5487
5488 // Last finger up.
5489 processMTSync(mapper);
5490 processSync(mapper);
5491
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5493 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5494 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5495 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5496 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5497 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5498 ASSERT_EQ(0, motionArgs.flags);
5499 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5500 ASSERT_EQ(0, motionArgs.buttonState);
5501 ASSERT_EQ(0, motionArgs.edgeFlags);
5502 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5503 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5506 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5507 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5508 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5509 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5510
5511 // Should not have sent any more keys or motions.
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5514}
5515
5516TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517 addConfigurationProperty("touch.deviceType", "touchScreen");
5518 prepareDisplay(DISPLAY_ORIENTATION_0);
5519 prepareAxes(POSITION | ID);
5520 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005521 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522
5523 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5524
5525 NotifyMotionArgs motionArgs;
5526
5527 // Two fingers down at once.
5528 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5529 processPosition(mapper, x1, y1);
5530 processId(mapper, 1);
5531 processMTSync(mapper);
5532 processPosition(mapper, x2, y2);
5533 processId(mapper, 2);
5534 processMTSync(mapper);
5535 processSync(mapper);
5536
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5539 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5540 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5543 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5547 motionArgs.action);
5548 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5551 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5554 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5556 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5557
5558 // Move.
5559 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5560 processPosition(mapper, x1, y1);
5561 processId(mapper, 1);
5562 processMTSync(mapper);
5563 processPosition(mapper, x2, y2);
5564 processId(mapper, 2);
5565 processMTSync(mapper);
5566 processSync(mapper);
5567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5570 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5571 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5573 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5576 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5578 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5579
5580 // First finger up.
5581 x2 += 15; y2 -= 20;
5582 processPosition(mapper, x2, y2);
5583 processId(mapper, 2);
5584 processMTSync(mapper);
5585 processSync(mapper);
5586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5588 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5589 motionArgs.action);
5590 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5591 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5593 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5596 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5598 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5599
5600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5602 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5603 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5606 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5607
5608 // Move.
5609 x2 += 20; y2 -= 25;
5610 processPosition(mapper, x2, y2);
5611 processId(mapper, 2);
5612 processMTSync(mapper);
5613 processSync(mapper);
5614
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5617 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5618 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5621 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5622
5623 // New finger down.
5624 int32_t x3 = 700, y3 = 300;
5625 processPosition(mapper, x2, y2);
5626 processId(mapper, 2);
5627 processMTSync(mapper);
5628 processPosition(mapper, x3, y3);
5629 processId(mapper, 3);
5630 processMTSync(mapper);
5631 processSync(mapper);
5632
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5634 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5635 motionArgs.action);
5636 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5637 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5639 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5642 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5644 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5645
5646 // Second finger up.
5647 x3 += 30; y3 -= 20;
5648 processPosition(mapper, x3, y3);
5649 processId(mapper, 3);
5650 processMTSync(mapper);
5651 processSync(mapper);
5652
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5655 motionArgs.action);
5656 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5659 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5662 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5664 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5668 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5669 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5672 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5673
5674 // Last finger up.
5675 processMTSync(mapper);
5676 processSync(mapper);
5677
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5679 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5680 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5684 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5685
5686 // Should not have sent any more keys or motions.
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5689}
5690
5691TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692 addConfigurationProperty("touch.deviceType", "touchScreen");
5693 prepareDisplay(DISPLAY_ORIENTATION_0);
5694 prepareAxes(POSITION | ID | SLOT);
5695 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005696 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697
5698 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5699
5700 NotifyMotionArgs motionArgs;
5701
5702 // Two fingers down at once.
5703 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5704 processPosition(mapper, x1, y1);
5705 processId(mapper, 1);
5706 processSlot(mapper, 1);
5707 processPosition(mapper, x2, y2);
5708 processId(mapper, 2);
5709 processSync(mapper);
5710
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5713 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5714 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5717 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5718
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5720 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5721 motionArgs.action);
5722 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5723 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5725 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5728 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5730 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5731
5732 // Move.
5733 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5734 processSlot(mapper, 0);
5735 processPosition(mapper, x1, y1);
5736 processSlot(mapper, 1);
5737 processPosition(mapper, x2, y2);
5738 processSync(mapper);
5739
5740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5742 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5743 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5745 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5748 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5750 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5751
5752 // First finger up.
5753 x2 += 15; y2 -= 20;
5754 processSlot(mapper, 0);
5755 processId(mapper, -1);
5756 processSlot(mapper, 1);
5757 processPosition(mapper, x2, y2);
5758 processSync(mapper);
5759
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5761 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5762 motionArgs.action);
5763 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5764 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5766 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5769 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5771 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5772
5773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5775 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5776 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5779 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5780
5781 // Move.
5782 x2 += 20; y2 -= 25;
5783 processPosition(mapper, x2, y2);
5784 processSync(mapper);
5785
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5788 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5789 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5792 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5793
5794 // New finger down.
5795 int32_t x3 = 700, y3 = 300;
5796 processPosition(mapper, x2, y2);
5797 processSlot(mapper, 0);
5798 processId(mapper, 3);
5799 processPosition(mapper, x3, y3);
5800 processSync(mapper);
5801
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5803 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5804 motionArgs.action);
5805 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5806 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5808 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5811 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5813 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5814
5815 // Second finger up.
5816 x3 += 30; y3 -= 20;
5817 processSlot(mapper, 1);
5818 processId(mapper, -1);
5819 processSlot(mapper, 0);
5820 processPosition(mapper, x3, y3);
5821 processSync(mapper);
5822
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5825 motionArgs.action);
5826 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5827 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5829 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5832 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5834 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5838 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5842 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5843
5844 // Last finger up.
5845 processId(mapper, -1);
5846 processSync(mapper);
5847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5850 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5854 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5855
5856 // Should not have sent any more keys or motions.
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5859}
5860
5861TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 addConfigurationProperty("touch.deviceType", "touchScreen");
5863 prepareDisplay(DISPLAY_ORIENTATION_0);
5864 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005865 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866
5867 // These calculations are based on the input device calibration documentation.
5868 int32_t rawX = 100;
5869 int32_t rawY = 200;
5870 int32_t rawTouchMajor = 7;
5871 int32_t rawTouchMinor = 6;
5872 int32_t rawToolMajor = 9;
5873 int32_t rawToolMinor = 8;
5874 int32_t rawPressure = 11;
5875 int32_t rawDistance = 0;
5876 int32_t rawOrientation = 3;
5877 int32_t id = 5;
5878
5879 float x = toDisplayX(rawX);
5880 float y = toDisplayY(rawY);
5881 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5882 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5883 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5884 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5885 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5886 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5887 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5888 float distance = float(rawDistance);
5889
5890 processPosition(mapper, rawX, rawY);
5891 processTouchMajor(mapper, rawTouchMajor);
5892 processTouchMinor(mapper, rawTouchMinor);
5893 processToolMajor(mapper, rawToolMajor);
5894 processToolMinor(mapper, rawToolMinor);
5895 processPressure(mapper, rawPressure);
5896 processOrientation(mapper, rawOrientation);
5897 processDistance(mapper, rawDistance);
5898 processId(mapper, id);
5899 processMTSync(mapper);
5900 processSync(mapper);
5901
5902 NotifyMotionArgs args;
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5904 ASSERT_EQ(0, args.pointerProperties[0].id);
5905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5906 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5907 orientation, distance));
5908}
5909
5910TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911 addConfigurationProperty("touch.deviceType", "touchScreen");
5912 prepareDisplay(DISPLAY_ORIENTATION_0);
5913 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5914 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005915 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916
5917 // These calculations are based on the input device calibration documentation.
5918 int32_t rawX = 100;
5919 int32_t rawY = 200;
5920 int32_t rawTouchMajor = 140;
5921 int32_t rawTouchMinor = 120;
5922 int32_t rawToolMajor = 180;
5923 int32_t rawToolMinor = 160;
5924
5925 float x = toDisplayX(rawX);
5926 float y = toDisplayY(rawY);
5927 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5928 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5929 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5930 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5931 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5932
5933 processPosition(mapper, rawX, rawY);
5934 processTouchMajor(mapper, rawTouchMajor);
5935 processTouchMinor(mapper, rawTouchMinor);
5936 processToolMajor(mapper, rawToolMajor);
5937 processToolMinor(mapper, rawToolMinor);
5938 processMTSync(mapper);
5939 processSync(mapper);
5940
5941 NotifyMotionArgs args;
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5944 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5945}
5946
5947TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005948 addConfigurationProperty("touch.deviceType", "touchScreen");
5949 prepareDisplay(DISPLAY_ORIENTATION_0);
5950 prepareAxes(POSITION | TOUCH | TOOL);
5951 addConfigurationProperty("touch.size.calibration", "diameter");
5952 addConfigurationProperty("touch.size.scale", "10");
5953 addConfigurationProperty("touch.size.bias", "160");
5954 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005955 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956
5957 // These calculations are based on the input device calibration documentation.
5958 // Note: We only provide a single common touch/tool value because the device is assumed
5959 // not to emit separate values for each pointer (isSummed = 1).
5960 int32_t rawX = 100;
5961 int32_t rawY = 200;
5962 int32_t rawX2 = 150;
5963 int32_t rawY2 = 250;
5964 int32_t rawTouchMajor = 5;
5965 int32_t rawToolMajor = 8;
5966
5967 float x = toDisplayX(rawX);
5968 float y = toDisplayY(rawY);
5969 float x2 = toDisplayX(rawX2);
5970 float y2 = toDisplayY(rawY2);
5971 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5972 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5973 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5974
5975 processPosition(mapper, rawX, rawY);
5976 processTouchMajor(mapper, rawTouchMajor);
5977 processToolMajor(mapper, rawToolMajor);
5978 processMTSync(mapper);
5979 processPosition(mapper, rawX2, rawY2);
5980 processTouchMajor(mapper, rawTouchMajor);
5981 processToolMajor(mapper, rawToolMajor);
5982 processMTSync(mapper);
5983 processSync(mapper);
5984
5985 NotifyMotionArgs args;
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5988
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5990 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5991 args.action);
5992 ASSERT_EQ(size_t(2), args.pointerCount);
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5994 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5996 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5997}
5998
5999TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000 addConfigurationProperty("touch.deviceType", "touchScreen");
6001 prepareDisplay(DISPLAY_ORIENTATION_0);
6002 prepareAxes(POSITION | TOUCH | TOOL);
6003 addConfigurationProperty("touch.size.calibration", "area");
6004 addConfigurationProperty("touch.size.scale", "43");
6005 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007
6008 // These calculations are based on the input device calibration documentation.
6009 int32_t rawX = 100;
6010 int32_t rawY = 200;
6011 int32_t rawTouchMajor = 5;
6012 int32_t rawToolMajor = 8;
6013
6014 float x = toDisplayX(rawX);
6015 float y = toDisplayY(rawY);
6016 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6017 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6018 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6019
6020 processPosition(mapper, rawX, rawY);
6021 processTouchMajor(mapper, rawTouchMajor);
6022 processToolMajor(mapper, rawToolMajor);
6023 processMTSync(mapper);
6024 processSync(mapper);
6025
6026 NotifyMotionArgs args;
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6029 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6030}
6031
6032TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 addConfigurationProperty("touch.deviceType", "touchScreen");
6034 prepareDisplay(DISPLAY_ORIENTATION_0);
6035 prepareAxes(POSITION | PRESSURE);
6036 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6037 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006038 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039
Michael Wrightaa449c92017-12-13 21:21:43 +00006040 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006041 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006042 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6043 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6044 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6045
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046 // These calculations are based on the input device calibration documentation.
6047 int32_t rawX = 100;
6048 int32_t rawY = 200;
6049 int32_t rawPressure = 60;
6050
6051 float x = toDisplayX(rawX);
6052 float y = toDisplayY(rawY);
6053 float pressure = float(rawPressure) * 0.01f;
6054
6055 processPosition(mapper, rawX, rawY);
6056 processPressure(mapper, rawPressure);
6057 processMTSync(mapper);
6058 processSync(mapper);
6059
6060 NotifyMotionArgs args;
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6063 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6064}
6065
6066TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067 addConfigurationProperty("touch.deviceType", "touchScreen");
6068 prepareDisplay(DISPLAY_ORIENTATION_0);
6069 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006070 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
6072 NotifyMotionArgs motionArgs;
6073 NotifyKeyArgs keyArgs;
6074
6075 processId(mapper, 1);
6076 processPosition(mapper, 100, 200);
6077 processSync(mapper);
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6079 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6080 ASSERT_EQ(0, motionArgs.buttonState);
6081
6082 // press BTN_LEFT, release BTN_LEFT
6083 processKey(mapper, BTN_LEFT, 1);
6084 processSync(mapper);
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6087 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6088
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6091 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6092
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 processKey(mapper, BTN_LEFT, 0);
6094 processSync(mapper);
6095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006098
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102
6103 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6104 processKey(mapper, BTN_RIGHT, 1);
6105 processKey(mapper, BTN_MIDDLE, 1);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6109 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6110 motionArgs.buttonState);
6111
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6114 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6115
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6118 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6119 motionArgs.buttonState);
6120
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121 processKey(mapper, BTN_RIGHT, 0);
6122 processSync(mapper);
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006125 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006126
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006129 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130
6131 processKey(mapper, BTN_MIDDLE, 0);
6132 processSync(mapper);
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006136
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006139 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140
6141 // press BTN_BACK, release BTN_BACK
6142 processKey(mapper, BTN_BACK, 1);
6143 processSync(mapper);
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6146 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006147
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006150 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6151
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6154 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155
6156 processKey(mapper, BTN_BACK, 0);
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164 ASSERT_EQ(0, motionArgs.buttonState);
6165
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6167 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6168 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6169
6170 // press BTN_SIDE, release BTN_SIDE
6171 processKey(mapper, BTN_SIDE, 1);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6174 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6175 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006176
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006179 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6180
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6183 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184
6185 processKey(mapper, BTN_SIDE, 0);
6186 processSync(mapper);
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006190
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006193 ASSERT_EQ(0, motionArgs.buttonState);
6194
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6196 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6197 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6198
6199 // press BTN_FORWARD, release BTN_FORWARD
6200 processKey(mapper, BTN_FORWARD, 1);
6201 processSync(mapper);
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6204 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006205
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006208 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6209
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6212 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213
6214 processKey(mapper, BTN_FORWARD, 0);
6215 processSync(mapper);
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006219
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006222 ASSERT_EQ(0, motionArgs.buttonState);
6223
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6225 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6226 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6227
6228 // press BTN_EXTRA, release BTN_EXTRA
6229 processKey(mapper, BTN_EXTRA, 1);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6232 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6233 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006234
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006237 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6238
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6241 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
6243 processKey(mapper, BTN_EXTRA, 0);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006251 ASSERT_EQ(0, motionArgs.buttonState);
6252
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6254 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6255 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6256
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6258
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259 // press BTN_STYLUS, release BTN_STYLUS
6260 processKey(mapper, BTN_STYLUS, 1);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006264 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6265
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6268 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006269
6270 processKey(mapper, BTN_STYLUS, 0);
6271 processSync(mapper);
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006275
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006278 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279
6280 // press BTN_STYLUS2, release BTN_STYLUS2
6281 processKey(mapper, BTN_STYLUS2, 1);
6282 processSync(mapper);
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006285 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6286
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6289 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290
6291 processKey(mapper, BTN_STYLUS2, 0);
6292 processSync(mapper);
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300
6301 // release touch
6302 processId(mapper, -1);
6303 processSync(mapper);
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6305 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6306 ASSERT_EQ(0, motionArgs.buttonState);
6307}
6308
6309TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 addConfigurationProperty("touch.deviceType", "touchScreen");
6311 prepareDisplay(DISPLAY_ORIENTATION_0);
6312 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006313 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314
6315 NotifyMotionArgs motionArgs;
6316
6317 // default tool type is finger
6318 processId(mapper, 1);
6319 processPosition(mapper, 100, 200);
6320 processSync(mapper);
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6322 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6324
6325 // eraser
6326 processKey(mapper, BTN_TOOL_RUBBER, 1);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6331
6332 // stylus
6333 processKey(mapper, BTN_TOOL_RUBBER, 0);
6334 processKey(mapper, BTN_TOOL_PEN, 1);
6335 processSync(mapper);
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6339
6340 // brush
6341 processKey(mapper, BTN_TOOL_PEN, 0);
6342 processKey(mapper, BTN_TOOL_BRUSH, 1);
6343 processSync(mapper);
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6347
6348 // pencil
6349 processKey(mapper, BTN_TOOL_BRUSH, 0);
6350 processKey(mapper, BTN_TOOL_PENCIL, 1);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6355
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006356 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357 processKey(mapper, BTN_TOOL_PENCIL, 0);
6358 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6363
6364 // mouse
6365 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6366 processKey(mapper, BTN_TOOL_MOUSE, 1);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6371
6372 // lens
6373 processKey(mapper, BTN_TOOL_MOUSE, 0);
6374 processKey(mapper, BTN_TOOL_LENS, 1);
6375 processSync(mapper);
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6379
6380 // double-tap
6381 processKey(mapper, BTN_TOOL_LENS, 0);
6382 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6387
6388 // triple-tap
6389 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6390 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6391 processSync(mapper);
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6395
6396 // quad-tap
6397 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6398 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6399 processSync(mapper);
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6403
6404 // finger
6405 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6406 processKey(mapper, BTN_TOOL_FINGER, 1);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6411
6412 // stylus trumps finger
6413 processKey(mapper, BTN_TOOL_PEN, 1);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6418
6419 // eraser trumps stylus
6420 processKey(mapper, BTN_TOOL_RUBBER, 1);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6425
6426 // mouse trumps eraser
6427 processKey(mapper, BTN_TOOL_MOUSE, 1);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6432
6433 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6434 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6435 processSync(mapper);
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6439
6440 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6441 processToolType(mapper, MT_TOOL_PEN);
6442 processSync(mapper);
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6446
6447 // back to default tool type
6448 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6449 processKey(mapper, BTN_TOOL_MOUSE, 0);
6450 processKey(mapper, BTN_TOOL_RUBBER, 0);
6451 processKey(mapper, BTN_TOOL_PEN, 0);
6452 processKey(mapper, BTN_TOOL_FINGER, 0);
6453 processSync(mapper);
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6457}
6458
6459TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 addConfigurationProperty("touch.deviceType", "touchScreen");
6461 prepareDisplay(DISPLAY_ORIENTATION_0);
6462 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006463 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006464 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465
6466 NotifyMotionArgs motionArgs;
6467
6468 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6469 processId(mapper, 1);
6470 processPosition(mapper, 100, 200);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6475 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6476
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6480 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6481
6482 // move a little
6483 processPosition(mapper, 150, 250);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6488 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6489
6490 // down when BTN_TOUCH is pressed, pressure defaults to 1
6491 processKey(mapper, BTN_TOUCH, 1);
6492 processSync(mapper);
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6496 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6497
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6501 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6502
6503 // up when BTN_TOUCH is released, hover restored
6504 processKey(mapper, BTN_TOUCH, 0);
6505 processSync(mapper);
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6509 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6510
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6514 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6515
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6519 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6520
6521 // exit hover when pointer goes away
6522 processId(mapper, -1);
6523 processSync(mapper);
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6527 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6528}
6529
6530TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006531 addConfigurationProperty("touch.deviceType", "touchScreen");
6532 prepareDisplay(DISPLAY_ORIENTATION_0);
6533 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006534 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006535
6536 NotifyMotionArgs motionArgs;
6537
6538 // initially hovering because pressure is 0
6539 processId(mapper, 1);
6540 processPosition(mapper, 100, 200);
6541 processPressure(mapper, 0);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6546 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6551 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6552
6553 // move a little
6554 processPosition(mapper, 150, 250);
6555 processSync(mapper);
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6559 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6560
6561 // down when pressure becomes non-zero
6562 processPressure(mapper, RAW_PRESSURE_MAX);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6567 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6568
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6572 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6573
6574 // up when pressure becomes 0, hover restored
6575 processPressure(mapper, 0);
6576 processSync(mapper);
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6580 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6581
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6585 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6586
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6590 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6591
6592 // exit hover when pointer goes away
6593 processId(mapper, -1);
6594 processSync(mapper);
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6598 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6599}
6600
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006601/**
6602 * Set the input device port <--> display port associations, and check that the
6603 * events are routed to the display that matches the display port.
6604 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6605 */
6606TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006607 const std::string usb2 = "USB2";
6608 const uint8_t hdmi1 = 0;
6609 const uint8_t hdmi2 = 1;
6610 const std::string secondaryUniqueId = "uniqueId2";
6611 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6612
6613 addConfigurationProperty("touch.deviceType", "touchScreen");
6614 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006615 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006616
6617 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6618 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6619
6620 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6621 // for this input device is specified, and the matching viewport is not present,
6622 // the input device should be disabled (at the mapper level).
6623
6624 // Add viewport for display 2 on hdmi2
6625 prepareSecondaryDisplay(type, hdmi2);
6626 // Send a touch event
6627 processPosition(mapper, 100, 100);
6628 processSync(mapper);
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6630
6631 // Add viewport for display 1 on hdmi1
6632 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6633 // Send a touch event again
6634 processPosition(mapper, 100, 100);
6635 processSync(mapper);
6636
6637 NotifyMotionArgs args;
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6639 ASSERT_EQ(DISPLAY_ID, args.displayId);
6640}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006641
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006642TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006643 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006644 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006645 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006646 fakePointerController->setPosition(100, 200);
6647 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006648 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6649
Garfield Tan888a6a42020-01-09 11:39:16 -08006650 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6651 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6652
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006653 prepareDisplay(DISPLAY_ORIENTATION_0);
6654 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006655 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006656
6657 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006658 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006659
6660 NotifyMotionArgs motionArgs;
6661 processPosition(mapper, 100, 100);
6662 processSync(mapper);
6663
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6666 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6667}
6668
Arthur Hung7c645402019-01-25 17:45:42 +08006669TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6670 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006671 prepareAxes(POSITION | ID | SLOT);
6672 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006674
6675 // Create the second touch screen device, and enable multi fingers.
6676 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006677 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006678 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006679 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006680 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006681 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006682 std::unique_ptr<InputDevice> device2 =
6683 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006684 identifier);
6685 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6686 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6687 0 /*flat*/, 0 /*fuzz*/);
6688 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6689 0 /*flat*/, 0 /*fuzz*/);
6690 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6691 0 /*flat*/, 0 /*fuzz*/);
6692 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6693 0 /*flat*/, 0 /*fuzz*/);
6694 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6695 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6696 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006697
6698 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006699 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006700 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6701 device2->reset(ARBITRARY_TIME);
6702
6703 // Setup PointerController.
6704 sp<FakePointerController> fakePointerController = new FakePointerController();
6705 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6706 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6707
6708 // Setup policy for associated displays and show touches.
6709 const uint8_t hdmi1 = 0;
6710 const uint8_t hdmi2 = 1;
6711 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6712 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6713 mFakePolicy->setShowTouches(true);
6714
6715 // Create displays.
6716 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6717 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6718
6719 // Default device will reconfigure above, need additional reconfiguration for another device.
6720 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6721 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6722
6723 // Two fingers down at default display.
6724 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6725 processPosition(mapper, x1, y1);
6726 processId(mapper, 1);
6727 processSlot(mapper, 1);
6728 processPosition(mapper, x2, y2);
6729 processId(mapper, 2);
6730 processSync(mapper);
6731
6732 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6733 fakePointerController->getSpots().find(DISPLAY_ID);
6734 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6735 ASSERT_EQ(size_t(2), iter->second.size());
6736
6737 // Two fingers down at second display.
6738 processPosition(mapper2, x1, y1);
6739 processId(mapper2, 1);
6740 processSlot(mapper2, 1);
6741 processPosition(mapper2, x2, y2);
6742 processId(mapper2, 2);
6743 processSync(mapper2);
6744
6745 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6746 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6747 ASSERT_EQ(size_t(2), iter->second.size());
6748}
6749
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006750TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006751 prepareAxes(POSITION);
6752 addConfigurationProperty("touch.deviceType", "touchScreen");
6753 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006754 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006755
6756 NotifyMotionArgs motionArgs;
6757 // Unrotated video frame
6758 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6759 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006760 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006761 processPosition(mapper, 100, 200);
6762 processSync(mapper);
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6764 ASSERT_EQ(frames, motionArgs.videoFrames);
6765
6766 // Subsequent touch events should not have any videoframes
6767 // This is implemented separately in FakeEventHub,
6768 // but that should match the behaviour of TouchVideoDevice.
6769 processPosition(mapper, 200, 200);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6773}
6774
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006775TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006776 prepareAxes(POSITION);
6777 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006778 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006779 // Unrotated video frame
6780 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6781 NotifyMotionArgs motionArgs;
6782
6783 // Test all 4 orientations
6784 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6785 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6786 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6787 clearViewports();
6788 prepareDisplay(orientation);
6789 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006790 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006791 processPosition(mapper, 100, 200);
6792 processSync(mapper);
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6794 frames[0].rotate(orientation);
6795 ASSERT_EQ(frames, motionArgs.videoFrames);
6796 }
6797}
6798
6799TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006800 prepareAxes(POSITION);
6801 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006802 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006803 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6804 // so mix these.
6805 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6806 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6807 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6808 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6809 NotifyMotionArgs motionArgs;
6810
6811 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006812 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006813 processPosition(mapper, 100, 200);
6814 processSync(mapper);
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6816 std::for_each(frames.begin(), frames.end(),
6817 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6818 ASSERT_EQ(frames, motionArgs.videoFrames);
6819}
6820
Arthur Hung9da14732019-09-02 16:16:58 +08006821/**
6822 * If we had defined port associations, but the viewport is not ready, the touch device would be
6823 * expected to be disabled, and it should be enabled after the viewport has found.
6824 */
6825TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006826 constexpr uint8_t hdmi2 = 1;
6827 const std::string secondaryUniqueId = "uniqueId2";
6828 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6829
6830 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6831
6832 addConfigurationProperty("touch.deviceType", "touchScreen");
6833 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006834 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006835
6836 ASSERT_EQ(mDevice->isEnabled(), false);
6837
6838 // Add display on hdmi2, the device should be enabled and can receive touch event.
6839 prepareSecondaryDisplay(type, hdmi2);
6840 ASSERT_EQ(mDevice->isEnabled(), true);
6841
6842 // Send a touch event.
6843 processPosition(mapper, 100, 100);
6844 processSync(mapper);
6845
6846 NotifyMotionArgs args;
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6848 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6849}
6850
Arthur Hung6cd19a42019-08-30 19:04:12 +08006851/**
6852 * Test touch should not work if outside of surface.
6853 */
6854TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
Arthur Hung6cd19a42019-08-30 19:04:12 +08006855 addConfigurationProperty("touch.deviceType", "touchScreen");
6856 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006857 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006858 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung6cd19a42019-08-30 19:04:12 +08006859
Arthur Hung05de5772019-09-26 18:31:26 +08006860 // Touch on left-top area should work.
6861 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6862 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6863 processPosition(mapper, rawX, rawY);
6864 processSync(mapper);
6865
6866 NotifyMotionArgs args;
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6868
6869 // Reset.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006870 mapper.reset(ARBITRARY_TIME);
Arthur Hung05de5772019-09-26 18:31:26 +08006871
6872 // Let logical display be different to physical display and rotate 90-degrees.
6873 std::optional<DisplayViewport> internalViewport =
6874 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6875 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6876 internalViewport->logicalLeft = 0;
6877 internalViewport->logicalTop = 0;
6878 internalViewport->logicalRight = DISPLAY_HEIGHT;
6879 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6880
6881 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6882 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6883 internalViewport->physicalRight = DISPLAY_HEIGHT;
6884 internalViewport->physicalBottom = DISPLAY_WIDTH;
6885
6886 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6887 internalViewport->deviceHeight = DISPLAY_WIDTH;
6888 mFakePolicy->updateViewport(internalViewport.value());
6889 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6890
6891 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006892 processPosition(mapper, rawX, rawY);
6893 processSync(mapper);
6894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6895}
6896
Arthur Hung421eb1c2020-01-16 00:09:42 +08006897TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006898 addConfigurationProperty("touch.deviceType", "touchScreen");
6899 prepareDisplay(DISPLAY_ORIENTATION_0);
6900 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006901 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006902
6903 NotifyMotionArgs motionArgs;
6904
6905 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6906 // finger down
6907 processId(mapper, 1);
6908 processPosition(mapper, x1, y1);
6909 processSync(mapper);
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6913
6914 // finger move
6915 processId(mapper, 1);
6916 processPosition(mapper, x2, y2);
6917 processSync(mapper);
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6921
6922 // finger up.
6923 processId(mapper, -1);
6924 processSync(mapper);
6925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6926 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6928
6929 // new finger down
6930 processId(mapper, 1);
6931 processPosition(mapper, x3, y3);
6932 processSync(mapper);
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6936}
6937
6938/**
6939 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6940 * UP events should be ignored.
6941 */
6942TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006943 addConfigurationProperty("touch.deviceType", "touchScreen");
6944 prepareDisplay(DISPLAY_ORIENTATION_0);
6945 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006946 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006947
6948 NotifyMotionArgs motionArgs;
6949
6950 // default tool type is finger
6951 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6952 processId(mapper, 1);
6953 processPosition(mapper, x1, y1);
6954 processSync(mapper);
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6956 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6958
6959 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6960 processToolType(mapper, MT_TOOL_PALM);
6961 processSync(mapper);
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6963 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6964
6965 // Ignore the following MOVE and UP events if had detect a palm event.
6966 processId(mapper, 1);
6967 processPosition(mapper, x2, y2);
6968 processSync(mapper);
6969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6970
6971 // finger up.
6972 processId(mapper, -1);
6973 processSync(mapper);
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6975
6976 // new finger down
6977 processToolType(mapper, MT_TOOL_FINGER);
6978 processId(mapper, 1);
6979 processPosition(mapper, x3, y3);
6980 processSync(mapper);
6981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6982 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6984}
6985
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006986// --- MultiTouchInputMapperTest_ExternalDevice ---
6987
6988class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
6989protected:
6990 virtual void SetUp() override {
6991 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
6992 }
6993};
6994
6995/**
6996 * Expect fallback to internal viewport if device is external and external viewport is not present.
6997 */
6998TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
6999 prepareAxes(POSITION);
7000 addConfigurationProperty("touch.deviceType", "touchScreen");
7001 prepareDisplay(DISPLAY_ORIENTATION_0);
7002 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7003
7004 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7005
7006 NotifyMotionArgs motionArgs;
7007
7008 // Expect the event to be sent to the internal viewport,
7009 // because an external viewport is not present.
7010 processPosition(mapper, 100, 100);
7011 processSync(mapper);
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7013 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7014
7015 // Expect the event to be sent to the external viewport if it is present.
7016 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
7017 processPosition(mapper, 100, 100);
7018 processSync(mapper);
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7020 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7021}
7022
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023} // namespace android