blob: 3ae8b56c1111555c96c71a589e3232d1632c3bb9 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
25#include <SingleTouchInputMapper.h>
26#include <SwitchInputMapper.h>
27#include <TestInputListener.h>
28#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080029#include <UinputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070031#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080033#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <math.h>
35
36namespace android {
37
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038using std::chrono_literals::operator""ms;
39
40// Timeout for waiting for an expected event
41static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
42
Michael Wrightd02c5b62014-02-10 15:10:22 -080043// An arbitrary time value.
44static const nsecs_t ARBITRARY_TIME = 1234;
45
46// Arbitrary display properties.
47static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070048static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080049static const int32_t DISPLAY_WIDTH = 480;
50static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070051static const int32_t VIRTUAL_DISPLAY_ID = 1;
52static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
53static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070054static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070055static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Error tolerance for floating point assertions.
58static const float EPSILON = 0.001f;
59
60template<typename T>
61static inline T min(T a, T b) {
62 return a < b ? a : b;
63}
64
65static inline float avg(float x, float y) {
66 return (x + y) / 2;
67}
68
69
70// --- FakePointerController ---
71
72class FakePointerController : public PointerControllerInterface {
73 bool mHaveBounds;
74 float mMinX, mMinY, mMaxX, mMaxY;
75 float mX, mY;
76 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080077 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
79protected:
80 virtual ~FakePointerController() { }
81
82public:
83 FakePointerController() :
84 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080085 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080086 }
87
88 void setBounds(float minX, float minY, float maxX, float maxY) {
89 mHaveBounds = true;
90 mMinX = minX;
91 mMinY = minY;
92 mMaxX = maxX;
93 mMaxY = maxY;
94 }
95
96 virtual void setPosition(float x, float y) {
97 mX = x;
98 mY = y;
99 }
100
101 virtual void setButtonState(int32_t buttonState) {
102 mButtonState = buttonState;
103 }
104
105 virtual int32_t getButtonState() const {
106 return mButtonState;
107 }
108
109 virtual void getPosition(float* outX, float* outY) const {
110 *outX = mX;
111 *outY = mY;
112 }
113
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800114 virtual int32_t getDisplayId() const {
115 return mDisplayId;
116 }
117
Garfield Tan888a6a42020-01-09 11:39:16 -0800118 virtual void setDisplayViewport(const DisplayViewport& viewport) {
119 mDisplayId = viewport.displayId;
120 }
121
Arthur Hung7c645402019-01-25 17:45:42 +0800122 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
123 return mSpotsByDisplay;
124 }
125
Michael Wrightd02c5b62014-02-10 15:10:22 -0800126private:
127 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
128 *outMinX = mMinX;
129 *outMinY = mMinY;
130 *outMaxX = mMaxX;
131 *outMaxY = mMaxY;
132 return mHaveBounds;
133 }
134
135 virtual void move(float deltaX, float deltaY) {
136 mX += deltaX;
137 if (mX < mMinX) mX = mMinX;
138 if (mX > mMaxX) mX = mMaxX;
139 mY += deltaY;
140 if (mY < mMinY) mY = mMinY;
141 if (mY > mMaxY) mY = mMaxY;
142 }
143
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100144 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 }
146
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100147 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 }
149
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100150 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 }
152
Arthur Hung7c645402019-01-25 17:45:42 +0800153 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
154 int32_t displayId) {
155 std::vector<int32_t> newSpots;
156 // Add spots for fingers that are down.
157 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
158 uint32_t id = idBits.clearFirstMarkedBit();
159 newSpots.push_back(id);
160 }
161
162 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 }
164
165 virtual void clearSpots() {
166 }
Arthur Hung7c645402019-01-25 17:45:42 +0800167
168 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169};
170
171
172// --- FakeInputReaderPolicy ---
173
174class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700175 std::mutex mLock;
176 std::condition_variable mDevicesChangedCondition;
177
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 InputReaderConfiguration mConfig;
179 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700180 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
181 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100182 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700183 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
185protected:
186 virtual ~FakeInputReaderPolicy() { }
187
188public:
189 FakeInputReaderPolicy() {
190 }
191
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700192 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800193 waitForInputDevices([](bool devicesChanged) {
194 if (!devicesChanged) {
195 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
196 }
197 });
198 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700199
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800200 void assertInputDevicesNotChanged() {
201 waitForInputDevices([](bool devicesChanged) {
202 if (devicesChanged) {
203 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
204 }
205 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700206 }
207
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700208 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100209 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100210 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700211 }
212
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700213 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
214 return mConfig.getDisplayViewportByUniqueId(uniqueId);
215 }
216 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
217 return mConfig.getDisplayViewportByType(type);
218 }
219
220 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
221 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700222 }
223
224 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700225 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
226 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700227 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700228 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700229 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100230 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 }
232
Arthur Hung6cd19a42019-08-30 19:04:12 +0800233 bool updateViewport(const DisplayViewport& viewport) {
234 size_t count = mViewports.size();
235 for (size_t i = 0; i < count; i++) {
236 const DisplayViewport& currentViewport = mViewports[i];
237 if (currentViewport.displayId == viewport.displayId) {
238 mViewports[i] = viewport;
239 mConfig.setDisplayViewports(mViewports);
240 return true;
241 }
242 }
243 // no viewport found.
244 return false;
245 }
246
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100247 void addExcludedDeviceName(const std::string& deviceName) {
248 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 }
250
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700251 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
252 mConfig.portAssociations.insert({inputPort, displayPort});
253 }
254
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000255 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700256
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000257 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
260 mPointerControllers.add(deviceId, controller);
261 }
262
263 const InputReaderConfiguration* getReaderConfiguration() const {
264 return &mConfig;
265 }
266
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800267 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 return mInputDevices;
269 }
270
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100271 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700272 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700273 return transform;
274 }
275
276 void setTouchAffineTransformation(const TouchAffineTransformation t) {
277 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800278 }
279
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800280 void setPointerCapture(bool enabled) {
281 mConfig.pointerCapture = enabled;
282 }
283
Arthur Hung7c645402019-01-25 17:45:42 +0800284 void setShowTouches(bool enabled) {
285 mConfig.showTouches = enabled;
286 }
287
Garfield Tan888a6a42020-01-09 11:39:16 -0800288 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
289 mConfig.defaultPointerDisplayId = pointerDisplayId;
290 }
291
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700293 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700294 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
295 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700296 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
297 || orientation == DISPLAY_ORIENTATION_270);
298 DisplayViewport v;
299 v.displayId = displayId;
300 v.orientation = orientation;
301 v.logicalLeft = 0;
302 v.logicalTop = 0;
303 v.logicalRight = isRotated ? height : width;
304 v.logicalBottom = isRotated ? width : height;
305 v.physicalLeft = 0;
306 v.physicalTop = 0;
307 v.physicalRight = isRotated ? height : width;
308 v.physicalBottom = isRotated ? width : height;
309 v.deviceWidth = isRotated ? height : width;
310 v.deviceHeight = isRotated ? width : height;
311 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700312 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100313 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700314 return v;
315 }
316
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
318 *outConfig = mConfig;
319 }
320
321 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
322 return mPointerControllers.valueFor(deviceId);
323 }
324
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800325 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700326 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700328 mInputDevicesChanged = true;
329 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 }
331
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100332 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700333 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100336 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
337 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800339
340 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
341 std::unique_lock<std::mutex> lock(mLock);
342 base::ScopedLockAssertion assumeLocked(mLock);
343
344 const bool devicesChanged =
345 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
346 return mInputDevicesChanged;
347 });
348 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
349 mInputDevicesChanged = false;
350 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351};
352
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353// --- FakeEventHub ---
354
355class FakeEventHub : public EventHubInterface {
356 struct KeyInfo {
357 int32_t keyCode;
358 uint32_t flags;
359 };
360
361 struct Device {
362 InputDeviceIdentifier identifier;
363 uint32_t classes;
364 PropertyMap configuration;
365 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
366 KeyedVector<int, bool> relativeAxes;
367 KeyedVector<int32_t, int32_t> keyCodeStates;
368 KeyedVector<int32_t, int32_t> scanCodeStates;
369 KeyedVector<int32_t, int32_t> switchStates;
370 KeyedVector<int32_t, int32_t> absoluteAxisValue;
371 KeyedVector<int32_t, KeyInfo> keysByScanCode;
372 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
373 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800374 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700375 bool enabled;
376
377 status_t enable() {
378 enabled = true;
379 return OK;
380 }
381
382 status_t disable() {
383 enabled = false;
384 return OK;
385 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700387 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700388 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 }
390 };
391
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700392 std::mutex mLock;
393 std::condition_variable mEventsCondition;
394
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100396 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700397 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600398 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700400public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 virtual ~FakeEventHub() {
402 for (size_t i = 0; i < mDevices.size(); i++) {
403 delete mDevices.valueAt(i);
404 }
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 FakeEventHub() { }
408
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100409 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 Device* device = new Device(classes);
411 device->identifier.name = name;
412 mDevices.add(deviceId, device);
413
414 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
415 }
416
417 void removeDevice(int32_t deviceId) {
418 delete mDevices.valueFor(deviceId);
419 mDevices.removeItem(deviceId);
420
421 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
422 }
423
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700424 bool isDeviceEnabled(int32_t deviceId) {
425 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700426 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700427 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
428 return false;
429 }
430 return device->enabled;
431 }
432
433 status_t enableDevice(int32_t deviceId) {
434 status_t result;
435 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700436 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
438 return BAD_VALUE;
439 }
440 if (device->enabled) {
441 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
442 return OK;
443 }
444 result = device->enable();
445 return result;
446 }
447
448 status_t disableDevice(int32_t deviceId) {
449 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700450 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700451 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
452 return BAD_VALUE;
453 }
454 if (!device->enabled) {
455 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
456 return OK;
457 }
458 return device->disable();
459 }
460
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 void finishDeviceScan() {
462 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
463 }
464
465 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
466 Device* device = getDevice(deviceId);
467 device->configuration.addProperty(key, value);
468 }
469
470 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
471 Device* device = getDevice(deviceId);
472 device->configuration.addAll(configuration);
473 }
474
475 void addAbsoluteAxis(int32_t deviceId, int axis,
476 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
477 Device* device = getDevice(deviceId);
478
479 RawAbsoluteAxisInfo info;
480 info.valid = true;
481 info.minValue = minValue;
482 info.maxValue = maxValue;
483 info.flat = flat;
484 info.fuzz = fuzz;
485 info.resolution = resolution;
486 device->absoluteAxes.add(axis, info);
487 }
488
489 void addRelativeAxis(int32_t deviceId, int32_t axis) {
490 Device* device = getDevice(deviceId);
491 device->relativeAxes.add(axis, true);
492 }
493
494 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
495 Device* device = getDevice(deviceId);
496 device->keyCodeStates.replaceValueFor(keyCode, state);
497 }
498
499 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
500 Device* device = getDevice(deviceId);
501 device->scanCodeStates.replaceValueFor(scanCode, state);
502 }
503
504 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
505 Device* device = getDevice(deviceId);
506 device->switchStates.replaceValueFor(switchCode, state);
507 }
508
509 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
510 Device* device = getDevice(deviceId);
511 device->absoluteAxisValue.replaceValueFor(axis, value);
512 }
513
514 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
515 int32_t keyCode, uint32_t flags) {
516 Device* device = getDevice(deviceId);
517 KeyInfo info;
518 info.keyCode = keyCode;
519 info.flags = flags;
520 if (scanCode) {
521 device->keysByScanCode.add(scanCode, info);
522 }
523 if (usageCode) {
524 device->keysByUsageCode.add(usageCode, info);
525 }
526 }
527
528 void addLed(int32_t deviceId, int32_t led, bool initialState) {
529 Device* device = getDevice(deviceId);
530 device->leds.add(led, initialState);
531 }
532
533 bool getLedState(int32_t deviceId, int32_t led) {
534 Device* device = getDevice(deviceId);
535 return device->leds.valueFor(led);
536 }
537
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100538 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 return mExcludedDevices;
540 }
541
542 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
543 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800544 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 }
546
547 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
548 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700549 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 RawEvent event;
551 event.when = when;
552 event.deviceId = deviceId;
553 event.type = type;
554 event.code = code;
555 event.value = value;
556 mEvents.push_back(event);
557
558 if (type == EV_ABS) {
559 setAbsoluteAxisValue(deviceId, code, value);
560 }
561 }
562
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600563 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
564 std::vector<TouchVideoFrame>> videoFrames) {
565 mVideoFrames = std::move(videoFrames);
566 }
567
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700569 std::unique_lock<std::mutex> lock(mLock);
570 base::ScopedLockAssertion assumeLocked(mLock);
571 const bool queueIsEmpty =
572 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
573 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
574 if (!queueIsEmpty) {
575 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
576 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
579private:
580 Device* getDevice(int32_t deviceId) const {
581 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100582 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 }
584
585 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
586 Device* device = getDevice(deviceId);
587 return device ? device->classes : 0;
588 }
589
590 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
591 Device* device = getDevice(deviceId);
592 return device ? device->identifier : InputDeviceIdentifier();
593 }
594
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100595 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 return 0;
597 }
598
599 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
600 Device* device = getDevice(deviceId);
601 if (device) {
602 *outConfiguration = device->configuration;
603 }
604 }
605
606 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
607 RawAbsoluteAxisInfo* outAxisInfo) const {
608 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800609 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 ssize_t index = device->absoluteAxes.indexOfKey(axis);
611 if (index >= 0) {
612 *outAxisInfo = device->absoluteAxes.valueAt(index);
613 return OK;
614 }
615 }
616 outAxisInfo->clear();
617 return -1;
618 }
619
620 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
621 Device* device = getDevice(deviceId);
622 if (device) {
623 return device->relativeAxes.indexOfKey(axis) >= 0;
624 }
625 return false;
626 }
627
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100628 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 return false;
630 }
631
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700632 virtual status_t mapKey(int32_t deviceId,
633 int32_t scanCode, int32_t usageCode, int32_t metaState,
634 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 Device* device = getDevice(deviceId);
636 if (device) {
637 const KeyInfo* key = getKey(device, scanCode, usageCode);
638 if (key) {
639 if (outKeycode) {
640 *outKeycode = key->keyCode;
641 }
642 if (outFlags) {
643 *outFlags = key->flags;
644 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700645 if (outMetaState) {
646 *outMetaState = metaState;
647 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 return OK;
649 }
650 }
651 return NAME_NOT_FOUND;
652 }
653
654 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
655 if (usageCode) {
656 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
657 if (index >= 0) {
658 return &device->keysByUsageCode.valueAt(index);
659 }
660 }
661 if (scanCode) {
662 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
663 if (index >= 0) {
664 return &device->keysByScanCode.valueAt(index);
665 }
666 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700667 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 }
669
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100670 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 return NAME_NOT_FOUND;
672 }
673
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100674 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 mExcludedDevices = devices;
676 }
677
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100678 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700679 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 if (mEvents.empty()) {
681 return 0;
682 }
683
684 *buffer = *mEvents.begin();
685 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700686 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 return 1;
688 }
689
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800690 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600691 auto it = mVideoFrames.find(deviceId);
692 if (it != mVideoFrames.end()) {
693 std::vector<TouchVideoFrame> frames = std::move(it->second);
694 mVideoFrames.erase(deviceId);
695 return frames;
696 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800697 return {};
698 }
699
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
701 Device* device = getDevice(deviceId);
702 if (device) {
703 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
704 if (index >= 0) {
705 return device->scanCodeStates.valueAt(index);
706 }
707 }
708 return AKEY_STATE_UNKNOWN;
709 }
710
711 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
712 Device* device = getDevice(deviceId);
713 if (device) {
714 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
715 if (index >= 0) {
716 return device->keyCodeStates.valueAt(index);
717 }
718 }
719 return AKEY_STATE_UNKNOWN;
720 }
721
722 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
723 Device* device = getDevice(deviceId);
724 if (device) {
725 ssize_t index = device->switchStates.indexOfKey(sw);
726 if (index >= 0) {
727 return device->switchStates.valueAt(index);
728 }
729 }
730 return AKEY_STATE_UNKNOWN;
731 }
732
733 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
734 int32_t* outValue) const {
735 Device* device = getDevice(deviceId);
736 if (device) {
737 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
738 if (index >= 0) {
739 *outValue = device->absoluteAxisValue.valueAt(index);
740 return OK;
741 }
742 }
743 *outValue = 0;
744 return -1;
745 }
746
747 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
748 uint8_t* outFlags) const {
749 bool result = false;
750 Device* device = getDevice(deviceId);
751 if (device) {
752 for (size_t i = 0; i < numCodes; i++) {
753 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
754 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
755 outFlags[i] = 1;
756 result = true;
757 }
758 }
759 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
760 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
761 outFlags[i] = 1;
762 result = true;
763 }
764 }
765 }
766 }
767 return result;
768 }
769
770 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
771 Device* device = getDevice(deviceId);
772 if (device) {
773 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
774 return index >= 0;
775 }
776 return false;
777 }
778
779 virtual bool hasLed(int32_t deviceId, int32_t led) const {
780 Device* device = getDevice(deviceId);
781 return device && device->leds.indexOfKey(led) >= 0;
782 }
783
784 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
785 Device* device = getDevice(deviceId);
786 if (device) {
787 ssize_t index = device->leds.indexOfKey(led);
788 if (index >= 0) {
789 device->leds.replaceValueAt(led, on);
790 } else {
791 ADD_FAILURE()
792 << "Attempted to set the state of an LED that the EventHub declared "
793 "was not present. led=" << led;
794 }
795 }
796 }
797
798 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800799 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 outVirtualKeys.clear();
801
802 Device* device = getDevice(deviceId);
803 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800804 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 }
806 }
807
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100808 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700809 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100812 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 return false;
814 }
815
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100816 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 }
818
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100819 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100822 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 return false;
824 }
825
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800826 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
828
829 virtual void monitor() {
830 }
831
832 virtual void requestReopenDevices() {
833 }
834
835 virtual void wake() {
836 }
837};
838
839
840// --- FakeInputReaderContext ---
841
842class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700843 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 sp<InputReaderPolicyInterface> mPolicy;
845 sp<InputListenerInterface> mListener;
846 int32_t mGlobalMetaState;
847 bool mUpdateGlobalMetaStateWasCalled;
848 int32_t mGeneration;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800849 int32_t mNextId;
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800850 wp<PointerControllerInterface> mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700853 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
854 const sp<InputReaderPolicyInterface>& policy,
855 const sp<InputListenerInterface>& listener)
856 : mEventHub(eventHub),
857 mPolicy(policy),
858 mListener(listener),
859 mGlobalMetaState(0),
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800860 mNextId(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
862 virtual ~FakeInputReaderContext() { }
863
864 void assertUpdateGlobalMetaStateWasCalled() {
865 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
866 << "Expected updateGlobalMetaState() to have been called.";
867 mUpdateGlobalMetaStateWasCalled = false;
868 }
869
870 void setGlobalMetaState(int32_t state) {
871 mGlobalMetaState = state;
872 }
873
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800874 uint32_t getGeneration() {
875 return mGeneration;
876 }
877
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800878 void updatePointerDisplay() {
879 sp<PointerControllerInterface> controller = mPointerController.promote();
880 if (controller != nullptr) {
881 InputReaderConfiguration config;
882 mPolicy->getReaderConfiguration(&config);
883 auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
884 if (viewport) {
885 controller->setDisplayViewport(*viewport);
886 }
887 }
888 }
889
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890private:
891 virtual void updateGlobalMetaState() {
892 mUpdateGlobalMetaStateWasCalled = true;
893 }
894
895 virtual int32_t getGlobalMetaState() {
896 return mGlobalMetaState;
897 }
898
899 virtual EventHubInterface* getEventHub() {
900 return mEventHub.get();
901 }
902
903 virtual InputReaderPolicyInterface* getPolicy() {
904 return mPolicy.get();
905 }
906
907 virtual InputListenerInterface* getListener() {
908 return mListener.get();
909 }
910
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100911 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 }
913
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800914 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800916 virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) {
917 sp<PointerControllerInterface> controller = mPointerController.promote();
918 if (controller == nullptr) {
919 controller = mPolicy->obtainPointerController(deviceId);
920 mPointerController = controller;
921 updatePointerDisplay();
922 }
923 return controller;
924 }
925
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 virtual void fadePointer() {
927 }
928
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100929 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 }
931
932 virtual int32_t bumpGeneration() {
933 return ++mGeneration;
934 }
Michael Wright842500e2015-03-13 17:32:02 -0700935
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800936 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700937
938 }
939
940 virtual void dispatchExternalStylusState(const StylusState&) {
941
942 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800943
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800944 virtual int32_t getNextId() { return mNextId++; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945};
946
947
948// --- FakeInputMapper ---
949
950class FakeInputMapper : public InputMapper {
951 uint32_t mSources;
952 int32_t mKeyboardType;
953 int32_t mMetaState;
954 KeyedVector<int32_t, int32_t> mKeyCodeStates;
955 KeyedVector<int32_t, int32_t> mScanCodeStates;
956 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800957 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700959 std::mutex mLock;
960 std::condition_variable mStateChangedCondition;
961 bool mConfigureWasCalled GUARDED_BY(mLock);
962 bool mResetWasCalled GUARDED_BY(mLock);
963 bool mProcessWasCalled GUARDED_BY(mLock);
964 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
Arthur Hungc23540e2018-11-29 20:42:11 +0800966 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800968 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
969 : InputMapper(deviceContext),
970 mSources(sources),
971 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800973 mConfigureWasCalled(false),
974 mResetWasCalled(false),
975 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976
977 virtual ~FakeInputMapper() { }
978
979 void setKeyboardType(int32_t keyboardType) {
980 mKeyboardType = keyboardType;
981 }
982
983 void setMetaState(int32_t metaState) {
984 mMetaState = metaState;
985 }
986
987 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700988 std::unique_lock<std::mutex> lock(mLock);
989 base::ScopedLockAssertion assumeLocked(mLock);
990 const bool configureCalled =
991 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
992 return mConfigureWasCalled;
993 });
994 if (!configureCalled) {
995 FAIL() << "Expected configure() to have been called.";
996 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 mConfigureWasCalled = false;
998 }
999
1000 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001001 std::unique_lock<std::mutex> lock(mLock);
1002 base::ScopedLockAssertion assumeLocked(mLock);
1003 const bool resetCalled =
1004 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1005 return mResetWasCalled;
1006 });
1007 if (!resetCalled) {
1008 FAIL() << "Expected reset() to have been called.";
1009 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010 mResetWasCalled = false;
1011 }
1012
Yi Kong9b14ac62018-07-17 13:48:38 -07001013 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001014 std::unique_lock<std::mutex> lock(mLock);
1015 base::ScopedLockAssertion assumeLocked(mLock);
1016 const bool processCalled =
1017 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1018 return mProcessWasCalled;
1019 });
1020 if (!processCalled) {
1021 FAIL() << "Expected process() to have been called.";
1022 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 if (outLastEvent) {
1024 *outLastEvent = mLastEvent;
1025 }
1026 mProcessWasCalled = false;
1027 }
1028
1029 void setKeyCodeState(int32_t keyCode, int32_t state) {
1030 mKeyCodeStates.replaceValueFor(keyCode, state);
1031 }
1032
1033 void setScanCodeState(int32_t scanCode, int32_t state) {
1034 mScanCodeStates.replaceValueFor(scanCode, state);
1035 }
1036
1037 void setSwitchState(int32_t switchCode, int32_t state) {
1038 mSwitchStates.replaceValueFor(switchCode, state);
1039 }
1040
1041 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001042 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 }
1044
1045private:
1046 virtual uint32_t getSources() {
1047 return mSources;
1048 }
1049
1050 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1051 InputMapper::populateDeviceInfo(deviceInfo);
1052
1053 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1054 deviceInfo->setKeyboardType(mKeyboardType);
1055 }
1056 }
1057
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001059 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001060 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001061
1062 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001063 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001064 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1065 mViewport = config->getDisplayViewportByPort(*displayPort);
1066 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001067
1068 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069 }
1070
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001071 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001072 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001074 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075 }
1076
1077 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001078 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079 mLastEvent = *rawEvent;
1080 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001081 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 }
1083
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001084 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1086 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1087 }
1088
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001089 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1091 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1092 }
1093
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001094 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1096 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1097 }
1098
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001099 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 const int32_t* keyCodes, uint8_t* outFlags) {
1101 bool result = false;
1102 for (size_t i = 0; i < numCodes; i++) {
1103 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1104 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1105 outFlags[i] = 1;
1106 result = true;
1107 }
1108 }
1109 }
1110 return result;
1111 }
1112
1113 virtual int32_t getMetaState() {
1114 return mMetaState;
1115 }
1116
1117 virtual void fadePointer() {
1118 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001119
1120 virtual std::optional<int32_t> getAssociatedDisplay() {
1121 if (mViewport) {
1122 return std::make_optional(mViewport->displayId);
1123 }
1124 return std::nullopt;
1125 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126};
1127
1128
1129// --- InstrumentedInputReader ---
1130
1131class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001132 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133
1134public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001135 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1136 const sp<InputReaderPolicyInterface>& policy,
1137 const sp<InputListenerInterface>& listener)
1138 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001139
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001140 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001142 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001144 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001145 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 InputDeviceIdentifier identifier;
1147 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001148 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001150 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 }
1152
Prabir Pradhan28efc192019-11-05 01:10:04 +00001153 // Make the protected loopOnce method accessible to tests.
1154 using InputReader::loopOnce;
1155
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001157 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1158 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001160 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001161 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 return device;
1163 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001164 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165 }
1166
1167 friend class InputReaderTest;
1168};
1169
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001170// --- InputReaderPolicyTest ---
1171class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001172protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173 sp<FakeInputReaderPolicy> mFakePolicy;
1174
Prabir Pradhan28efc192019-11-05 01:10:04 +00001175 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1176 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177};
1178
1179/**
1180 * Check that empty set of viewports is an acceptable configuration.
1181 * Also try to get internal viewport two different ways - by type and by uniqueId.
1182 *
1183 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1184 * Such configuration is not currently allowed.
1185 */
1186TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001187 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001188
1189 // We didn't add any viewports yet, so there shouldn't be any.
1190 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192 ASSERT_FALSE(internalViewport);
1193
1194 // Add an internal viewport, then clear it
1195 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001196 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001197
1198 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001199 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001200 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001201 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202
1203 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001204 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001205 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001206 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207
1208 mFakePolicy->clearViewports();
1209 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001210 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001211 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001212 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 ASSERT_FALSE(internalViewport);
1214}
1215
1216TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1217 const std::string internalUniqueId = "local:0";
1218 const std::string externalUniqueId = "local:1";
1219 const std::string virtualUniqueId1 = "virtual:2";
1220 const std::string virtualUniqueId2 = "virtual:3";
1221 constexpr int32_t virtualDisplayId1 = 2;
1222 constexpr int32_t virtualDisplayId2 = 3;
1223
1224 // Add an internal viewport
1225 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001226 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001227 // Add an external viewport
1228 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001229 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001230 // Add an virtual viewport
1231 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001232 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001233 // Add another virtual viewport
1234 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001235 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001236
1237 // Check matching by type for internal
1238 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001239 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 ASSERT_TRUE(internalViewport);
1241 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1242
1243 // Check matching by type for external
1244 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001245 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246 ASSERT_TRUE(externalViewport);
1247 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1248
1249 // Check matching by uniqueId for virtual viewport #1
1250 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001251 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001253 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1255 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1256
1257 // Check matching by uniqueId for virtual viewport #2
1258 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001259 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001260 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001261 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001262 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1263 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1264}
1265
1266
1267/**
1268 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1269 * that lookup works by checking display id.
1270 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1271 */
1272TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1273 const std::string uniqueId1 = "uniqueId1";
1274 const std::string uniqueId2 = "uniqueId2";
1275 constexpr int32_t displayId1 = 2;
1276 constexpr int32_t displayId2 = 3;
1277
1278 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1279 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1280 for (const ViewportType& type : types) {
1281 mFakePolicy->clearViewports();
1282 // Add a viewport
1283 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001284 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285 // Add another viewport
1286 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001287 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001288
1289 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001290 std::optional<DisplayViewport> viewport1 =
1291 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001292 ASSERT_TRUE(viewport1);
1293 ASSERT_EQ(displayId1, viewport1->displayId);
1294 ASSERT_EQ(type, viewport1->type);
1295
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001296 std::optional<DisplayViewport> viewport2 =
1297 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298 ASSERT_TRUE(viewport2);
1299 ASSERT_EQ(displayId2, viewport2->displayId);
1300 ASSERT_EQ(type, viewport2->type);
1301
1302 // When there are multiple viewports of the same kind, and uniqueId is not specified
1303 // in the call to getDisplayViewport, then that situation is not supported.
1304 // The viewports can be stored in any order, so we cannot rely on the order, since that
1305 // is just implementation detail.
1306 // However, we can check that it still returns *a* viewport, we just cannot assert
1307 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001308 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001309 ASSERT_TRUE(someViewport);
1310 }
1311}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001313/**
1314 * Check getDisplayViewportByPort
1315 */
1316TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1317 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1318 const std::string uniqueId1 = "uniqueId1";
1319 const std::string uniqueId2 = "uniqueId2";
1320 constexpr int32_t displayId1 = 1;
1321 constexpr int32_t displayId2 = 2;
1322 const uint8_t hdmi1 = 0;
1323 const uint8_t hdmi2 = 1;
1324 const uint8_t hdmi3 = 2;
1325
1326 mFakePolicy->clearViewports();
1327 // Add a viewport that's associated with some display port that's not of interest.
1328 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1329 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1330 // Add another viewport, connected to HDMI1 port
1331 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1332 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1333
1334 // Check that correct display viewport was returned by comparing the display ports.
1335 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1336 ASSERT_TRUE(hdmi1Viewport);
1337 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1338 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1339
1340 // Check that we can still get the same viewport using the uniqueId
1341 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1342 ASSERT_TRUE(hdmi1Viewport);
1343 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1344 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1345 ASSERT_EQ(type, hdmi1Viewport->type);
1346
1347 // Check that we cannot find a port with "HDMI2", because we never added one
1348 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1349 ASSERT_FALSE(hdmi2Viewport);
1350}
1351
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352// --- InputReaderTest ---
1353
1354class InputReaderTest : public testing::Test {
1355protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001356 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001357 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001358 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001359 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001360
Prabir Pradhan28efc192019-11-05 01:10:04 +00001361 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001362 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001363 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001364 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365
Prabir Pradhan28efc192019-11-05 01:10:04 +00001366 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1367 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001368 }
1369
Prabir Pradhan28efc192019-11-05 01:10:04 +00001370 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001371 mFakeListener.clear();
1372 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373 }
1374
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001375 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1376 const PropertyMap* configuration) {
1377 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378
1379 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001380 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 }
1382 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001383 mReader->loopOnce();
1384 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001385 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1386 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 }
1388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001389 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001390 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001391 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001392 }
1393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001394 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001395 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001396 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001397 }
1398
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001399 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001400 const std::string& name, uint32_t classes,
1401 uint32_t sources,
1402 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001403 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1404 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001405 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001406 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001407 return mapper;
1408 }
1409};
1410
1411TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001412 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001413 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001414 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001415 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001417 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001419 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001420 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001421 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001422 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1423 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1424 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1425
1426 // Should also have received a notification describing the new input devices.
1427 inputDevices = mFakePolicy->getInputDevices();
1428 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001429 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001430 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001431 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1432 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1433 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1434}
1435
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001436TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001437 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001438 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001439 constexpr int32_t eventHubId = 1;
1440 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001441 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001442 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001443 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001444 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001445
Yi Kong9b14ac62018-07-17 13:48:38 -07001446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001447
1448 NotifyDeviceResetArgs resetArgs;
1449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001450 ASSERT_EQ(deviceId, resetArgs.deviceId);
1451
1452 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001453 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001454 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001455
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001457 ASSERT_EQ(deviceId, resetArgs.deviceId);
1458 ASSERT_EQ(device->isEnabled(), false);
1459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001460 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001461 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001464 ASSERT_EQ(device->isEnabled(), false);
1465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001466 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001467 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001469 ASSERT_EQ(deviceId, resetArgs.deviceId);
1470 ASSERT_EQ(device->isEnabled(), true);
1471}
1472
Michael Wrightd02c5b62014-02-10 15:10:22 -08001473TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001474 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1475 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1476 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001477 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001478 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001479 AINPUT_SOURCE_KEYBOARD, nullptr);
1480 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481
1482 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1483 AINPUT_SOURCE_ANY, AKEYCODE_A))
1484 << "Should return unknown when the device id is >= 0 but unknown.";
1485
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001486 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1487 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1488 << "Should return unknown when the device id is valid but the sources are not "
1489 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001490
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001491 ASSERT_EQ(AKEY_STATE_DOWN,
1492 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1493 AKEYCODE_A))
1494 << "Should return value provided by mapper when device id is valid and the device "
1495 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001496
1497 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1498 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1499 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1500
1501 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1502 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1503 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1504}
1505
1506TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001507 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1508 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1509 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001510 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001511 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001512 AINPUT_SOURCE_KEYBOARD, nullptr);
1513 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514
1515 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1516 AINPUT_SOURCE_ANY, KEY_A))
1517 << "Should return unknown when the device id is >= 0 but unknown.";
1518
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1520 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1521 << "Should return unknown when the device id is valid but the sources are not "
1522 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001523
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 ASSERT_EQ(AKEY_STATE_DOWN,
1525 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1526 KEY_A))
1527 << "Should return value provided by mapper when device id is valid and the device "
1528 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529
1530 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1531 AINPUT_SOURCE_TRACKBALL, KEY_A))
1532 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1533
1534 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1535 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1536 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1537}
1538
1539TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001540 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1541 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1542 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001543 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001544 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001545 AINPUT_SOURCE_KEYBOARD, nullptr);
1546 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547
1548 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1549 AINPUT_SOURCE_ANY, SW_LID))
1550 << "Should return unknown when the device id is >= 0 but unknown.";
1551
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1553 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1554 << "Should return unknown when the device id is valid but the sources are not "
1555 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001557 ASSERT_EQ(AKEY_STATE_DOWN,
1558 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1559 SW_LID))
1560 << "Should return value provided by mapper when device id is valid and the device "
1561 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562
1563 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1564 AINPUT_SOURCE_TRACKBALL, SW_LID))
1565 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1566
1567 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1568 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1569 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1570}
1571
1572TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001573 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1574 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1575 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001576 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001577 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001578 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001580 mapper.addSupportedKeyCode(AKEYCODE_A);
1581 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582
1583 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1584 uint8_t flags[4] = { 0, 0, 0, 1 };
1585
1586 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1587 << "Should return false when device id is >= 0 but unknown.";
1588 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1589
1590 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1592 << "Should return false when device id is valid but the sources are not supported by "
1593 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1595
1596 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001597 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1598 keyCodes, flags))
1599 << "Should return value provided by mapper when device id is valid and the device "
1600 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1602
1603 flags[3] = 1;
1604 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1605 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1606 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1607
1608 flags[3] = 1;
1609 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1610 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1611 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1612}
1613
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001614TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 constexpr int32_t eventHubId = 1;
1616 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617
1618 NotifyConfigurationChangedArgs args;
1619
1620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1621 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1622}
1623
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001624TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1626 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1627 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001628 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001630 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001633 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1635
1636 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001637 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001638 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 ASSERT_EQ(EV_KEY, event.type);
1641 ASSERT_EQ(KEY_A, event.code);
1642 ASSERT_EQ(1, event.value);
1643}
1644
Garfield Tan1c7bc862020-01-28 13:24:04 -08001645TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001647 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 constexpr int32_t eventHubId = 1;
1649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001650 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001651 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001652 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001654
1655 NotifyDeviceResetArgs resetArgs;
1656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001657 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001658
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001659 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001660 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001662 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001663 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001664
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001665 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001666 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001668 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001669 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001670
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001671 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001672 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001674 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001675 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001676}
1677
Garfield Tan1c7bc862020-01-28 13:24:04 -08001678TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1679 constexpr int32_t deviceId = 1;
1680 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1681 constexpr int32_t eventHubId = 1;
1682 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1683 // Must add at least one mapper or the device will be ignored!
1684 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1685 mReader->setNextDevice(device);
1686 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1687
1688 NotifyDeviceResetArgs resetArgs;
1689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1690 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1691}
1692
Arthur Hungc23540e2018-11-29 20:42:11 +08001693TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001694 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001695 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001697 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001698 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1699 FakeInputMapper& mapper =
1700 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001701 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001702
1703 const uint8_t hdmi1 = 1;
1704
1705 // Associated touch screen with second display.
1706 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1707
1708 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001709 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001710 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1711 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1712 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1713 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1714 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001715 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001716
1717 // Add the device, and make sure all of the callbacks are triggered.
1718 // The device is added after the input port associations are processed since
1719 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001724
Arthur Hung2c9a3342019-07-23 14:18:59 +08001725 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001726 ASSERT_EQ(deviceId, device->getId());
1727 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1728 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001729
1730 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001731 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001732 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001733 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001734}
1735
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001736// --- InputReaderIntegrationTest ---
1737
1738// These tests create and interact with the InputReader only through its interface.
1739// The InputReader is started during SetUp(), which starts its processing in its own
1740// thread. The tests use linux uinput to emulate input devices.
1741// NOTE: Interacting with the physical device while these tests are running may cause
1742// the tests to fail.
1743class InputReaderIntegrationTest : public testing::Test {
1744protected:
1745 sp<TestInputListener> mTestListener;
1746 sp<FakeInputReaderPolicy> mFakePolicy;
1747 sp<InputReaderInterface> mReader;
1748
1749 virtual void SetUp() override {
1750 mFakePolicy = new FakeInputReaderPolicy();
1751 mTestListener = new TestInputListener();
1752
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001753 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001754 ASSERT_EQ(mReader->start(), OK);
1755
1756 // Since this test is run on a real device, all the input devices connected
1757 // to the test device will show up in mReader. We wait for those input devices to
1758 // show up before beginning the tests.
1759 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1760 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1761 }
1762
1763 virtual void TearDown() override {
1764 ASSERT_EQ(mReader->stop(), OK);
1765 mTestListener.clear();
1766 mFakePolicy.clear();
1767 }
1768};
1769
1770TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1771 // An invalid input device that is only used for this test.
1772 class InvalidUinputDevice : public UinputDevice {
1773 public:
1774 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1775
1776 private:
1777 void configureDevice(int fd, uinput_user_dev* device) override {}
1778 };
1779
1780 const size_t numDevices = mFakePolicy->getInputDevices().size();
1781
1782 // UinputDevice does not set any event or key bits, so InputReader should not
1783 // consider it as a valid device.
1784 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1785 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1786 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1787 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1788
1789 invalidDevice.reset();
1790 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1791 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1792 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1793}
1794
1795TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1796 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1797
1798 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1799 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1800 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1801 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1802
1803 // Find the test device by its name.
1804 std::vector<InputDeviceInfo> inputDevices;
1805 mReader->getInputDevices(inputDevices);
1806 InputDeviceInfo* keyboardInfo = nullptr;
1807 const char* keyboardName = keyboard->getName();
1808 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1809 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1810 keyboardInfo = &inputDevices[i];
1811 break;
1812 }
1813 }
1814 ASSERT_NE(keyboardInfo, nullptr);
1815 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1816 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1817 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1818
1819 keyboard.reset();
1820 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1821 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1822 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1823}
1824
1825TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1826 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1827 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1828
1829 NotifyConfigurationChangedArgs configChangedArgs;
1830 ASSERT_NO_FATAL_FAILURE(
1831 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001832 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001833 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1834
1835 NotifyKeyArgs keyArgs;
1836 keyboard->pressAndReleaseHomeKey();
1837 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1838 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001839 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001840 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001841 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1842 prevTimestamp = keyArgs.eventTime;
1843
1844 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1845 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001846 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001847 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1848}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849
1850// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851class InputDeviceTest : public testing::Test {
1852protected:
1853 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001854 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001855 static const int32_t DEVICE_ID;
1856 static const int32_t DEVICE_GENERATION;
1857 static const int32_t DEVICE_CONTROLLER_NUMBER;
1858 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001859 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001861 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001862 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001863 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001864 FakeInputReaderContext* mFakeContext;
1865
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001866 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001867
Prabir Pradhan28efc192019-11-05 01:10:04 +00001868 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001869 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001870 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001871 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001872 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001874 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001875 InputDeviceIdentifier identifier;
1876 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001877 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001878 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1879 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880 }
1881
Prabir Pradhan28efc192019-11-05 01:10:04 +00001882 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001883 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884 delete mFakeContext;
1885 mFakeListener.clear();
1886 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887 }
1888};
1889
1890const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001891const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001893const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1894const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1895const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1896 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001897const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898
1899TEST_F(InputDeviceTest, ImmutableProperties) {
1900 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001901 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001902 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001903}
1904
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001905TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
1906 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001907}
1908
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1910 // Configuration.
1911 InputReaderConfiguration config;
1912 mDevice->configure(ARBITRARY_TIME, &config, 0);
1913
1914 // Reset.
1915 mDevice->reset(ARBITRARY_TIME);
1916
1917 NotifyDeviceResetArgs resetArgs;
1918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1919 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1920 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1921
1922 // Metadata.
1923 ASSERT_TRUE(mDevice->isIgnored());
1924 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1925
1926 InputDeviceInfo info;
1927 mDevice->getDeviceInfo(&info);
1928 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001929 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1931 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1932
1933 // State queries.
1934 ASSERT_EQ(0, mDevice->getMetaState());
1935
1936 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1937 << "Ignored device should return unknown key code state.";
1938 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1939 << "Ignored device should return unknown scan code state.";
1940 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1941 << "Ignored device should return unknown switch state.";
1942
1943 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1944 uint8_t flags[2] = { 0, 1 };
1945 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1946 << "Ignored device should never mark any key codes.";
1947 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1948 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1949}
1950
1951TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1952 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001954
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001955 FakeInputMapper& mapper1 =
1956 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001957 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1958 mapper1.setMetaState(AMETA_ALT_ON);
1959 mapper1.addSupportedKeyCode(AKEYCODE_A);
1960 mapper1.addSupportedKeyCode(AKEYCODE_B);
1961 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1962 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1963 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
1964 mapper1.setScanCodeState(3, AKEY_STATE_UP);
1965 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001966
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001967 FakeInputMapper& mapper2 =
1968 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001969 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001970
1971 InputReaderConfiguration config;
1972 mDevice->configure(ARBITRARY_TIME, &config, 0);
1973
1974 String8 propertyValue;
1975 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1976 << "Device should have read configuration during configuration phase.";
1977 ASSERT_STREQ("value", propertyValue.string());
1978
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001979 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
1980 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001981
1982 // Reset
1983 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001984 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
1985 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001986
1987 NotifyDeviceResetArgs resetArgs;
1988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1989 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1990 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1991
1992 // Metadata.
1993 ASSERT_FALSE(mDevice->isIgnored());
1994 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1995
1996 InputDeviceInfo info;
1997 mDevice->getDeviceInfo(&info);
1998 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001999 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2001 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2002
2003 // State queries.
2004 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2005 << "Should query mappers and combine meta states.";
2006
2007 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2008 << "Should return unknown key code state when source not supported.";
2009 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2010 << "Should return unknown scan code state when source not supported.";
2011 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2012 << "Should return unknown switch state when source not supported.";
2013
2014 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2015 << "Should query mapper when source is supported.";
2016 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2017 << "Should query mapper when source is supported.";
2018 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2019 << "Should query mapper when source is supported.";
2020
2021 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2022 uint8_t flags[4] = { 0, 0, 0, 1 };
2023 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2024 << "Should do nothing when source is unsupported.";
2025 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2026 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2027 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2028 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2029
2030 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2031 << "Should query mapper when source is supported.";
2032 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2033 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2034 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2035 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2036
2037 // Event handling.
2038 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002039 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002040 mDevice->process(&event, 1);
2041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002042 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2043 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002044}
2045
Arthur Hung2c9a3342019-07-23 14:18:59 +08002046// A single input device is associated with a specific display. Check that:
2047// 1. Device is disabled if the viewport corresponding to the associated display is not found
2048// 2. Device is disabled when setEnabled API is called
2049TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002050 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002051
2052 // First Configuration.
2053 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2054
2055 // Device should be enabled by default.
2056 ASSERT_TRUE(mDevice->isEnabled());
2057
2058 // Prepare associated info.
2059 constexpr uint8_t hdmi = 1;
2060 const std::string UNIQUE_ID = "local:1";
2061
2062 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2063 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2064 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2065 // Device should be disabled because it is associated with a specific display via
2066 // input port <-> display port association, but the corresponding display is not found
2067 ASSERT_FALSE(mDevice->isEnabled());
2068
2069 // Prepare displays.
2070 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2071 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2072 ViewportType::VIEWPORT_INTERNAL);
2073 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2074 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2075 ASSERT_TRUE(mDevice->isEnabled());
2076
2077 // Device should be disabled after set disable.
2078 mFakePolicy->addDisabledDevice(mDevice->getId());
2079 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2080 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2081 ASSERT_FALSE(mDevice->isEnabled());
2082
2083 // Device should still be disabled even found the associated display.
2084 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2085 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2086 ASSERT_FALSE(mDevice->isEnabled());
2087}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002088
2089// --- InputMapperTest ---
2090
2091class InputMapperTest : public testing::Test {
2092protected:
2093 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002094 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002095 static const int32_t DEVICE_ID;
2096 static const int32_t DEVICE_GENERATION;
2097 static const int32_t DEVICE_CONTROLLER_NUMBER;
2098 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002099 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002101 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002102 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002103 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002104 FakeInputReaderContext* mFakeContext;
2105 InputDevice* mDevice;
2106
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002107 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002108 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002109 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002110 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002111 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2112 InputDeviceIdentifier identifier;
2113 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002114 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002115 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002116
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002117 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002118 }
2119
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002120 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2121
Prabir Pradhan28efc192019-11-05 01:10:04 +00002122 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002123 delete mDevice;
2124 delete mFakeContext;
2125 mFakeListener.clear();
2126 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002127 }
2128
2129 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002130 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002131 }
2132
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002133 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002134 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2135 mFakeContext->updatePointerDisplay();
2136 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002137 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2138 }
2139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002140 template <class T, typename... Args>
2141 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002142 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002143 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002144 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002145 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002146 }
2147
2148 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002149 int32_t orientation, const std::string& uniqueId,
2150 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002151 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002152 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002153 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2154 }
2155
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002156 void clearViewports() {
2157 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002158 }
2159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002160 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2161 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002162 RawEvent event;
2163 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002164 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002165 event.type = type;
2166 event.code = code;
2167 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002168 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002169 }
2170
2171 static void assertMotionRange(const InputDeviceInfo& info,
2172 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2173 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002174 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002175 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2176 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2177 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2178 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2179 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2180 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2181 }
2182
2183 static void assertPointerCoords(const PointerCoords& coords,
2184 float x, float y, float pressure, float size,
2185 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2186 float orientation, float distance) {
2187 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2188 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2189 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2190 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2191 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2192 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2193 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2194 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2195 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2196 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2197 }
2198
2199 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2200 float actualX, actualY;
2201 controller->getPosition(&actualX, &actualY);
2202 ASSERT_NEAR(x, actualX, 1);
2203 ASSERT_NEAR(y, actualY, 1);
2204 }
2205};
2206
2207const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002208const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002209const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2211const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2212const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002213const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002214
2215// --- SwitchInputMapperTest ---
2216
2217class SwitchInputMapperTest : public InputMapperTest {
2218protected:
2219};
2220
2221TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002222 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002224 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002225}
2226
2227TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002228 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002229
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002230 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002231 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002232
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002233 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002234 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235}
2236
2237TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002238 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002239
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002240 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2241 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2242 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2243 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002244
2245 NotifySwitchArgs args;
2246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2247 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002248 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2249 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002250 args.switchMask);
2251 ASSERT_EQ(uint32_t(0), args.policyFlags);
2252}
2253
2254
2255// --- KeyboardInputMapperTest ---
2256
2257class KeyboardInputMapperTest : public InputMapperTest {
2258protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002259 const std::string UNIQUE_ID = "local:0";
2260
2261 void prepareDisplay(int32_t orientation);
2262
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002263 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002264 int32_t originalKeyCode, int32_t rotatedKeyCode,
2265 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002266};
2267
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002268/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2269 * orientation.
2270 */
2271void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2272 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002273 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002274}
2275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002276void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002277 int32_t originalScanCode, int32_t originalKeyCode,
2278 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279 NotifyKeyArgs args;
2280
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002281 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2283 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2284 ASSERT_EQ(originalScanCode, args.scanCode);
2285 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002286 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002287
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002288 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2290 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2291 ASSERT_EQ(originalScanCode, args.scanCode);
2292 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002293 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002294}
2295
Michael Wrightd02c5b62014-02-10 15:10:22 -08002296TEST_F(KeyboardInputMapperTest, GetSources) {
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
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002301 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002302}
2303
2304TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2305 const int32_t USAGE_A = 0x070004;
2306 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002307 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2308 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002309
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002310 KeyboardInputMapper& mapper =
2311 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2312 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002313
2314 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002315 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002316 NotifyKeyArgs args;
2317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2318 ASSERT_EQ(DEVICE_ID, args.deviceId);
2319 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2320 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2321 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2322 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2323 ASSERT_EQ(KEY_HOME, args.scanCode);
2324 ASSERT_EQ(AMETA_NONE, args.metaState);
2325 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2326 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2327 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2328
2329 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002330 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2332 ASSERT_EQ(DEVICE_ID, args.deviceId);
2333 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2334 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2335 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2336 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2337 ASSERT_EQ(KEY_HOME, args.scanCode);
2338 ASSERT_EQ(AMETA_NONE, args.metaState);
2339 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2340 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2341 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2342
2343 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002344 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2345 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2347 ASSERT_EQ(DEVICE_ID, args.deviceId);
2348 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2349 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2351 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2352 ASSERT_EQ(0, args.scanCode);
2353 ASSERT_EQ(AMETA_NONE, args.metaState);
2354 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2355 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2356 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2357
2358 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002359 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2360 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2362 ASSERT_EQ(DEVICE_ID, args.deviceId);
2363 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2364 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2365 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2366 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2367 ASSERT_EQ(0, args.scanCode);
2368 ASSERT_EQ(AMETA_NONE, args.metaState);
2369 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2370 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2371 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2372
2373 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002374 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2375 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2377 ASSERT_EQ(DEVICE_ID, args.deviceId);
2378 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2379 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2380 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2381 ASSERT_EQ(0, args.keyCode);
2382 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2383 ASSERT_EQ(AMETA_NONE, args.metaState);
2384 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2385 ASSERT_EQ(0U, args.policyFlags);
2386 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2387
2388 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002389 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2390 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2392 ASSERT_EQ(DEVICE_ID, args.deviceId);
2393 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2394 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2395 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2396 ASSERT_EQ(0, args.keyCode);
2397 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2398 ASSERT_EQ(AMETA_NONE, args.metaState);
2399 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2400 ASSERT_EQ(0U, args.policyFlags);
2401 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2402}
2403
2404TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002405 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2406 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002408 KeyboardInputMapper& mapper =
2409 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2410 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411
2412 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002413 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414
2415 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002416 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417 NotifyKeyArgs args;
2418 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 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2422
2423 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002424 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2426 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002427 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428
2429 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002430 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2432 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002433 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434
2435 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002436 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2438 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002439 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002440 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2441}
2442
2443TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002444 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2445 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2446 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2447 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002449 KeyboardInputMapper& mapper =
2450 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2451 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002453 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2455 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2456 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2457 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2458 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2459 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2460 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2461 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2462}
2463
2464TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002465 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2466 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2467 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2468 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002471 KeyboardInputMapper& mapper =
2472 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2473 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002475 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002476 ASSERT_NO_FATAL_FAILURE(
2477 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2478 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2479 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2480 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2481 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2482 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2483 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002484
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002485 clearViewports();
2486 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002487 ASSERT_NO_FATAL_FAILURE(
2488 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2489 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2490 AKEYCODE_DPAD_UP, DISPLAY_ID));
2491 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2492 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2493 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2494 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002496 clearViewports();
2497 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002498 ASSERT_NO_FATAL_FAILURE(
2499 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2500 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2501 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2502 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2503 AKEYCODE_DPAD_UP, DISPLAY_ID));
2504 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2505 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002506
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002507 clearViewports();
2508 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002509 ASSERT_NO_FATAL_FAILURE(
2510 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2511 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2512 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2513 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2514 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2515 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2516 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002517
2518 // Special case: if orientation changes while key is down, we still emit the same keycode
2519 // in the key up as we did in the key down.
2520 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002521 clearViewports();
2522 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002523 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2525 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2526 ASSERT_EQ(KEY_UP, args.scanCode);
2527 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2528
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002529 clearViewports();
2530 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002531 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2533 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2534 ASSERT_EQ(KEY_UP, args.scanCode);
2535 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2536}
2537
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002538TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2539 // If the keyboard is not orientation aware,
2540 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002541 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002543 KeyboardInputMapper& mapper =
2544 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2545 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002546 NotifyKeyArgs args;
2547
2548 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002549 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002551 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2553 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2554
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002555 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002556 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002558 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2560 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2561}
2562
2563TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2564 // If the keyboard is orientation aware,
2565 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002566 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002567
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002568 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002569 KeyboardInputMapper& mapper =
2570 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2571 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002572 NotifyKeyArgs args;
2573
2574 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2575 // ^--- already checked by the previous test
2576
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002577 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002578 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002579 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002581 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2583 ASSERT_EQ(DISPLAY_ID, args.displayId);
2584
2585 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002586 clearViewports();
2587 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002588 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002589 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002591 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2593 ASSERT_EQ(newDisplayId, args.displayId);
2594}
2595
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002597 KeyboardInputMapper& mapper =
2598 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2599 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002601 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002602 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002604 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002605 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606}
2607
2608TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002609 KeyboardInputMapper& mapper =
2610 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2611 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002612
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002613 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002614 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002615
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002616 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002617 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618}
2619
2620TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002621 KeyboardInputMapper& mapper =
2622 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2623 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002625 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626
2627 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2628 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002629 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630 ASSERT_TRUE(flags[0]);
2631 ASSERT_FALSE(flags[1]);
2632}
2633
2634TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002635 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2636 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2637 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2638 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2639 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2640 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002642 KeyboardInputMapper& mapper =
2643 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2644 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645
2646 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002647 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2648 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2649 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650
2651 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002652 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2653 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002654 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2655 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2656 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002657 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658
2659 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002660 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2661 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002662 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2663 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2664 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002665 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666
2667 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002668 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2669 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002670 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2671 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2672 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002673 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674
2675 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002676 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2677 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002678 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2679 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2680 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002681 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682
2683 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002684 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2685 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002686 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2687 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2688 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002689 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690
2691 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002692 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2693 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002694 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2695 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2696 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002697 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698}
2699
Arthur Hung2c9a3342019-07-23 14:18:59 +08002700TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2701 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2705 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002706
2707 // keyboard 2.
2708 const std::string USB2 = "USB2";
2709 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002710 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002711 InputDeviceIdentifier identifier;
2712 identifier.name = "KEYBOARD2";
2713 identifier.location = USB2;
2714 std::unique_ptr<InputDevice> device2 =
2715 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002716 identifier);
2717 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2718 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2719 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2720 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2721 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002723 KeyboardInputMapper& mapper =
2724 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2725 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002726
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002727 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002728 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002729 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002730 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2731 device2->reset(ARBITRARY_TIME);
2732
2733 // Prepared displays and associated info.
2734 constexpr uint8_t hdmi1 = 0;
2735 constexpr uint8_t hdmi2 = 1;
2736 const std::string SECONDARY_UNIQUE_ID = "local:1";
2737
2738 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2739 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2740
2741 // No associated display viewport found, should disable the device.
2742 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2743 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2744 ASSERT_FALSE(device2->isEnabled());
2745
2746 // Prepare second display.
2747 constexpr int32_t newDisplayId = 2;
2748 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2749 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2750 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2751 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2752 // Default device will reconfigure above, need additional reconfiguration for another device.
2753 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2754 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2755
2756 // Device should be enabled after the associated display is found.
2757 ASSERT_TRUE(mDevice->isEnabled());
2758 ASSERT_TRUE(device2->isEnabled());
2759
2760 // Test pad key events
2761 ASSERT_NO_FATAL_FAILURE(
2762 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2763 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2764 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2765 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2766 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2767 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2768 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2769
2770 ASSERT_NO_FATAL_FAILURE(
2771 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2772 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2773 AKEYCODE_DPAD_RIGHT, newDisplayId));
2774 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2775 AKEYCODE_DPAD_DOWN, newDisplayId));
2776 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2777 AKEYCODE_DPAD_LEFT, newDisplayId));
2778}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002780// --- KeyboardInputMapperTest_ExternalDevice ---
2781
2782class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2783protected:
2784 virtual void SetUp() override {
2785 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2786 }
2787};
2788
2789TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002790 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2791 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002792
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2795 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2796 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002797
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002798 KeyboardInputMapper& mapper =
2799 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2800 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002801
2802 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2803 NotifyKeyArgs args;
2804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2805 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2806
2807 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2809 ASSERT_EQ(uint32_t(0), args.policyFlags);
2810
2811 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2813 ASSERT_EQ(uint32_t(0), args.policyFlags);
2814
2815 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2817 ASSERT_EQ(uint32_t(0), args.policyFlags);
2818
2819 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2821 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2822
2823 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2825 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2826}
2827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002828TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002829 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002831 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2832 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002834
Powei Fengd041c5d2019-05-03 17:11:33 -07002835 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002836 KeyboardInputMapper& mapper =
2837 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2838 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002839
2840 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2841 NotifyKeyArgs args;
2842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2843 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2844
2845 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2847 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2848
2849 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2851 ASSERT_EQ(uint32_t(0), args.policyFlags);
2852
2853 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2855 ASSERT_EQ(uint32_t(0), args.policyFlags);
2856
2857 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2859 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2860
2861 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2863 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2864}
2865
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866// --- CursorInputMapperTest ---
2867
2868class CursorInputMapperTest : public InputMapperTest {
2869protected:
2870 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2871
2872 sp<FakePointerController> mFakePointerController;
2873
Prabir Pradhan28efc192019-11-05 01:10:04 +00002874 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002875 InputMapperTest::SetUp();
2876
2877 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002878 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 }
2880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002881 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
2882 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002883
2884 void prepareDisplay(int32_t orientation) {
2885 const std::string uniqueId = "local:0";
2886 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2887 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2888 orientation, uniqueId, NO_PORT, viewportType);
2889 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002890};
2891
2892const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
2895 int32_t originalY, int32_t rotatedX,
2896 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897 NotifyMotionArgs args;
2898
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002899 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2900 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2901 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2905 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2906 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2907 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2908}
2909
2910TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002911 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002912 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002914 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915}
2916
2917TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002919 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002921 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922}
2923
2924TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002926 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
2928 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002929 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930
2931 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002932 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2933 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2935 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2936
2937 // When the bounds are set, then there should be a valid motion range.
2938 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2939
2940 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002941 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942
2943 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2944 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2945 1, 800 - 1, 0.0f, 0.0f));
2946 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2947 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2948 2, 480 - 1, 0.0f, 0.0f));
2949 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2950 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2951 0.0f, 1.0f, 0.0f, 0.0f));
2952}
2953
2954TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002956 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957
2958 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002959 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960
2961 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2962 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2963 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2964 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2965 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2966 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2967 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2968 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2969 0.0f, 1.0f, 0.0f, 0.0f));
2970}
2971
2972TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002974 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975
2976 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2977
2978 NotifyMotionArgs args;
2979
2980 // Button press.
2981 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002982 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2983 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2985 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2986 ASSERT_EQ(DEVICE_ID, args.deviceId);
2987 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2988 ASSERT_EQ(uint32_t(0), args.policyFlags);
2989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2990 ASSERT_EQ(0, args.flags);
2991 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2992 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2993 ASSERT_EQ(0, args.edgeFlags);
2994 ASSERT_EQ(uint32_t(1), args.pointerCount);
2995 ASSERT_EQ(0, args.pointerProperties[0].id);
2996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2998 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2999 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3000 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3001 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3002
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3004 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3005 ASSERT_EQ(DEVICE_ID, args.deviceId);
3006 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3007 ASSERT_EQ(uint32_t(0), args.policyFlags);
3008 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3009 ASSERT_EQ(0, args.flags);
3010 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3011 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3012 ASSERT_EQ(0, args.edgeFlags);
3013 ASSERT_EQ(uint32_t(1), args.pointerCount);
3014 ASSERT_EQ(0, args.pointerProperties[0].id);
3015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3017 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3018 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3019 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3020 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3021
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003023 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3024 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3026 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3027 ASSERT_EQ(DEVICE_ID, args.deviceId);
3028 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3029 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3031 ASSERT_EQ(0, args.flags);
3032 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3033 ASSERT_EQ(0, args.buttonState);
3034 ASSERT_EQ(0, args.edgeFlags);
3035 ASSERT_EQ(uint32_t(1), args.pointerCount);
3036 ASSERT_EQ(0, args.pointerProperties[0].id);
3037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3039 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3040 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3041 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3042 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3043
3044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3045 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3046 ASSERT_EQ(DEVICE_ID, args.deviceId);
3047 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3048 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3050 ASSERT_EQ(0, args.flags);
3051 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3052 ASSERT_EQ(0, args.buttonState);
3053 ASSERT_EQ(0, args.edgeFlags);
3054 ASSERT_EQ(uint32_t(1), args.pointerCount);
3055 ASSERT_EQ(0, args.pointerProperties[0].id);
3056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3058 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3059 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3060 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3061 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3062}
3063
3064TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003066 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067
3068 NotifyMotionArgs args;
3069
3070 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003071 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3072 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3076 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3077
3078 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003079 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3080 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3084 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3085}
3086
3087TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
3091 NotifyMotionArgs args;
3092
3093 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003094 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
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));
3097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3099 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3100
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3104 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3105
Michael Wrightd02c5b62014-02-10 15:10:22 -08003106 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003107 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3108 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003110 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3112 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3113
3114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3117 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3118}
3119
3120TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003122 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123
3124 NotifyMotionArgs args;
3125
3126 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003127 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3128 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3129 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3130 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3132 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3134 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3135 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3136
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3140 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3141 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3142
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003144 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3145 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3146 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3150 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3151 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3152
3153 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003154 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3155 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3159 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3160
3161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3164 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3165}
3166
3167TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003169 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003171 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3180}
3181
3182TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 addConfigurationProperty("cursor.mode", "navigation");
3184 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003185 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003186
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003187 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3196
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003197 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3206
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003207 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3216
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003217 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3222 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3223 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3224 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3225 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3226}
3227
3228TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003230 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231
3232 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3233 mFakePointerController->setPosition(100, 200);
3234 mFakePointerController->setButtonState(0);
3235
3236 NotifyMotionArgs motionArgs;
3237 NotifyKeyArgs keyArgs;
3238
3239 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003240 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3241 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3243 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3244 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3245 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3247 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3248
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3251 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3252 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3254 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3255
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003256 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3257 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 ASSERT_EQ(0, motionArgs.buttonState);
3261 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3263 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3264
3265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003266 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_EQ(0, motionArgs.buttonState);
3268 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3270 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3271
3272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003274 ASSERT_EQ(0, motionArgs.buttonState);
3275 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3277 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3278
3279 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003280 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3281 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3282 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3284 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3285 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3286 motionArgs.buttonState);
3287 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3288 mFakePointerController->getButtonState());
3289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3290 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3291
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3294 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3295 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3296 mFakePointerController->getButtonState());
3297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3298 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3299
3300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3302 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3303 motionArgs.buttonState);
3304 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3305 mFakePointerController->getButtonState());
3306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3307 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3308
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003309 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3310 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3314 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3316 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3317
3318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003320 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3321 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3323 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3324
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003325 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3326 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3329 ASSERT_EQ(0, motionArgs.buttonState);
3330 ASSERT_EQ(0, mFakePointerController->getButtonState());
3331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3332 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003333 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3334 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003335
3336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337 ASSERT_EQ(0, motionArgs.buttonState);
3338 ASSERT_EQ(0, mFakePointerController->getButtonState());
3339 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3341 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 -08003342
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3344 ASSERT_EQ(0, motionArgs.buttonState);
3345 ASSERT_EQ(0, mFakePointerController->getButtonState());
3346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3348 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3349
3350 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003351 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3352 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3354 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3355 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003356
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3360 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3362 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3363
3364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3366 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3367 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3369 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3370
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003371 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3372 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375 ASSERT_EQ(0, motionArgs.buttonState);
3376 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3378 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3379
3380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003382 ASSERT_EQ(0, motionArgs.buttonState);
3383 ASSERT_EQ(0, mFakePointerController->getButtonState());
3384
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3386 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3388 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3389 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3390
3391 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003392 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3393 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3395 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3396 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003397
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3401 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3403 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3404
3405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3407 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3408 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3410 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3411
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003412 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3413 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416 ASSERT_EQ(0, motionArgs.buttonState);
3417 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3419 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 -08003420
3421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3423 ASSERT_EQ(0, motionArgs.buttonState);
3424 ASSERT_EQ(0, mFakePointerController->getButtonState());
3425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3426 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3427
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3429 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3430 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3431
3432 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003433 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3434 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3436 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3437 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003438
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3444 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3445
3446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3448 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3449 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3451 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3452
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003453 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3454 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 ASSERT_EQ(0, motionArgs.buttonState);
3458 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3460 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 -08003461
3462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3464 ASSERT_EQ(0, motionArgs.buttonState);
3465 ASSERT_EQ(0, mFakePointerController->getButtonState());
3466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3467 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3468
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3471 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3472
3473 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003474 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3475 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3477 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3478 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003479
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3483 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3485 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3486
3487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3489 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3490 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3492 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3493
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003494 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3495 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 ASSERT_EQ(0, motionArgs.buttonState);
3499 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3501 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 -08003502
3503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3504 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3505 ASSERT_EQ(0, motionArgs.buttonState);
3506 ASSERT_EQ(0, mFakePointerController->getButtonState());
3507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3508 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3509
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3511 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3512 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3513}
3514
3515TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003517 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518
3519 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3520 mFakePointerController->setPosition(100, 200);
3521 mFakePointerController->setButtonState(0);
3522
3523 NotifyMotionArgs args;
3524
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003525 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3526 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3527 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003529 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3532 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3533 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3534}
3535
3536TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003537 addConfigurationProperty("cursor.mode", "pointer");
3538 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003539 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003540
3541 NotifyDeviceResetArgs resetArgs;
3542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3543 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3544 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3545
3546 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3547 mFakePointerController->setPosition(100, 200);
3548 mFakePointerController->setButtonState(0);
3549
3550 NotifyMotionArgs args;
3551
3552 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003553 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3554 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3555 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3557 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3560 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3561 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3562
3563 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003564 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3565 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3567 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3568 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3570 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3572 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3575 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3576
3577 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003578 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3579 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3581 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3582 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3584 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3586 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3587 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3589 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3590
3591 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003592 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3593 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3594 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3596 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3599 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3600 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3601
3602 // Disable pointer capture and check that the device generation got bumped
3603 // and events are generated the usual way.
3604 const uint32_t generation = mFakeContext->getGeneration();
3605 mFakePolicy->setPointerCapture(false);
3606 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3607 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3608
3609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3610 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3611 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3612
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003613 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3614 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3615 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3617 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3620 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3621 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3622}
3623
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003624TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003625 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003626
Garfield Tan888a6a42020-01-09 11:39:16 -08003627 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003628 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003629 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3630 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3631 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3632 ViewportType::VIEWPORT_EXTERNAL);
3633 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3634 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3635
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003636 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3637 mFakePointerController->setPosition(100, 200);
3638 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003639
3640 NotifyMotionArgs args;
3641 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3642 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3643 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3645 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3646 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3648 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3649 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3650 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3651}
3652
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653// --- TouchInputMapperTest ---
3654
3655class TouchInputMapperTest : public InputMapperTest {
3656protected:
3657 static const int32_t RAW_X_MIN;
3658 static const int32_t RAW_X_MAX;
3659 static const int32_t RAW_Y_MIN;
3660 static const int32_t RAW_Y_MAX;
3661 static const int32_t RAW_TOUCH_MIN;
3662 static const int32_t RAW_TOUCH_MAX;
3663 static const int32_t RAW_TOOL_MIN;
3664 static const int32_t RAW_TOOL_MAX;
3665 static const int32_t RAW_PRESSURE_MIN;
3666 static const int32_t RAW_PRESSURE_MAX;
3667 static const int32_t RAW_ORIENTATION_MIN;
3668 static const int32_t RAW_ORIENTATION_MAX;
3669 static const int32_t RAW_DISTANCE_MIN;
3670 static const int32_t RAW_DISTANCE_MAX;
3671 static const int32_t RAW_TILT_MIN;
3672 static const int32_t RAW_TILT_MAX;
3673 static const int32_t RAW_ID_MIN;
3674 static const int32_t RAW_ID_MAX;
3675 static const int32_t RAW_SLOT_MIN;
3676 static const int32_t RAW_SLOT_MAX;
3677 static const float X_PRECISION;
3678 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003679 static const float X_PRECISION_VIRTUAL;
3680 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681
3682 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003683 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684
3685 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3686
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003687 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003688 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003689
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690 enum Axes {
3691 POSITION = 1 << 0,
3692 TOUCH = 1 << 1,
3693 TOOL = 1 << 2,
3694 PRESSURE = 1 << 3,
3695 ORIENTATION = 1 << 4,
3696 MINOR = 1 << 5,
3697 ID = 1 << 6,
3698 DISTANCE = 1 << 7,
3699 TILT = 1 << 8,
3700 SLOT = 1 << 9,
3701 TOOL_TYPE = 1 << 10,
3702 };
3703
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003704 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3705 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003706 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003708 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709 int32_t toRawX(float displayX);
3710 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003711 float toCookedX(float rawX, float rawY);
3712 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003714 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003716 float toDisplayY(int32_t rawY, int32_t displayHeight);
3717
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718};
3719
3720const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3721const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3722const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3723const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3724const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3725const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3726const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3727const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003728const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3729const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3731const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3732const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3733const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3734const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3735const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3736const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3737const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3738const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3739const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3740const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3741const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003742const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3743 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3744const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3745 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003746const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3747 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748
3749const float TouchInputMapperTest::GEOMETRIC_SCALE =
3750 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3751 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3752
3753const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3754 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3755 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3756};
3757
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003758void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003759 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003760 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3761}
3762
3763void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3764 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3765 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766}
3767
Santos Cordonfa5cf462017-04-05 10:37:00 -07003768void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003769 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3770 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003771 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003772}
3773
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003775 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3776 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3777 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3778 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779}
3780
Jason Gerecke489fda82012-09-07 17:19:40 -07003781void TouchInputMapperTest::prepareLocationCalibration() {
3782 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3783}
3784
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785int32_t TouchInputMapperTest::toRawX(float displayX) {
3786 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3787}
3788
3789int32_t TouchInputMapperTest::toRawY(float displayY) {
3790 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3791}
3792
Jason Gerecke489fda82012-09-07 17:19:40 -07003793float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3794 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3795 return rawX;
3796}
3797
3798float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3799 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3800 return rawY;
3801}
3802
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003804 return toDisplayX(rawX, DISPLAY_WIDTH);
3805}
3806
3807float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3808 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809}
3810
3811float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003812 return toDisplayY(rawY, DISPLAY_HEIGHT);
3813}
3814
3815float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3816 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817}
3818
3819
3820// --- SingleTouchInputMapperTest ---
3821
3822class SingleTouchInputMapperTest : public TouchInputMapperTest {
3823protected:
3824 void prepareButtons();
3825 void prepareAxes(int axes);
3826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003827 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3828 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3829 void processUp(SingleTouchInputMapper& mappery);
3830 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3831 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3832 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3833 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3834 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3835 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836};
3837
3838void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003839 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840}
3841
3842void SingleTouchInputMapperTest::prepareAxes(int axes) {
3843 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003844 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3845 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846 }
3847 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003848 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
3849 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850 }
3851 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003852 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
3853 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854 }
3855 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003856 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
3857 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 }
3859 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003860 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3861 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862 }
3863}
3864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003866 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3867 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3868 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869}
3870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003872 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3873 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874}
3875
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003877 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878}
3879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003881 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882}
3883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003884void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
3885 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003886 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887}
3888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003889void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003890 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891}
3892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003893void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
3894 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003895 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3896 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897}
3898
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003899void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
3900 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003901 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902}
3903
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003904void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003905 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906}
3907
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909 prepareButtons();
3910 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003911 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003913 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914}
3915
3916TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003917 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
3918 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919 prepareButtons();
3920 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003921 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003923 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924}
3925
3926TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 prepareButtons();
3928 prepareAxes(POSITION);
3929 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003930 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003932 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933}
3934
3935TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936 prepareButtons();
3937 prepareAxes(POSITION);
3938 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003939 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003941 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942}
3943
3944TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 addConfigurationProperty("touch.deviceType", "touchScreen");
3946 prepareDisplay(DISPLAY_ORIENTATION_0);
3947 prepareButtons();
3948 prepareAxes(POSITION);
3949 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003950 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951
3952 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003953 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954
3955 // Virtual key is down.
3956 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3957 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3958 processDown(mapper, x, y);
3959 processSync(mapper);
3960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003962 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003963
3964 // Virtual key is up.
3965 processUp(mapper);
3966 processSync(mapper);
3967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003969 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003970}
3971
3972TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973 addConfigurationProperty("touch.deviceType", "touchScreen");
3974 prepareDisplay(DISPLAY_ORIENTATION_0);
3975 prepareButtons();
3976 prepareAxes(POSITION);
3977 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003978 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979
3980 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003981 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982
3983 // Virtual key is down.
3984 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3985 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3986 processDown(mapper, x, y);
3987 processSync(mapper);
3988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003990 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991
3992 // Virtual key is up.
3993 processUp(mapper);
3994 processSync(mapper);
3995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998}
3999
4000TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 addConfigurationProperty("touch.deviceType", "touchScreen");
4002 prepareDisplay(DISPLAY_ORIENTATION_0);
4003 prepareButtons();
4004 prepareAxes(POSITION);
4005 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004006 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007
4008 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4009 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004010 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 ASSERT_TRUE(flags[0]);
4012 ASSERT_FALSE(flags[1]);
4013}
4014
4015TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 addConfigurationProperty("touch.deviceType", "touchScreen");
4017 prepareDisplay(DISPLAY_ORIENTATION_0);
4018 prepareButtons();
4019 prepareAxes(POSITION);
4020 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004021 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022
4023 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4024
4025 NotifyKeyArgs args;
4026
4027 // Press virtual key.
4028 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4029 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4030 processDown(mapper, x, y);
4031 processSync(mapper);
4032
4033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4034 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4035 ASSERT_EQ(DEVICE_ID, args.deviceId);
4036 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4037 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4038 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4039 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4040 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4041 ASSERT_EQ(KEY_HOME, args.scanCode);
4042 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4043 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4044
4045 // Release virtual key.
4046 processUp(mapper);
4047 processSync(mapper);
4048
4049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4050 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4051 ASSERT_EQ(DEVICE_ID, args.deviceId);
4052 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4053 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4054 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4055 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4056 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4057 ASSERT_EQ(KEY_HOME, args.scanCode);
4058 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4059 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4060
4061 // Should not have sent any motions.
4062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4063}
4064
4065TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 addConfigurationProperty("touch.deviceType", "touchScreen");
4067 prepareDisplay(DISPLAY_ORIENTATION_0);
4068 prepareButtons();
4069 prepareAxes(POSITION);
4070 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004071 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072
4073 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4074
4075 NotifyKeyArgs keyArgs;
4076
4077 // Press virtual key.
4078 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4079 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4080 processDown(mapper, x, y);
4081 processSync(mapper);
4082
4083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4084 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4085 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4086 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4087 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4088 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4089 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4090 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4091 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4092 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4093 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4094
4095 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4096 // into the display area.
4097 y -= 100;
4098 processMove(mapper, x, y);
4099 processSync(mapper);
4100
4101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4102 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4103 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4104 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4105 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4106 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4107 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4108 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4109 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4110 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4111 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4112 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4113
4114 NotifyMotionArgs motionArgs;
4115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4116 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4117 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4118 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4119 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4120 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4121 ASSERT_EQ(0, motionArgs.flags);
4122 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4123 ASSERT_EQ(0, motionArgs.buttonState);
4124 ASSERT_EQ(0, motionArgs.edgeFlags);
4125 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4126 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4129 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4130 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4131 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4132 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4133
4134 // Keep moving out of bounds. Should generate a pointer move.
4135 y -= 50;
4136 processMove(mapper, x, y);
4137 processSync(mapper);
4138
4139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4140 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4141 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4142 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4143 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4145 ASSERT_EQ(0, motionArgs.flags);
4146 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4147 ASSERT_EQ(0, motionArgs.buttonState);
4148 ASSERT_EQ(0, motionArgs.edgeFlags);
4149 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4150 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4153 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4154 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4155 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4157
4158 // Release out of bounds. Should generate a pointer up.
4159 processUp(mapper);
4160 processSync(mapper);
4161
4162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4163 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4164 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4165 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4166 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4167 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4168 ASSERT_EQ(0, motionArgs.flags);
4169 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4170 ASSERT_EQ(0, motionArgs.buttonState);
4171 ASSERT_EQ(0, motionArgs.edgeFlags);
4172 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4173 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4176 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4177 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4178 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4179 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4180
4181 // Should not have sent any more keys or motions.
4182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4184}
4185
4186TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 addConfigurationProperty("touch.deviceType", "touchScreen");
4188 prepareDisplay(DISPLAY_ORIENTATION_0);
4189 prepareButtons();
4190 prepareAxes(POSITION);
4191 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004192 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193
4194 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4195
4196 NotifyMotionArgs motionArgs;
4197
4198 // Initially go down out of bounds.
4199 int32_t x = -10;
4200 int32_t y = -10;
4201 processDown(mapper, x, y);
4202 processSync(mapper);
4203
4204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4205
4206 // Move into the display area. Should generate a pointer down.
4207 x = 50;
4208 y = 75;
4209 processMove(mapper, x, y);
4210 processSync(mapper);
4211
4212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4214 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4215 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4216 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4217 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4218 ASSERT_EQ(0, motionArgs.flags);
4219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4220 ASSERT_EQ(0, motionArgs.buttonState);
4221 ASSERT_EQ(0, motionArgs.edgeFlags);
4222 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4223 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4226 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4227 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4228 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4229 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4230
4231 // Release. Should generate a pointer up.
4232 processUp(mapper);
4233 processSync(mapper);
4234
4235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4237 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4238 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4239 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4240 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4241 ASSERT_EQ(0, motionArgs.flags);
4242 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4243 ASSERT_EQ(0, motionArgs.buttonState);
4244 ASSERT_EQ(0, motionArgs.edgeFlags);
4245 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4246 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4249 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4250 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4251 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4253
4254 // Should not have sent any more keys or motions.
4255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4257}
4258
Santos Cordonfa5cf462017-04-05 10:37:00 -07004259TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004260 addConfigurationProperty("touch.deviceType", "touchScreen");
4261 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4262
4263 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4264 prepareButtons();
4265 prepareAxes(POSITION);
4266 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004267 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004268
4269 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4270
4271 NotifyMotionArgs motionArgs;
4272
4273 // Down.
4274 int32_t x = 100;
4275 int32_t y = 125;
4276 processDown(mapper, x, y);
4277 processSync(mapper);
4278
4279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4280 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4281 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4282 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4283 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4284 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4286 ASSERT_EQ(0, motionArgs.flags);
4287 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4288 ASSERT_EQ(0, motionArgs.buttonState);
4289 ASSERT_EQ(0, motionArgs.edgeFlags);
4290 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4291 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4294 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4295 1, 0, 0, 0, 0, 0, 0, 0));
4296 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4297 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4298 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4299
4300 // Move.
4301 x += 50;
4302 y += 75;
4303 processMove(mapper, x, y);
4304 processSync(mapper);
4305
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4307 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4308 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4309 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4310 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4311 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4313 ASSERT_EQ(0, motionArgs.flags);
4314 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4315 ASSERT_EQ(0, motionArgs.buttonState);
4316 ASSERT_EQ(0, motionArgs.edgeFlags);
4317 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4318 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4321 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4322 1, 0, 0, 0, 0, 0, 0, 0));
4323 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4324 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4326
4327 // Up.
4328 processUp(mapper);
4329 processSync(mapper);
4330
4331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4333 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4334 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4335 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4336 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4337 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4338 ASSERT_EQ(0, motionArgs.flags);
4339 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4340 ASSERT_EQ(0, motionArgs.buttonState);
4341 ASSERT_EQ(0, motionArgs.edgeFlags);
4342 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4343 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4346 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4347 1, 0, 0, 0, 0, 0, 0, 0));
4348 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4349 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4351
4352 // Should not have sent any more keys or motions.
4353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4355}
4356
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 addConfigurationProperty("touch.deviceType", "touchScreen");
4359 prepareDisplay(DISPLAY_ORIENTATION_0);
4360 prepareButtons();
4361 prepareAxes(POSITION);
4362 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004363 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364
4365 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4366
4367 NotifyMotionArgs motionArgs;
4368
4369 // Down.
4370 int32_t x = 100;
4371 int32_t y = 125;
4372 processDown(mapper, x, y);
4373 processSync(mapper);
4374
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4377 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4378 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4379 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4380 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4381 ASSERT_EQ(0, motionArgs.flags);
4382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4383 ASSERT_EQ(0, motionArgs.buttonState);
4384 ASSERT_EQ(0, motionArgs.edgeFlags);
4385 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4389 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4390 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4391 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4392 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4393
4394 // Move.
4395 x += 50;
4396 y += 75;
4397 processMove(mapper, x, y);
4398 processSync(mapper);
4399
4400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4402 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4403 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4404 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4406 ASSERT_EQ(0, motionArgs.flags);
4407 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4408 ASSERT_EQ(0, motionArgs.buttonState);
4409 ASSERT_EQ(0, motionArgs.edgeFlags);
4410 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4411 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4414 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4415 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4416 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4418
4419 // Up.
4420 processUp(mapper);
4421 processSync(mapper);
4422
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4424 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4425 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4426 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4427 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4429 ASSERT_EQ(0, motionArgs.flags);
4430 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4431 ASSERT_EQ(0, motionArgs.buttonState);
4432 ASSERT_EQ(0, motionArgs.edgeFlags);
4433 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4434 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4437 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4438 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4439 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4441
4442 // Should not have sent any more keys or motions.
4443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4445}
4446
4447TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 addConfigurationProperty("touch.deviceType", "touchScreen");
4449 prepareButtons();
4450 prepareAxes(POSITION);
4451 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004452 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453
4454 NotifyMotionArgs args;
4455
4456 // Rotation 90.
4457 prepareDisplay(DISPLAY_ORIENTATION_90);
4458 processDown(mapper, toRawX(50), toRawY(75));
4459 processSync(mapper);
4460
4461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4462 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4463 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4464
4465 processUp(mapper);
4466 processSync(mapper);
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4468}
4469
4470TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 addConfigurationProperty("touch.deviceType", "touchScreen");
4472 prepareButtons();
4473 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004474 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475
4476 NotifyMotionArgs args;
4477
4478 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004479 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 prepareDisplay(DISPLAY_ORIENTATION_0);
4481 processDown(mapper, toRawX(50), toRawY(75));
4482 processSync(mapper);
4483
4484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4485 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4486 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4487
4488 processUp(mapper);
4489 processSync(mapper);
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4491
4492 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004493 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 prepareDisplay(DISPLAY_ORIENTATION_90);
4495 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4496 processSync(mapper);
4497
4498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4499 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4500 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4501
4502 processUp(mapper);
4503 processSync(mapper);
4504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4505
4506 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004507 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 prepareDisplay(DISPLAY_ORIENTATION_180);
4509 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4510 processSync(mapper);
4511
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4513 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4514 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4515
4516 processUp(mapper);
4517 processSync(mapper);
4518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4519
4520 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004521 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 prepareDisplay(DISPLAY_ORIENTATION_270);
4523 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4524 processSync(mapper);
4525
4526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4527 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4528 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4529
4530 processUp(mapper);
4531 processSync(mapper);
4532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4533}
4534
4535TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536 addConfigurationProperty("touch.deviceType", "touchScreen");
4537 prepareDisplay(DISPLAY_ORIENTATION_0);
4538 prepareButtons();
4539 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004540 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541
4542 // These calculations are based on the input device calibration documentation.
4543 int32_t rawX = 100;
4544 int32_t rawY = 200;
4545 int32_t rawPressure = 10;
4546 int32_t rawToolMajor = 12;
4547 int32_t rawDistance = 2;
4548 int32_t rawTiltX = 30;
4549 int32_t rawTiltY = 110;
4550
4551 float x = toDisplayX(rawX);
4552 float y = toDisplayY(rawY);
4553 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4554 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4555 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4556 float distance = float(rawDistance);
4557
4558 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4559 float tiltScale = M_PI / 180;
4560 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4561 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4562 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4563 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4564
4565 processDown(mapper, rawX, rawY);
4566 processPressure(mapper, rawPressure);
4567 processToolMajor(mapper, rawToolMajor);
4568 processDistance(mapper, rawDistance);
4569 processTilt(mapper, rawTiltX, rawTiltY);
4570 processSync(mapper);
4571
4572 NotifyMotionArgs args;
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4575 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4576 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4577}
4578
Jason Gerecke489fda82012-09-07 17:19:40 -07004579TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004580 addConfigurationProperty("touch.deviceType", "touchScreen");
4581 prepareDisplay(DISPLAY_ORIENTATION_0);
4582 prepareLocationCalibration();
4583 prepareButtons();
4584 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004585 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004586
4587 int32_t rawX = 100;
4588 int32_t rawY = 200;
4589
4590 float x = toDisplayX(toCookedX(rawX, rawY));
4591 float y = toDisplayY(toCookedY(rawX, rawY));
4592
4593 processDown(mapper, rawX, rawY);
4594 processSync(mapper);
4595
4596 NotifyMotionArgs args;
4597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4599 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4600}
4601
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 addConfigurationProperty("touch.deviceType", "touchScreen");
4604 prepareDisplay(DISPLAY_ORIENTATION_0);
4605 prepareButtons();
4606 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004607 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608
4609 NotifyMotionArgs motionArgs;
4610 NotifyKeyArgs keyArgs;
4611
4612 processDown(mapper, 100, 200);
4613 processSync(mapper);
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4615 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4616 ASSERT_EQ(0, motionArgs.buttonState);
4617
4618 // press BTN_LEFT, release BTN_LEFT
4619 processKey(mapper, BTN_LEFT, 1);
4620 processSync(mapper);
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4623 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4624
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4626 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4627 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4628
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 processKey(mapper, BTN_LEFT, 0);
4630 processSync(mapper);
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004632 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004634
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004637 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638
4639 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4640 processKey(mapper, BTN_RIGHT, 1);
4641 processKey(mapper, BTN_MIDDLE, 1);
4642 processSync(mapper);
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4645 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4646 motionArgs.buttonState);
4647
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4649 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4650 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4651
4652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4653 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4654 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4655 motionArgs.buttonState);
4656
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 processKey(mapper, BTN_RIGHT, 0);
4658 processSync(mapper);
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004662
4663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004665 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666
4667 processKey(mapper, BTN_MIDDLE, 0);
4668 processSync(mapper);
4669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004672
4673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004675 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676
4677 // press BTN_BACK, release BTN_BACK
4678 processKey(mapper, BTN_BACK, 1);
4679 processSync(mapper);
4680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4681 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4682 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004683
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004686 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4687
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4690 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691
4692 processKey(mapper, BTN_BACK, 0);
4693 processSync(mapper);
4694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004695 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697
4698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004700 ASSERT_EQ(0, motionArgs.buttonState);
4701
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4703 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4704 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4705
4706 // press BTN_SIDE, release BTN_SIDE
4707 processKey(mapper, BTN_SIDE, 1);
4708 processSync(mapper);
4709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4710 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4711 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004712
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004715 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4716
4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4719 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720
4721 processKey(mapper, BTN_SIDE, 0);
4722 processSync(mapper);
4723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004724 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004726
4727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004729 ASSERT_EQ(0, motionArgs.buttonState);
4730
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4732 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4733 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4734
4735 // press BTN_FORWARD, release BTN_FORWARD
4736 processKey(mapper, BTN_FORWARD, 1);
4737 processSync(mapper);
4738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4739 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4740 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004741
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004744 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4745
4746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4748 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749
4750 processKey(mapper, BTN_FORWARD, 0);
4751 processSync(mapper);
4752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004755
4756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004758 ASSERT_EQ(0, motionArgs.buttonState);
4759
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4761 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4762 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4763
4764 // press BTN_EXTRA, release BTN_EXTRA
4765 processKey(mapper, BTN_EXTRA, 1);
4766 processSync(mapper);
4767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4768 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4769 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004770
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004773 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4774
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4776 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4777 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778
4779 processKey(mapper, BTN_EXTRA, 0);
4780 processSync(mapper);
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004782 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004784
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004787 ASSERT_EQ(0, motionArgs.buttonState);
4788
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4790 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4791 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4792
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4794
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 // press BTN_STYLUS, release BTN_STYLUS
4796 processKey(mapper, BTN_STYLUS, 1);
4797 processSync(mapper);
4798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4801
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4804 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805
4806 processKey(mapper, BTN_STYLUS, 0);
4807 processSync(mapper);
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004811
4812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815
4816 // press BTN_STYLUS2, release BTN_STYLUS2
4817 processKey(mapper, BTN_STYLUS2, 1);
4818 processSync(mapper);
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4822
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4825 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826
4827 processKey(mapper, BTN_STYLUS2, 0);
4828 processSync(mapper);
4829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004830 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004832
4833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004835 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836
4837 // release touch
4838 processUp(mapper);
4839 processSync(mapper);
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4841 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4842 ASSERT_EQ(0, motionArgs.buttonState);
4843}
4844
4845TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846 addConfigurationProperty("touch.deviceType", "touchScreen");
4847 prepareDisplay(DISPLAY_ORIENTATION_0);
4848 prepareButtons();
4849 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004850 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851
4852 NotifyMotionArgs motionArgs;
4853
4854 // default tool type is finger
4855 processDown(mapper, 100, 200);
4856 processSync(mapper);
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4860
4861 // eraser
4862 processKey(mapper, BTN_TOOL_RUBBER, 1);
4863 processSync(mapper);
4864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4867
4868 // stylus
4869 processKey(mapper, BTN_TOOL_RUBBER, 0);
4870 processKey(mapper, BTN_TOOL_PEN, 1);
4871 processSync(mapper);
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4875
4876 // brush
4877 processKey(mapper, BTN_TOOL_PEN, 0);
4878 processKey(mapper, BTN_TOOL_BRUSH, 1);
4879 processSync(mapper);
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4883
4884 // pencil
4885 processKey(mapper, BTN_TOOL_BRUSH, 0);
4886 processKey(mapper, BTN_TOOL_PENCIL, 1);
4887 processSync(mapper);
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4891
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004892 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 processKey(mapper, BTN_TOOL_PENCIL, 0);
4894 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4895 processSync(mapper);
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4899
4900 // mouse
4901 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4902 processKey(mapper, BTN_TOOL_MOUSE, 1);
4903 processSync(mapper);
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4907
4908 // lens
4909 processKey(mapper, BTN_TOOL_MOUSE, 0);
4910 processKey(mapper, BTN_TOOL_LENS, 1);
4911 processSync(mapper);
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4915
4916 // double-tap
4917 processKey(mapper, BTN_TOOL_LENS, 0);
4918 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4919 processSync(mapper);
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4923
4924 // triple-tap
4925 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4926 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4927 processSync(mapper);
4928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4931
4932 // quad-tap
4933 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4934 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4935 processSync(mapper);
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4939
4940 // finger
4941 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4942 processKey(mapper, BTN_TOOL_FINGER, 1);
4943 processSync(mapper);
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4945 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4947
4948 // stylus trumps finger
4949 processKey(mapper, BTN_TOOL_PEN, 1);
4950 processSync(mapper);
4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4954
4955 // eraser trumps stylus
4956 processKey(mapper, BTN_TOOL_RUBBER, 1);
4957 processSync(mapper);
4958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4961
4962 // mouse trumps eraser
4963 processKey(mapper, BTN_TOOL_MOUSE, 1);
4964 processSync(mapper);
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4968
4969 // back to default tool type
4970 processKey(mapper, BTN_TOOL_MOUSE, 0);
4971 processKey(mapper, BTN_TOOL_RUBBER, 0);
4972 processKey(mapper, BTN_TOOL_PEN, 0);
4973 processKey(mapper, BTN_TOOL_FINGER, 0);
4974 processSync(mapper);
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4977 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4978}
4979
4980TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981 addConfigurationProperty("touch.deviceType", "touchScreen");
4982 prepareDisplay(DISPLAY_ORIENTATION_0);
4983 prepareButtons();
4984 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004985 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004986 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987
4988 NotifyMotionArgs motionArgs;
4989
4990 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4991 processKey(mapper, BTN_TOOL_FINGER, 1);
4992 processMove(mapper, 100, 200);
4993 processSync(mapper);
4994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4995 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4997 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4998
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5002 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5003
5004 // move a little
5005 processMove(mapper, 150, 250);
5006 processSync(mapper);
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5010 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5011
5012 // down when BTN_TOUCH is pressed, pressure defaults to 1
5013 processKey(mapper, BTN_TOUCH, 1);
5014 processSync(mapper);
5015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5016 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5018 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5019
5020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5023 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5024
5025 // up when BTN_TOUCH is released, hover restored
5026 processKey(mapper, BTN_TOUCH, 0);
5027 processSync(mapper);
5028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5029 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5031 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5032
5033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5034 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5039 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5041 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5042
5043 // exit hover when pointer goes away
5044 processKey(mapper, BTN_TOOL_FINGER, 0);
5045 processSync(mapper);
5046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5047 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5049 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5050}
5051
5052TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053 addConfigurationProperty("touch.deviceType", "touchScreen");
5054 prepareDisplay(DISPLAY_ORIENTATION_0);
5055 prepareButtons();
5056 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005057 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058
5059 NotifyMotionArgs motionArgs;
5060
5061 // initially hovering because pressure is 0
5062 processDown(mapper, 100, 200);
5063 processPressure(mapper, 0);
5064 processSync(mapper);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5068 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5069
5070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5071 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5073 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5074
5075 // move a little
5076 processMove(mapper, 150, 250);
5077 processSync(mapper);
5078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5079 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5081 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5082
5083 // down when pressure is non-zero
5084 processPressure(mapper, RAW_PRESSURE_MAX);
5085 processSync(mapper);
5086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5087 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5089 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5090
5091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5094 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5095
5096 // up when pressure becomes 0, hover restored
5097 processPressure(mapper, 0);
5098 processSync(mapper);
5099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5100 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5102 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5103
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5105 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5110 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5112 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5113
5114 // exit hover when pointer goes away
5115 processUp(mapper);
5116 processSync(mapper);
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5118 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5120 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5121}
5122
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123// --- MultiTouchInputMapperTest ---
5124
5125class MultiTouchInputMapperTest : public TouchInputMapperTest {
5126protected:
5127 void prepareAxes(int axes);
5128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005129 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5130 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5131 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5132 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5133 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5134 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5135 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5136 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5137 void processId(MultiTouchInputMapper& mapper, int32_t id);
5138 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5139 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5140 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5141 void processMTSync(MultiTouchInputMapper& mapper);
5142 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143};
5144
5145void MultiTouchInputMapperTest::prepareAxes(int axes) {
5146 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5148 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 }
5150 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5152 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005154 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5155 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156 }
5157 }
5158 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005159 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5160 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5163 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164 }
5165 }
5166 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005167 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5168 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169 }
5170 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005171 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5172 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 }
5174 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005175 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5176 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177 }
5178 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005179 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5180 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181 }
5182 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005183 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5184 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185 }
5186 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005187 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188 }
5189}
5190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005191void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5192 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005193 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5194 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195}
5196
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005197void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5198 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005199 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200}
5201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005202void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5203 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005204 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205}
5206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005207void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005208 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209}
5210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005211void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005212 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213}
5214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005215void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5216 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005217 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218}
5219
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005220void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005221 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222}
5223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005224void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005225 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226}
5227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005228void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005229 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230}
5231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005232void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005233 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234}
5235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005236void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005237 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238}
5239
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005240void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5241 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005242 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243}
5244
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005245void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005246 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247}
5248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005249void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005250 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005251}
5252
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254 addConfigurationProperty("touch.deviceType", "touchScreen");
5255 prepareDisplay(DISPLAY_ORIENTATION_0);
5256 prepareAxes(POSITION);
5257 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005258 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259
5260 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5261
5262 NotifyMotionArgs motionArgs;
5263
5264 // Two fingers down at once.
5265 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5266 processPosition(mapper, x1, y1);
5267 processMTSync(mapper);
5268 processPosition(mapper, x2, y2);
5269 processMTSync(mapper);
5270 processSync(mapper);
5271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5278 ASSERT_EQ(0, motionArgs.flags);
5279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5280 ASSERT_EQ(0, motionArgs.buttonState);
5281 ASSERT_EQ(0, motionArgs.edgeFlags);
5282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5286 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5287 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5288 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5290
5291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5293 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5294 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5295 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5296 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5297 motionArgs.action);
5298 ASSERT_EQ(0, motionArgs.flags);
5299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5300 ASSERT_EQ(0, motionArgs.buttonState);
5301 ASSERT_EQ(0, motionArgs.edgeFlags);
5302 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5305 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5308 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5310 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5311 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5312 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5314
5315 // Move.
5316 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5317 processPosition(mapper, x1, y1);
5318 processMTSync(mapper);
5319 processPosition(mapper, x2, y2);
5320 processMTSync(mapper);
5321 processSync(mapper);
5322
5323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5324 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5325 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5326 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5327 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5329 ASSERT_EQ(0, motionArgs.flags);
5330 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5331 ASSERT_EQ(0, motionArgs.buttonState);
5332 ASSERT_EQ(0, motionArgs.edgeFlags);
5333 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5334 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5336 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5339 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5341 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5342 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5343 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5345
5346 // First finger up.
5347 x2 += 15; y2 -= 20;
5348 processPosition(mapper, x2, y2);
5349 processMTSync(mapper);
5350 processSync(mapper);
5351
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5354 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5355 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5356 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5357 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5358 motionArgs.action);
5359 ASSERT_EQ(0, motionArgs.flags);
5360 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5361 ASSERT_EQ(0, motionArgs.buttonState);
5362 ASSERT_EQ(0, motionArgs.edgeFlags);
5363 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5364 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5366 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5369 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5371 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5372 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5373 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5374 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5375
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5378 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5379 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5380 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5382 ASSERT_EQ(0, motionArgs.flags);
5383 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5384 ASSERT_EQ(0, motionArgs.buttonState);
5385 ASSERT_EQ(0, motionArgs.edgeFlags);
5386 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5387 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5390 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5391 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5392 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5394
5395 // Move.
5396 x2 += 20; y2 -= 25;
5397 processPosition(mapper, x2, y2);
5398 processMTSync(mapper);
5399 processSync(mapper);
5400
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5403 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5404 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5405 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5407 ASSERT_EQ(0, motionArgs.flags);
5408 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5409 ASSERT_EQ(0, motionArgs.buttonState);
5410 ASSERT_EQ(0, motionArgs.edgeFlags);
5411 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5412 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5415 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5416 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5417 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5419
5420 // New finger down.
5421 int32_t x3 = 700, y3 = 300;
5422 processPosition(mapper, x2, y2);
5423 processMTSync(mapper);
5424 processPosition(mapper, x3, y3);
5425 processMTSync(mapper);
5426 processSync(mapper);
5427
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5430 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5431 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5432 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5433 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5434 motionArgs.action);
5435 ASSERT_EQ(0, motionArgs.flags);
5436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5437 ASSERT_EQ(0, motionArgs.buttonState);
5438 ASSERT_EQ(0, motionArgs.edgeFlags);
5439 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5442 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5445 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5447 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5448 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5449 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5451
5452 // Second finger up.
5453 x3 += 30; y3 -= 20;
5454 processPosition(mapper, x3, y3);
5455 processMTSync(mapper);
5456 processSync(mapper);
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5460 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5461 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5462 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5463 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5464 motionArgs.action);
5465 ASSERT_EQ(0, motionArgs.flags);
5466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5467 ASSERT_EQ(0, motionArgs.buttonState);
5468 ASSERT_EQ(0, motionArgs.edgeFlags);
5469 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5472 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5475 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5477 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5478 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5479 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5480 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5481
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5484 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5485 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5486 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5488 ASSERT_EQ(0, motionArgs.flags);
5489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5490 ASSERT_EQ(0, motionArgs.buttonState);
5491 ASSERT_EQ(0, motionArgs.edgeFlags);
5492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5496 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5497 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5498 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5500
5501 // Last finger up.
5502 processMTSync(mapper);
5503 processSync(mapper);
5504
5505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5506 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5507 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5508 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5509 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5510 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5511 ASSERT_EQ(0, motionArgs.flags);
5512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5513 ASSERT_EQ(0, motionArgs.buttonState);
5514 ASSERT_EQ(0, motionArgs.edgeFlags);
5515 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5516 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5519 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5520 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5521 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5523
5524 // Should not have sent any more keys or motions.
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5527}
5528
5529TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 addConfigurationProperty("touch.deviceType", "touchScreen");
5531 prepareDisplay(DISPLAY_ORIENTATION_0);
5532 prepareAxes(POSITION | ID);
5533 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005534 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005535
5536 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5537
5538 NotifyMotionArgs motionArgs;
5539
5540 // Two fingers down at once.
5541 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5542 processPosition(mapper, x1, y1);
5543 processId(mapper, 1);
5544 processMTSync(mapper);
5545 processPosition(mapper, x2, y2);
5546 processId(mapper, 2);
5547 processMTSync(mapper);
5548 processSync(mapper);
5549
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5552 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5553 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5556 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5560 motionArgs.action);
5561 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5564 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5567 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5569 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5570
5571 // Move.
5572 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5573 processPosition(mapper, x1, y1);
5574 processId(mapper, 1);
5575 processMTSync(mapper);
5576 processPosition(mapper, x2, y2);
5577 processId(mapper, 2);
5578 processMTSync(mapper);
5579 processSync(mapper);
5580
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5583 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5584 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5586 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5589 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5591 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5592
5593 // First finger up.
5594 x2 += 15; y2 -= 20;
5595 processPosition(mapper, x2, y2);
5596 processId(mapper, 2);
5597 processMTSync(mapper);
5598 processSync(mapper);
5599
5600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5601 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5602 motionArgs.action);
5603 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5604 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5606 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5609 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5611 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5612
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5615 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5616 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5620
5621 // Move.
5622 x2 += 20; y2 -= 25;
5623 processPosition(mapper, x2, y2);
5624 processId(mapper, 2);
5625 processMTSync(mapper);
5626 processSync(mapper);
5627
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5630 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5631 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5634 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5635
5636 // New finger down.
5637 int32_t x3 = 700, y3 = 300;
5638 processPosition(mapper, x2, y2);
5639 processId(mapper, 2);
5640 processMTSync(mapper);
5641 processPosition(mapper, x3, y3);
5642 processId(mapper, 3);
5643 processMTSync(mapper);
5644 processSync(mapper);
5645
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5647 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5648 motionArgs.action);
5649 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5652 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5655 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5657 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5658
5659 // Second finger up.
5660 x3 += 30; y3 -= 20;
5661 processPosition(mapper, x3, y3);
5662 processId(mapper, 3);
5663 processMTSync(mapper);
5664 processSync(mapper);
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5667 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5668 motionArgs.action);
5669 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5672 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5675 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5677 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5678
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5681 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5682 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5683 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5685 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5686
5687 // Last finger up.
5688 processMTSync(mapper);
5689 processSync(mapper);
5690
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5692 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5693 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5694 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5697 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5698
5699 // Should not have sent any more keys or motions.
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5702}
5703
5704TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 addConfigurationProperty("touch.deviceType", "touchScreen");
5706 prepareDisplay(DISPLAY_ORIENTATION_0);
5707 prepareAxes(POSITION | ID | SLOT);
5708 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005709 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710
5711 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5712
5713 NotifyMotionArgs motionArgs;
5714
5715 // Two fingers down at once.
5716 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5717 processPosition(mapper, x1, y1);
5718 processId(mapper, 1);
5719 processSlot(mapper, 1);
5720 processPosition(mapper, x2, y2);
5721 processId(mapper, 2);
5722 processSync(mapper);
5723
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5726 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5727 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5730 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5731
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5733 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5734 motionArgs.action);
5735 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5736 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5738 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5741 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5743 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5744
5745 // Move.
5746 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5747 processSlot(mapper, 0);
5748 processPosition(mapper, x1, y1);
5749 processSlot(mapper, 1);
5750 processPosition(mapper, x2, y2);
5751 processSync(mapper);
5752
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5755 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5756 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5758 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5761 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5763 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5764
5765 // First finger up.
5766 x2 += 15; y2 -= 20;
5767 processSlot(mapper, 0);
5768 processId(mapper, -1);
5769 processSlot(mapper, 1);
5770 processPosition(mapper, x2, y2);
5771 processSync(mapper);
5772
5773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5774 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5775 motionArgs.action);
5776 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5779 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5782 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
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 // Move.
5795 x2 += 20; y2 -= 25;
5796 processPosition(mapper, x2, y2);
5797 processSync(mapper);
5798
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5801 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5802 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5805 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5806
5807 // New finger down.
5808 int32_t x3 = 700, y3 = 300;
5809 processPosition(mapper, x2, y2);
5810 processSlot(mapper, 0);
5811 processId(mapper, 3);
5812 processPosition(mapper, x3, y3);
5813 processSync(mapper);
5814
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5817 motionArgs.action);
5818 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5819 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5821 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5824 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5826 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5827
5828 // Second finger up.
5829 x3 += 30; y3 -= 20;
5830 processSlot(mapper, 1);
5831 processId(mapper, -1);
5832 processSlot(mapper, 0);
5833 processPosition(mapper, x3, y3);
5834 processSync(mapper);
5835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5838 motionArgs.action);
5839 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5845 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5847 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5848
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5856
5857 // Last finger up.
5858 processId(mapper, -1);
5859 processSync(mapper);
5860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5863 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5867 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5868
5869 // Should not have sent any more keys or motions.
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5872}
5873
5874TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875 addConfigurationProperty("touch.deviceType", "touchScreen");
5876 prepareDisplay(DISPLAY_ORIENTATION_0);
5877 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005878 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 // These calculations are based on the input device calibration documentation.
5881 int32_t rawX = 100;
5882 int32_t rawY = 200;
5883 int32_t rawTouchMajor = 7;
5884 int32_t rawTouchMinor = 6;
5885 int32_t rawToolMajor = 9;
5886 int32_t rawToolMinor = 8;
5887 int32_t rawPressure = 11;
5888 int32_t rawDistance = 0;
5889 int32_t rawOrientation = 3;
5890 int32_t id = 5;
5891
5892 float x = toDisplayX(rawX);
5893 float y = toDisplayY(rawY);
5894 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5895 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5896 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5897 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5898 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5899 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5900 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5901 float distance = float(rawDistance);
5902
5903 processPosition(mapper, rawX, rawY);
5904 processTouchMajor(mapper, rawTouchMajor);
5905 processTouchMinor(mapper, rawTouchMinor);
5906 processToolMajor(mapper, rawToolMajor);
5907 processToolMinor(mapper, rawToolMinor);
5908 processPressure(mapper, rawPressure);
5909 processOrientation(mapper, rawOrientation);
5910 processDistance(mapper, rawDistance);
5911 processId(mapper, id);
5912 processMTSync(mapper);
5913 processSync(mapper);
5914
5915 NotifyMotionArgs args;
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5917 ASSERT_EQ(0, args.pointerProperties[0].id);
5918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5919 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5920 orientation, distance));
5921}
5922
5923TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 addConfigurationProperty("touch.deviceType", "touchScreen");
5925 prepareDisplay(DISPLAY_ORIENTATION_0);
5926 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5927 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005928 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929
5930 // These calculations are based on the input device calibration documentation.
5931 int32_t rawX = 100;
5932 int32_t rawY = 200;
5933 int32_t rawTouchMajor = 140;
5934 int32_t rawTouchMinor = 120;
5935 int32_t rawToolMajor = 180;
5936 int32_t rawToolMinor = 160;
5937
5938 float x = toDisplayX(rawX);
5939 float y = toDisplayY(rawY);
5940 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5941 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5942 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5943 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5944 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5945
5946 processPosition(mapper, rawX, rawY);
5947 processTouchMajor(mapper, rawTouchMajor);
5948 processTouchMinor(mapper, rawTouchMinor);
5949 processToolMajor(mapper, rawToolMajor);
5950 processToolMinor(mapper, rawToolMinor);
5951 processMTSync(mapper);
5952 processSync(mapper);
5953
5954 NotifyMotionArgs args;
5955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5957 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5958}
5959
5960TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 addConfigurationProperty("touch.deviceType", "touchScreen");
5962 prepareDisplay(DISPLAY_ORIENTATION_0);
5963 prepareAxes(POSITION | TOUCH | TOOL);
5964 addConfigurationProperty("touch.size.calibration", "diameter");
5965 addConfigurationProperty("touch.size.scale", "10");
5966 addConfigurationProperty("touch.size.bias", "160");
5967 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005968 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969
5970 // These calculations are based on the input device calibration documentation.
5971 // Note: We only provide a single common touch/tool value because the device is assumed
5972 // not to emit separate values for each pointer (isSummed = 1).
5973 int32_t rawX = 100;
5974 int32_t rawY = 200;
5975 int32_t rawX2 = 150;
5976 int32_t rawY2 = 250;
5977 int32_t rawTouchMajor = 5;
5978 int32_t rawToolMajor = 8;
5979
5980 float x = toDisplayX(rawX);
5981 float y = toDisplayY(rawY);
5982 float x2 = toDisplayX(rawX2);
5983 float y2 = toDisplayY(rawY2);
5984 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5985 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5986 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5987
5988 processPosition(mapper, rawX, rawY);
5989 processTouchMajor(mapper, rawTouchMajor);
5990 processToolMajor(mapper, rawToolMajor);
5991 processMTSync(mapper);
5992 processPosition(mapper, rawX2, rawY2);
5993 processTouchMajor(mapper, rawTouchMajor);
5994 processToolMajor(mapper, rawToolMajor);
5995 processMTSync(mapper);
5996 processSync(mapper);
5997
5998 NotifyMotionArgs args;
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6000 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6001
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6003 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6004 args.action);
6005 ASSERT_EQ(size_t(2), args.pointerCount);
6006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6007 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6009 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6010}
6011
6012TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013 addConfigurationProperty("touch.deviceType", "touchScreen");
6014 prepareDisplay(DISPLAY_ORIENTATION_0);
6015 prepareAxes(POSITION | TOUCH | TOOL);
6016 addConfigurationProperty("touch.size.calibration", "area");
6017 addConfigurationProperty("touch.size.scale", "43");
6018 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020
6021 // These calculations are based on the input device calibration documentation.
6022 int32_t rawX = 100;
6023 int32_t rawY = 200;
6024 int32_t rawTouchMajor = 5;
6025 int32_t rawToolMajor = 8;
6026
6027 float x = toDisplayX(rawX);
6028 float y = toDisplayY(rawY);
6029 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6030 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6031 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6032
6033 processPosition(mapper, rawX, rawY);
6034 processTouchMajor(mapper, rawTouchMajor);
6035 processToolMajor(mapper, rawToolMajor);
6036 processMTSync(mapper);
6037 processSync(mapper);
6038
6039 NotifyMotionArgs args;
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6042 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6043}
6044
6045TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046 addConfigurationProperty("touch.deviceType", "touchScreen");
6047 prepareDisplay(DISPLAY_ORIENTATION_0);
6048 prepareAxes(POSITION | PRESSURE);
6049 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6050 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006051 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052
Michael Wrightaa449c92017-12-13 21:21:43 +00006053 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006054 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006055 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6056 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6057 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6058
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 // These calculations are based on the input device calibration documentation.
6060 int32_t rawX = 100;
6061 int32_t rawY = 200;
6062 int32_t rawPressure = 60;
6063
6064 float x = toDisplayX(rawX);
6065 float y = toDisplayY(rawY);
6066 float pressure = float(rawPressure) * 0.01f;
6067
6068 processPosition(mapper, rawX, rawY);
6069 processPressure(mapper, rawPressure);
6070 processMTSync(mapper);
6071 processSync(mapper);
6072
6073 NotifyMotionArgs args;
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6076 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6077}
6078
6079TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 addConfigurationProperty("touch.deviceType", "touchScreen");
6081 prepareDisplay(DISPLAY_ORIENTATION_0);
6082 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006083 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006084
6085 NotifyMotionArgs motionArgs;
6086 NotifyKeyArgs keyArgs;
6087
6088 processId(mapper, 1);
6089 processPosition(mapper, 100, 200);
6090 processSync(mapper);
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6093 ASSERT_EQ(0, motionArgs.buttonState);
6094
6095 // press BTN_LEFT, release BTN_LEFT
6096 processKey(mapper, BTN_LEFT, 1);
6097 processSync(mapper);
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6100 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6101
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6104 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6105
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106 processKey(mapper, BTN_LEFT, 0);
6107 processSync(mapper);
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006109 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006111
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006114 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115
6116 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6117 processKey(mapper, BTN_RIGHT, 1);
6118 processKey(mapper, BTN_MIDDLE, 1);
6119 processSync(mapper);
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6122 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6123 motionArgs.buttonState);
6124
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6127 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6128
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6130 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6131 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6132 motionArgs.buttonState);
6133
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 processKey(mapper, BTN_RIGHT, 0);
6135 processSync(mapper);
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006139
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143
6144 processKey(mapper, BTN_MIDDLE, 0);
6145 processSync(mapper);
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006149
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006152 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153
6154 // press BTN_BACK, release BTN_BACK
6155 processKey(mapper, BTN_BACK, 1);
6156 processSync(mapper);
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6158 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6159 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006160
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006163 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6164
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6167 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168
6169 processKey(mapper, BTN_BACK, 0);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006174
6175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006177 ASSERT_EQ(0, motionArgs.buttonState);
6178
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6180 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6181 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6182
6183 // press BTN_SIDE, release BTN_SIDE
6184 processKey(mapper, BTN_SIDE, 1);
6185 processSync(mapper);
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6187 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6188 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006192 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6193
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6196 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197
6198 processKey(mapper, BTN_SIDE, 0);
6199 processSync(mapper);
6200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006203
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006206 ASSERT_EQ(0, motionArgs.buttonState);
6207
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6209 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6210 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6211
6212 // press BTN_FORWARD, release BTN_FORWARD
6213 processKey(mapper, BTN_FORWARD, 1);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6216 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6217 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006218
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006221 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6222
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6225 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226
6227 processKey(mapper, BTN_FORWARD, 0);
6228 processSync(mapper);
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006235 ASSERT_EQ(0, motionArgs.buttonState);
6236
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6238 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6239 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6240
6241 // press BTN_EXTRA, release BTN_EXTRA
6242 processKey(mapper, BTN_EXTRA, 1);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6245 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6246 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006247
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006250 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6251
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6253 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6254 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255
6256 processKey(mapper, BTN_EXTRA, 0);
6257 processSync(mapper);
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006261
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006264 ASSERT_EQ(0, motionArgs.buttonState);
6265
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6267 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6268 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6269
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6271
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 // press BTN_STYLUS, release BTN_STYLUS
6273 processKey(mapper, BTN_STYLUS, 1);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6278
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6281 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282
6283 processKey(mapper, BTN_STYLUS, 0);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292
6293 // press BTN_STYLUS2, release BTN_STYLUS2
6294 processKey(mapper, BTN_STYLUS2, 1);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006298 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6299
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6302 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303
6304 processKey(mapper, BTN_STYLUS2, 0);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313
6314 // release touch
6315 processId(mapper, -1);
6316 processSync(mapper);
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6319 ASSERT_EQ(0, motionArgs.buttonState);
6320}
6321
6322TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323 addConfigurationProperty("touch.deviceType", "touchScreen");
6324 prepareDisplay(DISPLAY_ORIENTATION_0);
6325 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006326 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006327
6328 NotifyMotionArgs motionArgs;
6329
6330 // default tool type is finger
6331 processId(mapper, 1);
6332 processPosition(mapper, 100, 200);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6337
6338 // eraser
6339 processKey(mapper, BTN_TOOL_RUBBER, 1);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6344
6345 // stylus
6346 processKey(mapper, BTN_TOOL_RUBBER, 0);
6347 processKey(mapper, BTN_TOOL_PEN, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6352
6353 // brush
6354 processKey(mapper, BTN_TOOL_PEN, 0);
6355 processKey(mapper, BTN_TOOL_BRUSH, 1);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6360
6361 // pencil
6362 processKey(mapper, BTN_TOOL_BRUSH, 0);
6363 processKey(mapper, BTN_TOOL_PENCIL, 1);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6368
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006369 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 processKey(mapper, BTN_TOOL_PENCIL, 0);
6371 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6376
6377 // mouse
6378 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6379 processKey(mapper, BTN_TOOL_MOUSE, 1);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6384
6385 // lens
6386 processKey(mapper, BTN_TOOL_MOUSE, 0);
6387 processKey(mapper, BTN_TOOL_LENS, 1);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6392
6393 // double-tap
6394 processKey(mapper, BTN_TOOL_LENS, 0);
6395 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6400
6401 // triple-tap
6402 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6403 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6408
6409 // quad-tap
6410 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6411 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6416
6417 // finger
6418 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6419 processKey(mapper, BTN_TOOL_FINGER, 1);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6424
6425 // stylus trumps finger
6426 processKey(mapper, BTN_TOOL_PEN, 1);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6431
6432 // eraser trumps stylus
6433 processKey(mapper, BTN_TOOL_RUBBER, 1);
6434 processSync(mapper);
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6438
6439 // mouse trumps eraser
6440 processKey(mapper, BTN_TOOL_MOUSE, 1);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6445
6446 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6447 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6452
6453 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6454 processToolType(mapper, MT_TOOL_PEN);
6455 processSync(mapper);
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6459
6460 // back to default tool type
6461 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6462 processKey(mapper, BTN_TOOL_MOUSE, 0);
6463 processKey(mapper, BTN_TOOL_RUBBER, 0);
6464 processKey(mapper, BTN_TOOL_PEN, 0);
6465 processKey(mapper, BTN_TOOL_FINGER, 0);
6466 processSync(mapper);
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6470}
6471
6472TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473 addConfigurationProperty("touch.deviceType", "touchScreen");
6474 prepareDisplay(DISPLAY_ORIENTATION_0);
6475 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006476 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006477 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478
6479 NotifyMotionArgs motionArgs;
6480
6481 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6482 processId(mapper, 1);
6483 processPosition(mapper, 100, 200);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6488 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6489
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6493 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6494
6495 // move a little
6496 processPosition(mapper, 150, 250);
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6501 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6502
6503 // down when BTN_TOUCH is pressed, pressure defaults to 1
6504 processKey(mapper, BTN_TOUCH, 1);
6505 processSync(mapper);
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6509 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6510
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6514 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6515
6516 // up when BTN_TOUCH is released, hover restored
6517 processKey(mapper, BTN_TOUCH, 0);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6522 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6523
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6532 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6533
6534 // exit hover when pointer goes away
6535 processId(mapper, -1);
6536 processSync(mapper);
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6540 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6541}
6542
6543TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544 addConfigurationProperty("touch.deviceType", "touchScreen");
6545 prepareDisplay(DISPLAY_ORIENTATION_0);
6546 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006547 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006548
6549 NotifyMotionArgs motionArgs;
6550
6551 // initially hovering because pressure is 0
6552 processId(mapper, 1);
6553 processPosition(mapper, 100, 200);
6554 processPressure(mapper, 0);
6555 processSync(mapper);
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6559 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6560
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6564 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6565
6566 // move a little
6567 processPosition(mapper, 150, 250);
6568 processSync(mapper);
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6572 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6573
6574 // down when pressure becomes non-zero
6575 processPressure(mapper, RAW_PRESSURE_MAX);
6576 processSync(mapper);
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6580 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6581
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6585 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6586
6587 // up when pressure becomes 0, hover restored
6588 processPressure(mapper, 0);
6589 processSync(mapper);
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6593 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6594
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6603 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6604
6605 // exit hover when pointer goes away
6606 processId(mapper, -1);
6607 processSync(mapper);
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6612}
6613
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006614/**
6615 * Set the input device port <--> display port associations, and check that the
6616 * events are routed to the display that matches the display port.
6617 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6618 */
6619TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006620 const std::string usb2 = "USB2";
6621 const uint8_t hdmi1 = 0;
6622 const uint8_t hdmi2 = 1;
6623 const std::string secondaryUniqueId = "uniqueId2";
6624 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6625
6626 addConfigurationProperty("touch.deviceType", "touchScreen");
6627 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006628 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006629
6630 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6631 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6632
6633 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6634 // for this input device is specified, and the matching viewport is not present,
6635 // the input device should be disabled (at the mapper level).
6636
6637 // Add viewport for display 2 on hdmi2
6638 prepareSecondaryDisplay(type, hdmi2);
6639 // Send a touch event
6640 processPosition(mapper, 100, 100);
6641 processSync(mapper);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6643
6644 // Add viewport for display 1 on hdmi1
6645 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6646 // Send a touch event again
6647 processPosition(mapper, 100, 100);
6648 processSync(mapper);
6649
6650 NotifyMotionArgs args;
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6652 ASSERT_EQ(DISPLAY_ID, args.displayId);
6653}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006654
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006655TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006656 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006657 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006658 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006659 fakePointerController->setPosition(100, 200);
6660 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006661 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6662
Garfield Tan888a6a42020-01-09 11:39:16 -08006663 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6664 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6665
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006666 prepareDisplay(DISPLAY_ORIENTATION_0);
6667 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006668 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006669
6670 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006671 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006672
6673 NotifyMotionArgs motionArgs;
6674 processPosition(mapper, 100, 100);
6675 processSync(mapper);
6676
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6679 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6680}
6681
Arthur Hung7c645402019-01-25 17:45:42 +08006682TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6683 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006684 prepareAxes(POSITION | ID | SLOT);
6685 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006686 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006687
6688 // Create the second touch screen device, and enable multi fingers.
6689 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006690 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006691 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006692 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006693 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006694 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006695 std::unique_ptr<InputDevice> device2 =
6696 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006697 identifier);
6698 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6699 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6700 0 /*flat*/, 0 /*fuzz*/);
6701 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6702 0 /*flat*/, 0 /*fuzz*/);
6703 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6704 0 /*flat*/, 0 /*fuzz*/);
6705 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6706 0 /*flat*/, 0 /*fuzz*/);
6707 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6708 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6709 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006710
6711 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006712 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006713 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6714 device2->reset(ARBITRARY_TIME);
6715
6716 // Setup PointerController.
6717 sp<FakePointerController> fakePointerController = new FakePointerController();
6718 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6719 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6720
6721 // Setup policy for associated displays and show touches.
6722 const uint8_t hdmi1 = 0;
6723 const uint8_t hdmi2 = 1;
6724 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6725 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6726 mFakePolicy->setShowTouches(true);
6727
6728 // Create displays.
6729 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6730 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6731
6732 // Default device will reconfigure above, need additional reconfiguration for another device.
6733 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6734 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6735
6736 // Two fingers down at default display.
6737 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6738 processPosition(mapper, x1, y1);
6739 processId(mapper, 1);
6740 processSlot(mapper, 1);
6741 processPosition(mapper, x2, y2);
6742 processId(mapper, 2);
6743 processSync(mapper);
6744
6745 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6746 fakePointerController->getSpots().find(DISPLAY_ID);
6747 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6748 ASSERT_EQ(size_t(2), iter->second.size());
6749
6750 // Two fingers down at second display.
6751 processPosition(mapper2, x1, y1);
6752 processId(mapper2, 1);
6753 processSlot(mapper2, 1);
6754 processPosition(mapper2, x2, y2);
6755 processId(mapper2, 2);
6756 processSync(mapper2);
6757
6758 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6759 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6760 ASSERT_EQ(size_t(2), iter->second.size());
6761}
6762
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006763TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006764 prepareAxes(POSITION);
6765 addConfigurationProperty("touch.deviceType", "touchScreen");
6766 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006767 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006768
6769 NotifyMotionArgs motionArgs;
6770 // Unrotated video frame
6771 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6772 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006773 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006774 processPosition(mapper, 100, 200);
6775 processSync(mapper);
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6777 ASSERT_EQ(frames, motionArgs.videoFrames);
6778
6779 // Subsequent touch events should not have any videoframes
6780 // This is implemented separately in FakeEventHub,
6781 // but that should match the behaviour of TouchVideoDevice.
6782 processPosition(mapper, 200, 200);
6783 processSync(mapper);
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6785 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6786}
6787
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006788TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006789 prepareAxes(POSITION);
6790 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006791 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006792 // Unrotated video frame
6793 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6794 NotifyMotionArgs motionArgs;
6795
6796 // Test all 4 orientations
6797 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6798 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6799 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6800 clearViewports();
6801 prepareDisplay(orientation);
6802 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006803 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006804 processPosition(mapper, 100, 200);
6805 processSync(mapper);
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6807 frames[0].rotate(orientation);
6808 ASSERT_EQ(frames, motionArgs.videoFrames);
6809 }
6810}
6811
6812TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006813 prepareAxes(POSITION);
6814 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006815 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006816 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6817 // so mix these.
6818 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6819 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6820 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6821 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6822 NotifyMotionArgs motionArgs;
6823
6824 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006825 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006826 processPosition(mapper, 100, 200);
6827 processSync(mapper);
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6829 std::for_each(frames.begin(), frames.end(),
6830 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6831 ASSERT_EQ(frames, motionArgs.videoFrames);
6832}
6833
Arthur Hung9da14732019-09-02 16:16:58 +08006834/**
6835 * If we had defined port associations, but the viewport is not ready, the touch device would be
6836 * expected to be disabled, and it should be enabled after the viewport has found.
6837 */
6838TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08006839 constexpr uint8_t hdmi2 = 1;
6840 const std::string secondaryUniqueId = "uniqueId2";
6841 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6842
6843 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6844
6845 addConfigurationProperty("touch.deviceType", "touchScreen");
6846 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006847 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08006848
6849 ASSERT_EQ(mDevice->isEnabled(), false);
6850
6851 // Add display on hdmi2, the device should be enabled and can receive touch event.
6852 prepareSecondaryDisplay(type, hdmi2);
6853 ASSERT_EQ(mDevice->isEnabled(), true);
6854
6855 // Send a touch event.
6856 processPosition(mapper, 100, 100);
6857 processSync(mapper);
6858
6859 NotifyMotionArgs args;
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6861 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6862}
6863
Arthur Hung6cd19a42019-08-30 19:04:12 +08006864/**
6865 * Test touch should not work if outside of surface.
6866 */
6867TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
Arthur Hung6cd19a42019-08-30 19:04:12 +08006868 addConfigurationProperty("touch.deviceType", "touchScreen");
6869 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006870 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006871 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung6cd19a42019-08-30 19:04:12 +08006872
Arthur Hung05de5772019-09-26 18:31:26 +08006873 // Touch on left-top area should work.
6874 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6875 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6876 processPosition(mapper, rawX, rawY);
6877 processSync(mapper);
6878
6879 NotifyMotionArgs args;
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6881
6882 // Reset.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006883 mapper.reset(ARBITRARY_TIME);
Arthur Hung05de5772019-09-26 18:31:26 +08006884
6885 // Let logical display be different to physical display and rotate 90-degrees.
6886 std::optional<DisplayViewport> internalViewport =
6887 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6888 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6889 internalViewport->logicalLeft = 0;
6890 internalViewport->logicalTop = 0;
6891 internalViewport->logicalRight = DISPLAY_HEIGHT;
6892 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6893
6894 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6895 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6896 internalViewport->physicalRight = DISPLAY_HEIGHT;
6897 internalViewport->physicalBottom = DISPLAY_WIDTH;
6898
6899 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6900 internalViewport->deviceHeight = DISPLAY_WIDTH;
6901 mFakePolicy->updateViewport(internalViewport.value());
6902 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6903
6904 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006905 processPosition(mapper, rawX, rawY);
6906 processSync(mapper);
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6908}
6909
Arthur Hung421eb1c2020-01-16 00:09:42 +08006910TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006911 addConfigurationProperty("touch.deviceType", "touchScreen");
6912 prepareDisplay(DISPLAY_ORIENTATION_0);
6913 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006914 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006915
6916 NotifyMotionArgs motionArgs;
6917
6918 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6919 // finger down
6920 processId(mapper, 1);
6921 processPosition(mapper, x1, y1);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6926
6927 // finger move
6928 processId(mapper, 1);
6929 processPosition(mapper, x2, y2);
6930 processSync(mapper);
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6934
6935 // finger up.
6936 processId(mapper, -1);
6937 processSync(mapper);
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6939 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6940 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6941
6942 // new finger down
6943 processId(mapper, 1);
6944 processPosition(mapper, x3, y3);
6945 processSync(mapper);
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6949}
6950
6951/**
6952 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6953 * UP events should be ignored.
6954 */
6955TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08006956 addConfigurationProperty("touch.deviceType", "touchScreen");
6957 prepareDisplay(DISPLAY_ORIENTATION_0);
6958 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08006960
6961 NotifyMotionArgs motionArgs;
6962
6963 // default tool type is finger
6964 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6965 processId(mapper, 1);
6966 processPosition(mapper, x1, y1);
6967 processSync(mapper);
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6969 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6971
6972 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6973 processToolType(mapper, MT_TOOL_PALM);
6974 processSync(mapper);
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6976 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6977
6978 // Ignore the following MOVE and UP events if had detect a palm event.
6979 processId(mapper, 1);
6980 processPosition(mapper, x2, y2);
6981 processSync(mapper);
6982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6983
6984 // finger up.
6985 processId(mapper, -1);
6986 processSync(mapper);
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6988
6989 // new finger down
6990 processToolType(mapper, MT_TOOL_FINGER);
6991 processId(mapper, 1);
6992 processPosition(mapper, x3, y3);
6993 processSync(mapper);
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6995 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6997}
6998
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006999// --- MultiTouchInputMapperTest_ExternalDevice ---
7000
7001class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7002protected:
7003 virtual void SetUp() override {
7004 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7005 }
7006};
7007
7008/**
7009 * Expect fallback to internal viewport if device is external and external viewport is not present.
7010 */
7011TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7012 prepareAxes(POSITION);
7013 addConfigurationProperty("touch.deviceType", "touchScreen");
7014 prepareDisplay(DISPLAY_ORIENTATION_0);
7015 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7016
7017 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7018
7019 NotifyMotionArgs motionArgs;
7020
7021 // Expect the event to be sent to the internal viewport,
7022 // because an external viewport is not present.
7023 processPosition(mapper, 100, 100);
7024 processSync(mapper);
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7026 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7027
7028 // Expect the event to be sent to the external viewport if it is present.
7029 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
7030 processPosition(mapper, 100, 100);
7031 processSync(mapper);
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7033 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7034}
7035
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036} // namespace android